LLVM 22.0.0git
SPIRVLegalizePointerCast.cpp
Go to the documentation of this file.
1//===-- SPIRVLegalizePointerCast.cpp ----------------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// The LLVM IR has multiple legal patterns we cannot lower to Logical SPIR-V.
10// This pass modifies such loads to have an IR we can directly lower to valid
11// logical SPIR-V.
12// OpenCL can avoid this because they rely on ptrcast, which is not supported
13// by logical SPIR-V.
14//
15// This pass relies on the assign_ptr_type intrinsic to deduce the type of the
16// pointed values, must replace all occurences of `ptrcast`. This is why
17// unhandled cases are reported as unreachable: we MUST cover all cases.
18//
19// 1. Loading the first element of an array
20//
21// %array = [10 x i32]
22// %value = load i32, ptr %array
23//
24// LLVM can skip the GEP instruction, and only request loading the first 4
25// bytes. In logical SPIR-V, we need an OpAccessChain to access the first
26// element. This pass will add a getelementptr instruction before the load.
27//
28//
29// 2. Implicit downcast from load
30//
31// %1 = getelementptr <4 x i32>, ptr %vec4, i64 0
32// %2 = load <3 x i32>, ptr %1
33//
34// The pointer in the GEP instruction is only used for offset computations,
35// but it doesn't NEED to match the pointed type. OpAccessChain however
36// requires this. Also, LLVM loads define the bitwidth of the load, not the
37// pointer. In this example, we can guess %vec4 is a vec4 thanks to the GEP
38// instruction basetype, but we only want to load the first 3 elements, hence
39// do a partial load. In logical SPIR-V, this is not legal. What we must do
40// is load the full vector (basetype), extract 3 elements, and recombine them
41// to form a 3-element vector.
42//
43//===----------------------------------------------------------------------===//
44
45#include "SPIRV.h"
46#include "SPIRVSubtarget.h"
47#include "SPIRVTargetMachine.h"
48#include "SPIRVUtils.h"
49#include "llvm/IR/IRBuilder.h"
51#include "llvm/IR/Intrinsics.h"
52#include "llvm/IR/IntrinsicsSPIRV.h"
55
56using namespace llvm;
57
58namespace {
59class SPIRVLegalizePointerCast : public FunctionPass {
60
61 // Builds the `spv_assign_type` assigning |Ty| to |Value| at the current
62 // builder position.
63 void buildAssignType(IRBuilder<> &B, Type *Ty, Value *Arg) {
64 Value *OfType = PoisonValue::get(Ty);
65 CallInst *AssignCI = buildIntrWithMD(Intrinsic::spv_assign_type,
66 {Arg->getType()}, OfType, Arg, {}, B);
67 GR->addAssignPtrTypeInstr(Arg, AssignCI);
68 }
69
70 // Loads parts of the vector of type |SourceType| from the pointer |Source|
71 // and create a new vector of type |TargetType|. |TargetType| must be a vector
72 // type, and element types of |TargetType| and |SourceType| must match.
73 // Returns the loaded value.
74 Value *loadVectorFromVector(IRBuilder<> &B, FixedVectorType *SourceType,
75 FixedVectorType *TargetType, Value *Source) {
76 assert(TargetType->getNumElements() <= SourceType->getNumElements());
77 LoadInst *NewLoad = B.CreateLoad(SourceType, Source);
78 buildAssignType(B, SourceType, NewLoad);
79 Value *AssignValue = NewLoad;
80 if (TargetType->getElementType() != SourceType->getElementType()) {
81 AssignValue = B.CreateIntrinsic(Intrinsic::spv_bitcast,
82 {TargetType, SourceType}, {NewLoad});
83 buildAssignType(B, TargetType, AssignValue);
84 }
85
86 SmallVector<int> Mask(/* Size= */ TargetType->getNumElements());
87 for (unsigned I = 0; I < TargetType->getNumElements(); ++I)
88 Mask[I] = I;
89 Value *Output = B.CreateShuffleVector(AssignValue, AssignValue, Mask);
90 buildAssignType(B, TargetType, Output);
91 return Output;
92 }
93
94 // Loads the first value in an aggregate pointed by |Source| of containing
95 // elements of type |ElementType|. Load flags will be copied from |BadLoad|,
96 // which should be the load being legalized. Returns the loaded value.
97 Value *loadFirstValueFromAggregate(IRBuilder<> &B, Type *ElementType,
98 Value *Source, LoadInst *BadLoad) {
100 BadLoad->getPointerOperandType()};
101 SmallVector<Value *, 3> Args{/* isInBounds= */ B.getInt1(false), Source,
102 B.getInt32(0), B.getInt32(0)};
103 auto *GEP = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
104 GR->buildAssignPtr(B, ElementType, GEP);
105
106 LoadInst *LI = B.CreateLoad(ElementType, GEP);
107 LI->setAlignment(BadLoad->getAlign());
108 buildAssignType(B, ElementType, LI);
109 return LI;
110 }
111
112 // Replaces the load instruction to get rid of the ptrcast used as source
113 // operand.
114 void transformLoad(IRBuilder<> &B, LoadInst *LI, Value *CastedOperand,
115 Value *OriginalOperand) {
116 Type *FromTy = GR->findDeducedElementType(OriginalOperand);
117 Type *ToTy = GR->findDeducedElementType(CastedOperand);
118 Value *Output = nullptr;
119
120 auto *SAT = dyn_cast<ArrayType>(FromTy);
121 auto *SVT = dyn_cast<FixedVectorType>(FromTy);
122 auto *SST = dyn_cast<StructType>(FromTy);
123 auto *DVT = dyn_cast<FixedVectorType>(ToTy);
124
125 B.SetInsertPoint(LI);
126
127 // Destination is the element type of Source, and source is an array ->
128 // Loading 1st element.
129 // - float a = array[0];
130 if (SAT && SAT->getElementType() == ToTy)
131 Output = loadFirstValueFromAggregate(B, SAT->getElementType(),
132 OriginalOperand, LI);
133 // Destination is the element type of Source, and source is a vector ->
134 // Vector to scalar.
135 // - float a = vector.x;
136 else if (!DVT && SVT && SVT->getElementType() == ToTy) {
137 Output = loadFirstValueFromAggregate(B, SVT->getElementType(),
138 OriginalOperand, LI);
139 }
140 // Destination is a smaller vector than source or different vector type.
141 // - float3 v3 = vector4;
142 // - float4 v2 = int4;
143 else if (SVT && DVT)
144 Output = loadVectorFromVector(B, SVT, DVT, OriginalOperand);
145 // Destination is the scalar type stored at the start of an aggregate.
146 // - struct S { float m };
147 // - float v = s.m;
148 else if (SST && SST->getTypeAtIndex(0u) == ToTy)
149 Output = loadFirstValueFromAggregate(B, ToTy, OriginalOperand, LI);
150 else
151 llvm_unreachable("Unimplemented implicit down-cast from load.");
152
153 GR->replaceAllUsesWith(LI, Output, /* DeleteOld= */ true);
154 DeadInstructions.push_back(LI);
155 }
156
157 // Creates an spv_insertelt instruction (equivalent to llvm's insertelement).
158 Value *makeInsertElement(IRBuilder<> &B, Value *Vector, Value *Element,
159 unsigned Index) {
160 Type *Int32Ty = Type::getInt32Ty(B.getContext());
161 SmallVector<Type *, 4> Types = {Vector->getType(), Vector->getType(),
162 Element->getType(), Int32Ty};
163 SmallVector<Value *> Args = {Vector, Element, B.getInt32(Index)};
164 Instruction *NewI =
165 B.CreateIntrinsic(Intrinsic::spv_insertelt, {Types}, {Args});
166 buildAssignType(B, Vector->getType(), NewI);
167 return NewI;
168 }
169
170 // Creates an spv_extractelt instruction (equivalent to llvm's
171 // extractelement).
172 Value *makeExtractElement(IRBuilder<> &B, Type *ElementType, Value *Vector,
173 unsigned Index) {
174 Type *Int32Ty = Type::getInt32Ty(B.getContext());
176 SmallVector<Value *> Args = {Vector, B.getInt32(Index)};
177 Instruction *NewI =
178 B.CreateIntrinsic(Intrinsic::spv_extractelt, {Types}, {Args});
179 buildAssignType(B, ElementType, NewI);
180 return NewI;
181 }
182
183 // Stores the given Src vector operand into the Dst vector, adjusting the size
184 // if required.
185 Value *storeVectorFromVector(IRBuilder<> &B, Value *Src, Value *Dst,
186 Align Alignment) {
187 FixedVectorType *SrcType = cast<FixedVectorType>(Src->getType());
188 FixedVectorType *DstType =
189 cast<FixedVectorType>(GR->findDeducedElementType(Dst));
190 auto dstNumElements = DstType->getNumElements();
191 auto srcNumElements = SrcType->getNumElements();
192
193 // if the element type differs, it is a bitcast.
194 if (DstType->getElementType() != SrcType->getElementType()) {
195 // Support bitcast between vectors of different sizes only if
196 // the total bitwidth is the same.
197 [[maybe_unused]] auto dstBitWidth =
198 DstType->getElementType()->getScalarSizeInBits() * dstNumElements;
199 [[maybe_unused]] auto srcBitWidth =
200 SrcType->getElementType()->getScalarSizeInBits() * srcNumElements;
201 assert(dstBitWidth == srcBitWidth &&
202 "Unsupported bitcast between vectors of different sizes.");
203
204 Src =
205 B.CreateIntrinsic(Intrinsic::spv_bitcast, {DstType, SrcType}, {Src});
206 buildAssignType(B, DstType, Src);
207 SrcType = DstType;
208
209 StoreInst *SI = B.CreateStore(Src, Dst);
210 SI->setAlignment(Alignment);
211 return SI;
212 }
213
214 assert(DstType->getNumElements() >= SrcType->getNumElements());
215 LoadInst *LI = B.CreateLoad(DstType, Dst);
216 LI->setAlignment(Alignment);
217 Value *OldValues = LI;
218 buildAssignType(B, OldValues->getType(), OldValues);
219 Value *NewValues = Src;
220
221 for (unsigned I = 0; I < SrcType->getNumElements(); ++I) {
222 Value *Element =
223 makeExtractElement(B, SrcType->getElementType(), NewValues, I);
224 OldValues = makeInsertElement(B, OldValues, Element, I);
225 }
226
227 StoreInst *SI = B.CreateStore(OldValues, Dst);
228 SI->setAlignment(Alignment);
229 return SI;
230 }
231
232 void buildGEPIndexChain(IRBuilder<> &B, Type *Search, Type *Aggregate,
233 SmallVectorImpl<Value *> &Indices) {
234 Indices.push_back(B.getInt32(0));
235
236 if (Search == Aggregate)
237 return;
238
239 if (auto *ST = dyn_cast<StructType>(Aggregate))
240 buildGEPIndexChain(B, Search, ST->getTypeAtIndex(0u), Indices);
241 else if (auto *AT = dyn_cast<ArrayType>(Aggregate))
242 buildGEPIndexChain(B, Search, AT->getElementType(), Indices);
243 else if (auto *VT = dyn_cast<FixedVectorType>(Aggregate))
244 buildGEPIndexChain(B, Search, VT->getElementType(), Indices);
245 else
246 llvm_unreachable("Bad access chain?");
247 }
248
249 // Stores the given Src value into the first entry of the Dst aggregate.
250 Value *storeToFirstValueAggregate(IRBuilder<> &B, Value *Src, Value *Dst,
251 Type *DstPointeeType, Align Alignment) {
252 SmallVector<Type *, 2> Types = {Dst->getType(), Dst->getType()};
253 SmallVector<Value *, 3> Args{/* isInBounds= */ B.getInt1(true), Dst};
254 buildGEPIndexChain(B, Src->getType(), DstPointeeType, Args);
255 auto *GEP = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
256 GR->buildAssignPtr(B, Src->getType(), GEP);
257 StoreInst *SI = B.CreateStore(Src, GEP);
258 SI->setAlignment(Alignment);
259 return SI;
260 }
261
262 bool isTypeFirstElementAggregate(Type *Search, Type *Aggregate) {
263 if (Search == Aggregate)
264 return true;
265 if (auto *ST = dyn_cast<StructType>(Aggregate))
266 return isTypeFirstElementAggregate(Search, ST->getTypeAtIndex(0u));
267 if (auto *VT = dyn_cast<FixedVectorType>(Aggregate))
268 return isTypeFirstElementAggregate(Search, VT->getElementType());
269 if (auto *AT = dyn_cast<ArrayType>(Aggregate))
270 return isTypeFirstElementAggregate(Search, AT->getElementType());
271 return false;
272 }
273
274 // Transforms a store instruction (or SPV intrinsic) using a ptrcast as
275 // operand into a valid logical SPIR-V store with no ptrcast.
276 void transformStore(IRBuilder<> &B, Instruction *BadStore, Value *Src,
277 Value *Dst, Align Alignment) {
278 Type *ToTy = GR->findDeducedElementType(Dst);
279 Type *FromTy = Src->getType();
280
281 auto *S_VT = dyn_cast<FixedVectorType>(FromTy);
282 auto *D_ST = dyn_cast<StructType>(ToTy);
283 auto *D_VT = dyn_cast<FixedVectorType>(ToTy);
284
285 B.SetInsertPoint(BadStore);
286 if (D_ST && isTypeFirstElementAggregate(FromTy, D_ST))
287 storeToFirstValueAggregate(B, Src, Dst, D_ST, Alignment);
288 else if (D_VT && S_VT)
289 storeVectorFromVector(B, Src, Dst, Alignment);
290 else if (D_VT && !S_VT && FromTy == D_VT->getElementType())
291 storeToFirstValueAggregate(B, Src, Dst, D_VT, Alignment);
292 else
293 llvm_unreachable("Unsupported ptrcast use in store. Please fix.");
294
295 DeadInstructions.push_back(BadStore);
296 }
297
298 void legalizePointerCast(IntrinsicInst *II) {
299 Value *CastedOperand = II;
300 Value *OriginalOperand = II->getOperand(0);
301
302 IRBuilder<> B(II->getContext());
303 std::vector<Value *> Users;
304 for (Use &U : II->uses())
305 Users.push_back(U.getUser());
306
307 for (Value *User : Users) {
308 if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
309 transformLoad(B, LI, CastedOperand, OriginalOperand);
310 continue;
311 }
312
313 if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
314 transformStore(B, SI, SI->getValueOperand(), OriginalOperand,
315 SI->getAlign());
316 continue;
317 }
318
319 if (IntrinsicInst *Intrin = dyn_cast<IntrinsicInst>(User)) {
320 if (Intrin->getIntrinsicID() == Intrinsic::spv_assign_ptr_type) {
321 DeadInstructions.push_back(Intrin);
322 continue;
323 }
324
325 if (Intrin->getIntrinsicID() == Intrinsic::spv_gep) {
326 GR->replaceAllUsesWith(CastedOperand, OriginalOperand,
327 /* DeleteOld= */ false);
328 continue;
329 }
330
331 if (Intrin->getIntrinsicID() == Intrinsic::spv_store) {
332 Align Alignment;
333 if (ConstantInt *C = dyn_cast<ConstantInt>(Intrin->getOperand(3)))
334 Alignment = Align(C->getZExtValue());
335 transformStore(B, Intrin, Intrin->getArgOperand(0), OriginalOperand,
336 Alignment);
337 continue;
338 }
339 }
340
341 llvm_unreachable("Unsupported ptrcast user. Please fix.");
342 }
343
344 DeadInstructions.push_back(II);
345 }
346
347public:
348 SPIRVLegalizePointerCast(SPIRVTargetMachine *TM) : FunctionPass(ID), TM(TM) {}
349
350 virtual bool runOnFunction(Function &F) override {
351 const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(F);
352 GR = ST.getSPIRVGlobalRegistry();
353 DeadInstructions.clear();
354
355 std::vector<IntrinsicInst *> WorkList;
356 for (auto &BB : F) {
357 for (auto &I : BB) {
358 auto *II = dyn_cast<IntrinsicInst>(&I);
359 if (II && II->getIntrinsicID() == Intrinsic::spv_ptrcast)
360 WorkList.push_back(II);
361 }
362 }
363
364 for (IntrinsicInst *II : WorkList)
365 legalizePointerCast(II);
366
367 for (Instruction *I : DeadInstructions)
368 I->eraseFromParent();
369
370 return DeadInstructions.size() != 0;
371 }
372
373private:
374 SPIRVTargetMachine *TM = nullptr;
375 SPIRVGlobalRegistry *GR = nullptr;
376 std::vector<Instruction *> DeadInstructions;
377
378public:
379 static char ID;
380};
381} // namespace
382
383char SPIRVLegalizePointerCast::ID = 0;
384INITIALIZE_PASS(SPIRVLegalizePointerCast, "spirv-legalize-bitcast",
385 "SPIRV legalize bitcast pass", false, false)
386
388 return new SPIRVLegalizePointerCast(TM);
389}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static bool runOnFunction(Function &F, bool PostInlining)
Hexagon Common GEP
iv Induction Variable Users
Definition IVUsers.cpp:48
#define F(x, y, z)
Definition MD5.cpp:55
#define I(x, y, z)
Definition MD5.cpp:58
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
unsigned getNumElements() const
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:314
void setAlignment(Align Align)
Type * getPointerOperandType() const
Align getAlign() const
Return the alignment of the access that is being performed.
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
void push_back(const T &Elt)
LLVM_ABI unsigned getScalarSizeInBits() const LLVM_READONLY
If this is a vector type, return the getPrimitiveSizeInBits value for the element type.
Definition Type.cpp:231
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
Type * getElementType() const
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Align[]
Key for Kernel::Arg::Metadata::mAlign.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
ElementType
The element type of an SRV or UAV resource.
Definition DXILABI.h:60
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
CallInst * buildIntrWithMD(Intrinsic::ID IntrID, ArrayRef< Type * > Types, Value *Arg, Value *Arg2, ArrayRef< Constant * > Imms, IRBuilder<> &B)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
FunctionPass * createSPIRVLegalizePointerCastPass(SPIRVTargetMachine *TM)