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 "SPIRV.h"
15#include "SPIRVBuiltins.h"
16#include "SPIRVSubtarget.h"
17#include "SPIRVTargetMachine.h"
18#include "SPIRVUtils.h"
19#include "llvm/ADT/DenseSet.h"
20#include "llvm/ADT/StringSet.h"
21#include "llvm/IR/IRBuilder.h"
23#include "llvm/IR/InstVisitor.h"
24#include "llvm/IR/IntrinsicsSPIRV.h"
29
30#include <cassert>
31#include <queue>
32#include <unordered_set>
33
34// This pass performs the following transformation on LLVM IR level required
35// for the following translation to SPIR-V:
36// - replaces direct usages of aggregate constants with target-specific
37// intrinsics;
38// - replaces aggregates-related instructions (extract/insert, ld/st, etc)
39// with a target-specific intrinsics;
40// - emits intrinsics for the global variable initializers since IRTranslator
41// doesn't handle them and it's not very convenient to translate them
42// ourselves;
43// - emits intrinsics to keep track of the string names assigned to the values;
44// - emits intrinsics to keep track of constants (this is necessary to have an
45// LLVM IR constant after the IRTranslation is completed) for their further
46// deduplication;
47// - emits intrinsics to keep track of original LLVM types of the values
48// to be able to emit proper SPIR-V types eventually.
49//
50// TODO: consider removing spv.track.constant in favor of spv.assign.type.
51
52using namespace llvm;
53
54static cl::opt<bool>
55 SpirvEmitOpNames("spirv-emit-op-names",
56 cl::desc("Emit OpName for all instructions"),
57 cl::init(false));
58
59namespace llvm::SPIRV {
60#define GET_BuiltinGroup_DECL
61#include "SPIRVGenTables.inc"
62} // namespace llvm::SPIRV
63
64namespace {
65// This class keeps track of which functions reference which global variables.
66class GlobalVariableUsers {
67 template <typename T1, typename T2>
68 using OneToManyMapTy = DenseMap<T1, SmallPtrSet<T2, 4>>;
69
70 OneToManyMapTy<const GlobalVariable *, const Function *> GlobalIsUsedByFun;
71
72 void collectGlobalUsers(
73 const GlobalVariable *GV,
74 OneToManyMapTy<const GlobalVariable *, const GlobalVariable *>
75 &GlobalIsUsedByGlobal) {
77 while (!Stack.empty()) {
78 const Value *V = Stack.pop_back_val();
79
80 if (const Instruction *I = dyn_cast<Instruction>(V)) {
81 GlobalIsUsedByFun[GV].insert(I->getFunction());
82 continue;
83 }
84
85 if (const GlobalVariable *UserGV = dyn_cast<GlobalVariable>(V)) {
86 GlobalIsUsedByGlobal[GV].insert(UserGV);
87 continue;
88 }
89
90 if (const Constant *C = dyn_cast<Constant>(V))
91 Stack.append(C->user_begin(), C->user_end());
92 }
93 }
94
95 bool propagateGlobalToGlobalUsers(
96 OneToManyMapTy<const GlobalVariable *, const GlobalVariable *>
97 &GlobalIsUsedByGlobal) {
99 bool Changed = false;
100 for (auto &[GV, UserGlobals] : GlobalIsUsedByGlobal) {
101 OldUsersGlobals.assign(UserGlobals.begin(), UserGlobals.end());
102 for (const GlobalVariable *UserGV : OldUsersGlobals) {
103 auto It = GlobalIsUsedByGlobal.find(UserGV);
104 if (It == GlobalIsUsedByGlobal.end())
105 continue;
106 Changed |= set_union(UserGlobals, It->second);
107 }
108 }
109 return Changed;
110 }
111
112 void propagateGlobalToFunctionReferences(
113 OneToManyMapTy<const GlobalVariable *, const GlobalVariable *>
114 &GlobalIsUsedByGlobal) {
115 for (auto &[GV, UserGlobals] : GlobalIsUsedByGlobal) {
116 auto &UserFunctions = GlobalIsUsedByFun[GV];
117 for (const GlobalVariable *UserGV : UserGlobals) {
118 auto It = GlobalIsUsedByFun.find(UserGV);
119 if (It == GlobalIsUsedByFun.end())
120 continue;
121 set_union(UserFunctions, It->second);
122 }
123 }
124 }
125
126public:
127 void init(Module &M) {
128 // Collect which global variables are referenced by which global variables
129 // and which functions reference each global variables.
130 OneToManyMapTy<const GlobalVariable *, const GlobalVariable *>
131 GlobalIsUsedByGlobal;
132 GlobalIsUsedByFun.clear();
133 for (GlobalVariable &GV : M.globals())
134 collectGlobalUsers(&GV, GlobalIsUsedByGlobal);
135
136 // Compute indirect references by iterating until a fixed point is reached.
137 while (propagateGlobalToGlobalUsers(GlobalIsUsedByGlobal))
138 (void)0;
139
140 propagateGlobalToFunctionReferences(GlobalIsUsedByGlobal);
141 }
142
143 using FunctionSetType = typename decltype(GlobalIsUsedByFun)::mapped_type;
144 const FunctionSetType &
145 getTransitiveUserFunctions(const GlobalVariable &GV) const {
146 auto It = GlobalIsUsedByFun.find(&GV);
147 if (It != GlobalIsUsedByFun.end())
148 return It->second;
149
150 static const FunctionSetType Empty{};
151 return Empty;
152 }
153};
154
155static bool isaGEP(const Value *V) {
157}
158
159class SPIRVEmitIntrinsics
160 : public ModulePass,
161 public InstVisitor<SPIRVEmitIntrinsics, Instruction *> {
162 SPIRVTargetMachine *TM = nullptr;
163 SPIRVGlobalRegistry *GR = nullptr;
164 Function *CurrF = nullptr;
165 bool TrackConstants = true;
166 bool HaveFunPtrs = false;
167 DenseMap<Instruction *, Constant *> AggrConsts;
168 DenseMap<Instruction *, Type *> AggrConstTypes;
169 DenseSet<Instruction *> AggrStores;
170 GlobalVariableUsers GVUsers;
171 std::unordered_set<Value *> Named;
172
173 // map of function declarations to <pointer arg index => element type>
174 DenseMap<Function *, SmallVector<std::pair<unsigned, Type *>>> FDeclPtrTys;
175
176 // a register of Instructions that don't have a complete type definition
177 bool CanTodoType = true;
178 unsigned TodoTypeSz = 0;
179 DenseMap<Value *, bool> TodoType;
180 void insertTodoType(Value *Op) {
181 // TODO: add isa<CallInst>(Op) to no-insert
182 if (CanTodoType && !isaGEP(Op)) {
183 auto It = TodoType.try_emplace(Op, true);
184 if (It.second)
185 ++TodoTypeSz;
186 }
187 }
188 void eraseTodoType(Value *Op) {
189 auto It = TodoType.find(Op);
190 if (It != TodoType.end() && It->second) {
191 It->second = false;
192 --TodoTypeSz;
193 }
194 }
195 bool isTodoType(Value *Op) {
196 if (isaGEP(Op))
197 return false;
198 auto It = TodoType.find(Op);
199 return It != TodoType.end() && It->second;
200 }
201 // a register of Instructions that were visited by deduceOperandElementType()
202 // to validate operand types with an instruction
203 std::unordered_set<Instruction *> TypeValidated;
204
205 // well known result types of builtins
206 enum WellKnownTypes { Event };
207
208 // deduce element type of untyped pointers
209 Type *deduceElementType(Value *I, bool UnknownElemTypeI8);
210 Type *deduceElementTypeHelper(Value *I, bool UnknownElemTypeI8);
211 Type *deduceElementTypeHelper(Value *I, std::unordered_set<Value *> &Visited,
212 bool UnknownElemTypeI8,
213 bool IgnoreKnownType = false);
214 Type *deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
215 bool UnknownElemTypeI8);
216 Type *deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
217 std::unordered_set<Value *> &Visited,
218 bool UnknownElemTypeI8);
219 Type *deduceElementTypeByUsersDeep(Value *Op,
220 std::unordered_set<Value *> &Visited,
221 bool UnknownElemTypeI8);
222 void maybeAssignPtrType(Type *&Ty, Value *I, Type *RefTy,
223 bool UnknownElemTypeI8);
224
225 // deduce nested types of composites
226 Type *deduceNestedTypeHelper(User *U, bool UnknownElemTypeI8);
227 Type *deduceNestedTypeHelper(User *U, Type *Ty,
228 std::unordered_set<Value *> &Visited,
229 bool UnknownElemTypeI8);
230
231 // deduce Types of operands of the Instruction if possible
232 void deduceOperandElementType(Instruction *I,
233 SmallPtrSet<Instruction *, 4> *IncompleteRets,
234 const SmallPtrSet<Value *, 4> *AskOps = nullptr,
235 bool IsPostprocessing = false);
236
237 void preprocessCompositeConstants(IRBuilder<> &B);
238 void preprocessUndefs(IRBuilder<> &B);
239
240 Type *reconstructType(Value *Op, bool UnknownElemTypeI8,
241 bool IsPostprocessing);
242
243 void replaceMemInstrUses(Instruction *Old, Instruction *New, IRBuilder<> &B);
244 void processInstrAfterVisit(Instruction *I, IRBuilder<> &B);
245 bool insertAssignPtrTypeIntrs(Instruction *I, IRBuilder<> &B,
246 bool UnknownElemTypeI8);
247 void insertAssignTypeIntrs(Instruction *I, IRBuilder<> &B);
248 void insertAssignPtrTypeTargetExt(TargetExtType *AssignedType, Value *V,
249 IRBuilder<> &B);
250 void replacePointerOperandWithPtrCast(Instruction *I, Value *Pointer,
251 Type *ExpectedElementType,
252 unsigned OperandToReplace,
253 IRBuilder<> &B);
254 void insertPtrCastOrAssignTypeInstr(Instruction *I, IRBuilder<> &B);
255 bool shouldTryToAddMemAliasingDecoration(Instruction *Inst);
256 void insertSpirvDecorations(Instruction *I, IRBuilder<> &B);
257 void insertConstantsForFPFastMathDefault(Module &M);
258 void processGlobalValue(GlobalVariable &GV, IRBuilder<> &B);
259 void processParamTypes(Function *F, IRBuilder<> &B);
260 void processParamTypesByFunHeader(Function *F, IRBuilder<> &B);
261 Type *deduceFunParamElementType(Function *F, unsigned OpIdx);
262 Type *deduceFunParamElementType(Function *F, unsigned OpIdx,
263 std::unordered_set<Function *> &FVisited);
264
265 bool deduceOperandElementTypeCalledFunction(
266 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
267 Type *&KnownElemTy, bool &Incomplete);
268 void deduceOperandElementTypeFunctionPointer(
269 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
270 Type *&KnownElemTy, bool IsPostprocessing);
271 bool deduceOperandElementTypeFunctionRet(
272 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
273 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing,
274 Type *&KnownElemTy, Value *Op, Function *F);
275
276 CallInst *buildSpvPtrcast(Function *F, Value *Op, Type *ElemTy);
277 void replaceUsesOfWithSpvPtrcast(Value *Op, Type *ElemTy, Instruction *I,
278 DenseMap<Function *, CallInst *> Ptrcasts);
279 void propagateElemType(Value *Op, Type *ElemTy,
280 DenseSet<std::pair<Value *, Value *>> &VisitedSubst);
281 void
282 propagateElemTypeRec(Value *Op, Type *PtrElemTy, Type *CastElemTy,
283 DenseSet<std::pair<Value *, Value *>> &VisitedSubst);
284 void propagateElemTypeRec(Value *Op, Type *PtrElemTy, Type *CastElemTy,
285 DenseSet<std::pair<Value *, Value *>> &VisitedSubst,
286 std::unordered_set<Value *> &Visited,
287 DenseMap<Function *, CallInst *> Ptrcasts);
288
289 void replaceAllUsesWith(Value *Src, Value *Dest, bool DeleteOld = true);
290 void replaceAllUsesWithAndErase(IRBuilder<> &B, Instruction *Src,
291 Instruction *Dest, bool DeleteOld = true);
292
293 void applyDemangledPtrArgTypes(IRBuilder<> &B);
294
295 GetElementPtrInst *simplifyZeroLengthArrayGepInst(GetElementPtrInst *GEP);
296
297 bool runOnFunction(Function &F);
298 bool postprocessTypes(Module &M);
299 bool processFunctionPointers(Module &M);
300 void parseFunDeclarations(Module &M);
301 void useRoundingMode(ConstrainedFPIntrinsic *FPI, IRBuilder<> &B);
302
303 void emitUnstructuredLoopControls(Function &F, IRBuilder<> &B);
304
305 // Tries to walk the type accessed by the given GEP instruction.
306 // For each nested type access, one of the 2 callbacks is called:
307 // - OnLiteralIndexing when the index is a known constant value.
308 // Parameters:
309 // PointedType: the pointed type resulting of this indexing.
310 // If the parent type is an array, this is the index in the array.
311 // If the parent type is a struct, this is the field index.
312 // Index: index of the element in the parent type.
313 // - OnDynamnicIndexing when the index is a non-constant value.
314 // This callback is only called when indexing into an array.
315 // Parameters:
316 // ElementType: the type of the elements stored in the parent array.
317 // Offset: the Value* containing the byte offset into the array.
318 // Return true if an error occurred during the walk, false otherwise.
319 bool walkLogicalAccessChain(
320 GetElementPtrInst &GEP,
321 const std::function<void(Type *PointedType, uint64_t Index)>
322 &OnLiteralIndexing,
323 const std::function<void(Type *ElementType, Value *Offset)>
324 &OnDynamicIndexing);
325
326 // Returns the type accessed using the given GEP instruction by relying
327 // on the GEP type.
328 // FIXME: GEP types are not supposed to be used to retrieve the pointed
329 // type. This must be fixed.
330 Type *getGEPType(GetElementPtrInst *GEP);
331
332 // Returns the type accessed using the given GEP instruction by walking
333 // the source type using the GEP indices.
334 // FIXME: without help from the frontend, this method cannot reliably retrieve
335 // the stored type, nor can robustly determine the depth of the type
336 // we are accessing.
337 Type *getGEPTypeLogical(GetElementPtrInst *GEP);
338
339 Instruction *buildLogicalAccessChainFromGEP(GetElementPtrInst &GEP);
340
341public:
342 static char ID;
343 SPIRVEmitIntrinsics(SPIRVTargetMachine *TM = nullptr)
344 : ModulePass(ID), TM(TM) {}
345 Instruction *visitInstruction(Instruction &I) { return &I; }
346 Instruction *visitSwitchInst(SwitchInst &I);
347 Instruction *visitGetElementPtrInst(GetElementPtrInst &I);
348 Instruction *visitIntrinsicInst(IntrinsicInst &I);
349 Instruction *visitBitCastInst(BitCastInst &I);
350 Instruction *visitInsertElementInst(InsertElementInst &I);
351 Instruction *visitExtractElementInst(ExtractElementInst &I);
352 Instruction *visitInsertValueInst(InsertValueInst &I);
353 Instruction *visitExtractValueInst(ExtractValueInst &I);
354 Instruction *visitLoadInst(LoadInst &I);
355 Instruction *visitStoreInst(StoreInst &I);
356 Instruction *visitAllocaInst(AllocaInst &I);
357 Instruction *visitAtomicCmpXchgInst(AtomicCmpXchgInst &I);
358 Instruction *visitUnreachableInst(UnreachableInst &I);
359 Instruction *visitCallInst(CallInst &I);
360
361 StringRef getPassName() const override { return "SPIRV emit intrinsics"; }
362
363 bool runOnModule(Module &M) override;
364
365 void getAnalysisUsage(AnalysisUsage &AU) const override {
366 ModulePass::getAnalysisUsage(AU);
367 }
368};
369
370bool isConvergenceIntrinsic(const Instruction *I) {
371 const auto *II = dyn_cast<IntrinsicInst>(I);
372 if (!II)
373 return false;
374
375 return II->getIntrinsicID() == Intrinsic::experimental_convergence_entry ||
376 II->getIntrinsicID() == Intrinsic::experimental_convergence_loop ||
377 II->getIntrinsicID() == Intrinsic::experimental_convergence_anchor;
378}
379
380bool expectIgnoredInIRTranslation(const Instruction *I) {
381 const auto *II = dyn_cast<IntrinsicInst>(I);
382 if (!II)
383 return false;
384 switch (II->getIntrinsicID()) {
385 case Intrinsic::invariant_start:
386 case Intrinsic::spv_resource_handlefrombinding:
387 case Intrinsic::spv_resource_getpointer:
388 return true;
389 default:
390 return false;
391 }
392}
393
394// Returns the source pointer from `I` ignoring intermediate ptrcast.
395Value *getPointerRoot(Value *I) {
396 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
397 if (II->getIntrinsicID() == Intrinsic::spv_ptrcast) {
398 Value *V = II->getArgOperand(0);
399 return getPointerRoot(V);
400 }
401 }
402 return I;
403}
404
405} // namespace
406
407char SPIRVEmitIntrinsics::ID = 0;
408
409INITIALIZE_PASS(SPIRVEmitIntrinsics, "emit-intrinsics", "SPIRV emit intrinsics",
410 false, false)
411
412static inline bool isAssignTypeInstr(const Instruction *I) {
413 return isa<IntrinsicInst>(I) &&
414 cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::spv_assign_type;
415}
416
421
422static bool isAggrConstForceInt32(const Value *V) {
423 return isa<ConstantArray>(V) || isa<ConstantStruct>(V) ||
425 (isa<ConstantAggregateZero>(V) && !V->getType()->isVectorTy());
426}
427
429 if (isa<PHINode>(I))
430 B.SetInsertPoint(I->getParent()->getFirstNonPHIOrDbgOrAlloca());
431 else
432 B.SetInsertPoint(I);
433}
434
436 B.SetCurrentDebugLocation(I->getDebugLoc());
437 if (I->getType()->isVoidTy())
438 B.SetInsertPoint(I->getNextNode());
439 else
440 B.SetInsertPoint(*I->getInsertionPointAfterDef());
441}
442
444 if (const auto *Intr = dyn_cast<IntrinsicInst>(I)) {
445 switch (Intr->getIntrinsicID()) {
446 case Intrinsic::invariant_start:
447 case Intrinsic::invariant_end:
448 return false;
449 }
450 }
451 return true;
452}
453
454static inline void reportFatalOnTokenType(const Instruction *I) {
455 if (I->getType()->isTokenTy())
456 report_fatal_error("A token is encountered but SPIR-V without extensions "
457 "does not support token type",
458 false);
459}
460
462 if (!I->hasName() || I->getType()->isAggregateType() ||
463 expectIgnoredInIRTranslation(I))
464 return;
465
466 // We want to be conservative when adding the names because they can interfere
467 // with later optimizations.
468 bool KeepName = SpirvEmitOpNames;
469 if (!KeepName) {
470 if (isa<AllocaInst>(I)) {
471 KeepName = true;
472 } else if (auto *CI = dyn_cast<CallBase>(I)) {
473 Function *F = CI->getCalledFunction();
474 if (F && F->getName().starts_with("llvm.spv.alloca"))
475 KeepName = true;
476 }
477 }
478
479 if (!KeepName)
480 return;
481
484 LLVMContext &Ctx = I->getContext();
485 std::vector<Value *> Args = {
487 Ctx, MDNode::get(Ctx, MDString::get(Ctx, I->getName())))};
488 B.CreateIntrinsic(Intrinsic::spv_assign_name, {I->getType()}, Args);
489}
490
491void SPIRVEmitIntrinsics::replaceAllUsesWith(Value *Src, Value *Dest,
492 bool DeleteOld) {
493 GR->replaceAllUsesWith(Src, Dest, DeleteOld);
494 // Update uncomplete type records if any
495 if (isTodoType(Src)) {
496 if (DeleteOld)
497 eraseTodoType(Src);
498 insertTodoType(Dest);
499 }
500}
501
502void SPIRVEmitIntrinsics::replaceAllUsesWithAndErase(IRBuilder<> &B,
503 Instruction *Src,
504 Instruction *Dest,
505 bool DeleteOld) {
506 replaceAllUsesWith(Src, Dest, DeleteOld);
507 std::string Name = Src->hasName() ? Src->getName().str() : "";
508 Src->eraseFromParent();
509 if (!Name.empty()) {
510 Dest->setName(Name);
511 if (Named.insert(Dest).second)
512 emitAssignName(Dest, B);
513 }
514}
515
517 return SI && F->getCallingConv() == CallingConv::SPIR_KERNEL &&
518 isPointerTy(SI->getValueOperand()->getType()) &&
519 isa<Argument>(SI->getValueOperand());
520}
521
522// Maybe restore original function return type.
524 Type *Ty) {
526 if (!CI || CI->isIndirectCall() || CI->isInlineAsm() ||
528 return Ty;
529 if (Type *OriginalTy = GR->findMutated(CI->getCalledFunction()))
530 return OriginalTy;
531 return Ty;
532}
533
534// Reconstruct type with nested element types according to deduced type info.
535// Return nullptr if no detailed type info is available.
536Type *SPIRVEmitIntrinsics::reconstructType(Value *Op, bool UnknownElemTypeI8,
537 bool IsPostprocessing) {
538 Type *Ty = Op->getType();
539 if (auto *OpI = dyn_cast<Instruction>(Op))
540 Ty = restoreMutatedType(GR, OpI, Ty);
541 if (!isUntypedPointerTy(Ty))
542 return Ty;
543 // try to find the pointee type
544 if (Type *NestedTy = GR->findDeducedElementType(Op))
546 // not a pointer according to the type info (e.g., Event object)
547 CallInst *CI = GR->findAssignPtrTypeInstr(Op);
548 if (CI) {
549 MetadataAsValue *MD = cast<MetadataAsValue>(CI->getArgOperand(1));
550 return cast<ConstantAsMetadata>(MD->getMetadata())->getType();
551 }
552 if (UnknownElemTypeI8) {
553 if (!IsPostprocessing)
554 insertTodoType(Op);
555 return getTypedPointerWrapper(IntegerType::getInt8Ty(Op->getContext()),
557 }
558 return nullptr;
559}
560
561CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Function *F, Value *Op,
562 Type *ElemTy) {
563 IRBuilder<> B(Op->getContext());
564 if (auto *OpI = dyn_cast<Instruction>(Op)) {
565 // spv_ptrcast's argument Op denotes an instruction that generates
566 // a value, and we may use getInsertionPointAfterDef()
568 } else if (auto *OpA = dyn_cast<Argument>(Op)) {
569 B.SetInsertPointPastAllocas(OpA->getParent());
570 B.SetCurrentDebugLocation(DebugLoc());
571 } else {
572 B.SetInsertPoint(F->getEntryBlock().getFirstNonPHIOrDbgOrAlloca());
573 }
574 Type *OpTy = Op->getType();
575 SmallVector<Type *, 2> Types = {OpTy, OpTy};
576 SmallVector<Value *, 2> Args = {Op, buildMD(getNormalizedPoisonValue(ElemTy)),
577 B.getInt32(getPointerAddressSpace(OpTy))};
578 CallInst *PtrCasted =
579 B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
580 GR->buildAssignPtr(B, ElemTy, PtrCasted);
581 return PtrCasted;
582}
583
584void SPIRVEmitIntrinsics::replaceUsesOfWithSpvPtrcast(
585 Value *Op, Type *ElemTy, Instruction *I,
586 DenseMap<Function *, CallInst *> Ptrcasts) {
587 Function *F = I->getParent()->getParent();
588 CallInst *PtrCastedI = nullptr;
589 auto It = Ptrcasts.find(F);
590 if (It == Ptrcasts.end()) {
591 PtrCastedI = buildSpvPtrcast(F, Op, ElemTy);
592 Ptrcasts[F] = PtrCastedI;
593 } else {
594 PtrCastedI = It->second;
595 }
596 I->replaceUsesOfWith(Op, PtrCastedI);
597}
598
599void SPIRVEmitIntrinsics::propagateElemType(
600 Value *Op, Type *ElemTy,
601 DenseSet<std::pair<Value *, Value *>> &VisitedSubst) {
602 DenseMap<Function *, CallInst *> Ptrcasts;
603 SmallVector<User *> Users(Op->users());
604 for (auto *U : Users) {
605 if (!isa<Instruction>(U) || isSpvIntrinsic(U))
606 continue;
607 if (!VisitedSubst.insert(std::make_pair(U, Op)).second)
608 continue;
610 // If the instruction was validated already, we need to keep it valid by
611 // keeping current Op type.
612 if (isaGEP(UI) || TypeValidated.find(UI) != TypeValidated.end())
613 replaceUsesOfWithSpvPtrcast(Op, ElemTy, UI, Ptrcasts);
614 }
615}
616
617void SPIRVEmitIntrinsics::propagateElemTypeRec(
618 Value *Op, Type *PtrElemTy, Type *CastElemTy,
619 DenseSet<std::pair<Value *, Value *>> &VisitedSubst) {
620 std::unordered_set<Value *> Visited;
621 DenseMap<Function *, CallInst *> Ptrcasts;
622 propagateElemTypeRec(Op, PtrElemTy, CastElemTy, VisitedSubst, Visited,
623 std::move(Ptrcasts));
624}
625
626void SPIRVEmitIntrinsics::propagateElemTypeRec(
627 Value *Op, Type *PtrElemTy, Type *CastElemTy,
628 DenseSet<std::pair<Value *, Value *>> &VisitedSubst,
629 std::unordered_set<Value *> &Visited,
630 DenseMap<Function *, CallInst *> Ptrcasts) {
631 if (!Visited.insert(Op).second)
632 return;
633 SmallVector<User *> Users(Op->users());
634 for (auto *U : Users) {
635 if (!isa<Instruction>(U) || isSpvIntrinsic(U))
636 continue;
637 if (!VisitedSubst.insert(std::make_pair(U, Op)).second)
638 continue;
640 // If the instruction was validated already, we need to keep it valid by
641 // keeping current Op type.
642 if (isaGEP(UI) || TypeValidated.find(UI) != TypeValidated.end())
643 replaceUsesOfWithSpvPtrcast(Op, CastElemTy, UI, Ptrcasts);
644 }
645}
646
647// Set element pointer type to the given value of ValueTy and tries to
648// specify this type further (recursively) by Operand value, if needed.
649
650Type *
651SPIRVEmitIntrinsics::deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
652 bool UnknownElemTypeI8) {
653 std::unordered_set<Value *> Visited;
654 return deduceElementTypeByValueDeep(ValueTy, Operand, Visited,
655 UnknownElemTypeI8);
656}
657
658Type *SPIRVEmitIntrinsics::deduceElementTypeByValueDeep(
659 Type *ValueTy, Value *Operand, std::unordered_set<Value *> &Visited,
660 bool UnknownElemTypeI8) {
661 Type *Ty = ValueTy;
662 if (Operand) {
663 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
664 if (Type *NestedTy =
665 deduceElementTypeHelper(Operand, Visited, UnknownElemTypeI8))
666 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
667 } else {
668 Ty = deduceNestedTypeHelper(dyn_cast<User>(Operand), Ty, Visited,
669 UnknownElemTypeI8);
670 }
671 }
672 return Ty;
673}
674
675// Traverse User instructions to deduce an element pointer type of the operand.
676Type *SPIRVEmitIntrinsics::deduceElementTypeByUsersDeep(
677 Value *Op, std::unordered_set<Value *> &Visited, bool UnknownElemTypeI8) {
678 if (!Op || !isPointerTy(Op->getType()) || isa<ConstantPointerNull>(Op) ||
680 return nullptr;
681
682 if (auto ElemTy = getPointeeType(Op->getType()))
683 return ElemTy;
684
685 // maybe we already know operand's element type
686 if (Type *KnownTy = GR->findDeducedElementType(Op))
687 return KnownTy;
688
689 for (User *OpU : Op->users()) {
690 if (Instruction *Inst = dyn_cast<Instruction>(OpU)) {
691 if (Type *Ty = deduceElementTypeHelper(Inst, Visited, UnknownElemTypeI8))
692 return Ty;
693 }
694 }
695 return nullptr;
696}
697
698// Implements what we know in advance about intrinsics and builtin calls
699// TODO: consider feasibility of this particular case to be generalized by
700// encoding knowledge about intrinsics and builtin calls by corresponding
701// specification rules
703 Function *CalledF, unsigned OpIdx) {
704 if ((DemangledName.starts_with("__spirv_ocl_printf(") ||
705 DemangledName.starts_with("printf(")) &&
706 OpIdx == 0)
707 return IntegerType::getInt8Ty(CalledF->getContext());
708 return nullptr;
709}
710
711// Deduce and return a successfully deduced Type of the Instruction,
712// or nullptr otherwise.
713Type *SPIRVEmitIntrinsics::deduceElementTypeHelper(Value *I,
714 bool UnknownElemTypeI8) {
715 std::unordered_set<Value *> Visited;
716 return deduceElementTypeHelper(I, Visited, UnknownElemTypeI8);
717}
718
719void SPIRVEmitIntrinsics::maybeAssignPtrType(Type *&Ty, Value *Op, Type *RefTy,
720 bool UnknownElemTypeI8) {
721 if (isUntypedPointerTy(RefTy)) {
722 if (!UnknownElemTypeI8)
723 return;
724 insertTodoType(Op);
725 }
726 Ty = RefTy;
727}
728
729bool SPIRVEmitIntrinsics::walkLogicalAccessChain(
730 GetElementPtrInst &GEP,
731 const std::function<void(Type *, uint64_t)> &OnLiteralIndexing,
732 const std::function<void(Type *, Value *)> &OnDynamicIndexing) {
733 // We only rewrite i8* GEP. Other should be left as-is.
734 // Valid i8* GEP must always have a single index.
735 assert(GEP.getSourceElementType() ==
736 IntegerType::getInt8Ty(CurrF->getContext()));
737 assert(GEP.getNumIndices() == 1);
738
739 auto &DL = CurrF->getDataLayout();
740 Value *Src = getPointerRoot(GEP.getPointerOperand());
741 Type *CurType = deduceElementType(Src, true);
742
743 Value *Operand = *GEP.idx_begin();
744 ConstantInt *CI = dyn_cast<ConstantInt>(Operand);
745 if (!CI) {
746 ArrayType *AT = dyn_cast<ArrayType>(CurType);
747 // Operand is not constant. Either we have an array and accept it, or we
748 // give up.
749 if (AT)
750 OnDynamicIndexing(AT->getElementType(), Operand);
751 return AT == nullptr;
752 }
753
754 assert(CI);
755 uint64_t Offset = CI->getZExtValue();
756
757 do {
758 if (ArrayType *AT = dyn_cast<ArrayType>(CurType)) {
759 uint32_t EltTypeSize = DL.getTypeSizeInBits(AT->getElementType()) / 8;
760 assert(Offset < AT->getNumElements() * EltTypeSize);
761 uint64_t Index = Offset / EltTypeSize;
762 Offset = Offset - (Index * EltTypeSize);
763 CurType = AT->getElementType();
764 OnLiteralIndexing(CurType, Index);
765 } else if (StructType *ST = dyn_cast<StructType>(CurType)) {
766 uint32_t StructSize = DL.getTypeSizeInBits(ST) / 8;
767 assert(Offset < StructSize);
768 (void)StructSize;
769 const auto &STL = DL.getStructLayout(ST);
770 unsigned Element = STL->getElementContainingOffset(Offset);
771 Offset -= STL->getElementOffset(Element);
772 CurType = ST->getElementType(Element);
773 OnLiteralIndexing(CurType, Element);
774 } else if (auto *VT = dyn_cast<FixedVectorType>(CurType)) {
775 Type *EltTy = VT->getElementType();
776 TypeSize EltSizeBits = DL.getTypeSizeInBits(EltTy);
777 assert(EltSizeBits % 8 == 0 &&
778 "Element type size in bits must be a multiple of 8.");
779 uint32_t EltTypeSize = EltSizeBits / 8;
780 assert(Offset < VT->getNumElements() * EltTypeSize);
781 uint64_t Index = Offset / EltTypeSize;
782 Offset -= Index * EltTypeSize;
783 CurType = EltTy;
784 OnLiteralIndexing(CurType, Index);
785
786 } else {
787 // Unknown composite kind; give up.
788 return true;
789 }
790 } while (Offset > 0);
791
792 return false;
793}
794
796SPIRVEmitIntrinsics::buildLogicalAccessChainFromGEP(GetElementPtrInst &GEP) {
797 auto &DL = CurrF->getDataLayout();
798 IRBuilder<> B(GEP.getParent());
799 B.SetInsertPoint(&GEP);
800
801 std::vector<Value *> Indices;
802 Indices.push_back(ConstantInt::get(
803 IntegerType::getInt32Ty(CurrF->getContext()), 0, /* Signed= */ false));
804 walkLogicalAccessChain(
805 GEP,
806 [&Indices, &B](Type *EltType, uint64_t Index) {
807 Indices.push_back(
808 ConstantInt::get(B.getInt64Ty(), Index, /* Signed= */ false));
809 },
810 [&Indices, &B, &DL](Type *EltType, Value *Offset) {
811 uint32_t EltTypeSize = DL.getTypeSizeInBits(EltType) / 8;
812 Value *Index = B.CreateUDiv(
813 Offset, ConstantInt::get(Offset->getType(), EltTypeSize,
814 /* Signed= */ false));
815 Indices.push_back(Index);
816 });
817
818 SmallVector<Type *, 2> Types = {GEP.getType(), GEP.getOperand(0)->getType()};
819 SmallVector<Value *, 4> Args;
820 Args.push_back(B.getInt1(GEP.isInBounds()));
821 Args.push_back(GEP.getOperand(0));
822 llvm::append_range(Args, Indices);
823 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
824 replaceAllUsesWithAndErase(B, &GEP, NewI);
825 return NewI;
826}
827
828Type *SPIRVEmitIntrinsics::getGEPTypeLogical(GetElementPtrInst *GEP) {
829
830 Type *CurType = GEP->getResultElementType();
831
832 bool Interrupted = walkLogicalAccessChain(
833 *GEP, [&CurType](Type *EltType, uint64_t Index) { CurType = EltType; },
834 [&CurType](Type *EltType, Value *Index) { CurType = EltType; });
835
836 return Interrupted ? GEP->getResultElementType() : CurType;
837}
838
839Type *SPIRVEmitIntrinsics::getGEPType(GetElementPtrInst *Ref) {
840 if (Ref->getSourceElementType() ==
841 IntegerType::getInt8Ty(CurrF->getContext()) &&
843 return getGEPTypeLogical(Ref);
844 }
845
846 Type *Ty = nullptr;
847 // TODO: not sure if GetElementPtrInst::getTypeAtIndex() does anything
848 // useful here
849 if (isNestedPointer(Ref->getSourceElementType())) {
850 Ty = Ref->getSourceElementType();
851 for (Use &U : drop_begin(Ref->indices()))
852 Ty = GetElementPtrInst::getTypeAtIndex(Ty, U.get());
853 } else {
854 Ty = Ref->getResultElementType();
855 }
856 return Ty;
857}
858
859Type *SPIRVEmitIntrinsics::deduceElementTypeHelper(
860 Value *I, std::unordered_set<Value *> &Visited, bool UnknownElemTypeI8,
861 bool IgnoreKnownType) {
862 // allow to pass nullptr as an argument
863 if (!I)
864 return nullptr;
865
866 // maybe already known
867 if (!IgnoreKnownType)
868 if (Type *KnownTy = GR->findDeducedElementType(I))
869 return KnownTy;
870
871 // maybe a cycle
872 if (!Visited.insert(I).second)
873 return nullptr;
874
875 // fallback value in case when we fail to deduce a type
876 Type *Ty = nullptr;
877 // look for known basic patterns of type inference
878 if (auto *Ref = dyn_cast<AllocaInst>(I)) {
879 maybeAssignPtrType(Ty, I, Ref->getAllocatedType(), UnknownElemTypeI8);
880 } else if (auto *Ref = dyn_cast<GetElementPtrInst>(I)) {
881 Ty = getGEPType(Ref);
882 } else if (auto *SGEP = dyn_cast<StructuredGEPInst>(I)) {
883 Ty = SGEP->getResultElementType();
884 } else if (auto *Ref = dyn_cast<LoadInst>(I)) {
885 Value *Op = Ref->getPointerOperand();
886 Type *KnownTy = GR->findDeducedElementType(Op);
887 if (!KnownTy)
888 KnownTy = Op->getType();
889 if (Type *ElemTy = getPointeeType(KnownTy))
890 maybeAssignPtrType(Ty, I, ElemTy, UnknownElemTypeI8);
891 } else if (auto *Ref = dyn_cast<GlobalValue>(I)) {
892 if (auto *Fn = dyn_cast<Function>(Ref)) {
893 Ty = SPIRV::getOriginalFunctionType(*Fn);
894 GR->addDeducedElementType(I, Ty);
895 } else {
896 Ty = deduceElementTypeByValueDeep(
897 Ref->getValueType(),
898 Ref->getNumOperands() > 0 ? Ref->getOperand(0) : nullptr, Visited,
899 UnknownElemTypeI8);
900 }
901 } else if (auto *Ref = dyn_cast<AddrSpaceCastInst>(I)) {
902 Type *RefTy = deduceElementTypeHelper(Ref->getPointerOperand(), Visited,
903 UnknownElemTypeI8);
904 maybeAssignPtrType(Ty, I, RefTy, UnknownElemTypeI8);
905 } else if (auto *Ref = dyn_cast<IntToPtrInst>(I)) {
906 maybeAssignPtrType(Ty, I, Ref->getDestTy(), UnknownElemTypeI8);
907 } else if (auto *Ref = dyn_cast<BitCastInst>(I)) {
908 if (Type *Src = Ref->getSrcTy(), *Dest = Ref->getDestTy();
909 isPointerTy(Src) && isPointerTy(Dest))
910 Ty = deduceElementTypeHelper(Ref->getOperand(0), Visited,
911 UnknownElemTypeI8);
912 } else if (auto *Ref = dyn_cast<AtomicCmpXchgInst>(I)) {
913 Value *Op = Ref->getNewValOperand();
914 if (isPointerTy(Op->getType()))
915 Ty = deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8);
916 } else if (auto *Ref = dyn_cast<AtomicRMWInst>(I)) {
917 Value *Op = Ref->getValOperand();
918 if (isPointerTy(Op->getType()))
919 Ty = deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8);
920 } else if (auto *Ref = dyn_cast<PHINode>(I)) {
921 Type *BestTy = nullptr;
922 unsigned MaxN = 1;
923 DenseMap<Type *, unsigned> PhiTys;
924 for (int i = Ref->getNumIncomingValues() - 1; i >= 0; --i) {
925 Ty = deduceElementTypeByUsersDeep(Ref->getIncomingValue(i), Visited,
926 UnknownElemTypeI8);
927 if (!Ty)
928 continue;
929 auto It = PhiTys.try_emplace(Ty, 1);
930 if (!It.second) {
931 ++It.first->second;
932 if (It.first->second > MaxN) {
933 MaxN = It.first->second;
934 BestTy = Ty;
935 }
936 }
937 }
938 if (BestTy)
939 Ty = BestTy;
940 } else if (auto *Ref = dyn_cast<SelectInst>(I)) {
941 for (Value *Op : {Ref->getTrueValue(), Ref->getFalseValue()}) {
942 Ty = deduceElementTypeByUsersDeep(Op, Visited, UnknownElemTypeI8);
943 if (Ty)
944 break;
945 }
946 } else if (auto *CI = dyn_cast<CallInst>(I)) {
947 static StringMap<unsigned> ResTypeByArg = {
948 {"to_global", 0},
949 {"to_local", 0},
950 {"to_private", 0},
951 {"__spirv_GenericCastToPtr_ToGlobal", 0},
952 {"__spirv_GenericCastToPtr_ToLocal", 0},
953 {"__spirv_GenericCastToPtr_ToPrivate", 0},
954 {"__spirv_GenericCastToPtrExplicit_ToGlobal", 0},
955 {"__spirv_GenericCastToPtrExplicit_ToLocal", 0},
956 {"__spirv_GenericCastToPtrExplicit_ToPrivate", 0}};
957 // TODO: maybe improve performance by caching demangled names
958
960 if (II && II->getIntrinsicID() == Intrinsic::spv_resource_getpointer) {
961 auto *HandleType = cast<TargetExtType>(II->getOperand(0)->getType());
962 if (HandleType->getTargetExtName() == "spirv.Image" ||
963 HandleType->getTargetExtName() == "spirv.SignedImage") {
964 for (User *U : II->users()) {
965 Ty = cast<Instruction>(U)->getAccessType();
966 if (Ty)
967 break;
968 }
969 } else if (HandleType->getTargetExtName() == "spirv.VulkanBuffer") {
970 // This call is supposed to index into an array
971 Ty = HandleType->getTypeParameter(0);
972 if (Ty->isArrayTy())
973 Ty = Ty->getArrayElementType();
974 else {
975 assert(Ty && Ty->isStructTy());
976 uint32_t Index = cast<ConstantInt>(II->getOperand(1))->getZExtValue();
977 Ty = cast<StructType>(Ty)->getElementType(Index);
978 }
980 } else {
981 llvm_unreachable("Unknown handle type for spv_resource_getpointer.");
982 }
983 } else if (II && II->getIntrinsicID() ==
984 Intrinsic::spv_generic_cast_to_ptr_explicit) {
985 Ty = deduceElementTypeHelper(CI->getArgOperand(0), Visited,
986 UnknownElemTypeI8);
987 } else if (Function *CalledF = CI->getCalledFunction()) {
988 std::string DemangledName =
989 getOclOrSpirvBuiltinDemangledName(CalledF->getName());
990 if (DemangledName.length() > 0)
991 DemangledName = SPIRV::lookupBuiltinNameHelper(DemangledName);
992 auto AsArgIt = ResTypeByArg.find(DemangledName);
993 if (AsArgIt != ResTypeByArg.end())
994 Ty = deduceElementTypeHelper(CI->getArgOperand(AsArgIt->second),
995 Visited, UnknownElemTypeI8);
996 else if (Type *KnownRetTy = GR->findDeducedElementType(CalledF))
997 Ty = KnownRetTy;
998 }
999 }
1000
1001 // remember the found relationship
1002 if (Ty && !IgnoreKnownType) {
1003 // specify nested types if needed, otherwise return unchanged
1005 }
1006
1007 return Ty;
1008}
1009
1010// Re-create a type of the value if it has untyped pointer fields, also nested.
1011// Return the original value type if no corrections of untyped pointer
1012// information is found or needed.
1013Type *SPIRVEmitIntrinsics::deduceNestedTypeHelper(User *U,
1014 bool UnknownElemTypeI8) {
1015 std::unordered_set<Value *> Visited;
1016 return deduceNestedTypeHelper(U, U->getType(), Visited, UnknownElemTypeI8);
1017}
1018
1019Type *SPIRVEmitIntrinsics::deduceNestedTypeHelper(
1020 User *U, Type *OrigTy, std::unordered_set<Value *> &Visited,
1021 bool UnknownElemTypeI8) {
1022 if (!U)
1023 return OrigTy;
1024
1025 // maybe already known
1026 if (Type *KnownTy = GR->findDeducedCompositeType(U))
1027 return KnownTy;
1028
1029 // maybe a cycle
1030 if (!Visited.insert(U).second)
1031 return OrigTy;
1032
1033 if (isa<StructType>(OrigTy)) {
1035 bool Change = false;
1036 for (unsigned i = 0; i < U->getNumOperands(); ++i) {
1037 Value *Op = U->getOperand(i);
1038 assert(Op && "Operands should not be null.");
1039 Type *OpTy = Op->getType();
1040 Type *Ty = OpTy;
1041 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
1042 if (Type *NestedTy =
1043 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
1044 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
1045 } else {
1046 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
1047 UnknownElemTypeI8);
1048 }
1049 Tys.push_back(Ty);
1050 Change |= Ty != OpTy;
1051 }
1052 if (Change) {
1053 Type *NewTy = StructType::create(Tys);
1054 GR->addDeducedCompositeType(U, NewTy);
1055 return NewTy;
1056 }
1057 } else if (auto *ArrTy = dyn_cast<ArrayType>(OrigTy)) {
1058 if (Value *Op = U->getNumOperands() > 0 ? U->getOperand(0) : nullptr) {
1059 Type *OpTy = ArrTy->getElementType();
1060 Type *Ty = OpTy;
1061 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
1062 if (Type *NestedTy =
1063 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
1064 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
1065 } else {
1066 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
1067 UnknownElemTypeI8);
1068 }
1069 if (Ty != OpTy) {
1070 Type *NewTy = ArrayType::get(Ty, ArrTy->getNumElements());
1071 GR->addDeducedCompositeType(U, NewTy);
1072 return NewTy;
1073 }
1074 }
1075 } else if (auto *VecTy = dyn_cast<VectorType>(OrigTy)) {
1076 if (Value *Op = U->getNumOperands() > 0 ? U->getOperand(0) : nullptr) {
1077 Type *OpTy = VecTy->getElementType();
1078 Type *Ty = OpTy;
1079 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
1080 if (Type *NestedTy =
1081 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
1082 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
1083 } else {
1084 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
1085 UnknownElemTypeI8);
1086 }
1087 if (Ty != OpTy) {
1088 Type *NewTy = VectorType::get(Ty, VecTy->getElementCount());
1090 return NewTy;
1091 }
1092 }
1093 }
1094
1095 return OrigTy;
1096}
1097
1098Type *SPIRVEmitIntrinsics::deduceElementType(Value *I, bool UnknownElemTypeI8) {
1099 if (Type *Ty = deduceElementTypeHelper(I, UnknownElemTypeI8))
1100 return Ty;
1101 if (!UnknownElemTypeI8)
1102 return nullptr;
1103 insertTodoType(I);
1104 return IntegerType::getInt8Ty(I->getContext());
1105}
1106
1108 Value *PointerOperand) {
1109 Type *PointeeTy = GR->findDeducedElementType(PointerOperand);
1110 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
1111 return nullptr;
1112 auto *PtrTy = dyn_cast<PointerType>(I->getType());
1113 if (!PtrTy)
1114 return I->getType();
1115 if (Type *NestedTy = GR->findDeducedElementType(I))
1116 return getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
1117 return nullptr;
1118}
1119
1120// Try to deduce element type for a call base. Returns false if this is an
1121// indirect function invocation, and true otherwise.
1122bool SPIRVEmitIntrinsics::deduceOperandElementTypeCalledFunction(
1123 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
1124 Type *&KnownElemTy, bool &Incomplete) {
1125 Function *CalledF = CI->getCalledFunction();
1126 if (!CalledF)
1127 return false;
1128 std::string DemangledName =
1130 if (DemangledName.length() > 0 &&
1131 !StringRef(DemangledName).starts_with("llvm.")) {
1132 const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(*CalledF);
1133 auto [Grp, Opcode, ExtNo] = SPIRV::mapBuiltinToOpcode(
1134 DemangledName, ST.getPreferredInstructionSet());
1135 if (Opcode == SPIRV::OpGroupAsyncCopy) {
1136 for (unsigned i = 0, PtrCnt = 0; i < CI->arg_size() && PtrCnt < 2; ++i) {
1137 Value *Op = CI->getArgOperand(i);
1138 if (!isPointerTy(Op->getType()))
1139 continue;
1140 ++PtrCnt;
1141 if (Type *ElemTy = GR->findDeducedElementType(Op))
1142 KnownElemTy = ElemTy; // src will rewrite dest if both are defined
1143 Ops.push_back(std::make_pair(Op, i));
1144 }
1145 } else if (Grp == SPIRV::Atomic || Grp == SPIRV::AtomicFloating) {
1146 if (CI->arg_size() == 0)
1147 return true;
1148 Value *Op = CI->getArgOperand(0);
1149 if (!isPointerTy(Op->getType()))
1150 return true;
1151 switch (Opcode) {
1152 case SPIRV::OpAtomicFAddEXT:
1153 case SPIRV::OpAtomicFMinEXT:
1154 case SPIRV::OpAtomicFMaxEXT:
1155 case SPIRV::OpAtomicLoad:
1156 case SPIRV::OpAtomicCompareExchangeWeak:
1157 case SPIRV::OpAtomicCompareExchange:
1158 case SPIRV::OpAtomicExchange:
1159 case SPIRV::OpAtomicIAdd:
1160 case SPIRV::OpAtomicISub:
1161 case SPIRV::OpAtomicOr:
1162 case SPIRV::OpAtomicXor:
1163 case SPIRV::OpAtomicAnd:
1164 case SPIRV::OpAtomicUMin:
1165 case SPIRV::OpAtomicUMax:
1166 case SPIRV::OpAtomicSMin:
1167 case SPIRV::OpAtomicSMax: {
1168 KnownElemTy = isPointerTy(CI->getType()) ? getAtomicElemTy(GR, CI, Op)
1169 : CI->getType();
1170 if (!KnownElemTy)
1171 return true;
1172 Incomplete = isTodoType(Op);
1173 Ops.push_back(std::make_pair(Op, 0));
1174 } break;
1175 case SPIRV::OpAtomicStore: {
1176 if (CI->arg_size() < 4)
1177 return true;
1178 Value *ValOp = CI->getArgOperand(3);
1179 KnownElemTy = isPointerTy(ValOp->getType())
1180 ? getAtomicElemTy(GR, CI, Op)
1181 : ValOp->getType();
1182 if (!KnownElemTy)
1183 return true;
1184 Incomplete = isTodoType(Op);
1185 Ops.push_back(std::make_pair(Op, 0));
1186 } break;
1187 }
1188 }
1189 }
1190 return true;
1191}
1192
1193// Try to deduce element type for a function pointer.
1194void SPIRVEmitIntrinsics::deduceOperandElementTypeFunctionPointer(
1195 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
1196 Type *&KnownElemTy, bool IsPostprocessing) {
1197 Value *Op = CI->getCalledOperand();
1198 if (!Op || !isPointerTy(Op->getType()))
1199 return;
1200 Ops.push_back(std::make_pair(Op, std::numeric_limits<unsigned>::max()));
1201 FunctionType *FTy = SPIRV::getOriginalFunctionType(*CI);
1202 bool IsNewFTy = false, IsIncomplete = false;
1204 for (auto &&[ParmIdx, Arg] : llvm::enumerate(CI->args())) {
1205 Type *ArgTy = Arg->getType();
1206 if (ArgTy->isPointerTy()) {
1207 if (Type *ElemTy = GR->findDeducedElementType(Arg)) {
1208 IsNewFTy = true;
1209 ArgTy = getTypedPointerWrapper(ElemTy, getPointerAddressSpace(ArgTy));
1210 if (isTodoType(Arg))
1211 IsIncomplete = true;
1212 } else {
1213 IsIncomplete = true;
1214 }
1215 } else {
1216 ArgTy = FTy->getFunctionParamType(ParmIdx);
1217 }
1218 ArgTys.push_back(ArgTy);
1219 }
1220 Type *RetTy = FTy->getReturnType();
1221 if (CI->getType()->isPointerTy()) {
1222 if (Type *ElemTy = GR->findDeducedElementType(CI)) {
1223 IsNewFTy = true;
1224 RetTy =
1226 if (isTodoType(CI))
1227 IsIncomplete = true;
1228 } else {
1229 IsIncomplete = true;
1230 }
1231 }
1232 if (!IsPostprocessing && IsIncomplete)
1233 insertTodoType(Op);
1234 KnownElemTy =
1235 IsNewFTy ? FunctionType::get(RetTy, ArgTys, FTy->isVarArg()) : FTy;
1236}
1237
1238bool SPIRVEmitIntrinsics::deduceOperandElementTypeFunctionRet(
1239 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
1240 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing,
1241 Type *&KnownElemTy, Value *Op, Function *F) {
1242 KnownElemTy = GR->findDeducedElementType(F);
1243 if (KnownElemTy)
1244 return false;
1245 if (Type *OpElemTy = GR->findDeducedElementType(Op)) {
1246 OpElemTy = normalizeType(OpElemTy);
1247 GR->addDeducedElementType(F, OpElemTy);
1248 GR->addReturnType(
1249 F, TypedPointerType::get(OpElemTy,
1250 getPointerAddressSpace(F->getReturnType())));
1251 // non-recursive update of types in function uses
1252 DenseSet<std::pair<Value *, Value *>> VisitedSubst{std::make_pair(I, Op)};
1253 for (User *U : F->users()) {
1254 CallInst *CI = dyn_cast<CallInst>(U);
1255 if (!CI || CI->getCalledFunction() != F)
1256 continue;
1257 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(CI)) {
1258 if (Type *PrevElemTy = GR->findDeducedElementType(CI)) {
1259 GR->updateAssignType(AssignCI, CI,
1260 getNormalizedPoisonValue(OpElemTy));
1261 propagateElemType(CI, PrevElemTy, VisitedSubst);
1262 }
1263 }
1264 }
1265 // Non-recursive update of types in the function uncomplete returns.
1266 // This may happen just once per a function, the latch is a pair of
1267 // findDeducedElementType(F) / addDeducedElementType(F, ...).
1268 // With or without the latch it is a non-recursive call due to
1269 // IncompleteRets set to nullptr in this call.
1270 if (IncompleteRets)
1271 for (Instruction *IncompleteRetI : *IncompleteRets)
1272 deduceOperandElementType(IncompleteRetI, nullptr, AskOps,
1273 IsPostprocessing);
1274 } else if (IncompleteRets) {
1275 IncompleteRets->insert(I);
1276 }
1277 TypeValidated.insert(I);
1278 return true;
1279}
1280
1281// If the Instruction has Pointer operands with unresolved types, this function
1282// tries to deduce them. If the Instruction has Pointer operands with known
1283// types which differ from expected, this function tries to insert a bitcast to
1284// resolve the issue.
1285void SPIRVEmitIntrinsics::deduceOperandElementType(
1286 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
1287 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing) {
1289 Type *KnownElemTy = nullptr;
1290 bool Incomplete = false;
1291 // look for known basic patterns of type inference
1292 if (auto *Ref = dyn_cast<PHINode>(I)) {
1293 if (!isPointerTy(I->getType()) ||
1294 !(KnownElemTy = GR->findDeducedElementType(I)))
1295 return;
1296 Incomplete = isTodoType(I);
1297 for (unsigned i = 0; i < Ref->getNumIncomingValues(); i++) {
1298 Value *Op = Ref->getIncomingValue(i);
1299 if (isPointerTy(Op->getType()))
1300 Ops.push_back(std::make_pair(Op, i));
1301 }
1302 } else if (auto *Ref = dyn_cast<AddrSpaceCastInst>(I)) {
1303 KnownElemTy = GR->findDeducedElementType(I);
1304 if (!KnownElemTy)
1305 return;
1306 Incomplete = isTodoType(I);
1307 Ops.push_back(std::make_pair(Ref->getPointerOperand(), 0));
1308 } else if (auto *Ref = dyn_cast<BitCastInst>(I)) {
1309 if (!isPointerTy(I->getType()))
1310 return;
1311 KnownElemTy = GR->findDeducedElementType(I);
1312 if (!KnownElemTy)
1313 return;
1314 Incomplete = isTodoType(I);
1315 Ops.push_back(std::make_pair(Ref->getOperand(0), 0));
1316 } else if (auto *Ref = dyn_cast<GetElementPtrInst>(I)) {
1317 if (GR->findDeducedElementType(Ref->getPointerOperand()))
1318 return;
1319 KnownElemTy = Ref->getSourceElementType();
1320 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1322 } else if (auto *Ref = dyn_cast<StructuredGEPInst>(I)) {
1323 if (GR->findDeducedElementType(Ref->getPointerOperand()))
1324 return;
1325 KnownElemTy = Ref->getBaseType();
1326 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1328 } else if (auto *Ref = dyn_cast<LoadInst>(I)) {
1329 KnownElemTy = I->getType();
1330 if (isUntypedPointerTy(KnownElemTy))
1331 return;
1332 Type *PointeeTy = GR->findDeducedElementType(Ref->getPointerOperand());
1333 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
1334 return;
1335 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1337 } else if (auto *Ref = dyn_cast<StoreInst>(I)) {
1338 if (!(KnownElemTy =
1339 reconstructType(Ref->getValueOperand(), false, IsPostprocessing)))
1340 return;
1341 Type *PointeeTy = GR->findDeducedElementType(Ref->getPointerOperand());
1342 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
1343 return;
1344 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1346 } else if (auto *Ref = dyn_cast<AtomicCmpXchgInst>(I)) {
1347 KnownElemTy = isPointerTy(I->getType())
1348 ? getAtomicElemTy(GR, I, Ref->getPointerOperand())
1349 : I->getType();
1350 if (!KnownElemTy)
1351 return;
1352 Incomplete = isTodoType(Ref->getPointerOperand());
1353 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1355 } else if (auto *Ref = dyn_cast<AtomicRMWInst>(I)) {
1356 KnownElemTy = isPointerTy(I->getType())
1357 ? getAtomicElemTy(GR, I, Ref->getPointerOperand())
1358 : I->getType();
1359 if (!KnownElemTy)
1360 return;
1361 Incomplete = isTodoType(Ref->getPointerOperand());
1362 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1364 } else if (auto *Ref = dyn_cast<SelectInst>(I)) {
1365 if (!isPointerTy(I->getType()) ||
1366 !(KnownElemTy = GR->findDeducedElementType(I)))
1367 return;
1368 Incomplete = isTodoType(I);
1369 for (unsigned i = 0; i < Ref->getNumOperands(); i++) {
1370 Value *Op = Ref->getOperand(i);
1371 if (isPointerTy(Op->getType()))
1372 Ops.push_back(std::make_pair(Op, i));
1373 }
1374 } else if (auto *Ref = dyn_cast<ReturnInst>(I)) {
1375 if (!isPointerTy(CurrF->getReturnType()))
1376 return;
1377 Value *Op = Ref->getReturnValue();
1378 if (!Op)
1379 return;
1380 if (deduceOperandElementTypeFunctionRet(I, IncompleteRets, AskOps,
1381 IsPostprocessing, KnownElemTy, Op,
1382 CurrF))
1383 return;
1384 Incomplete = isTodoType(CurrF);
1385 Ops.push_back(std::make_pair(Op, 0));
1386 } else if (auto *Ref = dyn_cast<ICmpInst>(I)) {
1387 if (!isPointerTy(Ref->getOperand(0)->getType()))
1388 return;
1389 Value *Op0 = Ref->getOperand(0);
1390 Value *Op1 = Ref->getOperand(1);
1391 bool Incomplete0 = isTodoType(Op0);
1392 bool Incomplete1 = isTodoType(Op1);
1393 Type *ElemTy1 = GR->findDeducedElementType(Op1);
1394 Type *ElemTy0 = (Incomplete0 && !Incomplete1 && ElemTy1)
1395 ? nullptr
1396 : GR->findDeducedElementType(Op0);
1397 if (ElemTy0) {
1398 KnownElemTy = ElemTy0;
1399 Incomplete = Incomplete0;
1400 Ops.push_back(std::make_pair(Op1, 1));
1401 } else if (ElemTy1) {
1402 KnownElemTy = ElemTy1;
1403 Incomplete = Incomplete1;
1404 Ops.push_back(std::make_pair(Op0, 0));
1405 }
1406 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
1407 if (!CI->isIndirectCall())
1408 deduceOperandElementTypeCalledFunction(CI, Ops, KnownElemTy, Incomplete);
1409 else if (HaveFunPtrs)
1410 deduceOperandElementTypeFunctionPointer(CI, Ops, KnownElemTy,
1411 IsPostprocessing);
1412 }
1413
1414 // There is no enough info to deduce types or all is valid.
1415 if (!KnownElemTy || Ops.size() == 0)
1416 return;
1417
1418 LLVMContext &Ctx = CurrF->getContext();
1419 IRBuilder<> B(Ctx);
1420 for (auto &OpIt : Ops) {
1421 Value *Op = OpIt.first;
1422 if (AskOps && !AskOps->contains(Op))
1423 continue;
1424 Type *AskTy = nullptr;
1425 CallInst *AskCI = nullptr;
1426 if (IsPostprocessing && AskOps) {
1427 AskTy = GR->findDeducedElementType(Op);
1428 AskCI = GR->findAssignPtrTypeInstr(Op);
1429 assert(AskTy && AskCI);
1430 }
1431 Type *Ty = AskTy ? AskTy : GR->findDeducedElementType(Op);
1432 if (Ty == KnownElemTy)
1433 continue;
1434 Value *OpTyVal = getNormalizedPoisonValue(KnownElemTy);
1435 Type *OpTy = Op->getType();
1436 if (Op->hasUseList() &&
1437 (!Ty || AskTy || isUntypedPointerTy(Ty) || isTodoType(Op))) {
1438 Type *PrevElemTy = GR->findDeducedElementType(Op);
1439 GR->addDeducedElementType(Op, normalizeType(KnownElemTy));
1440 // check if KnownElemTy is complete
1441 if (!Incomplete)
1442 eraseTodoType(Op);
1443 else if (!IsPostprocessing)
1444 insertTodoType(Op);
1445 // check if there is existing Intrinsic::spv_assign_ptr_type instruction
1446 CallInst *AssignCI = AskCI ? AskCI : GR->findAssignPtrTypeInstr(Op);
1447 if (AssignCI == nullptr) {
1448 Instruction *User = dyn_cast<Instruction>(Op->use_begin()->get());
1449 setInsertPointSkippingPhis(B, User ? User->getNextNode() : I);
1450 CallInst *CI =
1451 buildIntrWithMD(Intrinsic::spv_assign_ptr_type, {OpTy}, OpTyVal, Op,
1452 {B.getInt32(getPointerAddressSpace(OpTy))}, B);
1453 GR->addAssignPtrTypeInstr(Op, CI);
1454 } else {
1455 GR->updateAssignType(AssignCI, Op, OpTyVal);
1456 DenseSet<std::pair<Value *, Value *>> VisitedSubst{
1457 std::make_pair(I, Op)};
1458 propagateElemTypeRec(Op, KnownElemTy, PrevElemTy, VisitedSubst);
1459 }
1460 } else {
1461 eraseTodoType(Op);
1462 CallInst *PtrCastI =
1463 buildSpvPtrcast(I->getParent()->getParent(), Op, KnownElemTy);
1464 if (OpIt.second == std::numeric_limits<unsigned>::max())
1465 dyn_cast<CallInst>(I)->setCalledOperand(PtrCastI);
1466 else
1467 I->setOperand(OpIt.second, PtrCastI);
1468 }
1469 }
1470 TypeValidated.insert(I);
1471}
1472
1473void SPIRVEmitIntrinsics::replaceMemInstrUses(Instruction *Old,
1474 Instruction *New,
1475 IRBuilder<> &B) {
1476 while (!Old->user_empty()) {
1477 auto *U = Old->user_back();
1478 if (isAssignTypeInstr(U)) {
1479 B.SetInsertPoint(U);
1480 SmallVector<Value *, 2> Args = {New, U->getOperand(1)};
1481 CallInst *AssignCI =
1482 B.CreateIntrinsic(Intrinsic::spv_assign_type, {New->getType()}, Args);
1483 GR->addAssignPtrTypeInstr(New, AssignCI);
1484 U->eraseFromParent();
1485 } else if (isMemInstrToReplace(U) || isa<ReturnInst>(U) ||
1486 isa<CallInst>(U)) {
1487 U->replaceUsesOfWith(Old, New);
1488 } else {
1489 llvm_unreachable("illegal aggregate intrinsic user");
1490 }
1491 }
1492 New->copyMetadata(*Old);
1493 Old->eraseFromParent();
1494}
1495
1496void SPIRVEmitIntrinsics::preprocessUndefs(IRBuilder<> &B) {
1497 std::queue<Instruction *> Worklist;
1498 for (auto &I : instructions(CurrF))
1499 Worklist.push(&I);
1500
1501 while (!Worklist.empty()) {
1502 Instruction *I = Worklist.front();
1503 bool BPrepared = false;
1504 Worklist.pop();
1505
1506 for (auto &Op : I->operands()) {
1507 auto *AggrUndef = dyn_cast<UndefValue>(Op);
1508 if (!AggrUndef || !Op->getType()->isAggregateType())
1509 continue;
1510
1511 if (!BPrepared) {
1513 BPrepared = true;
1514 }
1515 auto *IntrUndef = B.CreateIntrinsic(Intrinsic::spv_undef, {});
1516 Worklist.push(IntrUndef);
1517 I->replaceUsesOfWith(Op, IntrUndef);
1518 AggrConsts[IntrUndef] = AggrUndef;
1519 AggrConstTypes[IntrUndef] = AggrUndef->getType();
1520 }
1521 }
1522}
1523
1524void SPIRVEmitIntrinsics::preprocessCompositeConstants(IRBuilder<> &B) {
1525 std::queue<Instruction *> Worklist;
1526 for (auto &I : instructions(CurrF))
1527 Worklist.push(&I);
1528
1529 while (!Worklist.empty()) {
1530 auto *I = Worklist.front();
1531 bool IsPhi = isa<PHINode>(I), BPrepared = false;
1532 assert(I);
1533 bool KeepInst = false;
1534 for (const auto &Op : I->operands()) {
1535 Constant *AggrConst = nullptr;
1536 Type *ResTy = nullptr;
1537 if (auto *COp = dyn_cast<ConstantVector>(Op)) {
1538 AggrConst = COp;
1539 ResTy = COp->getType();
1540 } else if (auto *COp = dyn_cast<ConstantArray>(Op)) {
1541 AggrConst = COp;
1542 ResTy = B.getInt32Ty();
1543 } else if (auto *COp = dyn_cast<ConstantStruct>(Op)) {
1544 AggrConst = COp;
1545 ResTy = B.getInt32Ty();
1546 } else if (auto *COp = dyn_cast<ConstantDataArray>(Op)) {
1547 AggrConst = COp;
1548 ResTy = B.getInt32Ty();
1549 } else if (auto *COp = dyn_cast<ConstantAggregateZero>(Op)) {
1550 AggrConst = COp;
1551 ResTy = Op->getType()->isVectorTy() ? COp->getType() : B.getInt32Ty();
1552 }
1553 if (AggrConst) {
1555 if (auto *COp = dyn_cast<ConstantDataSequential>(Op))
1556 for (unsigned i = 0; i < COp->getNumElements(); ++i)
1557 Args.push_back(COp->getElementAsConstant(i));
1558 else
1559 llvm::append_range(Args, AggrConst->operands());
1560 if (!BPrepared) {
1561 IsPhi ? B.SetInsertPointPastAllocas(I->getParent()->getParent())
1562 : B.SetInsertPoint(I);
1563 BPrepared = true;
1564 }
1565 auto *CI =
1566 B.CreateIntrinsic(Intrinsic::spv_const_composite, {ResTy}, {Args});
1567 Worklist.push(CI);
1568 I->replaceUsesOfWith(Op, CI);
1569 KeepInst = true;
1570 AggrConsts[CI] = AggrConst;
1571 AggrConstTypes[CI] = deduceNestedTypeHelper(AggrConst, false);
1572 }
1573 }
1574 if (!KeepInst)
1575 Worklist.pop();
1576 }
1577}
1578
1580 IRBuilder<> &B) {
1581 LLVMContext &Ctx = I->getContext();
1583 B.CreateIntrinsic(Intrinsic::spv_assign_decoration, {I->getType()},
1584 {I, MetadataAsValue::get(Ctx, MDNode::get(Ctx, {Node}))});
1585}
1586
1588 unsigned RoundingModeDeco,
1589 IRBuilder<> &B) {
1590 LLVMContext &Ctx = I->getContext();
1592 MDNode *RoundingModeNode = MDNode::get(
1593 Ctx,
1595 ConstantInt::get(Int32Ty, SPIRV::Decoration::FPRoundingMode)),
1596 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, RoundingModeDeco))});
1597 createDecorationIntrinsic(I, RoundingModeNode, B);
1598}
1599
1601 IRBuilder<> &B) {
1602 LLVMContext &Ctx = I->getContext();
1604 MDNode *SaturatedConversionNode =
1605 MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(
1606 Int32Ty, SPIRV::Decoration::SaturatedConversion))});
1607 createDecorationIntrinsic(I, SaturatedConversionNode, B);
1608}
1609
1611 if (auto *CI = dyn_cast<CallInst>(I)) {
1612 if (Function *Fu = CI->getCalledFunction()) {
1613 if (Fu->isIntrinsic()) {
1614 unsigned const int IntrinsicId = Fu->getIntrinsicID();
1615 switch (IntrinsicId) {
1616 case Intrinsic::fptosi_sat:
1617 case Intrinsic::fptoui_sat:
1619 break;
1620 default:
1621 break;
1622 }
1623 }
1624 }
1625 }
1626}
1627
1628Instruction *SPIRVEmitIntrinsics::visitCallInst(CallInst &Call) {
1629 if (!Call.isInlineAsm())
1630 return &Call;
1631
1632 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
1633 LLVMContext &Ctx = CurrF->getContext();
1634
1635 Constant *TyC = UndefValue::get(IA->getFunctionType());
1636 MDString *ConstraintString = MDString::get(Ctx, IA->getConstraintString());
1638 buildMD(TyC),
1639 MetadataAsValue::get(Ctx, MDNode::get(Ctx, ConstraintString))};
1640 for (unsigned OpIdx = 0; OpIdx < Call.arg_size(); OpIdx++)
1641 Args.push_back(Call.getArgOperand(OpIdx));
1642
1644 B.SetInsertPoint(&Call);
1645 B.CreateIntrinsic(Intrinsic::spv_inline_asm, {Args});
1646 return &Call;
1647}
1648
1649// Use a tip about rounding mode to create a decoration.
1650void SPIRVEmitIntrinsics::useRoundingMode(ConstrainedFPIntrinsic *FPI,
1651 IRBuilder<> &B) {
1652 std::optional<RoundingMode> RM = FPI->getRoundingMode();
1653 if (!RM.has_value())
1654 return;
1655 unsigned RoundingModeDeco = std::numeric_limits<unsigned>::max();
1656 switch (RM.value()) {
1657 default:
1658 // ignore unknown rounding modes
1659 break;
1660 case RoundingMode::NearestTiesToEven:
1661 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTE;
1662 break;
1663 case RoundingMode::TowardNegative:
1664 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTN;
1665 break;
1666 case RoundingMode::TowardPositive:
1667 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTP;
1668 break;
1669 case RoundingMode::TowardZero:
1670 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTZ;
1671 break;
1672 case RoundingMode::Dynamic:
1673 case RoundingMode::NearestTiesToAway:
1674 // TODO: check if supported
1675 break;
1676 }
1677 if (RoundingModeDeco == std::numeric_limits<unsigned>::max())
1678 return;
1679 // Convert the tip about rounding mode into a decoration record.
1680 createRoundingModeDecoration(FPI, RoundingModeDeco, B);
1681}
1682
1683Instruction *SPIRVEmitIntrinsics::visitSwitchInst(SwitchInst &I) {
1684 BasicBlock *ParentBB = I.getParent();
1685 Function *F = ParentBB->getParent();
1686 IRBuilder<> B(ParentBB);
1687 B.SetInsertPoint(&I);
1688 SmallVector<Value *, 4> Args;
1690 Args.push_back(I.getCondition());
1691 BBCases.push_back(I.getDefaultDest());
1692 Args.push_back(BlockAddress::get(F, I.getDefaultDest()));
1693 for (auto &Case : I.cases()) {
1694 Args.push_back(Case.getCaseValue());
1695 BBCases.push_back(Case.getCaseSuccessor());
1696 Args.push_back(BlockAddress::get(F, Case.getCaseSuccessor()));
1697 }
1698 CallInst *NewI = B.CreateIntrinsic(Intrinsic::spv_switch,
1699 {I.getOperand(0)->getType()}, {Args});
1700 // remove switch to avoid its unneeded and undesirable unwrap into branches
1701 // and conditions
1702 replaceAllUsesWith(&I, NewI);
1703 I.eraseFromParent();
1704 // insert artificial and temporary instruction to preserve valid CFG,
1705 // it will be removed after IR translation pass
1706 B.SetInsertPoint(ParentBB);
1707 IndirectBrInst *BrI = B.CreateIndirectBr(
1708 Constant::getNullValue(PointerType::getUnqual(ParentBB->getContext())),
1709 BBCases.size());
1710 for (BasicBlock *BBCase : BBCases)
1711 BrI->addDestination(BBCase);
1712 return BrI;
1713}
1714
1716 if (GEP->getNumIndices() == 0)
1717 return false;
1718 if (const auto *CI = dyn_cast<ConstantInt>(GEP->getOperand(1))) {
1719 return CI->getZExtValue() == 0;
1720 }
1721 return false;
1722}
1723
1724Instruction *SPIRVEmitIntrinsics::visitIntrinsicInst(IntrinsicInst &I) {
1725 auto *SGEP = dyn_cast<StructuredGEPInst>(&I);
1726 if (!SGEP)
1727 return &I;
1728
1729 IRBuilder<> B(I.getParent());
1730 B.SetInsertPoint(&I);
1731 SmallVector<Type *, 2> Types = {I.getType(), I.getOperand(0)->getType()};
1732 SmallVector<Value *, 4> Args;
1733 Args.push_back(/* inBounds= */ B.getInt1(true));
1734 Args.push_back(I.getOperand(0));
1735 Args.push_back(/* zero index */ B.getInt32(0));
1736 for (unsigned J = 0; J < SGEP->getNumIndices(); ++J)
1737 Args.push_back(SGEP->getIndexOperand(J));
1738
1739 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, Types, Args);
1740 replaceAllUsesWithAndErase(B, &I, NewI);
1741 return NewI;
1742}
1743
1744Instruction *SPIRVEmitIntrinsics::visitGetElementPtrInst(GetElementPtrInst &I) {
1745 IRBuilder<> B(I.getParent());
1746 B.SetInsertPoint(&I);
1747
1749 // Logical SPIR-V cannot use the OpPtrAccessChain instruction. If the first
1750 // index of the GEP is not 0, then we need to try to adjust it.
1751 //
1752 // If the GEP is doing byte addressing, try to rebuild the full access chain
1753 // from the type of the pointer.
1754 if (I.getSourceElementType() ==
1755 IntegerType::getInt8Ty(CurrF->getContext())) {
1756 return buildLogicalAccessChainFromGEP(I);
1757 }
1758
1759 // Look for the array-to-pointer decay. If this is the pattern
1760 // we can adjust the types, and prepend a 0 to the indices.
1761 Value *PtrOp = I.getPointerOperand();
1762 Type *SrcElemTy = I.getSourceElementType();
1763 Type *DeducedPointeeTy = deduceElementType(PtrOp, true);
1764
1765 if (auto *ArrTy = dyn_cast<ArrayType>(DeducedPointeeTy)) {
1766 if (ArrTy->getElementType() == SrcElemTy) {
1767 SmallVector<Value *> NewIndices;
1768 Type *FirstIdxType = I.getOperand(1)->getType();
1769 NewIndices.push_back(ConstantInt::get(FirstIdxType, 0));
1770 for (Value *Idx : I.indices())
1771 NewIndices.push_back(Idx);
1772
1773 SmallVector<Type *, 2> Types = {I.getType(), I.getPointerOperandType()};
1774 SmallVector<Value *, 4> Args;
1775 Args.push_back(B.getInt1(I.isInBounds()));
1776 Args.push_back(I.getPointerOperand());
1777 Args.append(NewIndices.begin(), NewIndices.end());
1778
1779 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
1780 replaceAllUsesWithAndErase(B, &I, NewI);
1781 return NewI;
1782 }
1783 }
1784 }
1785
1786 SmallVector<Type *, 2> Types = {I.getType(), I.getOperand(0)->getType()};
1787 SmallVector<Value *, 4> Args;
1788 Args.push_back(B.getInt1(I.isInBounds()));
1789 llvm::append_range(Args, I.operands());
1790 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
1791 replaceAllUsesWithAndErase(B, &I, NewI);
1792 return NewI;
1793}
1794
1795Instruction *SPIRVEmitIntrinsics::visitBitCastInst(BitCastInst &I) {
1796 IRBuilder<> B(I.getParent());
1797 B.SetInsertPoint(&I);
1798 Value *Source = I.getOperand(0);
1799
1800 // SPIR-V, contrary to LLVM 17+ IR, supports bitcasts between pointers of
1801 // varying element types. In case of IR coming from older versions of LLVM
1802 // such bitcasts do not provide sufficient information, should be just skipped
1803 // here, and handled in insertPtrCastOrAssignTypeInstr.
1804 if (isPointerTy(I.getType())) {
1805 replaceAllUsesWith(&I, Source);
1806 I.eraseFromParent();
1807 return nullptr;
1808 }
1809
1810 SmallVector<Type *, 2> Types = {I.getType(), Source->getType()};
1811 SmallVector<Value *> Args(I.op_begin(), I.op_end());
1812 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_bitcast, {Types}, {Args});
1813 replaceAllUsesWithAndErase(B, &I, NewI);
1814 return NewI;
1815}
1816
1817void SPIRVEmitIntrinsics::insertAssignPtrTypeTargetExt(
1818 TargetExtType *AssignedType, Value *V, IRBuilder<> &B) {
1819 Type *VTy = V->getType();
1820
1821 // A couple of sanity checks.
1822 assert((isPointerTy(VTy)) && "Expect a pointer type!");
1823 if (Type *ElemTy = getPointeeType(VTy))
1824 if (ElemTy != AssignedType)
1825 report_fatal_error("Unexpected pointer element type!");
1826
1827 CallInst *AssignCI = GR->findAssignPtrTypeInstr(V);
1828 if (!AssignCI) {
1829 GR->buildAssignType(B, AssignedType, V);
1830 return;
1831 }
1832
1833 Type *CurrentType =
1835 cast<MetadataAsValue>(AssignCI->getOperand(1))->getMetadata())
1836 ->getType();
1837 if (CurrentType == AssignedType)
1838 return;
1839
1840 // Builtin types cannot be redeclared or casted.
1841 if (CurrentType->isTargetExtTy())
1842 report_fatal_error("Type mismatch " + CurrentType->getTargetExtName() +
1843 "/" + AssignedType->getTargetExtName() +
1844 " for value " + V->getName(),
1845 false);
1846
1847 // Our previous guess about the type seems to be wrong, let's update
1848 // inferred type according to a new, more precise type information.
1849 GR->updateAssignType(AssignCI, V, getNormalizedPoisonValue(AssignedType));
1850}
1851
1852void SPIRVEmitIntrinsics::replacePointerOperandWithPtrCast(
1853 Instruction *I, Value *Pointer, Type *ExpectedElementType,
1854 unsigned OperandToReplace, IRBuilder<> &B) {
1855 TypeValidated.insert(I);
1856
1857 // Do not emit spv_ptrcast if Pointer's element type is ExpectedElementType
1858 Type *PointerElemTy = deduceElementTypeHelper(Pointer, false);
1859 if (PointerElemTy == ExpectedElementType ||
1860 isEquivalentTypes(PointerElemTy, ExpectedElementType))
1861 return;
1862
1864 Value *ExpectedElementVal = getNormalizedPoisonValue(ExpectedElementType);
1865 MetadataAsValue *VMD = buildMD(ExpectedElementVal);
1866 unsigned AddressSpace = getPointerAddressSpace(Pointer->getType());
1867 bool FirstPtrCastOrAssignPtrType = true;
1868
1869 // Do not emit new spv_ptrcast if equivalent one already exists or when
1870 // spv_assign_ptr_type already targets this pointer with the same element
1871 // type.
1872 if (Pointer->hasUseList()) {
1873 for (auto User : Pointer->users()) {
1874 auto *II = dyn_cast<IntrinsicInst>(User);
1875 if (!II ||
1876 (II->getIntrinsicID() != Intrinsic::spv_assign_ptr_type &&
1877 II->getIntrinsicID() != Intrinsic::spv_ptrcast) ||
1878 II->getOperand(0) != Pointer)
1879 continue;
1880
1881 // There is some spv_ptrcast/spv_assign_ptr_type already targeting this
1882 // pointer.
1883 FirstPtrCastOrAssignPtrType = false;
1884 if (II->getOperand(1) != VMD ||
1885 dyn_cast<ConstantInt>(II->getOperand(2))->getSExtValue() !=
1887 continue;
1888
1889 // The spv_ptrcast/spv_assign_ptr_type targeting this pointer is of the
1890 // same element type and address space.
1891 if (II->getIntrinsicID() != Intrinsic::spv_ptrcast)
1892 return;
1893
1894 // This must be a spv_ptrcast, do not emit new if this one has the same BB
1895 // as I. Otherwise, search for other spv_ptrcast/spv_assign_ptr_type.
1896 if (II->getParent() != I->getParent())
1897 continue;
1898
1899 I->setOperand(OperandToReplace, II);
1900 return;
1901 }
1902 }
1903
1904 if (isa<Instruction>(Pointer) || isa<Argument>(Pointer)) {
1905 if (FirstPtrCastOrAssignPtrType) {
1906 // If this would be the first spv_ptrcast, do not emit spv_ptrcast and
1907 // emit spv_assign_ptr_type instead.
1908 GR->buildAssignPtr(B, ExpectedElementType, Pointer);
1909 return;
1910 } else if (isTodoType(Pointer)) {
1911 eraseTodoType(Pointer);
1912 if (!isa<CallInst>(Pointer) && !isaGEP(Pointer)) {
1913 // If this wouldn't be the first spv_ptrcast but existing type info is
1914 // uncomplete, update spv_assign_ptr_type arguments.
1915 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(Pointer)) {
1916 Type *PrevElemTy = GR->findDeducedElementType(Pointer);
1917 assert(PrevElemTy);
1918 DenseSet<std::pair<Value *, Value *>> VisitedSubst{
1919 std::make_pair(I, Pointer)};
1920 GR->updateAssignType(AssignCI, Pointer, ExpectedElementVal);
1921 propagateElemType(Pointer, PrevElemTy, VisitedSubst);
1922 } else {
1923 GR->buildAssignPtr(B, ExpectedElementType, Pointer);
1924 }
1925 return;
1926 }
1927 }
1928 }
1929
1930 // Emit spv_ptrcast
1931 SmallVector<Type *, 2> Types = {Pointer->getType(), Pointer->getType()};
1932 SmallVector<Value *, 2> Args = {Pointer, VMD, B.getInt32(AddressSpace)};
1933 auto *PtrCastI = B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
1934 I->setOperand(OperandToReplace, PtrCastI);
1935 // We need to set up a pointee type for the newly created spv_ptrcast.
1936 GR->buildAssignPtr(B, ExpectedElementType, PtrCastI);
1937}
1938
1939void SPIRVEmitIntrinsics::insertPtrCastOrAssignTypeInstr(Instruction *I,
1940 IRBuilder<> &B) {
1941 // Handle basic instructions:
1942 StoreInst *SI = dyn_cast<StoreInst>(I);
1943 if (IsKernelArgInt8(CurrF, SI)) {
1944 replacePointerOperandWithPtrCast(
1945 I, SI->getValueOperand(), IntegerType::getInt8Ty(CurrF->getContext()),
1946 0, B);
1947 }
1948 if (SI) {
1949 Value *Op = SI->getValueOperand();
1950 Value *Pointer = SI->getPointerOperand();
1951 Type *OpTy = Op->getType();
1952 if (auto *OpI = dyn_cast<Instruction>(Op))
1953 OpTy = restoreMutatedType(GR, OpI, OpTy);
1954 if (OpTy == Op->getType())
1955 OpTy = deduceElementTypeByValueDeep(OpTy, Op, false);
1956 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 1, B);
1957 return;
1958 }
1959 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1960 Value *Pointer = LI->getPointerOperand();
1961 Type *OpTy = LI->getType();
1962 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
1963 if (Type *ElemTy = GR->findDeducedElementType(LI)) {
1964 OpTy = getTypedPointerWrapper(ElemTy, PtrTy->getAddressSpace());
1965 } else {
1966 Type *NewOpTy = OpTy;
1967 OpTy = deduceElementTypeByValueDeep(OpTy, LI, false);
1968 if (OpTy == NewOpTy)
1969 insertTodoType(Pointer);
1970 }
1971 }
1972 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 0, B);
1973 return;
1974 }
1975 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
1976 Value *Pointer = GEPI->getPointerOperand();
1977 Type *OpTy = nullptr;
1978
1979 // Logical SPIR-V is not allowed to use Op*PtrAccessChain instructions. If
1980 // the first index is 0, then we can trivially lower to OpAccessChain. If
1981 // not we need to try to rewrite the GEP. We avoid adding a pointer cast at
1982 // this time, and will rewrite the GEP when visiting it.
1983 if (TM->getSubtargetImpl()->isLogicalSPIRV() && !isFirstIndexZero(GEPI)) {
1984 return;
1985 }
1986
1987 // In all cases, fall back to the GEP type if type scavenging failed.
1988 if (!OpTy)
1989 OpTy = GEPI->getSourceElementType();
1990
1991 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 0, B);
1992 if (isNestedPointer(OpTy))
1993 insertTodoType(Pointer);
1994 return;
1995 }
1996
1997 // TODO: review and merge with existing logics:
1998 // Handle calls to builtins (non-intrinsics):
1999 CallInst *CI = dyn_cast<CallInst>(I);
2000 if (!CI || CI->isIndirectCall() || CI->isInlineAsm() ||
2002 return;
2003
2004 // collect information about formal parameter types
2005 std::string DemangledName =
2007 Function *CalledF = CI->getCalledFunction();
2008 SmallVector<Type *, 4> CalledArgTys;
2009 bool HaveTypes = false;
2010 for (unsigned OpIdx = 0; OpIdx < CalledF->arg_size(); ++OpIdx) {
2011 Argument *CalledArg = CalledF->getArg(OpIdx);
2012 Type *ArgType = CalledArg->getType();
2013 if (!isPointerTy(ArgType)) {
2014 CalledArgTys.push_back(nullptr);
2015 } else if (Type *ArgTypeElem = getPointeeType(ArgType)) {
2016 CalledArgTys.push_back(ArgTypeElem);
2017 HaveTypes = true;
2018 } else {
2019 Type *ElemTy = GR->findDeducedElementType(CalledArg);
2020 if (!ElemTy && hasPointeeTypeAttr(CalledArg))
2021 ElemTy = getPointeeTypeByAttr(CalledArg);
2022 if (!ElemTy) {
2023 ElemTy = getPointeeTypeByCallInst(DemangledName, CalledF, OpIdx);
2024 if (ElemTy) {
2025 GR->addDeducedElementType(CalledArg, normalizeType(ElemTy));
2026 } else {
2027 for (User *U : CalledArg->users()) {
2028 if (Instruction *Inst = dyn_cast<Instruction>(U)) {
2029 if ((ElemTy = deduceElementTypeHelper(Inst, false)) != nullptr)
2030 break;
2031 }
2032 }
2033 }
2034 }
2035 HaveTypes |= ElemTy != nullptr;
2036 CalledArgTys.push_back(ElemTy);
2037 }
2038 }
2039
2040 if (DemangledName.empty() && !HaveTypes)
2041 return;
2042
2043 for (unsigned OpIdx = 0; OpIdx < CI->arg_size(); OpIdx++) {
2044 Value *ArgOperand = CI->getArgOperand(OpIdx);
2045 if (!isPointerTy(ArgOperand->getType()))
2046 continue;
2047
2048 // Constants (nulls/undefs) are handled in insertAssignPtrTypeIntrs()
2049 if (!isa<Instruction>(ArgOperand) && !isa<Argument>(ArgOperand)) {
2050 // However, we may have assumptions about the formal argument's type and
2051 // may have a need to insert a ptr cast for the actual parameter of this
2052 // call.
2053 Argument *CalledArg = CalledF->getArg(OpIdx);
2054 if (!GR->findDeducedElementType(CalledArg))
2055 continue;
2056 }
2057
2058 Type *ExpectedType =
2059 OpIdx < CalledArgTys.size() ? CalledArgTys[OpIdx] : nullptr;
2060 if (!ExpectedType && !DemangledName.empty())
2061 ExpectedType = SPIRV::parseBuiltinCallArgumentBaseType(
2062 DemangledName, OpIdx, I->getContext());
2063 if (!ExpectedType || ExpectedType->isVoidTy())
2064 continue;
2065
2066 if (ExpectedType->isTargetExtTy() &&
2068 insertAssignPtrTypeTargetExt(cast<TargetExtType>(ExpectedType),
2069 ArgOperand, B);
2070 else
2071 replacePointerOperandWithPtrCast(CI, ArgOperand, ExpectedType, OpIdx, B);
2072 }
2073}
2074
2075Instruction *SPIRVEmitIntrinsics::visitInsertElementInst(InsertElementInst &I) {
2076 // If it's a <1 x Type> vector type, don't modify it. It's not a legal vector
2077 // type in LLT and IRTranslator will replace it by the scalar.
2078 if (isVector1(I.getType()))
2079 return &I;
2080
2081 SmallVector<Type *, 4> Types = {I.getType(), I.getOperand(0)->getType(),
2082 I.getOperand(1)->getType(),
2083 I.getOperand(2)->getType()};
2084 IRBuilder<> B(I.getParent());
2085 B.SetInsertPoint(&I);
2086 SmallVector<Value *> Args(I.op_begin(), I.op_end());
2087 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_insertelt, {Types}, {Args});
2088 replaceAllUsesWithAndErase(B, &I, NewI);
2089 return NewI;
2090}
2091
2093SPIRVEmitIntrinsics::visitExtractElementInst(ExtractElementInst &I) {
2094 // If it's a <1 x Type> vector type, don't modify it. It's not a legal vector
2095 // type in LLT and IRTranslator will replace it by the scalar.
2096 if (isVector1(I.getVectorOperandType()))
2097 return &I;
2098
2099 IRBuilder<> B(I.getParent());
2100 B.SetInsertPoint(&I);
2101 SmallVector<Type *, 3> Types = {I.getType(), I.getVectorOperandType(),
2102 I.getIndexOperand()->getType()};
2103 SmallVector<Value *, 2> Args = {I.getVectorOperand(), I.getIndexOperand()};
2104 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_extractelt, {Types}, {Args});
2105 replaceAllUsesWithAndErase(B, &I, NewI);
2106 return NewI;
2107}
2108
2109Instruction *SPIRVEmitIntrinsics::visitInsertValueInst(InsertValueInst &I) {
2110 IRBuilder<> B(I.getParent());
2111 B.SetInsertPoint(&I);
2112 SmallVector<Type *, 1> Types = {I.getInsertedValueOperand()->getType()};
2114 Value *AggregateOp = I.getAggregateOperand();
2115 if (isa<UndefValue>(AggregateOp))
2116 Args.push_back(UndefValue::get(B.getInt32Ty()));
2117 else
2118 Args.push_back(AggregateOp);
2119 Args.push_back(I.getInsertedValueOperand());
2120 for (auto &Op : I.indices())
2121 Args.push_back(B.getInt32(Op));
2122 Instruction *NewI =
2123 B.CreateIntrinsic(Intrinsic::spv_insertv, {Types}, {Args});
2124 replaceMemInstrUses(&I, NewI, B);
2125 return NewI;
2126}
2127
2128Instruction *SPIRVEmitIntrinsics::visitExtractValueInst(ExtractValueInst &I) {
2129 if (I.getAggregateOperand()->getType()->isAggregateType())
2130 return &I;
2131 IRBuilder<> B(I.getParent());
2132 B.SetInsertPoint(&I);
2133 SmallVector<Value *> Args(I.operands());
2134 for (auto &Op : I.indices())
2135 Args.push_back(B.getInt32(Op));
2136 auto *NewI =
2137 B.CreateIntrinsic(Intrinsic::spv_extractv, {I.getType()}, {Args});
2138 replaceAllUsesWithAndErase(B, &I, NewI);
2139 return NewI;
2140}
2141
2142Instruction *SPIRVEmitIntrinsics::visitLoadInst(LoadInst &I) {
2143 if (!I.getType()->isAggregateType())
2144 return &I;
2145 IRBuilder<> B(I.getParent());
2146 B.SetInsertPoint(&I);
2147 TrackConstants = false;
2148 const auto *TLI = TM->getSubtargetImpl()->getTargetLowering();
2150 TLI->getLoadMemOperandFlags(I, CurrF->getDataLayout());
2151 auto *NewI =
2152 B.CreateIntrinsic(Intrinsic::spv_load, {I.getOperand(0)->getType()},
2153 {I.getPointerOperand(), B.getInt16(Flags),
2154 B.getInt32(I.getAlign().value())});
2155 replaceMemInstrUses(&I, NewI, B);
2156 return NewI;
2157}
2158
2159Instruction *SPIRVEmitIntrinsics::visitStoreInst(StoreInst &I) {
2160 if (!AggrStores.contains(&I))
2161 return &I;
2162 IRBuilder<> B(I.getParent());
2163 B.SetInsertPoint(&I);
2164 TrackConstants = false;
2165 const auto *TLI = TM->getSubtargetImpl()->getTargetLowering();
2167 TLI->getStoreMemOperandFlags(I, CurrF->getDataLayout());
2168 auto *PtrOp = I.getPointerOperand();
2169
2170 if (I.getValueOperand()->getType()->isAggregateType()) {
2171 // It is possible that what used to be an ExtractValueInst has been replaced
2172 // with a call to the spv_extractv intrinsic, and that said call hasn't
2173 // had its return type replaced with i32 during the dedicated pass (because
2174 // it was emitted later); we have to handle this here, because IRTranslator
2175 // cannot deal with multi-register types at the moment.
2176 CallBase *CB = dyn_cast<CallBase>(I.getValueOperand());
2177 assert(CB && CB->getIntrinsicID() == Intrinsic::spv_extractv &&
2178 "Unexpected argument of aggregate type, should be spv_extractv!");
2179 CB->mutateType(B.getInt32Ty());
2180 }
2181
2182 auto *NewI = B.CreateIntrinsic(
2183 Intrinsic::spv_store, {I.getValueOperand()->getType(), PtrOp->getType()},
2184 {I.getValueOperand(), PtrOp, B.getInt16(Flags),
2185 B.getInt32(I.getAlign().value())});
2186 NewI->copyMetadata(I);
2187 I.eraseFromParent();
2188 return NewI;
2189}
2190
2191Instruction *SPIRVEmitIntrinsics::visitAllocaInst(AllocaInst &I) {
2192 Value *ArraySize = nullptr;
2193 if (I.isArrayAllocation()) {
2194 const SPIRVSubtarget *STI = TM->getSubtargetImpl(*I.getFunction());
2195 if (!STI->canUseExtension(
2196 SPIRV::Extension::SPV_INTEL_variable_length_array))
2198 "array allocation: this instruction requires the following "
2199 "SPIR-V extension: SPV_INTEL_variable_length_array",
2200 false);
2201 ArraySize = I.getArraySize();
2202 }
2203 IRBuilder<> B(I.getParent());
2204 B.SetInsertPoint(&I);
2205 TrackConstants = false;
2206 Type *PtrTy = I.getType();
2207 auto *NewI =
2208 ArraySize
2209 ? B.CreateIntrinsic(Intrinsic::spv_alloca_array,
2210 {PtrTy, ArraySize->getType()},
2211 {ArraySize, B.getInt32(I.getAlign().value())})
2212 : B.CreateIntrinsic(Intrinsic::spv_alloca, {PtrTy},
2213 {B.getInt32(I.getAlign().value())});
2214 replaceAllUsesWithAndErase(B, &I, NewI);
2215 return NewI;
2216}
2217
2218Instruction *SPIRVEmitIntrinsics::visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
2219 assert(I.getType()->isAggregateType() && "Aggregate result is expected");
2220 IRBuilder<> B(I.getParent());
2221 B.SetInsertPoint(&I);
2222 SmallVector<Value *> Args(I.operands());
2223 Args.push_back(B.getInt32(
2224 static_cast<uint32_t>(getMemScope(I.getContext(), I.getSyncScopeID()))));
2225 Args.push_back(B.getInt32(
2226 static_cast<uint32_t>(getMemSemantics(I.getSuccessOrdering()))));
2227 Args.push_back(B.getInt32(
2228 static_cast<uint32_t>(getMemSemantics(I.getFailureOrdering()))));
2229 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_cmpxchg,
2230 {I.getPointerOperand()->getType()}, {Args});
2231 replaceMemInstrUses(&I, NewI, B);
2232 return NewI;
2233}
2234
2235Instruction *SPIRVEmitIntrinsics::visitUnreachableInst(UnreachableInst &I) {
2236 IRBuilder<> B(I.getParent());
2237 B.SetInsertPoint(&I);
2238 B.CreateIntrinsic(Intrinsic::spv_unreachable, {});
2239 return &I;
2240}
2241
2242static bool
2243shouldEmitIntrinsicsForGlobalValue(const GlobalVariableUsers &GVUsers,
2244 const GlobalVariable &GV,
2245 const Function *F) {
2246 // Skip special artificial variables.
2247 static const StringSet<> ArtificialGlobals{"llvm.global.annotations",
2248 "llvm.compiler.used", "llvm.used"};
2249
2250 if (ArtificialGlobals.contains(GV.getName()))
2251 return false;
2252
2253 auto &UserFunctions = GVUsers.getTransitiveUserFunctions(GV);
2254 if (UserFunctions.contains(F))
2255 return true;
2256
2257 // Do not emit the intrinsics in this function, it's going to be emitted on
2258 // the functions that reference it.
2259 if (!UserFunctions.empty())
2260 return false;
2261
2262 // Emit definitions for globals that are not referenced by any function on the
2263 // first function definition.
2264 const Module &M = *F->getParent();
2265 const Function &FirstDefinition = *M.getFunctionDefs().begin();
2266 return F == &FirstDefinition;
2267}
2268
2269void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
2270 IRBuilder<> &B) {
2271
2272 if (!shouldEmitIntrinsicsForGlobalValue(GVUsers, GV, CurrF))
2273 return;
2274
2275 Constant *Init = nullptr;
2276 if (hasInitializer(&GV)) {
2277 // Deduce element type and store results in Global Registry.
2278 // Result is ignored, because TypedPointerType is not supported
2279 // by llvm IR general logic.
2280 deduceElementTypeHelper(&GV, false);
2281 Init = GV.getInitializer();
2282 Type *Ty = isAggrConstForceInt32(Init) ? B.getInt32Ty() : Init->getType();
2283 Constant *Const = isAggrConstForceInt32(Init) ? B.getInt32(1) : Init;
2284 auto *InitInst = B.CreateIntrinsic(Intrinsic::spv_init_global,
2285 {GV.getType(), Ty}, {&GV, Const});
2286 InitInst->setArgOperand(1, Init);
2287 }
2288 if (!Init && GV.use_empty())
2289 B.CreateIntrinsic(Intrinsic::spv_unref_global, GV.getType(), &GV);
2290}
2291
2292// Return true, if we can't decide what is the pointee type now and will get
2293// back to the question later. Return false is spv_assign_ptr_type is not needed
2294// or can be inserted immediately.
2295bool SPIRVEmitIntrinsics::insertAssignPtrTypeIntrs(Instruction *I,
2296 IRBuilder<> &B,
2297 bool UnknownElemTypeI8) {
2299 if (!isPointerTy(I->getType()) || !requireAssignType(I))
2300 return false;
2301
2303 if (Type *ElemTy = deduceElementType(I, UnknownElemTypeI8)) {
2304 GR->buildAssignPtr(B, ElemTy, I);
2305 return false;
2306 }
2307 return true;
2308}
2309
2310void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
2311 IRBuilder<> &B) {
2312 // TODO: extend the list of functions with known result types
2313 static StringMap<unsigned> ResTypeWellKnown = {
2314 {"async_work_group_copy", WellKnownTypes::Event},
2315 {"async_work_group_strided_copy", WellKnownTypes::Event},
2316 {"__spirv_GroupAsyncCopy", WellKnownTypes::Event}};
2317
2319
2320 bool IsKnown = false;
2321 if (auto *CI = dyn_cast<CallInst>(I)) {
2322 if (!CI->isIndirectCall() && !CI->isInlineAsm() &&
2323 CI->getCalledFunction() && !CI->getCalledFunction()->isIntrinsic()) {
2324 Function *CalledF = CI->getCalledFunction();
2325 std::string DemangledName =
2327 FPDecorationId DecorationId = FPDecorationId::NONE;
2328 if (DemangledName.length() > 0)
2329 DemangledName =
2330 SPIRV::lookupBuiltinNameHelper(DemangledName, &DecorationId);
2331 auto ResIt = ResTypeWellKnown.find(DemangledName);
2332 if (ResIt != ResTypeWellKnown.end()) {
2333 IsKnown = true;
2335 switch (ResIt->second) {
2336 case WellKnownTypes::Event:
2337 GR->buildAssignType(
2338 B, TargetExtType::get(I->getContext(), "spirv.Event"), I);
2339 break;
2340 }
2341 }
2342 // check if a floating rounding mode or saturation info is present
2343 switch (DecorationId) {
2344 default:
2345 break;
2346 case FPDecorationId::SAT:
2348 break;
2349 case FPDecorationId::RTE:
2351 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTE, B);
2352 break;
2353 case FPDecorationId::RTZ:
2355 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTZ, B);
2356 break;
2357 case FPDecorationId::RTP:
2359 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTP, B);
2360 break;
2361 case FPDecorationId::RTN:
2363 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTN, B);
2364 break;
2365 }
2366 }
2367 }
2368
2369 Type *Ty = I->getType();
2370 if (!IsKnown && !Ty->isVoidTy() && !isPointerTy(Ty) && requireAssignType(I)) {
2372 Type *TypeToAssign = Ty;
2373 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2374 if (II->getIntrinsicID() == Intrinsic::spv_const_composite ||
2375 II->getIntrinsicID() == Intrinsic::spv_undef) {
2376 auto It = AggrConstTypes.find(II);
2377 if (It == AggrConstTypes.end())
2378 report_fatal_error("Unknown composite intrinsic type");
2379 TypeToAssign = It->second;
2380 }
2381 }
2382 TypeToAssign = restoreMutatedType(GR, I, TypeToAssign);
2383 GR->buildAssignType(B, TypeToAssign, I);
2384 }
2385 for (const auto &Op : I->operands()) {
2387 // Check GetElementPtrConstantExpr case.
2389 (isa<GEPOperator>(Op) ||
2390 (cast<ConstantExpr>(Op)->getOpcode() == CastInst::IntToPtr)))) {
2392 Type *OpTy = Op->getType();
2393 if (isa<UndefValue>(Op) && OpTy->isAggregateType()) {
2394 CallInst *AssignCI =
2395 buildIntrWithMD(Intrinsic::spv_assign_type, {B.getInt32Ty()}, Op,
2396 UndefValue::get(B.getInt32Ty()), {}, B);
2397 GR->addAssignPtrTypeInstr(Op, AssignCI);
2398 } else if (!isa<Instruction>(Op)) {
2399 Type *OpTy = Op->getType();
2400 Type *OpTyElem = getPointeeType(OpTy);
2401 if (OpTyElem) {
2402 GR->buildAssignPtr(B, OpTyElem, Op);
2403 } else if (isPointerTy(OpTy)) {
2404 Type *ElemTy = GR->findDeducedElementType(Op);
2405 GR->buildAssignPtr(B, ElemTy ? ElemTy : deduceElementType(Op, true),
2406 Op);
2407 } else {
2408 Value *OpTyVal = Op;
2409 if (OpTy->isTargetExtTy()) {
2410 // We need to do this in order to be consistent with how target ext
2411 // types are handled in `processInstrAfterVisit`
2412 OpTyVal = getNormalizedPoisonValue(OpTy);
2413 }
2414 CallInst *AssignCI =
2415 buildIntrWithMD(Intrinsic::spv_assign_type, {OpTy},
2416 getNormalizedPoisonValue(OpTy), OpTyVal, {}, B);
2417 GR->addAssignPtrTypeInstr(OpTyVal, AssignCI);
2418 }
2419 }
2420 }
2421 }
2422}
2423
2424bool SPIRVEmitIntrinsics::shouldTryToAddMemAliasingDecoration(
2425 Instruction *Inst) {
2426 const SPIRVSubtarget *STI = TM->getSubtargetImpl(*Inst->getFunction());
2427 if (!STI->canUseExtension(SPIRV::Extension::SPV_INTEL_memory_access_aliasing))
2428 return false;
2429 // Add aliasing decorations to internal load and store intrinsics
2430 // and atomic instructions, skipping atomic store as it won't have ID to
2431 // attach the decoration.
2432 CallInst *CI = dyn_cast<CallInst>(Inst);
2433 if (!CI)
2434 return false;
2435 if (Function *Fun = CI->getCalledFunction()) {
2436 if (Fun->isIntrinsic()) {
2437 switch (Fun->getIntrinsicID()) {
2438 case Intrinsic::spv_load:
2439 case Intrinsic::spv_store:
2440 return true;
2441 default:
2442 return false;
2443 }
2444 }
2446 const std::string Prefix = "__spirv_Atomic";
2447 const bool IsAtomic = Name.find(Prefix) == 0;
2448
2449 if (!Fun->getReturnType()->isVoidTy() && IsAtomic)
2450 return true;
2451 }
2452 return false;
2453}
2454
2455void SPIRVEmitIntrinsics::insertSpirvDecorations(Instruction *I,
2456 IRBuilder<> &B) {
2457 if (MDNode *MD = I->getMetadata("spirv.Decorations")) {
2459 B.CreateIntrinsic(Intrinsic::spv_assign_decoration, {I->getType()},
2460 {I, MetadataAsValue::get(I->getContext(), MD)});
2461 }
2462 // Lower alias.scope/noalias metadata
2463 {
2464 auto processMemAliasingDecoration = [&](unsigned Kind) {
2465 if (MDNode *AliasListMD = I->getMetadata(Kind)) {
2466 if (shouldTryToAddMemAliasingDecoration(I)) {
2467 uint32_t Dec = Kind == LLVMContext::MD_alias_scope
2468 ? SPIRV::Decoration::AliasScopeINTEL
2469 : SPIRV::Decoration::NoAliasINTEL;
2471 I, ConstantInt::get(B.getInt32Ty(), Dec),
2472 MetadataAsValue::get(I->getContext(), AliasListMD)};
2474 B.CreateIntrinsic(Intrinsic::spv_assign_aliasing_decoration,
2475 {I->getType()}, {Args});
2476 }
2477 }
2478 };
2479 processMemAliasingDecoration(LLVMContext::MD_alias_scope);
2480 processMemAliasingDecoration(LLVMContext::MD_noalias);
2481 }
2482 // MD_fpmath
2483 if (MDNode *MD = I->getMetadata(LLVMContext::MD_fpmath)) {
2484 const SPIRVSubtarget *STI = TM->getSubtargetImpl(*I->getFunction());
2485 bool AllowFPMaxError =
2486 STI->canUseExtension(SPIRV::Extension::SPV_INTEL_fp_max_error);
2487 if (!AllowFPMaxError)
2488 return;
2489
2491 B.CreateIntrinsic(Intrinsic::spv_assign_fpmaxerror_decoration,
2492 {I->getType()},
2493 {I, MetadataAsValue::get(I->getContext(), MD)});
2494 }
2495}
2496
2498 const Module &M,
2500 &FPFastMathDefaultInfoMap,
2501 Function *F) {
2502 auto it = FPFastMathDefaultInfoMap.find(F);
2503 if (it != FPFastMathDefaultInfoMap.end())
2504 return it->second;
2505
2506 // If the map does not contain the entry, create a new one. Initialize it to
2507 // contain all 3 elements sorted by bit width of target type: {half, float,
2508 // double}.
2509 SPIRV::FPFastMathDefaultInfoVector FPFastMathDefaultInfoVec;
2510 FPFastMathDefaultInfoVec.emplace_back(Type::getHalfTy(M.getContext()),
2511 SPIRV::FPFastMathMode::None);
2512 FPFastMathDefaultInfoVec.emplace_back(Type::getFloatTy(M.getContext()),
2513 SPIRV::FPFastMathMode::None);
2514 FPFastMathDefaultInfoVec.emplace_back(Type::getDoubleTy(M.getContext()),
2515 SPIRV::FPFastMathMode::None);
2516 return FPFastMathDefaultInfoMap[F] = std::move(FPFastMathDefaultInfoVec);
2517}
2518
2520 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec,
2521 const Type *Ty) {
2522 size_t BitWidth = Ty->getScalarSizeInBits();
2523 int Index =
2525 BitWidth);
2526 assert(Index >= 0 && Index < 3 &&
2527 "Expected FPFastMathDefaultInfo for half, float, or double");
2528 assert(FPFastMathDefaultInfoVec.size() == 3 &&
2529 "Expected FPFastMathDefaultInfoVec to have exactly 3 elements");
2530 return FPFastMathDefaultInfoVec[Index];
2531}
2532
2533void SPIRVEmitIntrinsics::insertConstantsForFPFastMathDefault(Module &M) {
2534 const SPIRVSubtarget *ST = TM->getSubtargetImpl();
2535 if (!ST->canUseExtension(SPIRV::Extension::SPV_KHR_float_controls2))
2536 return;
2537
2538 // Store the FPFastMathDefaultInfo in the FPFastMathDefaultInfoMap.
2539 // We need the entry point (function) as the key, and the target
2540 // type and flags as the value.
2541 // We also need to check ContractionOff and SignedZeroInfNanPreserve
2542 // execution modes, as they are now deprecated and must be replaced
2543 // with FPFastMathDefaultInfo.
2544 auto Node = M.getNamedMetadata("spirv.ExecutionMode");
2545 if (!Node) {
2546 if (!M.getNamedMetadata("opencl.enable.FP_CONTRACT")) {
2547 // This requires emitting ContractionOff. However, because
2548 // ContractionOff is now deprecated, we need to replace it with
2549 // FPFastMathDefaultInfo with FP Fast Math Mode bitmask set to all 0.
2550 // We need to create the constant for that.
2551
2552 // Create constant instruction with the bitmask flags.
2553 Constant *InitValue =
2554 ConstantInt::get(Type::getInt32Ty(M.getContext()), 0);
2555 // TODO: Reuse constant if there is one already with the required
2556 // value.
2557 [[maybe_unused]] GlobalVariable *GV =
2558 new GlobalVariable(M, // Module
2559 Type::getInt32Ty(M.getContext()), // Type
2560 true, // isConstant
2562 InitValue // Initializer
2563 );
2564 }
2565 return;
2566 }
2567
2568 // The table maps function pointers to their default FP fast math info. It
2569 // can be assumed that the SmallVector is sorted by the bit width of the
2570 // type. The first element is the smallest bit width, and the last element
2571 // is the largest bit width, therefore, we will have {half, float, double}
2572 // in the order of their bit widths.
2573 DenseMap<Function *, SPIRV::FPFastMathDefaultInfoVector>
2574 FPFastMathDefaultInfoMap;
2575
2576 for (unsigned i = 0; i < Node->getNumOperands(); i++) {
2577 MDNode *MDN = cast<MDNode>(Node->getOperand(i));
2578 assert(MDN->getNumOperands() >= 2 && "Expected at least 2 operands");
2580 cast<ConstantAsMetadata>(MDN->getOperand(0))->getValue());
2581 const auto EM =
2583 cast<ConstantAsMetadata>(MDN->getOperand(1))->getValue())
2584 ->getZExtValue();
2585 if (EM == SPIRV::ExecutionMode::FPFastMathDefault) {
2586 assert(MDN->getNumOperands() == 4 &&
2587 "Expected 4 operands for FPFastMathDefault");
2588 const Type *T = cast<ValueAsMetadata>(MDN->getOperand(2))->getType();
2589 unsigned Flags =
2591 cast<ConstantAsMetadata>(MDN->getOperand(3))->getValue())
2592 ->getZExtValue();
2593 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
2594 getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
2595 SPIRV::FPFastMathDefaultInfo &Info =
2596 getFPFastMathDefaultInfo(FPFastMathDefaultInfoVec, T);
2597 Info.FastMathFlags = Flags;
2598 Info.FPFastMathDefault = true;
2599 } else if (EM == SPIRV::ExecutionMode::ContractionOff) {
2600 assert(MDN->getNumOperands() == 2 &&
2601 "Expected no operands for ContractionOff");
2602
2603 // We need to save this info for every possible FP type, i.e. {half,
2604 // float, double, fp128}.
2605 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
2606 getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
2607 for (SPIRV::FPFastMathDefaultInfo &Info : FPFastMathDefaultInfoVec) {
2608 Info.ContractionOff = true;
2609 }
2610 } else if (EM == SPIRV::ExecutionMode::SignedZeroInfNanPreserve) {
2611 assert(MDN->getNumOperands() == 3 &&
2612 "Expected 1 operand for SignedZeroInfNanPreserve");
2613 unsigned TargetWidth =
2615 cast<ConstantAsMetadata>(MDN->getOperand(2))->getValue())
2616 ->getZExtValue();
2617 // We need to save this info only for the FP type with TargetWidth.
2618 SPIRV::FPFastMathDefaultInfoVector &FPFastMathDefaultInfoVec =
2619 getOrCreateFPFastMathDefaultInfoVec(M, FPFastMathDefaultInfoMap, F);
2622 assert(Index >= 0 && Index < 3 &&
2623 "Expected FPFastMathDefaultInfo for half, float, or double");
2624 assert(FPFastMathDefaultInfoVec.size() == 3 &&
2625 "Expected FPFastMathDefaultInfoVec to have exactly 3 elements");
2626 FPFastMathDefaultInfoVec[Index].SignedZeroInfNanPreserve = true;
2627 }
2628 }
2629
2630 std::unordered_map<unsigned, GlobalVariable *> GlobalVars;
2631 for (auto &[Func, FPFastMathDefaultInfoVec] : FPFastMathDefaultInfoMap) {
2632 if (FPFastMathDefaultInfoVec.empty())
2633 continue;
2634
2635 for (const SPIRV::FPFastMathDefaultInfo &Info : FPFastMathDefaultInfoVec) {
2636 assert(Info.Ty && "Expected target type for FPFastMathDefaultInfo");
2637 // Skip if none of the execution modes was used.
2638 unsigned Flags = Info.FastMathFlags;
2639 if (Flags == SPIRV::FPFastMathMode::None && !Info.ContractionOff &&
2640 !Info.SignedZeroInfNanPreserve && !Info.FPFastMathDefault)
2641 continue;
2642
2643 // Check if flags are compatible.
2644 if (Info.ContractionOff && (Flags & SPIRV::FPFastMathMode::AllowContract))
2645 report_fatal_error("Conflicting FPFastMathFlags: ContractionOff "
2646 "and AllowContract");
2647
2648 if (Info.SignedZeroInfNanPreserve &&
2649 !(Flags &
2650 (SPIRV::FPFastMathMode::NotNaN | SPIRV::FPFastMathMode::NotInf |
2651 SPIRV::FPFastMathMode::NSZ))) {
2652 if (Info.FPFastMathDefault)
2653 report_fatal_error("Conflicting FPFastMathFlags: "
2654 "SignedZeroInfNanPreserve but at least one of "
2655 "NotNaN/NotInf/NSZ is enabled.");
2656 }
2657
2658 if ((Flags & SPIRV::FPFastMathMode::AllowTransform) &&
2659 !((Flags & SPIRV::FPFastMathMode::AllowReassoc) &&
2660 (Flags & SPIRV::FPFastMathMode::AllowContract))) {
2661 report_fatal_error("Conflicting FPFastMathFlags: "
2662 "AllowTransform requires AllowReassoc and "
2663 "AllowContract to be set.");
2664 }
2665
2666 auto it = GlobalVars.find(Flags);
2667 GlobalVariable *GV = nullptr;
2668 if (it != GlobalVars.end()) {
2669 // Reuse existing global variable.
2670 GV = it->second;
2671 } else {
2672 // Create constant instruction with the bitmask flags.
2673 Constant *InitValue =
2674 ConstantInt::get(Type::getInt32Ty(M.getContext()), Flags);
2675 // TODO: Reuse constant if there is one already with the required
2676 // value.
2677 GV = new GlobalVariable(M, // Module
2678 Type::getInt32Ty(M.getContext()), // Type
2679 true, // isConstant
2681 InitValue // Initializer
2682 );
2683 GlobalVars[Flags] = GV;
2684 }
2685 }
2686 }
2687}
2688
2689void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
2690 IRBuilder<> &B) {
2691 auto *II = dyn_cast<IntrinsicInst>(I);
2692 bool IsConstComposite =
2693 II && II->getIntrinsicID() == Intrinsic::spv_const_composite;
2694 if (IsConstComposite && TrackConstants) {
2696 auto t = AggrConsts.find(I);
2697 assert(t != AggrConsts.end());
2698 auto *NewOp =
2699 buildIntrWithMD(Intrinsic::spv_track_constant,
2700 {II->getType(), II->getType()}, t->second, I, {}, B);
2701 replaceAllUsesWith(I, NewOp, false);
2702 NewOp->setArgOperand(0, I);
2703 }
2704 bool IsPhi = isa<PHINode>(I), BPrepared = false;
2705 for (const auto &Op : I->operands()) {
2706 if (isa<PHINode>(I) || isa<SwitchInst>(I) ||
2708 continue;
2709 unsigned OpNo = Op.getOperandNo();
2710 if (II && ((II->getIntrinsicID() == Intrinsic::spv_gep && OpNo == 0) ||
2711 (II->paramHasAttr(OpNo, Attribute::ImmArg))))
2712 continue;
2713
2714 if (!BPrepared) {
2715 IsPhi ? B.SetInsertPointPastAllocas(I->getParent()->getParent())
2716 : B.SetInsertPoint(I);
2717 BPrepared = true;
2718 }
2719 Type *OpTy = Op->getType();
2720 Type *OpElemTy = GR->findDeducedElementType(Op);
2721 Value *NewOp = Op;
2722 if (OpTy->isTargetExtTy()) {
2723 // Since this value is replaced by poison, we need to do the same in
2724 // `insertAssignTypeIntrs`.
2725 Value *OpTyVal = getNormalizedPoisonValue(OpTy);
2726 NewOp = buildIntrWithMD(Intrinsic::spv_track_constant,
2727 {OpTy, OpTyVal->getType()}, Op, OpTyVal, {}, B);
2728 }
2729 if (!IsConstComposite && isPointerTy(OpTy) && OpElemTy != nullptr &&
2730 OpElemTy != IntegerType::getInt8Ty(I->getContext())) {
2731 SmallVector<Type *, 2> Types = {OpTy, OpTy};
2732 SmallVector<Value *, 2> Args = {
2733 NewOp, buildMD(getNormalizedPoisonValue(OpElemTy)),
2734 B.getInt32(getPointerAddressSpace(OpTy))};
2735 CallInst *PtrCasted =
2736 B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
2737 GR->buildAssignPtr(B, OpElemTy, PtrCasted);
2738 NewOp = PtrCasted;
2739 }
2740 if (NewOp != Op)
2741 I->setOperand(OpNo, NewOp);
2742 }
2743 if (Named.insert(I).second)
2744 emitAssignName(I, B);
2745}
2746
2747Type *SPIRVEmitIntrinsics::deduceFunParamElementType(Function *F,
2748 unsigned OpIdx) {
2749 std::unordered_set<Function *> FVisited;
2750 return deduceFunParamElementType(F, OpIdx, FVisited);
2751}
2752
2753Type *SPIRVEmitIntrinsics::deduceFunParamElementType(
2754 Function *F, unsigned OpIdx, std::unordered_set<Function *> &FVisited) {
2755 // maybe a cycle
2756 if (!FVisited.insert(F).second)
2757 return nullptr;
2758
2759 std::unordered_set<Value *> Visited;
2761 // search in function's call sites
2762 for (User *U : F->users()) {
2763 CallInst *CI = dyn_cast<CallInst>(U);
2764 if (!CI || OpIdx >= CI->arg_size())
2765 continue;
2766 Value *OpArg = CI->getArgOperand(OpIdx);
2767 if (!isPointerTy(OpArg->getType()))
2768 continue;
2769 // maybe we already know operand's element type
2770 if (Type *KnownTy = GR->findDeducedElementType(OpArg))
2771 return KnownTy;
2772 // try to deduce from the operand itself
2773 Visited.clear();
2774 if (Type *Ty = deduceElementTypeHelper(OpArg, Visited, false))
2775 return Ty;
2776 // search in actual parameter's users
2777 for (User *OpU : OpArg->users()) {
2779 if (!Inst || Inst == CI)
2780 continue;
2781 Visited.clear();
2782 if (Type *Ty = deduceElementTypeHelper(Inst, Visited, false))
2783 return Ty;
2784 }
2785 // check if it's a formal parameter of the outer function
2786 if (!CI->getParent() || !CI->getParent()->getParent())
2787 continue;
2788 Function *OuterF = CI->getParent()->getParent();
2789 if (FVisited.find(OuterF) != FVisited.end())
2790 continue;
2791 for (unsigned i = 0; i < OuterF->arg_size(); ++i) {
2792 if (OuterF->getArg(i) == OpArg) {
2793 Lookup.push_back(std::make_pair(OuterF, i));
2794 break;
2795 }
2796 }
2797 }
2798
2799 // search in function parameters
2800 for (auto &Pair : Lookup) {
2801 if (Type *Ty = deduceFunParamElementType(Pair.first, Pair.second, FVisited))
2802 return Ty;
2803 }
2804
2805 return nullptr;
2806}
2807
2808void SPIRVEmitIntrinsics::processParamTypesByFunHeader(Function *F,
2809 IRBuilder<> &B) {
2810 B.SetInsertPointPastAllocas(F);
2811 for (unsigned OpIdx = 0; OpIdx < F->arg_size(); ++OpIdx) {
2812 Argument *Arg = F->getArg(OpIdx);
2813 if (!isUntypedPointerTy(Arg->getType()))
2814 continue;
2815 Type *ElemTy = GR->findDeducedElementType(Arg);
2816 if (ElemTy)
2817 continue;
2818 if (hasPointeeTypeAttr(Arg) &&
2819 (ElemTy = getPointeeTypeByAttr(Arg)) != nullptr) {
2820 GR->buildAssignPtr(B, ElemTy, Arg);
2821 continue;
2822 }
2823 // search in function's call sites
2824 for (User *U : F->users()) {
2825 CallInst *CI = dyn_cast<CallInst>(U);
2826 if (!CI || OpIdx >= CI->arg_size())
2827 continue;
2828 Value *OpArg = CI->getArgOperand(OpIdx);
2829 if (!isPointerTy(OpArg->getType()))
2830 continue;
2831 // maybe we already know operand's element type
2832 if ((ElemTy = GR->findDeducedElementType(OpArg)) != nullptr)
2833 break;
2834 }
2835 if (ElemTy) {
2836 GR->buildAssignPtr(B, ElemTy, Arg);
2837 continue;
2838 }
2839 if (HaveFunPtrs) {
2840 for (User *U : Arg->users()) {
2841 CallInst *CI = dyn_cast<CallInst>(U);
2842 if (CI && !isa<IntrinsicInst>(CI) && CI->isIndirectCall() &&
2843 CI->getCalledOperand() == Arg &&
2844 CI->getParent()->getParent() == CurrF) {
2846 deduceOperandElementTypeFunctionPointer(CI, Ops, ElemTy, false);
2847 if (ElemTy) {
2848 GR->buildAssignPtr(B, ElemTy, Arg);
2849 break;
2850 }
2851 }
2852 }
2853 }
2854 }
2855}
2856
2857void SPIRVEmitIntrinsics::processParamTypes(Function *F, IRBuilder<> &B) {
2858 B.SetInsertPointPastAllocas(F);
2859 for (unsigned OpIdx = 0; OpIdx < F->arg_size(); ++OpIdx) {
2860 Argument *Arg = F->getArg(OpIdx);
2861 if (!isUntypedPointerTy(Arg->getType()))
2862 continue;
2863 Type *ElemTy = GR->findDeducedElementType(Arg);
2864 if (!ElemTy && (ElemTy = deduceFunParamElementType(F, OpIdx)) != nullptr) {
2865 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(Arg)) {
2866 DenseSet<std::pair<Value *, Value *>> VisitedSubst;
2867 GR->updateAssignType(AssignCI, Arg, getNormalizedPoisonValue(ElemTy));
2868 propagateElemType(Arg, IntegerType::getInt8Ty(F->getContext()),
2869 VisitedSubst);
2870 } else {
2871 GR->buildAssignPtr(B, ElemTy, Arg);
2872 }
2873 }
2874 }
2875}
2876
2878 SPIRVGlobalRegistry *GR) {
2879 FunctionType *FTy = F->getFunctionType();
2880 bool IsNewFTy = false;
2882 for (Argument &Arg : F->args()) {
2883 Type *ArgTy = Arg.getType();
2884 if (ArgTy->isPointerTy())
2885 if (Type *ElemTy = GR->findDeducedElementType(&Arg)) {
2886 IsNewFTy = true;
2887 ArgTy = getTypedPointerWrapper(ElemTy, getPointerAddressSpace(ArgTy));
2888 }
2889 ArgTys.push_back(ArgTy);
2890 }
2891 return IsNewFTy
2892 ? FunctionType::get(FTy->getReturnType(), ArgTys, FTy->isVarArg())
2893 : FTy;
2894}
2895
2896bool SPIRVEmitIntrinsics::processFunctionPointers(Module &M) {
2897 SmallVector<Function *> Worklist;
2898 for (auto &F : M) {
2899 if (F.isIntrinsic())
2900 continue;
2901 if (F.isDeclaration()) {
2902 for (User *U : F.users()) {
2903 CallInst *CI = dyn_cast<CallInst>(U);
2904 if (!CI || CI->getCalledFunction() != &F) {
2905 Worklist.push_back(&F);
2906 break;
2907 }
2908 }
2909 } else {
2910 if (F.user_empty())
2911 continue;
2912 Type *FPElemTy = GR->findDeducedElementType(&F);
2913 if (!FPElemTy)
2914 FPElemTy = getFunctionPointerElemType(&F, GR);
2915 for (User *U : F.users()) {
2916 IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
2917 if (!II || II->arg_size() != 3 || II->getOperand(0) != &F)
2918 continue;
2919 if (II->getIntrinsicID() == Intrinsic::spv_assign_ptr_type ||
2920 II->getIntrinsicID() == Intrinsic::spv_ptrcast) {
2922 break;
2923 }
2924 }
2925 }
2926 }
2927 if (Worklist.empty())
2928 return false;
2929
2930 LLVMContext &Ctx = M.getContext();
2932 BasicBlock *BB = BasicBlock::Create(Ctx, "entry", SF);
2933 IRBuilder<> IRB(BB);
2934
2935 for (Function *F : Worklist) {
2937 for (const auto &Arg : F->args())
2938 Args.push_back(getNormalizedPoisonValue(Arg.getType()));
2939 IRB.CreateCall(F, Args);
2940 }
2941 IRB.CreateRetVoid();
2942
2943 return true;
2944}
2945
2946// Apply types parsed from demangled function declarations.
2947void SPIRVEmitIntrinsics::applyDemangledPtrArgTypes(IRBuilder<> &B) {
2948 DenseMap<Function *, CallInst *> Ptrcasts;
2949 for (auto It : FDeclPtrTys) {
2950 Function *F = It.first;
2951 for (auto *U : F->users()) {
2952 CallInst *CI = dyn_cast<CallInst>(U);
2953 if (!CI || CI->getCalledFunction() != F)
2954 continue;
2955 unsigned Sz = CI->arg_size();
2956 for (auto [Idx, ElemTy] : It.second) {
2957 if (Idx >= Sz)
2958 continue;
2959 Value *Param = CI->getArgOperand(Idx);
2960 if (GR->findDeducedElementType(Param) || isa<GlobalValue>(Param))
2961 continue;
2962 if (Argument *Arg = dyn_cast<Argument>(Param)) {
2963 if (!hasPointeeTypeAttr(Arg)) {
2964 B.SetInsertPointPastAllocas(Arg->getParent());
2965 B.SetCurrentDebugLocation(DebugLoc());
2966 GR->buildAssignPtr(B, ElemTy, Arg);
2967 }
2968 } else if (isaGEP(Param)) {
2969 replaceUsesOfWithSpvPtrcast(Param, normalizeType(ElemTy), CI,
2970 Ptrcasts);
2971 } else if (isa<Instruction>(Param)) {
2972 GR->addDeducedElementType(Param, normalizeType(ElemTy));
2973 // insertAssignTypeIntrs() will complete buildAssignPtr()
2974 } else {
2975 B.SetInsertPoint(CI->getParent()
2976 ->getParent()
2977 ->getEntryBlock()
2978 .getFirstNonPHIOrDbgOrAlloca());
2979 GR->buildAssignPtr(B, ElemTy, Param);
2980 }
2981 CallInst *Ref = dyn_cast<CallInst>(Param);
2982 if (!Ref)
2983 continue;
2984 Function *RefF = Ref->getCalledFunction();
2985 if (!RefF || !isPointerTy(RefF->getReturnType()) ||
2986 GR->findDeducedElementType(RefF))
2987 continue;
2988 ElemTy = normalizeType(ElemTy);
2989 GR->addDeducedElementType(RefF, ElemTy);
2990 GR->addReturnType(
2992 ElemTy, getPointerAddressSpace(RefF->getReturnType())));
2993 }
2994 }
2995 }
2996}
2997
2998GetElementPtrInst *
2999SPIRVEmitIntrinsics::simplifyZeroLengthArrayGepInst(GetElementPtrInst *GEP) {
3000 // getelementptr [0 x T], P, 0 (zero), I -> getelementptr T, P, I.
3001 // If type is 0-length array and first index is 0 (zero), drop both the
3002 // 0-length array type and the first index. This is a common pattern in
3003 // the IR, e.g. when using a zero-length array as a placeholder for a
3004 // flexible array such as unbound arrays.
3005 assert(GEP && "GEP is null");
3006 Type *SrcTy = GEP->getSourceElementType();
3007 SmallVector<Value *, 8> Indices(GEP->indices());
3008 ArrayType *ArrTy = dyn_cast<ArrayType>(SrcTy);
3009 if (ArrTy && ArrTy->getNumElements() == 0 &&
3011 Indices.erase(Indices.begin());
3012 SrcTy = ArrTy->getElementType();
3013 return GetElementPtrInst::Create(SrcTy, GEP->getPointerOperand(), Indices,
3014 GEP->getNoWrapFlags(), "",
3015 GEP->getIterator());
3016 }
3017 return nullptr;
3018}
3019
3020void SPIRVEmitIntrinsics::emitUnstructuredLoopControls(Function &F,
3021 IRBuilder<> &B) {
3022 const SPIRVSubtarget *ST = TM->getSubtargetImpl(F);
3023 // Shaders use SPIRVStructurizer which emits OpLoopMerge via spv_loop_merge.
3024 if (ST->isShader())
3025 return;
3026 if (!ST->canUseExtension(
3027 SPIRV::Extension::SPV_INTEL_unstructured_loop_controls))
3028 return;
3029
3030 for (BasicBlock &BB : F) {
3032 MDNode *LoopMD = Term->getMetadata(LLVMContext::MD_loop);
3033 if (!LoopMD)
3034 continue;
3035
3038 unsigned LC = Ops[0];
3039 if (LC == SPIRV::LoopControl::None)
3040 continue;
3041
3042 // Emit intrinsic: loop control mask + optional parameters.
3043 B.SetInsertPoint(Term);
3044 SmallVector<Value *, 4> IntrArgs;
3045 IntrArgs.push_back(B.getInt32(LC));
3046 for (unsigned I = 1; I < Ops.size(); ++I)
3047 IntrArgs.push_back(B.getInt32(Ops[I]));
3048 B.CreateIntrinsic(Intrinsic::spv_loop_control_intel, IntrArgs);
3049 }
3050}
3051
3052bool SPIRVEmitIntrinsics::runOnFunction(Function &Func) {
3053 if (Func.isDeclaration())
3054 return false;
3055
3056 const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(Func);
3057 GR = ST.getSPIRVGlobalRegistry();
3058
3059 if (!CurrF)
3060 HaveFunPtrs =
3061 ST.canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers);
3062
3063 CurrF = &Func;
3064 IRBuilder<> B(Func.getContext());
3065 AggrConsts.clear();
3066 AggrConstTypes.clear();
3067 AggrStores.clear();
3068
3069 // Fix GEP result types ahead of inference, and simplify if possible.
3070 // Data structure for dead instructions that were simplified and replaced.
3071 SmallPtrSet<Instruction *, 4> DeadInsts;
3072 for (auto &I : instructions(Func)) {
3074 auto *SGEP = dyn_cast<StructuredGEPInst>(&I);
3075
3076 if ((!GEP && !SGEP) || GR->findDeducedElementType(&I))
3077 continue;
3078
3079 if (SGEP) {
3080 GR->addDeducedElementType(SGEP,
3081 normalizeType(SGEP->getResultElementType()));
3082 continue;
3083 }
3084
3085 GetElementPtrInst *NewGEP = simplifyZeroLengthArrayGepInst(GEP);
3086 if (NewGEP) {
3087 GEP->replaceAllUsesWith(NewGEP);
3088 DeadInsts.insert(GEP);
3089 GEP = NewGEP;
3090 }
3091 if (Type *GepTy = getGEPType(GEP))
3092 GR->addDeducedElementType(GEP, normalizeType(GepTy));
3093 }
3094 // Remove dead instructions that were simplified and replaced.
3095 for (auto *I : DeadInsts) {
3096 assert(I->use_empty() && "Dead instruction should not have any uses left");
3097 I->eraseFromParent();
3098 }
3099
3100 processParamTypesByFunHeader(CurrF, B);
3101
3102 // StoreInst's operand type can be changed during the next
3103 // transformations, so we need to store it in the set. Also store already
3104 // transformed types.
3105 for (auto &I : instructions(Func)) {
3106 StoreInst *SI = dyn_cast<StoreInst>(&I);
3107 if (!SI)
3108 continue;
3109 Type *ElTy = SI->getValueOperand()->getType();
3110 if (ElTy->isAggregateType() || ElTy->isVectorTy())
3111 AggrStores.insert(&I);
3112 }
3113
3114 B.SetInsertPoint(&Func.getEntryBlock(), Func.getEntryBlock().begin());
3115 for (auto &GV : Func.getParent()->globals())
3116 processGlobalValue(GV, B);
3117
3118 preprocessUndefs(B);
3119 preprocessCompositeConstants(B);
3122
3123 applyDemangledPtrArgTypes(B);
3124
3125 // Pass forward: use operand to deduce instructions result.
3126 for (auto &I : Worklist) {
3127 // Don't emit intrinsincs for convergence intrinsics.
3128 if (isConvergenceIntrinsic(I))
3129 continue;
3130
3131 bool Postpone = insertAssignPtrTypeIntrs(I, B, false);
3132 // if Postpone is true, we can't decide on pointee type yet
3133 insertAssignTypeIntrs(I, B);
3134 insertPtrCastOrAssignTypeInstr(I, B);
3136 // if instruction requires a pointee type set, let's check if we know it
3137 // already, and force it to be i8 if not
3138 if (Postpone && !GR->findAssignPtrTypeInstr(I))
3139 insertAssignPtrTypeIntrs(I, B, true);
3140
3141 if (auto *FPI = dyn_cast<ConstrainedFPIntrinsic>(I))
3142 useRoundingMode(FPI, B);
3143 }
3144
3145 // Pass backward: use instructions results to specify/update/cast operands
3146 // where needed.
3147 SmallPtrSet<Instruction *, 4> IncompleteRets;
3148 for (auto &I : llvm::reverse(instructions(Func)))
3149 deduceOperandElementType(&I, &IncompleteRets);
3150
3151 // Pass forward for PHIs only, their operands are not preceed the
3152 // instruction in meaning of `instructions(Func)`.
3153 for (BasicBlock &BB : Func)
3154 for (PHINode &Phi : BB.phis())
3155 if (isPointerTy(Phi.getType()))
3156 deduceOperandElementType(&Phi, nullptr);
3157
3158 for (auto *I : Worklist) {
3159 TrackConstants = true;
3160 if (!I->getType()->isVoidTy() || isa<StoreInst>(I))
3162 // Visitors return either the original/newly created instruction for
3163 // further processing, nullptr otherwise.
3164 I = visit(*I);
3165 if (!I)
3166 continue;
3167
3168 // Don't emit intrinsics for convergence operations.
3169 if (isConvergenceIntrinsic(I))
3170 continue;
3171
3173 processInstrAfterVisit(I, B);
3174 }
3175
3176 emitUnstructuredLoopControls(Func, B);
3177
3178 return true;
3179}
3180
3181// Try to deduce a better type for pointers to untyped ptr.
3182bool SPIRVEmitIntrinsics::postprocessTypes(Module &M) {
3183 if (!GR || TodoTypeSz == 0)
3184 return false;
3185
3186 unsigned SzTodo = TodoTypeSz;
3187 DenseMap<Value *, SmallPtrSet<Value *, 4>> ToProcess;
3188 for (auto [Op, Enabled] : TodoType) {
3189 // TODO: add isa<CallInst>(Op) to continue
3190 if (!Enabled || isaGEP(Op))
3191 continue;
3192 CallInst *AssignCI = GR->findAssignPtrTypeInstr(Op);
3193 Type *KnownTy = GR->findDeducedElementType(Op);
3194 if (!KnownTy || !AssignCI)
3195 continue;
3196 assert(Op == AssignCI->getArgOperand(0));
3197 // Try to improve the type deduced after all Functions are processed.
3198 if (auto *CI = dyn_cast<Instruction>(Op)) {
3199 CurrF = CI->getParent()->getParent();
3200 std::unordered_set<Value *> Visited;
3201 if (Type *ElemTy = deduceElementTypeHelper(Op, Visited, false, true)) {
3202 if (ElemTy != KnownTy) {
3203 DenseSet<std::pair<Value *, Value *>> VisitedSubst;
3204 propagateElemType(CI, ElemTy, VisitedSubst);
3205 eraseTodoType(Op);
3206 continue;
3207 }
3208 }
3209 }
3210
3211 if (Op->hasUseList()) {
3212 for (User *U : Op->users()) {
3214 if (Inst && !isa<IntrinsicInst>(Inst))
3215 ToProcess[Inst].insert(Op);
3216 }
3217 }
3218 }
3219 if (TodoTypeSz == 0)
3220 return true;
3221
3222 for (auto &F : M) {
3223 CurrF = &F;
3224 SmallPtrSet<Instruction *, 4> IncompleteRets;
3225 for (auto &I : llvm::reverse(instructions(F))) {
3226 auto It = ToProcess.find(&I);
3227 if (It == ToProcess.end())
3228 continue;
3229 It->second.remove_if([this](Value *V) { return !isTodoType(V); });
3230 if (It->second.size() == 0)
3231 continue;
3232 deduceOperandElementType(&I, &IncompleteRets, &It->second, true);
3233 if (TodoTypeSz == 0)
3234 return true;
3235 }
3236 }
3237
3238 return SzTodo > TodoTypeSz;
3239}
3240
3241// Parse and store argument types of function declarations where needed.
3242void SPIRVEmitIntrinsics::parseFunDeclarations(Module &M) {
3243 for (auto &F : M) {
3244 if (!F.isDeclaration() || F.isIntrinsic())
3245 continue;
3246 // get the demangled name
3247 std::string DemangledName = getOclOrSpirvBuiltinDemangledName(F.getName());
3248 if (DemangledName.empty())
3249 continue;
3250 // allow only OpGroupAsyncCopy use case at the moment
3251 const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(F);
3252 auto [Grp, Opcode, ExtNo] = SPIRV::mapBuiltinToOpcode(
3253 DemangledName, ST.getPreferredInstructionSet());
3254 if (Opcode != SPIRV::OpGroupAsyncCopy)
3255 continue;
3256 // find pointer arguments
3257 SmallVector<unsigned> Idxs;
3258 for (unsigned OpIdx = 0; OpIdx < F.arg_size(); ++OpIdx) {
3259 Argument *Arg = F.getArg(OpIdx);
3260 if (isPointerTy(Arg->getType()) && !hasPointeeTypeAttr(Arg))
3261 Idxs.push_back(OpIdx);
3262 }
3263 if (!Idxs.size())
3264 continue;
3265 // parse function arguments
3266 LLVMContext &Ctx = F.getContext();
3268 SPIRV::parseBuiltinTypeStr(TypeStrs, DemangledName, Ctx);
3269 if (!TypeStrs.size())
3270 continue;
3271 // find type info for pointer arguments
3272 for (unsigned Idx : Idxs) {
3273 if (Idx >= TypeStrs.size())
3274 continue;
3275 if (Type *ElemTy =
3276 SPIRV::parseBuiltinCallArgumentType(TypeStrs[Idx].trim(), Ctx))
3278 !ElemTy->isTargetExtTy())
3279 FDeclPtrTys[&F].push_back(std::make_pair(Idx, ElemTy));
3280 }
3281 }
3282}
3283
3284bool SPIRVEmitIntrinsics::runOnModule(Module &M) {
3285 bool Changed = false;
3286
3287 parseFunDeclarations(M);
3288 insertConstantsForFPFastMathDefault(M);
3289 GVUsers.init(M);
3290
3291 TodoType.clear();
3292 for (auto &F : M)
3294
3295 // Specify function parameters after all functions were processed.
3296 for (auto &F : M) {
3297 // check if function parameter types are set
3298 CurrF = &F;
3299 if (!F.isDeclaration() && !F.isIntrinsic()) {
3300 IRBuilder<> B(F.getContext());
3301 processParamTypes(&F, B);
3302 }
3303 }
3304
3305 CanTodoType = false;
3306 Changed |= postprocessTypes(M);
3307
3308 if (HaveFunPtrs)
3309 Changed |= processFunctionPointers(M);
3310
3311 return Changed;
3312}
3313
3315 return new SPIRVEmitIntrinsics(TM);
3316}
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
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
Function * Fun
#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 cl::opt< bool > SpirvEmitOpNames("spirv-emit-op-names", cl::desc("Emit OpName for all instructions"), cl::init(false))
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 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
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
static std::optional< unsigned > getOpcode(ArrayRef< VPValue * > Values)
Returns the opcode of Values or ~0 if they do not all agree.
Definition VPlanSLP.cpp:247
static int Lookup(ArrayRef< TableEntry > Table, unsigned Opcode)
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:539
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 if the block is well formed or null if the block is not well forme...
Definition BasicBlock.h:233
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
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
This class represents a function call, abstracting a target machine's calling convention.
static ConstantAsMetadata * get(Constant *C)
Definition Metadata.h:537
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition Constants.h:168
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:178
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:256
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:241
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:362
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:246
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:358
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:2788
LLVM_ABI void addDestination(BasicBlock *Dest)
Add a destination.
Base class for instruction visitors.
Definition InstVisitor.h:78
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:1080
const MDOperand & getOperand(unsigned I) const
Definition Metadata.h:1444
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1572
unsigned getNumOperands() const
Return number of MDNode operands.
Definition Metadata.h:1450
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
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:224
iterator find(StringRef Key)
Definition StringMap.h:237
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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:619
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:907
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:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition Type.h:273
bool isArrayTy() const
True if this is an instance of ArrayType.
Definition Type.h:264
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
Definition Type.cpp:296
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:267
Type * getArrayElementType() const
Definition Type.h:408
LLVM_ABI StringRef getTargetExtName() const
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
Definition Type.cpp:294
bool isStructTy() const
True if this is an instance of StructType.
Definition Type.h:261
bool isTargetExtTy() const
Return true if this is a target extension type.
Definition Type.h:203
bool isAggregateType() const
Return true if the type is an aggregate type.
Definition Type.h:304
static LLVM_ABI Type * getDoubleTy(LLVMContext &C)
Definition Type.cpp:285
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
Definition Type.cpp:284
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
Definition Type.cpp:282
bool isVoidTy() const
Return true if this is 'void'.
Definition Type.h:139
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.
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:256
user_iterator user_begin()
Definition Value.h:403
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:397
iterator_range< user_iterator > users()
Definition Value.h:427
bool use_empty() const
Definition Value.h:347
user_iterator user_end()
Definition Value.h:411
void mutateType(Type *Ty)
Mutate the type of this Value to be of the specified type.
Definition Value.h:840
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:322
bool user_empty() const
Definition Value.h:390
std::pair< iterator, bool > insert(const ValueT &V)
Definition DenseSet.h:202
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
Definition DenseSet.h:175
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.
@ 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
bool match(Val *V, const Pattern &P)
is_zero m_Zero()
Match any null constant or a vector with all elements equal to 0.
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.
Definition Types.h:26
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:316
@ Offset
Definition DWP.cpp:532
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool isTypedPointerWrapper(const TargetExtType *ExtTy)
Definition SPIRVUtils.h:406
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:2554
ModulePass * createSPIRVEmitIntrinsicsPass(SPIRVTargetMachine *TM)
unsigned getPointerAddressSpace(const Type *T)
Definition SPIRVUtils.h:370
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
FunctionAddr VTableAddr uintptr_t uintptr_t Int32Ty
Definition InstrProf.h:296
CallInst * buildIntrWithMD(Intrinsic::ID IntrID, ArrayRef< Type * > Types, Value *Arg, Value *Arg2, ArrayRef< Constant * > Imms, IRBuilder<> &B)
void append_range(Container &C, Range &&R)
Wrapper function to append range R to container C.
Definition STLExtras.h:2208
FPDecorationId
Definition SPIRVUtils.h:550
bool isNestedPointer(const Type *Ty)
Function * getOrCreateBackendServiceFunction(Module &M)
MetadataAsValue * buildMD(Value *Arg)
Definition SPIRVUtils.h:516
std::string getOclOrSpirvBuiltinDemangledName(StringRef Name)
SmallVector< unsigned, 1 > getSpirvLoopControlOperandsFromLoopMetadata(MDNode *LoopMD)
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
Type * getTypedPointerWrapper(Type *ElemTy, unsigned AS)
Definition SPIRVUtils.h:401
bool isVector1(Type *Ty)
Definition SPIRVUtils.h:494
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:364
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::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 >
DWARFExpression::Operation Op
Type * getPointeeTypeByAttr(Argument *Arg)
Definition SPIRVUtils.h:383
bool hasPointeeTypeAttr(Argument *Arg)
Definition SPIRVUtils.h:378
constexpr unsigned BitWidth
bool isEquivalentTypes(Type *Ty1, Type *Ty2)
Definition SPIRVUtils.h:456
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:349
Type * normalizeType(Type *Ty)
Definition SPIRVUtils.h:502
@ Enabled
Convert any .debug_str_offsets tables to DWARF64 if needed.
Definition DWP.h:27
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
PoisonValue * getNormalizedPoisonValue(Type *Ty)
Definition SPIRVUtils.h:512
bool isUntypedPointerTy(const Type *T)
Definition SPIRVUtils.h:359
Type * reconstitutePeeledArrayType(Type *Ty)
SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord)
static size_t computeFPFastMathDefaultInfoVecIndex(size_t BitWidth)
Definition SPIRVUtils.h:149