LLVM 19.0.0git
AMDGPUMemoryUtils.cpp
Go to the documentation of this file.
1//===-- AMDGPUMemoryUtils.cpp - -------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "AMDGPUMemoryUtils.h"
10#include "AMDGPU.h"
11#include "AMDGPUBaseInfo.h"
12#include "llvm/ADT/SmallSet.h"
15#include "llvm/IR/DataLayout.h"
18#include "llvm/IR/IntrinsicsAMDGPU.h"
20
21#define DEBUG_TYPE "amdgpu-memory-utils"
22
23using namespace llvm;
24
25namespace llvm {
26
27namespace AMDGPU {
28
30 return DL.getValueOrABITypeAlignment(GV->getPointerAlignment(DL),
31 GV->getValueType());
32}
33
35 // external zero size addrspace(3) without initializer is dynlds.
36 const Module *M = GV.getParent();
37 const DataLayout &DL = M->getDataLayout();
39 return false;
40 return DL.getTypeAllocSize(GV.getValueType()) == 0;
41}
42
45 return false;
46 }
47 if (isDynamicLDS(GV)) {
48 return true;
49 }
50 if (GV.isConstant()) {
51 // A constant undef variable can't be written to, and any load is
52 // undef, so it should be eliminated by the optimizer. It could be
53 // dropped by the back end if not. This pass skips over it.
54 return false;
55 }
56 if (GV.hasInitializer() && !isa<UndefValue>(GV.getInitializer())) {
57 // Initializers are unimplemented for LDS address space.
58 // Leave such variables in place for consistent error reporting.
59 return false;
60 }
61 return true;
62}
63
64bool isReallyAClobber(const Value *Ptr, MemoryDef *Def, AAResults *AA) {
65 Instruction *DefInst = Def->getMemoryInst();
66
67 if (isa<FenceInst>(DefInst))
68 return false;
69
70 if (const IntrinsicInst *II = dyn_cast<IntrinsicInst>(DefInst)) {
71 switch (II->getIntrinsicID()) {
72 case Intrinsic::amdgcn_s_barrier:
73 case Intrinsic::amdgcn_s_barrier_signal:
74 case Intrinsic::amdgcn_s_barrier_signal_var:
75 case Intrinsic::amdgcn_s_barrier_signal_isfirst:
76 case Intrinsic::amdgcn_s_barrier_signal_isfirst_var:
77 case Intrinsic::amdgcn_s_barrier_init:
78 case Intrinsic::amdgcn_s_barrier_join:
79 case Intrinsic::amdgcn_s_barrier_wait:
80 case Intrinsic::amdgcn_s_barrier_leave:
81 case Intrinsic::amdgcn_s_get_barrier_state:
82 case Intrinsic::amdgcn_s_wakeup_barrier:
83 case Intrinsic::amdgcn_wave_barrier:
84 case Intrinsic::amdgcn_sched_barrier:
85 case Intrinsic::amdgcn_sched_group_barrier:
86 return false;
87 default:
88 break;
89 }
90 }
91
92 // Ignore atomics not aliasing with the original load, any atomic is a
93 // universal MemoryDef from MSSA's point of view too, just like a fence.
94 const auto checkNoAlias = [AA, Ptr](auto I) -> bool {
95 return I && AA->isNoAlias(I->getPointerOperand(), Ptr);
96 };
97
98 if (checkNoAlias(dyn_cast<AtomicCmpXchgInst>(DefInst)) ||
99 checkNoAlias(dyn_cast<AtomicRMWInst>(DefInst)))
100 return false;
101
102 return true;
103}
104
106 AAResults *AA) {
107 MemorySSAWalker *Walker = MSSA->getWalker();
111
112 LLVM_DEBUG(dbgs() << "Checking clobbering of: " << *Load << '\n');
113
114 // Start with a nearest dominating clobbering access, it will be either
115 // live on entry (nothing to do, load is not clobbered), MemoryDef, or
116 // MemoryPhi if several MemoryDefs can define this memory state. In that
117 // case add all Defs to WorkList and continue going up and checking all
118 // the definitions of this memory location until the root. When all the
119 // defs are exhausted and came to the entry state we have no clobber.
120 // Along the scan ignore barriers and fences which are considered clobbers
121 // by the MemorySSA, but not really writing anything into the memory.
122 while (!WorkList.empty()) {
123 MemoryAccess *MA = WorkList.pop_back_val();
124 if (!Visited.insert(MA).second)
125 continue;
126
127 if (MSSA->isLiveOnEntryDef(MA))
128 continue;
129
130 if (MemoryDef *Def = dyn_cast<MemoryDef>(MA)) {
131 LLVM_DEBUG(dbgs() << " Def: " << *Def->getMemoryInst() << '\n');
132
133 if (isReallyAClobber(Load->getPointerOperand(), Def, AA)) {
134 LLVM_DEBUG(dbgs() << " -> load is clobbered\n");
135 return true;
136 }
137
138 WorkList.push_back(
139 Walker->getClobberingMemoryAccess(Def->getDefiningAccess(), Loc));
140 continue;
141 }
142
143 const MemoryPhi *Phi = cast<MemoryPhi>(MA);
144 for (const auto &Use : Phi->incoming_values())
145 WorkList.push_back(cast<MemoryAccess>(&Use));
146 }
147
148 LLVM_DEBUG(dbgs() << " -> no clobber\n");
149 return false;
150}
151
152} // end namespace AMDGPU
153
154} // end namespace llvm
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define LLVM_DEBUG(X)
Definition: Debug.h:101
#define I(x, y, z)
Definition: MD5.cpp:58
This file exposes an interface to building/using memory SSA to walk memory instructions using a use/d...
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.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:110
Module * getParent()
Get the module that this global value is contained inside of...
Definition: GlobalValue.h:656
PointerType * getType() const
Global values are always pointers.
Definition: GlobalValue.h:294
Type * getValueType() const
Definition: GlobalValue.h:296
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.
Definition: IntrinsicInst.h:47
An instruction for reading from memory.
Definition: Instructions.h:184
Represents a read-write access to memory, whether it is a must-alias, or a may-alias.
Definition: MemorySSA.h:372
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.
Definition: MemorySSA.h:479
This is the generic walker interface for walkers of MemorySSA.
Definition: MemorySSA.h:1011
MemoryAccess * getClobberingMemoryAccess(const Instruction *I, BatchAAResults &AA)
Given a memory Mod/Ref/ModRef'ing instruction, calling this will give you the nearest dominating Memo...
Definition: MemorySSA.h:1040
Encapsulates MemorySSA, including all data associated with memory accesses.
Definition: MemorySSA.h:700
MemorySSAWalker * getWalker()
Definition: MemorySSA.cpp:1549
bool isLiveOnEntryDef(const MemoryAccess *MA) const
Return true if MA represents the live on entry value.
Definition: MemorySSA.h:737
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition: SmallSet.h:135
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition: SmallSet.h:179
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
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.
Definition: Use.h:43
LLVM Value Representation.
Definition: Value.h:74
Align getPointerAlignment(const DataLayout &DL) const
Returns an alignment of the pointer value.
Definition: Value.cpp:926
@ LOCAL_ADDRESS
Address space for local memory.
bool isDynamicLDS(const GlobalVariable &GV)
Align getAlign(DataLayout const &DL, const GlobalVariable *GV)
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...
bool isLDSVariableToLower(const GlobalVariable &GV)
bool isClobberedInFunction(const LoadInst *Load, MemorySSA *MSSA, AAResults *AA)
Check is a Load is clobbered in its function.
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition: Debug.cpp:163
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39