LLVM  5.0.0
AMDGPUAnnotateKernelFeatures.cpp
Go to the documentation of this file.
1 //===-- AMDGPUAnnotateKernelFeaturesPass.cpp ------------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file This pass adds target attributes to functions which use intrinsics
11 /// which will impact calling convention lowering.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "AMDGPU.h"
16 #include "AMDGPUSubtarget.h"
17 #include "llvm/ADT/Triple.h"
20 #include "llvm/IR/Constants.h"
21 #include "llvm/IR/InstIterator.h"
22 #include "llvm/IR/Instructions.h"
23 #include "llvm/IR/Module.h"
24 
25 #define DEBUG_TYPE "amdgpu-annotate-kernel-features"
26 
27 using namespace llvm;
28 
29 namespace {
30 
31 class AMDGPUAnnotateKernelFeatures : public CallGraphSCCPass {
32 private:
33  const TargetMachine *TM = nullptr;
34  AMDGPUAS AS;
35 
36  bool addFeatureAttributes(Function &F);
37 
38 public:
39  static char ID;
40 
41  AMDGPUAnnotateKernelFeatures() : CallGraphSCCPass(ID) {}
42 
43  bool doInitialization(CallGraph &CG) override;
44  bool runOnSCC(CallGraphSCC &SCC) override;
45  StringRef getPassName() const override {
46  return "AMDGPU Annotate Kernel Features";
47  }
48 
49  void getAnalysisUsage(AnalysisUsage &AU) const override {
50  AU.setPreservesAll();
52  }
53 
54  static bool visitConstantExpr(const ConstantExpr *CE, AMDGPUAS AS);
55  static bool visitConstantExprsRecursively(
56  const Constant *EntryC,
57  SmallPtrSet<const Constant *, 8> &ConstantExprVisited,
58  AMDGPUAS AS);
59 };
60 
61 }
62 
64 
66 
67 INITIALIZE_PASS(AMDGPUAnnotateKernelFeatures, DEBUG_TYPE,
68  "Add AMDGPU function attributes", false, false)
69 
70 
71 // The queue ptr is only needed when casting to flat, not from it.
72 static bool castRequiresQueuePtr(unsigned SrcAS, const AMDGPUAS &AS) {
73  return SrcAS == AS.LOCAL_ADDRESS || SrcAS == AS.PRIVATE_ADDRESS;
74 }
75 
76 static bool castRequiresQueuePtr(const AddrSpaceCastInst *ASC,
77  const AMDGPUAS &AS) {
78  return castRequiresQueuePtr(ASC->getSrcAddressSpace(), AS);
79 }
80 
81 bool AMDGPUAnnotateKernelFeatures::visitConstantExpr(const ConstantExpr *CE,
82  AMDGPUAS AS) {
83  if (CE->getOpcode() == Instruction::AddrSpaceCast) {
84  unsigned SrcAS = CE->getOperand(0)->getType()->getPointerAddressSpace();
85  return castRequiresQueuePtr(SrcAS, AS);
86  }
87 
88  return false;
89 }
90 
91 bool AMDGPUAnnotateKernelFeatures::visitConstantExprsRecursively(
92  const Constant *EntryC,
93  SmallPtrSet<const Constant *, 8> &ConstantExprVisited,
94  AMDGPUAS AS) {
95 
96  if (!ConstantExprVisited.insert(EntryC).second)
97  return false;
98 
100  Stack.push_back(EntryC);
101 
102  while (!Stack.empty()) {
103  const Constant *C = Stack.pop_back_val();
104 
105  // Check this constant expression.
106  if (const auto *CE = dyn_cast<ConstantExpr>(C)) {
107  if (visitConstantExpr(CE, AS))
108  return true;
109  }
110 
111  // Visit all sub-expressions.
112  for (const Use &U : C->operands()) {
113  const auto *OpC = dyn_cast<Constant>(U);
114  if (!OpC)
115  continue;
116 
117  if (!ConstantExprVisited.insert(OpC).second)
118  continue;
119 
120  Stack.push_back(OpC);
121  }
122  }
123 
124  return false;
125 }
126 
127 // We do not need to note the x workitem or workgroup id because they are always
128 // initialized.
129 //
130 // TODO: We should not add the attributes if the known compile time workgroup
131 // size is 1 for y/z.
133  bool &NonKernelOnly,
134  bool &IsQueuePtr) {
135  switch (ID) {
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:
164  IsQueuePtr = true;
165  return "amdgpu-queue-ptr";
166  default:
167  return "";
168  }
169 }
170 
171 static bool handleAttr(Function &Parent, const Function &Callee,
172  StringRef Name) {
173  if (Callee.hasFnAttribute(Name)) {
174  Parent.addFnAttr(Name);
175  return true;
176  }
177 
178  return false;
179 }
180 
181 static void copyFeaturesToFunction(Function &Parent, const Function &Callee,
182  bool &NeedQueuePtr) {
183  // X ids unnecessarily propagated to kernels.
184  static const StringRef AttrNames[] = {
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" }
194  };
195 
196  if (handleAttr(Parent, Callee, "amdgpu-queue-ptr"))
197  NeedQueuePtr = true;
198 
199  for (StringRef AttrName : AttrNames)
200  handleAttr(Parent, Callee, AttrName);
201 }
202 
203 bool AMDGPUAnnotateKernelFeatures::addFeatureAttributes(Function &F) {
204  const AMDGPUSubtarget &ST = TM->getSubtarget<AMDGPUSubtarget>(F);
205  bool HasFlat = ST.hasFlatAddressSpace();
206  bool HasApertureRegs = ST.hasApertureRegs();
207  SmallPtrSet<const Constant *, 8> ConstantExprVisited;
208 
209  bool Changed = false;
210  bool NeedQueuePtr = false;
211  bool HaveCall = false;
212  bool IsFunc = !AMDGPU::isEntryFunctionCC(F.getCallingConv());
213 
214  for (BasicBlock &BB : F) {
215  for (Instruction &I : BB) {
216  CallSite CS(&I);
217  if (CS) {
218  Function *Callee = CS.getCalledFunction();
219 
220  // TODO: Do something with indirect calls.
221  if (!Callee) {
222  if (!CS.isInlineAsm())
223  HaveCall = true;
224  continue;
225  }
226 
227  Intrinsic::ID IID = Callee->getIntrinsicID();
228  if (IID == Intrinsic::not_intrinsic) {
229  HaveCall = true;
230  copyFeaturesToFunction(F, *Callee, NeedQueuePtr);
231  Changed = true;
232  } else {
233  bool NonKernelOnly = false;
234  StringRef AttrName = intrinsicToAttrName(IID,
235  NonKernelOnly, NeedQueuePtr);
236  if (!AttrName.empty() && (IsFunc || !NonKernelOnly)) {
237  F.addFnAttr(AttrName);
238  Changed = true;
239  }
240  }
241  }
242 
243  if (NeedQueuePtr || HasApertureRegs)
244  continue;
245 
246  if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(&I)) {
247  if (castRequiresQueuePtr(ASC, AS)) {
248  NeedQueuePtr = true;
249  continue;
250  }
251  }
252 
253  for (const Use &U : I.operands()) {
254  const auto *OpC = dyn_cast<Constant>(U);
255  if (!OpC)
256  continue;
257 
258  if (visitConstantExprsRecursively(OpC, ConstantExprVisited, AS)) {
259  NeedQueuePtr = true;
260  break;
261  }
262  }
263  }
264  }
265 
266  if (NeedQueuePtr) {
267  F.addFnAttr("amdgpu-queue-ptr");
268  Changed = true;
269  }
270 
271  // TODO: We could refine this to captured pointers that could possibly be
272  // accessed by flat instructions. For now this is mostly a poor way of
273  // estimating whether there are calls before argument lowering.
274  if (HasFlat && !IsFunc && HaveCall) {
275  F.addFnAttr("amdgpu-flat-scratch");
276  Changed = true;
277  }
278 
279  return Changed;
280 }
281 
282 bool AMDGPUAnnotateKernelFeatures::runOnSCC(CallGraphSCC &SCC) {
283  Module &M = SCC.getCallGraph().getModule();
284  Triple TT(M.getTargetTriple());
285 
286  bool Changed = false;
287  for (CallGraphNode *I : SCC) {
288  Function *F = I->getFunction();
289  if (!F || F->isDeclaration())
290  continue;
291 
292  Changed |= addFeatureAttributes(*F);
293  }
294 
295 
296  return Changed;
297 }
298 
299 bool AMDGPUAnnotateKernelFeatures::doInitialization(CallGraph &CG) {
300  auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
301  if (!TPC)
302  report_fatal_error("TargetMachine is required");
303 
304  AS = AMDGPU::getAMDGPUAS(CG.getModule());
305  TM = &TPC->getTM<TargetMachine>();
306  return false;
307 }
308 
310  return new AMDGPUAnnotateKernelFeatures();
311 }
Pass interface - Implemented by all 'passes'.
Definition: Pass.h:81
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.
Definition: Error.cpp:103
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:63
AMDGPUAS getAMDGPUAS(const Module &M)
unsigned getOpcode() const
Return the opcode at the root of this constant expression.
Definition: Constants.h:1171
unsigned getSrcAddressSpace() const
Returns the address space of the pointer operand.
A node in the call graph for a module.
Definition: CallGraph.h:161
const std::string & getTargetTriple() const
Get the target triple which is a string describing the target host.
Definition: Module.h:233
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.
Definition: CallGraph.h:110
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
Definition: Function.h:186
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...
Definition: AMDGPU.h:164
A Use represents the edge between a Value definition and its users.
Definition: Use.h:56
Pass * createAMDGPUAnnotateKernelFeaturesPass()
const CallGraph & getCallGraph()
LLVM_NODISCARD bool empty() const
Definition: SmallVector.h:60
const AMDGPUAS &AS return SrcAS
A constant value that is initialized with an expression using other constant values.
Definition: Constants.h:862
#define F(x, y, z)
Definition: MD5.cpp:55
bool hasFlatAddressSpace() const
static bool castRequiresQueuePtr(const AddrSpaceCastInst *ASC, const AMDGPUAS &AS)
const char * Name
TracePC TPC
static void copyFeaturesToFunction(Function &Parent, const Function &Callee, bool &NeedQueuePtr)
LLVM Basic Block Representation.
Definition: BasicBlock.h:59
This is an important base class in LLVM.
Definition: Constant.h:42
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.
Definition: SmallPtrSet.h:372
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
Definition: User.h:154
op_range operands()
Definition: User.h:222
Triple - Helper class for working with autoconf configuration names.
Definition: Triple.h:44
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements...
Definition: SmallPtrSet.h:423
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Definition: SmallVector.h:861
Module.h This file contains the declarations for the Module class.
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:245
LLVM_NODISCARD T pop_back_val()
Definition: SmallVector.h:382
bool hasApertureRegs() const
Intrinsic::ID getIntrinsicID() const LLVM_READONLY
getIntrinsicID - This method returns the ID number of the specified function, or Intrinsic::not_intri...
Definition: Function.h:167
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.
Definition: StringRef.h:133
The basic data container for the call graph of a Module of IR.
Definition: CallGraph.h:70
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition: Function.h:254
bool isDeclaration() const
Return true if the primary definition of this global value is outside of the current translation unit...
Definition: Globals.cpp:200
#define C
Definition: LargeTest.cpp:25
#define I(x, y, z)
Definition: MD5.cpp:58
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
Definition: Casting.h:323
#define DEBUG_TYPE
aarch64 promote const
CallGraphSCC - This is a single SCC that a CallGraphSCCPass is run on.
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:56
void addFnAttr(Attribute::AttrKind Kind)
Add function attributes to this function.
Definition: Function.h:203
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
Definition: DerivedTypes.h:503
static StringRef intrinsicToAttrName(Intrinsic::ID ID, bool &NonKernelOnly, bool &IsQueuePtr)