LLVM 22.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/IR/IRBuilder.h"
22#include "llvm/IR/InstVisitor.h"
23#include "llvm/IR/IntrinsicsSPIRV.h"
27
28#include <queue>
29#include <unordered_set>
30
31// This pass performs the following transformation on LLVM IR level required
32// for the following translation to SPIR-V:
33// - replaces direct usages of aggregate constants with target-specific
34// intrinsics;
35// - replaces aggregates-related instructions (extract/insert, ld/st, etc)
36// with a target-specific intrinsics;
37// - emits intrinsics for the global variable initializers since IRTranslator
38// doesn't handle them and it's not very convenient to translate them
39// ourselves;
40// - emits intrinsics to keep track of the string names assigned to the values;
41// - emits intrinsics to keep track of constants (this is necessary to have an
42// LLVM IR constant after the IRTranslation is completed) for their further
43// deduplication;
44// - emits intrinsics to keep track of original LLVM types of the values
45// to be able to emit proper SPIR-V types eventually.
46//
47// TODO: consider removing spv.track.constant in favor of spv.assign.type.
48
49using namespace llvm;
50
51namespace llvm::SPIRV {
52#define GET_BuiltinGroup_DECL
53#include "SPIRVGenTables.inc"
54} // namespace llvm::SPIRV
55
56namespace {
57
58class SPIRVEmitIntrinsics
59 : public ModulePass,
60 public InstVisitor<SPIRVEmitIntrinsics, Instruction *> {
61 SPIRVTargetMachine *TM = nullptr;
62 SPIRVGlobalRegistry *GR = nullptr;
63 Function *CurrF = nullptr;
64 bool TrackConstants = true;
65 bool HaveFunPtrs = false;
66 DenseMap<Instruction *, Constant *> AggrConsts;
67 DenseMap<Instruction *, Type *> AggrConstTypes;
68 DenseSet<Instruction *> AggrStores;
69 std::unordered_set<Value *> Named;
70
71 // map of function declarations to <pointer arg index => element type>
72 DenseMap<Function *, SmallVector<std::pair<unsigned, Type *>>> FDeclPtrTys;
73
74 // a register of Instructions that don't have a complete type definition
75 bool CanTodoType = true;
76 unsigned TodoTypeSz = 0;
77 DenseMap<Value *, bool> TodoType;
78 void insertTodoType(Value *Op) {
79 // TODO: add isa<CallInst>(Op) to no-insert
80 if (CanTodoType && !isa<GetElementPtrInst>(Op)) {
81 auto It = TodoType.try_emplace(Op, true);
82 if (It.second)
83 ++TodoTypeSz;
84 }
85 }
86 void eraseTodoType(Value *Op) {
87 auto It = TodoType.find(Op);
88 if (It != TodoType.end() && It->second) {
89 It->second = false;
90 --TodoTypeSz;
91 }
92 }
93 bool isTodoType(Value *Op) {
95 return false;
96 auto It = TodoType.find(Op);
97 return It != TodoType.end() && It->second;
98 }
99 // a register of Instructions that were visited by deduceOperandElementType()
100 // to validate operand types with an instruction
101 std::unordered_set<Instruction *> TypeValidated;
102
103 // well known result types of builtins
104 enum WellKnownTypes { Event };
105
106 // deduce element type of untyped pointers
107 Type *deduceElementType(Value *I, bool UnknownElemTypeI8);
108 Type *deduceElementTypeHelper(Value *I, bool UnknownElemTypeI8);
109 Type *deduceElementTypeHelper(Value *I, std::unordered_set<Value *> &Visited,
110 bool UnknownElemTypeI8,
111 bool IgnoreKnownType = false);
112 Type *deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
113 bool UnknownElemTypeI8);
114 Type *deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
115 std::unordered_set<Value *> &Visited,
116 bool UnknownElemTypeI8);
117 Type *deduceElementTypeByUsersDeep(Value *Op,
118 std::unordered_set<Value *> &Visited,
119 bool UnknownElemTypeI8);
120 void maybeAssignPtrType(Type *&Ty, Value *I, Type *RefTy,
121 bool UnknownElemTypeI8);
122
123 // deduce nested types of composites
124 Type *deduceNestedTypeHelper(User *U, bool UnknownElemTypeI8);
125 Type *deduceNestedTypeHelper(User *U, Type *Ty,
126 std::unordered_set<Value *> &Visited,
127 bool UnknownElemTypeI8);
128
129 // deduce Types of operands of the Instruction if possible
130 void deduceOperandElementType(Instruction *I,
131 SmallPtrSet<Instruction *, 4> *IncompleteRets,
132 const SmallPtrSet<Value *, 4> *AskOps = nullptr,
133 bool IsPostprocessing = false);
134
135 void preprocessCompositeConstants(IRBuilder<> &B);
136 void preprocessUndefs(IRBuilder<> &B);
137
138 Type *reconstructType(Value *Op, bool UnknownElemTypeI8,
139 bool IsPostprocessing);
140
141 void replaceMemInstrUses(Instruction *Old, Instruction *New, IRBuilder<> &B);
142 void processInstrAfterVisit(Instruction *I, IRBuilder<> &B);
143 bool insertAssignPtrTypeIntrs(Instruction *I, IRBuilder<> &B,
144 bool UnknownElemTypeI8);
145 void insertAssignTypeIntrs(Instruction *I, IRBuilder<> &B);
146 void insertAssignPtrTypeTargetExt(TargetExtType *AssignedType, Value *V,
147 IRBuilder<> &B);
148 void replacePointerOperandWithPtrCast(Instruction *I, Value *Pointer,
149 Type *ExpectedElementType,
150 unsigned OperandToReplace,
151 IRBuilder<> &B);
152 void insertPtrCastOrAssignTypeInstr(Instruction *I, IRBuilder<> &B);
153 bool shouldTryToAddMemAliasingDecoration(Instruction *Inst);
155 void processGlobalValue(GlobalVariable &GV, IRBuilder<> &B);
156 void processParamTypes(Function *F, IRBuilder<> &B);
157 void processParamTypesByFunHeader(Function *F, IRBuilder<> &B);
158 Type *deduceFunParamElementType(Function *F, unsigned OpIdx);
159 Type *deduceFunParamElementType(Function *F, unsigned OpIdx,
160 std::unordered_set<Function *> &FVisited);
161
162 bool deduceOperandElementTypeCalledFunction(
163 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
164 Type *&KnownElemTy, bool &Incomplete);
165 void deduceOperandElementTypeFunctionPointer(
166 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
167 Type *&KnownElemTy, bool IsPostprocessing);
168 bool deduceOperandElementTypeFunctionRet(
169 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
170 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing,
171 Type *&KnownElemTy, Value *Op, Function *F);
172
173 CallInst *buildSpvPtrcast(Function *F, Value *Op, Type *ElemTy);
174 void replaceUsesOfWithSpvPtrcast(Value *Op, Type *ElemTy, Instruction *I,
175 DenseMap<Function *, CallInst *> Ptrcasts);
176 void propagateElemType(Value *Op, Type *ElemTy,
177 DenseSet<std::pair<Value *, Value *>> &VisitedSubst);
178 void
179 propagateElemTypeRec(Value *Op, Type *PtrElemTy, Type *CastElemTy,
180 DenseSet<std::pair<Value *, Value *>> &VisitedSubst);
181 void propagateElemTypeRec(Value *Op, Type *PtrElemTy, Type *CastElemTy,
182 DenseSet<std::pair<Value *, Value *>> &VisitedSubst,
183 std::unordered_set<Value *> &Visited,
184 DenseMap<Function *, CallInst *> Ptrcasts);
185
186 void replaceAllUsesWith(Value *Src, Value *Dest, bool DeleteOld = true);
187 void replaceAllUsesWithAndErase(IRBuilder<> &B, Instruction *Src,
188 Instruction *Dest, bool DeleteOld = true);
189
190 void applyDemangledPtrArgTypes(IRBuilder<> &B);
191
192 GetElementPtrInst *simplifyZeroLengthArrayGepInst(GetElementPtrInst *GEP);
193
194 bool runOnFunction(Function &F);
195 bool postprocessTypes(Module &M);
196 bool processFunctionPointers(Module &M);
197 void parseFunDeclarations(Module &M);
198
199 void useRoundingMode(ConstrainedFPIntrinsic *FPI, IRBuilder<> &B);
200
201 // Tries to walk the type accessed by the given GEP instruction.
202 // For each nested type access, one of the 2 callbacks is called:
203 // - OnLiteralIndexing when the index is a known constant value.
204 // Parameters:
205 // PointedType: the pointed type resulting of this indexing.
206 // If the parent type is an array, this is the index in the array.
207 // If the parent type is a struct, this is the field index.
208 // Index: index of the element in the parent type.
209 // - OnDynamnicIndexing when the index is a non-constant value.
210 // This callback is only called when indexing into an array.
211 // Parameters:
212 // ElementType: the type of the elements stored in the parent array.
213 // Offset: the Value* containing the byte offset into the array.
214 // Return true if an error occured during the walk, false otherwise.
215 bool walkLogicalAccessChain(
216 GetElementPtrInst &GEP,
217 const std::function<void(Type *PointedType, uint64_t Index)>
218 &OnLiteralIndexing,
219 const std::function<void(Type *ElementType, Value *Offset)>
220 &OnDynamicIndexing);
221
222 // Returns the type accessed using the given GEP instruction by relying
223 // on the GEP type.
224 // FIXME: GEP types are not supposed to be used to retrieve the pointed
225 // type. This must be fixed.
226 Type *getGEPType(GetElementPtrInst *GEP);
227
228 // Returns the type accessed using the given GEP instruction by walking
229 // the source type using the GEP indices.
230 // FIXME: without help from the frontend, this method cannot reliably retrieve
231 // the stored type, nor can robustly determine the depth of the type
232 // we are accessing.
233 Type *getGEPTypeLogical(GetElementPtrInst *GEP);
234
235 Instruction *buildLogicalAccessChainFromGEP(GetElementPtrInst &GEP);
236
237public:
238 static char ID;
239 SPIRVEmitIntrinsics(SPIRVTargetMachine *TM = nullptr)
240 : ModulePass(ID), TM(TM) {}
241 Instruction *visitInstruction(Instruction &I) { return &I; }
242 Instruction *visitSwitchInst(SwitchInst &I);
243 Instruction *visitGetElementPtrInst(GetElementPtrInst &I);
244 Instruction *visitBitCastInst(BitCastInst &I);
245 Instruction *visitInsertElementInst(InsertElementInst &I);
246 Instruction *visitExtractElementInst(ExtractElementInst &I);
247 Instruction *visitInsertValueInst(InsertValueInst &I);
248 Instruction *visitExtractValueInst(ExtractValueInst &I);
249 Instruction *visitLoadInst(LoadInst &I);
250 Instruction *visitStoreInst(StoreInst &I);
251 Instruction *visitAllocaInst(AllocaInst &I);
252 Instruction *visitAtomicCmpXchgInst(AtomicCmpXchgInst &I);
253 Instruction *visitUnreachableInst(UnreachableInst &I);
254 Instruction *visitCallInst(CallInst &I);
255
256 StringRef getPassName() const override { return "SPIRV emit intrinsics"; }
257
258 bool runOnModule(Module &M) override;
259
260 void getAnalysisUsage(AnalysisUsage &AU) const override {
261 ModulePass::getAnalysisUsage(AU);
262 }
263};
264
265bool isConvergenceIntrinsic(const Instruction *I) {
266 const auto *II = dyn_cast<IntrinsicInst>(I);
267 if (!II)
268 return false;
269
270 return II->getIntrinsicID() == Intrinsic::experimental_convergence_entry ||
271 II->getIntrinsicID() == Intrinsic::experimental_convergence_loop ||
272 II->getIntrinsicID() == Intrinsic::experimental_convergence_anchor;
273}
274
275bool expectIgnoredInIRTranslation(const Instruction *I) {
276 const auto *II = dyn_cast<IntrinsicInst>(I);
277 if (!II)
278 return false;
279 switch (II->getIntrinsicID()) {
280 case Intrinsic::invariant_start:
281 case Intrinsic::spv_resource_handlefrombinding:
282 case Intrinsic::spv_resource_getpointer:
283 return true;
284 default:
285 return false;
286 }
287}
288
289// Returns the source pointer from `I` ignoring intermediate ptrcast.
290Value *getPointerRoot(Value *I) {
291 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
292 if (II->getIntrinsicID() == Intrinsic::spv_ptrcast) {
293 Value *V = II->getArgOperand(0);
294 return getPointerRoot(V);
295 }
296 }
297 return I;
298}
299
300} // namespace
301
302char SPIRVEmitIntrinsics::ID = 0;
303
304INITIALIZE_PASS(SPIRVEmitIntrinsics, "emit-intrinsics", "SPIRV emit intrinsics",
305 false, false)
306
307static inline bool isAssignTypeInstr(const Instruction *I) {
308 return isa<IntrinsicInst>(I) &&
309 cast<IntrinsicInst>(I)->getIntrinsicID() == Intrinsic::spv_assign_type;
310}
311
316
317static bool isAggrConstForceInt32(const Value *V) {
318 return isa<ConstantArray>(V) || isa<ConstantStruct>(V) ||
320 (isa<ConstantAggregateZero>(V) && !V->getType()->isVectorTy());
321}
322
324 if (isa<PHINode>(I))
325 B.SetInsertPoint(I->getParent()->getFirstNonPHIOrDbgOrAlloca());
326 else
327 B.SetInsertPoint(I);
328}
329
331 B.SetCurrentDebugLocation(I->getDebugLoc());
332 if (I->getType()->isVoidTy())
333 B.SetInsertPoint(I->getNextNode());
334 else
335 B.SetInsertPoint(*I->getInsertionPointAfterDef());
336}
337
339 if (const auto *Intr = dyn_cast<IntrinsicInst>(I)) {
340 switch (Intr->getIntrinsicID()) {
341 case Intrinsic::invariant_start:
342 case Intrinsic::invariant_end:
343 return false;
344 }
345 }
346 return true;
347}
348
349static inline void reportFatalOnTokenType(const Instruction *I) {
350 if (I->getType()->isTokenTy())
351 report_fatal_error("A token is encountered but SPIR-V without extensions "
352 "does not support token type",
353 false);
354}
355
357 if (!I->hasName() || I->getType()->isAggregateType() ||
358 expectIgnoredInIRTranslation(I))
359 return;
362 LLVMContext &Ctx = I->getContext();
363 std::vector<Value *> Args = {
365 Ctx, MDNode::get(Ctx, MDString::get(Ctx, I->getName())))};
366 B.CreateIntrinsic(Intrinsic::spv_assign_name, {I->getType()}, Args);
367}
368
369void SPIRVEmitIntrinsics::replaceAllUsesWith(Value *Src, Value *Dest,
370 bool DeleteOld) {
371 GR->replaceAllUsesWith(Src, Dest, DeleteOld);
372 // Update uncomplete type records if any
373 if (isTodoType(Src)) {
374 if (DeleteOld)
375 eraseTodoType(Src);
376 insertTodoType(Dest);
377 }
378}
379
380void SPIRVEmitIntrinsics::replaceAllUsesWithAndErase(IRBuilder<> &B,
381 Instruction *Src,
382 Instruction *Dest,
383 bool DeleteOld) {
384 replaceAllUsesWith(Src, Dest, DeleteOld);
385 std::string Name = Src->hasName() ? Src->getName().str() : "";
386 Src->eraseFromParent();
387 if (!Name.empty()) {
388 Dest->setName(Name);
389 if (Named.insert(Dest).second)
390 emitAssignName(Dest, B);
391 }
392}
393
395 return SI && F->getCallingConv() == CallingConv::SPIR_KERNEL &&
396 isPointerTy(SI->getValueOperand()->getType()) &&
397 isa<Argument>(SI->getValueOperand());
398}
399
400// Maybe restore original function return type.
402 Type *Ty) {
404 if (!CI || CI->isIndirectCall() || CI->isInlineAsm() ||
406 return Ty;
407 if (Type *OriginalTy = GR->findMutated(CI->getCalledFunction()))
408 return OriginalTy;
409 return Ty;
410}
411
412// Reconstruct type with nested element types according to deduced type info.
413// Return nullptr if no detailed type info is available.
414Type *SPIRVEmitIntrinsics::reconstructType(Value *Op, bool UnknownElemTypeI8,
415 bool IsPostprocessing) {
416 Type *Ty = Op->getType();
417 if (auto *OpI = dyn_cast<Instruction>(Op))
418 Ty = restoreMutatedType(GR, OpI, Ty);
419 if (!isUntypedPointerTy(Ty))
420 return Ty;
421 // try to find the pointee type
422 if (Type *NestedTy = GR->findDeducedElementType(Op))
424 // not a pointer according to the type info (e.g., Event object)
425 CallInst *CI = GR->findAssignPtrTypeInstr(Op);
426 if (CI) {
427 MetadataAsValue *MD = cast<MetadataAsValue>(CI->getArgOperand(1));
428 return cast<ConstantAsMetadata>(MD->getMetadata())->getType();
429 }
430 if (UnknownElemTypeI8) {
431 if (!IsPostprocessing)
432 insertTodoType(Op);
433 return getTypedPointerWrapper(IntegerType::getInt8Ty(Op->getContext()),
435 }
436 return nullptr;
437}
438
439CallInst *SPIRVEmitIntrinsics::buildSpvPtrcast(Function *F, Value *Op,
440 Type *ElemTy) {
441 IRBuilder<> B(Op->getContext());
442 if (auto *OpI = dyn_cast<Instruction>(Op)) {
443 // spv_ptrcast's argument Op denotes an instruction that generates
444 // a value, and we may use getInsertionPointAfterDef()
446 } else if (auto *OpA = dyn_cast<Argument>(Op)) {
447 B.SetInsertPointPastAllocas(OpA->getParent());
448 B.SetCurrentDebugLocation(DebugLoc());
449 } else {
450 B.SetInsertPoint(F->getEntryBlock().getFirstNonPHIOrDbgOrAlloca());
451 }
452 Type *OpTy = Op->getType();
453 SmallVector<Type *, 2> Types = {OpTy, OpTy};
454 SmallVector<Value *, 2> Args = {Op, buildMD(getNormalizedPoisonValue(ElemTy)),
455 B.getInt32(getPointerAddressSpace(OpTy))};
456 CallInst *PtrCasted =
457 B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
458 GR->buildAssignPtr(B, ElemTy, PtrCasted);
459 return PtrCasted;
460}
461
462void SPIRVEmitIntrinsics::replaceUsesOfWithSpvPtrcast(
463 Value *Op, Type *ElemTy, Instruction *I,
464 DenseMap<Function *, CallInst *> Ptrcasts) {
465 Function *F = I->getParent()->getParent();
466 CallInst *PtrCastedI = nullptr;
467 auto It = Ptrcasts.find(F);
468 if (It == Ptrcasts.end()) {
469 PtrCastedI = buildSpvPtrcast(F, Op, ElemTy);
470 Ptrcasts[F] = PtrCastedI;
471 } else {
472 PtrCastedI = It->second;
473 }
474 I->replaceUsesOfWith(Op, PtrCastedI);
475}
476
477void SPIRVEmitIntrinsics::propagateElemType(
478 Value *Op, Type *ElemTy,
479 DenseSet<std::pair<Value *, Value *>> &VisitedSubst) {
480 DenseMap<Function *, CallInst *> Ptrcasts;
481 SmallVector<User *> Users(Op->users());
482 for (auto *U : Users) {
483 if (!isa<Instruction>(U) || isSpvIntrinsic(U))
484 continue;
485 if (!VisitedSubst.insert(std::make_pair(U, Op)).second)
486 continue;
488 // If the instruction was validated already, we need to keep it valid by
489 // keeping current Op type.
490 if (isa<GetElementPtrInst>(UI) ||
491 TypeValidated.find(UI) != TypeValidated.end())
492 replaceUsesOfWithSpvPtrcast(Op, ElemTy, UI, Ptrcasts);
493 }
494}
495
496void SPIRVEmitIntrinsics::propagateElemTypeRec(
497 Value *Op, Type *PtrElemTy, Type *CastElemTy,
498 DenseSet<std::pair<Value *, Value *>> &VisitedSubst) {
499 std::unordered_set<Value *> Visited;
500 DenseMap<Function *, CallInst *> Ptrcasts;
501 propagateElemTypeRec(Op, PtrElemTy, CastElemTy, VisitedSubst, Visited,
502 std::move(Ptrcasts));
503}
504
505void SPIRVEmitIntrinsics::propagateElemTypeRec(
506 Value *Op, Type *PtrElemTy, Type *CastElemTy,
507 DenseSet<std::pair<Value *, Value *>> &VisitedSubst,
508 std::unordered_set<Value *> &Visited,
509 DenseMap<Function *, CallInst *> Ptrcasts) {
510 if (!Visited.insert(Op).second)
511 return;
512 SmallVector<User *> Users(Op->users());
513 for (auto *U : Users) {
514 if (!isa<Instruction>(U) || isSpvIntrinsic(U))
515 continue;
516 if (!VisitedSubst.insert(std::make_pair(U, Op)).second)
517 continue;
519 // If the instruction was validated already, we need to keep it valid by
520 // keeping current Op type.
521 if (isa<GetElementPtrInst>(UI) ||
522 TypeValidated.find(UI) != TypeValidated.end())
523 replaceUsesOfWithSpvPtrcast(Op, CastElemTy, UI, Ptrcasts);
524 }
525}
526
527// Set element pointer type to the given value of ValueTy and tries to
528// specify this type further (recursively) by Operand value, if needed.
529
530Type *
531SPIRVEmitIntrinsics::deduceElementTypeByValueDeep(Type *ValueTy, Value *Operand,
532 bool UnknownElemTypeI8) {
533 std::unordered_set<Value *> Visited;
534 return deduceElementTypeByValueDeep(ValueTy, Operand, Visited,
535 UnknownElemTypeI8);
536}
537
538Type *SPIRVEmitIntrinsics::deduceElementTypeByValueDeep(
539 Type *ValueTy, Value *Operand, std::unordered_set<Value *> &Visited,
540 bool UnknownElemTypeI8) {
541 Type *Ty = ValueTy;
542 if (Operand) {
543 if (auto *PtrTy = dyn_cast<PointerType>(Ty)) {
544 if (Type *NestedTy =
545 deduceElementTypeHelper(Operand, Visited, UnknownElemTypeI8))
546 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
547 } else {
548 Ty = deduceNestedTypeHelper(dyn_cast<User>(Operand), Ty, Visited,
549 UnknownElemTypeI8);
550 }
551 }
552 return Ty;
553}
554
555// Traverse User instructions to deduce an element pointer type of the operand.
556Type *SPIRVEmitIntrinsics::deduceElementTypeByUsersDeep(
557 Value *Op, std::unordered_set<Value *> &Visited, bool UnknownElemTypeI8) {
558 if (!Op || !isPointerTy(Op->getType()) || isa<ConstantPointerNull>(Op) ||
560 return nullptr;
561
562 if (auto ElemTy = getPointeeType(Op->getType()))
563 return ElemTy;
564
565 // maybe we already know operand's element type
566 if (Type *KnownTy = GR->findDeducedElementType(Op))
567 return KnownTy;
568
569 for (User *OpU : Op->users()) {
570 if (Instruction *Inst = dyn_cast<Instruction>(OpU)) {
571 if (Type *Ty = deduceElementTypeHelper(Inst, Visited, UnknownElemTypeI8))
572 return Ty;
573 }
574 }
575 return nullptr;
576}
577
578// Implements what we know in advance about intrinsics and builtin calls
579// TODO: consider feasibility of this particular case to be generalized by
580// encoding knowledge about intrinsics and builtin calls by corresponding
581// specification rules
583 Function *CalledF, unsigned OpIdx) {
584 if ((DemangledName.starts_with("__spirv_ocl_printf(") ||
585 DemangledName.starts_with("printf(")) &&
586 OpIdx == 0)
587 return IntegerType::getInt8Ty(CalledF->getContext());
588 return nullptr;
589}
590
591// Deduce and return a successfully deduced Type of the Instruction,
592// or nullptr otherwise.
593Type *SPIRVEmitIntrinsics::deduceElementTypeHelper(Value *I,
594 bool UnknownElemTypeI8) {
595 std::unordered_set<Value *> Visited;
596 return deduceElementTypeHelper(I, Visited, UnknownElemTypeI8);
597}
598
599void SPIRVEmitIntrinsics::maybeAssignPtrType(Type *&Ty, Value *Op, Type *RefTy,
600 bool UnknownElemTypeI8) {
601 if (isUntypedPointerTy(RefTy)) {
602 if (!UnknownElemTypeI8)
603 return;
604 insertTodoType(Op);
605 }
606 Ty = RefTy;
607}
608
609bool SPIRVEmitIntrinsics::walkLogicalAccessChain(
610 GetElementPtrInst &GEP,
611 const std::function<void(Type *, uint64_t)> &OnLiteralIndexing,
612 const std::function<void(Type *, Value *)> &OnDynamicIndexing) {
613 // We only rewrite i8* GEP. Other should be left as-is.
614 // Valid i8* GEP must always have a single index.
615 assert(GEP.getSourceElementType() ==
616 IntegerType::getInt8Ty(CurrF->getContext()));
617 assert(GEP.getNumIndices() == 1);
618
619 auto &DL = CurrF->getDataLayout();
620 Value *Src = getPointerRoot(GEP.getPointerOperand());
621 Type *CurType = deduceElementType(Src, true);
622
623 Value *Operand = *GEP.idx_begin();
624 ConstantInt *CI = dyn_cast<ConstantInt>(Operand);
625 if (!CI) {
626 ArrayType *AT = dyn_cast<ArrayType>(CurType);
627 // Operand is not constant. Either we have an array and accept it, or we
628 // give up.
629 if (AT)
630 OnDynamicIndexing(AT->getElementType(), Operand);
631 return AT == nullptr;
632 }
633
634 assert(CI);
635 uint64_t Offset = CI->getZExtValue();
636
637 do {
638 if (ArrayType *AT = dyn_cast<ArrayType>(CurType)) {
639 uint32_t EltTypeSize = DL.getTypeSizeInBits(AT->getElementType()) / 8;
640 assert(Offset < AT->getNumElements() * EltTypeSize);
641 uint64_t Index = Offset / EltTypeSize;
642 Offset = Offset - (Index * EltTypeSize);
643 CurType = AT->getElementType();
644 OnLiteralIndexing(CurType, Index);
645 } else if (StructType *ST = dyn_cast<StructType>(CurType)) {
646 uint32_t StructSize = DL.getTypeSizeInBits(ST) / 8;
647 assert(Offset < StructSize);
648 (void)StructSize;
649 const auto &STL = DL.getStructLayout(ST);
650 unsigned Element = STL->getElementContainingOffset(Offset);
651 Offset -= STL->getElementOffset(Element);
652 CurType = ST->getElementType(Element);
653 OnLiteralIndexing(CurType, Element);
654 } else {
655 // Vector type indexing should not use GEP.
656 // So if we have an index left, something is wrong. Giving up.
657 return true;
658 }
659 } while (Offset > 0);
660
661 return false;
662}
663
665SPIRVEmitIntrinsics::buildLogicalAccessChainFromGEP(GetElementPtrInst &GEP) {
666 auto &DL = CurrF->getDataLayout();
667 IRBuilder<> B(GEP.getParent());
668 B.SetInsertPoint(&GEP);
669
670 std::vector<Value *> Indices;
671 Indices.push_back(ConstantInt::get(
672 IntegerType::getInt32Ty(CurrF->getContext()), 0, /* Signed= */ false));
673 walkLogicalAccessChain(
674 GEP,
675 [&Indices, &B](Type *EltType, uint64_t Index) {
676 Indices.push_back(
677 ConstantInt::get(B.getInt64Ty(), Index, /* Signed= */ false));
678 },
679 [&Indices, &B, &DL](Type *EltType, Value *Offset) {
680 uint32_t EltTypeSize = DL.getTypeSizeInBits(EltType) / 8;
681 Value *Index = B.CreateUDiv(
682 Offset, ConstantInt::get(Offset->getType(), EltTypeSize,
683 /* Signed= */ false));
684 Indices.push_back(Index);
685 });
686
687 SmallVector<Type *, 2> Types = {GEP.getType(), GEP.getOperand(0)->getType()};
689 Args.push_back(B.getInt1(GEP.isInBounds()));
690 Args.push_back(GEP.getOperand(0));
691 llvm::append_range(Args, Indices);
692 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
693 replaceAllUsesWithAndErase(B, &GEP, NewI);
694 return NewI;
695}
696
697Type *SPIRVEmitIntrinsics::getGEPTypeLogical(GetElementPtrInst *GEP) {
698
699 Type *CurType = GEP->getResultElementType();
700
701 bool Interrupted = walkLogicalAccessChain(
702 *GEP, [&CurType](Type *EltType, uint64_t Index) { CurType = EltType; },
703 [&CurType](Type *EltType, Value *Index) { CurType = EltType; });
704
705 return Interrupted ? GEP->getResultElementType() : CurType;
706}
707
708Type *SPIRVEmitIntrinsics::getGEPType(GetElementPtrInst *Ref) {
709 if (Ref->getSourceElementType() ==
710 IntegerType::getInt8Ty(CurrF->getContext()) &&
712 return getGEPTypeLogical(Ref);
713 }
714
715 Type *Ty = nullptr;
716 // TODO: not sure if GetElementPtrInst::getTypeAtIndex() does anything
717 // useful here
718 if (isNestedPointer(Ref->getSourceElementType())) {
719 Ty = Ref->getSourceElementType();
720 for (Use &U : drop_begin(Ref->indices()))
721 Ty = GetElementPtrInst::getTypeAtIndex(Ty, U.get());
722 } else {
723 Ty = Ref->getResultElementType();
724 }
725 return Ty;
726}
727
728Type *SPIRVEmitIntrinsics::deduceElementTypeHelper(
729 Value *I, std::unordered_set<Value *> &Visited, bool UnknownElemTypeI8,
730 bool IgnoreKnownType) {
731 // allow to pass nullptr as an argument
732 if (!I)
733 return nullptr;
734
735 // maybe already known
736 if (!IgnoreKnownType)
737 if (Type *KnownTy = GR->findDeducedElementType(I))
738 return KnownTy;
739
740 // maybe a cycle
741 if (!Visited.insert(I).second)
742 return nullptr;
743
744 // fallback value in case when we fail to deduce a type
745 Type *Ty = nullptr;
746 // look for known basic patterns of type inference
747 if (auto *Ref = dyn_cast<AllocaInst>(I)) {
748 maybeAssignPtrType(Ty, I, Ref->getAllocatedType(), UnknownElemTypeI8);
749 } else if (auto *Ref = dyn_cast<GetElementPtrInst>(I)) {
750 Ty = getGEPType(Ref);
751 } else if (auto *Ref = dyn_cast<LoadInst>(I)) {
752 Value *Op = Ref->getPointerOperand();
753 Type *KnownTy = GR->findDeducedElementType(Op);
754 if (!KnownTy)
755 KnownTy = Op->getType();
756 if (Type *ElemTy = getPointeeType(KnownTy))
757 maybeAssignPtrType(Ty, I, ElemTy, UnknownElemTypeI8);
758 } else if (auto *Ref = dyn_cast<GlobalValue>(I)) {
759 Ty = deduceElementTypeByValueDeep(
760 Ref->getValueType(),
761 Ref->getNumOperands() > 0 ? Ref->getOperand(0) : nullptr, Visited,
762 UnknownElemTypeI8);
763 } else if (auto *Ref = dyn_cast<AddrSpaceCastInst>(I)) {
764 Type *RefTy = deduceElementTypeHelper(Ref->getPointerOperand(), Visited,
765 UnknownElemTypeI8);
766 maybeAssignPtrType(Ty, I, RefTy, UnknownElemTypeI8);
767 } else if (auto *Ref = dyn_cast<BitCastInst>(I)) {
768 if (Type *Src = Ref->getSrcTy(), *Dest = Ref->getDestTy();
769 isPointerTy(Src) && isPointerTy(Dest))
770 Ty = deduceElementTypeHelper(Ref->getOperand(0), Visited,
771 UnknownElemTypeI8);
772 } else if (auto *Ref = dyn_cast<AtomicCmpXchgInst>(I)) {
773 Value *Op = Ref->getNewValOperand();
774 if (isPointerTy(Op->getType()))
775 Ty = deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8);
776 } else if (auto *Ref = dyn_cast<AtomicRMWInst>(I)) {
777 Value *Op = Ref->getValOperand();
778 if (isPointerTy(Op->getType()))
779 Ty = deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8);
780 } else if (auto *Ref = dyn_cast<PHINode>(I)) {
781 Type *BestTy = nullptr;
782 unsigned MaxN = 1;
783 DenseMap<Type *, unsigned> PhiTys;
784 for (int i = Ref->getNumIncomingValues() - 1; i >= 0; --i) {
785 Ty = deduceElementTypeByUsersDeep(Ref->getIncomingValue(i), Visited,
786 UnknownElemTypeI8);
787 if (!Ty)
788 continue;
789 auto It = PhiTys.try_emplace(Ty, 1);
790 if (!It.second) {
791 ++It.first->second;
792 if (It.first->second > MaxN) {
793 MaxN = It.first->second;
794 BestTy = Ty;
795 }
796 }
797 }
798 if (BestTy)
799 Ty = BestTy;
800 } else if (auto *Ref = dyn_cast<SelectInst>(I)) {
801 for (Value *Op : {Ref->getTrueValue(), Ref->getFalseValue()}) {
802 Ty = deduceElementTypeByUsersDeep(Op, Visited, UnknownElemTypeI8);
803 if (Ty)
804 break;
805 }
806 } else if (auto *CI = dyn_cast<CallInst>(I)) {
807 static StringMap<unsigned> ResTypeByArg = {
808 {"to_global", 0},
809 {"to_local", 0},
810 {"to_private", 0},
811 {"__spirv_GenericCastToPtr_ToGlobal", 0},
812 {"__spirv_GenericCastToPtr_ToLocal", 0},
813 {"__spirv_GenericCastToPtr_ToPrivate", 0},
814 {"__spirv_GenericCastToPtrExplicit_ToGlobal", 0},
815 {"__spirv_GenericCastToPtrExplicit_ToLocal", 0},
816 {"__spirv_GenericCastToPtrExplicit_ToPrivate", 0}};
817 // TODO: maybe improve performance by caching demangled names
818
820 if (II && II->getIntrinsicID() == Intrinsic::spv_resource_getpointer) {
821 auto *HandleType = cast<TargetExtType>(II->getOperand(0)->getType());
822 if (HandleType->getTargetExtName() == "spirv.Image" ||
823 HandleType->getTargetExtName() == "spirv.SignedImage") {
824 for (User *U : II->users()) {
825 Ty = cast<Instruction>(U)->getAccessType();
826 if (Ty)
827 break;
828 }
829 } else if (HandleType->getTargetExtName() == "spirv.VulkanBuffer") {
830 // This call is supposed to index into an array
831 Ty = HandleType->getTypeParameter(0);
832 if (Ty->isArrayTy())
833 Ty = Ty->getArrayElementType();
834 else {
835 TargetExtType *BufferTy = cast<TargetExtType>(Ty);
836 assert(BufferTy->getTargetExtName() == "spirv.Layout");
837 Ty = BufferTy->getTypeParameter(0);
838 assert(Ty && Ty->isStructTy());
839 uint32_t Index = cast<ConstantInt>(II->getOperand(1))->getZExtValue();
840 Ty = cast<StructType>(Ty)->getElementType(Index);
841 }
842 } else {
843 llvm_unreachable("Unknown handle type for spv_resource_getpointer.");
844 }
845 } else if (II && II->getIntrinsicID() ==
846 Intrinsic::spv_generic_cast_to_ptr_explicit) {
847 Ty = deduceElementTypeHelper(CI->getArgOperand(0), Visited,
848 UnknownElemTypeI8);
849 } else if (Function *CalledF = CI->getCalledFunction()) {
850 std::string DemangledName =
851 getOclOrSpirvBuiltinDemangledName(CalledF->getName());
852 if (DemangledName.length() > 0)
853 DemangledName = SPIRV::lookupBuiltinNameHelper(DemangledName);
854 auto AsArgIt = ResTypeByArg.find(DemangledName);
855 if (AsArgIt != ResTypeByArg.end())
856 Ty = deduceElementTypeHelper(CI->getArgOperand(AsArgIt->second),
857 Visited, UnknownElemTypeI8);
858 else if (Type *KnownRetTy = GR->findDeducedElementType(CalledF))
859 Ty = KnownRetTy;
860 }
861 }
862
863 // remember the found relationship
864 if (Ty && !IgnoreKnownType) {
865 // specify nested types if needed, otherwise return unchanged
867 }
868
869 return Ty;
870}
871
872// Re-create a type of the value if it has untyped pointer fields, also nested.
873// Return the original value type if no corrections of untyped pointer
874// information is found or needed.
875Type *SPIRVEmitIntrinsics::deduceNestedTypeHelper(User *U,
876 bool UnknownElemTypeI8) {
877 std::unordered_set<Value *> Visited;
878 return deduceNestedTypeHelper(U, U->getType(), Visited, UnknownElemTypeI8);
879}
880
881Type *SPIRVEmitIntrinsics::deduceNestedTypeHelper(
882 User *U, Type *OrigTy, std::unordered_set<Value *> &Visited,
883 bool UnknownElemTypeI8) {
884 if (!U)
885 return OrigTy;
886
887 // maybe already known
888 if (Type *KnownTy = GR->findDeducedCompositeType(U))
889 return KnownTy;
890
891 // maybe a cycle
892 if (!Visited.insert(U).second)
893 return OrigTy;
894
895 if (isa<StructType>(OrigTy)) {
897 bool Change = false;
898 for (unsigned i = 0; i < U->getNumOperands(); ++i) {
899 Value *Op = U->getOperand(i);
900 assert(Op && "Operands should not be null.");
901 Type *OpTy = Op->getType();
902 Type *Ty = OpTy;
903 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
904 if (Type *NestedTy =
905 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
906 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
907 } else {
908 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
909 UnknownElemTypeI8);
910 }
911 Tys.push_back(Ty);
912 Change |= Ty != OpTy;
913 }
914 if (Change) {
915 Type *NewTy = StructType::create(Tys);
916 GR->addDeducedCompositeType(U, NewTy);
917 return NewTy;
918 }
919 } else if (auto *ArrTy = dyn_cast<ArrayType>(OrigTy)) {
920 if (Value *Op = U->getNumOperands() > 0 ? U->getOperand(0) : nullptr) {
921 Type *OpTy = ArrTy->getElementType();
922 Type *Ty = OpTy;
923 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
924 if (Type *NestedTy =
925 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
926 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
927 } else {
928 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
929 UnknownElemTypeI8);
930 }
931 if (Ty != OpTy) {
932 Type *NewTy = ArrayType::get(Ty, ArrTy->getNumElements());
933 GR->addDeducedCompositeType(U, NewTy);
934 return NewTy;
935 }
936 }
937 } else if (auto *VecTy = dyn_cast<VectorType>(OrigTy)) {
938 if (Value *Op = U->getNumOperands() > 0 ? U->getOperand(0) : nullptr) {
939 Type *OpTy = VecTy->getElementType();
940 Type *Ty = OpTy;
941 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
942 if (Type *NestedTy =
943 deduceElementTypeHelper(Op, Visited, UnknownElemTypeI8))
944 Ty = getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
945 } else {
946 Ty = deduceNestedTypeHelper(dyn_cast<User>(Op), OpTy, Visited,
947 UnknownElemTypeI8);
948 }
949 if (Ty != OpTy) {
950 Type *NewTy = VectorType::get(Ty, VecTy->getElementCount());
952 return NewTy;
953 }
954 }
955 }
956
957 return OrigTy;
958}
959
960Type *SPIRVEmitIntrinsics::deduceElementType(Value *I, bool UnknownElemTypeI8) {
961 if (Type *Ty = deduceElementTypeHelper(I, UnknownElemTypeI8))
962 return Ty;
963 if (!UnknownElemTypeI8)
964 return nullptr;
965 insertTodoType(I);
966 return IntegerType::getInt8Ty(I->getContext());
967}
968
970 Value *PointerOperand) {
971 Type *PointeeTy = GR->findDeducedElementType(PointerOperand);
972 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
973 return nullptr;
974 auto *PtrTy = dyn_cast<PointerType>(I->getType());
975 if (!PtrTy)
976 return I->getType();
977 if (Type *NestedTy = GR->findDeducedElementType(I))
978 return getTypedPointerWrapper(NestedTy, PtrTy->getAddressSpace());
979 return nullptr;
980}
981
982// Try to deduce element type for a call base. Returns false if this is an
983// indirect function invocation, and true otherwise.
984bool SPIRVEmitIntrinsics::deduceOperandElementTypeCalledFunction(
985 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
986 Type *&KnownElemTy, bool &Incomplete) {
987 Function *CalledF = CI->getCalledFunction();
988 if (!CalledF)
989 return false;
990 std::string DemangledName =
992 if (DemangledName.length() > 0 &&
993 !StringRef(DemangledName).starts_with("llvm.")) {
994 const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(*CalledF);
995 auto [Grp, Opcode, ExtNo] = SPIRV::mapBuiltinToOpcode(
996 DemangledName, ST.getPreferredInstructionSet());
997 if (Opcode == SPIRV::OpGroupAsyncCopy) {
998 for (unsigned i = 0, PtrCnt = 0; i < CI->arg_size() && PtrCnt < 2; ++i) {
999 Value *Op = CI->getArgOperand(i);
1000 if (!isPointerTy(Op->getType()))
1001 continue;
1002 ++PtrCnt;
1003 if (Type *ElemTy = GR->findDeducedElementType(Op))
1004 KnownElemTy = ElemTy; // src will rewrite dest if both are defined
1005 Ops.push_back(std::make_pair(Op, i));
1006 }
1007 } else if (Grp == SPIRV::Atomic || Grp == SPIRV::AtomicFloating) {
1008 if (CI->arg_size() == 0)
1009 return true;
1010 Value *Op = CI->getArgOperand(0);
1011 if (!isPointerTy(Op->getType()))
1012 return true;
1013 switch (Opcode) {
1014 case SPIRV::OpAtomicFAddEXT:
1015 case SPIRV::OpAtomicFMinEXT:
1016 case SPIRV::OpAtomicFMaxEXT:
1017 case SPIRV::OpAtomicLoad:
1018 case SPIRV::OpAtomicCompareExchangeWeak:
1019 case SPIRV::OpAtomicCompareExchange:
1020 case SPIRV::OpAtomicExchange:
1021 case SPIRV::OpAtomicIAdd:
1022 case SPIRV::OpAtomicISub:
1023 case SPIRV::OpAtomicOr:
1024 case SPIRV::OpAtomicXor:
1025 case SPIRV::OpAtomicAnd:
1026 case SPIRV::OpAtomicUMin:
1027 case SPIRV::OpAtomicUMax:
1028 case SPIRV::OpAtomicSMin:
1029 case SPIRV::OpAtomicSMax: {
1030 KnownElemTy = isPointerTy(CI->getType()) ? getAtomicElemTy(GR, CI, Op)
1031 : CI->getType();
1032 if (!KnownElemTy)
1033 return true;
1034 Incomplete = isTodoType(Op);
1035 Ops.push_back(std::make_pair(Op, 0));
1036 } break;
1037 case SPIRV::OpAtomicStore: {
1038 if (CI->arg_size() < 4)
1039 return true;
1040 Value *ValOp = CI->getArgOperand(3);
1041 KnownElemTy = isPointerTy(ValOp->getType())
1042 ? getAtomicElemTy(GR, CI, Op)
1043 : ValOp->getType();
1044 if (!KnownElemTy)
1045 return true;
1046 Incomplete = isTodoType(Op);
1047 Ops.push_back(std::make_pair(Op, 0));
1048 } break;
1049 }
1050 }
1051 }
1052 return true;
1053}
1054
1055// Try to deduce element type for a function pointer.
1056void SPIRVEmitIntrinsics::deduceOperandElementTypeFunctionPointer(
1057 CallInst *CI, SmallVector<std::pair<Value *, unsigned>> &Ops,
1058 Type *&KnownElemTy, bool IsPostprocessing) {
1059 Value *Op = CI->getCalledOperand();
1060 if (!Op || !isPointerTy(Op->getType()))
1061 return;
1062 Ops.push_back(std::make_pair(Op, std::numeric_limits<unsigned>::max()));
1063 FunctionType *FTy = CI->getFunctionType();
1064 bool IsNewFTy = false, IsIncomplete = false;
1066 for (Value *Arg : CI->args()) {
1067 Type *ArgTy = Arg->getType();
1068 if (ArgTy->isPointerTy()) {
1069 if (Type *ElemTy = GR->findDeducedElementType(Arg)) {
1070 IsNewFTy = true;
1071 ArgTy = getTypedPointerWrapper(ElemTy, getPointerAddressSpace(ArgTy));
1072 if (isTodoType(Arg))
1073 IsIncomplete = true;
1074 } else {
1075 IsIncomplete = true;
1076 }
1077 }
1078 ArgTys.push_back(ArgTy);
1079 }
1080 Type *RetTy = FTy->getReturnType();
1081 if (CI->getType()->isPointerTy()) {
1082 if (Type *ElemTy = GR->findDeducedElementType(CI)) {
1083 IsNewFTy = true;
1084 RetTy =
1086 if (isTodoType(CI))
1087 IsIncomplete = true;
1088 } else {
1089 IsIncomplete = true;
1090 }
1091 }
1092 if (!IsPostprocessing && IsIncomplete)
1093 insertTodoType(Op);
1094 KnownElemTy =
1095 IsNewFTy ? FunctionType::get(RetTy, ArgTys, FTy->isVarArg()) : FTy;
1096}
1097
1098bool SPIRVEmitIntrinsics::deduceOperandElementTypeFunctionRet(
1099 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
1100 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing,
1101 Type *&KnownElemTy, Value *Op, Function *F) {
1102 KnownElemTy = GR->findDeducedElementType(F);
1103 if (KnownElemTy)
1104 return false;
1105 if (Type *OpElemTy = GR->findDeducedElementType(Op)) {
1106 OpElemTy = normalizeType(OpElemTy);
1107 GR->addDeducedElementType(F, OpElemTy);
1108 GR->addReturnType(
1109 F, TypedPointerType::get(OpElemTy,
1110 getPointerAddressSpace(F->getReturnType())));
1111 // non-recursive update of types in function uses
1112 DenseSet<std::pair<Value *, Value *>> VisitedSubst{std::make_pair(I, Op)};
1113 for (User *U : F->users()) {
1114 CallInst *CI = dyn_cast<CallInst>(U);
1115 if (!CI || CI->getCalledFunction() != F)
1116 continue;
1117 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(CI)) {
1118 if (Type *PrevElemTy = GR->findDeducedElementType(CI)) {
1119 GR->updateAssignType(AssignCI, CI,
1120 getNormalizedPoisonValue(OpElemTy));
1121 propagateElemType(CI, PrevElemTy, VisitedSubst);
1122 }
1123 }
1124 }
1125 // Non-recursive update of types in the function uncomplete returns.
1126 // This may happen just once per a function, the latch is a pair of
1127 // findDeducedElementType(F) / addDeducedElementType(F, ...).
1128 // With or without the latch it is a non-recursive call due to
1129 // IncompleteRets set to nullptr in this call.
1130 if (IncompleteRets)
1131 for (Instruction *IncompleteRetI : *IncompleteRets)
1132 deduceOperandElementType(IncompleteRetI, nullptr, AskOps,
1133 IsPostprocessing);
1134 } else if (IncompleteRets) {
1135 IncompleteRets->insert(I);
1136 }
1137 TypeValidated.insert(I);
1138 return true;
1139}
1140
1141// If the Instruction has Pointer operands with unresolved types, this function
1142// tries to deduce them. If the Instruction has Pointer operands with known
1143// types which differ from expected, this function tries to insert a bitcast to
1144// resolve the issue.
1145void SPIRVEmitIntrinsics::deduceOperandElementType(
1146 Instruction *I, SmallPtrSet<Instruction *, 4> *IncompleteRets,
1147 const SmallPtrSet<Value *, 4> *AskOps, bool IsPostprocessing) {
1149 Type *KnownElemTy = nullptr;
1150 bool Incomplete = false;
1151 // look for known basic patterns of type inference
1152 if (auto *Ref = dyn_cast<PHINode>(I)) {
1153 if (!isPointerTy(I->getType()) ||
1154 !(KnownElemTy = GR->findDeducedElementType(I)))
1155 return;
1156 Incomplete = isTodoType(I);
1157 for (unsigned i = 0; i < Ref->getNumIncomingValues(); i++) {
1158 Value *Op = Ref->getIncomingValue(i);
1159 if (isPointerTy(Op->getType()))
1160 Ops.push_back(std::make_pair(Op, i));
1161 }
1162 } else if (auto *Ref = dyn_cast<AddrSpaceCastInst>(I)) {
1163 KnownElemTy = GR->findDeducedElementType(I);
1164 if (!KnownElemTy)
1165 return;
1166 Incomplete = isTodoType(I);
1167 Ops.push_back(std::make_pair(Ref->getPointerOperand(), 0));
1168 } else if (auto *Ref = dyn_cast<BitCastInst>(I)) {
1169 if (!isPointerTy(I->getType()))
1170 return;
1171 KnownElemTy = GR->findDeducedElementType(I);
1172 if (!KnownElemTy)
1173 return;
1174 Incomplete = isTodoType(I);
1175 Ops.push_back(std::make_pair(Ref->getOperand(0), 0));
1176 } else if (auto *Ref = dyn_cast<GetElementPtrInst>(I)) {
1177 if (GR->findDeducedElementType(Ref->getPointerOperand()))
1178 return;
1179 KnownElemTy = Ref->getSourceElementType();
1180 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1182 } else if (auto *Ref = dyn_cast<LoadInst>(I)) {
1183 KnownElemTy = I->getType();
1184 if (isUntypedPointerTy(KnownElemTy))
1185 return;
1186 Type *PointeeTy = GR->findDeducedElementType(Ref->getPointerOperand());
1187 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
1188 return;
1189 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1191 } else if (auto *Ref = dyn_cast<StoreInst>(I)) {
1192 if (!(KnownElemTy =
1193 reconstructType(Ref->getValueOperand(), false, IsPostprocessing)))
1194 return;
1195 Type *PointeeTy = GR->findDeducedElementType(Ref->getPointerOperand());
1196 if (PointeeTy && !isUntypedPointerTy(PointeeTy))
1197 return;
1198 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1200 } else if (auto *Ref = dyn_cast<AtomicCmpXchgInst>(I)) {
1201 KnownElemTy = isPointerTy(I->getType())
1202 ? getAtomicElemTy(GR, I, Ref->getPointerOperand())
1203 : I->getType();
1204 if (!KnownElemTy)
1205 return;
1206 Incomplete = isTodoType(Ref->getPointerOperand());
1207 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1209 } else if (auto *Ref = dyn_cast<AtomicRMWInst>(I)) {
1210 KnownElemTy = isPointerTy(I->getType())
1211 ? getAtomicElemTy(GR, I, Ref->getPointerOperand())
1212 : I->getType();
1213 if (!KnownElemTy)
1214 return;
1215 Incomplete = isTodoType(Ref->getPointerOperand());
1216 Ops.push_back(std::make_pair(Ref->getPointerOperand(),
1218 } else if (auto *Ref = dyn_cast<SelectInst>(I)) {
1219 if (!isPointerTy(I->getType()) ||
1220 !(KnownElemTy = GR->findDeducedElementType(I)))
1221 return;
1222 Incomplete = isTodoType(I);
1223 for (unsigned i = 0; i < Ref->getNumOperands(); i++) {
1224 Value *Op = Ref->getOperand(i);
1225 if (isPointerTy(Op->getType()))
1226 Ops.push_back(std::make_pair(Op, i));
1227 }
1228 } else if (auto *Ref = dyn_cast<ReturnInst>(I)) {
1229 if (!isPointerTy(CurrF->getReturnType()))
1230 return;
1231 Value *Op = Ref->getReturnValue();
1232 if (!Op)
1233 return;
1234 if (deduceOperandElementTypeFunctionRet(I, IncompleteRets, AskOps,
1235 IsPostprocessing, KnownElemTy, Op,
1236 CurrF))
1237 return;
1238 Incomplete = isTodoType(CurrF);
1239 Ops.push_back(std::make_pair(Op, 0));
1240 } else if (auto *Ref = dyn_cast<ICmpInst>(I)) {
1241 if (!isPointerTy(Ref->getOperand(0)->getType()))
1242 return;
1243 Value *Op0 = Ref->getOperand(0);
1244 Value *Op1 = Ref->getOperand(1);
1245 bool Incomplete0 = isTodoType(Op0);
1246 bool Incomplete1 = isTodoType(Op1);
1247 Type *ElemTy1 = GR->findDeducedElementType(Op1);
1248 Type *ElemTy0 = (Incomplete0 && !Incomplete1 && ElemTy1)
1249 ? nullptr
1250 : GR->findDeducedElementType(Op0);
1251 if (ElemTy0) {
1252 KnownElemTy = ElemTy0;
1253 Incomplete = Incomplete0;
1254 Ops.push_back(std::make_pair(Op1, 1));
1255 } else if (ElemTy1) {
1256 KnownElemTy = ElemTy1;
1257 Incomplete = Incomplete1;
1258 Ops.push_back(std::make_pair(Op0, 0));
1259 }
1260 } else if (CallInst *CI = dyn_cast<CallInst>(I)) {
1261 if (!CI->isIndirectCall())
1262 deduceOperandElementTypeCalledFunction(CI, Ops, KnownElemTy, Incomplete);
1263 else if (HaveFunPtrs)
1264 deduceOperandElementTypeFunctionPointer(CI, Ops, KnownElemTy,
1265 IsPostprocessing);
1266 }
1267
1268 // There is no enough info to deduce types or all is valid.
1269 if (!KnownElemTy || Ops.size() == 0)
1270 return;
1271
1272 LLVMContext &Ctx = CurrF->getContext();
1273 IRBuilder<> B(Ctx);
1274 for (auto &OpIt : Ops) {
1275 Value *Op = OpIt.first;
1276 if (AskOps && !AskOps->contains(Op))
1277 continue;
1278 Type *AskTy = nullptr;
1279 CallInst *AskCI = nullptr;
1280 if (IsPostprocessing && AskOps) {
1281 AskTy = GR->findDeducedElementType(Op);
1282 AskCI = GR->findAssignPtrTypeInstr(Op);
1283 assert(AskTy && AskCI);
1284 }
1285 Type *Ty = AskTy ? AskTy : GR->findDeducedElementType(Op);
1286 if (Ty == KnownElemTy)
1287 continue;
1288 Value *OpTyVal = getNormalizedPoisonValue(KnownElemTy);
1289 Type *OpTy = Op->getType();
1290 if (Op->hasUseList() &&
1291 (!Ty || AskTy || isUntypedPointerTy(Ty) || isTodoType(Op))) {
1292 Type *PrevElemTy = GR->findDeducedElementType(Op);
1293 GR->addDeducedElementType(Op, normalizeType(KnownElemTy));
1294 // check if KnownElemTy is complete
1295 if (!Incomplete)
1296 eraseTodoType(Op);
1297 else if (!IsPostprocessing)
1298 insertTodoType(Op);
1299 // check if there is existing Intrinsic::spv_assign_ptr_type instruction
1300 CallInst *AssignCI = AskCI ? AskCI : GR->findAssignPtrTypeInstr(Op);
1301 if (AssignCI == nullptr) {
1302 Instruction *User = dyn_cast<Instruction>(Op->use_begin()->get());
1303 setInsertPointSkippingPhis(B, User ? User->getNextNode() : I);
1304 CallInst *CI =
1305 buildIntrWithMD(Intrinsic::spv_assign_ptr_type, {OpTy}, OpTyVal, Op,
1306 {B.getInt32(getPointerAddressSpace(OpTy))}, B);
1307 GR->addAssignPtrTypeInstr(Op, CI);
1308 } else {
1309 GR->updateAssignType(AssignCI, Op, OpTyVal);
1310 DenseSet<std::pair<Value *, Value *>> VisitedSubst{
1311 std::make_pair(I, Op)};
1312 propagateElemTypeRec(Op, KnownElemTy, PrevElemTy, VisitedSubst);
1313 }
1314 } else {
1315 eraseTodoType(Op);
1316 CallInst *PtrCastI =
1317 buildSpvPtrcast(I->getParent()->getParent(), Op, KnownElemTy);
1318 if (OpIt.second == std::numeric_limits<unsigned>::max())
1319 dyn_cast<CallInst>(I)->setCalledOperand(PtrCastI);
1320 else
1321 I->setOperand(OpIt.second, PtrCastI);
1322 }
1323 }
1324 TypeValidated.insert(I);
1325}
1326
1327void SPIRVEmitIntrinsics::replaceMemInstrUses(Instruction *Old,
1328 Instruction *New,
1329 IRBuilder<> &B) {
1330 while (!Old->user_empty()) {
1331 auto *U = Old->user_back();
1332 if (isAssignTypeInstr(U)) {
1333 B.SetInsertPoint(U);
1334 SmallVector<Value *, 2> Args = {New, U->getOperand(1)};
1335 CallInst *AssignCI =
1336 B.CreateIntrinsic(Intrinsic::spv_assign_type, {New->getType()}, Args);
1337 GR->addAssignPtrTypeInstr(New, AssignCI);
1338 U->eraseFromParent();
1339 } else if (isMemInstrToReplace(U) || isa<ReturnInst>(U) ||
1340 isa<CallInst>(U)) {
1341 U->replaceUsesOfWith(Old, New);
1342 } else {
1343 llvm_unreachable("illegal aggregate intrinsic user");
1344 }
1345 }
1346 New->copyMetadata(*Old);
1347 Old->eraseFromParent();
1348}
1349
1350void SPIRVEmitIntrinsics::preprocessUndefs(IRBuilder<> &B) {
1351 std::queue<Instruction *> Worklist;
1352 for (auto &I : instructions(CurrF))
1353 Worklist.push(&I);
1354
1355 while (!Worklist.empty()) {
1356 Instruction *I = Worklist.front();
1357 bool BPrepared = false;
1358 Worklist.pop();
1359
1360 for (auto &Op : I->operands()) {
1361 auto *AggrUndef = dyn_cast<UndefValue>(Op);
1362 if (!AggrUndef || !Op->getType()->isAggregateType())
1363 continue;
1364
1365 if (!BPrepared) {
1367 BPrepared = true;
1368 }
1369 auto *IntrUndef = B.CreateIntrinsic(Intrinsic::spv_undef, {});
1370 Worklist.push(IntrUndef);
1371 I->replaceUsesOfWith(Op, IntrUndef);
1372 AggrConsts[IntrUndef] = AggrUndef;
1373 AggrConstTypes[IntrUndef] = AggrUndef->getType();
1374 }
1375 }
1376}
1377
1378void SPIRVEmitIntrinsics::preprocessCompositeConstants(IRBuilder<> &B) {
1379 std::queue<Instruction *> Worklist;
1380 for (auto &I : instructions(CurrF))
1381 Worklist.push(&I);
1382
1383 while (!Worklist.empty()) {
1384 auto *I = Worklist.front();
1385 bool IsPhi = isa<PHINode>(I), BPrepared = false;
1386 assert(I);
1387 bool KeepInst = false;
1388 for (const auto &Op : I->operands()) {
1389 Constant *AggrConst = nullptr;
1390 Type *ResTy = nullptr;
1391 if (auto *COp = dyn_cast<ConstantVector>(Op)) {
1392 AggrConst = cast<Constant>(COp);
1393 ResTy = COp->getType();
1394 } else if (auto *COp = dyn_cast<ConstantArray>(Op)) {
1395 AggrConst = cast<Constant>(COp);
1396 ResTy = B.getInt32Ty();
1397 } else if (auto *COp = dyn_cast<ConstantStruct>(Op)) {
1398 AggrConst = cast<Constant>(COp);
1399 ResTy = B.getInt32Ty();
1400 } else if (auto *COp = dyn_cast<ConstantDataArray>(Op)) {
1401 AggrConst = cast<Constant>(COp);
1402 ResTy = B.getInt32Ty();
1403 } else if (auto *COp = dyn_cast<ConstantAggregateZero>(Op)) {
1404 AggrConst = cast<Constant>(COp);
1405 ResTy = Op->getType()->isVectorTy() ? COp->getType() : B.getInt32Ty();
1406 }
1407 if (AggrConst) {
1409 if (auto *COp = dyn_cast<ConstantDataSequential>(Op))
1410 for (unsigned i = 0; i < COp->getNumElements(); ++i)
1411 Args.push_back(COp->getElementAsConstant(i));
1412 else
1413 llvm::append_range(Args, AggrConst->operands());
1414 if (!BPrepared) {
1415 IsPhi ? B.SetInsertPointPastAllocas(I->getParent()->getParent())
1416 : B.SetInsertPoint(I);
1417 BPrepared = true;
1418 }
1419 auto *CI =
1420 B.CreateIntrinsic(Intrinsic::spv_const_composite, {ResTy}, {Args});
1421 Worklist.push(CI);
1422 I->replaceUsesOfWith(Op, CI);
1423 KeepInst = true;
1424 AggrConsts[CI] = AggrConst;
1425 AggrConstTypes[CI] = deduceNestedTypeHelper(AggrConst, false);
1426 }
1427 }
1428 if (!KeepInst)
1429 Worklist.pop();
1430 }
1431}
1432
1434 IRBuilder<> &B) {
1435 LLVMContext &Ctx = I->getContext();
1437 B.CreateIntrinsic(Intrinsic::spv_assign_decoration, {I->getType()},
1438 {I, MetadataAsValue::get(Ctx, MDNode::get(Ctx, {Node}))});
1439}
1440
1442 unsigned RoundingModeDeco,
1443 IRBuilder<> &B) {
1444 LLVMContext &Ctx = I->getContext();
1446 MDNode *RoundingModeNode = MDNode::get(
1447 Ctx,
1449 ConstantInt::get(Int32Ty, SPIRV::Decoration::FPRoundingMode)),
1450 ConstantAsMetadata::get(ConstantInt::get(Int32Ty, RoundingModeDeco))});
1451 createDecorationIntrinsic(I, RoundingModeNode, B);
1452}
1453
1455 IRBuilder<> &B) {
1456 LLVMContext &Ctx = I->getContext();
1458 MDNode *SaturatedConversionNode =
1459 MDNode::get(Ctx, {ConstantAsMetadata::get(ConstantInt::get(
1460 Int32Ty, SPIRV::Decoration::SaturatedConversion))});
1461 createDecorationIntrinsic(I, SaturatedConversionNode, B);
1462}
1463
1465 if (auto *CI = dyn_cast<CallInst>(I)) {
1466 if (Function *Fu = CI->getCalledFunction()) {
1467 if (Fu->isIntrinsic()) {
1468 unsigned const int IntrinsicId = Fu->getIntrinsicID();
1469 switch (IntrinsicId) {
1470 case Intrinsic::fptosi_sat:
1471 case Intrinsic::fptoui_sat:
1473 break;
1474 default:
1475 break;
1476 }
1477 }
1478 }
1479 }
1480}
1481
1482Instruction *SPIRVEmitIntrinsics::visitCallInst(CallInst &Call) {
1483 if (!Call.isInlineAsm())
1484 return &Call;
1485
1486 const InlineAsm *IA = cast<InlineAsm>(Call.getCalledOperand());
1487 LLVMContext &Ctx = CurrF->getContext();
1488
1489 Constant *TyC = UndefValue::get(IA->getFunctionType());
1490 MDString *ConstraintString = MDString::get(Ctx, IA->getConstraintString());
1492 buildMD(TyC),
1493 MetadataAsValue::get(Ctx, MDNode::get(Ctx, ConstraintString))};
1494 for (unsigned OpIdx = 0; OpIdx < Call.arg_size(); OpIdx++)
1495 Args.push_back(Call.getArgOperand(OpIdx));
1496
1498 B.SetInsertPoint(&Call);
1499 B.CreateIntrinsic(Intrinsic::spv_inline_asm, {Args});
1500 return &Call;
1501}
1502
1503// Use a tip about rounding mode to create a decoration.
1504void SPIRVEmitIntrinsics::useRoundingMode(ConstrainedFPIntrinsic *FPI,
1505 IRBuilder<> &B) {
1506 std::optional<RoundingMode> RM = FPI->getRoundingMode();
1507 if (!RM.has_value())
1508 return;
1509 unsigned RoundingModeDeco = std::numeric_limits<unsigned>::max();
1510 switch (RM.value()) {
1511 default:
1512 // ignore unknown rounding modes
1513 break;
1514 case RoundingMode::NearestTiesToEven:
1515 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTE;
1516 break;
1517 case RoundingMode::TowardNegative:
1518 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTN;
1519 break;
1520 case RoundingMode::TowardPositive:
1521 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTP;
1522 break;
1523 case RoundingMode::TowardZero:
1524 RoundingModeDeco = SPIRV::FPRoundingMode::FPRoundingMode::RTZ;
1525 break;
1526 case RoundingMode::Dynamic:
1527 case RoundingMode::NearestTiesToAway:
1528 // TODO: check if supported
1529 break;
1530 }
1531 if (RoundingModeDeco == std::numeric_limits<unsigned>::max())
1532 return;
1533 // Convert the tip about rounding mode into a decoration record.
1534 createRoundingModeDecoration(FPI, RoundingModeDeco, B);
1535}
1536
1537Instruction *SPIRVEmitIntrinsics::visitSwitchInst(SwitchInst &I) {
1538 BasicBlock *ParentBB = I.getParent();
1539 IRBuilder<> B(ParentBB);
1540 B.SetInsertPoint(&I);
1543 for (auto &Op : I.operands()) {
1544 if (Op.get()->getType()->isSized()) {
1545 Args.push_back(Op);
1546 } else if (BasicBlock *BB = dyn_cast<BasicBlock>(Op.get())) {
1547 BBCases.push_back(BB);
1548 Args.push_back(BlockAddress::get(BB->getParent(), BB));
1549 } else {
1550 report_fatal_error("Unexpected switch operand");
1551 }
1552 }
1553 CallInst *NewI = B.CreateIntrinsic(Intrinsic::spv_switch,
1554 {I.getOperand(0)->getType()}, {Args});
1555 // remove switch to avoid its unneeded and undesirable unwrap into branches
1556 // and conditions
1557 replaceAllUsesWith(&I, NewI);
1558 I.eraseFromParent();
1559 // insert artificial and temporary instruction to preserve valid CFG,
1560 // it will be removed after IR translation pass
1561 B.SetInsertPoint(ParentBB);
1562 IndirectBrInst *BrI = B.CreateIndirectBr(
1563 Constant::getNullValue(PointerType::getUnqual(ParentBB->getContext())),
1564 BBCases.size());
1565 for (BasicBlock *BBCase : BBCases)
1566 BrI->addDestination(BBCase);
1567 return BrI;
1568}
1569
1570Instruction *SPIRVEmitIntrinsics::visitGetElementPtrInst(GetElementPtrInst &I) {
1571 if (I.getSourceElementType() == IntegerType::getInt8Ty(CurrF->getContext()) &&
1573 Instruction *Result = buildLogicalAccessChainFromGEP(I);
1574 if (Result)
1575 return Result;
1576 }
1577
1578 IRBuilder<> B(I.getParent());
1579 B.SetInsertPoint(&I);
1580 SmallVector<Type *, 2> Types = {I.getType(), I.getOperand(0)->getType()};
1582 Args.push_back(B.getInt1(I.isInBounds()));
1583 llvm::append_range(Args, I.operands());
1584 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_gep, {Types}, {Args});
1585 replaceAllUsesWithAndErase(B, &I, NewI);
1586 return NewI;
1587}
1588
1589Instruction *SPIRVEmitIntrinsics::visitBitCastInst(BitCastInst &I) {
1590 IRBuilder<> B(I.getParent());
1591 B.SetInsertPoint(&I);
1592 Value *Source = I.getOperand(0);
1593
1594 // SPIR-V, contrary to LLVM 17+ IR, supports bitcasts between pointers of
1595 // varying element types. In case of IR coming from older versions of LLVM
1596 // such bitcasts do not provide sufficient information, should be just skipped
1597 // here, and handled in insertPtrCastOrAssignTypeInstr.
1598 if (isPointerTy(I.getType())) {
1599 replaceAllUsesWith(&I, Source);
1600 I.eraseFromParent();
1601 return nullptr;
1602 }
1603
1604 SmallVector<Type *, 2> Types = {I.getType(), Source->getType()};
1605 SmallVector<Value *> Args(I.op_begin(), I.op_end());
1606 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_bitcast, {Types}, {Args});
1607 replaceAllUsesWithAndErase(B, &I, NewI);
1608 return NewI;
1609}
1610
1611void SPIRVEmitIntrinsics::insertAssignPtrTypeTargetExt(
1612 TargetExtType *AssignedType, Value *V, IRBuilder<> &B) {
1613 Type *VTy = V->getType();
1614
1615 // A couple of sanity checks.
1616 assert((isPointerTy(VTy)) && "Expect a pointer type!");
1617 if (Type *ElemTy = getPointeeType(VTy))
1618 if (ElemTy != AssignedType)
1619 report_fatal_error("Unexpected pointer element type!");
1620
1621 CallInst *AssignCI = GR->findAssignPtrTypeInstr(V);
1622 if (!AssignCI) {
1623 GR->buildAssignType(B, AssignedType, V);
1624 return;
1625 }
1626
1627 Type *CurrentType =
1629 cast<MetadataAsValue>(AssignCI->getOperand(1))->getMetadata())
1630 ->getType();
1631 if (CurrentType == AssignedType)
1632 return;
1633
1634 // Builtin types cannot be redeclared or casted.
1635 if (CurrentType->isTargetExtTy())
1636 report_fatal_error("Type mismatch " + CurrentType->getTargetExtName() +
1637 "/" + AssignedType->getTargetExtName() +
1638 " for value " + V->getName(),
1639 false);
1640
1641 // Our previous guess about the type seems to be wrong, let's update
1642 // inferred type according to a new, more precise type information.
1643 GR->updateAssignType(AssignCI, V, getNormalizedPoisonValue(AssignedType));
1644}
1645
1646void SPIRVEmitIntrinsics::replacePointerOperandWithPtrCast(
1647 Instruction *I, Value *Pointer, Type *ExpectedElementType,
1648 unsigned OperandToReplace, IRBuilder<> &B) {
1649 TypeValidated.insert(I);
1650
1651 // Do not emit spv_ptrcast if Pointer's element type is ExpectedElementType
1652 Type *PointerElemTy = deduceElementTypeHelper(Pointer, false);
1653 if (PointerElemTy == ExpectedElementType ||
1654 isEquivalentTypes(PointerElemTy, ExpectedElementType))
1655 return;
1656
1658 Value *ExpectedElementVal = getNormalizedPoisonValue(ExpectedElementType);
1659 MetadataAsValue *VMD = buildMD(ExpectedElementVal);
1660 unsigned AddressSpace = getPointerAddressSpace(Pointer->getType());
1661 bool FirstPtrCastOrAssignPtrType = true;
1662
1663 // Do not emit new spv_ptrcast if equivalent one already exists or when
1664 // spv_assign_ptr_type already targets this pointer with the same element
1665 // type.
1666 if (Pointer->hasUseList()) {
1667 for (auto User : Pointer->users()) {
1668 auto *II = dyn_cast<IntrinsicInst>(User);
1669 if (!II ||
1670 (II->getIntrinsicID() != Intrinsic::spv_assign_ptr_type &&
1671 II->getIntrinsicID() != Intrinsic::spv_ptrcast) ||
1672 II->getOperand(0) != Pointer)
1673 continue;
1674
1675 // There is some spv_ptrcast/spv_assign_ptr_type already targeting this
1676 // pointer.
1677 FirstPtrCastOrAssignPtrType = false;
1678 if (II->getOperand(1) != VMD ||
1679 dyn_cast<ConstantInt>(II->getOperand(2))->getSExtValue() !=
1681 continue;
1682
1683 // The spv_ptrcast/spv_assign_ptr_type targeting this pointer is of the
1684 // same element type and address space.
1685 if (II->getIntrinsicID() != Intrinsic::spv_ptrcast)
1686 return;
1687
1688 // This must be a spv_ptrcast, do not emit new if this one has the same BB
1689 // as I. Otherwise, search for other spv_ptrcast/spv_assign_ptr_type.
1690 if (II->getParent() != I->getParent())
1691 continue;
1692
1693 I->setOperand(OperandToReplace, II);
1694 return;
1695 }
1696 }
1697
1698 if (isa<Instruction>(Pointer) || isa<Argument>(Pointer)) {
1699 if (FirstPtrCastOrAssignPtrType) {
1700 // If this would be the first spv_ptrcast, do not emit spv_ptrcast and
1701 // emit spv_assign_ptr_type instead.
1702 GR->buildAssignPtr(B, ExpectedElementType, Pointer);
1703 return;
1704 } else if (isTodoType(Pointer)) {
1705 eraseTodoType(Pointer);
1706 if (!isa<CallInst>(Pointer) && !isa<GetElementPtrInst>(Pointer)) {
1707 // If this wouldn't be the first spv_ptrcast but existing type info is
1708 // uncomplete, update spv_assign_ptr_type arguments.
1709 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(Pointer)) {
1710 Type *PrevElemTy = GR->findDeducedElementType(Pointer);
1711 assert(PrevElemTy);
1712 DenseSet<std::pair<Value *, Value *>> VisitedSubst{
1713 std::make_pair(I, Pointer)};
1714 GR->updateAssignType(AssignCI, Pointer, ExpectedElementVal);
1715 propagateElemType(Pointer, PrevElemTy, VisitedSubst);
1716 } else {
1717 GR->buildAssignPtr(B, ExpectedElementType, Pointer);
1718 }
1719 return;
1720 }
1721 }
1722 }
1723
1724 // Emit spv_ptrcast
1725 SmallVector<Type *, 2> Types = {Pointer->getType(), Pointer->getType()};
1726 SmallVector<Value *, 2> Args = {Pointer, VMD, B.getInt32(AddressSpace)};
1727 auto *PtrCastI = B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
1728 I->setOperand(OperandToReplace, PtrCastI);
1729 // We need to set up a pointee type for the newly created spv_ptrcast.
1730 GR->buildAssignPtr(B, ExpectedElementType, PtrCastI);
1731}
1732
1733void SPIRVEmitIntrinsics::insertPtrCastOrAssignTypeInstr(Instruction *I,
1734 IRBuilder<> &B) {
1735 // Handle basic instructions:
1736 StoreInst *SI = dyn_cast<StoreInst>(I);
1737 if (IsKernelArgInt8(CurrF, SI)) {
1738 replacePointerOperandWithPtrCast(
1739 I, SI->getValueOperand(), IntegerType::getInt8Ty(CurrF->getContext()),
1740 0, B);
1741 }
1742 if (SI) {
1743 Value *Op = SI->getValueOperand();
1744 Value *Pointer = SI->getPointerOperand();
1745 Type *OpTy = Op->getType();
1746 if (auto *OpI = dyn_cast<Instruction>(Op))
1747 OpTy = restoreMutatedType(GR, OpI, OpTy);
1748 if (OpTy == Op->getType())
1749 OpTy = deduceElementTypeByValueDeep(OpTy, Op, false);
1750 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 1, B);
1751 return;
1752 }
1753 if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
1754 Value *Pointer = LI->getPointerOperand();
1755 Type *OpTy = LI->getType();
1756 if (auto *PtrTy = dyn_cast<PointerType>(OpTy)) {
1757 if (Type *ElemTy = GR->findDeducedElementType(LI)) {
1758 OpTy = getTypedPointerWrapper(ElemTy, PtrTy->getAddressSpace());
1759 } else {
1760 Type *NewOpTy = OpTy;
1761 OpTy = deduceElementTypeByValueDeep(OpTy, LI, false);
1762 if (OpTy == NewOpTy)
1763 insertTodoType(Pointer);
1764 }
1765 }
1766 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 0, B);
1767 return;
1768 }
1769 if (GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(I)) {
1770 Value *Pointer = GEPI->getPointerOperand();
1771 Type *OpTy = nullptr;
1772
1773 // Knowing the accessed type is mandatory for logical SPIR-V. Sadly,
1774 // the GEP source element type should not be used for this purpose, and
1775 // the alternative type-scavenging method is not working.
1776 // Physical SPIR-V can work around this, but not logical, hence still
1777 // try to rely on the broken type scavenging for logical.
1778 bool IsRewrittenGEP =
1779 GEPI->getSourceElementType() == IntegerType::getInt8Ty(I->getContext());
1780 if (IsRewrittenGEP && TM->getSubtargetImpl()->isLogicalSPIRV()) {
1781 Value *Src = getPointerRoot(Pointer);
1782 OpTy = GR->findDeducedElementType(Src);
1783 }
1784
1785 // In all cases, fall back to the GEP type if type scavenging failed.
1786 if (!OpTy)
1787 OpTy = GEPI->getSourceElementType();
1788
1789 replacePointerOperandWithPtrCast(I, Pointer, OpTy, 0, B);
1790 if (isNestedPointer(OpTy))
1791 insertTodoType(Pointer);
1792 return;
1793 }
1794
1795 // TODO: review and merge with existing logics:
1796 // Handle calls to builtins (non-intrinsics):
1797 CallInst *CI = dyn_cast<CallInst>(I);
1798 if (!CI || CI->isIndirectCall() || CI->isInlineAsm() ||
1800 return;
1801
1802 // collect information about formal parameter types
1803 std::string DemangledName =
1805 Function *CalledF = CI->getCalledFunction();
1806 SmallVector<Type *, 4> CalledArgTys;
1807 bool HaveTypes = false;
1808 for (unsigned OpIdx = 0; OpIdx < CalledF->arg_size(); ++OpIdx) {
1809 Argument *CalledArg = CalledF->getArg(OpIdx);
1810 Type *ArgType = CalledArg->getType();
1811 if (!isPointerTy(ArgType)) {
1812 CalledArgTys.push_back(nullptr);
1813 } else if (Type *ArgTypeElem = getPointeeType(ArgType)) {
1814 CalledArgTys.push_back(ArgTypeElem);
1815 HaveTypes = true;
1816 } else {
1817 Type *ElemTy = GR->findDeducedElementType(CalledArg);
1818 if (!ElemTy && hasPointeeTypeAttr(CalledArg))
1819 ElemTy = getPointeeTypeByAttr(CalledArg);
1820 if (!ElemTy) {
1821 ElemTy = getPointeeTypeByCallInst(DemangledName, CalledF, OpIdx);
1822 if (ElemTy) {
1823 GR->addDeducedElementType(CalledArg, normalizeType(ElemTy));
1824 } else {
1825 for (User *U : CalledArg->users()) {
1826 if (Instruction *Inst = dyn_cast<Instruction>(U)) {
1827 if ((ElemTy = deduceElementTypeHelper(Inst, false)) != nullptr)
1828 break;
1829 }
1830 }
1831 }
1832 }
1833 HaveTypes |= ElemTy != nullptr;
1834 CalledArgTys.push_back(ElemTy);
1835 }
1836 }
1837
1838 if (DemangledName.empty() && !HaveTypes)
1839 return;
1840
1841 for (unsigned OpIdx = 0; OpIdx < CI->arg_size(); OpIdx++) {
1842 Value *ArgOperand = CI->getArgOperand(OpIdx);
1843 if (!isPointerTy(ArgOperand->getType()))
1844 continue;
1845
1846 // Constants (nulls/undefs) are handled in insertAssignPtrTypeIntrs()
1847 if (!isa<Instruction>(ArgOperand) && !isa<Argument>(ArgOperand)) {
1848 // However, we may have assumptions about the formal argument's type and
1849 // may have a need to insert a ptr cast for the actual parameter of this
1850 // call.
1851 Argument *CalledArg = CalledF->getArg(OpIdx);
1852 if (!GR->findDeducedElementType(CalledArg))
1853 continue;
1854 }
1855
1856 Type *ExpectedType =
1857 OpIdx < CalledArgTys.size() ? CalledArgTys[OpIdx] : nullptr;
1858 if (!ExpectedType && !DemangledName.empty())
1859 ExpectedType = SPIRV::parseBuiltinCallArgumentBaseType(
1860 DemangledName, OpIdx, I->getContext());
1861 if (!ExpectedType || ExpectedType->isVoidTy())
1862 continue;
1863
1864 if (ExpectedType->isTargetExtTy() &&
1866 insertAssignPtrTypeTargetExt(cast<TargetExtType>(ExpectedType),
1867 ArgOperand, B);
1868 else
1869 replacePointerOperandWithPtrCast(CI, ArgOperand, ExpectedType, OpIdx, B);
1870 }
1871}
1872
1873Instruction *SPIRVEmitIntrinsics::visitInsertElementInst(InsertElementInst &I) {
1874 // If it's a <1 x Type> vector type, don't modify it. It's not a legal vector
1875 // type in LLT and IRTranslator will replace it by the scalar.
1876 if (isVector1(I.getType()))
1877 return &I;
1878
1879 SmallVector<Type *, 4> Types = {I.getType(), I.getOperand(0)->getType(),
1880 I.getOperand(1)->getType(),
1881 I.getOperand(2)->getType()};
1882 IRBuilder<> B(I.getParent());
1883 B.SetInsertPoint(&I);
1884 SmallVector<Value *> Args(I.op_begin(), I.op_end());
1885 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_insertelt, {Types}, {Args});
1886 replaceAllUsesWithAndErase(B, &I, NewI);
1887 return NewI;
1888}
1889
1891SPIRVEmitIntrinsics::visitExtractElementInst(ExtractElementInst &I) {
1892 // If it's a <1 x Type> vector type, don't modify it. It's not a legal vector
1893 // type in LLT and IRTranslator will replace it by the scalar.
1894 if (isVector1(I.getVectorOperandType()))
1895 return &I;
1896
1897 IRBuilder<> B(I.getParent());
1898 B.SetInsertPoint(&I);
1899 SmallVector<Type *, 3> Types = {I.getType(), I.getVectorOperandType(),
1900 I.getIndexOperand()->getType()};
1901 SmallVector<Value *, 2> Args = {I.getVectorOperand(), I.getIndexOperand()};
1902 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_extractelt, {Types}, {Args});
1903 replaceAllUsesWithAndErase(B, &I, NewI);
1904 return NewI;
1905}
1906
1907Instruction *SPIRVEmitIntrinsics::visitInsertValueInst(InsertValueInst &I) {
1908 IRBuilder<> B(I.getParent());
1909 B.SetInsertPoint(&I);
1910 SmallVector<Type *, 1> Types = {I.getInsertedValueOperand()->getType()};
1912 Value *AggregateOp = I.getAggregateOperand();
1913 if (isa<UndefValue>(AggregateOp))
1914 Args.push_back(UndefValue::get(B.getInt32Ty()));
1915 else
1916 Args.push_back(AggregateOp);
1917 Args.push_back(I.getInsertedValueOperand());
1918 for (auto &Op : I.indices())
1919 Args.push_back(B.getInt32(Op));
1920 Instruction *NewI =
1921 B.CreateIntrinsic(Intrinsic::spv_insertv, {Types}, {Args});
1922 replaceMemInstrUses(&I, NewI, B);
1923 return NewI;
1924}
1925
1926Instruction *SPIRVEmitIntrinsics::visitExtractValueInst(ExtractValueInst &I) {
1927 if (I.getAggregateOperand()->getType()->isAggregateType())
1928 return &I;
1929 IRBuilder<> B(I.getParent());
1930 B.SetInsertPoint(&I);
1931 SmallVector<Value *> Args(I.operands());
1932 for (auto &Op : I.indices())
1933 Args.push_back(B.getInt32(Op));
1934 auto *NewI =
1935 B.CreateIntrinsic(Intrinsic::spv_extractv, {I.getType()}, {Args});
1936 replaceAllUsesWithAndErase(B, &I, NewI);
1937 return NewI;
1938}
1939
1940Instruction *SPIRVEmitIntrinsics::visitLoadInst(LoadInst &I) {
1941 if (!I.getType()->isAggregateType())
1942 return &I;
1943 IRBuilder<> B(I.getParent());
1944 B.SetInsertPoint(&I);
1945 TrackConstants = false;
1946 const auto *TLI = TM->getSubtargetImpl()->getTargetLowering();
1948 TLI->getLoadMemOperandFlags(I, CurrF->getDataLayout());
1949 auto *NewI =
1950 B.CreateIntrinsic(Intrinsic::spv_load, {I.getOperand(0)->getType()},
1951 {I.getPointerOperand(), B.getInt16(Flags),
1952 B.getInt8(I.getAlign().value())});
1953 replaceMemInstrUses(&I, NewI, B);
1954 return NewI;
1955}
1956
1957Instruction *SPIRVEmitIntrinsics::visitStoreInst(StoreInst &I) {
1958 if (!AggrStores.contains(&I))
1959 return &I;
1960 IRBuilder<> B(I.getParent());
1961 B.SetInsertPoint(&I);
1962 TrackConstants = false;
1963 const auto *TLI = TM->getSubtargetImpl()->getTargetLowering();
1965 TLI->getStoreMemOperandFlags(I, CurrF->getDataLayout());
1966 auto *PtrOp = I.getPointerOperand();
1967 auto *NewI = B.CreateIntrinsic(
1968 Intrinsic::spv_store, {I.getValueOperand()->getType(), PtrOp->getType()},
1969 {I.getValueOperand(), PtrOp, B.getInt16(Flags),
1970 B.getInt8(I.getAlign().value())});
1971 NewI->copyMetadata(I);
1972 I.eraseFromParent();
1973 return NewI;
1974}
1975
1976Instruction *SPIRVEmitIntrinsics::visitAllocaInst(AllocaInst &I) {
1977 Value *ArraySize = nullptr;
1978 if (I.isArrayAllocation()) {
1979 const SPIRVSubtarget *STI = TM->getSubtargetImpl(*I.getFunction());
1980 if (!STI->canUseExtension(
1981 SPIRV::Extension::SPV_INTEL_variable_length_array))
1983 "array allocation: this instruction requires the following "
1984 "SPIR-V extension: SPV_INTEL_variable_length_array",
1985 false);
1986 ArraySize = I.getArraySize();
1987 }
1988 IRBuilder<> B(I.getParent());
1989 B.SetInsertPoint(&I);
1990 TrackConstants = false;
1991 Type *PtrTy = I.getType();
1992 auto *NewI =
1993 ArraySize
1994 ? B.CreateIntrinsic(Intrinsic::spv_alloca_array,
1995 {PtrTy, ArraySize->getType()},
1996 {ArraySize, B.getInt8(I.getAlign().value())})
1997 : B.CreateIntrinsic(Intrinsic::spv_alloca, {PtrTy},
1998 {B.getInt8(I.getAlign().value())});
1999 replaceAllUsesWithAndErase(B, &I, NewI);
2000 return NewI;
2001}
2002
2003Instruction *SPIRVEmitIntrinsics::visitAtomicCmpXchgInst(AtomicCmpXchgInst &I) {
2004 assert(I.getType()->isAggregateType() && "Aggregate result is expected");
2005 IRBuilder<> B(I.getParent());
2006 B.SetInsertPoint(&I);
2007 SmallVector<Value *> Args(I.operands());
2008 Args.push_back(B.getInt32(
2009 static_cast<uint32_t>(getMemScope(I.getContext(), I.getSyncScopeID()))));
2010 Args.push_back(B.getInt32(
2011 static_cast<uint32_t>(getMemSemantics(I.getSuccessOrdering()))));
2012 Args.push_back(B.getInt32(
2013 static_cast<uint32_t>(getMemSemantics(I.getFailureOrdering()))));
2014 auto *NewI = B.CreateIntrinsic(Intrinsic::spv_cmpxchg,
2015 {I.getPointerOperand()->getType()}, {Args});
2016 replaceMemInstrUses(&I, NewI, B);
2017 return NewI;
2018}
2019
2020Instruction *SPIRVEmitIntrinsics::visitUnreachableInst(UnreachableInst &I) {
2021 IRBuilder<> B(I.getParent());
2022 B.SetInsertPoint(&I);
2023 B.CreateIntrinsic(Intrinsic::spv_unreachable, {});
2024 return &I;
2025}
2026
2027void SPIRVEmitIntrinsics::processGlobalValue(GlobalVariable &GV,
2028 IRBuilder<> &B) {
2029 // Skip special artifical variable llvm.global.annotations.
2030 if (GV.getName() == "llvm.global.annotations")
2031 return;
2032 Constant *Init = nullptr;
2033 if (hasInitializer(&GV)) {
2034 // Deduce element type and store results in Global Registry.
2035 // Result is ignored, because TypedPointerType is not supported
2036 // by llvm IR general logic.
2037 deduceElementTypeHelper(&GV, false);
2038 Init = GV.getInitializer();
2039 Type *Ty = isAggrConstForceInt32(Init) ? B.getInt32Ty() : Init->getType();
2040 Constant *Const = isAggrConstForceInt32(Init) ? B.getInt32(1) : Init;
2041 auto *InitInst = B.CreateIntrinsic(Intrinsic::spv_init_global,
2042 {GV.getType(), Ty}, {&GV, Const});
2043 InitInst->setArgOperand(1, Init);
2044 }
2045 if (!Init && GV.use_empty())
2046 B.CreateIntrinsic(Intrinsic::spv_unref_global, GV.getType(), &GV);
2047}
2048
2049// Return true, if we can't decide what is the pointee type now and will get
2050// back to the question later. Return false is spv_assign_ptr_type is not needed
2051// or can be inserted immediately.
2052bool SPIRVEmitIntrinsics::insertAssignPtrTypeIntrs(Instruction *I,
2053 IRBuilder<> &B,
2054 bool UnknownElemTypeI8) {
2056 if (!isPointerTy(I->getType()) || !requireAssignType(I))
2057 return false;
2058
2060 if (Type *ElemTy = deduceElementType(I, UnknownElemTypeI8)) {
2061 GR->buildAssignPtr(B, ElemTy, I);
2062 return false;
2063 }
2064 return true;
2065}
2066
2067void SPIRVEmitIntrinsics::insertAssignTypeIntrs(Instruction *I,
2068 IRBuilder<> &B) {
2069 // TODO: extend the list of functions with known result types
2070 static StringMap<unsigned> ResTypeWellKnown = {
2071 {"async_work_group_copy", WellKnownTypes::Event},
2072 {"async_work_group_strided_copy", WellKnownTypes::Event},
2073 {"__spirv_GroupAsyncCopy", WellKnownTypes::Event}};
2074
2076
2077 bool IsKnown = false;
2078 if (auto *CI = dyn_cast<CallInst>(I)) {
2079 if (!CI->isIndirectCall() && !CI->isInlineAsm() &&
2080 CI->getCalledFunction() && !CI->getCalledFunction()->isIntrinsic()) {
2081 Function *CalledF = CI->getCalledFunction();
2082 std::string DemangledName =
2084 FPDecorationId DecorationId = FPDecorationId::NONE;
2085 if (DemangledName.length() > 0)
2086 DemangledName =
2087 SPIRV::lookupBuiltinNameHelper(DemangledName, &DecorationId);
2088 auto ResIt = ResTypeWellKnown.find(DemangledName);
2089 if (ResIt != ResTypeWellKnown.end()) {
2090 IsKnown = true;
2092 switch (ResIt->second) {
2093 case WellKnownTypes::Event:
2094 GR->buildAssignType(
2095 B, TargetExtType::get(I->getContext(), "spirv.Event"), I);
2096 break;
2097 }
2098 }
2099 // check if a floating rounding mode or saturation info is present
2100 switch (DecorationId) {
2101 default:
2102 break;
2103 case FPDecorationId::SAT:
2105 break;
2106 case FPDecorationId::RTE:
2108 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTE, B);
2109 break;
2110 case FPDecorationId::RTZ:
2112 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTZ, B);
2113 break;
2114 case FPDecorationId::RTP:
2116 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTP, B);
2117 break;
2118 case FPDecorationId::RTN:
2120 CI, SPIRV::FPRoundingMode::FPRoundingMode::RTN, B);
2121 break;
2122 }
2123 }
2124 }
2125
2126 Type *Ty = I->getType();
2127 if (!IsKnown && !Ty->isVoidTy() && !isPointerTy(Ty) && requireAssignType(I)) {
2129 Type *TypeToAssign = Ty;
2130 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
2131 if (II->getIntrinsicID() == Intrinsic::spv_const_composite ||
2132 II->getIntrinsicID() == Intrinsic::spv_undef) {
2133 auto It = AggrConstTypes.find(II);
2134 if (It == AggrConstTypes.end())
2135 report_fatal_error("Unknown composite intrinsic type");
2136 TypeToAssign = It->second;
2137 }
2138 }
2139 TypeToAssign = restoreMutatedType(GR, I, TypeToAssign);
2140 GR->buildAssignType(B, TypeToAssign, I);
2141 }
2142 for (const auto &Op : I->operands()) {
2144 // Check GetElementPtrConstantExpr case.
2147 Type *OpTy = Op->getType();
2148 if (isa<UndefValue>(Op) && OpTy->isAggregateType()) {
2149 CallInst *AssignCI =
2150 buildIntrWithMD(Intrinsic::spv_assign_type, {B.getInt32Ty()}, Op,
2151 UndefValue::get(B.getInt32Ty()), {}, B);
2152 GR->addAssignPtrTypeInstr(Op, AssignCI);
2153 } else if (!isa<Instruction>(Op)) {
2154 Type *OpTy = Op->getType();
2155 Type *OpTyElem = getPointeeType(OpTy);
2156 if (OpTyElem) {
2157 GR->buildAssignPtr(B, OpTyElem, Op);
2158 } else if (isPointerTy(OpTy)) {
2159 Type *ElemTy = GR->findDeducedElementType(Op);
2160 GR->buildAssignPtr(B, ElemTy ? ElemTy : deduceElementType(Op, true),
2161 Op);
2162 } else {
2163 Value *OpTyVal = Op;
2164 if (OpTy->isTargetExtTy()) {
2165 // We need to do this in order to be consistent with how target ext
2166 // types are handled in `processInstrAfterVisit`
2167 OpTyVal = getNormalizedPoisonValue(OpTy);
2168 }
2169 CallInst *AssignCI =
2170 buildIntrWithMD(Intrinsic::spv_assign_type, {OpTy},
2171 getNormalizedPoisonValue(OpTy), OpTyVal, {}, B);
2172 GR->addAssignPtrTypeInstr(OpTyVal, AssignCI);
2173 }
2174 }
2175 }
2176 }
2177}
2178
2179bool SPIRVEmitIntrinsics::shouldTryToAddMemAliasingDecoration(
2180 Instruction *Inst) {
2181 const SPIRVSubtarget *STI = TM->getSubtargetImpl(*Inst->getFunction());
2182 if (!STI->canUseExtension(SPIRV::Extension::SPV_INTEL_memory_access_aliasing))
2183 return false;
2184 // Add aliasing decorations to internal load and store intrinsics
2185 // and atomic instructions, skipping atomic store as it won't have ID to
2186 // attach the decoration.
2187 CallInst *CI = dyn_cast<CallInst>(Inst);
2188 if (!CI)
2189 return false;
2190 if (Function *Fun = CI->getCalledFunction()) {
2191 if (Fun->isIntrinsic()) {
2192 switch (Fun->getIntrinsicID()) {
2193 case Intrinsic::spv_load:
2194 case Intrinsic::spv_store:
2195 return true;
2196 default:
2197 return false;
2198 }
2199 }
2201 const std::string Prefix = "__spirv_Atomic";
2202 const bool IsAtomic = Name.find(Prefix) == 0;
2203
2204 if (!Fun->getReturnType()->isVoidTy() && IsAtomic)
2205 return true;
2206 }
2207 return false;
2208}
2209
2210void SPIRVEmitIntrinsics::insertSpirvDecorations(Instruction *I,
2211 IRBuilder<> &B) {
2212 if (MDNode *MD = I->getMetadata("spirv.Decorations")) {
2214 B.CreateIntrinsic(Intrinsic::spv_assign_decoration, {I->getType()},
2215 {I, MetadataAsValue::get(I->getContext(), MD)});
2216 }
2217 // Lower alias.scope/noalias metadata
2218 {
2219 auto processMemAliasingDecoration = [&](unsigned Kind) {
2220 if (MDNode *AliasListMD = I->getMetadata(Kind)) {
2221 if (shouldTryToAddMemAliasingDecoration(I)) {
2222 uint32_t Dec = Kind == LLVMContext::MD_alias_scope
2223 ? SPIRV::Decoration::AliasScopeINTEL
2224 : SPIRV::Decoration::NoAliasINTEL;
2226 I, ConstantInt::get(B.getInt32Ty(), Dec),
2227 MetadataAsValue::get(I->getContext(), AliasListMD)};
2229 B.CreateIntrinsic(Intrinsic::spv_assign_aliasing_decoration,
2230 {I->getType()}, {Args});
2231 }
2232 }
2233 };
2234 processMemAliasingDecoration(LLVMContext::MD_alias_scope);
2235 processMemAliasingDecoration(LLVMContext::MD_noalias);
2236 }
2237 // MD_fpmath
2238 if (MDNode *MD = I->getMetadata(LLVMContext::MD_fpmath)) {
2239 const SPIRVSubtarget *STI = TM->getSubtargetImpl(*I->getFunction());
2240 bool AllowFPMaxError =
2241 STI->canUseExtension(SPIRV::Extension::SPV_INTEL_fp_max_error);
2242 if (!AllowFPMaxError)
2243 return;
2244
2246 B.CreateIntrinsic(Intrinsic::spv_assign_fpmaxerror_decoration,
2247 {I->getType()},
2248 {I, MetadataAsValue::get(I->getContext(), MD)});
2249 }
2250}
2251
2252void SPIRVEmitIntrinsics::processInstrAfterVisit(Instruction *I,
2253 IRBuilder<> &B) {
2254 auto *II = dyn_cast<IntrinsicInst>(I);
2255 bool IsConstComposite =
2256 II && II->getIntrinsicID() == Intrinsic::spv_const_composite;
2257 if (IsConstComposite && TrackConstants) {
2259 auto t = AggrConsts.find(I);
2260 assert(t != AggrConsts.end());
2261 auto *NewOp =
2262 buildIntrWithMD(Intrinsic::spv_track_constant,
2263 {II->getType(), II->getType()}, t->second, I, {}, B);
2264 replaceAllUsesWith(I, NewOp, false);
2265 NewOp->setArgOperand(0, I);
2266 }
2267 bool IsPhi = isa<PHINode>(I), BPrepared = false;
2268 for (const auto &Op : I->operands()) {
2269 if (isa<PHINode>(I) || isa<SwitchInst>(I) ||
2271 continue;
2272 unsigned OpNo = Op.getOperandNo();
2273 if (II && ((II->getIntrinsicID() == Intrinsic::spv_gep && OpNo == 0) ||
2274 (II->paramHasAttr(OpNo, Attribute::ImmArg))))
2275 continue;
2276
2277 if (!BPrepared) {
2278 IsPhi ? B.SetInsertPointPastAllocas(I->getParent()->getParent())
2279 : B.SetInsertPoint(I);
2280 BPrepared = true;
2281 }
2282 Type *OpTy = Op->getType();
2283 Type *OpElemTy = GR->findDeducedElementType(Op);
2284 Value *NewOp = Op;
2285 if (OpTy->isTargetExtTy()) {
2286 // Since this value is replaced by poison, we need to do the same in
2287 // `insertAssignTypeIntrs`.
2288 Value *OpTyVal = getNormalizedPoisonValue(OpTy);
2289 NewOp = buildIntrWithMD(Intrinsic::spv_track_constant,
2290 {OpTy, OpTyVal->getType()}, Op, OpTyVal, {}, B);
2291 }
2292 if (!IsConstComposite && isPointerTy(OpTy) && OpElemTy != nullptr &&
2293 OpElemTy != IntegerType::getInt8Ty(I->getContext())) {
2294 SmallVector<Type *, 2> Types = {OpTy, OpTy};
2295 SmallVector<Value *, 2> Args = {
2296 NewOp, buildMD(getNormalizedPoisonValue(OpElemTy)),
2297 B.getInt32(getPointerAddressSpace(OpTy))};
2298 CallInst *PtrCasted =
2299 B.CreateIntrinsic(Intrinsic::spv_ptrcast, {Types}, Args);
2300 GR->buildAssignPtr(B, OpElemTy, PtrCasted);
2301 NewOp = PtrCasted;
2302 }
2303 if (NewOp != Op)
2304 I->setOperand(OpNo, NewOp);
2305 }
2306 if (Named.insert(I).second)
2307 emitAssignName(I, B);
2308}
2309
2310Type *SPIRVEmitIntrinsics::deduceFunParamElementType(Function *F,
2311 unsigned OpIdx) {
2312 std::unordered_set<Function *> FVisited;
2313 return deduceFunParamElementType(F, OpIdx, FVisited);
2314}
2315
2316Type *SPIRVEmitIntrinsics::deduceFunParamElementType(
2317 Function *F, unsigned OpIdx, std::unordered_set<Function *> &FVisited) {
2318 // maybe a cycle
2319 if (!FVisited.insert(F).second)
2320 return nullptr;
2321
2322 std::unordered_set<Value *> Visited;
2324 // search in function's call sites
2325 for (User *U : F->users()) {
2326 CallInst *CI = dyn_cast<CallInst>(U);
2327 if (!CI || OpIdx >= CI->arg_size())
2328 continue;
2329 Value *OpArg = CI->getArgOperand(OpIdx);
2330 if (!isPointerTy(OpArg->getType()))
2331 continue;
2332 // maybe we already know operand's element type
2333 if (Type *KnownTy = GR->findDeducedElementType(OpArg))
2334 return KnownTy;
2335 // try to deduce from the operand itself
2336 Visited.clear();
2337 if (Type *Ty = deduceElementTypeHelper(OpArg, Visited, false))
2338 return Ty;
2339 // search in actual parameter's users
2340 for (User *OpU : OpArg->users()) {
2342 if (!Inst || Inst == CI)
2343 continue;
2344 Visited.clear();
2345 if (Type *Ty = deduceElementTypeHelper(Inst, Visited, false))
2346 return Ty;
2347 }
2348 // check if it's a formal parameter of the outer function
2349 if (!CI->getParent() || !CI->getParent()->getParent())
2350 continue;
2351 Function *OuterF = CI->getParent()->getParent();
2352 if (FVisited.find(OuterF) != FVisited.end())
2353 continue;
2354 for (unsigned i = 0; i < OuterF->arg_size(); ++i) {
2355 if (OuterF->getArg(i) == OpArg) {
2356 Lookup.push_back(std::make_pair(OuterF, i));
2357 break;
2358 }
2359 }
2360 }
2361
2362 // search in function parameters
2363 for (auto &Pair : Lookup) {
2364 if (Type *Ty = deduceFunParamElementType(Pair.first, Pair.second, FVisited))
2365 return Ty;
2366 }
2367
2368 return nullptr;
2369}
2370
2371void SPIRVEmitIntrinsics::processParamTypesByFunHeader(Function *F,
2372 IRBuilder<> &B) {
2373 B.SetInsertPointPastAllocas(F);
2374 for (unsigned OpIdx = 0; OpIdx < F->arg_size(); ++OpIdx) {
2375 Argument *Arg = F->getArg(OpIdx);
2376 if (!isUntypedPointerTy(Arg->getType()))
2377 continue;
2378 Type *ElemTy = GR->findDeducedElementType(Arg);
2379 if (ElemTy)
2380 continue;
2381 if (hasPointeeTypeAttr(Arg) &&
2382 (ElemTy = getPointeeTypeByAttr(Arg)) != nullptr) {
2383 GR->buildAssignPtr(B, ElemTy, Arg);
2384 continue;
2385 }
2386 // search in function's call sites
2387 for (User *U : F->users()) {
2388 CallInst *CI = dyn_cast<CallInst>(U);
2389 if (!CI || OpIdx >= CI->arg_size())
2390 continue;
2391 Value *OpArg = CI->getArgOperand(OpIdx);
2392 if (!isPointerTy(OpArg->getType()))
2393 continue;
2394 // maybe we already know operand's element type
2395 if ((ElemTy = GR->findDeducedElementType(OpArg)) != nullptr)
2396 break;
2397 }
2398 if (ElemTy) {
2399 GR->buildAssignPtr(B, ElemTy, Arg);
2400 continue;
2401 }
2402 if (HaveFunPtrs) {
2403 for (User *U : Arg->users()) {
2404 CallInst *CI = dyn_cast<CallInst>(U);
2405 if (CI && !isa<IntrinsicInst>(CI) && CI->isIndirectCall() &&
2406 CI->getCalledOperand() == Arg &&
2407 CI->getParent()->getParent() == CurrF) {
2409 deduceOperandElementTypeFunctionPointer(CI, Ops, ElemTy, false);
2410 if (ElemTy) {
2411 GR->buildAssignPtr(B, ElemTy, Arg);
2412 break;
2413 }
2414 }
2415 }
2416 }
2417 }
2418}
2419
2420void SPIRVEmitIntrinsics::processParamTypes(Function *F, IRBuilder<> &B) {
2421 B.SetInsertPointPastAllocas(F);
2422 for (unsigned OpIdx = 0; OpIdx < F->arg_size(); ++OpIdx) {
2423 Argument *Arg = F->getArg(OpIdx);
2424 if (!isUntypedPointerTy(Arg->getType()))
2425 continue;
2426 Type *ElemTy = GR->findDeducedElementType(Arg);
2427 if (!ElemTy && (ElemTy = deduceFunParamElementType(F, OpIdx)) != nullptr) {
2428 if (CallInst *AssignCI = GR->findAssignPtrTypeInstr(Arg)) {
2429 DenseSet<std::pair<Value *, Value *>> VisitedSubst;
2430 GR->updateAssignType(AssignCI, Arg, getNormalizedPoisonValue(ElemTy));
2431 propagateElemType(Arg, IntegerType::getInt8Ty(F->getContext()),
2432 VisitedSubst);
2433 } else {
2434 GR->buildAssignPtr(B, ElemTy, Arg);
2435 }
2436 }
2437 }
2438}
2439
2441 SPIRVGlobalRegistry *GR) {
2442 FunctionType *FTy = F->getFunctionType();
2443 bool IsNewFTy = false;
2445 for (Argument &Arg : F->args()) {
2446 Type *ArgTy = Arg.getType();
2447 if (ArgTy->isPointerTy())
2448 if (Type *ElemTy = GR->findDeducedElementType(&Arg)) {
2449 IsNewFTy = true;
2450 ArgTy = getTypedPointerWrapper(ElemTy, getPointerAddressSpace(ArgTy));
2451 }
2452 ArgTys.push_back(ArgTy);
2453 }
2454 return IsNewFTy
2455 ? FunctionType::get(FTy->getReturnType(), ArgTys, FTy->isVarArg())
2456 : FTy;
2457}
2458
2459bool SPIRVEmitIntrinsics::processFunctionPointers(Module &M) {
2460 SmallVector<Function *> Worklist;
2461 for (auto &F : M) {
2462 if (F.isIntrinsic())
2463 continue;
2464 if (F.isDeclaration()) {
2465 for (User *U : F.users()) {
2466 CallInst *CI = dyn_cast<CallInst>(U);
2467 if (!CI || CI->getCalledFunction() != &F) {
2468 Worklist.push_back(&F);
2469 break;
2470 }
2471 }
2472 } else {
2473 if (F.user_empty())
2474 continue;
2475 Type *FPElemTy = GR->findDeducedElementType(&F);
2476 if (!FPElemTy)
2477 FPElemTy = getFunctionPointerElemType(&F, GR);
2478 for (User *U : F.users()) {
2479 IntrinsicInst *II = dyn_cast<IntrinsicInst>(U);
2480 if (!II || II->arg_size() != 3 || II->getOperand(0) != &F)
2481 continue;
2482 if (II->getIntrinsicID() == Intrinsic::spv_assign_ptr_type ||
2483 II->getIntrinsicID() == Intrinsic::spv_ptrcast) {
2485 break;
2486 }
2487 }
2488 }
2489 }
2490 if (Worklist.empty())
2491 return false;
2492
2493 std::string ServiceFunName = SPIRV_BACKEND_SERVICE_FUN_NAME;
2494 if (!getVacantFunctionName(M, ServiceFunName))
2496 "cannot allocate a name for the internal service function");
2497 LLVMContext &Ctx = M.getContext();
2498 Function *SF =
2499 Function::Create(FunctionType::get(Type::getVoidTy(Ctx), {}, false),
2500 GlobalValue::PrivateLinkage, ServiceFunName, M);
2502 BasicBlock *BB = BasicBlock::Create(Ctx, "entry", SF);
2503 IRBuilder<> IRB(BB);
2504
2505 for (Function *F : Worklist) {
2507 for (const auto &Arg : F->args())
2508 Args.push_back(getNormalizedPoisonValue(Arg.getType()));
2509 IRB.CreateCall(F, Args);
2510 }
2511 IRB.CreateRetVoid();
2512
2513 return true;
2514}
2515
2516// Apply types parsed from demangled function declarations.
2517void SPIRVEmitIntrinsics::applyDemangledPtrArgTypes(IRBuilder<> &B) {
2518 DenseMap<Function *, CallInst *> Ptrcasts;
2519 for (auto It : FDeclPtrTys) {
2520 Function *F = It.first;
2521 for (auto *U : F->users()) {
2522 CallInst *CI = dyn_cast<CallInst>(U);
2523 if (!CI || CI->getCalledFunction() != F)
2524 continue;
2525 unsigned Sz = CI->arg_size();
2526 for (auto [Idx, ElemTy] : It.second) {
2527 if (Idx >= Sz)
2528 continue;
2529 Value *Param = CI->getArgOperand(Idx);
2530 if (GR->findDeducedElementType(Param) || isa<GlobalValue>(Param))
2531 continue;
2532 if (Argument *Arg = dyn_cast<Argument>(Param)) {
2533 if (!hasPointeeTypeAttr(Arg)) {
2534 B.SetInsertPointPastAllocas(Arg->getParent());
2535 B.SetCurrentDebugLocation(DebugLoc());
2536 GR->buildAssignPtr(B, ElemTy, Arg);
2537 }
2538 } else if (isa<GetElementPtrInst>(Param)) {
2539 replaceUsesOfWithSpvPtrcast(Param, normalizeType(ElemTy), CI,
2540 Ptrcasts);
2541 } else if (isa<Instruction>(Param)) {
2542 GR->addDeducedElementType(Param, normalizeType(ElemTy));
2543 // insertAssignTypeIntrs() will complete buildAssignPtr()
2544 } else {
2545 B.SetInsertPoint(CI->getParent()
2546 ->getParent()
2547 ->getEntryBlock()
2548 .getFirstNonPHIOrDbgOrAlloca());
2549 GR->buildAssignPtr(B, ElemTy, Param);
2550 }
2551 CallInst *Ref = dyn_cast<CallInst>(Param);
2552 if (!Ref)
2553 continue;
2554 Function *RefF = Ref->getCalledFunction();
2555 if (!RefF || !isPointerTy(RefF->getReturnType()) ||
2556 GR->findDeducedElementType(RefF))
2557 continue;
2558 ElemTy = normalizeType(ElemTy);
2559 GR->addDeducedElementType(RefF, ElemTy);
2560 GR->addReturnType(
2562 ElemTy, getPointerAddressSpace(RefF->getReturnType())));
2563 }
2564 }
2565 }
2566}
2567
2568GetElementPtrInst *
2569SPIRVEmitIntrinsics::simplifyZeroLengthArrayGepInst(GetElementPtrInst *GEP) {
2570 // getelementptr [0 x T], P, 0 (zero), I -> getelementptr T, P, I.
2571 // If type is 0-length array and first index is 0 (zero), drop both the
2572 // 0-length array type and the first index. This is a common pattern in the
2573 // IR, e.g. when using a zero-length array as a placeholder for a flexible
2574 // array such as unbound arrays.
2575 assert(GEP && "GEP is null");
2576 Type *SrcTy = GEP->getSourceElementType();
2577 SmallVector<Value *, 8> Indices(GEP->indices());
2578 ArrayType *ArrTy = dyn_cast<ArrayType>(SrcTy);
2579 if (ArrTy && ArrTy->getNumElements() == 0 &&
2581 IRBuilder<> Builder(GEP);
2582 Indices.erase(Indices.begin());
2583 SrcTy = ArrTy->getElementType();
2584 Value *NewGEP = Builder.CreateGEP(SrcTy, GEP->getPointerOperand(), Indices,
2585 "", GEP->getNoWrapFlags());
2586 assert(llvm::isa<GetElementPtrInst>(NewGEP) && "NewGEP should be a GEP");
2587 return cast<GetElementPtrInst>(NewGEP);
2588 }
2589 return nullptr;
2590}
2591
2592bool SPIRVEmitIntrinsics::runOnFunction(Function &Func) {
2593 if (Func.isDeclaration())
2594 return false;
2595
2596 const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(Func);
2597 GR = ST.getSPIRVGlobalRegistry();
2598
2599 if (!CurrF)
2600 HaveFunPtrs =
2601 ST.canUseExtension(SPIRV::Extension::SPV_INTEL_function_pointers);
2602
2603 CurrF = &Func;
2604 IRBuilder<> B(Func.getContext());
2605 AggrConsts.clear();
2606 AggrConstTypes.clear();
2607 AggrStores.clear();
2608
2609 // Fix GEP result types ahead of inference, and simplify if possible.
2610 // Data structure for dead instructions that were simplified and replaced.
2611 SmallPtrSet<Instruction *, 4> DeadInsts;
2612 for (auto &I : instructions(Func)) {
2614 if (!Ref || GR->findDeducedElementType(Ref))
2615 continue;
2616
2617 GetElementPtrInst *NewGEP = simplifyZeroLengthArrayGepInst(Ref);
2618 if (NewGEP) {
2619 Ref->replaceAllUsesWith(NewGEP);
2621 DeadInsts.insert(Ref);
2622
2623 Ref = NewGEP;
2624 }
2625 if (Type *GepTy = getGEPType(Ref))
2626 GR->addDeducedElementType(Ref, normalizeType(GepTy));
2627 }
2628 // Remove dead instructions that were simplified and replaced.
2629 for (auto *I : DeadInsts) {
2630 assert(I->use_empty() && "Dead instruction should not have any uses left");
2631 I->eraseFromParent();
2632 }
2633
2634 processParamTypesByFunHeader(CurrF, B);
2635
2636 // StoreInst's operand type can be changed during the next transformations,
2637 // so we need to store it in the set. Also store already transformed types.
2638 for (auto &I : instructions(Func)) {
2639 StoreInst *SI = dyn_cast<StoreInst>(&I);
2640 if (!SI)
2641 continue;
2642 Type *ElTy = SI->getValueOperand()->getType();
2643 if (ElTy->isAggregateType() || ElTy->isVectorTy())
2644 AggrStores.insert(&I);
2645 }
2646
2647 B.SetInsertPoint(&Func.getEntryBlock(), Func.getEntryBlock().begin());
2648 for (auto &GV : Func.getParent()->globals())
2649 processGlobalValue(GV, B);
2650
2651 preprocessUndefs(B);
2652 preprocessCompositeConstants(B);
2655
2656 applyDemangledPtrArgTypes(B);
2657
2658 // Pass forward: use operand to deduce instructions result.
2659 for (auto &I : Worklist) {
2660 // Don't emit intrinsincs for convergence intrinsics.
2661 if (isConvergenceIntrinsic(I))
2662 continue;
2663
2664 bool Postpone = insertAssignPtrTypeIntrs(I, B, false);
2665 // if Postpone is true, we can't decide on pointee type yet
2666 insertAssignTypeIntrs(I, B);
2667 insertPtrCastOrAssignTypeInstr(I, B);
2669 // if instruction requires a pointee type set, let's check if we know it
2670 // already, and force it to be i8 if not
2671 if (Postpone && !GR->findAssignPtrTypeInstr(I))
2672 insertAssignPtrTypeIntrs(I, B, true);
2673
2674 if (auto *FPI = dyn_cast<ConstrainedFPIntrinsic>(I))
2675 useRoundingMode(FPI, B);
2676 }
2677
2678 // Pass backward: use instructions results to specify/update/cast operands
2679 // where needed.
2680 SmallPtrSet<Instruction *, 4> IncompleteRets;
2681 for (auto &I : llvm::reverse(instructions(Func)))
2682 deduceOperandElementType(&I, &IncompleteRets);
2683
2684 // Pass forward for PHIs only, their operands are not preceed the instruction
2685 // in meaning of `instructions(Func)`.
2686 for (BasicBlock &BB : Func)
2687 for (PHINode &Phi : BB.phis())
2688 if (isPointerTy(Phi.getType()))
2689 deduceOperandElementType(&Phi, nullptr);
2690
2691 for (auto *I : Worklist) {
2692 TrackConstants = true;
2693 if (!I->getType()->isVoidTy() || isa<StoreInst>(I))
2695 // Visitors return either the original/newly created instruction for further
2696 // processing, nullptr otherwise.
2697 I = visit(*I);
2698 if (!I)
2699 continue;
2700
2701 // Don't emit intrinsics for convergence operations.
2702 if (isConvergenceIntrinsic(I))
2703 continue;
2704
2706 processInstrAfterVisit(I, B);
2707 }
2708
2709 return true;
2710}
2711
2712// Try to deduce a better type for pointers to untyped ptr.
2713bool SPIRVEmitIntrinsics::postprocessTypes(Module &M) {
2714 if (!GR || TodoTypeSz == 0)
2715 return false;
2716
2717 unsigned SzTodo = TodoTypeSz;
2718 DenseMap<Value *, SmallPtrSet<Value *, 4>> ToProcess;
2719 for (auto [Op, Enabled] : TodoType) {
2720 // TODO: add isa<CallInst>(Op) to continue
2722 continue;
2723 CallInst *AssignCI = GR->findAssignPtrTypeInstr(Op);
2724 Type *KnownTy = GR->findDeducedElementType(Op);
2725 if (!KnownTy || !AssignCI)
2726 continue;
2727 assert(Op == AssignCI->getArgOperand(0));
2728 // Try to improve the type deduced after all Functions are processed.
2729 if (auto *CI = dyn_cast<Instruction>(Op)) {
2730 CurrF = CI->getParent()->getParent();
2731 std::unordered_set<Value *> Visited;
2732 if (Type *ElemTy = deduceElementTypeHelper(Op, Visited, false, true)) {
2733 if (ElemTy != KnownTy) {
2734 DenseSet<std::pair<Value *, Value *>> VisitedSubst;
2735 propagateElemType(CI, ElemTy, VisitedSubst);
2736 eraseTodoType(Op);
2737 continue;
2738 }
2739 }
2740 }
2741
2742 if (Op->hasUseList()) {
2743 for (User *U : Op->users()) {
2745 if (Inst && !isa<IntrinsicInst>(Inst))
2746 ToProcess[Inst].insert(Op);
2747 }
2748 }
2749 }
2750 if (TodoTypeSz == 0)
2751 return true;
2752
2753 for (auto &F : M) {
2754 CurrF = &F;
2755 SmallPtrSet<Instruction *, 4> IncompleteRets;
2756 for (auto &I : llvm::reverse(instructions(F))) {
2757 auto It = ToProcess.find(&I);
2758 if (It == ToProcess.end())
2759 continue;
2760 It->second.remove_if([this](Value *V) { return !isTodoType(V); });
2761 if (It->second.size() == 0)
2762 continue;
2763 deduceOperandElementType(&I, &IncompleteRets, &It->second, true);
2764 if (TodoTypeSz == 0)
2765 return true;
2766 }
2767 }
2768
2769 return SzTodo > TodoTypeSz;
2770}
2771
2772// Parse and store argument types of function declarations where needed.
2773void SPIRVEmitIntrinsics::parseFunDeclarations(Module &M) {
2774 for (auto &F : M) {
2775 if (!F.isDeclaration() || F.isIntrinsic())
2776 continue;
2777 // get the demangled name
2778 std::string DemangledName = getOclOrSpirvBuiltinDemangledName(F.getName());
2779 if (DemangledName.empty())
2780 continue;
2781 // allow only OpGroupAsyncCopy use case at the moment
2782 const SPIRVSubtarget &ST = TM->getSubtarget<SPIRVSubtarget>(F);
2783 auto [Grp, Opcode, ExtNo] = SPIRV::mapBuiltinToOpcode(
2784 DemangledName, ST.getPreferredInstructionSet());
2785 if (Opcode != SPIRV::OpGroupAsyncCopy)
2786 continue;
2787 // find pointer arguments
2788 SmallVector<unsigned> Idxs;
2789 for (unsigned OpIdx = 0; OpIdx < F.arg_size(); ++OpIdx) {
2790 Argument *Arg = F.getArg(OpIdx);
2791 if (isPointerTy(Arg->getType()) && !hasPointeeTypeAttr(Arg))
2792 Idxs.push_back(OpIdx);
2793 }
2794 if (!Idxs.size())
2795 continue;
2796 // parse function arguments
2797 LLVMContext &Ctx = F.getContext();
2799 SPIRV::parseBuiltinTypeStr(TypeStrs, DemangledName, Ctx);
2800 if (!TypeStrs.size())
2801 continue;
2802 // find type info for pointer arguments
2803 for (unsigned Idx : Idxs) {
2804 if (Idx >= TypeStrs.size())
2805 continue;
2806 if (Type *ElemTy =
2807 SPIRV::parseBuiltinCallArgumentType(TypeStrs[Idx].trim(), Ctx))
2809 !ElemTy->isTargetExtTy())
2810 FDeclPtrTys[&F].push_back(std::make_pair(Idx, ElemTy));
2811 }
2812 }
2813}
2814
2815bool SPIRVEmitIntrinsics::runOnModule(Module &M) {
2816 bool Changed = false;
2817
2818 parseFunDeclarations(M);
2819
2820 TodoType.clear();
2821 for (auto &F : M)
2823
2824 // Specify function parameters after all functions were processed.
2825 for (auto &F : M) {
2826 // check if function parameter types are set
2827 CurrF = &F;
2828 if (!F.isDeclaration() && !F.isIntrinsic()) {
2829 IRBuilder<> B(F.getContext());
2830 processParamTypes(&F, B);
2831 }
2832 }
2833
2834 CanTodoType = false;
2835 Changed |= postprocessTypes(M);
2836
2837 if (HaveFunPtrs)
2838 Changed |= processFunctionPointers(M);
2839
2840 return Changed;
2841}
2842
2844 return new SPIRVEmitIntrinsics(TM);
2845}
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.
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:55
#define I(x, y, z)
Definition MD5.cpp:58
Machine Check Debug Module
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 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 bool IsKernelArgInt8(Function *F, StoreInst *SI)
static void addSaturatedDecorationToIntrinsic(Instruction *I, IRBuilder<> &B)
static void setInsertPointSkippingPhis(IRBuilder<> &B, Instruction *I)
static FunctionType * getFunctionPointerElemType(Function *F, SPIRVGlobalRegistry *GR)
static void createSaturatedConversionDecoration(Instruction *I, IRBuilder<> &B)
static Type * restoreMutatedType(SPIRVGlobalRegistry *GR, Instruction *I, Type *Ty)
static bool requireAssignType(Instruction *I)
void visit(MachineFunction &MF, MachineBasicBlock &Start, std::function< void(MachineBasicBlock *)> op)
static void insertSpirvDecorations(MachineFunction &MF, SPIRVGlobalRegistry *GR, MachineIRBuilder MIB)
#define SPIRV_BACKEND_SERVICE_FUN_NAME
Definition SPIRVUtils.h:452
static bool Enabled
Definition Statistic.cpp:46
DEMANGLE_NAMESPACE_BEGIN bool starts_with(std::string_view self, char C) noexcept
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
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:528
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.
static LLVM_ABI BlockAddress * get(Function *F, BasicBlock *BB)
Return a BlockAddress for the specified function and basic block.
bool isInlineAsm() const
Check if this call is an inline asm statement.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
LLVM_ABI bool isIndirectCall() const
Return true if the callsite is an indirect call.
Value * getCalledOperand() const
Value * getArgOperand(unsigned i) const
FunctionType * getFunctionType() const
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:535
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:163
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:167
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
Definition DenseMap.h:237
iterator end()
Definition DenseMap.h:81
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Definition DenseMap.h:222
static LLVM_ABI FunctionType * get(Type *Result, ArrayRef< Type * > Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition Function.cpp:637
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
Definition Function.h:166
const DataLayout & getDataLayout() const
Get the data layout of the module this function belongs to.
Definition Function.cpp:363
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition Function.h:244
bool isIntrinsic() const
isIntrinsic - Returns true if the function's name starts with "llvm.".
Definition Function.h:249
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
Definition Function.cpp:359
size_t arg_size() const
Definition Function.h:899
Type * getReturnType() const
Returns the type of the ret val.
Definition Function.h:214
Argument * getArg(unsigned i) const
Definition Function.h:884
static LLVM_ABI Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
static unsigned getPointerOperandIndex()
PointerType * getType() const
Global values are always pointers.
@ PrivateLinkage
Like Internal, but omit from symbol table.
Definition GlobalValue.h:61
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:2780
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:1077
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1561
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:607
Flags
Flags values. These may be or'd together.
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:103
Metadata * getMetadata() const
Definition Metadata.h:200
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:255
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 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:261
static LLVM_ABI StructType * create(LLVMContext &Context, StringRef Name)
This creates an identified struct.
Definition Type.cpp:620
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:908
Type * getTypeParameter(unsigned i) const
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:297
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:295
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
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:292
LLVM_ABI bool replaceUsesOfWith(Value *From, Value *To)
Replace uses of one Value with another.
Definition User.cpp:21
void setOperand(unsigned i, Value *Val)
Definition User.h:237
Value * getOperand(unsigned i) const
Definition User.h:232
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:256
LLVM_ABI void setName(const Twine &Name)
Change the name of the value.
Definition Value.cpp:390
iterator_range< user_iterator > users()
Definition Value.h:426
bool use_empty() const
Definition Value.h:346
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:389
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.
@ 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.
DenseSetImpl< ValueT, DenseMap< ValueT, DenseSetEmpty, ValueInfoT, DenseSetPair< ValueT > >, ValueInfoT > DenseSet
Definition DenseSet.h:264
ElementType
The element type of an SRV or UAV resource.
Definition DXILABI.h:60
@ User
could "use" a pointer
NodeAddr< PhiNode * > Phi
Definition RDFGraph.h:390
NodeAddr< FuncNode * > Func
Definition RDFGraph.h:393
friend class Instruction
Iterator for Instructions in a `BasicBlock.
Definition BasicBlock.h:73
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Definition STLExtras.h:318
bool getVacantFunctionName(Module &M, std::string &Name)
@ Offset
Definition DWP.cpp:477
FunctionAddr VTableAddr Value
Definition InstrProf.h:137
bool isTypedPointerWrapper(const TargetExtType *ExtTy)
Definition SPIRVUtils.h:330
ModulePass * createSPIRVEmitIntrinsicsPass(SPIRVTargetMachine *TM)
unsigned getPointerAddressSpace(const Type *T)
Definition SPIRVUtils.h:294
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:649
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:2116
FPDecorationId
Definition SPIRVUtils.h:476
bool isNestedPointer(const Type *Ty)
MetadataAsValue * buildMD(Value *Arg)
Definition SPIRVUtils.h:440
std::string getOclOrSpirvBuiltinDemangledName(StringRef Name)
LLVM_ABI bool isInstructionTriviallyDead(Instruction *I, const TargetLibraryInfo *TLI=nullptr)
Return true if the result produced by the instruction is not used, and the instruction will return.
Definition Local.cpp:402
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
Type * getTypedPointerWrapper(Type *ElemTy, unsigned AS)
Definition SPIRVUtils.h:325
bool isVector1(Type *Ty)
Definition SPIRVUtils.h:418
bool isPointerTy(const Type *T)
Definition SPIRVUtils.h:288
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
Definition Error.cpp:167
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:548
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:307
bool hasPointeeTypeAttr(Argument *Arg)
Definition SPIRVUtils.h:302
bool isEquivalentTypes(Type *Ty1, Type *Ty2)
Definition SPIRVUtils.h:380
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:565
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:363
bool hasInitializer(const GlobalVariable *GV)
Definition SPIRVUtils.h:273
Type * normalizeType(Type *Ty)
Definition SPIRVUtils.h:426
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
Type * getPointeeType(const Type *Ty)
Definition SPIRVUtils.h:357
PoisonValue * getNormalizedPoisonValue(Type *Ty)
Definition SPIRVUtils.h:436
bool isUntypedPointerTy(const Type *T)
Definition SPIRVUtils.h:283
SPIRV::MemorySemantics::MemorySemantics getMemSemantics(AtomicOrdering Ord)