LLVM 24.0.0git
SPIRVLowerResourceNames.cpp
Go to the documentation of this file.
1//===- SPIRVLowerResourceNames.cpp - Lower resource names to metadata -----===//
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#include "SPIRV.h"
10#include "llvm/ADT/MapVector.h"
14#include "llvm/IR/Dominators.h"
17#include "llvm/IR/IntrinsicsSPIRV.h"
18#include "llvm/IR/Module.h"
19#include "llvm/Pass.h"
22
23using namespace llvm;
24
25#define DEBUG_TYPE "spirv-lower-resource-names"
26
30 for (CallInst *CI : Intrinsics) {
31 auto *LI = dyn_cast<LoadInst>(CI->getArgOperand(4));
32 if (!LI)
33 continue;
34 auto *AI = dyn_cast<AllocaInst>(LI->getPointerOperand());
35 if (AI && !Seen.insert(AI).second)
36 continue;
37 if (AI && AI->isStaticAlloca() && isAllocaPromotable(AI))
38 Allocas[CI->getFunction()].push_back(AI);
39 else
40 reportFatalInternalError("unresolvable SPIR-V resource name");
41 }
42 for (auto &[F, Names] : Allocas) {
43 DominatorTree DT(*F);
44 PromoteMemToReg(Names, DT);
45 }
46}
47
48static bool lowerResourceNames(Module &M) {
50 for (Function &F : M) {
51 switch (F.getIntrinsicID()) {
52 case Intrinsic::spv_resource_handlefrombinding:
53 case Intrinsic::spv_resource_handlefromimplicitbinding:
54 for (User *U : F.users()) {
55 if (auto *CI = dyn_cast<CallInst>(U))
56 Intrinsics.push_back(CI);
57 }
58 break;
59 default:
60 break;
61 }
62 }
63 if (Intrinsics.empty())
64 return false;
65
66 promoteNameAllocas(Intrinsics);
67
69 for (CallInst *CI : Intrinsics) {
70 Function *F = CI->getCalledFunction();
71 Intrinsic::ID MdID =
72 F->getIntrinsicID() == Intrinsic::spv_resource_handlefrombinding
73 ? Intrinsic::spv_resource_handlefrombinding_mdname
74 : Intrinsic::spv_resource_handlefromimplicitbinding_mdname;
75 Function *MdFn =
76 Intrinsic::getOrInsertDeclaration(&M, MdID, {F->getReturnType()});
77 Value *NamePtr = CI->getArgOperand(4);
78 StringRef Name;
79 if (!isa<ConstantPointerNull>(NamePtr) &&
80 !getConstantStringInfo(NamePtr, Name))
81 reportFatalUsageError("expected a constant resource name");
82 if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(NamePtr)))
83 Names.insert(GV);
84 LLVMContext &Ctx = M.getContext();
85 auto *NameMD = MDNode::get(Ctx, MDString::get(Ctx, Name));
86 CI->setCalledFunction(MdFn);
87 CI->setArgOperand(4, MetadataAsValue::get(Ctx, NameMD));
88 CI->setAttributes(CI->getAttributes().removeParamAttributes(Ctx, 4));
89 }
90 for (GlobalVariable *GV : Names) {
91 GV->removeDeadConstantUsers();
92 if (GV->hasLocalLinkage() && GV->use_empty())
93 GV->eraseFromParent();
94 }
95 return true;
96}
97
103
104namespace {
105class SPIRVLowerResourceNamesLegacy : public ModulePass {
106public:
107 static char ID;
108 SPIRVLowerResourceNamesLegacy() : ModulePass(ID) {}
109 bool runOnModule(Module &M) override { return lowerResourceNames(M); }
110};
111char SPIRVLowerResourceNamesLegacy::ID = 0;
112} // namespace
113
114INITIALIZE_PASS(SPIRVLowerResourceNamesLegacy, DEBUG_TYPE,
115 "SPIRV Lower Resource Names", false, false)
116
118 return new SPIRVLowerResourceNamesLegacy();
119}
#define DEBUG_TYPE
Hexagon Lower HLX Pair Allocas
Module.h This file contains the declarations for the Module class.
#define F(x, y, z)
Definition MD5.cpp:54
This file implements a map that provides insertion order iteration.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition PassSupport.h:56
static void promoteNameAllocas(ArrayRef< CallInst * > Intrinsics)
static bool lowerResourceNames(Module &M)
This file defines the SmallPtrSet class.
This file defines the SmallVector class.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This class represents a function call, abstracting a target machine's calling convention.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
Definition Dominators.h:122
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition Metadata.h:1588
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
Definition Metadata.cpp:595
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:39
static LLVM_ABI MetadataAsValue * get(LLVMContext &Context, Metadata *MD)
Definition Metadata.cpp:105
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition Pass.h:253
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
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)
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
LLVM Value Representation.
Definition Value.h:75
LLVM_ABI Function * getOrInsertDeclaration(Module *M, ID id, ArrayRef< Type * > OverloadTys={})
Look up the Function declaration of the intrinsic id in the Module M.
This is an optimization pass for GlobalISel generic memory operations.
ModulePass * createSPIRVLowerResourceNamesLegacyPass()
LLVM_ABI void PromoteMemToReg(ArrayRef< AllocaInst * > Allocas, DominatorTree &DT, AssumptionCache *AC=nullptr)
Promote the specified list of alloca instructions into scalar registers, inserting PHI nodes as appro...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
LLVM_ABI bool getConstantStringInfo(const Value *V, StringRef &Str, bool TrimAtNul=true)
This function computes the length of a null-terminated C string pointed to by V.
LLVM_ABI void reportFatalInternalError(Error Err)
Report a fatal error that indicates a bug in LLVM.
Definition Error.cpp:173
LLVM_ABI bool isAllocaPromotable(const AllocaInst *AI)
Return true if this alloca is legal for promotion.
LLVM_ABI const Value * getUnderlyingObject(const Value *V, unsigned MaxLookup=MaxLookupSearchDepth, bool MustPreserveProvenance=false)
This method strips off any GEP address adjustments, pointer casts or llvm.threadlocal....
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
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
Definition Error.cpp:177