20#include "llvm/IR/IntrinsicsAMDGPU.h"
23#define DEBUG_TYPE "amdgpu-memory-utils"
44 if (
auto *TTy = dyn_cast<TargetExtType>(Ty))
45 return TTy->getName() ==
"amdgcn.named.barrier" ? TTy :
nullptr;
46 if (
auto *STy = dyn_cast<StructType>(Ty)) {
47 if (STy->getNumElements() == 0)
49 Ty = STy->getElementType(0);
101 for (
auto &GV : M.globals())
112 for (
auto &GV : M.globals()) {
115 for (
User *V : GV.users()) {
116 if (
auto *
I = dyn_cast<Instruction>(V)) {
148 if (
F.hasAddressTaken(
nullptr,
153 set_union(VariablesReachableThroughFunctionPointer,
154 DirectMapFunction[&
F]);
158 auto FunctionMakesUnknownCall = [&](
const Function *
F) ->
bool {
161 if (!R.second->getFunction())
173 if (!
F.isDeclaration() && FunctionMakesUnknownCall(&
F)) {
176 VariablesReachableThroughFunctionPointer);
183 for (
Function &Func : M.functions()) {
190 while (!wip.
empty()) {
195 set_union(TransitiveMapFunction[&Func], DirectMapFunction[
F]);
198 Function *Ith = R.second->getFunction();
213 for (
Function &Func : M.functions()) {
218 Function *Ith = R.second->getFunction();
220 set_union(IndirectMapKernel[&Func], TransitiveMapFunction[Ith]);
223 VariablesReachableThroughFunctionPointer);
234 std::optional<bool> HasAbsoluteGVs;
235 bool HasSpecialGVs =
false;
236 for (
auto &Map : {DirectMapKernel, IndirectMapKernel}) {
237 for (
auto &[Fn, GVs] : Map) {
238 for (
auto *GV : GVs) {
239 bool IsAbsolute = GV->isAbsoluteSymbolRef();
240 bool IsDirectMapDynLDSGV =
242 if (IsDirectMapDynLDSGV)
245 HasSpecialGVs =
true;
248 if (HasAbsoluteGVs.has_value()) {
249 if (*HasAbsoluteGVs != IsAbsolute) {
251 "Module cannot mix absolute and non-absolute LDS GVs");
254 HasAbsoluteGVs = IsAbsolute;
261 if (HasAbsoluteGVs && *HasAbsoluteGVs)
264 return {std::move(DirectMapKernel), std::move(IndirectMapKernel),
275 bool SeenUnknownCall =
false;
277 while (!WorkList.
empty()) {
280 for (
auto &CallRecord : *CG[
F]) {
281 if (!CallRecord.second)
284 Function *Callee = CallRecord.second->getFunction();
286 if (!SeenUnknownCall) {
287 SeenUnknownCall =
true;
304 Callee->removeFnAttr(Attr);
305 if (Visited.
insert(Callee).second)
315 if (isa<FenceInst>(DefInst))
319 switch (
II->getIntrinsicID()) {
320 case Intrinsic::amdgcn_s_barrier:
321 case Intrinsic::amdgcn_s_barrier_signal:
322 case Intrinsic::amdgcn_s_barrier_signal_var:
323 case Intrinsic::amdgcn_s_barrier_signal_isfirst:
324 case Intrinsic::amdgcn_s_barrier_init:
325 case Intrinsic::amdgcn_s_barrier_join:
326 case Intrinsic::amdgcn_s_barrier_wait:
327 case Intrinsic::amdgcn_s_barrier_leave:
328 case Intrinsic::amdgcn_s_get_barrier_state:
329 case Intrinsic::amdgcn_s_wakeup_barrier:
330 case Intrinsic::amdgcn_wave_barrier:
331 case Intrinsic::amdgcn_sched_barrier:
332 case Intrinsic::amdgcn_sched_group_barrier:
341 const auto checkNoAlias = [AA,
Ptr](
auto I) ->
bool {
345 if (checkNoAlias(dyn_cast<AtomicCmpXchgInst>(DefInst)) ||
346 checkNoAlias(dyn_cast<AtomicRMWInst>(DefInst)))
359 LLVM_DEBUG(
dbgs() <<
"Checking clobbering of: " << *Load <<
'\n');
369 while (!WorkList.empty()) {
371 if (!Visited.
insert(MA).second)
377 if (
MemoryDef *Def = dyn_cast<MemoryDef>(MA)) {
390 const MemoryPhi *Phi = cast<MemoryPhi>(MA);
391 for (
const auto &
Use : Phi->incoming_values())
392 WorkList.push_back(cast<MemoryAccess>(&
Use));
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
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines generic set operations that may be used on set's of different types,...
This file defines the SmallSet class.
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 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.
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.
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
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...
The instances of the Type class are immutable: once they are created, they are never changed.
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.
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.
LLVM_READNONE bool isKernel(CallingConv::ID CC)
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.
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)
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.
bool convertUsersOfConstantsToInstructions(ArrayRef< Constant * > Consts, Function *RestrictToFunc=nullptr, bool RemoveDeadConstants=true, bool IncludeSelf=false)
Replace constant expressions users of the given constants with instructions.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
This struct is a compact representation of a valid (non-zero power of two) alignment.