28#include "llvm/IR/IntrinsicsAMDGPU.h"
34#define DEBUG_TYPE "amdgpu-lower-kernel-arguments"
78 if (Arg.hasNoAliasAttr() && !Arg.use_empty())
81 if (NoAliasArgs.
empty())
92 for (
unsigned I = 0u;
I < NoAliasArgs.
size(); ++
I) {
95 NewScopes.
insert({Arg, NewScope});
100 Inst != InstEnd; ++Inst) {
105 bool IsUnrestrictedCall =
false;
120 if (!Arg->getType()->isPointerTy())
135 for (
const Value *Val : PtrArgs) {
141 bool RequiresNoCaptureBefore =
false;
142 bool UsesUnknownObject =
false;
143 bool UsesAliasingPtr =
false;
145 for (
const Value *Val : ObjSet) {
150 if (!Arg->hasAttribute(Attribute::NoAlias))
151 UsesAliasingPtr =
true;
153 UsesAliasingPtr =
true;
157 RequiresNoCaptureBefore =
true;
160 UsesUnknownObject =
true;
164 if (UsesUnknownObject)
167 if (IsUnrestrictedCall)
168 RequiresNoCaptureBefore =
true;
172 if (!UsesAliasingPtr && !IsUnrestrictedCall)
173 for (
const Argument *Arg : NoAliasArgs) {
174 if (ObjSet.
count(Arg))
175 Scopes.push_back(NewScopes[Arg]);
182 for (
const Argument *Arg : NoAliasArgs) {
186 if (!RequiresNoCaptureBefore ||
197 Inst->setMetadata(LLVMContext::MD_noalias, NewMD);
201 if (!Scopes.empty()) {
205 Inst->setMetadata(LLVMContext::MD_alias_scope, NewMD);
222 const Align KernArgBaseAlign(16);
223 const uint64_t BaseOffset = ST.getExplicitKernelArgOffset();
227 const uint64_t TotalKernArgSize = ST.getKernArgSegmentSize(
F, MaxAlign);
228 if (TotalKernArgSize == 0)
231 CallInst *KernArgSegment = Builder.CreateIntrinsicWithoutFolding(
232 Intrinsic::amdgcn_kernarg_segment_ptr, {},
nullptr,
233 F.getName() +
".kernarg.segment");
234 KernArgSegment->
addRetAttr(Attribute::NonNull);
243 const bool IsByRef = Arg.hasByRefAttr();
244 Type *ArgTy = IsByRef ? Arg.getParamByRefType() : Arg.getType();
245 MaybeAlign ParamAlign = IsByRef ? Arg.getParamAlign() : std::nullopt;
246 Align ABITypeAlign =
DL.getValueOrABITypeAlignment(ParamAlign, ArgTy);
249 uint64_t AllocSize =
DL.getTypeAllocSize(ArgTy);
251 uint64_t EltOffset =
alignTo(ExplicitArgOffset, ABITypeAlign) + BaseOffset;
252 ExplicitArgOffset =
alignTo(ExplicitArgOffset, ABITypeAlign) + AllocSize;
255 if (Arg.use_empty() || Arg.hasInRegAttr())
261 Value *ArgOffsetPtr = Builder.CreateConstInBoundsGEP1_64(
262 Builder.getInt8Ty(), KernArgSegment, EltOffset,
263 Arg.getName() +
".byval.kernarg.offset");
265 Value *CastOffsetPtr =
266 Builder.CreateAddrSpaceCast(ArgOffsetPtr, Arg.
getType());
278 !ST.hasUsableDSOffset())
283 bool IsV3 = VT && VT->getNumElements() == 3;
288 int64_t AlignDownOffset =
alignDown(EltOffset, 4);
289 int64_t OffsetDiff = EltOffset - AlignDownOffset;
291 KernArgBaseAlign, DoShiftOpt ? AlignDownOffset : EltOffset);
303 ArgPtr = Builder.CreateConstInBoundsGEP1_64(
304 Builder.getInt8Ty(), KernArgSegment, AlignDownOffset,
305 Arg.getName() +
".kernarg.offset.align.down");
308 ArgPtr = Builder.CreateConstInBoundsGEP1_64(
309 Builder.getInt8Ty(), KernArgSegment, EltOffset,
310 Arg.getName() +
".kernarg.offset");
311 AdjustedArgTy = ArgTy;
314 if (IsV3 &&
Size >= 32) {
317 AdjustedArgTy = V4Ty;
321 Builder.CreateAlignedLoad(AdjustedArgTy, ArgPtr, AdjustedAlign);
322 Load->setMetadata(LLVMContext::MD_invariant_load,
MDNode::get(Ctx, {}));
326 if (Arg.hasAttribute(Attribute::NoUndef) && AdjustedArgTy == ArgTy)
329 if (Arg.hasAttribute(Attribute::Range) && AdjustedArgTy == ArgTy) {
331 Arg.getAttribute(Attribute::Range).getValueAsConstantRange();
332 Load->setMetadata(LLVMContext::MD_range,
336 if (Arg.hasAttribute(Attribute::NoFPClass) && AdjustedArgTy == ArgTy) {
339 LLVMContext::MD_nofpclass,
345 if (Arg.hasNonNullAttr())
348 uint64_t DerefBytes = Arg.getDereferenceableBytes();
349 if (DerefBytes != 0) {
351 LLVMContext::MD_dereferenceable,
354 ConstantInt::get(Builder.getInt64Ty(), DerefBytes))));
357 uint64_t DerefOrNullBytes = Arg.getDereferenceableOrNullBytes();
358 if (DerefOrNullBytes != 0) {
360 LLVMContext::MD_dereferenceable_or_null,
363 DerefOrNullBytes))));
366 if (
MaybeAlign ParamAlign = Arg.getParamAlign()) {
368 LLVMContext::MD_align,
370 Builder.getInt64Ty(), ParamAlign->value()))));
375 Value *ExtractBits = OffsetDiff == 0 ?
376 Load : Builder.CreateLShr(
Load, OffsetDiff * 8);
379 Value *Trunc = Builder.CreateTrunc(ExtractBits, ArgIntTy);
380 Value *NewVal = Builder.CreateBitCast(Trunc, ArgTy,
381 Arg.getName() +
".load");
385 Arg.getName() +
".load");
388 Load->setName(Arg.getName() +
".load");
389 Arg.replaceAllUsesWith(
Load);
399bool AMDGPULowerKernelArguments::runOnFunction(
Function &
F) {
400 auto &TPC = getAnalysis<TargetPassConfig>();
401 const TargetMachine &TM = TPC.getTM<TargetMachine>();
402 DominatorTree &DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree();
407 "AMDGPU Lower Kernel Arguments",
false,
false)
411char AMDGPULowerKernelArguments::ID = 0;
414 return new AMDGPULowerKernelArguments();
static void addAliasScopeMetadata(Function &F, const DataLayout &DL, DominatorTree &DT)
static BasicBlock::iterator getInsertPt(BasicBlock &BB)
static bool lowerKernelArguments(Function &F, const TargetMachine &TM, DominatorTree &DT)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file contains the simple types necessary to represent the attributes associated with functions a...
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static bool runOnFunction(Function &F, bool PostInlining)
AMD GCN specific subclass of TargetSubtarget.
static cl::opt< bool > NoAliases("loongarch-no-aliases", cl::desc("Disable the emission of assembler pseudo instructions"), cl::init(false), cl::Hidden)
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This is the interface for a metadata-based scoped no-alias analysis.
Target-Independent Code Generator Pass Configuration Options pass.
PreservedAnalyses run(Function &, FunctionAnalysisManager &)
an instruction to allocate memory on the stack
LLVM_ABI bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
This class represents an incoming formal argument to a Function.
Represent a constant reference to an array (0 or more elements consecutively in memory),...
static LLVM_ABI Attribute getWithDereferenceableBytes(LLVMContext &Context, uint64_t Bytes)
static LLVM_ABI Attribute getWithAlignment(LLVMContext &Context, Align Alignment)
Return a uniquified Attribute object that has the specific alignment set.
LLVM Basic Block Representation.
LLVM_ABI const_iterator getFirstInsertionPt() const
Returns an iterator to the first instruction in this block that is suitable for inserting a non-PHI i...
InstListType::iterator iterator
Instruction iterators...
Represents analyses that only rely on functions' control flow.
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
void addRetAttr(Attribute::AttrKind Kind)
Adds the attribute to the return value.
This class represents a function call, abstracting a target machine's calling convention.
This class represents a range of values.
A parsed version of the target data layout string in and methods for querying it.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Analysis pass which computes a DominatorTree.
Legacy analysis pass which computes a DominatorTree.
Concrete subclass of DominatorTreeBase that is used to compute a normal dominator tree.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
FunctionPass class - This class is used to implement most global optimizations.
This provides a uniform API for creating instructions and inserting them into a basic block: either a...
Class to represent integer types.
This is an important class for using LLVM in a threaded context.
An instruction for reading from memory.
MDNode * createAnonymousAliasScope(MDNode *Domain, StringRef Name=StringRef())
Return metadata appropriate for an alias scope root node.
LLVM_ABI ConstantAsMetadata * createConstant(Constant *C)
Return the given constant as metadata.
LLVM_ABI MDNode * createRange(const APInt &Lo, const APInt &Hi)
Return metadata describing the range [Lo, Hi).
LLVM_ABI MDNode * createAnonymousAliasScopeDomain(StringRef Description=StringRef(), bool DisjointScopes=false)
Return metadata appropriate for an alias scope domain node, described by Description,...
static LLVM_ABI MDNode * concatenate(MDNode *A, MDNode *B)
Methods for metadata merging.
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
bool doesNotAccessMemory() const
Whether this function accesses no memory.
bool onlyAccessesInaccessibleMem() const
Whether this function only (at most) accesses inaccessible memory.
bool onlyAccessesArgPointees() const
Whether this function only (at most) accesses argument memory.
static LLVM_ABI std::optional< MemoryLocation > getOrNone(const Instruction *Inst)
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.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
size_type count(ConstPtrType Ptr) const
count - Return 1 if the specified pointer is in the set, 0 otherwise.
void insert_range(Range &&R)
bool contains(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Primary interface to the complete machine description for the target machine.
const STC & getSubtarget(const Function &F) const
This method returns a pointer to the specified type of TargetSubtargetInfo.
Target-Independent Code Generator Pass Configuration Options.
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
bool isAggregateType() const
Return true if the type is an aggregate type.
static LLVM_ABI IntegerType * getIntNTy(LLVMContext &C, unsigned N)
LLVM Value Representation.
Type * getType() const
All values are typed, get the type of this value.
LLVM_ABI void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
@ REGION_ADDRESS
Address space for region memory. (GDS)
@ LOCAL_ADDRESS
Address space for local memory.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ AMDGPU_KERNEL
Used for AMDGPU code object kernels.
This is an optimization pass for GlobalISel generic memory operations.
InstIterator< SymbolTableList< BasicBlock >, Function::iterator, BasicBlock::iterator, Instruction > inst_iterator
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
@ Load
The value being inserted comes from a load (InsertElement only).
constexpr T alignDown(U Value, V Align, W Skew=0)
Returns the largest unsigned integer less than or equal to Value and is Skew mod Align.
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
inst_iterator inst_begin(Function *F)
LLVM_ABI bool PointerMayBeCapturedBefore(const Value *V, bool ReturnCaptures, const Instruction *I, const DominatorTree *DT, bool IncludeI=false, unsigned MaxUsesToExplore=0, const LoopInfo *LI=nullptr)
PointerMayBeCapturedBefore - Return true if this pointer value may be captured by the enclosing funct...
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
FunctionPass * createAMDGPULowerKernelArgumentsPass()
constexpr uint64_t alignTo(uint64_t Size, Align A)
Returns a multiple of A needed to store Size bytes.
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
inst_iterator inst_end(Function *F)
LLVM_ABI bool isEscapeSource(const Value *V)
Returns true if the pointer is one which would have been considered an escape by isNotCapturedBefore.
Align commonAlignment(Align A, uint64_t Offset)
Returns the alignment that satisfies both alignments.
bool capturesAnything(CaptureComponents CC)
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
LLVM_ABI void getUnderlyingObjects(const Value *V, SmallVectorImpl< const Value * > &Objects, const LoopInfo *LI=nullptr, unsigned MaxLookup=MaxLookupSearchDepth)
This method is similar to getUnderlyingObject except that it can look through phi and select instruct...
LLVM_ABI bool isIdentifiedObject(const Value *V)
Return true if this pointer refers to a distinct and identifiable object.
This struct is a compact representation of a valid (non-zero power of two) alignment.
This struct is a compact representation of a valid (power of two) or undefined (0) alignment.