25 #define DEBUG_TYPE "amdgpu-annotate-kernel-features"
43 bool doInitialization(
CallGraph &CG)
override;
46 return "AMDGPU Annotate Kernel Features";
55 static bool visitConstantExprsRecursively(
68 "Add AMDGPU function attributes",
false,
false)
73 return SrcAS == AS.LOCAL_ADDRESS || SrcAS == AS.PRIVATE_ADDRESS;
81 bool AMDGPUAnnotateKernelFeatures::visitConstantExpr(
const ConstantExpr *CE,
83 if (CE->
getOpcode() == Instruction::AddrSpaceCast) {
91 bool AMDGPUAnnotateKernelFeatures::visitConstantExprsRecursively(
96 if (!ConstantExprVisited.
insert(EntryC).second)
102 while (!Stack.
empty()) {
106 if (
const auto *CE = dyn_cast<ConstantExpr>(C)) {
107 if (visitConstantExpr(CE, AS))
117 if (!ConstantExprVisited.
insert(OpC).second)
136 case Intrinsic::amdgcn_workitem_id_x:
137 NonKernelOnly =
true;
138 return "amdgpu-work-item-id-x";
139 case Intrinsic::amdgcn_workgroup_id_x:
140 NonKernelOnly =
true;
141 return "amdgpu-work-group-id-x";
142 case Intrinsic::amdgcn_workitem_id_y:
143 case Intrinsic::r600_read_tidig_y:
144 return "amdgpu-work-item-id-y";
145 case Intrinsic::amdgcn_workitem_id_z:
146 case Intrinsic::r600_read_tidig_z:
147 return "amdgpu-work-item-id-z";
148 case Intrinsic::amdgcn_workgroup_id_y:
149 case Intrinsic::r600_read_tgid_y:
150 return "amdgpu-work-group-id-y";
151 case Intrinsic::amdgcn_workgroup_id_z:
152 case Intrinsic::r600_read_tgid_z:
153 return "amdgpu-work-group-id-z";
154 case Intrinsic::amdgcn_dispatch_ptr:
155 return "amdgpu-dispatch-ptr";
156 case Intrinsic::amdgcn_dispatch_id:
157 return "amdgpu-dispatch-id";
158 case Intrinsic::amdgcn_kernarg_segment_ptr:
159 case Intrinsic::amdgcn_implicitarg_ptr:
160 return "amdgpu-kernarg-segment-ptr";
161 case Intrinsic::amdgcn_queue_ptr:
162 case Intrinsic::trap:
163 case Intrinsic::debugtrap:
165 return "amdgpu-queue-ptr";
182 bool &NeedQueuePtr) {
185 {
"amdgpu-work-item-id-x" },
186 {
"amdgpu-work-item-id-y" },
187 {
"amdgpu-work-item-id-z" },
188 {
"amdgpu-work-group-id-x" },
189 {
"amdgpu-work-group-id-y" },
190 {
"amdgpu-work-group-id-z" },
191 {
"amdgpu-dispatch-ptr" },
192 {
"amdgpu-dispatch-id" },
193 {
"amdgpu-kernarg-segment-ptr" }
196 if (
handleAttr(Parent, Callee,
"amdgpu-queue-ptr"))
203 bool AMDGPUAnnotateKernelFeatures::addFeatureAttributes(
Function &
F) {
209 bool Changed =
false;
210 bool NeedQueuePtr =
false;
211 bool HaveCall =
false;
222 if (!
CS.isInlineAsm())
233 bool NonKernelOnly =
false;
235 NonKernelOnly, NeedQueuePtr);
236 if (!AttrName.
empty() && (IsFunc || !NonKernelOnly)) {
237 F.addFnAttr(AttrName);
243 if (NeedQueuePtr || HasApertureRegs)
253 for (
const Use &U :
I.operands()) {
258 if (visitConstantExprsRecursively(OpC, ConstantExprVisited, AS)) {
267 F.addFnAttr(
"amdgpu-queue-ptr");
274 if (HasFlat && !IsFunc && HaveCall) {
275 F.addFnAttr(
"amdgpu-flat-scratch");
282 bool AMDGPUAnnotateKernelFeatures::runOnSCC(
CallGraphSCC &SCC) {
286 bool Changed =
false;
292 Changed |= addFeatureAttributes(*F);
299 bool AMDGPUAnnotateKernelFeatures::doInitialization(
CallGraph &CG) {
300 auto *
TPC = getAnalysisIfAvailable<TargetPassConfig>();
310 return new AMDGPUAnnotateKernelFeatures();
Pass interface - Implemented by all 'passes'.
void push_back(const T &Elt)
AMDGPU specific subclass of TargetSubtarget.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
A Module instance is used to store all the information related to an LLVM module. ...
AMDGPUAS getAMDGPUAS(const Module &M)
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
unsigned getSrcAddressSpace() const
Returns the address space of the pointer operand.
A node in the call graph for a module.
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
void getAnalysisUsage(AnalysisUsage &Info) const override
getAnalysisUsage - For this class, we declare that we require and preserve the call graph...
Module & getModule() const
Returns the module the call graph corresponds to.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
This class represents a conversion between pointers from one address space to another.
OpenCL uses address spaces to differentiate between various memory regions on the hardware...
A Use represents the edge between a Value definition and its users.
Pass * createAMDGPUAnnotateKernelFeaturesPass()
const CallGraph & getCallGraph()
LLVM_NODISCARD bool empty() const
const AMDGPUAS &AS return SrcAS
A constant value that is initialized with an expression using other constant values.
bool hasFlatAddressSpace() const
static bool castRequiresQueuePtr(const AddrSpaceCastInst *ASC, const AMDGPUAS &AS)
static void copyFeaturesToFunction(Function &Parent, const Function &Callee, bool &NeedQueuePtr)
LLVM Basic Block Representation.
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.
bool isEntryFunctionCC(CallingConv::ID CC)
INITIALIZE_PASS(AMDGPUAnnotateKernelFeatures, DEBUG_TYPE,"Add AMDGPU function attributes", false, false) static bool castRequiresQueuePtr(unsigned SrcAS
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()
bool hasApertureRegs() const
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
void setPreservesAll()
Set by analyses that do not transform their input at all.
static bool handleAttr(Function &Parent, const Function &Callee, StringRef Name)
LLVM_NODISCARD LLVM_ATTRIBUTE_ALWAYS_INLINE bool empty() const
empty - Check if the string is empty.
The basic data container for the call graph of a Module of IR.
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
Primary interface to the complete machine description for the target machine.
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.
static StringRef intrinsicToAttrName(Intrinsic::ID ID, bool &NonKernelOnly, bool &IsQueuePtr)