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