19#include "llvm/IR/IntrinsicsAMDGPU.h"
22#define DEBUG_TYPE "amdgpu-memory-utils"
45 if (
auto *TTy = dyn_cast<TargetExtType>(Ty))
47 if (
auto *STy = dyn_cast<StructType>(Ty)) {
48 if (STy->getNumElements() != 1)
50 Ty = STy->getElementType(0);
53 if (
auto *ATy = dyn_cast<ArrayType>(Ty)) {
54 Ty = ATy->getElementType();
63 return Ty->
getName() ==
"amdgcn.named.barrier" ? Ty :
nullptr;
112 for (
auto &GV : M.globals())
123 for (
auto &GV : M.globals()) {
126 for (
User *V : GV.users()) {
127 if (
auto *
I = dyn_cast<Instruction>(V)) {
152 if (
F.hasAddressTaken(
nullptr,
164 set_union(VariablesReachableThroughFunctionPointer, DirectMapFunction[
F]);
167 auto FunctionMakesUnknownCall = [&](
const Function *
F) ->
bool {
170 if (!R.second->getFunction())
182 if (!
F.isDeclaration() && FunctionMakesUnknownCall(&
F)) {
185 VariablesReachableThroughFunctionPointer);
192 for (
Function &Func : M.functions()) {
199 while (!wip.
empty()) {
204 set_union(TransitiveMapFunction[&Func], DirectMapFunction[
F]);
207 Function *Ith = R.second->getFunction();
221 set_union(VariablesReachableThroughFunctionPointer,
222 TransitiveMapFunction[
F]);
229 for (
Function &Func : M.functions()) {
234 Function *Ith = R.second->getFunction();
236 set_union(IndirectMapKernel[&Func], TransitiveMapFunction[Ith]);
242 bool SeesUnknownCalls = [&]() {
246 while (!WorkList.
empty()) {
250 if (!CallRecord.second)
253 Function *Callee = CallRecord.second->getFunction();
257 if (Visited.
insert(Callee).second)
264 if (SeesUnknownCalls) {
266 VariablesReachableThroughFunctionPointer);
276 std::optional<bool> HasAbsoluteGVs;
277 bool HasSpecialGVs =
false;
278 for (
auto &Map : {DirectMapKernel, IndirectMapKernel}) {
279 for (
auto &[Fn, GVs] : Map) {
280 for (
auto *GV : GVs) {
281 bool IsAbsolute = GV->isAbsoluteSymbolRef();
282 bool IsDirectMapDynLDSGV =
284 if (IsDirectMapDynLDSGV)
287 HasSpecialGVs =
true;
290 if (HasAbsoluteGVs.has_value()) {
291 if (*HasAbsoluteGVs != IsAbsolute) {
293 "module cannot mix absolute and non-absolute LDS GVs");
296 HasAbsoluteGVs = IsAbsolute;
303 if (HasAbsoluteGVs && *HasAbsoluteGVs)
306 return {std::move(DirectMapKernel), std::move(IndirectMapKernel),
317 bool SeenUnknownCall =
false;
319 while (!WorkList.
empty()) {
322 for (
auto &CallRecord : *CG[
F]) {
323 if (!CallRecord.second)
326 Function *Callee = CallRecord.second->getFunction();
328 if (!SeenUnknownCall) {
329 SeenUnknownCall =
true;
346 Callee->removeFnAttr(Attr);
347 if (Visited.
insert(Callee).second)
357 if (isa<FenceInst>(DefInst))
361 switch (
II->getIntrinsicID()) {
362 case Intrinsic::amdgcn_s_barrier:
363 case Intrinsic::amdgcn_s_barrier_signal:
364 case Intrinsic::amdgcn_s_barrier_signal_var:
365 case Intrinsic::amdgcn_s_barrier_signal_isfirst:
366 case Intrinsic::amdgcn_s_barrier_init:
367 case Intrinsic::amdgcn_s_barrier_join:
368 case Intrinsic::amdgcn_s_barrier_wait:
369 case Intrinsic::amdgcn_s_barrier_leave:
370 case Intrinsic::amdgcn_s_get_barrier_state:
371 case Intrinsic::amdgcn_wave_barrier:
372 case Intrinsic::amdgcn_sched_barrier:
373 case Intrinsic::amdgcn_sched_group_barrier:
374 case Intrinsic::amdgcn_iglp_opt:
383 const auto checkNoAlias = [AA,
Ptr](
auto I) ->
bool {
387 if (checkNoAlias(dyn_cast<AtomicCmpXchgInst>(DefInst)) ||
388 checkNoAlias(dyn_cast<AtomicRMWInst>(DefInst)))
401 LLVM_DEBUG(
dbgs() <<
"Checking clobbering of: " << *Load <<
'\n');
411 while (!WorkList.empty()) {
413 if (!Visited.
insert(MA).second)
419 if (
MemoryDef *Def = dyn_cast<MemoryDef>(MA)) {
432 const MemoryPhi *Phi = cast<MemoryPhi>(MA);
433 for (
const auto &
Use : Phi->incoming_values())
434 WorkList.push_back(cast<MemoryAccess>(&
Use));
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file provides interfaces used to build and manipulate a call graph, which is a very useful tool ...
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
uint64_t IntrinsicInst * II
This file defines generic set operations that may be used on set's of different types,...
A private abstract base class describing the concept of an individual alias analysis implementation.
bool isNoAlias(const MemoryLocation &LocA, const MemoryLocation &LocB)
A trivial helper function to check to see if the specified pointers are no-alias.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
std::pair< std::optional< WeakTrackingVH >, CallGraphNode * > CallRecord
A pair of the calling instruction (a call or invoke) and the call graph node being called.
The basic data container for the call graph of a Module of IR.
CallGraphNode * getExternalCallingNode() const
Returns the CallGraphNode which is used to represent undetermined calls into the callgraph.
A parsed version of the target data layout string in and methods for querying it.
bool contains(const_arg_type_t< KeyT > Val) const
Return true if the specified key is in the map, false otherwise.
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
Implements a dense probed hash-table based set.
const Function & getFunction() const
void removeFnAttr(Attribute::AttrKind Kind)
Remove function attributes from this function.
Module * getParent()
Get the module that this global value is contained inside of...
PointerType * getType() const
Global values are always pointers.
Type * getValueType() const
const Constant * getInitializer() const
getInitializer - Return the initializer for this global variable.
bool hasInitializer() const
Definitions have initializers, declarations don't.
bool isConstant() const
If the value is a global constant, its value is immutable throughout the runtime execution of the pro...
A wrapper class for inspecting calls to intrinsic functions.
An instruction for reading from memory.
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Representation for a specific memory location.
static LLVM_ABI MemoryLocation get(const LoadInst *LI)
Return a location with information about the memory reference by the given instruction.
Represents phi nodes for memory accesses.
This is the generic walker interface for walkers of MemorySSA.
MemoryAccess * getClobberingMemoryAccess(const Instruction *I, BatchAAResults &AA)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
Encapsulates MemorySSA, including all data associated with memory accesses.
LLVM_ABI MemorySSAWalker * getWalker()
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
A Module instance is used to store all the information related to an LLVM module.
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.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
Class to represent target extensions types, which are generally unintrospectable from target-independ...
StringRef getName() const
Return the name for this target extension type.
The instances of the Type class are immutable: once they are created, they are never changed.
LLVM_ABI unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
A Use represents the edge between a Value definition and its users.
LLVM Value Representation.
LLVM_ABI Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
std::pair< iterator, bool > insert(const ValueT &V)
bool contains(const_arg_type_t< ValueT > V) const
Check if the set contains the given element.
@ LOCAL_ADDRESS
Address space for local memory.
bool isDynamicLDS(const GlobalVariable &GV)
void removeFnAttrFromReachable(CallGraph &CG, Function *KernelRoot, ArrayRef< StringRef > FnAttrs)
Strip FnAttr attribute from any functions where we may have introduced its use.
LLVM_READNONE constexpr bool isKernel(CallingConv::ID CC)
void getUsesOfLDSByFunction(const CallGraph &CG, Module &M, FunctionVariableMap &kernels, FunctionVariableMap &Functions)
bool isReallyAClobber(const Value *Ptr, MemoryDef *Def, AAResults *AA)
Given a Def clobbering a load from Ptr according to the MSSA check if this is actually a memory updat...
LDSUsesInfoTy getTransitiveUsesOfLDS(const CallGraph &CG, Module &M)
static TargetExtType * getTargetExtType(const GlobalVariable &GV)
TargetExtType * isNamedBarrier(const GlobalVariable &GV)
bool isLDSVariableToLower(const GlobalVariable &GV)
bool eliminateConstantExprUsesOfLDSFromAllInstructions(Module &M)
Align getAlign(const DataLayout &DL, const GlobalVariable *GV)
bool isKernelLDS(const Function *F)
bool isClobberedInFunction(const LoadInst *Load, MemorySSA *MSSA, AAResults *AA)
Check is a Load is clobbered in its function.
DenseMap< Function *, DenseSet< GlobalVariable * > > FunctionVariableMap
This is an optimization pass for GlobalISel generic memory operations.
LLVM_ABI bool convertUsersOfConstantsToInstructions(ArrayRef< Constant * > Consts, Function *RestrictToFunc=nullptr, bool RemoveDeadConstants=true, bool IncludeSelf=false)
Replace constant expressions users of the given constants with instructions.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
LLVM_ABI void reportFatalUsageError(Error Err)
Report a fatal error that does not indicate a bug in LLVM.
This struct is a compact representation of a valid (non-zero power of two) alignment.