1919// ===----------------------------------------------------------------------===//
2020
2121#include " AMDGPU.h"
22+ #include " AMDGPUArgumentUsageInfo.h"
2223#include " AMDGPUTargetMachine.h"
2324#include " llvm/Analysis/ValueTracking.h"
2425#include " llvm/IR/Function.h"
3233#define DEBUG_TYPE " amdgpu-preload-kernel-arguments"
3334
3435using namespace llvm ;
36+ using namespace llvm ::KernArgPreload;
3537
3638static cl::opt<unsigned > KernargPreloadCount (
3739 " amdgpu-kernarg-preload-count" ,
@@ -60,59 +62,6 @@ class PreloadKernelArgInfo {
6062 const GCNSubtarget &ST;
6163 unsigned NumFreeUserSGPRs;
6264
63- enum HiddenArg : unsigned {
64- HIDDEN_BLOCK_COUNT_X,
65- HIDDEN_BLOCK_COUNT_Y,
66- HIDDEN_BLOCK_COUNT_Z,
67- HIDDEN_GROUP_SIZE_X,
68- HIDDEN_GROUP_SIZE_Y,
69- HIDDEN_GROUP_SIZE_Z,
70- HIDDEN_REMAINDER_X,
71- HIDDEN_REMAINDER_Y,
72- HIDDEN_REMAINDER_Z,
73- END_HIDDEN_ARGS
74- };
75-
76- // Stores information about a specific hidden argument.
77- struct HiddenArgInfo {
78- // Offset in bytes from the location in the kernearg segment pointed to by
79- // the implicitarg pointer.
80- uint8_t Offset;
81- // The size of the hidden argument in bytes.
82- uint8_t Size;
83- // The name of the hidden argument in the kernel signature.
84- const char *Name;
85- };
86-
87- static constexpr HiddenArgInfo HiddenArgs[END_HIDDEN_ARGS] = {
88- {0 , 4 , " _hidden_block_count_x" }, {4 , 4 , " _hidden_block_count_y" },
89- {8 , 4 , " _hidden_block_count_z" }, {12 , 2 , " _hidden_group_size_x" },
90- {14 , 2 , " _hidden_group_size_y" }, {16 , 2 , " _hidden_group_size_z" },
91- {18 , 2 , " _hidden_remainder_x" }, {20 , 2 , " _hidden_remainder_y" },
92- {22 , 2 , " _hidden_remainder_z" }};
93-
94- static HiddenArg getHiddenArgFromOffset (unsigned Offset) {
95- for (unsigned I = 0 ; I < END_HIDDEN_ARGS; ++I)
96- if (HiddenArgs[I].Offset == Offset)
97- return static_cast <HiddenArg>(I);
98-
99- return END_HIDDEN_ARGS;
100- }
101-
102- static Type *getHiddenArgType (LLVMContext &Ctx, HiddenArg HA) {
103- if (HA < END_HIDDEN_ARGS)
104- return Type::getIntNTy (Ctx, HiddenArgs[HA].Size * 8 );
105-
106- llvm_unreachable (" Unexpected hidden argument." );
107- }
108-
109- static const char *getHiddenArgName (HiddenArg HA) {
110- if (HA < END_HIDDEN_ARGS)
111- return HiddenArgs[HA].Name ;
112-
113- llvm_unreachable (" Unexpected hidden argument." );
114- }
115-
11665 // Clones the function after adding implicit arguments to the argument list
11766 // and returns the new updated function. Preloaded implicit arguments are
11867 // added up to and including the last one that will be preloaded, indicated by
@@ -125,7 +74,7 @@ class PreloadKernelArgInfo {
12574 LLVMContext &Ctx = F.getParent ()->getContext ();
12675 SmallVector<Type *, 16 > FTypes (FT->param_begin (), FT->param_end ());
12776 for (unsigned I = 0 ; I <= LastPreloadIndex; ++I)
128- FTypes.push_back (getHiddenArgType (Ctx, HiddenArg (I)));
77+ FTypes.push_back (HiddenArgUtils:: getHiddenArgType (Ctx, HiddenArg (I)));
12978
13079 FunctionType *NFT =
13180 FunctionType::get (FT->getReturnType (), FTypes, FT->isVarArg ());
@@ -153,7 +102,7 @@ class PreloadKernelArgInfo {
153102 AttributeList AL = NF->getAttributes ();
154103 for (unsigned I = 0 ; I <= LastPreloadIndex; ++I) {
155104 AL = AL.addParamAttributes (Ctx, NFArg->getArgNo (), AB);
156- NFArg++->setName (getHiddenArgName (HiddenArg (I)));
105+ NFArg++->setName (HiddenArgUtils:: getHiddenArgName (HiddenArg (I)));
157106 }
158107
159108 NF->setAttributes (AL);
@@ -211,8 +160,9 @@ class PreloadKernelArgInfo {
211160 // FIXME: Expand handle merged loads.
212161 LLVMContext &Ctx = F.getParent ()->getContext ();
213162 Type *LoadTy = Load->getType ();
214- HiddenArg HA = getHiddenArgFromOffset (Offset);
215- if (HA == END_HIDDEN_ARGS || LoadTy != getHiddenArgType (Ctx, HA))
163+ HiddenArg HA = HiddenArgUtils::getHiddenArgFromOffset (Offset);
164+ if (HA == END_HIDDEN_ARGS ||
165+ LoadTy != HiddenArgUtils::getHiddenArgType (Ctx, HA))
216166 continue ;
217167
218168 ImplicitArgLoads.push_back (std::make_pair (Load, Offset));
@@ -243,14 +193,16 @@ class PreloadKernelArgInfo {
243193 if (PreloadEnd == ImplicitArgLoads.begin ())
244194 return ;
245195
246- unsigned LastHiddenArgIndex = getHiddenArgFromOffset (PreloadEnd[-1 ].second );
196+ unsigned LastHiddenArgIndex =
197+ HiddenArgUtils::getHiddenArgFromOffset (PreloadEnd[-1 ].second );
247198 Function *NF = cloneFunctionWithPreloadImplicitArgs (LastHiddenArgIndex);
248199 assert (NF);
249200 FunctionsToErase.push_back (&F);
250201 for (const auto *I = ImplicitArgLoads.begin (); I != PreloadEnd; ++I) {
251202 LoadInst *LoadInst = I->first ;
252203 unsigned LoadOffset = I->second ;
253- unsigned HiddenArgIndex = getHiddenArgFromOffset (LoadOffset);
204+ unsigned HiddenArgIndex =
205+ HiddenArgUtils::getHiddenArgFromOffset (LoadOffset);
254206 unsigned Index = NF->arg_size () - LastHiddenArgIndex + HiddenArgIndex - 1 ;
255207 Argument *Arg = NF->getArg (Index);
256208 LoadInst->replaceAllUsesWith (Arg);
0 commit comments