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