24#define DEBUG_TYPE "amdgpu-wait-sgpr-hazards"
28 cl::desc(
"Cull hazards on function boundaries"));
33 cl::desc(
"Cull hazards on memory waits"));
37 cl::desc(
"Number of tracked SGPRs before initiating hazard cull on memory "
42class AMDGPUWaitSGPRHazards {
50 bool CullSGPRHazardsOnFunctionBoundary;
51 bool CullSGPRHazardsAtMemWait;
52 unsigned CullSGPRHazardsMemWaitThreshold;
54 AMDGPUWaitSGPRHazards() =
default;
57 static std::optional<unsigned> sgprNumber(
Register Reg,
64 case AMDGPU::SGPR_NULL:
65 case AMDGPU::SGPR_NULL64:
70 unsigned RegN = TRI.getHWRegIndex(
Reg);
77 return Reg == AMDGPU::VCC ||
Reg == AMDGPU::VCC_LO ||
Reg == AMDGPU::VCC_HI;
88 while (
I->isBundledWithPred())
94 if (
I->getOpcode() != AMDGPU::S_GETPC_B64)
98 const unsigned NewBytes = 4;
100 "Unexpected instruction insertion in bundle");
103 while (NextMI != End && NextMI->isBundledWithPred()) {
104 for (
auto &Operand : NextMI->operands()) {
105 if (Operand.isGlobal())
106 Operand.setOffset(Operand.getOffset() + NewBytes);
113 static constexpr unsigned None = 0;
114 static constexpr unsigned SALU = (1 << 0);
115 static constexpr unsigned VALU = (1 << 1);
117 std::bitset<64> Tracked;
118 std::bitset<128> SALUHazards;
119 std::bitset<128> VALUHazards;
120 unsigned VCCHazard = None;
121 bool ActiveFlat =
false;
123 bool merge(
const HazardState &
RHS) {
124 HazardState Orig(*
this);
126 return (*
this != Orig);
130 return Tracked ==
RHS.Tracked && SALUHazards ==
RHS.SALUHazards &&
131 VALUHazards ==
RHS.VALUHazards && VCCHazard ==
RHS.VCCHazard &&
132 ActiveFlat ==
RHS.ActiveFlat;
138 Tracked |=
RHS.Tracked;
139 SALUHazards |=
RHS.SALUHazards;
140 VALUHazards |=
RHS.VALUHazards;
141 VCCHazard |=
RHS.VCCHazard;
142 ActiveFlat |=
RHS.ActiveFlat;
146 struct BlockHazardState {
151 DenseMap<const MachineBasicBlock *, BlockHazardState> BlockState;
153 static constexpr unsigned WAVE32_NOPS = 4;
154 static constexpr unsigned WAVE64_NOPS = 8;
156 void insertHazardCull(MachineBasicBlock &
MBB,
159 unsigned Count = DsNopCount;
164 unsigned mergeMasks(
unsigned Mask1,
unsigned Mask2) {
195 auto MBB =
MI->getParent();
200 if (It->getOpcode() != AMDGPU::S_WAITCNT_DEPCTR)
203 It->getOperand(0).setImm(mergeMasks(Mask, It->getOperand(0).getImm()));
207 bool runOnMachineBasicBlock(MachineBasicBlock &
MBB,
bool Emit) {
208 enum { WA_VALU = 0x1, WA_SALU = 0x2, WA_VCC = 0x4 };
210 HazardState State = BlockState[&
MBB].In;
211 SmallSet<Register, 8> SeenRegs;
218 if (
MI->isMetaInstruction())
222 if (
MI->getOpcode() == AMDGPU::DS_NOP) {
223 if (++DsNops >= DsNopCount)
224 State.Tracked.reset();
232 State.ActiveFlat =
true;
238 State.VCCHazard = HazardState::None;
239 State.SALUHazards.reset();
240 State.VALUHazards.reset();
245 if (
MI->getOpcode() == AMDGPU::S_WAITCNT_DEPCTR) {
246 unsigned int Mask =
MI->getOperand(0).getImm();
248 State.VCCHazard &= ~HazardState::VALU;
250 State.SALUHazards.reset();
251 State.VCCHazard &= ~HazardState::SALU;
254 State.VALUHazards.reset();
259 if (CullSGPRHazardsAtMemWait &&
260 (
MI->getOpcode() == AMDGPU::S_WAIT_LOADCNT ||
261 MI->getOpcode() == AMDGPU::S_WAIT_SAMPLECNT ||
262 MI->getOpcode() == AMDGPU::S_WAIT_BVHCNT) &&
263 (
MI->getOperand(0).isImm() &&
MI->getOperand(0).getImm() == 0) &&
264 (State.Tracked.count() >= CullSGPRHazardsMemWaitThreshold)) {
265 if (
MI->getOpcode() == AMDGPU::S_WAIT_LOADCNT && State.ActiveFlat) {
266 State.ActiveFlat =
false;
268 State.Tracked.reset();
270 insertHazardCull(
MBB,
MI);
278 if (!IsVALU && !IsSALU)
283 auto processOperand = [&](
const MachineOperand &
Op,
bool IsUse) {
288 if (!TRI->isSGPRReg(*MRI,
Reg))
295 auto RegNumber = sgprNumber(
Reg, *TRI);
301 unsigned RegN = *RegNumber;
302 unsigned PairN = (RegN >> 1) & 0x3f;
306 if (!State.Tracked[PairN]) {
308 State.Tracked.set(PairN);
319 if (State.VCCHazard & HazardState::VALU)
320 State.VCCHazard = HazardState::None;
322 State.VALUHazards.reset();
326 for (uint8_t RegIdx = 0; RegIdx < SGPRCount; ++RegIdx) {
327 Wait |= State.SALUHazards[RegN + RegIdx] ? WA_SALU : 0;
328 Wait |= IsVALU && State.VALUHazards[RegN + RegIdx] ? WA_VALU : 0;
330 if (isVCC(
Reg) && State.VCCHazard) {
333 if (State.VCCHazard & HazardState::SALU)
335 if (State.VCCHazard & HazardState::VALU)
341 State.VCCHazard = IsSALU ? HazardState::SALU : HazardState::VALU;
343 for (uint8_t RegIdx = 0; RegIdx < SGPRCount; ++RegIdx) {
345 State.SALUHazards.set(RegN + RegIdx);
347 State.VALUHazards.set(RegN + RegIdx);
354 (
MI->isCall() ||
MI->isReturn() ||
MI->isIndirectBranch()) &&
355 MI->getOpcode() != AMDGPU::S_ENDPGM &&
356 MI->getOpcode() != AMDGPU::S_ENDPGM_SAVED;
359 const bool HasImplicitVCC =
366 if (State.VCCHazard & HazardState::VALU)
368 if (State.SALUHazards.any() || (State.VCCHazard & HazardState::SALU))
370 if (State.VALUHazards.any())
372 if (CullSGPRHazardsOnFunctionBoundary && State.Tracked.any()) {
373 State.Tracked.reset();
375 insertHazardCull(
MBB,
MI);
380 for (
const MachineOperand &
Op :
MI->all_uses()) {
381 if (
Op.isImplicit() &&
382 (!HasImplicitVCC || !
Op.isReg() || !isVCC(
Op.getReg())))
384 processOperand(
Op,
true);
392 State.VCCHazard &= ~HazardState::VALU;
395 if (
Wait & WA_SALU) {
396 State.SALUHazards.reset();
397 State.VCCHazard &= ~HazardState::SALU;
400 if (
Wait & WA_VALU) {
401 State.VALUHazards.reset();
405 if (!mergeConsecutiveWaitAlus(
MI, Mask)) {
407 TII->get(AMDGPU::S_WAITCNT_DEPCTR))
416 if (
MI->isCall() && !CullSGPRHazardsOnFunctionBoundary)
421 for (
const MachineOperand &
Op :
MI->all_defs()) {
422 if (
Op.isImplicit() &&
423 (!HasImplicitVCC || !
Op.isReg() || !isVCC(
Op.getReg())))
425 processOperand(
Op,
false);
429 BlockHazardState &BS = BlockState[&
MBB];
430 bool Changed = State != BS.Out;
432 assert(!
Changed &&
"Hazard state should not change on emit pass");
440 bool run(MachineFunction &MF) {
442 if (!ST->hasVALUReadSGPRHazard())
451 CullSGPRHazardsOnFunctionBoundary =
454 CullSGPRHazardsAtMemWait =
457 CullSGPRHazardsMemWaitThreshold =
459 "amdgpu-sgpr-hazard-mem-wait-cull-threshold",
460 CullSGPRHazardsMemWaitThreshold);
462 TII = ST->getInstrInfo();
463 TRI = ST->getRegisterInfo();
465 DsNopCount = ST->isWave64() ? WAVE64_NOPS : WAVE32_NOPS;
469 !CullSGPRHazardsOnFunctionBoundary) {
472 MachineBasicBlock &EntryBlock = MF.
front();
473 BlockState[&EntryBlock].In.Tracked.set();
484 SetVector<MachineBasicBlock *> Worklist;
487 while (!Worklist.
empty()) {
489 bool Changed = runOnMachineBasicBlock(
MBB,
false);
492 HazardState NewState = BlockState[&
MBB].Out;
496 auto &SuccState = BlockState[Succ];
497 if (Succ->getSinglePredecessor() && !Succ->isEntryBlock()) {
498 if (SuccState.In != NewState) {
499 SuccState.In = NewState;
502 }
else if (SuccState.In.merge(NewState)) {
525 AMDGPUWaitSGPRHazardsLegacy() : MachineFunctionPass(ID) {}
527 bool runOnMachineFunction(MachineFunction &MF)
override {
528 return AMDGPUWaitSGPRHazards().run(MF);
531 void getAnalysisUsage(AnalysisUsage &AU)
const override {
539char AMDGPUWaitSGPRHazardsLegacy::ID = 0;
544 "AMDGPU Insert waits for SGPR read hazards",
false,
false)
549 if (AMDGPUWaitSGPRHazards().run(MF))
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Provides AMDGPU specific target descriptions.
static cl::opt< bool > GlobalCullSGPRHazardsAtMemWait("amdgpu-sgpr-hazard-mem-wait-cull", cl::init(false), cl::Hidden, cl::desc("Cull hazards on memory waits"))
static cl::opt< unsigned > GlobalCullSGPRHazardsMemWaitThreshold("amdgpu-sgpr-hazard-mem-wait-cull-threshold", cl::init(8), cl::Hidden, cl::desc("Number of tracked SGPRs before initiating hazard cull on memory " "wait"))
static cl::opt< bool > GlobalCullSGPRHazardsOnFunctionBoundary("amdgpu-sgpr-hazard-boundary-cull", cl::init(false), cl::Hidden, cl::desc("Cull hazards on function boundaries"))
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static void updateGetPCBundle(MachineInstr *NewMI)
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
static LoopDeletionResult merge(LoopDeletionResult A, LoopDeletionResult B)
Register const TargetRegisterInfo * TRI
Promote Memory to Register
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Interface definition for SIInstrInfo.
This file implements a set that has insertion order iteration characteristics.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
uint64_t getFnAttributeAsParsedInteger(StringRef Kind, uint64_t Default=0) const
For a string attribute Kind, parse attribute as an integer.
CallingConv::ID getCallingConv() const
getCallingConv()/setCallingConv(CC) - These method get and set the calling convention of this functio...
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
instr_iterator instr_begin()
Instructions::iterator instr_iterator
instr_iterator instr_end()
iterator_range< succ_iterator > successors()
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
const MachineBasicBlock & front() const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineBasicBlock * getParent() const
bool isBundled() const
Return true if this instruction part of a bundle.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
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.
Wrapper class representing virtual and physical registers.
static bool isVMEM(const MachineInstr &MI)
static bool isSMRD(const MachineInstr &MI)
static bool isSALU(const MachineInstr &MI)
static bool isFLATGlobal(const MachineInstr &MI)
static bool isVALU(const MachineInstr &MI, bool AllowLDSDMA)
static bool isFLAT(const MachineInstr &MI)
bool empty() const
Determine if the SetVector is empty or not.
bool insert(const value_type &X)
Insert a new element into the SetVector.
value_type pop_back_val()
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
int getNumOccurrences() const
self_iterator getIterator()
unsigned decodeFieldVaVcc(unsigned Encoded)
unsigned encodeFieldVaVcc(unsigned Encoded, unsigned VaVcc)
unsigned decodeFieldHoldCnt(unsigned Encoded, const IsaVersion &Version)
unsigned encodeFieldHoldCnt(unsigned Encoded, unsigned HoldCnt, const IsaVersion &Version)
unsigned encodeFieldVaSsrc(unsigned Encoded, unsigned VaSsrc)
unsigned encodeFieldVaVdst(unsigned Encoded, unsigned VaVdst)
unsigned decodeFieldSaSdst(unsigned Encoded)
unsigned decodeFieldVaSdst(unsigned Encoded)
unsigned encodeFieldVmVsrc(unsigned Encoded, unsigned VmVsrc)
unsigned decodeFieldVaSsrc(unsigned Encoded)
unsigned encodeFieldSaSdst(unsigned Encoded, unsigned SaSdst)
unsigned decodeFieldVaVdst(unsigned Encoded)
int getDefaultDepCtrEncoding(const MCSubtargetInfo &STI)
unsigned decodeFieldVmVsrc(unsigned Encoded)
unsigned encodeFieldVaSdst(unsigned Encoded, unsigned VaSdst)
LLVM_ABI IsaVersion getIsaVersion(StringRef GPU)
unsigned getRegBitWidth(unsigned RCID)
Get the size in bits of a register from the register class RC.
LLVM_READNONE constexpr bool isEntryFunctionCC(CallingConv::ID CC)
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
initializer< Ty > init(const Ty &Val)
DXILDebugInfoMap run(Module &M)
@ Emitted
Assigned address, still materializing.
This is an optimization pass for GlobalISel generic memory operations.
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
bool operator!=(uint64_t V1, const APInt &V2)
char & AMDGPUWaitSGPRHazardsLegacyID
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
auto reverse(ContainerTy &&C)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Count
DWARFExpression::Operation Op
bool operator|=(SparseBitVector< ElementSize > &LHS, const SparseBitVector< ElementSize > *RHS)
IterT prev_nodbg(IterT It, IterT Begin, bool SkipPseudoOp=true)
Decrement It, then continue decrementing it while it points to a debug instruction.