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 "llvm/ADT/Triple.h"
18 #include "llvm/CodeGen/TargetPassConfig.h"
19 #include "llvm/IR/Constants.h"
20 #include "llvm/IR/Instructions.h"
21 #include "llvm/IR/Module.h"
23 #define DEBUG_TYPE "amdgpu-annotate-kernel-features"
29 class AMDGPUAnnotateKernelFeatures : public ModulePass {
32 static bool hasAddrSpaceCast(const Function &F, AMDGPUAS AS);
34 void addAttrToCallers(Function *Intrin, StringRef AttrName);
35 bool addAttrsForIntrinsics(Module &M, ArrayRef<StringRef[2]>);
40 AMDGPUAnnotateKernelFeatures() : ModulePass(ID) {}
41 bool runOnModule(Module &M) override;
42 StringRef getPassName() const override {
43 return "AMDGPU Annotate Kernel Features";
46 void getAnalysisUsage(AnalysisUsage &AU) const override {
48 ModulePass::getAnalysisUsage(AU);
51 static bool visitConstantExpr(const ConstantExpr *CE, AMDGPUAS AS);
52 static bool visitConstantExprsRecursively(
53 const Constant *EntryC,
54 SmallPtrSet<const Constant *, 8> &ConstantExprVisited,
60 char AMDGPUAnnotateKernelFeatures::ID = 0;
62 char &llvm::AMDGPUAnnotateKernelFeaturesID = AMDGPUAnnotateKernelFeatures::ID;
64 INITIALIZE_PASS(AMDGPUAnnotateKernelFeatures, DEBUG_TYPE,
65 "Add AMDGPU function attributes", false, false)
68 // The queue ptr is only needed when casting to flat, not from it.
69 static bool castRequiresQueuePtr(unsigned SrcAS, const AMDGPUAS &AS) {
70 return SrcAS == AS.LOCAL_ADDRESS || SrcAS == AS.PRIVATE_ADDRESS;
73 static bool castRequiresQueuePtr(const AddrSpaceCastInst *ASC,
75 return castRequiresQueuePtr(ASC->getSrcAddressSpace(), AS);
78 bool AMDGPUAnnotateKernelFeatures::visitConstantExpr(const ConstantExpr *CE,
80 if (CE->getOpcode() == Instruction::AddrSpaceCast) {
81 unsigned SrcAS = CE->getOperand(0)->getType()->getPointerAddressSpace();
82 return castRequiresQueuePtr(SrcAS, AS);
88 bool AMDGPUAnnotateKernelFeatures::visitConstantExprsRecursively(
89 const Constant *EntryC,
90 SmallPtrSet<const Constant *, 8> &ConstantExprVisited,
93 if (!ConstantExprVisited.insert(EntryC).second)
96 SmallVector<const Constant *, 16> Stack;
97 Stack.push_back(EntryC);
99 while (!Stack.empty()) {
100 const Constant *C = Stack.pop_back_val();
102 // Check this constant expression.
103 if (const auto *CE = dyn_cast<ConstantExpr>(C)) {
104 if (visitConstantExpr(CE, AS))
108 // Visit all sub-expressions.
109 for (const Use &U : C->operands()) {
110 const auto *OpC = dyn_cast<Constant>(U);
114 if (!ConstantExprVisited.insert(OpC).second)
117 Stack.push_back(OpC);
124 // Return true if an addrspacecast is used that requires the queue ptr.
125 bool AMDGPUAnnotateKernelFeatures::hasAddrSpaceCast(const Function &F,
127 SmallPtrSet<const Constant *, 8> ConstantExprVisited;
129 for (const BasicBlock &BB : F) {
130 for (const Instruction &I : BB) {
131 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(&I)) {
132 if (castRequiresQueuePtr(ASC, AS))
136 for (const Use &U : I.operands()) {
137 const auto *OpC = dyn_cast<Constant>(U);
141 if (visitConstantExprsRecursively(OpC, ConstantExprVisited, AS))
150 void AMDGPUAnnotateKernelFeatures::addAttrToCallers(Function *Intrin,
151 StringRef AttrName) {
152 SmallPtrSet<Function *, 4> SeenFuncs;
154 for (User *U : Intrin->users()) {
155 // CallInst is the only valid user for an intrinsic.
156 CallInst *CI = cast<CallInst>(U);
158 Function *CallingFunction = CI->getParent()->getParent();
159 if (SeenFuncs.insert(CallingFunction).second)
160 CallingFunction->addFnAttr(AttrName);
164 bool AMDGPUAnnotateKernelFeatures::addAttrsForIntrinsics(
166 ArrayRef<StringRef[2]> IntrinsicToAttr) {
167 bool Changed = false;
169 for (const StringRef *Arr : IntrinsicToAttr) {
170 if (Function *Fn = M.getFunction(Arr[0])) {
171 addAttrToCallers(Fn, Arr[1]);
179 bool AMDGPUAnnotateKernelFeatures::runOnModule(Module &M) {
180 Triple TT(M.getTargetTriple());
181 AS = AMDGPU::getAMDGPUAS(M);
183 static const StringRef IntrinsicToAttr[][2] = {
185 { "llvm.amdgcn.workitem.id.y", "amdgpu-work-item-id-y" },
186 { "llvm.amdgcn.workitem.id.z", "amdgpu-work-item-id-z" },
188 { "llvm.amdgcn.workgroup.id.y", "amdgpu-work-group-id-y" },
189 { "llvm.amdgcn.workgroup.id.z", "amdgpu-work-group-id-z" },
191 { "llvm.r600.read.tgid.y", "amdgpu-work-group-id-y" },
192 { "llvm.r600.read.tgid.z", "amdgpu-work-group-id-z" },
195 { "llvm.r600.read.tidig.y", "amdgpu-work-item-id-y" },
196 { "llvm.r600.read.tidig.z", "amdgpu-work-item-id-z" }
199 static const StringRef HSAIntrinsicToAttr[][2] = {
200 { "llvm.amdgcn.dispatch.ptr", "amdgpu-dispatch-ptr" },
201 { "llvm.amdgcn.queue.ptr", "amdgpu-queue-ptr" },
202 { "llvm.amdgcn.dispatch.id", "amdgpu-dispatch-id" },
203 { "llvm.trap", "amdgpu-queue-ptr" },
204 { "llvm.debugtrap", "amdgpu-queue-ptr" }
207 // TODO: We should not add the attributes if the known compile time workgroup
208 // size is 1 for y/z.
210 // TODO: Intrinsics that require queue ptr.
212 // We do not need to note the x workitem or workgroup id because they are
213 // always initialized.
215 bool Changed = addAttrsForIntrinsics(M, IntrinsicToAttr);
216 if (TT.getOS() == Triple::AMDHSA || TT.getOS() == Triple::Mesa3D) {
217 Changed |= addAttrsForIntrinsics(M, HSAIntrinsicToAttr);
219 for (Function &F : M) {
220 if (F.hasFnAttribute("amdgpu-queue-ptr"))
223 auto *TPC = getAnalysisIfAvailable<TargetPassConfig>();
224 bool HasApertureRegs = TPC && TPC->getTM<TargetMachine>()
225 .getSubtarget<AMDGPUSubtarget>(F)
227 if (!HasApertureRegs && hasAddrSpaceCast(F, AS))
228 F.addFnAttr("amdgpu-queue-ptr");
235 ModulePass *llvm::createAMDGPUAnnotateKernelFeaturesPass() {
236 return new AMDGPUAnnotateKernelFeatures();