LLVM 23.0.0git
SPIRVEmitIntrinsics.cpp
Go to the documentation of this file.
1//===-- SPIRVEmitIntrinsics.cpp - emit SPIRV intrinsics ---------*- 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 pass emits SPIRV intrinsics keeping essential high-level information for
10// the translation of LLVM IR to SPIR-V.
11//
12//===----------------------------------------------------------------------===//
13
14#include "SPIRVEmitIntrinsics.h"
15#include "SPIRV.h"
16#include "SPIRVBuiltins.h"
17#include "SPIRVSubtarget.h"
18#include "SPIRVTargetMachine.h"
19#include "SPIRVUtils.h"
20#include "llvm/ADT/DenseSet.h"
21#include "llvm/ADT/StringSet.h"
23#include "llvm/IR/Dominators.h"
24#include "llvm/IR/IRBuilder.h"
26#include "llvm/IR/InstVisitor.h"
27#include "llvm/IR/IntrinsicsSPIRV.h"
30#include "llvm/IR/Value.h"
32#include "llvm/Support/Debug.h"
34
35#include <cassert>
36#include <optional>
37#include <queue>
38#include <unordered_set>
39
40// This pass performs the following transformation on LLVM IR level required
41// for the following translation to SPIR-V:
42// - replaces direct usages of aggregate constants with target-specific
43// intrinsics;
44// - replaces aggregates-related instructions (extract/insert, ld/st, etc)
45// with a target-specific intrinsics;
46// - emits intrinsics for the global variable initializers since IRTranslator
47// doesn't handle them and it's not very convenient to translate them
48// ourselves;
49// - emits intrinsics to keep track of the string names assigned to the values;
50// - emits intrinsics to keep track of constants (this is necessary to have an
51// LLVM IR constant after the IRTranslation is completed) for their further
52// deduplication;
53// - emits intrinsics to keep track of original LLVM types of the values
54// to be able to emit proper SPIR-V types eventually.
55//
56// TODO: consider removing spv.track.constant in favor of spv.assign.type.
57
58using namespace llvm;
59using namespace llvm::PatternMatch;
60
61#define DEBUG_TYPE "spirv-emit-intrinsics"
62
63static cl::opt<bool>
64 SpirvEmitOpNames("spirv-emit-op-names",
65 cl::desc("Emit OpName for all instructions"),
66 cl::init(false));
67
68namespace llvm::SPIRV {
69#define GET_BuiltinGroup_DECL
70#include "SPIRVGenTables.inc"
71} // namespace llvm::SPIRV
72
73namespace {
74// This class keeps track of which functions reference which global variables.
75class GlobalVariableUsers {
76 template <typename T1, typename T2>
77 using OneToManyMapTy = DenseMap<T1, SmallPtrSet<T2, 4>>;
78
79 OneToManyMapTy<const GlobalVariable *, const Function *> GlobalIsUsedByFun;
80
81 void collectGlobalUsers(
82 const GlobalVariable *GV,
83 OneToManyMapTy<const GlobalVariable *, const GlobalVariable *>
84 &GlobalIsUsedByGlobal) {
86 while (!Stack.empty()) {
87 const Value *V = Stack.pop_back_val();
88
89 if (const Instruction *I = dyn_cast<Instruction>(V)) {
90 GlobalIsUsedByFun[GV].insert(I->getFunction());
91 continue;
92 }
93
94 if (const GlobalVariable *UserGV = dyn_cast<GlobalVariable>(V)) {
95 GlobalIsUsedByGlobal[GV].insert(UserGV);
96 continue;
97 }
98
99 if (const Constant *C = dyn_cast<Constant>(V))
100 Stack.append(C->user_begin(), C->user_end());
101 }
102 }
103
104 bool propagateGlobalToGlobalUsers(
105 OneToManyMapTy<const GlobalVariable *, const GlobalVariable *>
106 &GlobalIsUsedByGlobal) {
108 bool Changed = false;
109 for (auto &[GV, UserGlobals] : GlobalIsUsedByGlobal) {
110 OldUsersGlobals.assign(UserGlobals.begin(), UserGlobals.end());
111 for (const GlobalVariable *UserGV : OldUsersGlobals) {
112 auto It = GlobalIsUsedByGlobal.find(UserGV);
113 if (It == GlobalIsUsedByGlobal.end())
114 continue;
115 Changed |= set_union(UserGlobals, It->second);
116 }
117 }
118 return Changed;
119 }
120
121 void propagateGlobalToFunctionReferences(
122 OneToManyMapTy<const GlobalVariable *, const GlobalVariable *>
123 &GlobalIsUsedByGlobal) {
124 for (auto &[GV, UserGlobals] : GlobalIsUsedByGlobal) {
125 auto &UserFunctions = GlobalIsUsedByFun[GV];
126 for (const GlobalVariable *UserGV : UserGlobals) {
127 auto It = GlobalIsUsedByFun.find(UserGV);
128 if (It == GlobalIsUsedByFun.end())
129 continue;
130 set_union(UserFunctions, It->second);
131 }
132 }
133 }
134
135public:
136 void init(Module &M) {
137 // Collect which global variables are referenced by which global variables
138 // and which functions reference each global variables.
139 OneToManyMapTy<const GlobalVariable *, const GlobalVariable *>
140 GlobalIsUsedByGlobal;
141 GlobalIsUsedByFun.clear();
142 for (GlobalVariable &GV : M.globals())
143 collectGlobalUsers(&GV, GlobalIsUsedByGlobal);
144
145 // Compute indirect references by iterating until a fixed point is reached.
146 while (propagateGlobalToGlobalUsers(GlobalIsUsedByGlobal))
147 (void)0;
148
149 propagateGlobalToFunctionReferences(GlobalIsUsedByGlobal);
150 }
151
152 using FunctionSetType = typename decltype(GlobalIsUsedByFun)::mapped_type;
153 const FunctionSetType &
154 getTransitiveUserFunctions(const GlobalVariable &GV) const {
155 auto It = GlobalIsUsedByFun.find(&GV);
156 if (It != GlobalIsUsedByFun.end())
157 return It->second;
158
159 static const FunctionSetType Empty{};
160 return Empty;
161 }
162};
163
164static bool isaGEP(const Value *V) {
166}
167
168// If Ty is a byte-addressing type, return the multiplier for the offset.
169// Otherwise return std::nullopt.
170static std::optional<uint64_t> getByteAddressingMultiplier(Type *Ty) {
171 if (Ty == IntegerType::getInt8Ty(Ty->getContext())) {
172 return 1;
173 }
174 if (auto *AT = dyn_cast<ArrayType>(Ty)) {
175 if (AT->getElementType() == IntegerType::getInt8Ty(Ty->getContext())) {
176 return AT->getNumElements();
177 }
178 }
179 return std::nullopt;
180}
181
182class SPIRVEmitIntrinsics
183 : public ModulePass,
184 public InstVisitor<SPIRVEmitIntrinsics, Instruction *> {
185 const SPIRVTargetMachine &TM;
186 SPIRVGlobalRegistry *GR = nullptr;
187 Function *CurrF = nullptr;
188 bool TrackConstants = true;
189 bool HaveFunPtrs = false;
190 DenseMap<Instruction *, Constant *> AggrConsts;
191 DenseMap<Instruction *, Type *> AggrConstTypes;
192 DenseSet<Instruction *> AggrStores;
193 GlobalVariableUsers GVUsers;
194 std::unordered_set<Value *> Named;
195
196 // map of function declarations to <pointer arg index => element type>
197 DenseMap<Function *, SmallVector<std::pair<unsigned, Type *>>> FDeclPtrTys;
198
199 // a register of Instructions that don't have a complete type definition
200 bool CanTodoType = true;
201 unsigned TodoTypeSz = 0;
202 DenseMap<Value *, bool> TodoType;
203 void insertTodoType(Value *Op) {
204 // TODO: add isa<CallInst>(Op) to no-insert
205 if (CanTodoType && !isaGEP(Op)) {
206 auto It = TodoType.try_emplace(Op, true);
207 if (It.second)
208 ++TodoTypeSz;
209 }
210 }
211 void eraseTodoType(Value *Op) {
212 auto It = TodoType.find(Op);
213 if (It != TodoType.end() && It->second) {
214 It->second = false;
215 --TodoTypeSz;
216 }
217 }
218 bool isTodoType(Value *Op) {
219 if (isaGEP(Op))
220 return false;
221 auto It = TodoType.find(Op);
222 return It != TodoType.end() && It->second;
223 }
224 // a register of Instructions that were visited by deduceOperandElementType()
225 // to validate operand types with an instruction
226 std::unordered_set<Instruction *> TypeValidated;
227
228 // well known result types of builtins
229 enum WellKnownTypes { Event };
230
231 // deduce element type of untyped pointers
232 Type *deduceElementType(Value *I, bool UnknownElemTypeI8);
233 Type *deduceElementTypeHelper(Value *I, bool UnknownElemTypeI8);
234 Type *deduceElementTypeHelper(Value *I, std::unordered_set<Value *> &Visited,
235 bool UnknownElemTypeI8,
236 bool IgnoreKnownType = false);
237 Type *deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
238 bool UnknownElemTypeI8);
239 Type *deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
240 std::unordered_set<Value *> &Visited,
241 bool UnknownElemTypeI8);
242 Type *deduceElementTypeByUsersDeep(Value *Op,
243 std::unordered_set<Value *> &Visited,
244 bool UnknownElemTypeI8);
245 void maybeAssignPtrType(Type *&Ty, Value *I, Type *RefTy,
246 bool UnknownElemTypeI8);
247
248 // deduce nested types of composites
249 Type *deduceNestedTypeHelper(User *U, bool UnknownElemTypeI8);
250 Type *deduceNestedTypeHelper(User *U, Type *Ty,
251 std::unordered_set<Value *> &Visited,
252 bool UnknownElemTypeI8);
253
254 // deduce Types of operands of the Instruction if possible
255 void deduceOperandElementType(Instruction *I,
256 SmallPtrSet<Instruction *, 4> *IncompleteRets,
257 const SmallPtrSet<Value *, 4> *AskOps = nullptr,
258 bool IsPostprocessing = false);
259
260 void preprocessCompositeConstants(IRBuilder<> &B);
261 void preprocessUndefs(IRBuilder<> &B);
262 void preprocessPoisons(IRBuilder<> &B);
263 void simplifyNullAddrSpaceCasts();
264
265 Type *reconstructType(Value *Op, bool UnknownElemTypeI8,
266 bool IsPostprocessing);
267
268 void replaceMemInstrUses(Instruction *Old, Instruction *New, IRBuilder<> &B);
269 void processInstrAfterVisit(Instruction *I, IRBuilder<> &B);
270 bool insertAssignPtrTypeIntrs(Instruction *I, IRBuilder<> &B,
271 bool UnknownElemTypeI8);
272 void insertAssignTypeIntrs(Instruction *I, IRBuilder<> &B);
273 void insertAssignPtrTypeTargetExt(TargetExtType *AssignedType, Value *V,
274 IRBuilder<> &B);
275 void replacePointerOperandWithPtrCast(Instruction *I, Value *Pointer,
276 Type *ExpectedElementType,
277 unsigned OperandToReplace,
278 IRBuilder<> &B);
279 void insertPtrCastOrAssignTypeInstr(Instruction *I, IRBuilder<> &B);
280 bool shouldTryToAddMemAliasingDecoration(Instruction *Inst);
281 void insertSpirvDecorations(Instruction *I, IRBuilder<> &B);
282 void insertConstantsForFPFastMathDefault(Module &M);
283 Value *buildSpvUndefComposite(Type *AggrTy, IRBuilder<> &B);
284 void processGlobalValue(GlobalVariable &GV, IRBuilder<> &B);
285 void processParamTypes(Function *F, IRBuilder<> &B);
286 void processParamTypesByFunHeader(Function *F, IRBuilder<> &B);
287 Type *deduceFunParamElementType(Function *F, unsigned OpIdx);
288 Type *deduceFunParamElementType(Function *F, unsigned OpIdx,
289 std::unordered_set<Function *> &FVisited);
290
291 bool deduceOperandElementTypeCalledFunction(
292 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
293 Type *&KnownElemTy, bool &Incomplete);
294 void deduceOperandElementTypeFunctionPointer(
295 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
296 Type *&KnownElemTy, bool IsPostprocessing);
297 bool deduceOperandElementTypeFunctionRet(
298 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
299 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing,
300 Type *&KnownElemTy, Value *Op, Function *F);
301
302 CallInst *buildSpvPtrcast(Function *F, Value *Op, Type *ElemTy);
303 void replaceUsesOfWithSpvPtrcast(Value *Op, Type *ElemTy, Instruction *I,
304 DenseMap<Function *, CallInst *> Ptrcasts);
305 void propagateElemType(Value *Op, Type *ElemTy,
306 DenseSet<std::pair<Value *, Value *>> &VisitedSubst);
307 void
308 propagateElemTypeRec(Value *Op, Type *PtrElemTy, Type *CastElemTy,
309 DenseSet<std::pair<Value *, Value *>> &VisitedSubst);
310 void propagateElemTypeRec(Value *Op, Type *PtrElemTy, Type *CastElemTy,
311 DenseSet<std::pair<Value *, Value *>> &VisitedSubst,
312 std::unordered_set<Value *> &Visited,
313 DenseMap<Function *, CallInst *> Ptrcasts);
314
315 void replaceAllUsesWith(Value *Src, Value *Dest, bool DeleteOld = true);
316 void replaceAllUsesWithAndErase(IRBuilder<> &B, Instruction *Src,
317 Instruction *Dest, bool DeleteOld = true);
318
319 void applyDemangledPtrArgTypes(IRBuilder<> &B);
320
321 GetElementPtrInst *simplifyZeroLengthArrayGepInst(GetElementPtrInst *GEP);
322
323 bool runOnFunction(Function &F);
324 bool postprocessTypes(Module &M);
325 bool processFunctionPointers(Module &M);
326 void parseFunDeclarations(Module &M);
327 void useRoundingMode(ConstrainedFPIntrinsic *FPI, IRBuilder<> &B);
328 bool processMaskedMemIntrinsic(IntrinsicInst &I);
329 bool convertMaskedMemIntrinsics(Module &M);
330 void preprocessBoolVectorBitcasts(Function &F);
331
332 void emitUnstructuredLoopControls(Function &F, IRBuilder<> &B);
333
334 // Tries to walk the type accessed by the given GEP instruction.
335 // For each nested type access, one of the 2 callbacks is called:
336 // - OnLiteralIndexing when the index is a known constant value.
337 // Parameters:
338 // PointedType: the pointed type resulting of this indexing.
339 // If the parent type is an array, this is the index in the array.
340 // If the parent type is a struct, this is the field index.
341 // Index: index of the element in the parent type.
342 // - OnDynamnicIndexing when the index is a non-constant value.
343 // This callback is only called when indexing into an array.
344 // Parameters:
345 // ElementType: the type of the elements stored in the parent array.
346 // Offset: the Value* containing the byte offset into the array.
347 // Multiplier: a scaling factor for the offset.
348 // Return true if an error occurred during the walk, false otherwise.
349 bool walkLogicalAccessChain(
350 GetElementPtrInst &GEP,
351 const std::function<void(Type *PointedType, uint64_t Index)>
352 &OnLiteralIndexing,
353 const std::function<void(Type *ElementType, Value *Offset,
354 uint64_t Multiplier)> &OnDynamicIndexing);
355
356 bool walkLogicalAccessChainDynamic(
357 Type *CurType, Value *Operand, uint64_t Multiplier,
358 const std::function<void(Type *, uint64_t)> &OnLiteralIndexing,
359 const std::function<void(Type *, Value *, uint64_t)> &OnDynamicIndexing);
360
361 bool walkLogicalAccessChainConstant(
362 Type *CurType, uint64_t Offset,
363 const std::function<void(Type *, uint64_t)> &OnLiteralIndexing);
364
365 // Returns the type accessed using the given GEP instruction by relying
366 // on the GEP type.
367 // FIXME: GEP types are not supposed to be used to retrieve the pointed
368 // type. This must be fixed.
369 Type *getGEPType(GetElementPtrInst *GEP);
370
371 // Returns the type accessed using the given GEP instruction by walking
372 // the source type using the GEP indices.
373 // FIXME: without help from the frontend, this method cannot reliably retrieve
374 // the stored type, nor can robustly determine the depth of the type
375 // we are accessing.
376 Type *getGEPTypeLogical(GetElementPtrInst *GEP);
377
378 Instruction *buildLogicalAccessChainFromGEP(GetElementPtrInst &GEP);
379
380public:
381 static char ID;
382 SPIRVEmitIntrinsics(const SPIRVTargetMachine &TM) : ModulePass(ID), TM(TM) {}
383 Instruction *visitInstruction(Instruction &I) { return &I; }
384 Instruction *visitSwitchInst(SwitchInst &I);
385 Instruction *visitGetElementPtrInst(GetElementPtrInst &I);
386 Instruction *visitIntrinsicInst(IntrinsicInst &I);
387 Instruction *visitBitCastInst(BitCastInst &I);
388 Instruction *visitInsertElementInst(InsertElementInst &I);
389 Instruction *visitExtractElementInst(ExtractElementInst &I);
390 Instruction *visitInsertValueInst(InsertValueInst &I);
391 Instruction *visitExtractValueInst(ExtractValueInst &I);
392 Instruction *visitLoadInst(LoadInst &I);
393 Instruction *visitStoreInst(StoreInst &I);
394 Instruction *visitAllocaInst(AllocaInst &I);
395 Instruction *visitAtomicCmpXchgInst(AtomicCmpXchgInst &I);
396 Instruction *visitUnreachableInst(UnreachableInst &I);
397 Instruction *visitCallInst(CallInst &I);
398
399 StringRef getPassName() const override { return "SPIRV emit intrinsics"; }
400
401 bool runOnModule(Module &M) override;
402
403 void getAnalysisUsage(AnalysisUsage &AU) const override {
404 ModulePass::getAnalysisUsage(AU);
405 }
406};
407
408bool isConvergenceIntrinsic(const Instruction *I) {
409 return match(I, m_AnyIntrinsic<Intrinsic::experimental_convergence_entry,
410 Intrinsic::experimental_convergence_loop,
411 Intrinsic::experimental_convergence_anchor>());
412}
413
414bool expectIgnoredInIRTranslation(const Instruction *I) {
415 return match(I, m_AnyIntrinsic<Intrinsic::invariant_start,
416 Intrinsic::spv_resource_handlefrombinding,
417 Intrinsic::spv_resource_getbasepointer,
418 Intrinsic::spv_resource_getpointer>());
419}
420
421// Returns the source pointer from `I` ignoring intermediate ptrcast.
422Value *getPointerRoot(Value *I) {
423 Value *V;
425 return getPointerRoot(V);
426 return I;
427}
428
429} // namespace
430
431char SPIRVEmitIntrinsics::ID = 0;
432
433INITIALIZE_PASS(SPIRVEmitIntrinsics, "spirv-emit-intrinsics",
434 "SPIRV emit intrinsics", false, false)
435
436static inline bool isAssignTypeInstr(const Instruction *I) {
438}
439
444
445static bool isAggrConstForceInt32(const Value *V) {
446 bool IsAggrZero =
447 isa<ConstantAggregateZero>(V) && !V->getType()->isVectorTy();
448 bool IsUndefAggregate = isa<UndefValue>(V) && V->getType()->isAggregateType();
449 return isa<ConstantArray>(V) || isa<ConstantStruct>(V) ||
450 isa<ConstantDataArray>(V) || IsAggrZero || IsUndefAggregate;
451}
452
458
460 if (isa<PHINode>(I))
461 B.SetInsertPoint(I->getParent()->getFirstNonPHIOrDbgOrAlloca());
462 else
463 B.SetInsertPoint(I);
464}
465
467 B.SetCurrentDebugLocation(I->getDebugLoc());
468 if (I->getType()->isVoidTy())
469 B.SetInsertPoint(I->getNextNode());
470 else
471 B.SetInsertPoint(*I->getInsertionPointAfterDef());
472}
473
479
480static inline void reportFatalOnTokenType(const Instruction *I) {
481 if (I->getType()->isTokenTy())
482 report_fatal_error("A token is encountered but SPIR-V without extensions "
483 "does not support token type",
484 false);
485}
486
488 if (!I->hasName() || I->getType()->isAggregateType() ||
489 expectIgnoredInIRTranslation(I))
490 return;
491
492 // We want to be conservative when adding the names because they can interfere
493 // with later optimizations.
494 bool KeepName = SpirvEmitOpNames;
495 if (!KeepName) {
496 if (isa<AllocaInst>(I)) {
497 KeepName = true;
498 } else if (auto *CI = dyn_cast<CallBase>(I)) {
499 Function *F = CI->getCalledFunction();
500 if (F && F->getName().starts_with("llvm.spv.alloca"))
501 KeepName = true;
502 }
503 }
504
505 if (!KeepName)
506 return;
507
510 LLVMContext &Ctx = I->getContext();
511 std::vector<Value *> Args = {
513 Ctx, MDNode::get(Ctx, MDString::get(Ctx, I->getName())))};
514 B.CreateIntrinsic(Intrinsic::spv_assign_name, {I->getType()}, Args);
515}
516
517void SPIRVEmitIntrinsics::replaceAllUsesWith(Value *Src, Value *Dest,
518 bool DeleteOld) {
519 GR->replaceAllUsesWith(Src, Dest, DeleteOld);
520 // Update uncomplete type records if any
521 if (isTodoType(Src)) {
522 if (DeleteOld)
523 eraseTodoType(Src);
524 insertTodoType(Dest);
525 }
526}
527
528void SPIRVEmitIntrinsics::replaceAllUsesWithAndErase(IRBuilder<> &B,
529 Instruction *Src,
530 Instruction *Dest,
531 bool DeleteOld) {
532 replaceAllUsesWith(Src, Dest, DeleteOld);
533 std::string Name = Src->hasName() ? Src->getName().str() : "";
534 Src->eraseFromParent();
535 if (!Name.empty()) {
536 Dest->setName(Name);
537 if (Named.insert(Dest).second)
538 emitAssignName(Dest, B);
539 }
540}
541
543 return SI && F->getCallingConv() == CallingConv::SPIR_KERNEL &&
544 isPointerTy(SI->getValueOperand()->getType()) &&
545 isa<Argument>(SI->getValueOperand());
546}
547
548// A pointer-typed local holds a pointer, so its deduced pointee must stay a
549// pointer.
551 using namespace PatternMatch;
552 V = V->stripPointerCasts();
553 if (auto *AI = dyn_cast<AllocaInst>(V))
554 return isUntypedPointerTy(AI->getAllocatedType());
555 return match(
557}
558
559// Maybe restore original function return type.
561 Type *Ty) {
563 if (!CI || CI->isIndirectCall() || CI->isInlineAsm() ||
565 return Ty;
566 if (Type *OriginalTy = GR->findMutated(CI->getCalledFunction()))
567 return OriginalTy;
568 return Ty;
569}
570
571// Reconstruct type with nested element types according to deduced type info.
572// Return nullptr if no detailed type info is available.
573Type *SPIRVEmitIntrinsics::reconstructType(Value *Op, bool UnknownElemTypeI8,
574 bool IsPostprocessing) {
575 Type *Ty = Op->getType();
576 if (auto *OpI = dyn_cast<Instruction>(Op)) {
577 Ty = restoreMutatedType(GR, OpI, Ty);
578 if (auto It = AggrConstTypes.find(OpI); It != AggrConstTypes.end())
579 Ty = It->second;
580 }
581 if (!isUntypedPointerTy(Ty))
582 return Ty;
583 // try to find the pointee type
584 if (Type *NestedTy = GR->findDeducedElementType(Op))
586 // not a pointer according to the type info (e.g., Event object)
587 CallInst *CI = GR->findAssignPtrTypeInstr(Op);
588 if (CI) {
589 MetadataAsValue *MD = cast<MetadataAsValue>(CI->getArgOperand(1));
590 return cast<ConstantAsMetadata>(MD->getMetadata())->getType();
591 }
592 if (UnknownElemTypeI8) {
593 if (!IsPostprocessing)
594 insertTodoType(Op);
595 return getTypedPointerWrapper(IntegerType::getInt8Ty(Op->getContext()),
597 }
598 return nullptr;
599}
600
601CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Function *F, Value *Op,
602 Type *ElemTy) {
603 IRBuilder<> B(Op->getContext());
604 if (auto *OpI = dyn_cast<Instruction>(Op)) {
605 // spv_ptrcast's argument Op denotes an instruction that generates
606 // a value, and we may use getInsertionPointAfterDef()
608 } else if (auto *OpA = dyn_cast<Argument>(Op)) {
609 B.SetInsertPointPastAllocas(OpA->getParent());
610 B.SetCurrentDebugLocation(DebugLoc());
611 } else {
612 B.SetInsertPoint(F->getEntryBlock().getFirstNonPHIOrDbgOrAlloca());
613 }
614 Type *OpTy = Op->getType();
615 SmallVector<Type *, 2> Types = {OpTy, OpTy};
616 SmallVector<Value *, 2> Args = {Op, buildMD(getNormalizedPoisonValue(ElemTy)),
617 B.getInt32(getPointerAddressSpace(OpTy))};
618 CallInst *PtrCasted =
619 B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
620 GR->buildAssignPtr(B, ElemTy, PtrCasted);
621 return PtrCasted;
622}
623
624void SPIRVEmitIntrinsics::replaceUsesOfWithSpvPtrcast(
625 Value *Op, Type *ElemTy, Instruction *I,
626 DenseMap<Function *, CallInst *> Ptrcasts) {
627 Function *F = I->getParent()->getParent();
628 CallInst *PtrCastedI = nullptr;
629 auto It = Ptrcasts.find(F);
630 if (It == Ptrcasts.end()) {
631 PtrCastedI = buildSpvPtrcast(F, Op, ElemTy);
632 Ptrcasts[F] = PtrCastedI;
633 } else {
634 PtrCastedI = It->second;
635 }
636 I->replaceUsesOfWith(Op, PtrCastedI);
637}
638
639void SPIRVEmitIntrinsics::propagateElemType(
640 Value *Op, Type *ElemTy,
641 DenseSet<std::pair<Value *, Value *>> &VisitedSubst) {
642 DenseMap<Function *, CallInst *> Ptrcasts;
643 SmallVector<User *> Users(Op->users());
644 for (auto *U : Users) {
645 if (!isa<Instruction>(U) || isSpvIntrinsic(U))
646 continue;
647 if (!VisitedSubst.insert(std::make_pair(U, Op)).second)
648 continue;
650 // If the instruction was validated already, we need to keep it valid by
651 // keeping current Op type.
652 if (isaGEP(UI) || TypeValidated.find(UI) != TypeValidated.end())
653 replaceUsesOfWithSpvPtrcast(Op, ElemTy, UI, Ptrcasts);
654 }
655}
656
657void SPIRVEmitIntrinsics::propagateElemTypeRec(
658 Value *Op, Type *PtrElemTy, Type *CastElemTy,
659 DenseSet<std::pair<Value *, Value *>> &VisitedSubst) {
660 std::unordered_set<Value *> Visited;
661 DenseMap<Function *, CallInst *> Ptrcasts;
662 propagateElemTypeRec(Op, PtrElemTy, CastElemTy, VisitedSubst, Visited,
663 std::move(Ptrcasts));
664}
665
666void SPIRVEmitIntrinsics::propagateElemTypeRec(
667 Value *Op, Type *PtrElemTy, Type *CastElemTy,
668 DenseSet<std::pair<Value *, Value *>> &VisitedSubst,
669 std::unordered_set<Value *> &Visited,
670 DenseMap<Function *, CallInst *> Ptrcasts) {
671 if (!Visited.insert(Op).second)
672 return;
673 SmallVector<User *> Users(Op->users());
674 for (auto *U : Users) {
675 if (!isa<Instruction>(U) || isSpvIntrinsic(U))
676 continue;
677 if (!VisitedSubst.insert(std::make_pair(U, Op)).second)
678 continue;
680 // If the instruction was validated already, we need to keep it valid by
681 // keeping current Op type.
682 if (isaGEP(UI) || TypeValidated.find(UI) != TypeValidated.end())
683 replaceUsesOfWithSpvPtrcast(Op, CastElemTy, UI, Ptrcasts);
684 }
685}
686
687// Set element pointer type to the given value of ValueTy and tries to
688// specify this type further (recursively) by Operand value, if needed.
689
690Type *
691SPIRVEmitIntrinsics::deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
692 bool UnknownElemTypeI8) {
693 std::unordered_set<Value *> Visited;
694 return deduceElementTypeByValueDeep(ValueTy, Operand, Visited,
695 UnknownElemTypeI8);
696}
697
698Type *SPIRVEmitIntrinsics::deduceElementTypeByValueDeep(
699 Type *ValueTy, Value *Operand, std::unordered_set<Value *> &Visited,
700 bool UnknownElemTypeI8) {
701 Type *Ty = ValueTy;
702 if (Operand) {
703 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
704 if (Type *NestedTy =
705 deduceElementTypeHelper(Operand, Visited, UnknownElemTypeI8))
706 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
707 } else {
708 Ty = deduceNestedTypeHelper(dyn_cast<User>(Operand), Ty, Visited,
709 UnknownElemTypeI8);
710 }
711 }
712 return Ty;
713}
714
715// Traverse User instructions to deduce an element pointer type of the operand.
716Type *SPIRVEmitIntrinsics::deduceElementTypeByUsersDeep(
717 Value *Op, std::unordered_set<Value *> &Visited, bool UnknownElemTypeI8) {
718 if (!Op || !isPointerTy(Op->getType()) || isa<ConstantPointerNull>(Op) ||
720 return nullptr;
721
722 if (auto ElemTy = getPointeeType(Op->getType()))
723 return ElemTy;
724
725 // maybe we already know operand's element type
726 if (Type *KnownTy = GR->findDeducedElementType(Op))
727 return KnownTy;
728
729 for (User *OpU : Op->users()) {
730 if (Instruction *Inst = dyn_cast<Instruction>(OpU)) {
731 if (Type *Ty = deduceElementTypeHelper(Inst, Visited, UnknownElemTypeI8))
732 return Ty;
733 }
734 }
735 return nullptr;
736}
737
738// Implements what we know in advance about intrinsics and builtin calls
739// TODO: consider feasibility of this particular case to be generalized by
740// encoding knowledge about intrinsics and builtin calls by corresponding
741// specification rules
743 Function *CalledF, unsigned OpIdx) {
744 if ((DemangledName.starts_with("__spirv_ocl_printf(") ||
745 DemangledName.starts_with("printf(")) &&
746 OpIdx == 0)
747 return IntegerType::getInt8Ty(CalledF->getContext());
748 return nullptr;
749}
750
751// Deduce and return a successfully deduced Type of the Instruction,
752// or nullptr otherwise.
753Type *SPIRVEmitIntrinsics::deduceElementTypeHelper(Value *I,
754 bool UnknownElemTypeI8) {
755 std::unordered_set<Value *> Visited;
756 return deduceElementTypeHelper(I, Visited, UnknownElemTypeI8);
757}
758
759void SPIRVEmitIntrinsics::maybeAssignPtrType(Type *&Ty, Value *Op, Type *RefTy,
760 bool UnknownElemTypeI8) {
761 if (isUntypedPointerTy(RefTy)) {
762 if (!UnknownElemTypeI8)
763 return;
764 insertTodoType(Op);
766 return;
767 }
768 Ty = RefTy;
769}
770
771bool SPIRVEmitIntrinsics::walkLogicalAccessChainDynamic(
772 Type *CurType, Value *Operand, uint64_t Multiplier,
773 const std::function<void(Type *, uint64_t)> &OnLiteralIndexing,
774 const std::function<void(Type *, Value *, uint64_t)> &OnDynamicIndexing) {
775 // Dynamic indexing into a struct is not possible.
776 // We know that we must be accessing the first element
777 // of the struct if the current type is a struct.
778 // Try to find the first array type that is at offset 0 in the struct.
779 while (auto *ST = dyn_cast<StructType>(CurType)) {
780 if (ST->getNumElements() == 0)
781 break;
782 CurType = ST->getElementType(0);
783 OnLiteralIndexing(CurType, 0);
784 }
785
786 assert(CurType);
787 ArrayType *AT = dyn_cast<ArrayType>(CurType);
788 // Operand is not constant. Either we have an array and accept it, or we
789 // give up.
790 if (AT)
791 OnDynamicIndexing(AT->getElementType(), Operand, Multiplier);
792 return AT == nullptr;
793}
794
795bool SPIRVEmitIntrinsics::walkLogicalAccessChainConstant(
796 Type *CurType, uint64_t Offset,
797 const std::function<void(Type *, uint64_t)> &OnLiteralIndexing) {
798 auto &DL = CurrF->getDataLayout();
799
800 do {
801 if (ArrayType *AT = dyn_cast<ArrayType>(CurType)) {
802 uint32_t EltTypeSize = DL.getTypeSizeInBits(AT->getElementType()) / 8;
803 assert(Offset < AT->getNumElements() * EltTypeSize);
804 uint64_t Index = Offset / EltTypeSize;
805 Offset = Offset - (Index * EltTypeSize);
806 CurType = AT->getElementType();
807 OnLiteralIndexing(CurType, Index);
808 } else if (StructType *ST = dyn_cast<StructType>(CurType)) {
809 uint32_t StructSize = DL.getTypeSizeInBits(ST) / 8;
810 assert(Offset < StructSize);
811 (void)StructSize;
812 const auto &STL = DL.getStructLayout(ST);
813 unsigned Element = STL->getElementContainingOffset(Offset);
814 Offset -= STL->getElementOffset(Element);
815 CurType = ST->getElementType(Element);
816 OnLiteralIndexing(CurType, Element);
817 } else if (auto *VT = dyn_cast<FixedVectorType>(CurType)) {
818 Type *EltTy = VT->getElementType();
819 TypeSize EltSizeBits = DL.getTypeSizeInBits(EltTy);
820 assert(EltSizeBits % 8 == 0 &&
821 "Element type size in bits must be a multiple of 8.");
822 uint32_t EltTypeSize = EltSizeBits / 8;
823 assert(Offset < VT->getNumElements() * EltTypeSize);
824 uint64_t Index = Offset / EltTypeSize;
825 Offset -= Index * EltTypeSize;
826 CurType = EltTy;
827 OnLiteralIndexing(CurType, Index);
828 } else {
829 // Unknown composite kind; give up.
830 return true;
831 }
832 } while (Offset > 0);
833
834 return false;
835}
836
837bool SPIRVEmitIntrinsics::walkLogicalAccessChain(
838 GetElementPtrInst &GEP,
839 const std::function<void(Type *, uint64_t)> &OnLiteralIndexing,
840 const std::function<void(Type *, Value *, uint64_t)> &OnDynamicIndexing) {
841 // We only rewrite byte-addressing GEP. Other should be left as-is.
842 // Valid byte-addressing GEP must always have a single index.
843 std::optional<uint64_t> MultiplierOpt =
844 getByteAddressingMultiplier(GEP.getSourceElementType());
845 assert(MultiplierOpt && "We only rewrite byte-addressing GEP");
846 uint64_t Multiplier = *MultiplierOpt;
847 assert(GEP.getNumIndices() == 1);
848
849 Value *Src = getPointerRoot(GEP.getPointerOperand());
850 Type *CurType = deduceElementType(Src, true);
851
852 Value *Operand = *GEP.idx_begin();
853 if (ConstantInt *CI = dyn_cast<ConstantInt>(Operand))
854 return walkLogicalAccessChainConstant(
855 CurType, CI->getZExtValue() * Multiplier, OnLiteralIndexing);
856
857 return walkLogicalAccessChainDynamic(CurType, Operand, Multiplier,
858 OnLiteralIndexing, OnDynamicIndexing);
859}
860
862SPIRVEmitIntrinsics::buildLogicalAccessChainFromGEP(GetElementPtrInst &GEP) {
863 auto &DL = CurrF->getDataLayout();
864 IRBuilder<> B(GEP.getParent());
865 B.SetInsertPoint(&GEP);
866
867 std::vector<Value *> Indices;
868 Indices.push_back(ConstantInt::get(
869 IntegerType::getInt32Ty(CurrF->getContext()), 0, /* Signed= */ false));
870 walkLogicalAccessChain(
871 GEP,
872 [&Indices, &B](Type *EltType, uint64_t Index) {
873 Indices.push_back(
874 ConstantInt::get(B.getInt64Ty(), Index, /* Signed= */ false));
875 },
876 [&Indices, &B, &DL, this](Type *EltType, Value *Offset,
877 uint64_t Multiplier) {
878 Value *Index = nullptr;
879 uint32_t EltTypeSize = DL.getTypeSizeInBits(EltType) / 8;
880 assert(Multiplier != 0);
881 if (Multiplier == EltTypeSize) {
882 Index = Offset;
883 } else if (EltTypeSize % Multiplier == 0) {
884 Index =
885 B.CreateUDiv(Offset, ConstantInt::get(Offset->getType(),
886 EltTypeSize / Multiplier,
887 /* Signed= */ false));
888 } else {
889 Index = B.CreateMul(Offset,
890 ConstantInt::get(Offset->getType(), Multiplier,
891 /* Signed= */ false));
892 insertAssignTypeIntrs(cast<Instruction>(Index), B);
893 Index = B.CreateUDiv(Index,
894 ConstantInt::get(Offset->getType(), EltTypeSize,
895 /* Signed= */ false));
896 }
897 insertAssignTypeIntrs(cast<Instruction>(Index), B);
898 Indices.push_back(Index);
899 });
900
901 SmallVector<Type *, 2> Types = {GEP.getType(), GEP.getOperand(0)->getType()};
902 SmallVector<Value *, 4> Args;
903 Args.push_back(B.getInt1(GEP.isInBounds()));
904 Args.push_back(GEP.getOperand(0));
905 llvm::append_range(Args, Indices);
906 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
907 replaceAllUsesWithAndErase(B, &GEP, NewI);
908 return NewI;
909}
910
911Type *SPIRVEmitIntrinsics::getGEPTypeLogical(GetElementPtrInst *GEP) {
912
913 Type *CurType = GEP->getResultElementType();
914
915 bool Interrupted = walkLogicalAccessChain(
916 *GEP, [&CurType](Type *EltType, uint64_t Index) { CurType = EltType; },
917 [&CurType](Type *EltType, Value *Index, uint64_t) { CurType = EltType; });
918
919 return Interrupted ? GEP->getResultElementType() : CurType;
920}
921
922Type *SPIRVEmitIntrinsics::getGEPType(GetElementPtrInst *Ref) {
923 if (getByteAddressingMultiplier(Ref->getSourceElementType()) &&
925 return getGEPTypeLogical(Ref);
926 }
927
928 Type *Ty = nullptr;
929 // TODO: not sure if GetElementPtrInst::getTypeAtIndex() does anything
930 // useful here
931 if (isNestedPointer(Ref->getSourceElementType())) {
932 Ty = Ref->getSourceElementType();
933 for (Use &U : drop_begin(Ref->indices()))
934 Ty = GetElementPtrInst::getTypeAtIndex(Ty, U.get());
935 } else {
936 Ty = Ref->getResultElementType();
937 }
938 return Ty;
939}
940
941Type *SPIRVEmitIntrinsics::deduceElementTypeHelper(
942 Value *I, std::unordered_set<Value *> &Visited, bool UnknownElemTypeI8,
943 bool IgnoreKnownType) {
944 // allow to pass nullptr as an argument
945 if (!I)
946 return nullptr;
947
948 // maybe already known
949 if (!IgnoreKnownType)
950 if (Type *KnownTy = GR->findDeducedElementType(I))
951 return KnownTy;
952
953 // maybe a cycle
954 if (!Visited.insert(I).second)
955 return nullptr;
956
957 // fallback value in case when we fail to deduce a type
958 Type *Ty = nullptr;
959 // look for known basic patterns of type inference
960 if (auto *Ref = dyn_cast<AllocaInst>(I)) {
961 maybeAssignPtrType(Ty, I, Ref->getAllocatedType(), UnknownElemTypeI8);
962 } else if (auto *Ref = dyn_cast<GetElementPtrInst>(I)) {
963 Ty = getGEPType(Ref);
964 } else if (auto *SGEP = dyn_cast<StructuredGEPInst>(I)) {
965 Ty = SGEP->getResultElementType();
966 } else if (auto *Ref = dyn_cast<LoadInst>(I)) {
967 Value *Op = Ref->getPointerOperand();
968 Type *KnownTy = GR->findDeducedElementType(Op);
969 if (!KnownTy)
970 KnownTy = Op->getType();
971 if (Type *ElemTy = getPointeeType(KnownTy))
972 maybeAssignPtrType(Ty, I, ElemTy, UnknownElemTypeI8);
973 } else if (auto *Ref = dyn_cast<GlobalValue>(I)) {
974 if (auto *Fn = dyn_cast<Function>(Ref)) {
975 Ty = SPIRV::getOriginalFunctionType(*Fn);
976 GR->addDeducedElementType(I, Ty);
977 } else {
978 Ty = deduceElementTypeByValueDeep(
979 Ref->getValueType(),
980 Ref->getNumOperands() > 0 ? Ref->getOperand(0) : nullptr, Visited,
981 UnknownElemTypeI8);
982 }
983 } else if (auto *Ref = dyn_cast<AddrSpaceCastInst>(I)) {
984 Type *RefTy = deduceElementTypeHelper(Ref->getPointerOperand(), Visited,
985 UnknownElemTypeI8);
986 maybeAssignPtrType(Ty, I, RefTy, UnknownElemTypeI8);
987 } else if (auto *Ref = dyn_cast<IntToPtrInst>(I)) {
988 maybeAssignPtrType(Ty, I, Ref->getDestTy(), UnknownElemTypeI8);
989 } else if (auto *Ref = dyn_cast<BitCastInst>(I)) {
990 if (Type *Src = Ref->getSrcTy(), *Dest = Ref->getDestTy();
991 isPointerTy(Src) && isPointerTy(Dest))
992 Ty = deduceElementTypeHelper(Ref->getOperand(0), Visited,
993 UnknownElemTypeI8);
994 } else if (auto *Ref = dyn_cast<AtomicCmpXchgInst>(I)) {
995 Value *Op = Ref->getNewValOperand();
996 if (isPointerTy(Op->getType()))
997 Ty = deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8);
998 } else if (auto *Ref = dyn_cast<AtomicRMWInst>(I)) {
999 Value *Op = Ref->getValOperand();
1000 if (isPointerTy(Op->getType()))
1001 Ty = deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8);
1002 } else if (auto *Ref = dyn_cast<PHINode>(I)) {
1003 Type *BestTy = nullptr;
1004 unsigned MaxN = 1;
1005 DenseMap<Type *, unsigned> PhiTys;
1006 for (int i = Ref->getNumIncomingValues() - 1; i >= 0; --i) {
1007 Ty = deduceElementTypeByUsersDeep(Ref->getIncomingValue(i), Visited,
1008 UnknownElemTypeI8);
1009 if (!Ty)
1010 continue;
1011 auto It = PhiTys.try_emplace(Ty, 1);
1012 if (!It.second) {
1013 ++It.first->second;
1014 if (It.first->second > MaxN) {
1015 MaxN = It.first->second;
1016 BestTy = Ty;
1017 }
1018 }
1019 }
1020 if (BestTy)
1021 Ty = BestTy;
1022 } else if (auto *Ref = dyn_cast<SelectInst>(I)) {
1023 for (Value *Op : {Ref->getTrueValue(), Ref->getFalseValue()}) {
1024 Ty = deduceElementTypeByUsersDeep(Op, Visited, UnknownElemTypeI8);
1025 if (Ty)
1026 break;
1027 }
1028 } else if (auto *CI = dyn_cast<CallInst>(I)) {
1029 static StringMap<unsigned> ResTypeByArg = {
1030 {"to_global", 0},
1031 {"to_local", 0},
1032 {"to_private", 0},
1033 {"__spirv_GenericCastToPtr_ToGlobal", 0},
1034 {"__spirv_GenericCastToPtr_ToLocal", 0},
1035 {"__spirv_GenericCastToPtr_ToPrivate", 0},
1036 {"__spirv_GenericCastToPtrExplicit_ToGlobal", 0},
1037 {"__spirv_GenericCastToPtrExplicit_ToLocal", 0},
1038 {"__spirv_GenericCastToPtrExplicit_ToPrivate", 0}};
1039 // TODO: maybe improve performance by caching demangled names
1040
1041 auto *II = dyn_cast<IntrinsicInst>(I);
1042 if (II && (II->getIntrinsicID() == Intrinsic::spv_resource_getbasepointer ||
1043 II->getIntrinsicID() == Intrinsic::spv_resource_getpointer)) {
1044 auto *HandleType = cast<TargetExtType>(II->getOperand(0)->getType());
1045 if (HandleType->getTargetExtName() == "spirv.Image" ||
1046 HandleType->getTargetExtName() == "spirv.SignedImage") {
1047 for (User *U : II->users()) {
1048 Ty = cast<Instruction>(U)->getAccessType();
1049 if (Ty)
1050 break;
1051 }
1052 } else if (HandleType->getTargetExtName() == "spirv.VulkanBuffer") {
1053 // This call is supposed to index into an array
1054 Ty = HandleType->getTypeParameter(0);
1055 if (II->getIntrinsicID() == Intrinsic::spv_resource_getpointer) {
1056 if (Ty->isArrayTy())
1057 Ty = Ty->getArrayElementType();
1058 else {
1059 assert(Ty && Ty->isStructTy());
1060 uint32_t Index =
1061 cast<ConstantInt>(II->getOperand(1))->getZExtValue();
1062 Ty = cast<StructType>(Ty)->getElementType(Index);
1063 }
1064 }
1066 } else {
1067 llvm_unreachable("Unknown handle type for spv_resource_getpointer.");
1068 }
1069 } else if (II && II->getIntrinsicID() ==
1070 Intrinsic::spv_generic_cast_to_ptr_explicit) {
1071 Ty = deduceElementTypeHelper(CI->getArgOperand(0), Visited,
1072 UnknownElemTypeI8);
1073 } else if (Function *CalledF = CI->getCalledFunction()) {
1074 std::string DemangledName =
1075 getOclOrSpirvBuiltinDemangledName(CalledF->getName());
1076 if (DemangledName.length() > 0)
1077 DemangledName = SPIRV::lookupBuiltinNameHelper(DemangledName);
1078 auto AsArgIt = ResTypeByArg.find(DemangledName);
1079 if (AsArgIt != ResTypeByArg.end())
1080 Ty = deduceElementTypeHelper(CI->getArgOperand(AsArgIt->second),
1081 Visited, UnknownElemTypeI8);
1082 else if (Type *KnownRetTy = GR->findDeducedElementType(CalledF))
1083 Ty = KnownRetTy;
1084 }
1085 }
1086
1087 // remember the found relationship
1088 if (Ty && !IgnoreKnownType) {
1089 // specify nested types if needed, otherwise return unchanged
1091 }
1092
1093 return Ty;
1094}
1095
1096// Re-create a type of the value if it has untyped pointer fields, also nested.
1097// Return the original value type if no corrections of untyped pointer
1098// information is found or needed.
1099Type *SPIRVEmitIntrinsics::deduceNestedTypeHelper(User *U,
1100 bool UnknownElemTypeI8) {
1101 std::unordered_set<Value *> Visited;
1102 return deduceNestedTypeHelper(U, U->getType(), Visited, UnknownElemTypeI8);
1103}
1104
1105Type *SPIRVEmitIntrinsics::deduceNestedTypeHelper(
1106 User *U, Type *OrigTy, std::unordered_set<Value *> &Visited,
1107 bool UnknownElemTypeI8) {
1108 if (!U)
1109 return OrigTy;
1110
1111 // maybe already known
1112 if (Type *KnownTy = GR->findDeducedCompositeType(U))
1113 return KnownTy;
1114
1115 // maybe a cycle
1116 if (!Visited.insert(U).second)
1117 return OrigTy;
1118
1119 if (isa<StructType>(OrigTy)) {
1121 bool Change = false;
1122 for (unsigned i = 0; i < U->getNumOperands(); ++i) {
1123 Value *Op = U->getOperand(i);
1124 assert(Op && "Operands should not be null.");
1125 Type *OpTy = Op->getType();
1126 Type *Ty = OpTy;
1127 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
1128 if (Type *NestedTy =
1129 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
1130 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
1131 } else {
1132 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
1133 UnknownElemTypeI8);
1134 }
1135 Tys.push_back(Ty);
1136 Change |= Ty != OpTy;
1137 }
1138 if (Change) {
1139 Type *NewTy = StructType::create(Tys);
1140 GR->addDeducedCompositeType(U, NewTy);
1141 return NewTy;
1142 }
1143 } else if (auto *ArrTy = dyn_cast<ArrayType>(OrigTy)) {
1144 if (Value *Op = U->getNumOperands() > 0 ? U->getOperand(0) : nullptr) {
1145 Type *OpTy = ArrTy->getElementType();
1146 Type *Ty = OpTy;
1147 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
1148 if (Type *NestedTy =
1149 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
1150 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
1151 } else {
1152 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
1153 UnknownElemTypeI8);
1154 }
1155 if (Ty != OpTy) {
1156 Type *NewTy = ArrayType::get(Ty, ArrTy->getNumElements());
1157 GR->addDeducedCompositeType(U, NewTy);
1158 return NewTy;
1159 }
1160 }
1161 } else if (auto *VecTy = dyn_cast<VectorType>(OrigTy)) {
1162 if (Value *Op = U->getNumOperands() > 0 ? U->getOperand(0) : nullptr) {
1163 Type *OpTy = VecTy->getElementType();
1164 Type *Ty = OpTy;
1165 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
1166 if (Type *NestedTy =
1167 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
1168 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
1169 } else {
1170 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
1171 UnknownElemTypeI8);
1172 }
1173 if (Ty != OpTy) {
1174 Type *NewTy = VectorType::get(Ty, VecTy->getElementCount());
1176 return NewTy;
1177 }
1178 }
1179 }
1180
1181 return OrigTy;
1182}
1183
1184Type *SPIRVEmitIntrinsics::deduceElementType(Value *I, bool UnknownElemTypeI8) {
1185 if (Type *Ty = deduceElementTypeHelper(I, UnknownElemTypeI8))
1186 return Ty;
1187 if (!UnknownElemTypeI8)
1188 return nullptr;
1189 insertTodoType(I);
1190 return IntegerType::getInt8Ty(I->getContext());
1191}
1192
1194 Value *PointerOperand) {
1195 Type *PointeeTy = GR->findDeducedElementType(PointerOperand);
1196 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
1197 return nullptr;
1198 auto *PtrTy = dyn_cast<PointerType>(I->getType());
1199 if (!PtrTy)
1200 return I->getType();
1201 if (Type *NestedTy = GR->findDeducedElementType(I))
1202 return getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
1203 return nullptr;
1204}
1205
1206// Try to deduce element type for a call base. Returns false if this is an
1207// indirect function invocation, and true otherwise.
1208bool SPIRVEmitIntrinsics::deduceOperandElementTypeCalledFunction(
1209 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
1210 Type *&KnownElemTy, bool &Incomplete) {
1211 Function *CalledF = CI->getCalledFunction();
1212 if (!CalledF)
1213 return false;
1214 std::string DemangledName =
1216 if (DemangledName.length() > 0 &&
1217 !StringRef(DemangledName).starts_with("llvm.")) {
1218 const SPIRVSubtarget &ST = TM.getSubtarget<SPIRVSubtarget>(*CalledF);
1219 auto [Grp, Opcode, ExtNo] = SPIRV::mapBuiltinToOpcode(
1220 DemangledName, ST.getPreferredInstructionSet());
1221 if (Opcode == SPIRV::OpGroupAsyncCopy) {
1222 for (unsigned i = 0, PtrCnt = 0; i < CI->arg_size() && PtrCnt < 2; ++i) {
1223 Value *Op = CI->getArgOperand(i);
1224 if (!isPointerTy(Op->getType()))
1225 continue;
1226 ++PtrCnt;
1227 if (Type *ElemTy = GR->findDeducedElementType(Op))
1228 KnownElemTy = ElemTy; // src will rewrite dest if both are defined
1229 Ops.push_back(std::make_pair(Op, i));
1230 }
1231 } else if (Grp == SPIRV::Atomic || Grp == SPIRV::AtomicFloating) {
1232 if (CI->arg_size() == 0)
1233 return true;
1234 Value *Op = CI->getArgOperand(0);
1235 if (!isPointerTy(Op->getType()))
1236 return true;
1237 switch (Opcode) {
1238 case SPIRV::OpAtomicFAddEXT:
1239 case SPIRV::OpAtomicFMinEXT:
1240 case SPIRV::OpAtomicFMaxEXT:
1241 case SPIRV::OpAtomicLoad:
1242 case SPIRV::OpAtomicCompareExchangeWeak:
1243 case SPIRV::OpAtomicCompareExchange:
1244 case SPIRV::OpAtomicExchange:
1245 case SPIRV::OpAtomicIAdd:
1246 case SPIRV::OpAtomicISub:
1247 case SPIRV::OpAtomicOr:
1248 case SPIRV::OpAtomicXor:
1249 case SPIRV::OpAtomicAnd:
1250 case SPIRV::OpAtomicUMin:
1251 case SPIRV::OpAtomicUMax:
1252 case SPIRV::OpAtomicSMin:
1253 case SPIRV::OpAtomicSMax: {
1254 KnownElemTy = isPointerTy(CI->getType()) ? getAtomicElemTy(GR, CI, Op)
1255 : CI->getType();
1256 if (!KnownElemTy)
1257 return true;
1258 Incomplete = isTodoType(Op);
1259 Ops.push_back(std::make_pair(Op, 0));
1260 } break;
1261 case SPIRV::OpAtomicStore: {
1262 if (CI->arg_size() < 4)
1263 return true;
1264 Value *ValOp = CI->getArgOperand(3);
1265 KnownElemTy = isPointerTy(ValOp->getType())
1266 ? getAtomicElemTy(GR, CI, Op)
1267 : ValOp->getType();
1268 if (!KnownElemTy)
1269 return true;
1270 Incomplete = isTodoType(Op);
1271 Ops.push_back(std::make_pair(Op, 0));
1272 } break;
1273 }
1274 }
1275 }
1276 return true;
1277}
1278
1279// Try to deduce element type for a function pointer.
1280void SPIRVEmitIntrinsics::deduceOperandElementTypeFunctionPointer(
1281 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
1282 Type *&KnownElemTy, bool IsPostprocessing) {
1283 Value *Op = CI->getCalledOperand();
1284 if (!Op || !isPointerTy(Op->getType()))
1285 return;
1286 Ops.push_back(std::make_pair(Op, std::numeric_limits<unsigned>::max()));
1287 FunctionType *FTy = SPIRV::getOriginalFunctionType(*CI);
1288 bool IsNewFTy = false, IsIncomplete = false;
1290 for (auto &&[ParmIdx, Arg] : llvm::enumerate(CI->args())) {
1291 Type *ArgTy = Arg->getType();
1292 if (ArgTy->isPointerTy()) {
1293 if (Type *ElemTy = GR->findDeducedElementType(Arg)) {
1294 IsNewFTy = true;
1295 ArgTy = getTypedPointerWrapper(ElemTy, getPointerAddressSpace(ArgTy));
1296 if (isTodoType(Arg))
1297 IsIncomplete = true;
1298 } else {
1299 IsIncomplete = true;
1300 }
1301 } else {
1302 ArgTy = FTy->getFunctionParamType(ParmIdx);
1303 }
1304 ArgTys.push_back(ArgTy);
1305 }
1306 Type *RetTy = FTy->getReturnType();
1307 if (CI->getType()->isPointerTy()) {
1308 if (Type *ElemTy = GR->findDeducedElementType(CI)) {
1309 IsNewFTy = true;
1310 RetTy =
1312 if (isTodoType(CI))
1313 IsIncomplete = true;
1314 } else {
1315 IsIncomplete = true;
1316 }
1317 }
1318 if (!IsPostprocessing && IsIncomplete)
1319 insertTodoType(Op);
1320 KnownElemTy =
1321 IsNewFTy ? FunctionType::get(RetTy, ArgTys, FTy->isVarArg()) : FTy;
1322}
1323
1324bool SPIRVEmitIntrinsics::deduceOperandElementTypeFunctionRet(
1325 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
1326 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing,
1327 Type *&KnownElemTy, Value *Op, Function *F) {
1328 KnownElemTy = GR->findDeducedElementType(F);
1329 if (KnownElemTy)
1330 return false;
1331 if (Type *OpElemTy = GR->findDeducedElementType(Op)) {
1332 OpElemTy = normalizeType(OpElemTy);
1333 GR->addDeducedElementType(F, OpElemTy);
1334 GR->addReturnType(
1335 F, TypedPointerType::get(OpElemTy,
1336 getPointerAddressSpace(F->getReturnType())));
1337 // non-recursive update of types in function uses
1338 DenseSet<std::pair<Value *, Value *>> VisitedSubst{std::make_pair(I, Op)};
1339 for (User *U : F->users()) {
1340 CallInst *CI = dyn_cast<CallInst>(U);
1341 if (!CI || CI->getCalledFunction() != F)
1342 continue;
1343 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(CI)) {
1344 if (Type *PrevElemTy = GR->findDeducedElementType(CI)) {
1345 GR->updateAssignType(AssignCI, CI,
1346 getNormalizedPoisonValue(OpElemTy));
1347 propagateElemType(CI, PrevElemTy, VisitedSubst);
1348 }
1349 }
1350 }
1351 // Non-recursive update of types in the function uncomplete returns.
1352 // This may happen just once per a function, the latch is a pair of
1353 // findDeducedElementType(F) / addDeducedElementType(F, ...).
1354 // With or without the latch it is a non-recursive call due to
1355 // IncompleteRets set to nullptr in this call.
1356 if (IncompleteRets)
1357 for (Instruction *IncompleteRetI : *IncompleteRets)
1358 deduceOperandElementType(IncompleteRetI, nullptr, AskOps,
1359 IsPostprocessing);
1360 } else if (IncompleteRets) {
1361 IncompleteRets->insert(I);
1362 }
1363 TypeValidated.insert(I);
1364 return true;
1365}
1366
1367// If the Instruction has Pointer operands with unresolved types, this function
1368// tries to deduce them. If the Instruction has Pointer operands with known
1369// types which differ from expected, this function tries to insert a bitcast to
1370// resolve the issue.
1371void SPIRVEmitIntrinsics::deduceOperandElementType(
1372 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
1373 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing) {
1375 Type *KnownElemTy = nullptr;
1376 bool Incomplete = false;
1377 // look for known basic patterns of type inference
1378 if (auto *Ref = dyn_cast<PHINode>(I)) {
1379 if (!isPointerTy(I->getType()) ||
1380 !(KnownElemTy = GR->findDeducedElementType(I)))
1381 return;
1382 Incomplete = isTodoType(I);
1383 for (unsigned i = 0; i < Ref->getNumIncomingValues(); i++) {
1384 Value *Op = Ref->getIncomingValue(i);
1385 if (isPointerTy(Op->getType()))
1386 Ops.push_back(std::make_pair(Op, i));
1387 }
1388 } else if (auto *Ref = dyn_cast<AddrSpaceCastInst>(I)) {
1389 KnownElemTy = GR->findDeducedElementType(I);
1390 if (!KnownElemTy)
1391 return;
1392 Incomplete = isTodoType(I);
1393 Ops.push_back(std::make_pair(Ref->getPointerOperand(), 0));
1394 } else if (auto *Ref = dyn_cast<BitCastInst>(I)) {
1395 if (!isPointerTy(I->getType()))
1396 return;
1397 KnownElemTy = GR->findDeducedElementType(I);
1398 if (!KnownElemTy)
1399 return;
1400 Incomplete = isTodoType(I);
1401 Ops.push_back(std::make_pair(Ref->getOperand(0), 0));
1402 } else if (auto *Ref = dyn_cast<GetElementPtrInst>(I)) {
1403 if (GR->findDeducedElementType(Ref->getPointerOperand()))
1404 return;
1405 KnownElemTy = Ref->getSourceElementType();
1406 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1408 } else if (auto *Ref = dyn_cast<StructuredGEPInst>(I)) {
1409 if (GR->findDeducedElementType(Ref->getPointerOperand()))
1410 return;
1411 KnownElemTy = Ref->getBaseType();
1412 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1414 } else if (auto *Ref = dyn_cast<LoadInst>(I)) {
1415 KnownElemTy = I->getType();
1416 if (isUntypedPointerTy(KnownElemTy)) {
1417 // A T** loaded back from its alloca comes out opaque, dropping type info.
1418 // When the load is a pointer-to-pointer, type the alloca as that pointer.
1419 Type *LoadedElemTy = GR->findDeducedElementType(I);
1420 if (!LoadedElemTy || !isPointerTyOrWrapper(LoadedElemTy))
1421 return;
1422 Value *Root = Ref->getPointerOperand()->stripPointerCasts();
1423 if (!isa<AllocaInst>(Root))
1424 return;
1425 KnownElemTy = getTypedPointerWrapper(LoadedElemTy,
1426 getPointerAddressSpace(KnownElemTy));
1427 }
1428 Type *PointeeTy = GR->findDeducedElementType(Ref->getPointerOperand());
1429 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
1430 return;
1431 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1433 } else if (auto *Ref = dyn_cast<StoreInst>(I)) {
1434 if (!(KnownElemTy =
1435 reconstructType(Ref->getValueOperand(), false, IsPostprocessing)))
1436 return;
1437 Type *PointeeTy = GR->findDeducedElementType(Ref->getPointerOperand());
1438 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
1439 return;
1440 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1442 } else if (auto *Ref = dyn_cast<AtomicCmpXchgInst>(I)) {
1443 KnownElemTy = isPointerTy(I->getType())
1444 ? getAtomicElemTy(GR, I, Ref->getPointerOperand())
1445 : I->getType();
1446 if (!KnownElemTy)
1447 return;
1448 Incomplete = isTodoType(Ref->getPointerOperand());
1449 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1451 } else if (auto *Ref = dyn_cast<AtomicRMWInst>(I)) {
1452 KnownElemTy = isPointerTy(I->getType())
1453 ? getAtomicElemTy(GR, I, Ref->getPointerOperand())
1454 : I->getType();
1455 if (!KnownElemTy)
1456 return;
1457 Incomplete = isTodoType(Ref->getPointerOperand());
1458 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1460 } else if (auto *Ref = dyn_cast<SelectInst>(I)) {
1461 if (!isPointerTy(I->getType()) ||
1462 !(KnownElemTy = GR->findDeducedElementType(I)))
1463 return;
1464 Incomplete = isTodoType(I);
1465 for (unsigned i = 0; i < Ref->getNumOperands(); i++) {
1466 Value *Op = Ref->getOperand(i);
1467 if (isPointerTy(Op->getType()))
1468 Ops.push_back(std::make_pair(Op, i));
1469 }
1470 } else if (auto *Ref = dyn_cast<ReturnInst>(I)) {
1471 if (!isPointerTy(CurrF->getReturnType()))
1472 return;
1473 Value *Op = Ref->getReturnValue();
1474 if (!Op)
1475 return;
1476 if (deduceOperandElementTypeFunctionRet(I, IncompleteRets, AskOps,
1477 IsPostprocessing, KnownElemTy, Op,
1478 CurrF))
1479 return;
1480 Incomplete = isTodoType(CurrF);
1481 Ops.push_back(std::make_pair(Op, 0));
1482 } else if (auto *Ref = dyn_cast<ICmpInst>(I)) {
1483 if (!isPointerTy(Ref->getOperand(0)->getType()))
1484 return;
1485 Value *Op0 = Ref->getOperand(0);
1486 Value *Op1 = Ref->getOperand(1);
1487 bool Incomplete0 = isTodoType(Op0);
1488 bool Incomplete1 = isTodoType(Op1);
1489 Type *ElemTy1 = GR->findDeducedElementType(Op1);
1490 Type *ElemTy0 = (Incomplete0 && !Incomplete1 && ElemTy1)
1491 ? nullptr
1492 : GR->findDeducedElementType(Op0);
1493 if (ElemTy0) {
1494 KnownElemTy = ElemTy0;
1495 Incomplete = Incomplete0;
1496 Ops.push_back(std::make_pair(Op1, 1));
1497 } else if (ElemTy1) {
1498 KnownElemTy = ElemTy1;
1499 Incomplete = Incomplete1;
1500 Ops.push_back(std::make_pair(Op0, 0));
1501 }
1502 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
1503 if (!CI->isIndirectCall())
1504 deduceOperandElementTypeCalledFunction(CI, Ops, KnownElemTy, Incomplete);
1505 else if (HaveFunPtrs)
1506 deduceOperandElementTypeFunctionPointer(CI, Ops, KnownElemTy,
1507 IsPostprocessing);
1508 }
1509
1510 // There is no enough info to deduce types or all is valid.
1511 if (!KnownElemTy || Ops.size() == 0)
1512 return;
1513
1514 LLVMContext &Ctx = CurrF->getContext();
1515 IRBuilder<> B(Ctx);
1516 for (auto &OpIt : Ops) {
1517 Value *Op = OpIt.first;
1518 if (AskOps && !AskOps->contains(Op))
1519 continue;
1520 Type *AskTy = nullptr;
1521 CallInst *AskCI = nullptr;
1522 if (IsPostprocessing && AskOps) {
1523 AskTy = GR->findDeducedElementType(Op);
1524 AskCI = GR->findAssignPtrTypeInstr(Op);
1525 assert(AskTy && AskCI);
1526 }
1527 Type *Ty = AskTy ? AskTy : GR->findDeducedElementType(Op);
1528 if (Ty == KnownElemTy)
1529 continue;
1530 Value *OpTyVal = getNormalizedPoisonValue(KnownElemTy);
1531 Type *OpTy = Op->getType();
1532 // Do not let a non-pointer element type clobber an already-deduced pointer
1533 // pointee.
1534 bool WouldClobberPtrWithNonPtr = Ty && isPointerTyOrWrapper(Ty) &&
1535 !isPointerTyOrWrapper(KnownElemTy) &&
1537 if (Op->hasUseList() && !WouldClobberPtrWithNonPtr &&
1538 (!Ty || AskTy || isUntypedPointerTy(Ty) || isTodoType(Op))) {
1539 Type *PrevElemTy = GR->findDeducedElementType(Op);
1540 GR->addDeducedElementType(Op, normalizeType(KnownElemTy));
1541 // check if KnownElemTy is complete
1542 if (!Incomplete)
1543 eraseTodoType(Op);
1544 else if (!IsPostprocessing)
1545 insertTodoType(Op);
1546 // check if there is existing Intrinsic::spv_assign_ptr_type instruction
1547 CallInst *AssignCI = AskCI ? AskCI : GR->findAssignPtrTypeInstr(Op);
1548 if (AssignCI == nullptr) {
1549 Instruction *User = dyn_cast<Instruction>(Op->use_begin()->get());
1550 setInsertPointSkippingPhis(B, User ? User->getNextNode() : I);
1551 CallInst *CI =
1552 buildIntrWithMD(Intrinsic::spv_assign_ptr_type, {OpTy}, OpTyVal, Op,
1553 {B.getInt32(getPointerAddressSpace(OpTy))}, B);
1554 GR->addAssignPtrTypeInstr(Op, CI);
1555 } else {
1556 GR->updateAssignType(AssignCI, Op, OpTyVal);
1557 DenseSet<std::pair<Value *, Value *>> VisitedSubst{
1558 std::make_pair(I, Op)};
1559 propagateElemTypeRec(Op, KnownElemTy, PrevElemTy, VisitedSubst);
1560 }
1561 } else {
1562 eraseTodoType(Op);
1563 CallInst *PtrCastI =
1564 buildSpvPtrcast(I->getParent()->getParent(), Op, KnownElemTy);
1565 if (OpIt.second == std::numeric_limits<unsigned>::max())
1566 dyn_cast<CallInst>(I)->setCalledOperand(PtrCastI);
1567 else
1568 I->setOperand(OpIt.second, PtrCastI);
1569 }
1570 }
1571 TypeValidated.insert(I);
1572}
1573
1574void SPIRVEmitIntrinsics::replaceMemInstrUses(Instruction *Old,
1575 Instruction *New,
1576 IRBuilder<> &B) {
1577 while (!Old->user_empty()) {
1578 auto *U = Old->user_back();
1579 if (isAssignTypeInstr(U)) {
1580 B.SetInsertPoint(U);
1581 SmallVector<Value *, 2> Args = {New, U->getOperand(1)};
1582 CallInst *AssignCI =
1583 B.CreateIntrinsic(Intrinsic::spv_assign_type, {New->getType()}, Args);
1584 GR->addAssignPtrTypeInstr(New, AssignCI);
1585 U->eraseFromParent();
1586 } else if (isMemInstrToReplace(U) || isa<ReturnInst>(U) ||
1587 isa<CallInst>(U)) {
1588 U->replaceUsesOfWith(Old, New);
1589 // For a `llvm.spv.abort` call whose composite message argument was
1590 // rewritten to a value-id (i32), also retarget the call to a matching
1591 // intrinsic declaration so the IR verifier is satisfied. The SPIR-V
1592 // type of the value is tracked via the GlobalRegistry, so the selector
1593 // still emits OpAbortKHR with the original composite type.
1594 if (auto *CI = dyn_cast<CallInst>(U);
1595 CI && CI->getIntrinsicID() == Intrinsic::spv_abort) {
1596 Type *NewArgTy = New->getType();
1597 Type *ExpectedArgTy = CI->getFunctionType()->getParamType(0);
1598 if (NewArgTy != ExpectedArgTy) {
1599 Module *M = CI->getModule();
1601 M, Intrinsic::spv_abort, {NewArgTy});
1602 CI->setCalledFunction(NewF);
1603 }
1604 }
1605 } else if (isa<PHINode>(U) || isa<SelectInst>(U) || isa<FreezeInst>(U)) {
1606 // Aggregate-typed PHIs, selects and freezes have already been mutated to
1607 // the i32 value-id type up front in runOnFunction, so only the operand
1608 // needs replacing here; their extractvalue users are lowered to
1609 // spv_extractv by visitExtractValueInst.
1610 assert(U->getType() == New->getType() &&
1611 "aggregate PHI/select/freeze should have been mutated to value-id "
1612 "type");
1613 U->replaceUsesOfWith(Old, New);
1614 } else {
1615 llvm_unreachable("illegal aggregate intrinsic user");
1616 }
1617 }
1618 New->copyMetadata(*Old);
1619 Old->eraseFromParent();
1620}
1621
1622void SPIRVEmitIntrinsics::preprocessUndefs(IRBuilder<> &B) {
1623 const SPIRVSubtarget *STI = TM.getSubtargetImpl(*CurrF);
1624 bool HasPoisonExt =
1625 STI->canUseExtension(SPIRV::Extension::SPV_KHR_poison_freeze);
1626 SmallVector<Instruction *, 16> Insts;
1627 for (auto &I : instructions(CurrF))
1628 Insts.push_back(&I);
1629
1630 for (Instruction *I : Insts) {
1631 bool BPrepared = false;
1632 for (auto &Op : I->operands()) {
1633 auto *AggrUndef = dyn_cast<UndefValue>(Op);
1634 if (!AggrUndef || !Op->getType()->isAggregateType())
1635 continue;
1636 if (HasPoisonExt && isa<PoisonValue>(AggrUndef))
1637 continue;
1638 if (isa<PoisonValue>(AggrUndef))
1639 LLVM_DEBUG(dbgs() << "SPV_KHR_poison_freeze is not enabled. Poison is "
1640 "lowered as undef\n");
1641
1642 if (!BPrepared) {
1644 BPrepared = true;
1645 }
1646 auto *IntrUndef = B.CreateIntrinsic(Intrinsic::spv_undef, {});
1647 I->replaceUsesOfWith(Op, IntrUndef);
1648 AggrConsts[IntrUndef] = AggrUndef;
1649 AggrConstTypes[IntrUndef] = AggrUndef->getType();
1650 }
1651 }
1652}
1653
1654void SPIRVEmitIntrinsics::preprocessPoisons(IRBuilder<> &B) {
1655 const SPIRVSubtarget *STI = TM.getSubtargetImpl(*CurrF);
1656 if (!STI->canUseExtension(SPIRV::Extension::SPV_KHR_poison_freeze))
1657 return;
1658
1659 for (Instruction &I : instructions(CurrF)) {
1660 bool BPrepared = false;
1661 auto *Phi = dyn_cast<PHINode>(&I);
1662
1663 for (unsigned Idx = 0; Idx < I.getNumOperands(); ++Idx) {
1664 Value *Op = I.getOperand(Idx);
1666 if (!Poison || Op->getType()->isMetadataTy())
1667 continue;
1668
1669 Type *OpTy = Op->getType();
1670 Value *Replacement = nullptr;
1671 if (OpTy->isAggregateType()) {
1672 if (!BPrepared) {
1674 BPrepared = true;
1675 }
1676 auto *Call =
1677 B.CreateIntrinsic(Intrinsic::spv_poison, {B.getInt32Ty()}, {});
1678 AggrConsts[Call] = Poison;
1679 AggrConstTypes[Call] = OpTy;
1680 Replacement = Call;
1681 } else {
1682 if (Phi)
1683 B.SetInsertPoint(Phi->getIncomingBlock(Idx)->getTerminator());
1684 else if (!BPrepared) {
1686 BPrepared = true;
1687 }
1688 Replacement = B.CreateIntrinsic(Intrinsic::spv_poison, {OpTy}, {});
1689 }
1690 I.setOperand(Idx, Replacement);
1691 }
1692 }
1693}
1694
1695// Simplify addrspacecast(null) instructions to ConstantPointerNull of the
1696// target type. Casting null always yields null, and this avoids SPIR-V
1697// lowering issues where the null gets typed as an integer instead of a
1698// pointer.
1699void SPIRVEmitIntrinsics::simplifyNullAddrSpaceCasts() {
1700 for (Instruction &I : make_early_inc_range(instructions(CurrF)))
1701 if (auto *ASC = dyn_cast<AddrSpaceCastInst>(&I))
1702 if (isa<ConstantPointerNull>(ASC->getPointerOperand())) {
1703 ASC->replaceAllUsesWith(
1705 ASC->eraseFromParent();
1706 }
1707}
1708
1709void SPIRVEmitIntrinsics::preprocessCompositeConstants(IRBuilder<> &B) {
1710 const SPIRVSubtarget *STI = TM.getSubtargetImpl(*CurrF);
1711 bool HasPoisonExt =
1712 STI->canUseExtension(SPIRV::Extension::SPV_KHR_poison_freeze);
1713 std::queue<Instruction *> Worklist;
1714 for (auto &I : instructions(CurrF))
1715 Worklist.push(&I);
1716
1717 while (!Worklist.empty()) {
1718 auto *I = Worklist.front();
1719 bool IsPhi = isa<PHINode>(I), BPrepared = false;
1720 assert(I);
1721 bool KeepInst = false;
1722 for (const auto &Op : I->operands()) {
1723 Constant *AggrConst = nullptr;
1724 Type *ResTy = nullptr;
1725 if (auto *COp = dyn_cast<ConstantVector>(Op)) {
1726 AggrConst = COp;
1727 ResTy = COp->getType();
1728 } else if (auto *COp = dyn_cast<ConstantArray>(Op)) {
1729 AggrConst = COp;
1730 ResTy = B.getInt32Ty();
1731 } else if (auto *COp = dyn_cast<ConstantStruct>(Op)) {
1732 AggrConst = COp;
1733 ResTy = B.getInt32Ty();
1734 } else if (auto *COp = dyn_cast<ConstantDataArray>(Op)) {
1735 AggrConst = COp;
1736 ResTy = B.getInt32Ty();
1737 } else if (auto *COp = dyn_cast<ConstantAggregateZero>(Op)) {
1738 AggrConst = COp;
1739 ResTy = Op->getType()->isVectorTy() ? COp->getType() : B.getInt32Ty();
1740 }
1741 if (AggrConst) {
1742 auto PrepareInsert = [&]() {
1743 if (BPrepared)
1744 return;
1745 IsPhi ? B.SetInsertPointPastAllocas(I->getParent()->getParent())
1746 : B.SetInsertPoint(I);
1747 BPrepared = true;
1748 };
1750 if (auto *COp = dyn_cast<ConstantDataSequential>(Op))
1751 for (unsigned i = 0; i < COp->getNumElements(); ++i)
1752 Args.push_back(COp->getElementAsConstant(i));
1753 else
1754 for (Value *Op : AggrConst->operands()) {
1755 // Simplify addrspacecast(null) to null in the target address space
1756 // so that null pointers get the correct pointer type when lowered.
1757 if (auto *CE = dyn_cast<ConstantExpr>(Op);
1758 CE && CE->getOpcode() == Instruction::AddrSpaceCast &&
1759 isa<ConstantPointerNull>(CE->getOperand(0)))
1761 if (HasPoisonExt && isa<PoisonValue>(Op)) {
1762 PrepareInsert();
1763 Type *PoisonTy = Op->getType();
1764 if (PoisonTy->isAggregateType()) {
1765 auto *Call = B.CreateIntrinsic(Intrinsic::spv_poison,
1766 {B.getInt32Ty()}, {});
1767 AggrConsts[Call] = cast<PoisonValue>(Op);
1768 AggrConstTypes[Call] = PoisonTy;
1769 Op = Call;
1770 } else {
1771 Op = B.CreateIntrinsic(Intrinsic::spv_poison, {PoisonTy}, {});
1772 }
1773 }
1774 Args.push_back(Op);
1775 }
1776 PrepareInsert();
1777 auto *CI =
1778 B.CreateIntrinsic(Intrinsic::spv_const_composite, {ResTy}, {Args});
1779 Worklist.push(CI);
1780 I->replaceUsesOfWith(Op, CI);
1781 KeepInst = true;
1782 AggrConsts[CI] = AggrConst;
1783 AggrConstTypes[CI] = deduceNestedTypeHelper(AggrConst, false);
1784 }
1785 }
1786 if (!KeepInst)
1787 Worklist.pop();
1788 }
1789}
1790
1792 IRBuilder<> &B) {
1793 LLVMContext &Ctx = I->getContext();
1795 B.CreateIntrinsic(Intrinsic::spv_assign_decoration, {I->getType()},
1796 {I, MetadataAsValue::get(Ctx, MDNode::get(Ctx, {Node}))});
1797}
1798
1800 unsigned RoundingModeDeco,
1801 IRBuilder<> &B) {
1802 LLVMContext &Ctx = I->getContext();
1804 MDNode *RoundingModeNode = MDNode::get(
1805 Ctx,
1807 ConstantInt::get(Int32Ty, SPIRV::Decoration::FPRoundingMode)),
1808 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, RoundingModeDeco))});
1809 createDecorationIntrinsic(I, RoundingModeNode, B);
1810}
1811
1813 IRBuilder<> &B) {
1814 LLVMContext &Ctx = I->getContext();
1816 MDNode *SaturatedConversionNode =
1817 MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(
1818 Int32Ty, SPIRV::Decoration::SaturatedConversion))});
1819 createDecorationIntrinsic(I, SaturatedConversionNode, B);
1820}
1821
1826
1827Instruction *SPIRVEmitIntrinsics::visitCallInst(CallInst &Call) {
1828 if (!Call.isInlineAsm())
1829 return &Call;
1830
1831 LLVMContext &Ctx = CurrF->getContext();
1832 // TODO: this does not retain elementtype info for memory constraints, which
1833 // in turn means that we lower them into pointers to i8, rather than
1834 // pointers to elementtype; this can be fixed during reverse translation
1835 // but we should correct it here, possibly by tweaking the function
1836 // type to take TypedPointerType args.
1837 Constant *TyC = UndefValue::get(SPIRV::getOriginalFunctionType(Call));
1838 MDString *ConstraintString =
1839 MDString::get(Ctx, SPIRV::getOriginalAsmConstraints(Call));
1841 buildMD(TyC),
1842 MetadataAsValue::get(Ctx, MDNode::get(Ctx, ConstraintString))};
1843 for (unsigned OpIdx = 0; OpIdx < Call.arg_size(); OpIdx++)
1844 Args.push_back(Call.getArgOperand(OpIdx));
1845
1847 B.SetInsertPoint(&Call);
1848 B.CreateIntrinsic(Intrinsic::spv_inline_asm, {Args});
1849 return &Call;
1850}
1851
1852// Use a tip about rounding mode to create a decoration.
1853void SPIRVEmitIntrinsics::useRoundingMode(ConstrainedFPIntrinsic *FPI,
1854 IRBuilder<> &B) {
1855 std::optional<RoundingMode> RM = FPI->getRoundingMode();
1856 if (!RM.has_value())
1857 return;
1858 unsigned RoundingModeDeco = std::numeric_limits<unsigned>::max();
1859 switch (RM.value()) {
1860 default:
1861 // ignore unknown rounding modes
1862 break;
1863 case RoundingMode::NearestTiesToEven:
1864 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTE;
1865 break;
1866 case RoundingMode::TowardNegative:
1867 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTN;
1868 break;
1869 case RoundingMode::TowardPositive:
1870 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTP;
1871 break;
1872 case RoundingMode::TowardZero:
1873 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTZ;
1874 break;
1875 case RoundingMode::Dynamic:
1876 case RoundingMode::NearestTiesToAway:
1877 // TODO: check if supported
1878 break;
1879 }
1880 if (RoundingModeDeco == std::numeric_limits<unsigned>::max())
1881 return;
1882 // Convert the tip about rounding mode into a decoration record.
1883 createRoundingModeDecoration(FPI, RoundingModeDeco, B);
1884}
1885
1886Instruction *SPIRVEmitIntrinsics::visitSwitchInst(SwitchInst &I) {
1887 BasicBlock *ParentBB = I.getParent();
1888 Function *F = ParentBB->getParent();
1889 IRBuilder<> B(ParentBB);
1890 B.SetInsertPoint(&I);
1891 SmallVector<Value *, 4> Args;
1893 Args.push_back(I.getCondition());
1894 BBCases.push_back(I.getDefaultDest());
1895 Args.push_back(BlockAddress::get(F, I.getDefaultDest()));
1896 for (auto &Case : I.cases()) {
1897 Args.push_back(Case.getCaseValue());
1898 BBCases.push_back(Case.getCaseSuccessor());
1899 Args.push_back(BlockAddress::get(F, Case.getCaseSuccessor()));
1900 }
1901 CallInst *NewI = B.CreateIntrinsic(Intrinsic::spv_switch,
1902 {I.getOperand(0)->getType()}, {Args});
1903 // remove switch to avoid its unneeded and undesirable unwrap into branches
1904 // and conditions
1905 replaceAllUsesWith(&I, NewI);
1906 I.eraseFromParent();
1907 // insert artificial and temporary instruction to preserve valid CFG,
1908 // it will be removed after IR translation pass
1909 B.SetInsertPoint(ParentBB);
1910 IndirectBrInst *BrI = B.CreateIndirectBr(
1911 Constant::getNullValue(PointerType::getUnqual(ParentBB->getContext())),
1912 BBCases.size());
1913 for (BasicBlock *BBCase : BBCases)
1914 BrI->addDestination(BBCase);
1915 return BrI;
1916}
1917
1919 return GEP->getNumIndices() > 0 && match(GEP->getOperand(1), m_Zero());
1920}
1921
1922Instruction *SPIRVEmitIntrinsics::visitIntrinsicInst(IntrinsicInst &I) {
1923 auto *SGEP = dyn_cast<StructuredGEPInst>(&I);
1924 if (!SGEP)
1925 return &I;
1926
1927 IRBuilder<> B(I.getParent());
1928 B.SetInsertPoint(&I);
1929 SmallVector<Type *, 2> Types = {I.getType(), I.getOperand(0)->getType()};
1930 SmallVector<Value *, 4> Args;
1931 Args.push_back(/* inBounds= */ B.getInt1(true));
1932 Args.push_back(I.getOperand(0));
1933 Args.push_back(/* zero index */ B.getInt32(0));
1934 for (unsigned J = 0; J < SGEP->getNumIndices(); ++J)
1935 Args.push_back(SGEP->getIndexOperand(J));
1936
1937 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, Types, Args);
1938 replaceAllUsesWithAndErase(B, &I, NewI);
1939 return NewI;
1940}
1941
1942Instruction *SPIRVEmitIntrinsics::visitGetElementPtrInst(GetElementPtrInst &I) {
1943 IRBuilder<> B(I.getParent());
1944 B.SetInsertPoint(&I);
1945
1946 // OpPtrAccessChain requires a scalar pointer result; scalarize per-lane
1947 // GEPs that return <N x ptr> and rebuild the vector via insertelement.
1948 if (auto *RetVTy = dyn_cast<FixedVectorType>(I.getType())) {
1949 unsigned N = RetVTy->getNumElements();
1950 Value *PtrOp = I.getPointerOperand();
1951 bool PtrIsVec = isa<VectorType>(PtrOp->getType());
1952 Type *ResultPtrTy = RetVTy->getElementType();
1953 Type *ScalarPtrTy = PtrOp->getType()->getScalarType();
1954 SmallVector<Type *, 2> GepTypes = {ResultPtrTy, ScalarPtrTy};
1955 Value *InBounds = B.getInt1(I.isInBounds());
1956 Type *LanePointeeTy = getGEPType(&I);
1957 Type *SrcElemTy = I.getSourceElementType();
1958
1959 // Pin the lane pointee type on the vector operand and on each extracted
1960 // lane so the prelegalizer wraps them as OpTypeVector/OpTypePointer of
1961 // the right element type instead of defaulting to i8.
1962 if (PtrIsVec)
1963 GR->buildAssignPtr(B, SrcElemTy, PtrOp);
1964
1965 Value *VecResult = PoisonValue::get(RetVTy);
1966 for (unsigned Lane = 0; Lane < N; ++Lane) {
1967 Value *LaneIdx = B.getInt32(Lane);
1968 Value *ScalarPtr = PtrOp;
1969 if (PtrIsVec) {
1970 SmallVector<Type *, 3> ExtractTypes = {ScalarPtrTy, PtrOp->getType(),
1971 LaneIdx->getType()};
1972 ScalarPtr = B.CreateIntrinsic(Intrinsic::spv_extractelt, {ExtractTypes},
1973 {PtrOp, LaneIdx});
1974 GR->buildAssignPtr(B, SrcElemTy, ScalarPtr);
1975 }
1976 SmallVector<Value *, 4> Args;
1977 Args.push_back(InBounds);
1978 Args.push_back(ScalarPtr);
1979 for (Value *Idx : I.indices()) {
1980 if (isa<VectorType>(Idx->getType()))
1981 Args.push_back(B.CreateExtractElement(Idx, LaneIdx));
1982 else
1983 Args.push_back(Idx);
1984 }
1985 Value *ScalarGep = B.CreateIntrinsic(Intrinsic::spv_gep, GepTypes, Args);
1986 GR->buildAssignPtr(B, LanePointeeTy, ScalarGep);
1987 VecResult = B.CreateInsertElement(VecResult, ScalarGep, LaneIdx);
1988 }
1989
1990 auto *NewI = cast<Instruction>(VecResult);
1991 replaceAllUsesWithAndErase(B, &I, NewI);
1992
1993 if (CallInst *Old = GR->findAssignPtrTypeInstr(NewI)) {
1994 Old->eraseFromParent();
1995 GR->addAssignPtrTypeInstr(NewI, nullptr);
1996 }
1998 GR->buildAssignPtr(B, LanePointeeTy, NewI);
1999
2000 return NewI;
2001 }
2002
2004 // Logical SPIR-V cannot use the OpPtrAccessChain instruction. If the first
2005 // index of the GEP is not 0, then we need to try to adjust it.
2006 //
2007 // If the GEP is doing byte addressing, try to rebuild the full access chain
2008 // from the type of the pointer.
2009 if (getByteAddressingMultiplier(I.getSourceElementType())) {
2010 return buildLogicalAccessChainFromGEP(I);
2011 }
2012
2013 // Look for the array-to-pointer decay. If this is the pattern
2014 // we can adjust the types, and prepend a 0 to the indices.
2015 Value *PtrOp = I.getPointerOperand();
2016 Type *SrcElemTy = I.getSourceElementType();
2017 Type *DeducedPointeeTy = deduceElementType(PtrOp, true);
2018
2019 if (auto *ArrTy = dyn_cast<ArrayType>(DeducedPointeeTy)) {
2020 if (ArrTy->getElementType() == SrcElemTy) {
2021 SmallVector<Value *> NewIndices;
2022 Type *FirstIdxType = I.getOperand(1)->getType();
2023 NewIndices.push_back(ConstantInt::get(FirstIdxType, 0));
2024 for (Value *Idx : I.indices())
2025 NewIndices.push_back(Idx);
2026
2027 SmallVector<Type *, 2> Types = {I.getType(), I.getPointerOperandType()};
2028 SmallVector<Value *, 4> Args;
2029 Args.push_back(B.getInt1(I.isInBounds()));
2030 Args.push_back(I.getPointerOperand());
2031 Args.append(NewIndices.begin(), NewIndices.end());
2032
2033 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
2034 replaceAllUsesWithAndErase(B, &I, NewI);
2035 return NewI;
2036 }
2037 }
2038 }
2039
2040 SmallVector<Type *, 2> Types = {I.getType(), I.getOperand(0)->getType()};
2041 SmallVector<Value *, 4> Args;
2042 Args.push_back(B.getInt1(I.isInBounds()));
2043 llvm::append_range(Args, I.operands());
2044 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
2045 replaceAllUsesWithAndErase(B, &I, NewI);
2046 return NewI;
2047}
2048
2049Instruction *SPIRVEmitIntrinsics::visitBitCastInst(BitCastInst &I) {
2050 IRBuilder<> B(I.getParent());
2051 B.SetInsertPoint(&I);
2052 Value *Source = I.getOperand(0);
2053
2054 // SPIR-V, contrary to LLVM 17+ IR, supports bitcasts between pointers of
2055 // varying element types. In case of IR coming from older versions of LLVM
2056 // such bitcasts do not provide sufficient information, should be just skipped
2057 // here, and handled in insertPtrCastOrAssignTypeInstr.
2058 if (isPointerTy(I.getType())) {
2059 replaceAllUsesWith(&I, Source);
2060 I.eraseFromParent();
2061 return nullptr;
2062 }
2063
2064 SmallVector<Type *, 2> Types = {I.getType(), Source->getType()};
2065 SmallVector<Value *> Args(I.op_begin(), I.op_end());
2066 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_bitcast, {Types}, {Args});
2067 replaceAllUsesWithAndErase(B, &I, NewI);
2068 return NewI;
2069}
2070
2071void SPIRVEmitIntrinsics::insertAssignPtrTypeTargetExt(
2072 TargetExtType *AssignedType, Value *V, IRBuilder<> &B) {
2073 Type *VTy = V->getType();
2074
2075 // A couple of sanity checks.
2076 assert((isPointerTy(VTy)) && "Expect a pointer type!");
2077 if (Type *ElemTy = getPointeeType(VTy))
2078 if (ElemTy != AssignedType)
2079 report_fatal_error("Unexpected pointer element type!");
2080
2081 CallInst *AssignCI = GR->findAssignPtrTypeInstr(V);
2082 if (!AssignCI) {
2083 GR->buildAssignType(B, AssignedType, V);
2084 return;
2085 }
2086
2087 Type *CurrentType =
2089 cast<MetadataAsValue>(AssignCI->getOperand(1))->getMetadata())
2090 ->getType();
2091 if (CurrentType == AssignedType)
2092 return;
2093
2094 // Builtin types cannot be redeclared or casted.
2095 if (CurrentType->isTargetExtTy())
2096 report_fatal_error("Type mismatch " + CurrentType->getTargetExtName() +
2097 "/" + AssignedType->getTargetExtName() +
2098 " for value " + V->getName(),
2099 false);
2100
2101 // Our previous guess about the type seems to be wrong, let's update
2102 // inferred type according to a new, more precise type information.
2103 GR->updateAssignType(AssignCI, V, getNormalizedPoisonValue(AssignedType));
2104}
2105
2106void SPIRVEmitIntrinsics::replacePointerOperandWithPtrCast(
2107 Instruction *I, Value *Pointer, Type *ExpectedElementType,
2108 unsigned OperandToReplace, IRBuilder<> &B) {
2109 TypeValidated.insert(I);
2110
2111 // Do not emit spv_ptrcast if Pointer's element type is ExpectedElementType
2112 Type *PointerElemTy = deduceElementTypeHelper(Pointer, false);
2113 if (PointerElemTy == ExpectedElementType ||
2114 isEquivalentTypes(PointerElemTy, ExpectedElementType))
2115 return;
2116
2118 Value *ExpectedElementVal = getNormalizedPoisonValue(ExpectedElementType);
2119 MetadataAsValue *VMD = buildMD(ExpectedElementVal);
2120 unsigned AddressSpace = getPointerAddressSpace(Pointer->getType());
2121 bool FirstPtrCastOrAssignPtrType = true;
2122
2123 // Do not emit new spv_ptrcast if equivalent one already exists or when
2124 // spv_assign_ptr_type already targets this pointer with the same element
2125 // type.
2126 if (Pointer->hasUseList()) {
2127 for (auto User : Pointer->users()) {
2128 auto *II = dyn_cast<IntrinsicInst>(User);
2129 if (!II ||
2130 (II->getIntrinsicID() != Intrinsic::spv_assign_ptr_type &&
2131 II->getIntrinsicID() != Intrinsic::spv_ptrcast) ||
2132 II->getOperand(0) != Pointer)
2133 continue;
2134
2135 // There is some spv_ptrcast/spv_assign_ptr_type already targeting this
2136 // pointer.
2137 FirstPtrCastOrAssignPtrType = false;
2138 if (II->getOperand(1) != VMD ||
2139 dyn_cast<ConstantInt>(II->getOperand(2))->getSExtValue() !=
2141 continue;
2142
2143 // The spv_ptrcast/spv_assign_ptr_type targeting this pointer is of the
2144 // same element type and address space.
2145 if (II->getIntrinsicID() != Intrinsic::spv_ptrcast)
2146 return;
2147
2148 // This must be a spv_ptrcast, do not emit new if this one has the same BB
2149 // as I. Otherwise, search for other spv_ptrcast/spv_assign_ptr_type.
2150 if (II->getParent() != I->getParent())
2151 continue;
2152
2153 I->setOperand(OperandToReplace, II);
2154 return;
2155 }
2156 }
2157
2158 // Never replace an already-deduced pointer pointee with a non-pointer one.
2159 // The conflicting use comes from a mis-deduced expected type. Leave the
2160 // operand untouched rather than emitting a ptrcast that re-introduces
2161 // the collapsed type at the use site.
2162 if (PointerElemTy && isPointerTyOrWrapper(PointerElemTy) &&
2163 !isPointerTyOrWrapper(ExpectedElementType) &&
2164 tracesToPointerAlloca(Pointer))
2165 return;
2166
2167 if (isa<Instruction>(Pointer) || isa<Argument>(Pointer)) {
2168 if (FirstPtrCastOrAssignPtrType) {
2169 // If this would be the first spv_ptrcast, do not emit spv_ptrcast and
2170 // emit spv_assign_ptr_type instead.
2171 GR->buildAssignPtr(B, ExpectedElementType, Pointer);
2172 return;
2173 } else if (isTodoType(Pointer)) {
2174 eraseTodoType(Pointer);
2175 if (!isa<CallInst>(Pointer) && !isaGEP(Pointer) &&
2176 !isa<AllocaInst>(Pointer)) {
2177 // If this wouldn't be the first spv_ptrcast but existing type info is
2178 // uncomplete, update spv_assign_ptr_type arguments.
2179 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(Pointer)) {
2180 Type *PrevElemTy = GR->findDeducedElementType(Pointer);
2181 assert(PrevElemTy);
2182 DenseSet<std::pair<Value *, Value *>> VisitedSubst{
2183 std::make_pair(I, Pointer)};
2184 GR->updateAssignType(AssignCI, Pointer, ExpectedElementVal);
2185 propagateElemType(Pointer, PrevElemTy, VisitedSubst);
2186 } else {
2187 GR->buildAssignPtr(B, ExpectedElementType, Pointer);
2188 }
2189 return;
2190 }
2191 }
2192 }
2193
2194 // Emit spv_ptrcast
2195 SmallVector<Type *, 2> Types = {Pointer->getType(), Pointer->getType()};
2196 SmallVector<Value *, 2> Args = {Pointer, VMD, B.getInt32(AddressSpace)};
2197 auto *PtrCastI = B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
2198 I->setOperand(OperandToReplace, PtrCastI);
2199 // We need to set up a pointee type for the newly created spv_ptrcast.
2200 GR->buildAssignPtr(B, ExpectedElementType, PtrCastI);
2201}
2202
2203void SPIRVEmitIntrinsics::insertPtrCastOrAssignTypeInstr(Instruction *I,
2204 IRBuilder<> &B) {
2205 // Handle basic instructions:
2206 StoreInst *SI = dyn_cast<StoreInst>(I);
2207 if (IsKernelArgInt8(CurrF, SI)) {
2208 replacePointerOperandWithPtrCast(
2209 I, SI->getValueOperand(), IntegerType::getInt8Ty(CurrF->getContext()),
2210 0, B);
2211 }
2212 if (SI) {
2213 Value *Op = SI->getValueOperand();
2214 Value *Pointer = SI->getPointerOperand();
2215 Type *OpTy = Op->getType();
2216 if (auto *OpI = dyn_cast<Instruction>(Op)) {
2217 OpTy = restoreMutatedType(GR, OpI, OpTy);
2218 if (auto It = AggrConstTypes.find(OpI); It != AggrConstTypes.end())
2219 OpTy = It->second;
2220 }
2221 if (OpTy == Op->getType())
2222 OpTy = deduceElementTypeByValueDeep(OpTy, Op, false);
2223 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 1, B);
2224 return;
2225 }
2226 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
2227 Value *Pointer = LI->getPointerOperand();
2228 Type *OpTy = LI->getType();
2229 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
2230 if (Type *ElemTy = GR->findDeducedElementType(LI)) {
2231 OpTy = getTypedPointerWrapper(ElemTy, PtrTy->getAddressSpace());
2232 } else {
2233 Type *NewOpTy = OpTy;
2234 OpTy = deduceElementTypeByValueDeep(OpTy, LI, false);
2235 if (OpTy == NewOpTy)
2236 insertTodoType(Pointer);
2237 }
2238 }
2239 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 0, B);
2240 return;
2241 }
2242 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
2243 Value *Pointer = GEPI->getPointerOperand();
2244 Type *OpTy = nullptr;
2245
2246 // Logical SPIR-V is not allowed to use Op*PtrAccessChain instructions. If
2247 // the first index is 0, then we can trivially lower to OpAccessChain. If
2248 // not we need to try to rewrite the GEP. We avoid adding a pointer cast at
2249 // this time, and will rewrite the GEP when visiting it.
2250 if (TM.getSubtargetImpl()->isLogicalSPIRV() && !isFirstIndexZero(GEPI)) {
2251 return;
2252 }
2253
2254 // In all cases, fall back to the GEP type if type scavenging failed.
2255 if (!OpTy)
2256 OpTy = GEPI->getSourceElementType();
2257
2258 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 0, B);
2259 if (isNestedPointer(OpTy))
2260 insertTodoType(Pointer);
2261 return;
2262 }
2263
2264 // TODO: review and merge with existing logics:
2265 // Handle calls to builtins (non-intrinsics):
2266 CallInst *CI = dyn_cast<CallInst>(I);
2267 if (!CI || CI->isIndirectCall() || CI->isInlineAsm() ||
2269 return;
2270
2271 // collect information about formal parameter types
2272 std::string DemangledName =
2274 Function *CalledF = CI->getCalledFunction();
2275 SmallVector<Type *, 4> CalledArgTys;
2276 bool HaveTypes = false;
2277 for (unsigned OpIdx = 0; OpIdx < CalledF->arg_size(); ++OpIdx) {
2278 Argument *CalledArg = CalledF->getArg(OpIdx);
2279 Type *ArgType = CalledArg->getType();
2280 if (!isPointerTy(ArgType)) {
2281 CalledArgTys.push_back(nullptr);
2282 } else if (Type *ArgTypeElem = getPointeeType(ArgType)) {
2283 CalledArgTys.push_back(ArgTypeElem);
2284 HaveTypes = true;
2285 } else {
2286 Type *ElemTy = GR->findDeducedElementType(CalledArg);
2287 if (!ElemTy && hasPointeeTypeAttr(CalledArg))
2288 ElemTy = getPointeeTypeByAttr(CalledArg);
2289 if (!ElemTy) {
2290 ElemTy = getPointeeTypeByCallInst(DemangledName, CalledF, OpIdx);
2291 if (ElemTy) {
2292 GR->addDeducedElementType(CalledArg, normalizeType(ElemTy));
2293 } else {
2294 for (User *U : CalledArg->users()) {
2295 if (Instruction *Inst = dyn_cast<Instruction>(U)) {
2296 if ((ElemTy = deduceElementTypeHelper(Inst, false)) != nullptr)
2297 break;
2298 }
2299 }
2300 }
2301 }
2302 HaveTypes |= ElemTy != nullptr;
2303 CalledArgTys.push_back(ElemTy);
2304 }
2305 }
2306
2307 if (DemangledName.empty() && !HaveTypes)
2308 return;
2309
2310 for (unsigned OpIdx = 0; OpIdx < CI->arg_size(); OpIdx++) {
2311 Value *ArgOperand = CI->getArgOperand(OpIdx);
2312 if (!isPointerTy(ArgOperand->getType()))
2313 continue;
2314
2315 // Constants (nulls/undefs) are handled in insertAssignPtrTypeIntrs()
2316 if (!isa<Instruction>(ArgOperand) && !isa<Argument>(ArgOperand)) {
2317 // However, we may have assumptions about the formal argument's type and
2318 // may have a need to insert a ptr cast for the actual parameter of this
2319 // call.
2320 Argument *CalledArg = CalledF->getArg(OpIdx);
2321 if (!GR->findDeducedElementType(CalledArg))
2322 continue;
2323 }
2324
2325 Type *ExpectedType =
2326 OpIdx < CalledArgTys.size() ? CalledArgTys[OpIdx] : nullptr;
2327 if (!ExpectedType && !DemangledName.empty())
2328 ExpectedType = SPIRV::parseBuiltinCallArgumentBaseType(
2329 DemangledName, OpIdx, I->getContext());
2330 if (!ExpectedType || ExpectedType->isVoidTy())
2331 continue;
2332
2333 if (ExpectedType->isTargetExtTy() &&
2335 insertAssignPtrTypeTargetExt(cast<TargetExtType>(ExpectedType),
2336 ArgOperand, B);
2337 else
2338 replacePointerOperandWithPtrCast(CI, ArgOperand, ExpectedType, OpIdx, B);
2339 }
2340}
2341
2342Instruction *SPIRVEmitIntrinsics::visitInsertElementInst(InsertElementInst &I) {
2343 // If it's a <1 x Type> vector type, don't modify it. It's not a legal vector
2344 // type in LLT and IRTranslator will replace it by the scalar.
2345 if (isVector1(I.getType()))
2346 return &I;
2347
2348 SmallVector<Type *, 4> Types = {I.getType(), I.getOperand(0)->getType(),
2349 I.getOperand(1)->getType(),
2350 I.getOperand(2)->getType()};
2351 IRBuilder<> B(I.getParent());
2352 B.SetInsertPoint(&I);
2353 SmallVector<Value *> Args(I.op_begin(), I.op_end());
2354 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_insertelt, {Types}, {Args});
2355 replaceAllUsesWithAndErase(B, &I, NewI);
2356 return NewI;
2357}
2358
2360SPIRVEmitIntrinsics::visitExtractElementInst(ExtractElementInst &I) {
2361 // If it's a <1 x Type> vector type, don't modify it. It's not a legal vector
2362 // type in LLT and IRTranslator will replace it by the scalar.
2363 if (isVector1(I.getVectorOperandType()))
2364 return &I;
2365
2366 IRBuilder<> B(I.getParent());
2367 B.SetInsertPoint(&I);
2368 SmallVector<Type *, 3> Types = {I.getType(), I.getVectorOperandType(),
2369 I.getIndexOperand()->getType()};
2370 SmallVector<Value *, 2> Args = {I.getVectorOperand(), I.getIndexOperand()};
2371 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_extractelt, {Types}, {Args});
2372 replaceAllUsesWithAndErase(B, &I, NewI);
2373 return NewI;
2374}
2375
2376Instruction *SPIRVEmitIntrinsics::visitInsertValueInst(InsertValueInst &I) {
2377 IRBuilder<> B(I.getParent());
2378 B.SetInsertPoint(&I);
2379 SmallVector<Type *, 1> Types = {I.getInsertedValueOperand()->getType()};
2381 Value *AggregateOp = I.getAggregateOperand();
2382 if (isa<UndefValue>(AggregateOp))
2383 Args.push_back(UndefValue::get(B.getInt32Ty()));
2384 else
2385 Args.push_back(AggregateOp);
2386 Args.push_back(I.getInsertedValueOperand());
2387 for (auto &Op : I.indices())
2388 Args.push_back(B.getInt32(Op));
2389 Instruction *NewI =
2390 B.CreateIntrinsic(Intrinsic::spv_insertv, {Types}, {Args});
2391 replaceMemInstrUses(&I, NewI, B);
2392 return NewI;
2393}
2394
2395Instruction *SPIRVEmitIntrinsics::visitExtractValueInst(ExtractValueInst &I) {
2396 IRBuilder<> B(I.getParent());
2397 B.SetInsertPoint(&I);
2398 if (I.getAggregateOperand()->getType()->isAggregateType()) {
2399 // Mutate an aggregate-returning spv_extractv producer to i32 so
2400 // IRTranslator does not see a multi-register value.
2401 CallBase *CB = dyn_cast<CallBase>(I.getAggregateOperand());
2402 if (!CB || CB->getIntrinsicID() != Intrinsic::spv_extractv)
2403 return &I;
2404 CB->mutateType(B.getInt32Ty());
2405 }
2406 SmallVector<Value *> Args(I.operands());
2407 for (auto &Op : I.indices())
2408 Args.push_back(B.getInt32(Op));
2409 auto *NewI =
2410 B.CreateIntrinsic(Intrinsic::spv_extractv, {I.getType()}, {Args});
2411 replaceAllUsesWithAndErase(B, &I, NewI);
2412 return NewI;
2413}
2414
2415Instruction *SPIRVEmitIntrinsics::visitLoadInst(LoadInst &I) {
2416 if (!I.getType()->isAggregateType())
2417 return &I;
2418 IRBuilder<> B(I.getParent());
2419 B.SetInsertPoint(&I);
2420 TrackConstants = false;
2421 const auto *TLI = TM.getSubtargetImpl()->getTargetLowering();
2423 TLI->getLoadMemOperandFlags(I, CurrF->getDataLayout());
2424
2425 unsigned IntrinsicId;
2426 SmallVector<Value *, 4> Args = {I.getPointerOperand(), B.getInt16(Flags)};
2427 if (!I.isAtomic()) {
2428 IntrinsicId = Intrinsic::spv_load;
2429 Args.push_back(B.getInt32(I.getAlign().value()));
2430 } else {
2431 IntrinsicId = Intrinsic::spv_atomic_load;
2432 Args.push_back(B.getInt8(static_cast<uint8_t>(I.getOrdering())));
2433 }
2434 CallInst *NewI =
2435 B.CreateIntrinsic(IntrinsicId, {I.getOperand(0)->getType()}, Args);
2436
2437 replaceMemInstrUses(&I, NewI, B);
2438 return NewI;
2439}
2440
2441Instruction *SPIRVEmitIntrinsics::visitStoreInst(StoreInst &I) {
2442 if (!AggrStores.contains(&I))
2443 return &I;
2444 IRBuilder<> B(I.getParent());
2445 B.SetInsertPoint(&I);
2446 TrackConstants = false;
2447 const auto *TLI = TM.getSubtargetImpl()->getTargetLowering();
2449 TLI->getStoreMemOperandFlags(I, CurrF->getDataLayout());
2450 auto *PtrOp = I.getPointerOperand();
2451
2452 if (I.getValueOperand()->getType()->isAggregateType()) {
2453 // It is possible that what used to be an ExtractValueInst has been replaced
2454 // with a call to the spv_extractv intrinsic, and that said call hasn't
2455 // had its return type replaced with i32 during the dedicated pass (because
2456 // it was emitted later); we have to handle this here, because IRTranslator
2457 // cannot deal with multi-register types at the moment.
2458 CallBase *CB = dyn_cast<CallBase>(I.getValueOperand());
2459 assert(CB && CB->getIntrinsicID() == Intrinsic::spv_extractv &&
2460 "Unexpected argument of aggregate type, should be spv_extractv!");
2461 CB->mutateType(B.getInt32Ty());
2462 }
2463
2464 unsigned IntrinsicId;
2465 SmallVector<Value *, 4> Args = {I.getValueOperand(), PtrOp,
2466 B.getInt16(Flags)};
2467 if (!I.isAtomic()) {
2468 IntrinsicId = Intrinsic::spv_store;
2469 Args.push_back(B.getInt32(I.getAlign().value()));
2470 } else {
2471 IntrinsicId = Intrinsic::spv_atomic_store;
2472 Args.push_back(B.getInt8(static_cast<uint8_t>(I.getOrdering())));
2473 }
2474 auto *NewI = B.CreateIntrinsic(
2475 IntrinsicId, {I.getValueOperand()->getType(), PtrOp->getType()}, Args);
2476 NewI->copyMetadata(I);
2477 I.eraseFromParent();
2478 return NewI;
2479}
2480
2481Instruction *SPIRVEmitIntrinsics::visitAllocaInst(AllocaInst &I) {
2482 Value *ArraySize = nullptr;
2483 if (I.isArrayAllocation()) {
2484 const SPIRVSubtarget *STI = TM.getSubtargetImpl(*I.getFunction());
2485 if (!STI->canUseExtension(
2486 SPIRV::Extension::SPV_INTEL_variable_length_array))
2488 "array allocation: this instruction requires the following "
2489 "SPIR-V extension: SPV_INTEL_variable_length_array",
2490 false);
2491 ArraySize = I.getArraySize();
2492 }
2493 IRBuilder<> B(I.getParent());
2494 B.SetInsertPoint(&I);
2495 TrackConstants = false;
2496 Type *PtrTy = I.getType();
2497 auto *NewI =
2498 ArraySize
2499 ? B.CreateIntrinsic(Intrinsic::spv_alloca_array,
2500 {PtrTy, ArraySize->getType()},
2501 {ArraySize, B.getInt32(I.getAlign().value())})
2502 : B.CreateIntrinsic(Intrinsic::spv_alloca, {PtrTy},
2503 {B.getInt32(I.getAlign().value())});
2504 replaceAllUsesWithAndErase(B, &I, NewI);
2505 return NewI;
2506}
2507
2508Instruction *SPIRVEmitIntrinsics::visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
2509 assert(I.getType()->isAggregateType() && "Aggregate result is expected");
2510 IRBuilder<> B(I.getParent());
2511 B.SetInsertPoint(&I);
2512 SmallVector<Value *> Args(I.operands());
2513 Args.push_back(B.getInt32(
2514 static_cast<uint32_t>(getMemScope(I.getContext(), I.getSyncScopeID()))));
2515 // Per SPIR-V spec atomic ops must combine the ordering bits with the
2516 // storage-class bit.
2517 const SPIRVSubtarget &ST = TM.getSubtarget<SPIRVSubtarget>(*I.getFunction());
2518 unsigned AS = I.getPointerOperand()->getType()->getPointerAddressSpace();
2519 uint32_t ScSem = static_cast<uint32_t>(
2521 Args.push_back(B.getInt32(
2522 static_cast<uint32_t>(getMemSemantics(I.getSuccessOrdering())) | ScSem));
2523 Args.push_back(B.getInt32(
2524 static_cast<uint32_t>(getMemSemantics(I.getFailureOrdering())) | ScSem));
2525 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_cmpxchg,
2526 {I.getPointerOperand()->getType()}, {Args});
2527 replaceMemInstrUses(&I, NewI, B);
2528 return NewI;
2529}
2530
2531static bool isAbortCall(const Instruction &I, const SPIRVSubtarget &ST) {
2532 auto *CI = dyn_cast<CallInst>(&I);
2533 if (!CI)
2534 return false;
2535 switch (CI->getIntrinsicID()) {
2536 case Intrinsic::spv_abort:
2537 return true;
2538 case Intrinsic::trap:
2539 case Intrinsic::ubsantrap:
2540 // When the extension is enabled, selection lowers these to OpAbortKHR.
2541 return ST.canUseExtension(SPIRV::Extension::SPV_KHR_abort);
2542 default:
2543 return false;
2544 }
2545}
2546
2547// The OpAbortKHR instruction itself is a block terminator, so we don't need to
2548// emit an extra OpUnreachable instruction.
2550 const SPIRVSubtarget &ST) {
2551 // Find a previous non-debug instruction.
2552 const Instruction *Prev = I.getPrevNode();
2553 while (Prev && Prev->isDebugOrPseudoInst())
2554 Prev = Prev->getPrevNode();
2555
2556 if (Prev && isAbortCall(*Prev, ST))
2557 return true;
2558
2560 *I.getParent(),
2561 [&ST](const Instruction &II) { return isAbortCall(II, ST); }) &&
2562 "abort-like call must be the last non-debug instruction before its "
2563 "block's terminator");
2564 return false;
2565}
2566
2567Instruction *SPIRVEmitIntrinsics::visitUnreachableInst(UnreachableInst &I) {
2568 const SPIRVSubtarget &ST = TM.getSubtarget<SPIRVSubtarget>(*I.getFunction());
2569 if (precededByAbortIntrinsic(I, ST))
2570 return &I;
2571 IRBuilder<> B(&I);
2572 B.CreateIntrinsic(Intrinsic::spv_unreachable, {});
2573 return &I;
2574}
2575
2576static bool
2577shouldEmitIntrinsicsForGlobalValue(const GlobalVariableUsers &GVUsers,
2578 const GlobalVariable &GV,
2579 const Function *F) {
2580 // Skip special artificial variables.
2581 static const StringSet<> ArtificialGlobals{"llvm.global.annotations",
2582 "llvm.compiler.used", "llvm.used"};
2583
2584 if (ArtificialGlobals.contains(GV.getName()))
2585 return false;
2586
2587 auto &UserFunctions = GVUsers.getTransitiveUserFunctions(GV);
2588 if (UserFunctions.contains(F))
2589 return true;
2590
2591 // Do not emit the intrinsics in this function, it's going to be emitted on
2592 // the functions that reference it.
2593 if (!UserFunctions.empty())
2594 return false;
2595
2596 // Emit definitions for globals that are not referenced by any function on the
2597 // first function definition.
2598 const Module &M = *F->getParent();
2599 const Function &FirstDefinition = *M.getFunctionDefs().begin();
2600 return F == &FirstDefinition;
2601}
2602
2603Value *SPIRVEmitIntrinsics::buildSpvUndefComposite(Type *AggrTy,
2604 IRBuilder<> &B) {
2605 auto MakeLeaf = [&](Type *ElemTy) -> Instruction * {
2606 auto *Leaf = B.CreateIntrinsic(Intrinsic::spv_undef, {});
2607 AggrConsts[Leaf] = PoisonValue::get(ElemTy);
2608 AggrConstTypes[Leaf] = ElemTy;
2609 return Leaf;
2610 };
2611 SmallVector<Value *, 4> Elems;
2612 if (auto *ArrTy = dyn_cast<ArrayType>(AggrTy)) {
2613 Elems.assign(ArrTy->getNumElements(), MakeLeaf(ArrTy->getElementType()));
2614 } else {
2615 auto *StructTy = cast<StructType>(AggrTy);
2616 DenseMap<Type *, Instruction *> LeafByType;
2617 for (unsigned I = 0; I < StructTy->getNumElements(); ++I) {
2618 Type *ElemTy = StructTy->getContainedType(I);
2619 auto &Entry = LeafByType[ElemTy];
2620 if (!Entry)
2621 Entry = MakeLeaf(ElemTy);
2622 Elems.push_back(Entry);
2623 }
2624 }
2625 auto *Composite = B.CreateIntrinsic(Intrinsic::spv_const_composite,
2626 {B.getInt32Ty()}, Elems);
2627 AggrConsts[Composite] = PoisonValue::get(AggrTy);
2628 AggrConstTypes[Composite] = AggrTy;
2629 return Composite;
2630}
2631
2632void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
2633 IRBuilder<> &B) {
2634
2635 if (!shouldEmitIntrinsicsForGlobalValue(GVUsers, GV, CurrF))
2636 return;
2637
2638 Constant *Init = nullptr;
2639 if (hasInitializer(&GV)) {
2640 // Deduce element type and store results in Global Registry.
2641 // Result is ignored, because TypedPointerType is not supported
2642 // by llvm IR general logic.
2643 deduceElementTypeHelper(&GV, false);
2644 Init = GV.getInitializer();
2645 Value *InitOp = Init;
2646 if (isa<UndefValue>(Init) && Init->getType()->isAggregateType()) {
2647 const SPIRVSubtarget *STI = TM.getSubtargetImpl();
2648 bool UsePoison =
2649 isa<PoisonValue>(Init) &&
2650 STI->canUseExtension(SPIRV::Extension::SPV_KHR_poison_freeze);
2651 if (UsePoison) {
2652 auto *Call =
2653 B.CreateIntrinsic(Intrinsic::spv_poison, {B.getInt32Ty()}, {});
2654 AggrConsts[Call] = cast<PoisonValue>(Init);
2655 AggrConstTypes[Call] = Init->getType();
2656 InitOp = Call;
2657 } else {
2658 InitOp = buildSpvUndefComposite(Init->getType(), B);
2659 }
2660 }
2661 Type *Ty = isAggrConstForceInt32(Init) ? B.getInt32Ty() : Init->getType();
2662 Constant *Const = isAggrConstForceInt32(Init) ? B.getInt32(1) : Init;
2663 auto *InitInst = B.CreateIntrinsic(Intrinsic::spv_init_global,
2664 {GV.getType(), Ty}, {&GV, Const});
2665 InitInst->setArgOperand(1, InitOp);
2666 }
2667 if (!Init && GV.use_empty())
2668 B.CreateIntrinsic(Intrinsic::spv_unref_global, GV.getType(), &GV);
2669}
2670
2671// Return true, if we can't decide what is the pointee type now and will get
2672// back to the question later. Return false is spv_assign_ptr_type is not needed
2673// or can be inserted immediately.
2674bool SPIRVEmitIntrinsics::insertAssignPtrTypeIntrs(Instruction *I,
2675 IRBuilder<> &B,
2676 bool UnknownElemTypeI8) {
2678 if (!isPointerTy(I->getType()) || !requireAssignType(I))
2679 return false;
2680
2682 if (Type *ElemTy = deduceElementType(I, UnknownElemTypeI8)) {
2683 GR->buildAssignPtr(B, ElemTy, I);
2684 return false;
2685 }
2686 return true;
2687}
2688
2689void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
2690 IRBuilder<> &B) {
2691 // TODO: extend the list of functions with known result types
2692 static StringMap<unsigned> ResTypeWellKnown = {
2693 {"async_work_group_copy", WellKnownTypes::Event},
2694 {"async_work_group_strided_copy", WellKnownTypes::Event},
2695 {"__spirv_GroupAsyncCopy", WellKnownTypes::Event}};
2696
2698
2699 bool IsKnown = false;
2700 if (auto *CI = dyn_cast<CallInst>(I)) {
2701 if (!CI->isIndirectCall() && !CI->isInlineAsm() &&
2702 CI->getCalledFunction() && !CI->getCalledFunction()->isIntrinsic()) {
2703 Function *CalledF = CI->getCalledFunction();
2704 std::string DemangledName =
2706 FPDecorationId DecorationId = FPDecorationId::NONE;
2707 if (DemangledName.length() > 0)
2708 DemangledName =
2709 SPIRV::lookupBuiltinNameHelper(DemangledName, &DecorationId);
2710 auto ResIt = ResTypeWellKnown.find(DemangledName);
2711 if (ResIt != ResTypeWellKnown.end()) {
2712 IsKnown = true;
2714 switch (ResIt->second) {
2715 case WellKnownTypes::Event:
2716 GR->buildAssignType(
2717 B, TargetExtType::get(I->getContext(), "spirv.Event"), I);
2718 break;
2719 }
2720 }
2721 // check if a floating rounding mode or saturation info is present
2722 switch (DecorationId) {
2723 default:
2724 break;
2725 case FPDecorationId::SAT:
2727 break;
2728 case FPDecorationId::RTE:
2730 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTE, B);
2731 break;
2732 case FPDecorationId::RTZ:
2734 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTZ, B);
2735 break;
2736 case FPDecorationId::RTP:
2738 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTP, B);
2739 break;
2740 case FPDecorationId::RTN:
2742 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTN, B);
2743 break;
2744 }
2745 }
2746 }
2747
2748 Type *Ty = I->getType();
2749 if (!IsKnown && !Ty->isVoidTy() && !isPointerTy(Ty) && requireAssignType(I)) {
2751 Type *TypeToAssign = Ty;
2752 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2753 if (isSpvAggrPlaceholder(II)) {
2754 auto It = AggrConstTypes.find(II);
2755 if (It == AggrConstTypes.end())
2756 report_fatal_error("Unknown composite intrinsic type");
2757 TypeToAssign = It->second;
2758 } else if (II->getIntrinsicID() == Intrinsic::spv_poison) {
2759 if (auto It = AggrConstTypes.find(II); It != AggrConstTypes.end())
2760 TypeToAssign = It->second;
2761 }
2762 } else if (auto It = AggrConstTypes.find(I); It != AggrConstTypes.end())
2763 TypeToAssign = It->second;
2764 TypeToAssign = restoreMutatedType(GR, I, TypeToAssign);
2765 GR->buildAssignType(B, TypeToAssign, I);
2766 }
2767 for (const auto &Op : I->operands()) {
2769 // Check GetElementPtrConstantExpr case.
2771 (isa<GEPOperator>(Op) ||
2772 (cast<ConstantExpr>(Op)->getOpcode() == CastInst::IntToPtr)))) {
2774 Type *OpTy = Op->getType();
2775 if (isa<UndefValue>(Op) && OpTy->isAggregateType()) {
2776 CallInst *AssignCI =
2777 buildIntrWithMD(Intrinsic::spv_assign_type, {B.getInt32Ty()}, Op,
2778 UndefValue::get(B.getInt32Ty()), {}, B);
2779 GR->addAssignPtrTypeInstr(Op, AssignCI);
2780 } else if (!isa<Instruction>(Op)) {
2781 Type *OpTy = Op->getType();
2782 Type *OpTyElem = getPointeeType(OpTy);
2783 if (OpTyElem) {
2784 GR->buildAssignPtr(B, OpTyElem, Op);
2785 } else if (isPointerTy(OpTy)) {
2786 Type *ElemTy = GR->findDeducedElementType(Op);
2787 GR->buildAssignPtr(B, ElemTy ? ElemTy : deduceElementType(Op, true),
2788 Op);
2789 } else {
2790 Value *OpTyVal = Op;
2791 if (OpTy->isTargetExtTy()) {
2792 // We need to do this in order to be consistent with how target ext
2793 // types are handled in `processInstrAfterVisit`
2794 OpTyVal = getNormalizedPoisonValue(OpTy);
2795 }
2796 CallInst *AssignCI =
2797 buildIntrWithMD(Intrinsic::spv_assign_type, {OpTy},
2798 getNormalizedPoisonValue(OpTy), OpTyVal, {}, B);
2799 GR->addAssignPtrTypeInstr(OpTyVal, AssignCI);
2800 }
2801 }
2802 }
2803 }
2804}
2805
2806bool SPIRVEmitIntrinsics::shouldTryToAddMemAliasingDecoration(
2807 Instruction *Inst) {
2808 const SPIRVSubtarget *STI = TM.getSubtargetImpl(*Inst->getFunction());
2809 if (!STI->canUseExtension(SPIRV::Extension::SPV_INTEL_memory_access_aliasing))
2810 return false;
2811 // Add aliasing decorations to internal load and store intrinsics.
2812 // Do not attach them to store atomic or load atomic intrinsics / instructions
2813 // since the extension is inconsistent at the moment (we cannot add the
2814 // decoration to atomic stores because they do not have an id).
2815 return match(Inst,
2817}
2818
2819void SPIRVEmitIntrinsics::insertSpirvDecorations(Instruction *I,
2820 IRBuilder<> &B) {
2821 if (MDNode *MD = I->getMetadata("spirv.Decorations")) {
2823 B.CreateIntrinsic(Intrinsic::spv_assign_decoration, {I->getType()},
2824 {I, MetadataAsValue::get(I->getContext(), MD)});
2825 }
2826 // Lower alias.scope/noalias metadata
2827 {
2828 auto processMemAliasingDecoration = [&](unsigned Kind) {
2829 if (MDNode *AliasListMD = I->getMetadata(Kind)) {
2830 if (shouldTryToAddMemAliasingDecoration(I)) {
2831 uint32_t Dec = Kind == LLVMContext::MD_alias_scope
2832 ? SPIRV::Decoration::AliasScopeINTEL
2833 : SPIRV::Decoration::NoAliasINTEL;
2835 I, ConstantInt::get(B.getInt32Ty(), Dec),
2836 MetadataAsValue::get(I->getContext(), AliasListMD)};
2838 B.CreateIntrinsic(Intrinsic::spv_assign_aliasing_decoration,
2839 {I->getType()}, {Args});
2840 }
2841 }
2842 };
2843 processMemAliasingDecoration(LLVMContext::MD_alias_scope);
2844 processMemAliasingDecoration(LLVMContext::MD_noalias);
2845 }
2846 // MD_fpmath
2847 if (MDNode *MD = I->getMetadata(LLVMContext::MD_fpmath)) {
2848 const SPIRVSubtarget *STI = TM.getSubtargetImpl(*I->getFunction());
2849 bool AllowFPMaxError =
2850 STI->canUseExtension(SPIRV::Extension::SPV_INTEL_fp_max_error);
2851 if (!AllowFPMaxError)
2852 return;
2853
2855 B.CreateIntrinsic(Intrinsic::spv_assign_fpmaxerror_decoration,
2856 {I->getType()},
2857 {I, MetadataAsValue::get(I->getContext(), MD)});
2858 }
2859 if (I->getModule()->getTargetTriple().getVendor() == Triple::AMD &&
2861 // If present, we encode AMDGPU atomic metadata as UserSemantic string
2862 // decorations, which will be parsed during reverse translation.
2863 auto &Ctx = B.getContext();
2864 auto *US = ConstantAsMetadata::get(
2865 ConstantInt::get(B.getInt32Ty(), SPIRV::Decoration::UserSemantic));
2866
2868 if (I->hasMetadata("amdgpu.no.fine.grained.memory"))
2870 Ctx, {US, MDString::get(Ctx, "amdgpu.no.fine.grained.memory")}));
2871 if (I->hasMetadata("amdgpu.no.remote.memory"))
2873 Ctx, {US, MDString::get(Ctx, "amdgpu.no.remote.memory")}));
2874 if (I->hasMetadata("amdgpu.ignore.denormal.mode"))
2876 Ctx, {US, MDString::get(Ctx, "amdgpu.ignore.denormal.mode")}));
2877 if (!MDs.empty())
2878 B.CreateIntrinsic(Intrinsic::spv_assign_decoration, {I->getType()},
2879 {I, MetadataAsValue::get(Ctx, MDNode::get(Ctx, MDs))});
2880 }
2881}
2882
2884 const Module &M,
2886 &FPFastMathDefaultInfoMap,
2887 Function *F) {
2888 auto it = FPFastMathDefaultInfoMap.find(F);
2889 if (it != FPFastMathDefaultInfoMap.end())
2890 return it->second;
2891
2892 // If the map does not contain the entry, create a new one. Initialize it to
2893 // contain all 3 elements sorted by bit width of target type: {half, float,
2894 // double}.
2895 SPIRV::FPFastMathDefaultInfoVector FPFastMathDefaultInfoVec;
2896 FPFastMathDefaultInfoVec.emplace_back(Type::getHalfTy(M.getContext()),
2897 SPIRV::FPFastMathMode::None);
2898 FPFastMathDefaultInfoVec.emplace_back(Type::getFloatTy(M.getContext()),
2899 SPIRV::FPFastMathMode::None);
2900 FPFastMathDefaultInfoVec.emplace_back(Type::getDoubleTy(M.getContext()),
2901 SPIRV::FPFastMathMode::None);
2902 return FPFastMathDefaultInfoMap[F] = std::move(FPFastMathDefaultInfoVec);
2903}
2904
2906 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec,
2907 const Type *Ty) {
2908 size_t BitWidth = Ty->getScalarSizeInBits();
2909 int Index =
2911 BitWidth);
2912 assert(Index >= 0 && Index < 3 &&
2913 "Expected FPFastMathDefaultInfo for half, float, or double");
2914 assert(FPFastMathDefaultInfoVec.size() == 3 &&
2915 "Expected FPFastMathDefaultInfoVec to have exactly 3 elements");
2916 return FPFastMathDefaultInfoVec[Index];
2917}
2918
2919void SPIRVEmitIntrinsics::insertConstantsForFPFastMathDefault(Module &M) {
2920 const SPIRVSubtarget *ST = TM.getSubtargetImpl();
2921 if (!ST->canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2))
2922 return;
2923
2924 // Store the FPFastMathDefaultInfo in the FPFastMathDefaultInfoMap.
2925 // We need the entry point (function) as the key, and the target
2926 // type and flags as the value.
2927 // We also need to check ContractionOff and SignedZeroInfNanPreserve
2928 // execution modes, as they are now deprecated and must be replaced
2929 // with FPFastMathDefaultInfo.
2930 auto Node = M.getNamedMetadata("spirv.ExecutionMode");
2931 if (!Node) {
2932 if (!M.getNamedMetadata("opencl.enable.FP_CONTRACT")) {
2933 // This requires emitting ContractionOff. However, because
2934 // ContractionOff is now deprecated, we need to replace it with
2935 // FPFastMathDefaultInfo with FP Fast Math Mode bitmask set to all 0.
2936 // We need to create the constant for that.
2937
2938 // Create constant instruction with the bitmask flags.
2939 Constant *InitValue =
2940 ConstantInt::get(Type::getInt32Ty(M.getContext()), 0);
2941 // TODO: Reuse constant if there is one already with the required
2942 // value.
2943 [[maybe_unused]] GlobalVariable *GV =
2944 new GlobalVariable(M, // Module
2945 Type::getInt32Ty(M.getContext()), // Type
2946 true, // isConstant
2948 InitValue // Initializer
2949 );
2950 }
2951 return;
2952 }
2953
2954 // The table maps function pointers to their default FP fast math info. It
2955 // can be assumed that the SmallVector is sorted by the bit width of the
2956 // type. The first element is the smallest bit width, and the last element
2957 // is the largest bit width, therefore, we will have {half, float, double}
2958 // in the order of their bit widths.
2959 DenseMap<Function *, SPIRV::FPFastMathDefaultInfoVector>
2960 FPFastMathDefaultInfoMap;
2961
2962 for (unsigned i = 0; i < Node->getNumOperands(); i++) {
2963 MDNode *MDN = cast<MDNode>(Node->getOperand(i));
2964 assert(MDN->getNumOperands() >= 2 && "Expected at least 2 operands");
2966 cast<ConstantAsMetadata>(MDN->getOperand(0))->getValue());
2967 const auto EM =
2969 cast<ConstantAsMetadata>(MDN->getOperand(1))->getValue())
2970 ->getZExtValue();
2971 if (EM == SPIRV::ExecutionMode::FPFastMathDefault) {
2972 assert(MDN->getNumOperands() == 4 &&
2973 "Expected 4 operands for FPFastMathDefault");
2974 const Type *T = cast<ValueAsMetadata>(MDN->getOperand(2))->getType();
2975 unsigned Flags =
2977 cast<ConstantAsMetadata>(MDN->getOperand(3))->getValue())
2978 ->getZExtValue();
2979 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
2980 getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
2981 SPIRV::FPFastMathDefaultInfo &Info =
2982 getFPFastMathDefaultInfo(FPFastMathDefaultInfoVec, T);
2983 Info.FastMathFlags = Flags;
2984 Info.FPFastMathDefault = true;
2985 } else if (EM == SPIRV::ExecutionMode::ContractionOff) {
2986 assert(MDN->getNumOperands() == 2 &&
2987 "Expected no operands for ContractionOff");
2988
2989 // We need to save this info for every possible FP type, i.e. {half,
2990 // float, double, fp128}.
2991 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
2992 getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
2993 for (SPIRV::FPFastMathDefaultInfo &Info : FPFastMathDefaultInfoVec) {
2994 Info.ContractionOff = true;
2995 }
2996 } else if (EM == SPIRV::ExecutionMode::SignedZeroInfNanPreserve) {
2997 assert(MDN->getNumOperands() == 3 &&
2998 "Expected 1 operand for SignedZeroInfNanPreserve");
2999 unsigned TargetWidth =
3001 cast<ConstantAsMetadata>(MDN->getOperand(2))->getValue())
3002 ->getZExtValue();
3003 // We need to save this info only for the FP type with TargetWidth.
3004 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
3005 getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
3008 assert(Index >= 0 && Index < 3 &&
3009 "Expected FPFastMathDefaultInfo for half, float, or double");
3010 assert(FPFastMathDefaultInfoVec.size() == 3 &&
3011 "Expected FPFastMathDefaultInfoVec to have exactly 3 elements");
3012 FPFastMathDefaultInfoVec[Index].SignedZeroInfNanPreserve = true;
3013 }
3014 }
3015
3016 std::unordered_map<unsigned, GlobalVariable *> GlobalVars;
3017 for (auto &[Func, FPFastMathDefaultInfoVec] : FPFastMathDefaultInfoMap) {
3018 if (FPFastMathDefaultInfoVec.empty())
3019 continue;
3020
3021 for (const SPIRV::FPFastMathDefaultInfo &Info : FPFastMathDefaultInfoVec) {
3022 assert(Info.Ty && "Expected target type for FPFastMathDefaultInfo");
3023 // Skip if none of the execution modes was used.
3024 unsigned Flags = Info.FastMathFlags;
3025 if (Flags == SPIRV::FPFastMathMode::None && !Info.ContractionOff &&
3026 !Info.SignedZeroInfNanPreserve && !Info.FPFastMathDefault)
3027 continue;
3028
3029 // Check if flags are compatible.
3030 if (Info.ContractionOff && (Flags & SPIRV::FPFastMathMode::AllowContract))
3031 report_fatal_error("Conflicting FPFastMathFlags: ContractionOff "
3032 "and AllowContract");
3033
3034 if (Info.SignedZeroInfNanPreserve &&
3035 !(Flags &
3036 (SPIRV::FPFastMathMode::NotNaN | SPIRV::FPFastMathMode::NotInf |
3037 SPIRV::FPFastMathMode::NSZ))) {
3038 if (Info.FPFastMathDefault)
3039 report_fatal_error("Conflicting FPFastMathFlags: "
3040 "SignedZeroInfNanPreserve but at least one of "
3041 "NotNaN/NotInf/NSZ is enabled.");
3042 }
3043
3044 if ((Flags & SPIRV::FPFastMathMode::AllowTransform) &&
3045 !((Flags & SPIRV::FPFastMathMode::AllowReassoc) &&
3046 (Flags & SPIRV::FPFastMathMode::AllowContract))) {
3047 report_fatal_error("Conflicting FPFastMathFlags: "
3048 "AllowTransform requires AllowReassoc and "
3049 "AllowContract to be set.");
3050 }
3051
3052 auto it = GlobalVars.find(Flags);
3053 GlobalVariable *GV = nullptr;
3054 if (it != GlobalVars.end()) {
3055 // Reuse existing global variable.
3056 GV = it->second;
3057 } else {
3058 // Create constant instruction with the bitmask flags.
3059 Constant *InitValue =
3060 ConstantInt::get(Type::getInt32Ty(M.getContext()), Flags);
3061 // TODO: Reuse constant if there is one already with the required
3062 // value.
3063 GV = new GlobalVariable(M, // Module
3064 Type::getInt32Ty(M.getContext()), // Type
3065 true, // isConstant
3067 InitValue // Initializer
3068 );
3069 GlobalVars[Flags] = GV;
3070 }
3071 }
3072 }
3073}
3074
3075void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
3076 IRBuilder<> &B) {
3077 auto *II = dyn_cast<IntrinsicInst>(I);
3078 bool IsConstComposite =
3079 II && II->getIntrinsicID() == Intrinsic::spv_const_composite;
3080 if (IsConstComposite && TrackConstants) {
3082 auto t = AggrConsts.find(I);
3083 assert(t != AggrConsts.end());
3084 auto *NewOp =
3085 buildIntrWithMD(Intrinsic::spv_track_constant,
3086 {II->getType(), II->getType()}, t->second, I, {}, B);
3087 replaceAllUsesWith(I, NewOp, false);
3088 NewOp->setArgOperand(0, I);
3089 }
3090 bool IsPhi = isa<PHINode>(I), BPrepared = false;
3091 for (const auto &Op : I->operands()) {
3092 if (isa<PHINode>(I) || isa<SwitchInst>(I) ||
3094 continue;
3095 unsigned OpNo = Op.getOperandNo();
3096 if (II && ((II->getIntrinsicID() == Intrinsic::spv_gep && OpNo == 0) ||
3097 (!II->isBundleOperand(OpNo) &&
3098 II->paramHasAttr(OpNo, Attribute::ImmArg))))
3099 continue;
3100
3101 if (!BPrepared) {
3102 IsPhi ? B.SetInsertPointPastAllocas(I->getParent()->getParent())
3103 : B.SetInsertPoint(I);
3104 BPrepared = true;
3105 }
3106 Type *OpTy = Op->getType();
3107 Type *OpElemTy = GR->findDeducedElementType(Op);
3108 Value *NewOp = Op;
3109 if (OpTy->isTargetExtTy()) {
3110 // Since this value is replaced by poison, we need to do the same in
3111 // `insertAssignTypeIntrs`.
3112 Value *OpTyVal = getNormalizedPoisonValue(OpTy);
3113 NewOp = buildIntrWithMD(Intrinsic::spv_track_constant,
3114 {OpTy, OpTyVal->getType()}, Op, OpTyVal, {}, B);
3115 }
3116 if (!IsConstComposite && isPointerTy(OpTy) && OpElemTy != nullptr &&
3117 OpElemTy != IntegerType::getInt8Ty(I->getContext())) {
3118 SmallVector<Type *, 2> Types = {OpTy, OpTy};
3119 SmallVector<Value *, 2> Args = {
3120 NewOp, buildMD(getNormalizedPoisonValue(OpElemTy)),
3121 B.getInt32(getPointerAddressSpace(OpTy))};
3122 CallInst *PtrCasted =
3123 B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
3124 GR->buildAssignPtr(B, OpElemTy, PtrCasted);
3125 NewOp = PtrCasted;
3126 }
3127 if (NewOp != Op)
3128 I->setOperand(OpNo, NewOp);
3129 }
3130 if (Named.insert(I).second)
3131 emitAssignName(I, B);
3132}
3133
3134Type *SPIRVEmitIntrinsics::deduceFunParamElementType(Function *F,
3135 unsigned OpIdx) {
3136 std::unordered_set<Function *> FVisited;
3137 return deduceFunParamElementType(F, OpIdx, FVisited);
3138}
3139
3140Type *SPIRVEmitIntrinsics::deduceFunParamElementType(
3141 Function *F, unsigned OpIdx, std::unordered_set<Function *> &FVisited) {
3142 // maybe a cycle
3143 if (!FVisited.insert(F).second)
3144 return nullptr;
3145
3146 std::unordered_set<Value *> Visited;
3148 // search in function's call sites
3149 for (User *U : F->users()) {
3150 CallInst *CI = dyn_cast<CallInst>(U);
3151 if (!CI || OpIdx >= CI->arg_size())
3152 continue;
3153 Value *OpArg = CI->getArgOperand(OpIdx);
3154 if (!isPointerTy(OpArg->getType()))
3155 continue;
3156 // maybe we already know operand's element type
3157 if (Type *KnownTy = GR->findDeducedElementType(OpArg))
3158 return KnownTy;
3159 // try to deduce from the operand itself
3160 Visited.clear();
3161 if (Type *Ty = deduceElementTypeHelper(OpArg, Visited, false))
3162 return Ty;
3163 // search in actual parameter's users
3164 for (User *OpU : OpArg->users()) {
3166 if (!Inst || Inst == CI)
3167 continue;
3168 Visited.clear();
3169 if (Type *Ty = deduceElementTypeHelper(Inst, Visited, false))
3170 return Ty;
3171 }
3172 // check if it's a formal parameter of the outer function
3173 if (!CI->getParent() || !CI->getParent()->getParent())
3174 continue;
3175 Function *OuterF = CI->getParent()->getParent();
3176 if (FVisited.find(OuterF) != FVisited.end())
3177 continue;
3178 for (unsigned i = 0; i < OuterF->arg_size(); ++i) {
3179 if (OuterF->getArg(i) == OpArg) {
3180 Lookup.push_back(std::make_pair(OuterF, i));
3181 break;
3182 }
3183 }
3184 }
3185
3186 // search in function parameters
3187 for (auto &Pair : Lookup) {
3188 if (Type *Ty = deduceFunParamElementType(Pair.first, Pair.second, FVisited))
3189 return Ty;
3190 }
3191
3192 return nullptr;
3193}
3194
3195void SPIRVEmitIntrinsics::processParamTypesByFunHeader(Function *F,
3196 IRBuilder<> &B) {
3197 B.SetInsertPointPastAllocas(F);
3198 for (unsigned OpIdx = 0; OpIdx < F->arg_size(); ++OpIdx) {
3199 Argument *Arg = F->getArg(OpIdx);
3200 // Vector-of-pointers arg: deduce pointee from a GEP user so the function
3201 // type isn't emitted with the default i8 pointee.
3202 if (isUntypedPointerVectorTy(Arg->getType()) &&
3203 !GR->findDeducedElementType(Arg)) {
3204 for (User *U : Arg->users()) {
3206 if (GEP && GEP->getPointerOperand() == Arg) {
3207 GR->buildAssignPtr(B, GEP->getSourceElementType(), Arg);
3208 break;
3209 }
3210 }
3211 continue;
3212 }
3213 if (!isUntypedPointerTy(Arg->getType()))
3214 continue;
3215 Type *ElemTy = GR->findDeducedElementType(Arg);
3216 if (ElemTy)
3217 continue;
3218 if (hasPointeeTypeAttr(Arg) &&
3219 (ElemTy = getPointeeTypeByAttr(Arg)) != nullptr) {
3220 GR->buildAssignPtr(B, ElemTy, Arg);
3221 continue;
3222 }
3223 // search in function's call sites
3224 for (User *U : F->users()) {
3225 CallInst *CI = dyn_cast<CallInst>(U);
3226 if (!CI || OpIdx >= CI->arg_size())
3227 continue;
3228 Value *OpArg = CI->getArgOperand(OpIdx);
3229 if (!isPointerTy(OpArg->getType()))
3230 continue;
3231 // maybe we already know operand's element type
3232 if ((ElemTy = GR->findDeducedElementType(OpArg)) != nullptr)
3233 break;
3234 }
3235 if (ElemTy) {
3236 GR->buildAssignPtr(B, ElemTy, Arg);
3237 continue;
3238 }
3239 if (HaveFunPtrs) {
3240 for (User *U : Arg->users()) {
3241 CallInst *CI = dyn_cast<CallInst>(U);
3242 if (CI && !isa<IntrinsicInst>(CI) && CI->isIndirectCall() &&
3243 CI->getCalledOperand() == Arg &&
3244 CI->getParent()->getParent() == CurrF) {
3246 deduceOperandElementTypeFunctionPointer(CI, Ops, ElemTy, false);
3247 if (ElemTy) {
3248 GR->buildAssignPtr(B, ElemTy, Arg);
3249 break;
3250 }
3251 }
3252 }
3253 }
3254 }
3255}
3256
3257void SPIRVEmitIntrinsics::processParamTypes(Function *F, IRBuilder<> &B) {
3258 B.SetInsertPointPastAllocas(F);
3259 for (unsigned OpIdx = 0; OpIdx < F->arg_size(); ++OpIdx) {
3260 Argument *Arg = F->getArg(OpIdx);
3261 if (!isUntypedPointerTy(Arg->getType()))
3262 continue;
3263 Type *ElemTy = GR->findDeducedElementType(Arg);
3264 if (!ElemTy && (ElemTy = deduceFunParamElementType(F, OpIdx)) != nullptr) {
3265 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(Arg)) {
3266 DenseSet<std::pair<Value *, Value *>> VisitedSubst;
3267 GR->updateAssignType(AssignCI, Arg, getNormalizedPoisonValue(ElemTy));
3268 propagateElemType(Arg, IntegerType::getInt8Ty(F->getContext()),
3269 VisitedSubst);
3270 } else {
3271 GR->buildAssignPtr(B, ElemTy, Arg);
3272 }
3273 }
3274 }
3275}
3276
3278 SPIRVGlobalRegistry *GR) {
3279 FunctionType *FTy = F->getFunctionType();
3280 bool IsNewFTy = false;
3282 for (Argument &Arg : F->args()) {
3283 Type *ArgTy = Arg.getType();
3284 if (ArgTy->isPointerTy())
3285 if (Type *ElemTy = GR->findDeducedElementType(&Arg)) {
3286 IsNewFTy = true;
3287 ArgTy = getTypedPointerWrapper(ElemTy, getPointerAddressSpace(ArgTy));
3288 }
3289 ArgTys.push_back(ArgTy);
3290 }
3291 return IsNewFTy
3292 ? FunctionType::get(FTy->getReturnType(), ArgTys, FTy->isVarArg())
3293 : FTy;
3294}
3295
3296bool SPIRVEmitIntrinsics::processFunctionPointers(Module &M) {
3297 SmallVector<Function *> Worklist;
3298 for (auto &F : M) {
3299 if (F.isIntrinsic())
3300 continue;
3301 if (F.isDeclaration()) {
3302 for (User *U : F.users()) {
3303 CallInst *CI = dyn_cast<CallInst>(U);
3304 if (!CI || CI->getCalledFunction() != &F) {
3305 Worklist.push_back(&F);
3306 break;
3307 }
3308 }
3309 } else {
3310 if (F.user_empty())
3311 continue;
3312 Type *FPElemTy = GR->findDeducedElementType(&F);
3313 if (!FPElemTy)
3314 FPElemTy = getFunctionPointerElemType(&F, GR);
3315 for (User *U : F.users()) {
3316 IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
3317 if (!II || II->arg_size() != 3 || II->getOperand(0) != &F)
3318 continue;
3319 if (II->getIntrinsicID() == Intrinsic::spv_assign_ptr_type ||
3320 II->getIntrinsicID() == Intrinsic::spv_ptrcast) {
3322 break;
3323 }
3324 }
3325 }
3326 }
3327 if (Worklist.empty())
3328 return false;
3329
3330 LLVMContext &Ctx = M.getContext();
3332 BasicBlock *BB = BasicBlock::Create(Ctx, "entry", SF);
3333 IRBuilder<> IRB(BB);
3334
3335 for (Function *F : Worklist) {
3337 for (const auto &Arg : F->args())
3338 Args.push_back(getNormalizedPoisonValue(Arg.getType()));
3339 IRB.CreateCall(F, Args);
3340 }
3341 IRB.CreateRetVoid();
3342
3343 return true;
3344}
3345
3346// Apply types parsed from demangled function declarations.
3347void SPIRVEmitIntrinsics::applyDemangledPtrArgTypes(IRBuilder<> &B) {
3348 DenseMap<Function *, CallInst *> Ptrcasts;
3349 for (auto It : FDeclPtrTys) {
3350 Function *F = It.first;
3351 for (auto *U : F->users()) {
3352 CallInst *CI = dyn_cast<CallInst>(U);
3353 if (!CI || CI->getCalledFunction() != F)
3354 continue;
3355 unsigned Sz = CI->arg_size();
3356 for (auto [Idx, ElemTy] : It.second) {
3357 if (Idx >= Sz)
3358 continue;
3359 Value *Param = CI->getArgOperand(Idx);
3360 if (GR->findDeducedElementType(Param) || isa<GlobalValue>(Param))
3361 continue;
3362 if (Argument *Arg = dyn_cast<Argument>(Param)) {
3363 if (!hasPointeeTypeAttr(Arg)) {
3364 B.SetInsertPointPastAllocas(Arg->getParent());
3365 B.SetCurrentDebugLocation(DebugLoc());
3366 GR->buildAssignPtr(B, ElemTy, Arg);
3367 }
3368 } else if (isaGEP(Param)) {
3369 replaceUsesOfWithSpvPtrcast(Param, normalizeType(ElemTy), CI,
3370 Ptrcasts);
3371 } else if (isa<Instruction>(Param)) {
3372 GR->addDeducedElementType(Param, normalizeType(ElemTy));
3373 // insertAssignTypeIntrs() will complete buildAssignPtr()
3374 } else {
3375 B.SetInsertPoint(CI->getParent()
3376 ->getParent()
3377 ->getEntryBlock()
3378 .getFirstNonPHIOrDbgOrAlloca());
3379 GR->buildAssignPtr(B, ElemTy, Param);
3380 }
3381 CallInst *Ref = dyn_cast<CallInst>(Param);
3382 if (!Ref)
3383 continue;
3384 Function *RefF = Ref->getCalledFunction();
3385 if (!RefF || !isPointerTy(RefF->getReturnType()) ||
3386 GR->findDeducedElementType(RefF))
3387 continue;
3388 ElemTy = normalizeType(ElemTy);
3389 GR->addDeducedElementType(RefF, ElemTy);
3390 GR->addReturnType(
3392 ElemTy, getPointerAddressSpace(RefF->getReturnType())));
3393 }
3394 }
3395 }
3396}
3397
3398GetElementPtrInst *
3399SPIRVEmitIntrinsics::simplifyZeroLengthArrayGepInst(GetElementPtrInst *GEP) {
3400 // getelementptr [0 x T], P, 0 (zero), I -> getelementptr T, P, I.
3401 // If type is 0-length array and first index is 0 (zero), drop both the
3402 // 0-length array type and the first index. This is a common pattern in
3403 // the IR, e.g. when using a zero-length array as a placeholder for a
3404 // flexible array such as unbound arrays.
3405 assert(GEP && "GEP is null");
3406 Type *SrcTy = GEP->getSourceElementType();
3407 SmallVector<Value *, 8> Indices(GEP->indices());
3408 ArrayType *ArrTy = dyn_cast<ArrayType>(SrcTy);
3409 if (ArrTy && ArrTy->getNumElements() == 0 && match(Indices[0], m_Zero())) {
3410 Indices.erase(Indices.begin());
3411 SrcTy = ArrTy->getElementType();
3412 return GetElementPtrInst::Create(SrcTy, GEP->getPointerOperand(), Indices,
3413 GEP->getNoWrapFlags(), "",
3414 GEP->getIterator());
3415 }
3416 return nullptr;
3417}
3418
3419void SPIRVEmitIntrinsics::emitUnstructuredLoopControls(Function &F,
3420 IRBuilder<> &B) {
3421 const SPIRVSubtarget *ST = TM.getSubtargetImpl(F);
3422 // Shaders use SPIRVStructurizer which emits OpLoopMerge via spv_loop_merge.
3423 if (ST->isShader())
3424 return;
3425
3426 if (ST->canUseExtension(
3427 SPIRV::Extension::SPV_INTEL_unstructured_loop_controls)) {
3428 for (BasicBlock &BB : F) {
3430 MDNode *LoopMD = Term->getMetadata(LLVMContext::MD_loop);
3431 if (!LoopMD)
3432 continue;
3433
3434 SmallVector<unsigned, 1> Ops =
3436 unsigned LC = Ops[0];
3437 if (LC == SPIRV::LoopControl::None)
3438 continue;
3439
3440 // Emit intrinsic: loop control mask + optional parameters.
3441 B.SetInsertPoint(Term);
3442 SmallVector<Value *, 4> IntrArgs;
3443 for (unsigned Op : Ops)
3444 IntrArgs.push_back(B.getInt32(Op));
3445 B.CreateIntrinsic(Intrinsic::spv_loop_control_intel, IntrArgs);
3446 }
3447 return;
3448 }
3449
3450 // For non-shader targets without the Intel extension, emit OpLoopMerge
3451 // using spv_loop_merge intrinsics, mirroring the structurizer approach.
3452 DominatorTree DT(F);
3453 LoopInfo LI(DT);
3454 if (LI.empty())
3455 return;
3456
3457 for (Loop *L : LI.getLoopsInPreorder()) {
3458 BasicBlock *Latch = L->getLoopLatch();
3459 if (!Latch)
3460 continue;
3461 BasicBlock *MergeBlock = L->getUniqueExitBlock();
3462 if (!MergeBlock)
3463 continue;
3464
3465 // Check for loop unroll metadata on the latch terminator.
3466 SmallVector<unsigned, 1> LoopControlOps =
3468 if (LoopControlOps[0] == SPIRV::LoopControl::None)
3469 continue;
3470
3471 BasicBlock *Header = L->getHeader();
3472 B.SetInsertPoint(Header->getTerminator());
3473 auto *MergeAddress = BlockAddress::get(&F, MergeBlock);
3474 auto *ContinueAddress = BlockAddress::get(&F, Latch);
3475 SmallVector<Value *, 4> Args = {MergeAddress, ContinueAddress};
3476 for (unsigned Imm : LoopControlOps)
3477 Args.emplace_back(B.getInt32(Imm));
3478 B.CreateIntrinsic(Intrinsic::spv_loop_merge, {Args});
3479 }
3480}
3481
3482bool SPIRVEmitIntrinsics::runOnFunction(Function &Func) {
3483 if (Func.isDeclaration())
3484 return false;
3485
3486 const SPIRVSubtarget &ST = TM.getSubtarget<SPIRVSubtarget>(Func);
3487 GR = ST.getSPIRVGlobalRegistry();
3488
3489 if (!CurrF)
3490 HaveFunPtrs =
3491 ST.canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers);
3492
3493 CurrF = &Func;
3494 IRBuilder<> B(Func.getContext());
3495 AggrConsts.clear();
3496 AggrConstTypes.clear();
3497 AggrStores.clear();
3498
3499 processParamTypesByFunHeader(CurrF, B);
3500
3501 // Fix GEP result types ahead of inference, and simplify if possible.
3502 // Data structure for dead instructions that were simplified and replaced.
3503 SmallPtrSet<Instruction *, 4> DeadInsts;
3504 for (auto &I : instructions(Func)) {
3506 auto *SGEP = dyn_cast<StructuredGEPInst>(&I);
3507
3508 if ((!GEP && !SGEP) || GR->findDeducedElementType(&I))
3509 continue;
3510
3511 if (SGEP) {
3512 GR->addDeducedElementType(SGEP,
3513 normalizeType(SGEP->getResultElementType()));
3514 continue;
3515 }
3516
3517 GetElementPtrInst *NewGEP = simplifyZeroLengthArrayGepInst(GEP);
3518 if (NewGEP) {
3519 GEP->replaceAllUsesWith(NewGEP);
3520 DeadInsts.insert(GEP);
3521 GEP = NewGEP;
3522 }
3523 if (Type *GepTy = getGEPType(GEP))
3524 GR->addDeducedElementType(GEP, normalizeType(GepTy));
3525 }
3526 // Remove dead instructions that were simplified and replaced.
3527 for (auto *I : DeadInsts) {
3528 assert(I->use_empty() && "Dead instruction should not have any uses left");
3529 I->eraseFromParent();
3530 }
3531
3532 // StoreInst's operand type can be changed during the next
3533 // transformations, so we need to store it in the set. Also store already
3534 // transformed types.
3535 for (auto &I : instructions(Func)) {
3536 StoreInst *SI = dyn_cast<StoreInst>(&I);
3537 if (!SI)
3538 continue;
3539 Type *ElTy = SI->getValueOperand()->getType();
3540 if (ElTy->isAggregateType() || ElTy->isVectorTy())
3541 AggrStores.insert(&I);
3542 }
3543
3544 B.SetInsertPoint(&Func.getEntryBlock(), Func.getEntryBlock().begin());
3545 for (auto &GV : Func.getParent()->globals())
3546 processGlobalValue(GV, B);
3547
3548 preprocessUndefs(B);
3549 preprocessPoisons(B);
3550 simplifyNullAddrSpaceCasts();
3551 preprocessCompositeConstants(B);
3552
3553 // A PHINode, SelectInst or FreezeInst takes its result type from its
3554 // operands. Aggregate arms are lowered to i32 value-ids (composite constants
3555 // here, loads and other producers during the visitor pass below), so mutate
3556 // an aggregate PHI, select or freeze to match. The original type is tracked
3557 // in AggrConstTypes (used to assign the SPIR-V type) and its extractvalue
3558 // users are lowered to spv_extractv.
3559 Type *I32Ty = B.getInt32Ty();
3560 for (Instruction &I : instructions(Func)) {
3562 continue;
3563 if (!I.getType()->isAggregateType())
3564 continue;
3565 AggrConstTypes[&I] = I.getType();
3566 I.mutateType(I32Ty);
3567 }
3568
3569 preprocessBoolVectorBitcasts(Func);
3572
3573 applyDemangledPtrArgTypes(B);
3574
3575 // Pass forward: use operand to deduce instructions result.
3576 for (auto &I : Worklist) {
3577 // Don't emit intrinsincs for convergence intrinsics.
3578 if (isConvergenceIntrinsic(I))
3579 continue;
3580
3581 bool Postpone = insertAssignPtrTypeIntrs(I, B, false);
3582 // if Postpone is true, we can't decide on pointee type yet
3583 insertAssignTypeIntrs(I, B);
3584 insertPtrCastOrAssignTypeInstr(I, B);
3586 // if instruction requires a pointee type set, let's check if we know it
3587 // already, and force it to be i8 if not
3588 if (Postpone && !GR->findAssignPtrTypeInstr(I))
3589 insertAssignPtrTypeIntrs(I, B, true);
3590
3591 if (auto *FPI = dyn_cast<ConstrainedFPIntrinsic>(I))
3592 useRoundingMode(FPI, B);
3593 }
3594
3595 // Pass backward: use instructions results to specify/update/cast operands
3596 // where needed.
3597 SmallPtrSet<Instruction *, 4> IncompleteRets;
3598 for (auto &I : llvm::reverse(instructions(Func)))
3599 deduceOperandElementType(&I, &IncompleteRets);
3600
3601 // Pass forward for PHIs only, their operands are not preceed the
3602 // instruction in meaning of `instructions(Func)`.
3603 for (BasicBlock &BB : Func)
3604 for (PHINode &Phi : BB.phis())
3605 if (isPointerTy(Phi.getType()))
3606 deduceOperandElementType(&Phi, nullptr);
3607
3608 for (auto *I : Worklist) {
3609 TrackConstants = true;
3610 if (!I->getType()->isVoidTy() || isa<StoreInst>(I))
3612 // Visitors return either the original/newly created instruction for
3613 // further processing, nullptr otherwise.
3614 I = visit(*I);
3615 if (!I)
3616 continue;
3617
3618 // Don't emit intrinsics for convergence operations.
3619 if (isConvergenceIntrinsic(I))
3620 continue;
3621
3623 processInstrAfterVisit(I, B);
3624 }
3625
3626 emitUnstructuredLoopControls(Func, B);
3627
3628 return true;
3629}
3630
3631// Try to deduce a better type for pointers to untyped ptr.
3632bool SPIRVEmitIntrinsics::postprocessTypes(Module &M) {
3633 if (!GR || TodoTypeSz == 0)
3634 return false;
3635
3636 unsigned SzTodo = TodoTypeSz;
3637 DenseMap<Value *, SmallPtrSet<Value *, 4>> ToProcess;
3638 for (auto [Op, Enabled] : TodoType) {
3639 // TODO: add isa<CallInst>(Op) to continue
3640 if (!Enabled || isaGEP(Op))
3641 continue;
3642 CallInst *AssignCI = GR->findAssignPtrTypeInstr(Op);
3643 Type *KnownTy = GR->findDeducedElementType(Op);
3644 if (!KnownTy || !AssignCI)
3645 continue;
3646 assert(Op == AssignCI->getArgOperand(0));
3647 // Try to improve the type deduced after all Functions are processed.
3648 if (auto *CI = dyn_cast<Instruction>(Op)) {
3649 CurrF = CI->getParent()->getParent();
3650 std::unordered_set<Value *> Visited;
3651 if (Type *ElemTy = deduceElementTypeHelper(Op, Visited, false, true)) {
3652 if (ElemTy != KnownTy) {
3653 DenseSet<std::pair<Value *, Value *>> VisitedSubst;
3654 propagateElemType(CI, ElemTy, VisitedSubst);
3655 eraseTodoType(Op);
3656 continue;
3657 }
3658 }
3659 }
3660
3661 if (Op->hasUseList()) {
3662 for (User *U : Op->users()) {
3664 if (Inst && !isa<IntrinsicInst>(Inst))
3665 ToProcess[Inst].insert(Op);
3666 }
3667 }
3668 }
3669 if (TodoTypeSz == 0)
3670 return true;
3671
3672 for (auto &F : M) {
3673 CurrF = &F;
3674 SmallPtrSet<Instruction *, 4> IncompleteRets;
3675 for (auto &I : llvm::reverse(instructions(F))) {
3676 auto It = ToProcess.find(&I);
3677 if (It == ToProcess.end())
3678 continue;
3679 It->second.remove_if([this](Value *V) { return !isTodoType(V); });
3680 if (It->second.size() == 0)
3681 continue;
3682 deduceOperandElementType(&I, &IncompleteRets, &It->second, true);
3683 if (TodoTypeSz == 0)
3684 return true;
3685 }
3686 }
3687
3688 return SzTodo > TodoTypeSz;
3689}
3690
3691// Parse and store argument types of function declarations where needed.
3692void SPIRVEmitIntrinsics::parseFunDeclarations(Module &M) {
3693 for (auto &F : M) {
3694 if (!F.isDeclaration() || F.isIntrinsic())
3695 continue;
3696 // get the demangled name
3697 std::string DemangledName = getOclOrSpirvBuiltinDemangledName(F.getName());
3698 if (DemangledName.empty())
3699 continue;
3700 // allow only OpGroupAsyncCopy use case at the moment
3701 const SPIRVSubtarget &ST = TM.getSubtarget<SPIRVSubtarget>(F);
3702 auto [Grp, Opcode, ExtNo] = SPIRV::mapBuiltinToOpcode(
3703 DemangledName, ST.getPreferredInstructionSet());
3704 if (Opcode != SPIRV::OpGroupAsyncCopy)
3705 continue;
3706 // find pointer arguments
3707 SmallVector<unsigned> Idxs;
3708 for (unsigned OpIdx = 0; OpIdx < F.arg_size(); ++OpIdx) {
3709 Argument *Arg = F.getArg(OpIdx);
3710 if (isPointerTy(Arg->getType()) && !hasPointeeTypeAttr(Arg))
3711 Idxs.push_back(OpIdx);
3712 }
3713 if (!Idxs.size())
3714 continue;
3715 // parse function arguments
3716 LLVMContext &Ctx = F.getContext();
3718 SPIRV::parseBuiltinTypeStr(TypeStrs, DemangledName, Ctx);
3719 if (!TypeStrs.size())
3720 continue;
3721 // find type info for pointer arguments
3722 for (unsigned Idx : Idxs) {
3723 if (Idx >= TypeStrs.size())
3724 continue;
3725 if (Type *ElemTy =
3726 SPIRV::parseBuiltinCallArgumentType(TypeStrs[Idx].trim(), Ctx))
3728 !ElemTy->isTargetExtTy())
3729 FDeclPtrTys[&F].push_back(std::make_pair(Idx, ElemTy));
3730 }
3731 }
3732}
3733
3734bool SPIRVEmitIntrinsics::processMaskedMemIntrinsic(IntrinsicInst &I) {
3735 const SPIRVSubtarget &ST = TM.getSubtarget<SPIRVSubtarget>(*I.getFunction());
3736
3737 if (I.getIntrinsicID() == Intrinsic::masked_gather) {
3738 if (!ST.canUseExtension(
3739 SPIRV::Extension::SPV_INTEL_masked_gather_scatter)) {
3740 I.getContext().emitError(
3741 &I, "llvm.masked.gather requires SPV_INTEL_masked_gather_scatter "
3742 "extension");
3743 // Replace with poison to allow compilation to continue and report error.
3744 I.replaceAllUsesWith(PoisonValue::get(I.getType()));
3745 I.eraseFromParent();
3746 return true;
3747 }
3748
3749 IRBuilder<> B(&I);
3750
3751 Value *Ptrs = I.getArgOperand(0);
3752 Value *Mask = I.getArgOperand(1);
3753 Value *Passthru = I.getArgOperand(2);
3754
3755 // Alignment is stored as a parameter attribute, not as a regular parameter.
3756 uint32_t Alignment = I.getParamAlign(0).valueOrOne().value();
3757
3758 SmallVector<Value *, 4> Args = {Ptrs, B.getInt32(Alignment), Mask,
3759 Passthru};
3760 SmallVector<Type *, 4> Types = {I.getType(), Ptrs->getType(),
3761 Mask->getType(), Passthru->getType()};
3762
3763 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_masked_gather, Types, Args);
3764 I.replaceAllUsesWith(NewI);
3765 I.eraseFromParent();
3766 return true;
3767 }
3768
3769 if (I.getIntrinsicID() == Intrinsic::masked_scatter) {
3770 if (!ST.canUseExtension(
3771 SPIRV::Extension::SPV_INTEL_masked_gather_scatter)) {
3772 I.getContext().emitError(
3773 &I, "llvm.masked.scatter requires SPV_INTEL_masked_gather_scatter "
3774 "extension");
3775 // Erase the intrinsic to allow compilation to continue and report error.
3776 I.eraseFromParent();
3777 return true;
3778 }
3779
3780 IRBuilder<> B(&I);
3781
3782 Value *Values = I.getArgOperand(0);
3783 Value *Ptrs = I.getArgOperand(1);
3784 Value *Mask = I.getArgOperand(2);
3785
3786 // Alignment is stored as a parameter attribute on the ptrs parameter (arg
3787 // 1).
3788 uint32_t Alignment = I.getParamAlign(1).valueOrOne().value();
3789
3790 SmallVector<Value *, 4> Args = {Values, Ptrs, B.getInt32(Alignment), Mask};
3791 SmallVector<Type *, 3> Types = {Values->getType(), Ptrs->getType(),
3792 Mask->getType()};
3793
3794 B.CreateIntrinsic(Intrinsic::spv_masked_scatter, Types, Args);
3795 I.eraseFromParent();
3796 return true;
3797 }
3798
3799 return false;
3800}
3801
3802// SPIR-V doesn't support bitcasts involving vector boolean type. Decompose such
3803// bitcasts into element-wise operations before building instructions
3804// worklist, so new instructions are properly visited and converted to
3805// SPIR-V intrinsics.
3806void SPIRVEmitIntrinsics::preprocessBoolVectorBitcasts(Function &F) {
3807 struct BoolVecBitcast {
3808 BitCastInst *BC;
3809 FixedVectorType *BoolVecTy;
3810 bool SrcIsBoolVec;
3811 };
3812
3813 auto getAsBoolVec = [](Type *Ty) -> FixedVectorType * {
3814 auto *VTy = dyn_cast<FixedVectorType>(Ty);
3815 return (VTy && VTy->getElementType()->isIntegerTy(1)) ? VTy : nullptr;
3816 };
3817
3819 for (auto &I : instructions(F)) {
3820 auto *BC = dyn_cast<BitCastInst>(&I);
3821 if (!BC)
3822 continue;
3823 if (auto *BVTy = getAsBoolVec(BC->getSrcTy()))
3824 ToReplace.push_back({BC, BVTy, true});
3825 else if (auto *BVTy = getAsBoolVec(BC->getDestTy()))
3826 ToReplace.push_back({BC, BVTy, false});
3827 }
3828
3829 for (auto &[BC, BoolVecTy, SrcIsBoolVec] : ToReplace) {
3830 IRBuilder<> B(BC);
3831 Value *Src = BC->getOperand(0);
3832 unsigned BoolVecN = BoolVecTy->getNumElements();
3833 // Use iN as the scalar intermediate type for the bool vector side.
3834 Type *IntTy = B.getIntNTy(BoolVecN);
3835
3836 // Convert source to scalar integer.
3837 Value *IntVal;
3838 if (SrcIsBoolVec) {
3839 // Extract each bool, zext, shift, and OR.
3840 IntVal = ConstantInt::get(IntTy, 0);
3841 for (unsigned I = 0; I < BoolVecN; ++I) {
3842 Value *Elem = B.CreateExtractElement(Src, B.getInt32(I));
3843 Value *Ext = B.CreateZExt(Elem, IntTy);
3844 if (I > 0)
3845 Ext = B.CreateShl(Ext, ConstantInt::get(IntTy, I));
3846 IntVal = B.CreateOr(IntVal, Ext);
3847 }
3848 } else {
3849 // Source is a non-bool type. If it's already a scalar integer, use it
3850 // directly, otherwise bitcast to iN first.
3851 IntVal = Src;
3852 if (!Src->getType()->isIntegerTy())
3853 IntVal = B.CreateBitCast(Src, IntTy);
3854 }
3855
3856 // Convert scalar integer to destination type.
3857 Value *Result;
3858 if (!SrcIsBoolVec) {
3859 // Test each bit with AND + icmp.
3860 Result = PoisonValue::get(BoolVecTy);
3861 for (unsigned I = 0; I < BoolVecN; ++I) {
3862 Value *Mask = ConstantInt::get(IntTy, APInt::getOneBitSet(BoolVecN, I));
3863 Value *And = B.CreateAnd(IntVal, Mask);
3864 Value *Cmp = B.CreateICmpNE(And, ConstantInt::get(IntTy, 0));
3865 Result = B.CreateInsertElement(Result, Cmp, B.getInt32(I));
3866 }
3867 } else {
3868 // Destination is a non-bool type. If it's a scalar integer, use IntVal
3869 // directly, otherwise bitcast from iN.
3870 Result = IntVal;
3871 if (!BC->getDestTy()->isIntegerTy())
3872 Result = B.CreateBitCast(IntVal, BC->getDestTy());
3873 }
3874
3875 BC->replaceAllUsesWith(Result);
3876 BC->eraseFromParent();
3877 }
3878}
3879
3880bool SPIRVEmitIntrinsics::convertMaskedMemIntrinsics(Module &M) {
3881 bool Changed = false;
3882
3883 for (Function &F : make_early_inc_range(M)) {
3884 if (!F.isIntrinsic())
3885 continue;
3886 Intrinsic::ID IID = F.getIntrinsicID();
3887 if (IID != Intrinsic::masked_gather && IID != Intrinsic::masked_scatter)
3888 continue;
3889
3890 for (User *U : make_early_inc_range(F.users())) {
3891 if (auto *II = dyn_cast<IntrinsicInst>(U))
3892 Changed |= processMaskedMemIntrinsic(*II);
3893 }
3894
3895 if (F.use_empty())
3896 F.eraseFromParent();
3897 }
3898
3899 return Changed;
3900}
3901
3902bool SPIRVEmitIntrinsics::runOnModule(Module &M) {
3903 bool Changed = false;
3904
3905 Changed |= convertMaskedMemIntrinsics(M);
3906
3907 parseFunDeclarations(M);
3908 insertConstantsForFPFastMathDefault(M);
3909 GVUsers.init(M);
3910
3911 TodoType.clear();
3912 for (auto &F : M)
3914
3915 // Specify function parameters after all functions were processed.
3916 for (auto &F : M) {
3917 // check if function parameter types are set
3918 CurrF = &F;
3919 if (!F.isDeclaration() && !F.isIntrinsic()) {
3920 IRBuilder<> B(F.getContext());
3921 processParamTypes(&F, B);
3922 }
3923 }
3924
3925 CanTodoType = false;
3926 Changed |= postprocessTypes(M);
3927
3928 if (HaveFunPtrs)
3929 Changed |= processFunctionPointers(M);
3930
3931 return Changed;
3932}
3933
3934PreservedAnalyses
3936 SPIRVEmitIntrinsics Legacy(TM);
3937 if (Legacy.runOnModule(M))
3938 return PreservedAnalyses::none();
3939 return PreservedAnalyses::all();
3940}
3941
3943 return new SPIRVEmitIntrinsics(TM);
3944}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
always inline
Expand Atomic instructions
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
static void replaceAllUsesWith(Value *Old, Value *New, SmallPtrSet< BasicBlock *, 32 > &FreshBBs, bool IsHuge)
Replace all old uses with new ones, and push the updated BBs into FreshBBs.
static Type * getPointeeType(Value *Ptr, const DataLayout &DL)
This file defines the DenseSet and SmallDenseSet classes.
static bool runOnFunction(Function &F, bool PostInlining)
Hexagon Common GEP
iv Induction Variable Users
Definition IVUsers.cpp:48
static Value * getOpcode(Value &V, Type &Ty, InstrumentationConfig &IConf, InstrumentorIRBuilderTy &IIRB)
const AbstractManglingParser< Derived, Alloc >::OperatorInfo AbstractManglingParser< Derived, Alloc >::Ops[]
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
Machine Check Debug Module
#define T
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static unsigned getNumElements(Type *Ty)
static bool isMemInstrToReplace(Instruction *I)
static bool isAggrConstForceInt32(const Value *V)
static SPIRV::FPFastMathDefaultInfoVector & getOrCreateFPFastMathDefaultInfoVec(const Module &M, DenseMap< Function *, SPIRV::FPFastMathDefaultInfoVector > &FPFastMathDefaultInfoMap, Function *F)
static Type * getAtomicElemTy(SPIRVGlobalRegistry *GR, Instruction *I, Value *PointerOperand)
static void reportFatalOnTokenType(const Instruction *I)
static void setInsertPointAfterDef(IRBuilder<> &B, Instruction *I)
static void emitAssignName(Instruction *I, IRBuilder<> &B)
static Type * getPointeeTypeByCallInst(StringRef DemangledName, Function *CalledF, unsigned OpIdx)
static void createRoundingModeDecoration(Instruction *I, unsigned RoundingModeDeco, IRBuilder<> &B)
static void createDecorationIntrinsic(Instruction *I, MDNode *Node, IRBuilder<> &B)
static SPIRV::FPFastMathDefaultInfo & getFPFastMathDefaultInfo(SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec, const Type *Ty)
static bool isAbortCall(const Instruction &I, const SPIRVSubtarget &ST)
static cl::opt< bool > SpirvEmitOpNames("spirv-emit-op-names", cl::desc("Emit OpName for all instructions"), cl::init(false))
static bool tracesToPointerAlloca(Value *V)
static bool IsKernelArgInt8(Function *F, StoreInst *SI)
static void addSaturatedDecorationToIntrinsic(Instruction *I, IRBuilder<> &B)
static bool isFirstIndexZero(const GetElementPtrInst *GEP)
static void setInsertPointSkippingPhis(IRBuilder<> &B, Instruction *I)
static bool isSpvAggrPlaceholder(const Value *V)
static bool precededByAbortIntrinsic(const UnreachableInst &I, const SPIRVSubtarget &ST)
static FunctionType * getFunctionPointerElemType(Function *F, SPIRVGlobalRegistry *GR)
static void createSaturatedConversionDecoration(Instruction *I, IRBuilder<> &B)
static bool shouldEmitIntrinsicsForGlobalValue(const GlobalVariableUsers &GVUsers, const GlobalVariable &GV, const Function *F)
static Type * restoreMutatedType(SPIRVGlobalRegistry *GR, Instruction *I, Type *Ty)
static bool requireAssignType(Instruction *I)
static void insertSpirvDecorations(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
static void visit(BasicBlock &Start, std::function< bool(BasicBlock *)> op)
StringSet - A set-like wrapper for the StringMap.
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
#define LLVM_DEBUG(...)
Definition Debug.h:119
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
static APInt getOneBitSet(unsigned numBits, unsigned BitNo)
Return an APInt with exactly one bit set in the result.
Definition APInt.h:240
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
const Function * getParent() const
Definition Argument.h:44
static unsigned getPointerOperandIndex()
static unsigned getPointerOperandIndex()
iterator_range< const_phi_iterator > phis() const
Returns a range that iterates over the phis in the basic block.
Definition BasicBlock.h:530
const Function * getParent() const
Return the enclosing method, or null if none.
Definition BasicBlock.h:213
static BasicBlock * Create(LLVMContext &Context, const Twine &Name="", Function *Parent=nullptr, BasicBlock *InsertBefore=nullptr)
Creates a new BasicBlock.
Definition BasicBlock.h:206
LLVM_ABI LLVMContext & getContext() const
Get the context in which this basic block lives.
const Instruction * getTerminator() const LLVM_READONLY
Returns the terminator instruction; assumes that the block is well-formed.
Definition BasicBlock.h:237
static LLVM_ABI BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
bool isInlineAsm() const
Check if this call is an inline asm statement.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
FunctionType * getFunctionType() const
LLVM_ABI Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
iterator_range< User::op_iterator > args()
Iteration adapter for range-for loops.
unsigned arg_size() const
void setCalledFunction(Function *Fn)
Sets the function called, including updating the function type.
This class represents a function call, abstracting a target machine's calling convention.
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
static LLVM_ABI ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
static LLVM_ABI Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
LLVM_ABI std::optional< RoundingMode > getRoundingMode() const
iterator find(const_arg_type_t< KeyT > Val)
Definition DenseMap.h:225
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:301
iterator end()
Definition DenseMap.h:143
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:286
Type * getParamType(unsigned i) const
Parameter type accessors.
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
Definition Function.cpp:358
iterator begin()
Definition Function.h:853
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:251
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:354
size_t arg_size() const
Definition Function.h:901
Type * getReturnType() const
Returns the type of the ret val.
Definition Function.h:216
Argument * getArg(unsigned i) const
Definition Function.h:886
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
static LLVM_ABI Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static unsigned getPointerOperandIndex()
PointerType * getType() const
Global values are always pointers.
@ InternalLinkage
Rename collisions when linking (static functions).
Definition GlobalValue.h:60
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Definition IRBuilder.h:2868
LLVM_ABI void addDestination(BasicBlock *Dest)
Add a destination.
Base class for instruction visitors.
Definition InstVisitor.h:78
LLVM_ABI bool isDebugOrPseudoInst() const LLVM_READONLY
Return true if the instruction is a DbgInfoIntrinsic or PseudoProbeInst.
LLVM_ABI const Module * getModule() const
Return the module owning the function this instruction belongs to or nullptr it the function does not...
LLVM_ABI InstListType::iterator eraseFromParent()
This method unlinks 'this' from the containing basic block and deletes it.
Instruction * user_back()
Specialize the methods defined in Value, as we know that an instruction can only be used by other ins...
LLVM_ABI const Function * getFunction() const
Return the function this instruction belongs to.
LLVM_ABI void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:68
static unsigned getPointerOperandIndex()
Metadata node.
Definition Metadata.h:1069
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1433
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1561
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1439
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:614
Flags
Flags values. These may be or'd together.
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:110
Metadata * getMetadata() const
Definition Metadata.h:202
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:67
static LLVM_ABI PoisonValue * get(Type *T)
Static factory methods - Return an 'poison' object of the specified type.
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(Module &M, ModuleAnalysisManager &AM)
void addAssignPtrTypeInstr(Value *Val, CallInst *AssignPtrTyCI)
void buildAssignPtr(IRBuilder<> &B, Type *ElemTy, Value *Arg)
Type * findDeducedCompositeType(const Value *Val)
void replaceAllUsesWith(Value *Old, Value *New, bool DeleteOld=true)
void addDeducedElementType(Value *Val, Type *Ty)
void addReturnType(const Function *ArgF, TypedPointerType *DerivedTy)
Type * findMutated(const Value *Val)
void addDeducedCompositeType(Value *Val, Type *Ty)
void buildAssignType(IRBuilder<> &B, Type *Ty, Value *Arg)
Type * findDeducedElementType(const Value *Val)
void updateAssignType(CallInst *AssignCI, Value *Arg, Value *OfType)
CallInst * findAssignPtrTypeInstr(const Value *Val)
const SPIRVTargetLowering * getTargetLowering() const override
bool isLogicalSPIRV() const
bool canUseExtension(SPIRV::Extension::Extension E) const
const SPIRVSubtarget * getSubtargetImpl() const
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
bool contains(ConstPtrType Ptr) const
void assign(size_type NumElts, ValueParamT Elt)
reference emplace_back(ArgTypes &&... Args)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
static unsigned getPointerOperandIndex()
iterator end()
Definition StringMap.h:213
iterator find(StringRef Key)
Definition StringMap.h:226
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition StringSet.h:25
bool contains(StringRef key) const
Check if the set contains the given key.
Definition StringSet.h:60
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition Type.cpp:685
static unsigned getPointerOperandIndex()
static LLVM_ABI TargetExtType * get(LLVMContext &Context, StringRef Name, ArrayRef< Type * > Types={}, ArrayRef< unsigned > Ints={})
Return a target extension type having the specified name and optional type and integer parameters.
Definition Type.cpp:974
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:288
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:279
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:309
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:282
Type * getArrayElementType() const
Definition Type.h:425
LLVM_ABI StringRef getTargetExtName() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:307
Type * getScalarType() const
If this is a vector type, return the element type, otherwise return 'this'.
Definition Type.h:368
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:276
bool isTargetExtTy() const
Return true if this is a target extension type.
Definition Type.h:205
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:319
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:257
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:287
Type * getContainedType(unsigned i) const
This method is used to implement the type iterator (defined at the end of the file).
Definition Type.h:397
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:286
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:284
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:141
static LLVM_ABI bool isValidElementType(Type *ElemTy)
Return true if the specified type is valid as a element type.
static LLVM_ABI TypedPointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
This function has undefined behavior.
op_range operands()
Definition User.h:267
void setOperand(unsigned i, Value *Val)
Definition User.h:212
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:25
Value * getOperand(unsigned i) const
Definition User.h:207
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:255
user_iterator user_begin()
Definition Value.h:402
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:394
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
Definition Value.cpp:553
iterator_range< user_iterator > users()
Definition Value.h:426
bool use_empty() const
Definition Value.h:346
user_iterator user_end()
Definition Value.h:410
void mutateType(Type *Ty)
Mutate the type of this Value to be of the specified type.
Definition Value.h:807
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:319
bool user_empty() const
Definition Value.h:389
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:212
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:185
const ParentTy * getParent() const
Definition ilist_node.h:34
CallInst * Call
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
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.
@ Entry
Definition COFF.h:862
@ SPIR_KERNEL
Used for SPIR kernel functions.
@ C
The default llvm calling convention, compatible with C.
Definition CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition ISDOpcodes.h:81
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
bool match(Val *V, const Pattern &P)
IntrinsicID_match m_Intrinsic()
Match intrinsic calls like this: m_Intrinsic<Intrinsic::fabs>(m_Value(X))
auto m_Value()
Match an arbitrary value and ignore it.
auto m_AnyIntrinsic()
Matches any intrinsic call and ignore it.
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
@ CE
Windows NT (Windows on ARM)
Definition MCAsmInfo.h:50
initializer< Ty > init(const Ty &Val)
@ User
could "use" a pointer
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< NodeBase * > Node
Definition RDFGraph.h:381
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:315
@ Offset
Definition DWP.cpp:558
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
ModulePass * createSPIRVEmitIntrinsicsPass(const SPIRVTargetMachine &TM)
bool isTypedPointerWrapper(const TargetExtType *ExtTy)
Definition SPIRVUtils.h:421
auto enumerate(FirstRange &&First, RestRanges &&...Rest)
Given two or more input ranges, returns a new range whose values are tuples (A, B,...
Definition STLExtras.h:2553
unsigned getPointerAddressSpace(const Type *T)
Definition SPIRVUtils.h:392
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)
bool isUntypedPointerVectorTy(const Type *T)
Definition SPIRVUtils.h:385
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2207
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
Definition STLExtras.h:633
FPDecorationId
Definition SPIRVUtils.h:567
SPIRV::MemorySemantics::MemorySemantics getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC)
bool isNestedPointer(const Type *Ty)
Function * getOrCreateBackendServiceFunction(Module &M)
MetadataAsValue * buildMD(Value *Arg)
Definition SPIRVUtils.h:531
std::string getOclOrSpirvBuiltinDemangledName(StringRef Name)
SmallVector< unsigned, 1 > getSpirvLoopControlOperandsFromLoopMetadata(MDNode *LoopMD)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:407
Type * getTypedPointerWrapper(Type *ElemTy, unsigned AS)
Definition SPIRVUtils.h:416
bool isVector1(Type *Ty)
Definition SPIRVUtils.h:509
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:380
bool none_of(R &&Range, UnaryPredicate P)
Provide wrappers to std::none_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1752
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:163
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
SPIRV::StorageClass::StorageClass addressSpaceToStorageClass(unsigned AddrSpace, const SPIRVSubtarget &STI)
SPIRV::Scope::Scope getMemScope(LLVMContext &Ctx, SyncScope::ID Id)
@ Ref
The access may reference the value stored in memory.
Definition ModRef.h:32
IRBuilder(LLVMContext &, FolderTy, InserterTy, MDNode *, ArrayRef< OperandBundleDef >) -> IRBuilder< FolderTy, InserterTy >
@ And
Bitwise or logical AND of integers.
DWARFExpression::Operation Op
Type * getPointeeTypeByAttr(Argument *Arg)
Definition SPIRVUtils.h:405
bool hasPointeeTypeAttr(Argument *Arg)
Definition SPIRVUtils.h:400
constexpr unsigned BitWidth
bool isEquivalentTypes(Type *Ty1, Type *Ty2)
Definition SPIRVUtils.h:471
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
bool hasInitializer(const GlobalVariable *GV)
Definition SPIRVUtils.h:361
Type * normalizeType(Type *Ty)
Definition SPIRVUtils.h:517
bool isPointerTyOrWrapper(const Type *Ty)
Definition SPIRVUtils.h:428
@ Enabled
Convert any .debug_str_offsets tables to DWARF64 if needed.
Definition DWP.h:31
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
PoisonValue * getNormalizedPoisonValue(Type *Ty)
Definition SPIRVUtils.h:527
bool isUntypedPointerTy(const Type *T)
Definition SPIRVUtils.h:375
Type * reconstitutePeeledArrayType(Type *Ty)
SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord)
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
#define N
static size_t computeFPFastMathDefaultInfoVecIndex(size_t BitWidth)
Definition SPIRVUtils.h:148