LLVM 23.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 static FixedVectorType *makeVectorFromTotalBits(Type *ElemTy,
71 TypeSize TotalBits) {
72 unsigned ElemBits = ElemTy->getScalarSizeInBits();
73 assert(ElemBits && TotalBits % ElemBits == 0 &&
74 "TotalBits must be divisible by element bit size");
75 return FixedVectorType::get(ElemTy, TotalBits / ElemBits);
76 }
77
78 Value *resizeVectorBitsWithShuffle(IRBuilder<> &B, Value *V,
79 FixedVectorType *DstTy) {
80 auto *SrcTy = cast<FixedVectorType>(V->getType());
81 assert(SrcTy->getElementType() == DstTy->getElementType() &&
82 "shuffle resize expects identical element types");
83
84 const unsigned NumNeeded = DstTy->getNumElements();
85 const unsigned NumSource = SrcTy->getNumElements();
86
87 SmallVector<int> Mask(NumNeeded);
88 for (unsigned I = 0; I < NumNeeded; ++I)
89 Mask[I] = (I < NumSource) ? static_cast<int>(I) : -1;
90
91 Value *Resized = B.CreateShuffleVector(V, V, Mask);
92 buildAssignType(B, DstTy, Resized);
93 return Resized;
94 }
95
96 // Loads parts of the vector of type |SourceType| from the pointer |Source|
97 // and create a new vector of type |TargetType|. |TargetType| must be a vector
98 // type.
99 // Returns the loaded value.
100 Value *loadVectorFromVector(IRBuilder<> &B, FixedVectorType *SourceType,
101 FixedVectorType *TargetType, Value *Source) {
102 LoadInst *NewLoad = B.CreateLoad(SourceType, Source);
103 buildAssignType(B, SourceType, NewLoad);
104 Value *AssignValue = NewLoad;
105 if (TargetType->getElementType() != SourceType->getElementType()) {
106 const DataLayout &DL = B.GetInsertBlock()->getModule()->getDataLayout();
107 TypeSize TargetTypeSize = DL.getTypeSizeInBits(TargetType);
108 TypeSize SourceTypeSize = DL.getTypeSizeInBits(SourceType);
109
110 Value *BitcastSrcVal = NewLoad;
111 FixedVectorType *BitcastSrcTy =
112 cast<FixedVectorType>(BitcastSrcVal->getType());
113 FixedVectorType *BitcastDstTy = TargetType;
114
115 if (TargetTypeSize != SourceTypeSize) {
116 unsigned TargetElemBits =
117 TargetType->getElementType()->getScalarSizeInBits();
118 if (SourceTypeSize % TargetElemBits == 0) {
119 // No Resize needed. Same total bits as source, but use target element
120 // type.
121 BitcastDstTy = makeVectorFromTotalBits(TargetType->getElementType(),
122 SourceTypeSize);
123 } else {
124 // Resize source to target total bitwidth using source element type.
125 BitcastSrcTy = makeVectorFromTotalBits(SourceType->getElementType(),
126 TargetTypeSize);
127 BitcastSrcVal = resizeVectorBitsWithShuffle(B, NewLoad, BitcastSrcTy);
128 }
129 }
130 AssignValue =
131 B.CreateIntrinsic(Intrinsic::spv_bitcast,
132 {BitcastDstTy, BitcastSrcTy}, {BitcastSrcVal});
133 buildAssignType(B, BitcastDstTy, AssignValue);
134 if (BitcastDstTy == TargetType)
135 return AssignValue;
136 }
137
138 assert(TargetType->getNumElements() < SourceType->getNumElements());
139 SmallVector<int> Mask(/* Size= */ TargetType->getNumElements());
140 for (unsigned I = 0; I < TargetType->getNumElements(); ++I)
141 Mask[I] = I;
142 Value *Output = B.CreateShuffleVector(AssignValue, AssignValue, Mask);
143 buildAssignType(B, TargetType, Output);
144 return Output;
145 }
146
147 // Loads the first value in an aggregate pointed by |Source| of containing
148 // elements of type |ElementType|. Load flags will be copied from |BadLoad|,
149 // which should be the load being legalized. Returns the loaded value.
150 Value *loadFirstValueFromAggregate(IRBuilder<> &B, Type *ElementType,
151 Value *Source, LoadInst *BadLoad) {
152 std::array<Type *, 2> Types = {BadLoad->getPointerOperandType(),
153 Source->getType()};
154 SmallVector<Value *, 8> Args{/* isInBounds= */ B.getInt1(false), Source};
155
156 Type *AggregateType = GR->findDeducedElementType(Source);
157 assert(AggregateType && "Could not deduce aggregate type");
158 buildGEPIndexChain(B, ElementType, AggregateType, Args);
159
160 auto *GEP = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
161 GR->buildAssignPtr(B, ElementType, GEP);
162
163 LoadInst *LI = B.CreateLoad(ElementType, GEP);
164 LI->setAlignment(BadLoad->getAlign());
165 buildAssignType(B, ElementType, LI);
166 return LI;
167 }
168 Value *
169 buildVectorFromLoadedElements(IRBuilder<> &B, FixedVectorType *TargetType,
170 SmallVector<Value *, 4> &LoadedElements) {
171 // Build the vector from the loaded elements.
172 Value *NewVector = PoisonValue::get(TargetType);
173 buildAssignType(B, TargetType, NewVector);
174
175 for (unsigned I = 0, E = TargetType->getNumElements(); I < E; ++I) {
176 Value *Index = B.getInt32(I);
177 SmallVector<Type *, 4> Types = {TargetType, TargetType,
178 TargetType->getElementType(),
179 Index->getType()};
180 SmallVector<Value *> Args = {NewVector, LoadedElements[I], Index};
181 NewVector = B.CreateIntrinsic(Intrinsic::spv_insertelt, {Types}, {Args});
182 buildAssignType(B, TargetType, NewVector);
183 }
184 return NewVector;
185 }
186
187 // Loads elements from a matrix with an array of vector memory layout and
188 // constructs a vector.
189 Value *loadVectorFromMatrixArray(IRBuilder<> &B, FixedVectorType *TargetType,
190 Value *Source,
191 FixedVectorType *ArrElemVecTy) {
192 Type *TargetElemTy = TargetType->getElementType();
193 unsigned ScalarsPerArrayElement = ArrElemVecTy->getNumElements();
194 // Load each element of the array.
195 SmallVector<Value *, 4> LoadedElements;
196 std::array<Type *, 2> Types = {Source->getType(), Source->getType()};
197 for (unsigned I = 0, E = TargetType->getNumElements(); I < E; ++I) {
198 unsigned ArrayIndex = I / ScalarsPerArrayElement;
199 unsigned ElementIndexInArrayElem = I % ScalarsPerArrayElement;
200 // Create a GEP to access the i-th element of the array.
201 std::array<Value *, 4> Args = {
202 B.getInt1(/*Inbounds=*/false), Source, B.getInt32(0),
203 ConstantInt::get(B.getInt32Ty(), ArrayIndex)};
204 auto *ElementPtr = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
205 GR->buildAssignPtr(B, ArrElemVecTy, ElementPtr);
206 Value *LoadVec = B.CreateLoad(ArrElemVecTy, ElementPtr);
207 buildAssignType(B, ArrElemVecTy, LoadVec);
208 LoadedElements.push_back(makeExtractElement(B, TargetElemTy, LoadVec,
209 ElementIndexInArrayElem));
210 }
211 return buildVectorFromLoadedElements(B, TargetType, LoadedElements);
212 }
213 // Loads elements from an array and constructs a vector.
214 Value *loadVectorFromArray(IRBuilder<> &B, FixedVectorType *TargetType,
215 Value *Source) {
216 // Load each element of the array.
217 SmallVector<Value *, 4> LoadedElements;
218 std::array<Type *, 2> Types = {Source->getType(), Source->getType()};
219 for (unsigned I = 0, E = TargetType->getNumElements(); I < E; ++I) {
220 // Create a GEP to access the i-th element of the array.
221 std::array<Value *, 4> Args = {B.getInt1(/*Inbounds=*/false), Source,
222 B.getInt32(0),
223 ConstantInt::get(B.getInt32Ty(), I)};
224 auto *ElementPtr = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
225 GR->buildAssignPtr(B, TargetType->getElementType(), ElementPtr);
226
227 // Load the value from the element pointer.
228 Value *Load = B.CreateLoad(TargetType->getElementType(), ElementPtr);
229 buildAssignType(B, TargetType->getElementType(), Load);
230 LoadedElements.push_back(Load);
231 }
232 return buildVectorFromLoadedElements(B, TargetType, LoadedElements);
233 }
234
235 // Stores elements from a vector into a matrix (an array of vectors).
236 void storeMatrixArrayFromVector(IRBuilder<> &B, Value *SrcVector,
237 Value *DstArrayPtr, ArrayType *ArrTy,
238 Align Alignment) {
239 auto *SrcVecTy = cast<FixedVectorType>(SrcVector->getType());
240 auto *ArrElemVecTy = cast<FixedVectorType>(ArrTy->getElementType());
241 Type *ElemTy = ArrElemVecTy->getElementType();
242 unsigned ScalarsPerArrayElement = ArrElemVecTy->getNumElements();
243 unsigned SrcNumElements = SrcVecTy->getNumElements();
244 assert(
245 SrcNumElements % ScalarsPerArrayElement == 0 &&
246 "Source vector size must be a multiple of array element vector size");
247
248 std::array<Type *, 2> Types = {DstArrayPtr->getType(),
249 DstArrayPtr->getType()};
250
251 for (unsigned I = 0; I < SrcNumElements; I += ScalarsPerArrayElement) {
252 unsigned ArrayIndex = I / ScalarsPerArrayElement;
253 // Create a GEP to access the array element.
254 std::array<Value *, 4> Args = {
255 B.getInt1(/*Inbounds=*/false), DstArrayPtr, B.getInt32(0),
256 ConstantInt::get(B.getInt32Ty(), ArrayIndex)};
257 auto *ElementPtr = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
258 GR->buildAssignPtr(B, ArrElemVecTy, ElementPtr);
259
260 // Extract scalar elements from the source vector for this array slot.
261 SmallVector<Value *, 4> Elements;
262 for (unsigned J = 0; J < ScalarsPerArrayElement; ++J)
263 Elements.push_back(makeExtractElement(B, ElemTy, SrcVector, I + J));
264
265 // Build a vector from the extracted elements and store it.
266 Value *Vec = buildVectorFromLoadedElements(B, ArrElemVecTy, Elements);
267 StoreInst *SI = B.CreateStore(Vec, ElementPtr);
268 SI->setAlignment(Alignment);
269 }
270 }
271
272 // Stores elements from a vector into an array.
273 void storeArrayFromVector(IRBuilder<> &B, Value *SrcVector,
274 Value *DstArrayPtr, ArrayType *ArrTy,
275 Align Alignment) {
276 auto *VecTy = cast<FixedVectorType>(SrcVector->getType());
277 Type *ElemTy = ArrTy->getElementType();
278
279 // Ensure the element types of the array and vector are the same.
280 assert(VecTy->getElementType() == ElemTy &&
281 "Element types of array and vector must be the same.");
282 std::array<Type *, 2> Types = {DstArrayPtr->getType(),
283 DstArrayPtr->getType()};
284
285 for (unsigned I = 0, E = VecTy->getNumElements(); I < E; ++I) {
286 // Create a GEP to access the i-th element of the array.
287 std::array<Value *, 4> Args = {B.getInt1(/*Inbounds=*/false), DstArrayPtr,
288 B.getInt32(0),
289 ConstantInt::get(B.getInt32Ty(), I)};
290 auto *ElementPtr = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
291 GR->buildAssignPtr(B, ElemTy, ElementPtr);
292
293 // Extract the element from the vector and store it.
294 Value *Element = makeExtractElement(B, ElemTy, SrcVector, I);
295 StoreInst *SI = B.CreateStore(Element, ElementPtr);
296 SI->setAlignment(Alignment);
297 }
298 }
299
300 // Replaces the load instruction to get rid of the ptrcast used as source
301 // operand.
302 void transformLoad(IRBuilder<> &B, LoadInst *LI, Value *CastedOperand,
303 Value *OriginalOperand) {
304 Type *FromTy = GR->findDeducedElementType(OriginalOperand);
305 Type *ToTy = GR->findDeducedElementType(CastedOperand);
306 Value *Output = nullptr;
307
308 auto *SAT = dyn_cast<ArrayType>(FromTy);
309 auto *SVT = dyn_cast<FixedVectorType>(FromTy);
310 auto *DVT = dyn_cast<FixedVectorType>(ToTy);
311 auto *MAT =
312 SAT ? dyn_cast<FixedVectorType>(SAT->getElementType()) : nullptr;
313
314 B.SetInsertPoint(LI);
315
316 // Destination is the element type of some member of FromTy. For example,
317 // loading the 1st element of an array:
318 // - float a = array[0];
319 if (isTypeFirstElementAggregate(ToTy, FromTy))
320 Output = loadFirstValueFromAggregate(B, ToTy, OriginalOperand, LI);
321 // Destination is a smaller vector than source or different vector type.
322 // - float3 v3 = vector4;
323 // - float4 v2 = int4;
324 else if (SVT && DVT)
325 Output = loadVectorFromVector(B, SVT, DVT, OriginalOperand);
326 else if (SAT && DVT && SAT->getElementType() == DVT->getElementType())
327 Output = loadVectorFromArray(B, DVT, OriginalOperand);
328 else if (MAT && DVT && MAT->getElementType() == DVT->getElementType())
329 Output = loadVectorFromMatrixArray(B, DVT, OriginalOperand, MAT);
330 else
331 llvm_unreachable("Unimplemented implicit down-cast from load.");
332
333 GR->replaceAllUsesWith(LI, Output, /* DeleteOld= */ true);
334 DeadInstructions.push_back(LI);
335 }
336
337 // Creates an spv_insertelt instruction (equivalent to llvm's insertelement).
338 Value *makeInsertElement(IRBuilder<> &B, Value *Vector, Value *Element,
339 unsigned Index) {
340 Type *Int32Ty = Type::getInt32Ty(B.getContext());
341 SmallVector<Type *, 4> Types = {Vector->getType(), Vector->getType(),
342 Element->getType(), Int32Ty};
343 SmallVector<Value *> Args = {Vector, Element, B.getInt32(Index)};
344 Instruction *NewI =
345 B.CreateIntrinsic(Intrinsic::spv_insertelt, {Types}, {Args});
346 buildAssignType(B, Vector->getType(), NewI);
347 return NewI;
348 }
349
350 // Creates an spv_extractelt instruction (equivalent to llvm's
351 // extractelement).
352 Value *makeExtractElement(IRBuilder<> &B, Type *ElementType, Value *Vector,
353 unsigned Index) {
354 Type *Int32Ty = Type::getInt32Ty(B.getContext());
356 SmallVector<Value *> Args = {Vector, B.getInt32(Index)};
357 Instruction *NewI =
358 B.CreateIntrinsic(Intrinsic::spv_extractelt, {Types}, {Args});
359 buildAssignType(B, ElementType, NewI);
360 return NewI;
361 }
362
363 // Stores the given Src vector operand into the Dst vector, adjusting the size
364 // if required.
365 Value *storeVectorFromVector(IRBuilder<> &B, Value *Src, Value *Dst,
366 Align Alignment) {
367 FixedVectorType *SrcType = cast<FixedVectorType>(Src->getType());
368 FixedVectorType *DstType =
369 cast<FixedVectorType>(GR->findDeducedElementType(Dst));
370 auto dstNumElements = DstType->getNumElements();
371 auto srcNumElements = SrcType->getNumElements();
372
373 // if the element type differs, it is a bitcast.
374 if (DstType->getElementType() != SrcType->getElementType()) {
375 // Support bitcast between vectors of different sizes only if
376 // the total bitwidth is the same.
377 [[maybe_unused]] auto dstBitWidth =
378 DstType->getElementType()->getScalarSizeInBits() * dstNumElements;
379 [[maybe_unused]] auto srcBitWidth =
380 SrcType->getElementType()->getScalarSizeInBits() * srcNumElements;
381 assert(dstBitWidth == srcBitWidth &&
382 "Unsupported bitcast between vectors of different sizes.");
383
384 Src =
385 B.CreateIntrinsic(Intrinsic::spv_bitcast, {DstType, SrcType}, {Src});
386 buildAssignType(B, DstType, Src);
387 SrcType = DstType;
388
389 StoreInst *SI = B.CreateStore(Src, Dst);
390 SI->setAlignment(Alignment);
391 return SI;
392 }
393
394 assert(DstType->getNumElements() >= SrcType->getNumElements());
395 LoadInst *LI = B.CreateLoad(DstType, Dst);
396 LI->setAlignment(Alignment);
397 Value *OldValues = LI;
398 buildAssignType(B, OldValues->getType(), OldValues);
399 Value *NewValues = Src;
400
401 for (unsigned I = 0; I < SrcType->getNumElements(); ++I) {
402 Value *Element =
403 makeExtractElement(B, SrcType->getElementType(), NewValues, I);
404 OldValues = makeInsertElement(B, OldValues, Element, I);
405 }
406
407 StoreInst *SI = B.CreateStore(OldValues, Dst);
408 SI->setAlignment(Alignment);
409 return SI;
410 }
411
412 void buildGEPIndexChain(IRBuilder<> &B, Type *Search, Type *Aggregate,
413 SmallVectorImpl<Value *> &Indices) {
414 Indices.push_back(B.getInt32(0));
415
416 if (Search == Aggregate)
417 return;
418
419 if (auto *ST = dyn_cast<StructType>(Aggregate))
420 buildGEPIndexChain(B, Search, ST->getTypeAtIndex(0u), Indices);
421 else if (auto *AT = dyn_cast<ArrayType>(Aggregate))
422 buildGEPIndexChain(B, Search, AT->getElementType(), Indices);
423 else if (auto *VT = dyn_cast<FixedVectorType>(Aggregate))
424 buildGEPIndexChain(B, Search, VT->getElementType(), Indices);
425 else
426 llvm_unreachable("Bad access chain?");
427 }
428
429 // Stores the given Src value into the first entry of the Dst aggregate.
430 Value *storeToFirstValueAggregate(IRBuilder<> &B, Value *Src, Value *Dst,
431 Type *DstPointeeType, Align Alignment) {
432 std::array<Type *, 2> Types = {Dst->getType(), Dst->getType()};
433 SmallVector<Value *, 8> Args{/* isInBounds= */ B.getInt1(true), Dst};
434 buildGEPIndexChain(B, Src->getType(), DstPointeeType, Args);
435 auto *GEP = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
436 GR->buildAssignPtr(B, Src->getType(), GEP);
437 StoreInst *SI = B.CreateStore(Src, GEP);
438 SI->setAlignment(Alignment);
439 return SI;
440 }
441
442 bool isTypeFirstElementAggregate(Type *Search, Type *Aggregate) {
443 if (Search == Aggregate)
444 return true;
445 if (auto *ST = dyn_cast<StructType>(Aggregate))
446 return isTypeFirstElementAggregate(Search, ST->getTypeAtIndex(0u));
447 if (auto *VT = dyn_cast<FixedVectorType>(Aggregate))
448 return isTypeFirstElementAggregate(Search, VT->getElementType());
449 if (auto *AT = dyn_cast<ArrayType>(Aggregate))
450 return isTypeFirstElementAggregate(Search, AT->getElementType());
451 return false;
452 }
453
454 // Transforms a store instruction (or SPV intrinsic) using a ptrcast as
455 // operand into a valid logical SPIR-V store with no ptrcast.
456 void transformStore(IRBuilder<> &B, Instruction *BadStore, Value *Src,
457 Value *Dst, Align Alignment) {
458 Type *ToTy = GR->findDeducedElementType(Dst);
459 Type *FromTy = Src->getType();
460
461 auto *S_VT = dyn_cast<FixedVectorType>(FromTy);
462 auto *D_VT = dyn_cast<FixedVectorType>(ToTy);
463 auto *D_AT = dyn_cast<ArrayType>(ToTy);
464 auto *D_MAT =
465 D_AT ? dyn_cast<FixedVectorType>(D_AT->getElementType()) : nullptr;
466
467 B.SetInsertPoint(BadStore);
468 if (isTypeFirstElementAggregate(FromTy, ToTy))
469 storeToFirstValueAggregate(B, Src, Dst, ToTy, Alignment);
470 else if (D_VT && S_VT)
471 storeVectorFromVector(B, Src, Dst, Alignment);
472 else if (D_VT && !S_VT && FromTy == D_VT->getElementType())
473 storeToFirstValueAggregate(B, Src, Dst, D_VT, Alignment);
474 else if (D_AT && S_VT && S_VT->getElementType() == D_AT->getElementType())
475 storeArrayFromVector(B, Src, Dst, D_AT, Alignment);
476 else if (D_MAT && S_VT && D_MAT->getElementType() == S_VT->getElementType())
477 storeMatrixArrayFromVector(B, Src, Dst, D_AT, Alignment);
478 else
479 llvm_unreachable("Unsupported ptrcast use in store. Please fix.");
480
481 DeadInstructions.push_back(BadStore);
482 }
483
484 void legalizePointerCast(IntrinsicInst *II) {
485 Value *CastedOperand = II;
486 Value *OriginalOperand = II->getOperand(0);
487
488 IRBuilder<> B(II->getContext());
489 std::vector<Value *> Users;
490 for (Use &U : II->uses())
491 Users.push_back(U.getUser());
492
493 for (Value *User : Users) {
494 if (LoadInst *LI = dyn_cast<LoadInst>(User)) {
495 transformLoad(B, LI, CastedOperand, OriginalOperand);
496 continue;
497 }
498
499 if (StoreInst *SI = dyn_cast<StoreInst>(User)) {
500 transformStore(B, SI, SI->getValueOperand(), OriginalOperand,
501 SI->getAlign());
502 continue;
503 }
504
505 if (IntrinsicInst *Intrin = dyn_cast<IntrinsicInst>(User)) {
506 if (Intrin->getIntrinsicID() == Intrinsic::spv_assign_ptr_type) {
507 DeadInstructions.push_back(Intrin);
508 continue;
509 }
510
511 if (Intrin->getIntrinsicID() == Intrinsic::spv_gep) {
512 GR->replaceAllUsesWith(CastedOperand, OriginalOperand,
513 /* DeleteOld= */ false);
514 continue;
515 }
516
517 if (Intrin->getIntrinsicID() == Intrinsic::spv_store) {
518 Align Alignment;
519 if (ConstantInt *C = dyn_cast<ConstantInt>(Intrin->getOperand(3)))
520 Alignment = Align(C->getZExtValue());
521 transformStore(B, Intrin, Intrin->getArgOperand(0), OriginalOperand,
522 Alignment);
523 continue;
524 }
525 }
526
527 llvm_unreachable("Unsupported ptrcast user. Please fix.");
528 }
529
530 DeadInstructions.push_back(II);
531 }
532
533public:
534 SPIRVLegalizePointerCast(SPIRVTargetMachine *TM) : FunctionPass(ID), TM(TM) {}
535
536 bool runOnFunction(Function &F) override {
537 const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(F);
538 GR = ST.getSPIRVGlobalRegistry();
539 DeadInstructions.clear();
540
541 std::vector<IntrinsicInst *> WorkList;
542 for (auto &BB : F) {
543 for (auto &I : BB) {
544 auto *II = dyn_cast<IntrinsicInst>(&I);
545 if (II && II->getIntrinsicID() == Intrinsic::spv_ptrcast)
546 WorkList.push_back(II);
547 }
548 }
549
550 for (IntrinsicInst *II : WorkList)
551 legalizePointerCast(II);
552
553 for (Instruction *I : DeadInstructions)
554 I->eraseFromParent();
555
556 return DeadInstructions.size() != 0;
557 }
558
559private:
560 SPIRVTargetMachine *TM = nullptr;
561 SPIRVGlobalRegistry *GR = nullptr;
562 std::vector<Instruction *> DeadInstructions;
563
564public:
565 static char ID;
566};
567} // namespace
568
569char SPIRVLegalizePointerCast::ID = 0;
570INITIALIZE_PASS(SPIRVLegalizePointerCast, "spirv-legalize-bitcast",
571 "SPIRV legalize bitcast pass", false, false)
572
574 return new SPIRVLegalizePointerCast(TM);
575}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
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:54
#define I(x, y, z)
Definition MD5.cpp:57
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
unsigned getNumElements() const
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Definition Type.cpp:873
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:236
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:68
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:328
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)