25#include "llvm/IR/IntrinsicsAMDGPU.h"
29#define DEBUG_TYPE "amdgpu-lower-kernel-attributes"
36enum DispatchPackedOffsets {
47enum ImplicitArgOffsets {
48 HIDDEN_BLOCK_COUNT_X = 0,
49 HIDDEN_BLOCK_COUNT_Y = 4,
50 HIDDEN_BLOCK_COUNT_Z = 8,
52 HIDDEN_GROUP_SIZE_X = 12,
53 HIDDEN_GROUP_SIZE_Y = 14,
54 HIDDEN_GROUP_SIZE_Z = 16,
56 HIDDEN_REMAINDER_X = 18,
57 HIDDEN_REMAINDER_Y = 20,
58 HIDDEN_REMAINDER_Z = 22,
61class AMDGPULowerKernelAttributes :
public ModulePass {
70 return "AMDGPU Kernel Attributes";
79 auto IntrinsicId = IsV5OrAbove ? Intrinsic::amdgcn_implicitarg_ptr
80 : Intrinsic::amdgcn_dispatch_ptr;
82 return M.getFunction(
Name);
90 auto MD =
F->getMetadata(
"reqd_work_group_size");
91 const bool HasReqdWorkGroupSize = MD && MD->getNumOperands() == 3;
93 const bool HasUniformWorkGroupSize =
94 F->getFnAttribute(
"uniform-work-group-size").getValueAsBool();
96 if (!HasReqdWorkGroupSize && !HasUniformWorkGroupSize)
99 Value *BlockCounts[3] = {
nullptr,
nullptr,
nullptr};
100 Value *GroupSizes[3] = {
nullptr,
nullptr,
nullptr};
101 Value *Remainders[3] = {
nullptr,
nullptr,
nullptr};
102 Value *GridSizes[3] = {
nullptr,
nullptr,
nullptr};
113 auto *Load = dyn_cast<LoadInst>(U);
114 auto *BCI = dyn_cast<BitCastInst>(U);
118 Load = dyn_cast<LoadInst>(*U->user_begin());
119 BCI = dyn_cast<BitCastInst>(*U->user_begin());
123 if (!BCI->hasOneUse())
125 Load = dyn_cast<LoadInst>(*BCI->user_begin());
128 if (!Load || !Load->isSimple())
131 unsigned LoadSize =
DL.getTypeStoreSize(Load->getType());
136 case HIDDEN_BLOCK_COUNT_X:
138 BlockCounts[0] = Load;
140 case HIDDEN_BLOCK_COUNT_Y:
142 BlockCounts[1] = Load;
144 case HIDDEN_BLOCK_COUNT_Z:
146 BlockCounts[2] = Load;
148 case HIDDEN_GROUP_SIZE_X:
150 GroupSizes[0] = Load;
152 case HIDDEN_GROUP_SIZE_Y:
154 GroupSizes[1] = Load;
156 case HIDDEN_GROUP_SIZE_Z:
158 GroupSizes[2] = Load;
160 case HIDDEN_REMAINDER_X:
162 Remainders[0] = Load;
164 case HIDDEN_REMAINDER_Y:
166 Remainders[1] = Load;
168 case HIDDEN_REMAINDER_Z:
170 Remainders[2] = Load;
177 case WORKGROUP_SIZE_X:
179 GroupSizes[0] = Load;
181 case WORKGROUP_SIZE_Y:
183 GroupSizes[1] = Load;
185 case WORKGROUP_SIZE_Z:
187 GroupSizes[2] = Load;
207 bool MadeChange =
false;
208 if (IsV5OrAbove && HasUniformWorkGroupSize) {
216 for (
int I = 0;
I < 3; ++
I) {
217 Value *BlockCount = BlockCounts[
I];
223 I == 0 ? m_Intrinsic<Intrinsic::amdgcn_workgroup_id_x>()
224 : (
I == 1 ? m_Intrinsic<Intrinsic::amdgcn_workgroup_id_y>()
225 : m_Intrinsic<Intrinsic::amdgcn_workgroup_id_z>());
237 for (
Value *Remainder : Remainders) {
243 }
else if (HasUniformWorkGroupSize) {
263 for (
int I = 0;
I < 3; ++
I) {
264 Value *GroupSize = GroupSizes[
I];
265 Value *GridSize = GridSizes[
I];
266 if (!GroupSize || !GridSize)
271 I == 0 ? m_Intrinsic<Intrinsic::amdgcn_workgroup_id_x>()
272 : (
I == 1 ? m_Intrinsic<Intrinsic::amdgcn_workgroup_id_y>()
273 : m_Intrinsic<Intrinsic::amdgcn_workgroup_id_z>());
276 auto *ZextGroupSize = dyn_cast<ZExtInst>(U);
280 for (
User *
UMin : ZextGroupSize->users()) {
285 if (HasReqdWorkGroupSize) {
287 = mdconst::extract<ConstantInt>(MD->getOperand(
I));
289 KnownSize,
UMin->getType(),
false,
DL));
291 UMin->replaceAllUsesWith(ZextGroupSize);
302 if (!HasReqdWorkGroupSize)
305 for (
int I = 0;
I < 3;
I++) {
306 Value *GroupSize = GroupSizes[
I];
310 ConstantInt *KnownSize = mdconst::extract<ConstantInt>(MD->getOperand(
I));
322bool AMDGPULowerKernelAttributes::runOnModule(
Module &M) {
323 bool MadeChange =
false;
332 for (
auto *U :
BasePtr->users()) {
334 if (HandledUses.
insert(CI).second) {
345 "AMDGPU Kernel Attributes",
false,
false)
349char AMDGPULowerKernelAttributes::
ID = 0;
352 return new AMDGPULowerKernelAttributes();
359 Function *BasePtr = getBasePtrIntrinsic(*
F.getParent(), IsV5OrAbove);
365 if (
CallInst *CI = dyn_cast<CallInst>(&
I)) {
static bool processUse(CallInst *CI, bool IsV5OrAbove)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
Expand Atomic instructions
This file contains the declarations for the subclasses of Constant, which represent the different fla...
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Target-Independent Code Generator Pass Configuration Options pass.
A container for analyses that lazily runs them and caches their results.
Represent the analysis usage information of a pass.
void setPreservesAll()
Set by analyses that do not transform their input at all.
Function * getCalledFunction() const
Returns the function called, or null if this is an indirect function invocation or the function signa...
This class represents a function call, abstracting a target machine's calling convention.
This is the shared class of boolean and integer constants.
static ConstantInt * getTrue(LLVMContext &Context)
static Constant * getNullValue(Type *Ty)
Constructor to create a '0' constant of arbitrary type.
A parsed version of the target data layout string in and methods for querying it.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
A set of analyses that are preserved following a run of a transformation pass.
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
StringRef - Represent a constant reference to a string, i.e.
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
iterator_range< user_iterator > users()
const ParentTy * getParent() const
unsigned getAMDHSACodeObjectVersion(const Module &M)
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
StringRef getName(ID id)
Return the LLVM name for an intrinsic, such as "llvm.ppc.altivec.lvx".
bool match(Val *V, const Pattern &P)
specificval_ty m_Specific(const Value *V)
Match if we have a specific specified value.
BinaryOp_match< LHS, RHS, Instruction::Mul > m_Mul(const LHS &L, const RHS &R)
SpecificCmpClass_match< LHS, RHS, ICmpInst, ICmpInst::Predicate > m_SpecificICmp(ICmpInst::Predicate MatchPred, const LHS &L, const RHS &R)
BinaryOp_match< LHS, RHS, Instruction::Sub > m_Sub(const LHS &L, const RHS &R)
MaxMin_match< ICmpInst, LHS, RHS, umin_pred_ty > m_UMin(const LHS &L, const RHS &R)
This is an optimization pass for GlobalISel generic memory operations.
Value * GetPointerBaseWithConstantOffset(Value *Ptr, int64_t &Offset, const DataLayout &DL, bool AllowNonInbounds=true)
Analyze the specified pointer to see if it can be expressed as a base pointer plus a constant offset.
ModulePass * createAMDGPULowerKernelAttributesPass()
@ UMin
Unsigned integer min implemented in terms of select(cmp()).
Constant * ConstantFoldIntegerCast(Constant *C, Type *DestTy, bool IsSigned, const DataLayout &DL)
Constant fold a zext, sext or trunc, depending on IsSigned and whether the DestTy is wider or narrowe...
PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM)