1eb11fae6SDimitry Andric //===-- AMDGPULowerKernelAttributes.cpp ------------------------------------------===//
2eb11fae6SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6eb11fae6SDimitry Andric //
7eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
8eb11fae6SDimitry Andric //
9eb11fae6SDimitry Andric /// \file This pass does attempts to make use of reqd_work_group_size metadata
10eb11fae6SDimitry Andric /// to eliminate loads from the dispatch packet and to constant fold OpenCL
11eb11fae6SDimitry Andric /// get_local_size-like functions.
12eb11fae6SDimitry Andric //
13eb11fae6SDimitry Andric //===----------------------------------------------------------------------===//
14eb11fae6SDimitry Andric
15eb11fae6SDimitry Andric #include "AMDGPU.h"
16e3b55780SDimitry Andric #include "Utils/AMDGPUBaseInfo.h"
17b1c73532SDimitry Andric #include "llvm/Analysis/ConstantFolding.h"
18eb11fae6SDimitry Andric #include "llvm/Analysis/ValueTracking.h"
19eb11fae6SDimitry Andric #include "llvm/CodeGen/Passes.h"
20eb11fae6SDimitry Andric #include "llvm/CodeGen/TargetPassConfig.h"
21eb11fae6SDimitry Andric #include "llvm/IR/Constants.h"
22eb11fae6SDimitry Andric #include "llvm/IR/Function.h"
23b60736ecSDimitry Andric #include "llvm/IR/InstIterator.h"
24eb11fae6SDimitry Andric #include "llvm/IR/Instructions.h"
25b60736ecSDimitry Andric #include "llvm/IR/IntrinsicsAMDGPU.h"
26eb11fae6SDimitry Andric #include "llvm/IR/PatternMatch.h"
27eb11fae6SDimitry Andric #include "llvm/Pass.h"
28eb11fae6SDimitry Andric
29eb11fae6SDimitry Andric #define DEBUG_TYPE "amdgpu-lower-kernel-attributes"
30eb11fae6SDimitry Andric
31eb11fae6SDimitry Andric using namespace llvm;
32eb11fae6SDimitry Andric
33eb11fae6SDimitry Andric namespace {
34eb11fae6SDimitry Andric
35eb11fae6SDimitry Andric // Field offsets in hsa_kernel_dispatch_packet_t.
36eb11fae6SDimitry Andric enum DispatchPackedOffsets {
37eb11fae6SDimitry Andric WORKGROUP_SIZE_X = 4,
38eb11fae6SDimitry Andric WORKGROUP_SIZE_Y = 6,
39eb11fae6SDimitry Andric WORKGROUP_SIZE_Z = 8,
40eb11fae6SDimitry Andric
41eb11fae6SDimitry Andric GRID_SIZE_X = 12,
42eb11fae6SDimitry Andric GRID_SIZE_Y = 16,
43eb11fae6SDimitry Andric GRID_SIZE_Z = 20
44eb11fae6SDimitry Andric };
45eb11fae6SDimitry Andric
46e3b55780SDimitry Andric // Field offsets to implicit kernel argument pointer.
47e3b55780SDimitry Andric enum ImplicitArgOffsets {
48e3b55780SDimitry Andric HIDDEN_BLOCK_COUNT_X = 0,
49e3b55780SDimitry Andric HIDDEN_BLOCK_COUNT_Y = 4,
50e3b55780SDimitry Andric HIDDEN_BLOCK_COUNT_Z = 8,
51e3b55780SDimitry Andric
52e3b55780SDimitry Andric HIDDEN_GROUP_SIZE_X = 12,
53e3b55780SDimitry Andric HIDDEN_GROUP_SIZE_Y = 14,
54e3b55780SDimitry Andric HIDDEN_GROUP_SIZE_Z = 16,
55e3b55780SDimitry Andric
56e3b55780SDimitry Andric HIDDEN_REMAINDER_X = 18,
57e3b55780SDimitry Andric HIDDEN_REMAINDER_Y = 20,
58e3b55780SDimitry Andric HIDDEN_REMAINDER_Z = 22,
59e3b55780SDimitry Andric };
60e3b55780SDimitry Andric
61eb11fae6SDimitry Andric class AMDGPULowerKernelAttributes : public ModulePass {
62eb11fae6SDimitry Andric public:
63eb11fae6SDimitry Andric static char ID;
64eb11fae6SDimitry Andric
AMDGPULowerKernelAttributes()65eb11fae6SDimitry Andric AMDGPULowerKernelAttributes() : ModulePass(ID) {}
66eb11fae6SDimitry Andric
67eb11fae6SDimitry Andric bool runOnModule(Module &M) override;
68eb11fae6SDimitry Andric
getPassName() const69eb11fae6SDimitry Andric StringRef getPassName() const override {
70eb11fae6SDimitry Andric return "AMDGPU Kernel Attributes";
71eb11fae6SDimitry Andric }
72eb11fae6SDimitry Andric
getAnalysisUsage(AnalysisUsage & AU) const73eb11fae6SDimitry Andric void getAnalysisUsage(AnalysisUsage &AU) const override {
74eb11fae6SDimitry Andric AU.setPreservesAll();
75eb11fae6SDimitry Andric }
76eb11fae6SDimitry Andric };
77eb11fae6SDimitry Andric
getBasePtrIntrinsic(Module & M,bool IsV5OrAbove)78e3b55780SDimitry Andric Function *getBasePtrIntrinsic(Module &M, bool IsV5OrAbove) {
79e3b55780SDimitry Andric auto IntrinsicId = IsV5OrAbove ? Intrinsic::amdgcn_implicitarg_ptr
80e3b55780SDimitry Andric : Intrinsic::amdgcn_dispatch_ptr;
81e3b55780SDimitry Andric StringRef Name = Intrinsic::getName(IntrinsicId);
82e3b55780SDimitry Andric return M.getFunction(Name);
83e3b55780SDimitry Andric }
84e3b55780SDimitry Andric
85eb11fae6SDimitry Andric } // end anonymous namespace
86eb11fae6SDimitry Andric
processUse(CallInst * CI,bool IsV5OrAbove)87e3b55780SDimitry Andric static bool processUse(CallInst *CI, bool IsV5OrAbove) {
88eb11fae6SDimitry Andric Function *F = CI->getParent()->getParent();
89eb11fae6SDimitry Andric
90eb11fae6SDimitry Andric auto MD = F->getMetadata("reqd_work_group_size");
91eb11fae6SDimitry Andric const bool HasReqdWorkGroupSize = MD && MD->getNumOperands() == 3;
92eb11fae6SDimitry Andric
93eb11fae6SDimitry Andric const bool HasUniformWorkGroupSize =
94344a3780SDimitry Andric F->getFnAttribute("uniform-work-group-size").getValueAsBool();
95eb11fae6SDimitry Andric
96eb11fae6SDimitry Andric if (!HasReqdWorkGroupSize && !HasUniformWorkGroupSize)
97eb11fae6SDimitry Andric return false;
98eb11fae6SDimitry Andric
99e3b55780SDimitry Andric Value *BlockCounts[3] = {nullptr, nullptr, nullptr};
100e3b55780SDimitry Andric Value *GroupSizes[3] = {nullptr, nullptr, nullptr};
101e3b55780SDimitry Andric Value *Remainders[3] = {nullptr, nullptr, nullptr};
102e3b55780SDimitry Andric Value *GridSizes[3] = {nullptr, nullptr, nullptr};
103eb11fae6SDimitry Andric
104ac9a064cSDimitry Andric const DataLayout &DL = F->getDataLayout();
105eb11fae6SDimitry Andric
106eb11fae6SDimitry Andric // We expect to see several GEP users, casted to the appropriate type and
107eb11fae6SDimitry Andric // loaded.
108eb11fae6SDimitry Andric for (User *U : CI->users()) {
109eb11fae6SDimitry Andric if (!U->hasOneUse())
110eb11fae6SDimitry Andric continue;
111eb11fae6SDimitry Andric
112eb11fae6SDimitry Andric int64_t Offset = 0;
113e3b55780SDimitry Andric auto *Load = dyn_cast<LoadInst>(U); // Load from ImplicitArgPtr/DispatchPtr?
114e3b55780SDimitry Andric auto *BCI = dyn_cast<BitCastInst>(U);
115e3b55780SDimitry Andric if (!Load && !BCI) {
116eb11fae6SDimitry Andric if (GetPointerBaseWithConstantOffset(U, Offset, DL) != CI)
117eb11fae6SDimitry Andric continue;
118e3b55780SDimitry Andric Load = dyn_cast<LoadInst>(*U->user_begin()); // Load from GEP?
119e3b55780SDimitry Andric BCI = dyn_cast<BitCastInst>(*U->user_begin());
120e3b55780SDimitry Andric }
121eb11fae6SDimitry Andric
122e3b55780SDimitry Andric if (BCI) {
123e3b55780SDimitry Andric if (!BCI->hasOneUse())
124eb11fae6SDimitry Andric continue;
125e3b55780SDimitry Andric Load = dyn_cast<LoadInst>(*BCI->user_begin()); // Load from BCI?
126e3b55780SDimitry Andric }
127eb11fae6SDimitry Andric
128eb11fae6SDimitry Andric if (!Load || !Load->isSimple())
129eb11fae6SDimitry Andric continue;
130eb11fae6SDimitry Andric
131eb11fae6SDimitry Andric unsigned LoadSize = DL.getTypeStoreSize(Load->getType());
132eb11fae6SDimitry Andric
133eb11fae6SDimitry Andric // TODO: Handle merged loads.
134e3b55780SDimitry Andric if (IsV5OrAbove) { // Base is ImplicitArgPtr.
135e3b55780SDimitry Andric switch (Offset) {
136e3b55780SDimitry Andric case HIDDEN_BLOCK_COUNT_X:
137e3b55780SDimitry Andric if (LoadSize == 4)
138e3b55780SDimitry Andric BlockCounts[0] = Load;
139e3b55780SDimitry Andric break;
140e3b55780SDimitry Andric case HIDDEN_BLOCK_COUNT_Y:
141e3b55780SDimitry Andric if (LoadSize == 4)
142e3b55780SDimitry Andric BlockCounts[1] = Load;
143e3b55780SDimitry Andric break;
144e3b55780SDimitry Andric case HIDDEN_BLOCK_COUNT_Z:
145e3b55780SDimitry Andric if (LoadSize == 4)
146e3b55780SDimitry Andric BlockCounts[2] = Load;
147e3b55780SDimitry Andric break;
148e3b55780SDimitry Andric case HIDDEN_GROUP_SIZE_X:
149e3b55780SDimitry Andric if (LoadSize == 2)
150e3b55780SDimitry Andric GroupSizes[0] = Load;
151e3b55780SDimitry Andric break;
152e3b55780SDimitry Andric case HIDDEN_GROUP_SIZE_Y:
153e3b55780SDimitry Andric if (LoadSize == 2)
154e3b55780SDimitry Andric GroupSizes[1] = Load;
155e3b55780SDimitry Andric break;
156e3b55780SDimitry Andric case HIDDEN_GROUP_SIZE_Z:
157e3b55780SDimitry Andric if (LoadSize == 2)
158e3b55780SDimitry Andric GroupSizes[2] = Load;
159e3b55780SDimitry Andric break;
160e3b55780SDimitry Andric case HIDDEN_REMAINDER_X:
161e3b55780SDimitry Andric if (LoadSize == 2)
162e3b55780SDimitry Andric Remainders[0] = Load;
163e3b55780SDimitry Andric break;
164e3b55780SDimitry Andric case HIDDEN_REMAINDER_Y:
165e3b55780SDimitry Andric if (LoadSize == 2)
166e3b55780SDimitry Andric Remainders[1] = Load;
167e3b55780SDimitry Andric break;
168e3b55780SDimitry Andric case HIDDEN_REMAINDER_Z:
169e3b55780SDimitry Andric if (LoadSize == 2)
170e3b55780SDimitry Andric Remainders[2] = Load;
171e3b55780SDimitry Andric break;
172e3b55780SDimitry Andric default:
173e3b55780SDimitry Andric break;
174e3b55780SDimitry Andric }
175e3b55780SDimitry Andric } else { // Base is DispatchPtr.
176eb11fae6SDimitry Andric switch (Offset) {
177eb11fae6SDimitry Andric case WORKGROUP_SIZE_X:
178eb11fae6SDimitry Andric if (LoadSize == 2)
179e3b55780SDimitry Andric GroupSizes[0] = Load;
180eb11fae6SDimitry Andric break;
181eb11fae6SDimitry Andric case WORKGROUP_SIZE_Y:
182eb11fae6SDimitry Andric if (LoadSize == 2)
183e3b55780SDimitry Andric GroupSizes[1] = Load;
184eb11fae6SDimitry Andric break;
185eb11fae6SDimitry Andric case WORKGROUP_SIZE_Z:
186eb11fae6SDimitry Andric if (LoadSize == 2)
187e3b55780SDimitry Andric GroupSizes[2] = Load;
188eb11fae6SDimitry Andric break;
189eb11fae6SDimitry Andric case GRID_SIZE_X:
190eb11fae6SDimitry Andric if (LoadSize == 4)
191e3b55780SDimitry Andric GridSizes[0] = Load;
192eb11fae6SDimitry Andric break;
193eb11fae6SDimitry Andric case GRID_SIZE_Y:
194eb11fae6SDimitry Andric if (LoadSize == 4)
195e3b55780SDimitry Andric GridSizes[1] = Load;
196eb11fae6SDimitry Andric break;
197eb11fae6SDimitry Andric case GRID_SIZE_Z:
198eb11fae6SDimitry Andric if (LoadSize == 4)
199e3b55780SDimitry Andric GridSizes[2] = Load;
200eb11fae6SDimitry Andric break;
201eb11fae6SDimitry Andric default:
202eb11fae6SDimitry Andric break;
203eb11fae6SDimitry Andric }
204eb11fae6SDimitry Andric }
205e3b55780SDimitry Andric }
206eb11fae6SDimitry Andric
207e3b55780SDimitry Andric bool MadeChange = false;
208e3b55780SDimitry Andric if (IsV5OrAbove && HasUniformWorkGroupSize) {
209e3b55780SDimitry Andric // Under v5 __ockl_get_local_size returns the value computed by the expression:
210e3b55780SDimitry Andric //
211e3b55780SDimitry Andric // workgroup_id < hidden_block_count ? hidden_group_size : hidden_remainder
212e3b55780SDimitry Andric //
213e3b55780SDimitry Andric // For functions with the attribute uniform-work-group-size=true. we can evaluate
214e3b55780SDimitry Andric // workgroup_id < hidden_block_count as true, and thus hidden_group_size is returned
215e3b55780SDimitry Andric // for __ockl_get_local_size.
216e3b55780SDimitry Andric for (int I = 0; I < 3; ++I) {
217e3b55780SDimitry Andric Value *BlockCount = BlockCounts[I];
218e3b55780SDimitry Andric if (!BlockCount)
219e3b55780SDimitry Andric continue;
220e3b55780SDimitry Andric
221e3b55780SDimitry Andric using namespace llvm::PatternMatch;
222e3b55780SDimitry Andric auto GroupIDIntrin =
223e3b55780SDimitry Andric I == 0 ? m_Intrinsic<Intrinsic::amdgcn_workgroup_id_x>()
224e3b55780SDimitry Andric : (I == 1 ? m_Intrinsic<Intrinsic::amdgcn_workgroup_id_y>()
225e3b55780SDimitry Andric : m_Intrinsic<Intrinsic::amdgcn_workgroup_id_z>());
226e3b55780SDimitry Andric
227e3b55780SDimitry Andric for (User *ICmp : BlockCount->users()) {
228e3b55780SDimitry Andric ICmpInst::Predicate Pred;
229e3b55780SDimitry Andric if (match(ICmp, m_ICmp(Pred, GroupIDIntrin, m_Specific(BlockCount)))) {
230e3b55780SDimitry Andric if (Pred != ICmpInst::ICMP_ULT)
231e3b55780SDimitry Andric continue;
232e3b55780SDimitry Andric ICmp->replaceAllUsesWith(llvm::ConstantInt::getTrue(ICmp->getType()));
233e3b55780SDimitry Andric MadeChange = true;
234e3b55780SDimitry Andric }
235e3b55780SDimitry Andric }
236e3b55780SDimitry Andric }
237e3b55780SDimitry Andric
238e3b55780SDimitry Andric // All remainders should be 0 with uniform work group size.
239e3b55780SDimitry Andric for (Value *Remainder : Remainders) {
240e3b55780SDimitry Andric if (!Remainder)
241e3b55780SDimitry Andric continue;
242e3b55780SDimitry Andric Remainder->replaceAllUsesWith(Constant::getNullValue(Remainder->getType()));
243e3b55780SDimitry Andric MadeChange = true;
244e3b55780SDimitry Andric }
245e3b55780SDimitry Andric } else if (HasUniformWorkGroupSize) { // Pre-V5.
246eb11fae6SDimitry Andric // Pattern match the code used to handle partial workgroup dispatches in the
247eb11fae6SDimitry Andric // library implementation of get_local_size, so the entire function can be
248eb11fae6SDimitry Andric // constant folded with a known group size.
249eb11fae6SDimitry Andric //
250eb11fae6SDimitry Andric // uint r = grid_size - group_id * group_size;
251eb11fae6SDimitry Andric // get_local_size = (r < group_size) ? r : group_size;
252eb11fae6SDimitry Andric //
253eb11fae6SDimitry Andric // If we have uniform-work-group-size (which is the default in OpenCL 1.2),
254eb11fae6SDimitry Andric // the grid_size is required to be a multiple of group_size). In this case:
255eb11fae6SDimitry Andric //
256eb11fae6SDimitry Andric // grid_size - (group_id * group_size) < group_size
257eb11fae6SDimitry Andric // ->
258eb11fae6SDimitry Andric // grid_size < group_size + (group_id * group_size)
259eb11fae6SDimitry Andric //
260eb11fae6SDimitry Andric // (grid_size / group_size) < 1 + group_id
261eb11fae6SDimitry Andric //
262eb11fae6SDimitry Andric // grid_size / group_size is at least 1, so we can conclude the select
263eb11fae6SDimitry Andric // condition is false (except for group_id == 0, where the select result is
264eb11fae6SDimitry Andric // the same).
265e3b55780SDimitry Andric for (int I = 0; I < 3; ++I) {
266e3b55780SDimitry Andric Value *GroupSize = GroupSizes[I];
267eb11fae6SDimitry Andric Value *GridSize = GridSizes[I];
268eb11fae6SDimitry Andric if (!GroupSize || !GridSize)
269eb11fae6SDimitry Andric continue;
270eb11fae6SDimitry Andric
271145449b1SDimitry Andric using namespace llvm::PatternMatch;
272145449b1SDimitry Andric auto GroupIDIntrin =
273145449b1SDimitry Andric I == 0 ? m_Intrinsic<Intrinsic::amdgcn_workgroup_id_x>()
274145449b1SDimitry Andric : (I == 1 ? m_Intrinsic<Intrinsic::amdgcn_workgroup_id_y>()
275145449b1SDimitry Andric : m_Intrinsic<Intrinsic::amdgcn_workgroup_id_z>());
276145449b1SDimitry Andric
277eb11fae6SDimitry Andric for (User *U : GroupSize->users()) {
278eb11fae6SDimitry Andric auto *ZextGroupSize = dyn_cast<ZExtInst>(U);
279eb11fae6SDimitry Andric if (!ZextGroupSize)
280eb11fae6SDimitry Andric continue;
281eb11fae6SDimitry Andric
282145449b1SDimitry Andric for (User *UMin : ZextGroupSize->users()) {
283145449b1SDimitry Andric if (match(UMin,
284145449b1SDimitry Andric m_UMin(m_Sub(m_Specific(GridSize),
285145449b1SDimitry Andric m_Mul(GroupIDIntrin, m_Specific(ZextGroupSize))),
286145449b1SDimitry Andric m_Specific(ZextGroupSize)))) {
287eb11fae6SDimitry Andric if (HasReqdWorkGroupSize) {
288eb11fae6SDimitry Andric ConstantInt *KnownSize
289eb11fae6SDimitry Andric = mdconst::extract<ConstantInt>(MD->getOperand(I));
290b1c73532SDimitry Andric UMin->replaceAllUsesWith(ConstantFoldIntegerCast(
291b1c73532SDimitry Andric KnownSize, UMin->getType(), false, DL));
292eb11fae6SDimitry Andric } else {
293145449b1SDimitry Andric UMin->replaceAllUsesWith(ZextGroupSize);
294eb11fae6SDimitry Andric }
295eb11fae6SDimitry Andric
296eb11fae6SDimitry Andric MadeChange = true;
297eb11fae6SDimitry Andric }
298eb11fae6SDimitry Andric }
299eb11fae6SDimitry Andric }
300eb11fae6SDimitry Andric }
301e3b55780SDimitry Andric }
302eb11fae6SDimitry Andric
303e3b55780SDimitry Andric // If reqd_work_group_size is set, we can replace work group size with it.
304eb11fae6SDimitry Andric if (!HasReqdWorkGroupSize)
305eb11fae6SDimitry Andric return MadeChange;
306eb11fae6SDimitry Andric
307e3b55780SDimitry Andric for (int I = 0; I < 3; I++) {
308e3b55780SDimitry Andric Value *GroupSize = GroupSizes[I];
309eb11fae6SDimitry Andric if (!GroupSize)
310eb11fae6SDimitry Andric continue;
311eb11fae6SDimitry Andric
312eb11fae6SDimitry Andric ConstantInt *KnownSize = mdconst::extract<ConstantInt>(MD->getOperand(I));
313eb11fae6SDimitry Andric GroupSize->replaceAllUsesWith(
314b1c73532SDimitry Andric ConstantFoldIntegerCast(KnownSize, GroupSize->getType(), false, DL));
315eb11fae6SDimitry Andric MadeChange = true;
316eb11fae6SDimitry Andric }
317eb11fae6SDimitry Andric
318eb11fae6SDimitry Andric return MadeChange;
319eb11fae6SDimitry Andric }
320eb11fae6SDimitry Andric
321e3b55780SDimitry Andric
322eb11fae6SDimitry Andric // TODO: Move makeLIDRangeMetadata usage into here. Seem to not get
323eb11fae6SDimitry Andric // TargetPassConfig for subtarget.
runOnModule(Module & M)324eb11fae6SDimitry Andric bool AMDGPULowerKernelAttributes::runOnModule(Module &M) {
325e3b55780SDimitry Andric bool MadeChange = false;
3264df029ccSDimitry Andric bool IsV5OrAbove =
3274df029ccSDimitry Andric AMDGPU::getAMDHSACodeObjectVersion(M) >= AMDGPU::AMDHSA_COV5;
328e3b55780SDimitry Andric Function *BasePtr = getBasePtrIntrinsic(M, IsV5OrAbove);
329eb11fae6SDimitry Andric
330e3b55780SDimitry Andric if (!BasePtr) // ImplicitArgPtr/DispatchPtr not used.
331eb11fae6SDimitry Andric return false;
332eb11fae6SDimitry Andric
333eb11fae6SDimitry Andric SmallPtrSet<Instruction *, 4> HandledUses;
334e3b55780SDimitry Andric for (auto *U : BasePtr->users()) {
335eb11fae6SDimitry Andric CallInst *CI = cast<CallInst>(U);
336eb11fae6SDimitry Andric if (HandledUses.insert(CI).second) {
337e3b55780SDimitry Andric if (processUse(CI, IsV5OrAbove))
338eb11fae6SDimitry Andric MadeChange = true;
339eb11fae6SDimitry Andric }
340eb11fae6SDimitry Andric }
341eb11fae6SDimitry Andric
342eb11fae6SDimitry Andric return MadeChange;
343eb11fae6SDimitry Andric }
344eb11fae6SDimitry Andric
345e3b55780SDimitry Andric
346eb11fae6SDimitry Andric INITIALIZE_PASS_BEGIN(AMDGPULowerKernelAttributes, DEBUG_TYPE,
347344a3780SDimitry Andric "AMDGPU Kernel Attributes", false, false)
348344a3780SDimitry Andric INITIALIZE_PASS_END(AMDGPULowerKernelAttributes, DEBUG_TYPE,
349344a3780SDimitry Andric "AMDGPU Kernel Attributes", false, false)
350eb11fae6SDimitry Andric
351eb11fae6SDimitry Andric char AMDGPULowerKernelAttributes::ID = 0;
352eb11fae6SDimitry Andric
createAMDGPULowerKernelAttributesPass()353eb11fae6SDimitry Andric ModulePass *llvm::createAMDGPULowerKernelAttributesPass() {
354eb11fae6SDimitry Andric return new AMDGPULowerKernelAttributes();
355eb11fae6SDimitry Andric }
356b60736ecSDimitry Andric
357b60736ecSDimitry Andric PreservedAnalyses
run(Function & F,FunctionAnalysisManager & AM)358b60736ecSDimitry Andric AMDGPULowerKernelAttributesPass::run(Function &F, FunctionAnalysisManager &AM) {
3597fa27ce4SDimitry Andric bool IsV5OrAbove =
3604df029ccSDimitry Andric AMDGPU::getAMDHSACodeObjectVersion(*F.getParent()) >= AMDGPU::AMDHSA_COV5;
361e3b55780SDimitry Andric Function *BasePtr = getBasePtrIntrinsic(*F.getParent(), IsV5OrAbove);
362b60736ecSDimitry Andric
363e3b55780SDimitry Andric if (!BasePtr) // ImplicitArgPtr/DispatchPtr not used.
364b60736ecSDimitry Andric return PreservedAnalyses::all();
365b60736ecSDimitry Andric
366b60736ecSDimitry Andric for (Instruction &I : instructions(F)) {
367b60736ecSDimitry Andric if (CallInst *CI = dyn_cast<CallInst>(&I)) {
368e3b55780SDimitry Andric if (CI->getCalledFunction() == BasePtr)
369e3b55780SDimitry Andric processUse(CI, IsV5OrAbove);
370b60736ecSDimitry Andric }
371b60736ecSDimitry Andric }
372b60736ecSDimitry Andric
373b60736ecSDimitry Andric return PreservedAnalyses::all();
374b60736ecSDimitry Andric }
375