20#include "llvm/IR/IntrinsicsAMDGPU.h"
24#define DEBUG_TYPE "amdgpu-memory-utils"
80 for (
auto &GV : M.globals())
91 for (
auto &GV : M.globals()) {
94 for (
User *V : GV.users()) {
95 if (
auto *
I = dyn_cast<Instruction>(V)) {
127 if (
F.hasAddressTaken(
nullptr,
132 set_union(VariablesReachableThroughFunctionPointer,
133 DirectMapFunction[&
F]);
137 auto FunctionMakesUnknownCall = [&](
const Function *
F) ->
bool {
140 if (!R.second->getFunction())
152 if (!
F.isDeclaration() && FunctionMakesUnknownCall(&
F)) {
155 VariablesReachableThroughFunctionPointer);
162 for (
Function &Func : M.functions()) {
169 while (!wip.
empty()) {
174 set_union(TransitiveMapFunction[&Func], DirectMapFunction[
F]);
177 Function *Ith = R.second->getFunction();
192 for (
Function &Func : M.functions()) {
197 Function *Ith = R.second->getFunction();
199 set_union(IndirectMapKernel[&Func], TransitiveMapFunction[Ith]);
202 VariablesReachableThroughFunctionPointer);
213 std::optional<bool> HasAbsoluteGVs;
214 for (
auto &Map : {DirectMapKernel, IndirectMapKernel}) {
215 for (
auto &[Fn, GVs] : Map) {
216 for (
auto *GV : GVs) {
217 bool IsAbsolute = GV->isAbsoluteSymbolRef();
219 if (IsDirectMapDynLDSGV)
221 if (HasAbsoluteGVs.has_value()) {
222 if (*HasAbsoluteGVs != IsAbsolute) {
224 "Module cannot mix absolute and non-absolute LDS GVs");
227 HasAbsoluteGVs = IsAbsolute;
234 if (HasAbsoluteGVs && *HasAbsoluteGVs)
237 return {std::move(DirectMapKernel), std::move(IndirectMapKernel)};
247 bool SeenUnknownCall =
false;
249 while (!WorkList.
empty()) {
252 for (
auto &CallRecord : *CG[
F]) {
253 if (!CallRecord.second)
256 Function *Callee = CallRecord.second->getFunction();
258 if (!SeenUnknownCall) {
259 SeenUnknownCall =
true;
276 Callee->removeFnAttr(Attr);
277 if (Visited.
insert(Callee).second)
287 if (isa<FenceInst>(DefInst))
291 switch (
II->getIntrinsicID()) {
292 case Intrinsic::amdgcn_s_barrier:
293 case Intrinsic::amdgcn_s_barrier_signal:
294 case Intrinsic::amdgcn_s_barrier_signal_var:
295 case Intrinsic::amdgcn_s_barrier_signal_isfirst:
296 case Intrinsic::amdgcn_s_barrier_signal_isfirst_var:
297 case Intrinsic::amdgcn_s_barrier_init:
298 case Intrinsic::amdgcn_s_barrier_join:
299 case Intrinsic::amdgcn_s_barrier_wait:
300 case Intrinsic::amdgcn_s_barrier_leave:
301 case Intrinsic::amdgcn_s_get_barrier_state:
302 case Intrinsic::amdgcn_s_wakeup_barrier:
303 case Intrinsic::amdgcn_wave_barrier:
304 case Intrinsic::amdgcn_sched_barrier:
305 case Intrinsic::amdgcn_sched_group_barrier:
314 const auto checkNoAlias = [AA,
Ptr](
auto I) ->
bool {
318 if (checkNoAlias(dyn_cast<AtomicCmpXchgInst>(DefInst)) ||
319 checkNoAlias(dyn_cast<AtomicRMWInst>(DefInst)))
332 LLVM_DEBUG(
dbgs() <<
"Checking clobbering of: " << *Load <<
'\n');
342 while (!WorkList.empty()) {
344 if (!Visited.
insert(MA).second)
350 if (
MemoryDef *Def = dyn_cast<MemoryDef>(MA)) {
363 const MemoryPhi *Phi = cast<MemoryPhi>(MA);
364 for (
const auto &
Use : Phi->incoming_values())
365 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.
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)
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.