1 //===- AMDGPUAnnotateKernelFeaturesPass.cpp -------------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 /// \file This pass adds target attributes to functions which use intrinsics
11 /// which will impact calling convention lowering.
13 //===----------------------------------------------------------------------===//
16 #include "AMDGPUSubtarget.h"
17 #include "Utils/AMDGPUBaseInfo.h"
18 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/ADT/StringRef.h"
21 #include "llvm/ADT/Triple.h"
22 #include "llvm/Analysis/CallGraph.h"
23 #include "llvm/Analysis/CallGraphSCCPass.h"
24 #include "llvm/CodeGen/TargetPassConfig.h"
25 #include "llvm/IR/CallSite.h"
26 #include "llvm/IR/Constant.h"
27 #include "llvm/IR/Constants.h"
28 #include "llvm/IR/Function.h"
29 #include "llvm/IR/Instruction.h"
30 #include "llvm/IR/Instructions.h"
31 #include "llvm/IR/Intrinsics.h"
32 #include "llvm/IR/Module.h"
33 #include "llvm/IR/Type.h"
34 #include "llvm/IR/Use.h"
35 #include "llvm/Pass.h"
36 #include "llvm/Support/Casting.h"
37 #include "llvm/Support/ErrorHandling.h"
38 #include "llvm/Target/TargetMachine.h"
40 #define DEBUG_TYPE "amdgpu-annotate-kernel-features"
46 class AMDGPUAnnotateKernelFeatures
: public CallGraphSCCPass
{
48 const TargetMachine
*TM
= nullptr;
50 bool addFeatureAttributes(Function
&F
);
55 AMDGPUAnnotateKernelFeatures() : CallGraphSCCPass(ID
) {}
57 bool doInitialization(CallGraph
&CG
) override
;
58 bool runOnSCC(CallGraphSCC
&SCC
) override
;
60 StringRef
getPassName() const override
{
61 return "AMDGPU Annotate Kernel Features";
64 void getAnalysisUsage(AnalysisUsage
&AU
) const override
{
66 CallGraphSCCPass::getAnalysisUsage(AU
);
69 static bool visitConstantExpr(const ConstantExpr
*CE
);
70 static bool visitConstantExprsRecursively(
71 const Constant
*EntryC
,
72 SmallPtrSet
<const Constant
*, 8> &ConstantExprVisited
);
75 } // end anonymous namespace
77 char AMDGPUAnnotateKernelFeatures::ID
= 0;
79 char &llvm::AMDGPUAnnotateKernelFeaturesID
= AMDGPUAnnotateKernelFeatures::ID
;
81 INITIALIZE_PASS(AMDGPUAnnotateKernelFeatures
, DEBUG_TYPE
,
82 "Add AMDGPU function attributes", false, false)
85 // The queue ptr is only needed when casting to flat, not from it.
86 static bool castRequiresQueuePtr(unsigned SrcAS
) {
87 return SrcAS
== AMDGPUAS::LOCAL_ADDRESS
|| SrcAS
== AMDGPUAS::PRIVATE_ADDRESS
;
90 static bool castRequiresQueuePtr(const AddrSpaceCastInst
*ASC
) {
91 return castRequiresQueuePtr(ASC
->getSrcAddressSpace());
94 bool AMDGPUAnnotateKernelFeatures::visitConstantExpr(const ConstantExpr
*CE
) {
95 if (CE
->getOpcode() == Instruction::AddrSpaceCast
) {
96 unsigned SrcAS
= CE
->getOperand(0)->getType()->getPointerAddressSpace();
97 return castRequiresQueuePtr(SrcAS
);
103 bool AMDGPUAnnotateKernelFeatures::visitConstantExprsRecursively(
104 const Constant
*EntryC
,
105 SmallPtrSet
<const Constant
*, 8> &ConstantExprVisited
) {
107 if (!ConstantExprVisited
.insert(EntryC
).second
)
110 SmallVector
<const Constant
*, 16> Stack
;
111 Stack
.push_back(EntryC
);
113 while (!Stack
.empty()) {
114 const Constant
*C
= Stack
.pop_back_val();
116 // Check this constant expression.
117 if (const auto *CE
= dyn_cast
<ConstantExpr
>(C
)) {
118 if (visitConstantExpr(CE
))
122 // Visit all sub-expressions.
123 for (const Use
&U
: C
->operands()) {
124 const auto *OpC
= dyn_cast
<Constant
>(U
);
128 if (!ConstantExprVisited
.insert(OpC
).second
)
131 Stack
.push_back(OpC
);
138 // We do not need to note the x workitem or workgroup id because they are always
141 // TODO: We should not add the attributes if the known compile time workgroup
142 // size is 1 for y/z.
143 static StringRef
intrinsicToAttrName(Intrinsic::ID ID
,
147 case Intrinsic::amdgcn_workitem_id_x
:
148 NonKernelOnly
= true;
149 return "amdgpu-work-item-id-x";
150 case Intrinsic::amdgcn_workgroup_id_x
:
151 NonKernelOnly
= true;
152 return "amdgpu-work-group-id-x";
153 case Intrinsic::amdgcn_workitem_id_y
:
154 case Intrinsic::r600_read_tidig_y
:
155 return "amdgpu-work-item-id-y";
156 case Intrinsic::amdgcn_workitem_id_z
:
157 case Intrinsic::r600_read_tidig_z
:
158 return "amdgpu-work-item-id-z";
159 case Intrinsic::amdgcn_workgroup_id_y
:
160 case Intrinsic::r600_read_tgid_y
:
161 return "amdgpu-work-group-id-y";
162 case Intrinsic::amdgcn_workgroup_id_z
:
163 case Intrinsic::r600_read_tgid_z
:
164 return "amdgpu-work-group-id-z";
165 case Intrinsic::amdgcn_dispatch_ptr
:
166 return "amdgpu-dispatch-ptr";
167 case Intrinsic::amdgcn_dispatch_id
:
168 return "amdgpu-dispatch-id";
169 case Intrinsic::amdgcn_kernarg_segment_ptr
:
170 return "amdgpu-kernarg-segment-ptr";
171 case Intrinsic::amdgcn_implicitarg_ptr
:
172 return "amdgpu-implicitarg-ptr";
173 case Intrinsic::amdgcn_queue_ptr
:
174 case Intrinsic::trap
:
175 case Intrinsic::debugtrap
:
177 return "amdgpu-queue-ptr";
183 static bool handleAttr(Function
&Parent
, const Function
&Callee
,
185 if (Callee
.hasFnAttribute(Name
)) {
186 Parent
.addFnAttr(Name
);
193 static void copyFeaturesToFunction(Function
&Parent
, const Function
&Callee
,
194 bool &NeedQueuePtr
) {
195 // X ids unnecessarily propagated to kernels.
196 static const StringRef AttrNames
[] = {
197 { "amdgpu-work-item-id-x" },
198 { "amdgpu-work-item-id-y" },
199 { "amdgpu-work-item-id-z" },
200 { "amdgpu-work-group-id-x" },
201 { "amdgpu-work-group-id-y" },
202 { "amdgpu-work-group-id-z" },
203 { "amdgpu-dispatch-ptr" },
204 { "amdgpu-dispatch-id" },
205 { "amdgpu-kernarg-segment-ptr" },
206 { "amdgpu-implicitarg-ptr" }
209 if (handleAttr(Parent
, Callee
, "amdgpu-queue-ptr"))
212 for (StringRef AttrName
: AttrNames
)
213 handleAttr(Parent
, Callee
, AttrName
);
216 bool AMDGPUAnnotateKernelFeatures::addFeatureAttributes(Function
&F
) {
217 const GCNSubtarget
&ST
= TM
->getSubtarget
<GCNSubtarget
>(F
);
218 bool HasFlat
= ST
.hasFlatAddressSpace();
219 bool HasApertureRegs
= ST
.hasApertureRegs();
220 SmallPtrSet
<const Constant
*, 8> ConstantExprVisited
;
222 bool Changed
= false;
223 bool NeedQueuePtr
= false;
224 bool HaveCall
= false;
225 bool IsFunc
= !AMDGPU::isEntryFunctionCC(F
.getCallingConv());
227 for (BasicBlock
&BB
: F
) {
228 for (Instruction
&I
: BB
) {
231 Function
*Callee
= CS
.getCalledFunction();
233 // TODO: Do something with indirect calls.
235 if (!CS
.isInlineAsm())
240 Intrinsic::ID IID
= Callee
->getIntrinsicID();
241 if (IID
== Intrinsic::not_intrinsic
) {
243 copyFeaturesToFunction(F
, *Callee
, NeedQueuePtr
);
246 bool NonKernelOnly
= false;
247 StringRef AttrName
= intrinsicToAttrName(IID
,
248 NonKernelOnly
, NeedQueuePtr
);
249 if (!AttrName
.empty() && (IsFunc
|| !NonKernelOnly
)) {
250 F
.addFnAttr(AttrName
);
256 if (NeedQueuePtr
|| HasApertureRegs
)
259 if (const AddrSpaceCastInst
*ASC
= dyn_cast
<AddrSpaceCastInst
>(&I
)) {
260 if (castRequiresQueuePtr(ASC
)) {
266 for (const Use
&U
: I
.operands()) {
267 const auto *OpC
= dyn_cast
<Constant
>(U
);
271 if (visitConstantExprsRecursively(OpC
, ConstantExprVisited
)) {
280 F
.addFnAttr("amdgpu-queue-ptr");
284 // TODO: We could refine this to captured pointers that could possibly be
285 // accessed by flat instructions. For now this is mostly a poor way of
286 // estimating whether there are calls before argument lowering.
287 if (HasFlat
&& !IsFunc
&& HaveCall
) {
288 F
.addFnAttr("amdgpu-flat-scratch");
295 bool AMDGPUAnnotateKernelFeatures::runOnSCC(CallGraphSCC
&SCC
) {
296 Module
&M
= SCC
.getCallGraph().getModule();
297 Triple
TT(M
.getTargetTriple());
299 bool Changed
= false;
300 for (CallGraphNode
*I
: SCC
) {
301 Function
*F
= I
->getFunction();
302 if (!F
|| F
->isDeclaration())
305 Changed
|= addFeatureAttributes(*F
);
311 bool AMDGPUAnnotateKernelFeatures::doInitialization(CallGraph
&CG
) {
312 auto *TPC
= getAnalysisIfAvailable
<TargetPassConfig
>();
314 report_fatal_error("TargetMachine is required");
316 TM
= &TPC
->getTM
<TargetMachine
>();
320 Pass
*llvm::createAMDGPUAnnotateKernelFeaturesPass() {
321 return new AMDGPUAnnotateKernelFeatures();