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"
11#include "llvm/ADT/STLExtras.h"
15#include "llvm/IR/Dominators.h"
19#include "llvm/IR/IntrinsicsSPIRV.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Pass.h"
24
25using namespace llvm;
26
27#define DEBUG_TYPE "spirv-lower-resource-names"
28
32 for (CallInst *CI : Intrinsics) {
33 auto *LI = dyn_cast<LoadInst>(CI->getArgOperand(4));
34 if (!LI)
35 continue;
36 auto *AI = dyn_cast<AllocaInst>(LI->getPointerOperand());
37 if (AI && !Seen.insert(AI).second)
38 continue;
39 if (AI && AI->isStaticAlloca() && isAllocaPromotable(AI))
40 Allocas[CI->getFunction()].push_back(AI);
41 else
42 reportFatalInternalError("unresolvable SPIR-V resource name");
43 }
44 for (auto &[F, Names] : Allocas) {
45 DominatorTree DT(*F);
46 PromoteMemToReg(Names, DT);
47 }
48}
49
50static bool lowerResourceNames(Module &M) {
52 for (Function &F : M) {
53 switch (F.getIntrinsicID()) {
54 case Intrinsic::spv_resource_handlefrombinding:
55 case Intrinsic::spv_resource_handlefromimplicitbinding:
56 for (User *U : F.users()) {
57 if (auto *CI = dyn_cast<CallInst>(U))
58 Intrinsics.push_back(CI);
59 }
60 break;
61 default:
62 break;
63 }
64 }
65 if (Intrinsics.empty())
66 return false;
67
68 promoteNameAllocas(Intrinsics);
69
71 for (CallInst *CI : Intrinsics) {
72 Function *F = CI->getCalledFunction();
73 Intrinsic::ID MdID =
74 F->getIntrinsicID() == Intrinsic::spv_resource_handlefrombinding
75 ? Intrinsic::spv_resource_handlefrombinding_mdname
76 : Intrinsic::spv_resource_handlefromimplicitbinding_mdname;
77 Function *MdFn =
78 Intrinsic::getOrInsertDeclaration(&M, MdID, {F->getReturnType()});
79 Value *NamePtr = CI->getArgOperand(4);
80 StringRef Name;
81 if (!isa<ConstantPointerNull>(NamePtr) &&
82 !getConstantStringInfo(NamePtr, Name))
83 reportFatalUsageError("expected a constant resource name");
84 if (auto *GV = dyn_cast<GlobalVariable>(getUnderlyingObject(NamePtr)))
85 Names.insert(GV);
86 LLVMContext &Ctx = M.getContext();
87 auto *NameMD = MDNode::get(Ctx, MDString::get(Ctx, Name));
88 CI->setCalledFunction(MdFn);
89 CI->setArgOperand(4, MetadataAsValue::get(Ctx, NameMD));
90 CI->setAttributes(CI->getAttributes().removeParamAttributes(Ctx, 4));
91 }
92 for (GlobalVariable *GV : Names) {
93 GV->removeDeadConstantUsers();
94 if (GV->hasLocalLinkage() && GV->use_empty())
95 GV->eraseFromParent();
96 }
97 return true;
98}
99
105
106namespace {
107class SPIRVLowerResourceNamesLegacy : public ModulePass {
108public:
109 static char ID;
110 SPIRVLowerResourceNamesLegacy() : ModulePass(ID) {}
111 bool runOnModule(Module &M) override { return lowerResourceNames(M); }
112};
113char SPIRVLowerResourceNamesLegacy::ID = 0;
114} // namespace
115
116INITIALIZE_PASS(SPIRVLowerResourceNamesLegacy, DEBUG_TYPE,
117 "SPIRV Lower Resource Names", false, false)
118
120 return new SPIRVLowerResourceNamesLegacy();
121}
#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 contains some templates that are useful if you are working with the STL at all.
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