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 "llvm/IR/Constants.h"
17 #include "llvm/IR/Instructions.h"
18 #include "llvm/IR/Module.h"
20 #define DEBUG_TYPE "amdgpu-annotate-kernel-features"
26 class AMDGPUAnnotateKernelFeatures : public ModulePass {
28 static bool hasAddrSpaceCast(const Function &F);
30 void addAttrToCallers(Function *Intrin, StringRef AttrName);
31 bool addAttrsForIntrinsics(Module &M, ArrayRef<StringRef[2]>);
36 AMDGPUAnnotateKernelFeatures() : ModulePass(ID) { }
37 bool runOnModule(Module &M) override;
38 const char *getPassName() const override {
39 return "AMDGPU Annotate Kernel Features";
42 void getAnalysisUsage(AnalysisUsage &AU) const override {
44 ModulePass::getAnalysisUsage(AU);
47 static bool visitConstantExpr(const ConstantExpr *CE);
48 static bool visitConstantExprsRecursively(
49 const Constant *EntryC,
50 SmallPtrSet<const Constant *, 8> &ConstantExprVisited);
55 char AMDGPUAnnotateKernelFeatures::ID = 0;
57 char &llvm::AMDGPUAnnotateKernelFeaturesID = AMDGPUAnnotateKernelFeatures::ID;
59 INITIALIZE_PASS(AMDGPUAnnotateKernelFeatures, DEBUG_TYPE,
60 "Add AMDGPU function attributes", false, false)
63 // The queue ptr is only needed when casting to flat, not from it.
64 static bool castRequiresQueuePtr(unsigned SrcAS) {
65 return SrcAS == AMDGPUAS::LOCAL_ADDRESS || SrcAS == AMDGPUAS::PRIVATE_ADDRESS;
68 static bool castRequiresQueuePtr(const AddrSpaceCastInst *ASC) {
69 return castRequiresQueuePtr(ASC->getSrcAddressSpace());
72 bool AMDGPUAnnotateKernelFeatures::visitConstantExpr(const ConstantExpr *CE) {
73 if (CE->getOpcode() == Instruction::AddrSpaceCast) {
74 unsigned SrcAS = CE->getOperand(0)->getType()->getPointerAddressSpace();
75 return castRequiresQueuePtr(SrcAS);
81 bool AMDGPUAnnotateKernelFeatures::visitConstantExprsRecursively(
82 const Constant *EntryC,
83 SmallPtrSet<const Constant *, 8> &ConstantExprVisited) {
85 if (!ConstantExprVisited.insert(EntryC).second)
88 SmallVector<const Constant *, 16> Stack;
89 Stack.push_back(EntryC);
91 while (!Stack.empty()) {
92 const Constant *C = Stack.pop_back_val();
94 // Check this constant expression.
95 if (const auto *CE = dyn_cast<ConstantExpr>(C)) {
96 if (visitConstantExpr(CE))
100 // Visit all sub-expressions.
101 for (const Use &U : C->operands()) {
102 const auto *OpC = dyn_cast<Constant>(U);
106 if (!ConstantExprVisited.insert(OpC).second)
109 Stack.push_back(OpC);
116 // Return true if an addrspacecast is used that requires the queue ptr.
117 bool AMDGPUAnnotateKernelFeatures::hasAddrSpaceCast(const Function &F) {
118 SmallPtrSet<const Constant *, 8> ConstantExprVisited;
120 for (const BasicBlock &BB : F) {
121 for (const Instruction &I : BB) {
122 if (const AddrSpaceCastInst *ASC = dyn_cast<AddrSpaceCastInst>(&I)) {
123 if (castRequiresQueuePtr(ASC))
127 for (const Use &U : I.operands()) {
128 const auto *OpC = dyn_cast<Constant>(U);
132 if (visitConstantExprsRecursively(OpC, ConstantExprVisited))
141 void AMDGPUAnnotateKernelFeatures::addAttrToCallers(Function *Intrin,
142 StringRef AttrName) {
143 SmallPtrSet<Function *, 4> SeenFuncs;
145 for (User *U : Intrin->users()) {
146 // CallInst is the only valid user for an intrinsic.
147 CallInst *CI = cast<CallInst>(U);
149 Function *CallingFunction = CI->getParent()->getParent();
150 if (SeenFuncs.insert(CallingFunction).second)
151 CallingFunction->addFnAttr(AttrName);
155 bool AMDGPUAnnotateKernelFeatures::addAttrsForIntrinsics(
157 ArrayRef<StringRef[2]> IntrinsicToAttr) {
158 bool Changed = false;
160 for (const StringRef *Arr : IntrinsicToAttr) {
161 if (Function *Fn = M.getFunction(Arr[0])) {
162 addAttrToCallers(Fn, Arr[1]);
170 bool AMDGPUAnnotateKernelFeatures::runOnModule(Module &M) {
171 Triple TT(M.getTargetTriple());
173 static const StringRef IntrinsicToAttr[][2] = {
175 { "llvm.amdgcn.workitem.id.y", "amdgpu-work-item-id-y" },
176 { "llvm.amdgcn.workitem.id.z", "amdgpu-work-item-id-z" },
178 { "llvm.amdgcn.workgroup.id.y", "amdgpu-work-group-id-y" },
179 { "llvm.amdgcn.workgroup.id.z", "amdgpu-work-group-id-z" },
181 { "llvm.r600.read.tgid.y", "amdgpu-work-group-id-y" },
182 { "llvm.r600.read.tgid.z", "amdgpu-work-group-id-z" },
185 { "llvm.r600.read.tidig.y", "amdgpu-work-item-id-y" },
186 { "llvm.r600.read.tidig.z", "amdgpu-work-item-id-z" }
189 static const StringRef HSAIntrinsicToAttr[][2] = {
190 { "llvm.amdgcn.dispatch.ptr", "amdgpu-dispatch-ptr" },
191 { "llvm.amdgcn.queue.ptr", "amdgpu-queue-ptr" }
194 // TODO: We should not add the attributes if the known compile time workgroup
195 // size is 1 for y/z.
197 // TODO: Intrinsics that require queue ptr.
199 // We do not need to note the x workitem or workgroup id because they are
200 // always initialized.
202 bool Changed = addAttrsForIntrinsics(M, IntrinsicToAttr);
203 if (TT.getOS() == Triple::AMDHSA) {
204 Changed |= addAttrsForIntrinsics(M, HSAIntrinsicToAttr);
206 for (Function &F : M) {
207 if (F.hasFnAttribute("amdgpu-queue-ptr"))
210 if (hasAddrSpaceCast(F))
211 F.addFnAttr("amdgpu-queue-ptr");
218 ModulePass *llvm::createAMDGPUAnnotateKernelFeaturesPass() {
219 return new AMDGPUAnnotateKernelFeatures();