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