21 #define DEBUG_TYPE "amdgpu-annotate-kernel-features"
27 class AMDGPUAnnotateKernelFeatures :
public ModulePass {
29 static bool hasAddrSpaceCast(
const Function &
F);
38 bool runOnModule(
Module &M)
override;
40 return "AMDGPU Annotate Kernel Features";
49 static bool visitConstantExprsRecursively(
61 "Add AMDGPU function attributes",
false,
false)
73 bool AMDGPUAnnotateKernelFeatures::visitConstantExpr(
const ConstantExpr *CE) {
74 if (CE->
getOpcode() == Instruction::AddrSpaceCast) {
82 bool AMDGPUAnnotateKernelFeatures::visitConstantExprsRecursively(
86 if (!ConstantExprVisited.
insert(EntryC).second)
92 while (!Stack.
empty()) {
96 if (
const auto *CE = dyn_cast<ConstantExpr>(C)) {
97 if (visitConstantExpr(CE))
107 if (!ConstantExprVisited.
insert(OpC).second)
118 bool AMDGPUAnnotateKernelFeatures::hasAddrSpaceCast(
const Function &
F) {
128 for (
const Use &U :
I.operands()) {
133 if (visitConstantExprsRecursively(OpC, ConstantExprVisited))
142 void AMDGPUAnnotateKernelFeatures::addAttrToCallers(
Function *Intrin,
151 if (SeenFuncs.
insert(CallingFunction).second)
156 bool AMDGPUAnnotateKernelFeatures::addAttrsForIntrinsics(
159 bool Changed =
false;
161 for (
const StringRef *Arr : IntrinsicToAttr) {
163 addAttrToCallers(Fn, Arr[1]);
171 bool AMDGPUAnnotateKernelFeatures::runOnModule(
Module &M) {
174 static const StringRef IntrinsicToAttr[][2] = {
176 {
"llvm.amdgcn.workitem.id.y",
"amdgpu-work-item-id-y" },
177 {
"llvm.amdgcn.workitem.id.z",
"amdgpu-work-item-id-z" },
179 {
"llvm.amdgcn.workgroup.id.y",
"amdgpu-work-group-id-y" },
180 {
"llvm.amdgcn.workgroup.id.z",
"amdgpu-work-group-id-z" },
182 {
"llvm.r600.read.tgid.y",
"amdgpu-work-group-id-y" },
183 {
"llvm.r600.read.tgid.z",
"amdgpu-work-group-id-z" },
186 {
"llvm.r600.read.tidig.y",
"amdgpu-work-item-id-y" },
187 {
"llvm.r600.read.tidig.z",
"amdgpu-work-item-id-z" }
190 static const StringRef HSAIntrinsicToAttr[][2] = {
191 {
"llvm.amdgcn.dispatch.ptr",
"amdgpu-dispatch-ptr" },
192 {
"llvm.amdgcn.queue.ptr",
"amdgpu-queue-ptr" },
193 {
"llvm.amdgcn.dispatch.id",
"amdgpu-dispatch-id" }
204 bool Changed = addAttrsForIntrinsics(M, IntrinsicToAttr);
206 Changed |= addAttrsForIntrinsics(M, HSAIntrinsicToAttr);
209 if (F.hasFnAttribute(
"amdgpu-queue-ptr"))
212 if (hasAddrSpaceCast(F))
213 F.addFnAttr(
"amdgpu-queue-ptr");
221 return new AMDGPUAnnotateKernelFeatures();
ModulePass * createAMDGPUAnnotateKernelFeaturesPass()
void push_back(const T &Elt)
A Module instance is used to store all the information related to an LLVM module. ...
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
This class represents a function call, abstracting a target machine's calling convention.
const Function * getParent() const
Return the enclosing method, or null if none.
Address space for local memory.
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
unsigned getSrcAddressSpace() const
Returns the address space of the pointer operand.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
This class represents a conversion between pointers from one address space to another.
A Use represents the edge between a Value definition and its users.
static bool castRequiresQueuePtr(const AddrSpaceCastInst *ASC)
LLVM_NODISCARD bool empty() const
A constant value that is initialized with an expression using other constant values.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
INITIALIZE_PASS(AMDGPUAnnotateKernelFeatures, DEBUG_TYPE,"Add AMDGPU function attributes", false, false) static bool castRequiresQueuePtr(unsigned SrcAS)
Function * getFunction(StringRef Name) const
Look up the specified function in the module symbol table.
LLVM Basic Block Representation.
Address space for private memory.
This is an important base class in LLVM.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
char & AMDGPUAnnotateKernelFeaturesID
Represent the analysis usage information of a pass.
Value * getOperand(unsigned i) const
Triple - Helper class for working with autoconf configuration names.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
LLVM_NODISCARD T pop_back_val()
static GCRegistry::Add< ShadowStackGC > C("shadow-stack","Very portable GC for uncooperative code generators")
void setPreservesAll()
Set by analyses that do not transform their input at all.
iterator_range< user_iterator > users()
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
StringRef - Represent a constant reference to a string, i.e.
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
const BasicBlock * getParent() const