46#define DEBUG_TYPE "si-insert-waitcnts"
49 "Force emit s_waitcnt expcnt(0) instrs");
51 "Force emit s_waitcnt lgkmcnt(0) instrs");
53 "Force emit s_waitcnt vmcnt(0) instrs");
57 cl::desc(
"Force all waitcnt instrs to be emitted as "
58 "s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)"),
62 "amdgpu-waitcnt-load-forcezero",
63 cl::desc(
"Force all waitcnt load counters to wait until 0"),
67 "amdgpu-expert-scheduling-mode",
68 cl::desc(
"Enable expert scheduling mode 2 for all functions (GFX12+ only)"),
82 SAMPLE_CNT = NUM_NORMAL_INST_CNTS,
86 NUM_EXTENDED_INST_CNTS,
87 VA_VDST = NUM_EXTENDED_INST_CNTS,
90 NUM_INST_CNTS = NUM_EXPERT_INST_CNTS
104auto inst_counter_types(InstCounterType MaxCounter = NUM_INST_CNTS) {
105 return enum_seq(LOAD_CNT, MaxCounter);
152 TRACKINGID_RANGE_LEN = (1 << 16),
157 REGUNITS_END = REGUNITS_BEGIN + TRACKINGID_RANGE_LEN,
162 NUM_LDSDMA = TRACKINGID_RANGE_LEN,
163 LDSDMA_BEGIN = REGUNITS_END,
164 LDSDMA_END = LDSDMA_BEGIN + NUM_LDSDMA,
168static constexpr VMEMID toVMEMID(MCRegUnit RU) {
169 return static_cast<unsigned>(RU);
172#define AMDGPU_DECLARE_WAIT_EVENTS(DECL) \
174 DECL(VMEM_SAMPLER_READ_ACCESS) \
175 DECL(VMEM_BVH_READ_ACCESS) \
176 DECL(GLOBAL_INV_ACCESS) \
177 DECL(VMEM_WRITE_ACCESS) \
178 DECL(SCRATCH_WRITE_ACCESS) \
188 DECL(EXP_POS_ACCESS) \
189 DECL(EXP_PARAM_ACCESS) \
191 DECL(EXP_LDS_ACCESS) \
192 DECL(VGPR_CSMACC_WRITE) \
193 DECL(VGPR_DPMACC_WRITE) \
194 DECL(VGPR_TRANS_WRITE) \
195 DECL(VGPR_XDL_WRITE) \
196 DECL(VGPR_LDS_READ) \
197 DECL(VGPR_FLAT_READ) \
201#define AMDGPU_EVENT_ENUM(Name) Name,
206#undef AMDGPU_EVENT_ENUM
208#define AMDGPU_EVENT_NAME(Name) #Name,
212#undef AMDGPU_EVENT_NAME
233static const unsigned instrsForExtendedCounterTypes[NUM_EXTENDED_INST_CNTS] = {
234 AMDGPU::S_WAIT_LOADCNT, AMDGPU::S_WAIT_DSCNT, AMDGPU::S_WAIT_EXPCNT,
235 AMDGPU::S_WAIT_STORECNT, AMDGPU::S_WAIT_SAMPLECNT, AMDGPU::S_WAIT_BVHCNT,
236 AMDGPU::S_WAIT_KMCNT, AMDGPU::S_WAIT_XCNT};
244static bool isNormalMode(InstCounterType MaxCounter) {
245 return MaxCounter == NUM_NORMAL_INST_CNTS;
250 assert(updateVMCntOnly(Inst));
252 return VMEM_NOSAMPLER;
266 return VMEM_NOSAMPLER;
278 return Wait.StoreCnt;
280 return Wait.SampleCnt;
297 unsigned &WC = getCounterRef(
Wait,
T);
298 WC = std::min(WC,
Count);
302 getCounterRef(
Wait,
T) = ~0
u;
306 return getCounterRef(
Wait,
T);
310InstCounterType eventCounter(
const unsigned *masks, WaitEventType
E) {
311 for (
auto T : inst_counter_types()) {
312 if (masks[
T] & (1 <<
E))
318class WaitcntBrackets;
326class WaitcntGenerator {
328 const GCNSubtarget *ST =
nullptr;
329 const SIInstrInfo *TII =
nullptr;
330 AMDGPU::IsaVersion IV;
331 InstCounterType MaxCounter;
333 bool ExpandWaitcntProfiling =
false;
334 const AMDGPU::HardwareLimits *Limits =
nullptr;
337 WaitcntGenerator() =
default;
338 WaitcntGenerator(
const MachineFunction &MF, InstCounterType MaxCounter,
339 const AMDGPU::HardwareLimits *Limits)
340 : ST(&MF.getSubtarget<GCNSubtarget>()), TII(ST->getInstrInfo()),
344 ExpandWaitcntProfiling(
345 MF.
getFunction().hasFnAttribute(
"amdgpu-expand-waitcnt-profiling")),
350 bool isOptNone()
const {
return OptNone; }
352 const AMDGPU::HardwareLimits &getLimits()
const {
return *Limits; }
366 applyPreexistingWaitcnt(WaitcntBrackets &ScoreBrackets,
367 MachineInstr &OldWaitcntInstr, AMDGPU::Waitcnt &
Wait,
371 bool promoteSoftWaitCnt(MachineInstr *Waitcnt)
const;
376 virtual bool createNewWaitcnt(MachineBasicBlock &
Block,
378 AMDGPU::Waitcnt
Wait,
379 WaitcntBrackets *ScoreBrackets =
nullptr) = 0;
383 virtual const unsigned *getWaitEventMask()
const = 0;
387 virtual AMDGPU::Waitcnt getAllZeroWaitcnt(
bool IncludeVSCnt)
const = 0;
389 virtual ~WaitcntGenerator() =
default;
392 static constexpr unsigned
393 eventMask(std::initializer_list<WaitEventType> Events) {
395 for (
auto &
E : Events)
402class WaitcntGeneratorPreGFX12 :
public WaitcntGenerator {
404 WaitcntGeneratorPreGFX12() =
default;
405 WaitcntGeneratorPreGFX12(
const MachineFunction &MF,
406 const AMDGPU::HardwareLimits *Limits)
407 : WaitcntGenerator(MF, NUM_NORMAL_INST_CNTS, Limits) {}
410 applyPreexistingWaitcnt(WaitcntBrackets &ScoreBrackets,
411 MachineInstr &OldWaitcntInstr, AMDGPU::Waitcnt &
Wait,
414 bool createNewWaitcnt(MachineBasicBlock &
Block,
416 AMDGPU::Waitcnt
Wait,
417 WaitcntBrackets *ScoreBrackets =
nullptr)
override;
419 const unsigned *getWaitEventMask()
const override {
422 static const unsigned WaitEventMaskForInstPreGFX12[NUM_INST_CNTS] = {
424 {VMEM_ACCESS, VMEM_SAMPLER_READ_ACCESS, VMEM_BVH_READ_ACCESS}),
425 eventMask({SMEM_ACCESS, LDS_ACCESS, GDS_ACCESS, SQ_MESSAGE}),
426 eventMask({EXP_GPR_LOCK, GDS_GPR_LOCK, VMW_GPR_LOCK, EXP_PARAM_ACCESS,
427 EXP_POS_ACCESS, EXP_LDS_ACCESS}),
428 eventMask({VMEM_WRITE_ACCESS, SCRATCH_WRITE_ACCESS}),
436 return WaitEventMaskForInstPreGFX12;
439 AMDGPU::Waitcnt getAllZeroWaitcnt(
bool IncludeVSCnt)
const override;
442class WaitcntGeneratorGFX12Plus :
public WaitcntGenerator {
447 WaitcntGeneratorGFX12Plus() =
default;
448 WaitcntGeneratorGFX12Plus(
const MachineFunction &MF,
449 InstCounterType MaxCounter,
450 const AMDGPU::HardwareLimits *Limits,
452 : WaitcntGenerator(MF, MaxCounter, Limits), IsExpertMode(IsExpertMode) {}
455 applyPreexistingWaitcnt(WaitcntBrackets &ScoreBrackets,
456 MachineInstr &OldWaitcntInstr, AMDGPU::Waitcnt &
Wait,
459 bool createNewWaitcnt(MachineBasicBlock &
Block,
461 AMDGPU::Waitcnt
Wait,
462 WaitcntBrackets *ScoreBrackets =
nullptr)
override;
464 const unsigned *getWaitEventMask()
const override {
467 static const unsigned WaitEventMaskForInstGFX12Plus[NUM_INST_CNTS] = {
468 eventMask({VMEM_ACCESS, GLOBAL_INV_ACCESS}),
469 eventMask({LDS_ACCESS, GDS_ACCESS}),
470 eventMask({EXP_GPR_LOCK, GDS_GPR_LOCK, VMW_GPR_LOCK, EXP_PARAM_ACCESS,
471 EXP_POS_ACCESS, EXP_LDS_ACCESS}),
472 eventMask({VMEM_WRITE_ACCESS, SCRATCH_WRITE_ACCESS}),
473 eventMask({VMEM_SAMPLER_READ_ACCESS}),
474 eventMask({VMEM_BVH_READ_ACCESS}),
475 eventMask({SMEM_ACCESS, SQ_MESSAGE, SCC_WRITE}),
476 eventMask({VMEM_GROUP, SMEM_GROUP}),
477 eventMask({VGPR_CSMACC_WRITE, VGPR_DPMACC_WRITE, VGPR_TRANS_WRITE,
479 eventMask({VGPR_LDS_READ, VGPR_FLAT_READ, VGPR_VMEM_READ})};
481 return WaitEventMaskForInstGFX12Plus;
484 AMDGPU::Waitcnt getAllZeroWaitcnt(
bool IncludeVSCnt)
const override;
487class SIInsertWaitcnts {
489 const GCNSubtarget *ST;
490 const SIInstrInfo *TII =
nullptr;
491 const SIRegisterInfo *TRI =
nullptr;
492 const MachineRegisterInfo *MRI =
nullptr;
493 InstCounterType SmemAccessCounter;
494 InstCounterType MaxCounter;
495 bool IsExpertMode =
false;
496 const unsigned *WaitEventMaskForInst;
499 DenseMap<const Value *, MachineBasicBlock *> SLoadAddresses;
500 DenseMap<MachineBasicBlock *, bool> PreheadersToFlush;
501 MachineLoopInfo *MLI;
502 MachinePostDominatorTree *PDT;
506 std::unique_ptr<WaitcntBrackets> Incoming;
510 MapVector<MachineBasicBlock *, BlockInfo> BlockInfos;
512 bool ForceEmitWaitcnt[NUM_INST_CNTS];
517 WaitcntGeneratorPreGFX12 WCGPreGFX12;
518 WaitcntGeneratorGFX12Plus WCGGFX12Plus;
520 WaitcntGenerator *WCG =
nullptr;
523 DenseSet<MachineInstr *> CallInsts;
524 DenseSet<MachineInstr *> ReturnInsts;
528 DenseSet<MachineInstr *> ReleaseVGPRInsts;
530 AMDGPU::HardwareLimits Limits;
533 SIInsertWaitcnts(MachineLoopInfo *MLI, MachinePostDominatorTree *PDT,
535 : MLI(MLI), PDT(PDT), AA(AA) {
536 (void)ForceExpCounter;
537 (void)ForceLgkmCounter;
538 (void)ForceVMCounter;
541 const AMDGPU::HardwareLimits &getLimits()
const {
return Limits; }
543 bool shouldFlushVmCnt(MachineLoop *
ML,
const WaitcntBrackets &Brackets);
544 bool isPreheaderToFlush(MachineBasicBlock &
MBB,
545 const WaitcntBrackets &ScoreBrackets);
546 bool isVMEMOrFlatVMEM(
const MachineInstr &
MI)
const;
547 bool run(MachineFunction &MF);
549 void setForceEmitWaitcnt() {
555 ForceEmitWaitcnt[
EXP_CNT] =
true;
557 ForceEmitWaitcnt[
EXP_CNT] =
false;
562 ForceEmitWaitcnt[DS_CNT] =
true;
563 ForceEmitWaitcnt[KM_CNT] =
true;
565 ForceEmitWaitcnt[DS_CNT] =
false;
566 ForceEmitWaitcnt[KM_CNT] =
false;
571 ForceEmitWaitcnt[LOAD_CNT] =
true;
572 ForceEmitWaitcnt[SAMPLE_CNT] =
true;
573 ForceEmitWaitcnt[BVH_CNT] =
true;
575 ForceEmitWaitcnt[LOAD_CNT] =
false;
576 ForceEmitWaitcnt[SAMPLE_CNT] =
false;
577 ForceEmitWaitcnt[BVH_CNT] =
false;
580 ForceEmitWaitcnt[VA_VDST] =
false;
581 ForceEmitWaitcnt[VM_VSRC] =
false;
587 WaitEventType getVmemWaitEventType(
const MachineInstr &Inst)
const {
590 case AMDGPU::GLOBAL_INV:
591 return GLOBAL_INV_ACCESS;
593 case AMDGPU::GLOBAL_WB:
594 case AMDGPU::GLOBAL_WBINV:
595 return VMEM_WRITE_ACCESS;
601 static const WaitEventType VmemReadMapping[NUM_VMEM_TYPES] = {
602 VMEM_ACCESS, VMEM_SAMPLER_READ_ACCESS, VMEM_BVH_READ_ACCESS};
611 if (TII->mayAccessScratch(Inst))
612 return SCRATCH_WRITE_ACCESS;
613 return VMEM_WRITE_ACCESS;
617 return VmemReadMapping[getVmemType(Inst)];
620 std::optional<WaitEventType>
621 getExpertSchedulingEventType(
const MachineInstr &Inst)
const;
623 bool isVmemAccess(
const MachineInstr &
MI)
const;
624 bool generateWaitcntInstBefore(MachineInstr &
MI,
625 WaitcntBrackets &ScoreBrackets,
626 MachineInstr *OldWaitcntInstr,
628 bool generateWaitcnt(AMDGPU::Waitcnt
Wait,
630 MachineBasicBlock &
Block, WaitcntBrackets &ScoreBrackets,
631 MachineInstr *OldWaitcntInstr);
632 void updateEventWaitcntAfter(MachineInstr &Inst,
633 WaitcntBrackets *ScoreBrackets);
635 MachineBasicBlock *
Block)
const;
636 bool insertForcedWaitAfter(MachineInstr &Inst, MachineBasicBlock &
Block,
637 WaitcntBrackets &ScoreBrackets);
638 bool insertWaitcntInBlock(MachineFunction &MF, MachineBasicBlock &
Block,
639 WaitcntBrackets &ScoreBrackets);
641 bool ExpertMode)
const;
652class WaitcntBrackets {
654 WaitcntBrackets(
const SIInsertWaitcnts *Context) : Context(Context) {
655 assert(Context->TRI->getNumRegUnits() < REGUNITS_END);
660 unsigned NumUnusedVmem = 0, NumUnusedSGPRs = 0;
661 for (
auto &[
ID, Val] : VMem) {
665 for (
auto &[
ID, Val] : SGPRs) {
670 if (NumUnusedVmem || NumUnusedSGPRs) {
671 errs() <<
"WaitcntBracket had unused entries at destruction time: "
672 << NumUnusedVmem <<
" VMem and " << NumUnusedSGPRs
673 <<
" SGPR unused entries\n";
679 bool isSmemCounter(InstCounterType
T)
const {
680 return T == Context->SmemAccessCounter ||
T == X_CNT;
683 unsigned getSgprScoresIdx(InstCounterType
T)
const {
684 assert(isSmemCounter(
T) &&
"Invalid SMEM counter");
685 return T == X_CNT ? 1 : 0;
688 unsigned getScoreLB(InstCounterType
T)
const {
693 unsigned getScoreUB(InstCounterType
T)
const {
698 unsigned getScoreRange(InstCounterType
T)
const {
699 return getScoreUB(
T) - getScoreLB(
T);
702 unsigned getSGPRScore(MCRegUnit RU, InstCounterType
T)
const {
703 auto It = SGPRs.find(RU);
704 return It != SGPRs.end() ? It->second.Scores[getSgprScoresIdx(
T)] : 0;
707 unsigned getVMemScore(VMEMID TID, InstCounterType
T)
const {
708 auto It = VMem.find(TID);
709 return It != VMem.end() ? It->second.Scores[
T] : 0;
714 bool counterOutOfOrder(InstCounterType
T)
const;
715 void simplifyWaitcnt(AMDGPU::Waitcnt &
Wait)
const {
718 void simplifyWaitcnt(
const AMDGPU::Waitcnt &CheckWait,
719 AMDGPU::Waitcnt &UpdateWait)
const;
720 void simplifyWaitcnt(InstCounterType
T,
unsigned &
Count)
const;
721 void simplifyXcnt(
const AMDGPU::Waitcnt &CheckWait,
722 AMDGPU::Waitcnt &UpdateWait)
const;
723 void simplifyVmVsrc(
const AMDGPU::Waitcnt &CheckWait,
724 AMDGPU::Waitcnt &UpdateWait)
const;
726 void determineWaitForPhysReg(InstCounterType
T,
MCPhysReg Reg,
727 AMDGPU::Waitcnt &
Wait)
const;
728 void determineWaitForLDSDMA(InstCounterType
T, VMEMID TID,
729 AMDGPU::Waitcnt &
Wait)
const;
730 void tryClearSCCWriteEvent(MachineInstr *Inst);
732 void applyWaitcnt(
const AMDGPU::Waitcnt &
Wait);
733 void applyWaitcnt(InstCounterType
T,
unsigned Count);
734 void updateByEvent(WaitEventType
E, MachineInstr &
MI);
736 unsigned hasPendingEvent()
const {
return PendingEvents; }
737 unsigned hasPendingEvent(WaitEventType
E)
const {
738 return PendingEvents & (1 <<
E);
740 unsigned hasPendingEvent(InstCounterType
T)
const {
741 unsigned HasPending = PendingEvents & Context->WaitEventMaskForInst[
T];
742 assert((HasPending != 0) == (getScoreRange(
T) != 0));
746 bool hasMixedPendingEvents(InstCounterType
T)
const {
747 unsigned Events = hasPendingEvent(
T);
749 return Events & (Events - 1);
752 bool hasPendingFlat()
const {
753 return ((LastFlat[DS_CNT] > ScoreLBs[DS_CNT] &&
754 LastFlat[DS_CNT] <= ScoreUBs[DS_CNT]) ||
755 (LastFlat[LOAD_CNT] > ScoreLBs[LOAD_CNT] &&
756 LastFlat[LOAD_CNT] <= ScoreUBs[LOAD_CNT]));
759 void setPendingFlat() {
760 LastFlat[LOAD_CNT] = ScoreUBs[LOAD_CNT];
761 LastFlat[DS_CNT] = ScoreUBs[DS_CNT];
764 bool hasPendingGDS()
const {
765 return LastGDS > ScoreLBs[DS_CNT] && LastGDS <= ScoreUBs[DS_CNT];
768 unsigned getPendingGDSWait()
const {
769 return std::min(getScoreUB(DS_CNT) - LastGDS,
770 getWaitCountMax(Context->getLimits(), DS_CNT) - 1);
773 void setPendingGDS() { LastGDS = ScoreUBs[DS_CNT]; }
777 bool hasOtherPendingVmemTypes(
MCPhysReg Reg, VmemType V)
const {
778 for (MCRegUnit RU : regunits(
Reg)) {
779 auto It = VMem.find(toVMEMID(RU));
780 if (It != VMem.end() && (It->second.VMEMTypes & ~(1 << V)))
787 for (MCRegUnit RU : regunits(
Reg)) {
788 if (
auto It = VMem.find(toVMEMID(RU)); It != VMem.end()) {
789 It->second.VMEMTypes = 0;
790 if (It->second.empty())
796 void setStateOnFunctionEntryOrReturn() {
797 setScoreUB(STORE_CNT, getScoreUB(STORE_CNT) +
798 getWaitCountMax(Context->getLimits(), STORE_CNT));
799 PendingEvents |= Context->WaitEventMaskForInst[STORE_CNT];
802 ArrayRef<const MachineInstr *> getLDSDMAStores()
const {
806 bool hasPointSampleAccel(
const MachineInstr &
MI)
const;
807 bool hasPointSamplePendingVmemTypes(
const MachineInstr &
MI,
810 void print(raw_ostream &)
const;
815 void purgeEmptyTrackingData();
825 void determineWaitForScore(InstCounterType
T,
unsigned Score,
826 AMDGPU::Waitcnt &
Wait)
const;
828 static bool mergeScore(
const MergeInfo &M,
unsigned &Score,
829 unsigned OtherScore);
832 assert(
Reg != AMDGPU::SCC &&
"Shouldn't be used on SCC");
833 if (!Context->TRI->isInAllocatableClass(
Reg))
835 const TargetRegisterClass *RC = Context->TRI->getPhysRegBaseClass(
Reg);
836 unsigned Size = Context->TRI->getRegSizeInBits(*RC);
837 if (
Size == 16 && Context->ST->hasD16Writes32BitVgpr())
838 Reg = Context->TRI->get32BitRegister(
Reg);
839 return Context->TRI->regunits(
Reg);
842 void setScoreLB(InstCounterType
T,
unsigned Val) {
847 void setScoreUB(InstCounterType
T,
unsigned Val) {
854 if (getScoreRange(EXP_CNT) > getWaitCountMax(Context->getLimits(), EXP_CNT))
856 ScoreUBs[
EXP_CNT] - getWaitCountMax(Context->getLimits(), EXP_CNT);
859 void setRegScore(
MCPhysReg Reg, InstCounterType
T,
unsigned Val) {
860 const SIRegisterInfo *
TRI = Context->TRI;
861 if (
Reg == AMDGPU::SCC) {
863 }
else if (
TRI->isVectorRegister(*Context->MRI,
Reg)) {
864 for (MCRegUnit RU : regunits(
Reg))
865 VMem[toVMEMID(RU)].Scores[
T] = Val;
866 }
else if (
TRI->isSGPRReg(*Context->MRI,
Reg)) {
867 auto STy = getSgprScoresIdx(
T);
868 for (MCRegUnit RU : regunits(
Reg))
869 SGPRs[RU].Scores[STy] = Val;
875 void setVMemScore(VMEMID TID, InstCounterType
T,
unsigned Val) {
876 VMem[TID].Scores[
T] = Val;
879 void setScoreByOperand(
const MachineOperand &
Op, InstCounterType CntTy,
882 const SIInsertWaitcnts *Context;
884 unsigned ScoreLBs[NUM_INST_CNTS] = {0};
885 unsigned ScoreUBs[NUM_INST_CNTS] = {0};
886 unsigned PendingEvents = 0;
888 unsigned LastFlat[NUM_INST_CNTS] = {0};
890 unsigned LastGDS = 0;
907 std::array<unsigned, NUM_INST_CNTS> Scores = {0};
909 unsigned VMEMTypes = 0;
912 return all_of(Scores, [](
unsigned K) {
return K == 0; }) && !VMEMTypes;
921 std::array<unsigned, 2> Scores = {0};
923 bool empty()
const {
return !Scores[0] && !Scores[1]; }
926 DenseMap<VMEMID, VMEMInfo> VMem;
927 DenseMap<MCRegUnit, SGPRInfo> SGPRs;
930 unsigned SCCScore = 0;
932 const MachineInstr *PendingSCCWrite =
nullptr;
936 SmallVector<const MachineInstr *> LDSDMAStores;
942 SIInsertWaitcntsLegacy() : MachineFunctionPass(ID) {}
944 bool runOnMachineFunction(MachineFunction &MF)
override;
946 StringRef getPassName()
const override {
947 return "SI insert wait instructions";
950 void getAnalysisUsage(AnalysisUsage &AU)
const override {
953 AU.
addRequired<MachinePostDominatorTreeWrapperPass>();
963 InstCounterType CntTy,
unsigned Score) {
964 setRegScore(
Op.getReg().asMCReg(), CntTy, Score);
972bool WaitcntBrackets::hasPointSampleAccel(
const MachineInstr &
MI)
const {
977 const AMDGPU::MIMGBaseOpcodeInfo *BaseInfo =
987bool WaitcntBrackets::hasPointSamplePendingVmemTypes(
const MachineInstr &
MI,
989 if (!hasPointSampleAccel(
MI))
992 return hasOtherPendingVmemTypes(
Reg, VMEM_NOSAMPLER);
995void WaitcntBrackets::updateByEvent(WaitEventType
E, MachineInstr &Inst) {
996 InstCounterType
T = eventCounter(
Context->WaitEventMaskForInst,
E);
999 unsigned UB = getScoreUB(
T);
1000 unsigned CurrScore = UB + 1;
1006 PendingEvents |= 1 <<
E;
1007 setScoreUB(
T, CurrScore);
1010 const MachineRegisterInfo *
MRI =
Context->MRI;
1019 if (
const auto *AddrOp =
TII->getNamedOperand(Inst, AMDGPU::OpName::addr))
1020 setScoreByOperand(*AddrOp, EXP_CNT, CurrScore);
1023 if (
const auto *Data0 =
1024 TII->getNamedOperand(Inst, AMDGPU::OpName::data0))
1025 setScoreByOperand(*Data0, EXP_CNT, CurrScore);
1026 if (
const auto *Data1 =
1027 TII->getNamedOperand(Inst, AMDGPU::OpName::data1))
1028 setScoreByOperand(*Data1, EXP_CNT, CurrScore);
1030 Inst.
getOpcode() != AMDGPU::DS_APPEND &&
1031 Inst.
getOpcode() != AMDGPU::DS_CONSUME &&
1032 Inst.
getOpcode() != AMDGPU::DS_ORDERED_COUNT) {
1033 for (
const MachineOperand &
Op : Inst.
all_uses()) {
1034 if (
TRI->isVectorRegister(*
MRI,
Op.getReg()))
1035 setScoreByOperand(
Op, EXP_CNT, CurrScore);
1038 }
else if (
TII->isFLAT(Inst)) {
1040 setScoreByOperand(*
TII->getNamedOperand(Inst, AMDGPU::OpName::data),
1041 EXP_CNT, CurrScore);
1043 setScoreByOperand(*
TII->getNamedOperand(Inst, AMDGPU::OpName::data),
1044 EXP_CNT, CurrScore);
1046 }
else if (
TII->isMIMG(Inst)) {
1048 setScoreByOperand(Inst.
getOperand(0), EXP_CNT, CurrScore);
1050 setScoreByOperand(*
TII->getNamedOperand(Inst, AMDGPU::OpName::data),
1051 EXP_CNT, CurrScore);
1053 }
else if (
TII->isMTBUF(Inst)) {
1055 setScoreByOperand(Inst.
getOperand(0), EXP_CNT, CurrScore);
1056 }
else if (
TII->isMUBUF(Inst)) {
1058 setScoreByOperand(Inst.
getOperand(0), EXP_CNT, CurrScore);
1060 setScoreByOperand(*
TII->getNamedOperand(Inst, AMDGPU::OpName::data),
1061 EXP_CNT, CurrScore);
1063 }
else if (
TII->isLDSDIR(Inst)) {
1065 setScoreByOperand(*
TII->getNamedOperand(Inst, AMDGPU::OpName::vdst),
1066 EXP_CNT, CurrScore);
1068 if (
TII->isEXP(Inst)) {
1073 for (MachineOperand &DefMO : Inst.
all_defs()) {
1074 if (
TRI->isVGPR(*
MRI, DefMO.getReg())) {
1075 setScoreByOperand(DefMO, EXP_CNT, CurrScore);
1079 for (
const MachineOperand &
Op : Inst.
all_uses()) {
1080 if (
TRI->isVectorRegister(*
MRI,
Op.getReg()))
1081 setScoreByOperand(
Op, EXP_CNT, CurrScore);
1084 }
else if (
T == X_CNT) {
1085 WaitEventType OtherEvent =
E == SMEM_GROUP ? VMEM_GROUP : SMEM_GROUP;
1086 if (PendingEvents & (1 << OtherEvent)) {
1091 setScoreLB(
T, getScoreUB(
T) - 1);
1092 PendingEvents &= ~(1 << OtherEvent);
1094 for (
const MachineOperand &
Op : Inst.
all_uses())
1095 setScoreByOperand(
Op,
T, CurrScore);
1096 }
else if (
T == VA_VDST ||
T == VM_VSRC) {
1099 for (
const MachineOperand &
Op : Inst.
operands()) {
1100 if (!
Op.isReg() || (
T == VA_VDST &&
Op.isUse()) ||
1101 (
T == VM_VSRC &&
Op.isDef()))
1104 setScoreByOperand(
Op,
T, CurrScore);
1116 for (
const MachineOperand &
Op : Inst.
defs()) {
1117 if (
T == LOAD_CNT ||
T == SAMPLE_CNT ||
T == BVH_CNT) {
1118 if (!
TRI->isVectorRegister(*
MRI,
Op.getReg()))
1120 if (updateVMCntOnly(Inst)) {
1125 VmemType
V = getVmemType(Inst);
1126 unsigned char TypesMask = 1 <<
V;
1129 if (hasPointSampleAccel(Inst))
1130 TypesMask |= 1 << VMEM_NOSAMPLER;
1131 for (MCRegUnit RU : regunits(
Op.getReg().asMCReg()))
1132 VMem[toVMEMID(RU)].VMEMTypes |= TypesMask;
1135 setScoreByOperand(
Op,
T, CurrScore);
1138 (
TII->isDS(Inst) ||
TII->mayWriteLDSThroughDMA(Inst))) {
1147 if (!MemOp->isStore() ||
1152 auto AAI = MemOp->getAAInfo();
1158 if (!AAI || !AAI.Scope)
1160 for (
unsigned I = 0,
E = LDSDMAStores.
size();
I !=
E && !Slot; ++
I) {
1161 for (
const auto *MemOp : LDSDMAStores[
I]->memoperands()) {
1162 if (MemOp->isStore() && AAI == MemOp->getAAInfo()) {
1177 setVMemScore(LDSDMA_BEGIN,
T, CurrScore);
1178 if (Slot && Slot < NUM_LDSDMA)
1179 setVMemScore(LDSDMA_BEGIN + Slot,
T, CurrScore);
1183 setRegScore(AMDGPU::SCC,
T, CurrScore);
1184 PendingSCCWrite = &Inst;
1189void WaitcntBrackets::print(raw_ostream &OS)
const {
1193 for (
auto T : inst_counter_types(
Context->MaxCounter)) {
1194 unsigned SR = getScoreRange(
T);
1198 OS <<
" " << (
ST->hasExtendedWaitCounts() ?
"LOAD" :
"VM") <<
"_CNT("
1202 OS <<
" " << (
ST->hasExtendedWaitCounts() ?
"DS" :
"LGKM") <<
"_CNT("
1206 OS <<
" EXP_CNT(" << SR <<
"):";
1209 OS <<
" " << (
ST->hasExtendedWaitCounts() ?
"STORE" :
"VS") <<
"_CNT("
1213 OS <<
" SAMPLE_CNT(" << SR <<
"):";
1216 OS <<
" BVH_CNT(" << SR <<
"):";
1219 OS <<
" KM_CNT(" << SR <<
"):";
1222 OS <<
" X_CNT(" << SR <<
"):";
1225 OS <<
" VA_VDST(" << SR <<
"): ";
1228 OS <<
" VM_VSRC(" << SR <<
"): ";
1231 OS <<
" UNKNOWN(" << SR <<
"):";
1237 unsigned LB = getScoreLB(
T);
1240 sort(SortedVMEMIDs);
1242 for (
auto ID : SortedVMEMIDs) {
1243 unsigned RegScore = VMem.at(
ID).Scores[
T];
1246 unsigned RelScore = RegScore - LB - 1;
1247 if (
ID < REGUNITS_END) {
1248 OS <<
' ' << RelScore <<
":vRU" <<
ID;
1250 assert(
ID >= LDSDMA_BEGIN &&
ID < LDSDMA_END &&
1251 "Unhandled/unexpected ID value!");
1252 OS <<
' ' << RelScore <<
":LDSDMA" <<
ID;
1257 if (isSmemCounter(
T)) {
1259 sort(SortedSMEMIDs);
1260 for (
auto ID : SortedSMEMIDs) {
1261 unsigned RegScore = SGPRs.at(
ID).Scores[getSgprScoresIdx(
T)];
1264 unsigned RelScore = RegScore - LB - 1;
1265 OS <<
' ' << RelScore <<
":sRU" <<
static_cast<unsigned>(
ID);
1269 if (
T == KM_CNT && SCCScore > 0)
1270 OS <<
' ' << SCCScore <<
":scc";
1275 OS <<
"Pending Events: ";
1276 if (hasPendingEvent()) {
1278 for (
unsigned I = 0;
I != NUM_WAIT_EVENTS; ++
I) {
1279 if (hasPendingEvent((WaitEventType)
I)) {
1280 OS <<
LS << WaitEventTypeName[
I];
1293void WaitcntBrackets::simplifyWaitcnt(
const AMDGPU::Waitcnt &CheckWait,
1294 AMDGPU::Waitcnt &UpdateWait)
const {
1295 simplifyWaitcnt(LOAD_CNT, UpdateWait.
LoadCnt);
1296 simplifyWaitcnt(EXP_CNT, UpdateWait.
ExpCnt);
1297 simplifyWaitcnt(DS_CNT, UpdateWait.
DsCnt);
1298 simplifyWaitcnt(STORE_CNT, UpdateWait.
StoreCnt);
1299 simplifyWaitcnt(SAMPLE_CNT, UpdateWait.
SampleCnt);
1300 simplifyWaitcnt(BVH_CNT, UpdateWait.
BvhCnt);
1301 simplifyWaitcnt(KM_CNT, UpdateWait.
KmCnt);
1302 simplifyXcnt(CheckWait, UpdateWait);
1303 simplifyWaitcnt(VA_VDST, UpdateWait.
VaVdst);
1304 simplifyVmVsrc(CheckWait, UpdateWait);
1307void WaitcntBrackets::simplifyWaitcnt(InstCounterType
T,
1308 unsigned &
Count)
const {
1312 if (
Count >= getScoreRange(
T))
1316void WaitcntBrackets::simplifyVmVsrc(
const AMDGPU::Waitcnt &CheckWait,
1317 AMDGPU::Waitcnt &UpdateWait)
const {
1322 std::min({CheckWait.LoadCnt, CheckWait.StoreCnt, CheckWait.SampleCnt,
1323 CheckWait.BvhCnt, CheckWait.DsCnt}))
1325 simplifyWaitcnt(VM_VSRC, UpdateWait.
VmVsrc);
1328void WaitcntBrackets::purgeEmptyTrackingData() {
1339void WaitcntBrackets::determineWaitForScore(InstCounterType
T,
1340 unsigned ScoreToWait,
1341 AMDGPU::Waitcnt &
Wait)
const {
1342 const unsigned LB = getScoreLB(
T);
1343 const unsigned UB = getScoreUB(
T);
1346 if ((UB >= ScoreToWait) && (ScoreToWait > LB)) {
1347 if ((
T == LOAD_CNT ||
T == DS_CNT) && hasPendingFlat() &&
1348 !
Context->ST->hasFlatLgkmVMemCountInOrder()) {
1352 addWait(
Wait,
T, 0);
1353 }
else if (counterOutOfOrder(
T)) {
1357 addWait(
Wait,
T, 0);
1361 unsigned NeededWait = std::min(
1362 UB - ScoreToWait, getWaitCountMax(
Context->getLimits(),
T) - 1);
1363 addWait(
Wait,
T, NeededWait);
1368void WaitcntBrackets::determineWaitForPhysReg(InstCounterType
T,
MCPhysReg Reg,
1369 AMDGPU::Waitcnt &
Wait)
const {
1370 if (
Reg == AMDGPU::SCC) {
1371 determineWaitForScore(
T, SCCScore,
Wait);
1374 for (MCRegUnit RU : regunits(
Reg))
1375 determineWaitForScore(
1376 T, IsVGPR ? getVMemScore(toVMEMID(RU),
T) : getSGPRScore(RU,
T),
1381void WaitcntBrackets::determineWaitForLDSDMA(InstCounterType
T, VMEMID TID,
1382 AMDGPU::Waitcnt &
Wait)
const {
1383 assert(TID >= LDSDMA_BEGIN && TID < LDSDMA_END);
1384 determineWaitForScore(
T, getVMemScore(TID,
T),
Wait);
1387void WaitcntBrackets::tryClearSCCWriteEvent(MachineInstr *Inst) {
1390 if (PendingSCCWrite &&
1391 PendingSCCWrite->
getOpcode() == AMDGPU::S_BARRIER_SIGNAL_ISFIRST_IMM &&
1393 unsigned SCC_WRITE_PendingEvent = 1 << SCC_WRITE;
1395 if ((PendingEvents &
Context->WaitEventMaskForInst[KM_CNT]) ==
1396 SCC_WRITE_PendingEvent) {
1397 setScoreLB(KM_CNT, getScoreUB(KM_CNT));
1400 PendingEvents &= ~SCC_WRITE_PendingEvent;
1401 PendingSCCWrite =
nullptr;
1405void WaitcntBrackets::applyWaitcnt(
const AMDGPU::Waitcnt &
Wait) {
1406 applyWaitcnt(LOAD_CNT,
Wait.LoadCnt);
1407 applyWaitcnt(EXP_CNT,
Wait.ExpCnt);
1408 applyWaitcnt(DS_CNT,
Wait.DsCnt);
1409 applyWaitcnt(STORE_CNT,
Wait.StoreCnt);
1410 applyWaitcnt(SAMPLE_CNT,
Wait.SampleCnt);
1411 applyWaitcnt(BVH_CNT,
Wait.BvhCnt);
1412 applyWaitcnt(KM_CNT,
Wait.KmCnt);
1413 applyWaitcnt(X_CNT,
Wait.XCnt);
1414 applyWaitcnt(VA_VDST,
Wait.VaVdst);
1415 applyWaitcnt(VM_VSRC,
Wait.VmVsrc);
1418void WaitcntBrackets::applyWaitcnt(InstCounterType
T,
unsigned Count) {
1419 const unsigned UB = getScoreUB(
T);
1423 if (counterOutOfOrder(
T))
1425 setScoreLB(
T, std::max(getScoreLB(
T), UB -
Count));
1428 PendingEvents &= ~Context->WaitEventMaskForInst[
T];
1431 if (
T == KM_CNT &&
Count == 0 && hasPendingEvent(SMEM_GROUP)) {
1432 if (!hasMixedPendingEvents(X_CNT))
1433 applyWaitcnt(X_CNT, 0);
1435 PendingEvents &= ~(1 << SMEM_GROUP);
1437 if (
T == LOAD_CNT && hasPendingEvent(VMEM_GROUP) &&
1438 !hasPendingEvent(STORE_CNT)) {
1439 if (!hasMixedPendingEvents(X_CNT))
1440 applyWaitcnt(X_CNT,
Count);
1441 else if (
Count == 0)
1442 PendingEvents &= ~(1 << VMEM_GROUP);
1446void WaitcntBrackets::simplifyXcnt(
const AMDGPU::Waitcnt &CheckWait,
1447 AMDGPU::Waitcnt &UpdateWait)
const {
1456 if (CheckWait.
KmCnt == 0 && hasPendingEvent(SMEM_GROUP))
1457 UpdateWait.
XCnt = ~0
u;
1461 if (CheckWait.
LoadCnt != ~0u && hasPendingEvent(VMEM_GROUP) &&
1462 !hasPendingEvent(STORE_CNT) && CheckWait.
XCnt >= CheckWait.
LoadCnt)
1463 UpdateWait.
XCnt = ~0
u;
1464 simplifyWaitcnt(X_CNT, UpdateWait.
XCnt);
1469bool WaitcntBrackets::counterOutOfOrder(InstCounterType
T)
const {
1471 if ((
T ==
Context->SmemAccessCounter && hasPendingEvent(SMEM_ACCESS)) ||
1472 (
T == X_CNT && hasPendingEvent(SMEM_GROUP)))
1478 if (
T == LOAD_CNT) {
1479 unsigned Events = hasPendingEvent(
T);
1482 Events &= ~(1 << GLOBAL_INV_ACCESS);
1485 return Events & (Events - 1);
1488 return hasMixedPendingEvents(
T);
1498char SIInsertWaitcntsLegacy::
ID = 0;
1503 return new SIInsertWaitcntsLegacy();
1508 int OpIdx = AMDGPU::getNamedOperandIdx(
MI.getOpcode(),
OpName);
1513 if (NewEnc == MO.
getImm())
1524 case AMDGPU::S_WAIT_LOADCNT:
1526 case AMDGPU::S_WAIT_EXPCNT:
1528 case AMDGPU::S_WAIT_STORECNT:
1530 case AMDGPU::S_WAIT_SAMPLECNT:
1532 case AMDGPU::S_WAIT_BVHCNT:
1534 case AMDGPU::S_WAIT_DSCNT:
1536 case AMDGPU::S_WAIT_KMCNT:
1538 case AMDGPU::S_WAIT_XCNT:
1545bool WaitcntGenerator::promoteSoftWaitCnt(MachineInstr *Waitcnt)
const {
1559bool WaitcntGeneratorPreGFX12::applyPreexistingWaitcnt(
1560 WaitcntBrackets &ScoreBrackets, MachineInstr &OldWaitcntInstr,
1563 assert(isNormalMode(MaxCounter));
1566 MachineInstr *WaitcntInstr =
nullptr;
1567 MachineInstr *WaitcntVsCntInstr =
nullptr;
1570 dbgs() <<
"PreGFX12::applyPreexistingWaitcnt at: ";
1572 dbgs() <<
"end of block\n";
1580 if (
II.isMetaInstruction()) {
1586 bool TrySimplify = Opcode !=
II.getOpcode() && !OptNone;
1590 if (Opcode == AMDGPU::S_WAITCNT) {
1591 unsigned IEnc =
II.getOperand(0).getImm();
1594 ScoreBrackets.simplifyWaitcnt(OldWait);
1598 if (WaitcntInstr || (!
Wait.hasWaitExceptStoreCnt() && TrySimplify)) {
1599 II.eraseFromParent();
1603 }
else if (Opcode == AMDGPU::S_WAITCNT_lds_direct) {
1606 <<
"Before: " <<
Wait <<
'\n';);
1607 ScoreBrackets.determineWaitForLDSDMA(LOAD_CNT, LDSDMA_BEGIN,
Wait);
1616 II.eraseFromParent();
1618 assert(Opcode == AMDGPU::S_WAITCNT_VSCNT);
1619 assert(
II.getOperand(0).getReg() == AMDGPU::SGPR_NULL);
1622 TII->getNamedOperand(
II, AMDGPU::OpName::simm16)->getImm();
1624 ScoreBrackets.simplifyWaitcnt(InstCounterType::STORE_CNT, OldVSCnt);
1625 Wait.StoreCnt = std::min(
Wait.StoreCnt, OldVSCnt);
1627 if (WaitcntVsCntInstr || (!
Wait.hasWaitStoreCnt() && TrySimplify)) {
1628 II.eraseFromParent();
1631 WaitcntVsCntInstr = &
II;
1638 Modified |= promoteSoftWaitCnt(WaitcntInstr);
1640 ScoreBrackets.applyWaitcnt(LOAD_CNT,
Wait.LoadCnt);
1641 ScoreBrackets.applyWaitcnt(EXP_CNT,
Wait.ExpCnt);
1642 ScoreBrackets.applyWaitcnt(DS_CNT,
Wait.DsCnt);
1647 LLVM_DEBUG(It.isEnd() ?
dbgs() <<
"applied pre-existing waitcnt\n"
1648 <<
"New Instr at block end: "
1649 << *WaitcntInstr <<
'\n'
1650 :
dbgs() <<
"applied pre-existing waitcnt\n"
1651 <<
"Old Instr: " << *It
1652 <<
"New Instr: " << *WaitcntInstr <<
'\n');
1655 if (WaitcntVsCntInstr) {
1657 AMDGPU::OpName::simm16,
Wait.StoreCnt);
1658 Modified |= promoteSoftWaitCnt(WaitcntVsCntInstr);
1660 ScoreBrackets.applyWaitcnt(STORE_CNT,
Wait.StoreCnt);
1661 Wait.StoreCnt = ~0
u;
1664 ?
dbgs() <<
"applied pre-existing waitcnt\n"
1665 <<
"New Instr at block end: " << *WaitcntVsCntInstr
1667 :
dbgs() <<
"applied pre-existing waitcnt\n"
1668 <<
"Old Instr: " << *It
1669 <<
"New Instr: " << *WaitcntVsCntInstr <<
'\n');
1677bool WaitcntGeneratorPreGFX12::createNewWaitcnt(
1679 AMDGPU::Waitcnt
Wait, WaitcntBrackets *ScoreBrackets) {
1681 assert(isNormalMode(MaxCounter));
1689 auto EmitExpandedWaitcnt = [&](
unsigned Outstanding,
unsigned Target,
1691 if (Outstanding > Target) {
1692 for (
unsigned i = Outstanding - 1; i >=
Target && i != ~0
u; --i) {
1697 EmitWaitcnt(Target);
1704 if (
Wait.hasWaitExceptStoreCnt()) {
1707 if (ExpandWaitcntProfiling && ScoreBrackets) {
1711 bool AnyOutOfOrder =
false;
1712 for (
auto CT : {LOAD_CNT, DS_CNT,
EXP_CNT}) {
1713 unsigned &WaitCnt = getCounterRef(
Wait, CT);
1714 if (WaitCnt != ~0u && ScoreBrackets->counterOutOfOrder(CT)) {
1715 AnyOutOfOrder =
true;
1720 if (AnyOutOfOrder) {
1727 for (
auto CT : {LOAD_CNT, DS_CNT,
EXP_CNT}) {
1728 unsigned &WaitCnt = getCounterRef(
Wait, CT);
1732 unsigned Outstanding = std::min(ScoreBrackets->getScoreUB(CT) -
1733 ScoreBrackets->getScoreLB(CT),
1734 getWaitCountMax(getLimits(), CT) - 1);
1735 EmitExpandedWaitcnt(Outstanding, WaitCnt, [&](
unsigned Count) {
1737 getCounterRef(W, CT) =
Count;
1746 [[maybe_unused]]
auto SWaitInst =
1751 if (It !=
Block.instr_end())
dbgs() <<
"Old Instr: " << *It;
1752 dbgs() <<
"New Instr: " << *SWaitInst <<
'\n');
1756 if (
Wait.hasWaitStoreCnt()) {
1759 if (ExpandWaitcntProfiling && ScoreBrackets &&
Wait.StoreCnt != ~0u &&
1760 !ScoreBrackets->counterOutOfOrder(STORE_CNT)) {
1762 unsigned Outstanding =
1763 std::min(ScoreBrackets->getScoreUB(STORE_CNT) -
1764 ScoreBrackets->getScoreLB(STORE_CNT),
1765 getWaitCountMax(getLimits(), STORE_CNT) - 1);
1766 EmitExpandedWaitcnt(Outstanding,
Wait.StoreCnt, [&](
unsigned Count) {
1767 BuildMI(Block, It, DL, TII->get(AMDGPU::S_WAITCNT_VSCNT))
1768 .addReg(AMDGPU::SGPR_NULL, RegState::Undef)
1772 [[maybe_unused]]
auto SWaitInst =
1779 if (It !=
Block.instr_end())
dbgs() <<
"Old Instr: " << *It;
1780 dbgs() <<
"New Instr: " << *SWaitInst <<
'\n');
1788WaitcntGeneratorPreGFX12::getAllZeroWaitcnt(
bool IncludeVSCnt)
const {
1789 return AMDGPU::Waitcnt(0, 0, 0, IncludeVSCnt &&
ST->hasVscnt() ? 0 : ~0u);
1793WaitcntGeneratorGFX12Plus::getAllZeroWaitcnt(
bool IncludeVSCnt)
const {
1794 unsigned ExpertVal = IsExpertMode ? 0 : ~0
u;
1795 return AMDGPU::Waitcnt(0, 0, 0, IncludeVSCnt ? 0 : ~0u, 0, 0, 0,
1796 ~0u , ExpertVal, ExpertVal);
1803bool WaitcntGeneratorGFX12Plus::applyPreexistingWaitcnt(
1804 WaitcntBrackets &ScoreBrackets, MachineInstr &OldWaitcntInstr,
1807 assert(!isNormalMode(MaxCounter));
1810 MachineInstr *CombinedLoadDsCntInstr =
nullptr;
1811 MachineInstr *CombinedStoreDsCntInstr =
nullptr;
1812 MachineInstr *WaitcntDepctrInstr =
nullptr;
1813 MachineInstr *WaitInstrs[NUM_EXTENDED_INST_CNTS] = {};
1816 dbgs() <<
"GFX12Plus::applyPreexistingWaitcnt at: ";
1818 dbgs() <<
"end of block\n";
1824 AMDGPU::Waitcnt RequiredWait;
1829 if (
II.isMetaInstruction()) {
1834 MachineInstr **UpdatableInstr;
1840 bool TrySimplify = Opcode !=
II.getOpcode() && !OptNone;
1844 if (Opcode == AMDGPU::S_WAITCNT)
1847 if (Opcode == AMDGPU::S_WAIT_LOADCNT_DSCNT) {
1849 TII->getNamedOperand(
II, AMDGPU::OpName::simm16)->getImm();
1854 RequiredWait = RequiredWait.combined(OldWait);
1855 UpdatableInstr = &CombinedLoadDsCntInstr;
1856 }
else if (Opcode == AMDGPU::S_WAIT_STORECNT_DSCNT) {
1858 TII->getNamedOperand(
II, AMDGPU::OpName::simm16)->getImm();
1863 RequiredWait = RequiredWait.combined(OldWait);
1864 UpdatableInstr = &CombinedStoreDsCntInstr;
1865 }
else if (Opcode == AMDGPU::S_WAITCNT_DEPCTR) {
1867 TII->getNamedOperand(
II, AMDGPU::OpName::simm16)->getImm();
1868 AMDGPU::Waitcnt OldWait;
1872 ScoreBrackets.simplifyWaitcnt(OldWait);
1874 UpdatableInstr = &WaitcntDepctrInstr;
1875 }
else if (Opcode == AMDGPU::S_WAITCNT_lds_direct) {
1878 II.eraseFromParent();
1884 TII->getNamedOperand(
II, AMDGPU::OpName::simm16)->getImm();
1886 addWait(
Wait, CT.value(), OldCnt);
1888 addWait(RequiredWait, CT.value(), OldCnt);
1889 UpdatableInstr = &WaitInstrs[CT.value()];
1893 if (!*UpdatableInstr) {
1894 *UpdatableInstr = &
II;
1895 }
else if (Opcode == AMDGPU::S_WAITCNT_DEPCTR) {
1902 unsigned Enc =
TII->getNamedOperand(
II, AMDGPU::OpName::simm16)->getImm();
1910 II.eraseFromParent();
1914 II.eraseFromParent();
1919 ScoreBrackets.simplifyWaitcnt(
Wait.combined(RequiredWait),
Wait);
1920 Wait =
Wait.combined(RequiredWait);
1922 if (CombinedLoadDsCntInstr) {
1935 if (
Wait.LoadCnt != ~0u &&
Wait.DsCnt != ~0u) {
1938 AMDGPU::OpName::simm16, NewEnc);
1939 Modified |= promoteSoftWaitCnt(CombinedLoadDsCntInstr);
1940 ScoreBrackets.applyWaitcnt(LOAD_CNT,
Wait.LoadCnt);
1941 ScoreBrackets.applyWaitcnt(DS_CNT,
Wait.DsCnt);
1945 LLVM_DEBUG(It.isEnd() ?
dbgs() <<
"applied pre-existing waitcnt\n"
1946 <<
"New Instr at block end: "
1947 << *CombinedLoadDsCntInstr <<
'\n'
1948 :
dbgs() <<
"applied pre-existing waitcnt\n"
1949 <<
"Old Instr: " << *It <<
"New Instr: "
1950 << *CombinedLoadDsCntInstr <<
'\n');
1957 if (CombinedStoreDsCntInstr) {
1959 if (
Wait.StoreCnt != ~0u &&
Wait.DsCnt != ~0u) {
1962 AMDGPU::OpName::simm16, NewEnc);
1963 Modified |= promoteSoftWaitCnt(CombinedStoreDsCntInstr);
1964 ScoreBrackets.applyWaitcnt(STORE_CNT,
Wait.StoreCnt);
1965 ScoreBrackets.applyWaitcnt(DS_CNT,
Wait.DsCnt);
1966 Wait.StoreCnt = ~0
u;
1969 LLVM_DEBUG(It.isEnd() ?
dbgs() <<
"applied pre-existing waitcnt\n"
1970 <<
"New Instr at block end: "
1971 << *CombinedStoreDsCntInstr <<
'\n'
1972 :
dbgs() <<
"applied pre-existing waitcnt\n"
1973 <<
"Old Instr: " << *It <<
"New Instr: "
1974 << *CombinedStoreDsCntInstr <<
'\n');
1987 if (
Wait.DsCnt != ~0u) {
1996 if (
Wait.LoadCnt != ~0u) {
1997 WaitsToErase.
push_back(&WaitInstrs[LOAD_CNT]);
1998 WaitsToErase.
push_back(&WaitInstrs[DS_CNT]);
1999 }
else if (
Wait.StoreCnt != ~0u) {
2000 WaitsToErase.
push_back(&WaitInstrs[STORE_CNT]);
2001 WaitsToErase.
push_back(&WaitInstrs[DS_CNT]);
2004 for (MachineInstr **WI : WaitsToErase) {
2008 (*WI)->eraseFromParent();
2014 for (
auto CT : inst_counter_types(NUM_EXTENDED_INST_CNTS)) {
2015 if (!WaitInstrs[CT])
2018 unsigned NewCnt = getWait(
Wait, CT);
2019 if (NewCnt != ~0u) {
2021 AMDGPU::OpName::simm16, NewCnt);
2022 Modified |= promoteSoftWaitCnt(WaitInstrs[CT]);
2024 ScoreBrackets.applyWaitcnt(CT, NewCnt);
2025 setNoWait(
Wait, CT);
2028 ?
dbgs() <<
"applied pre-existing waitcnt\n"
2029 <<
"New Instr at block end: " << *WaitInstrs[CT]
2031 :
dbgs() <<
"applied pre-existing waitcnt\n"
2032 <<
"Old Instr: " << *It
2033 <<
"New Instr: " << *WaitInstrs[CT] <<
'\n');
2040 if (WaitcntDepctrInstr) {
2044 TII->getNamedOperand(*WaitcntDepctrInstr, AMDGPU::OpName::simm16)
2049 ScoreBrackets.applyWaitcnt(VA_VDST,
Wait.VaVdst);
2050 ScoreBrackets.applyWaitcnt(VM_VSRC,
Wait.VmVsrc);
2059 AMDGPU::OpName::simm16, Enc);
2061 <<
"New Instr at block end: "
2062 << *WaitcntDepctrInstr <<
'\n'
2063 :
dbgs() <<
"applyPreexistingWaitcnt\n"
2064 <<
"Old Instr: " << *It <<
"New Instr: "
2065 << *WaitcntDepctrInstr <<
'\n');
2076bool WaitcntGeneratorGFX12Plus::createNewWaitcnt(
2078 AMDGPU::Waitcnt
Wait, WaitcntBrackets *ScoreBrackets) {
2080 assert(!isNormalMode(MaxCounter));
2086 auto EmitExpandedWaitcnt = [&](
unsigned Outstanding,
unsigned Target,
2088 if (Outstanding > Target) {
2089 for (
unsigned i = Outstanding - 1; i >=
Target && i != ~0
u; --i) {
2094 EmitWaitcnt(Target);
2101 if (ExpandWaitcntProfiling && ScoreBrackets) {
2102 for (
auto CT : inst_counter_types(NUM_EXTENDED_INST_CNTS)) {
2108 if (ScoreBrackets->counterOutOfOrder(CT)) {
2115 unsigned Outstanding = std::min(ScoreBrackets->getScoreUB(CT) -
2116 ScoreBrackets->getScoreLB(CT),
2117 getWaitCountMax(getLimits(), CT) - 1);
2118 EmitExpandedWaitcnt(Outstanding,
Count, [&](
unsigned Val) {
2128 if (
Wait.DsCnt != ~0u) {
2129 MachineInstr *SWaitInst =
nullptr;
2131 if (
Wait.LoadCnt != ~0u) {
2139 }
else if (
Wait.StoreCnt != ~0u) {
2146 Wait.StoreCnt = ~0
u;
2154 if (It !=
Block.instr_end())
dbgs() <<
"Old Instr: " << *It;
2155 dbgs() <<
"New Instr: " << *SWaitInst <<
'\n');
2162 for (
auto CT : inst_counter_types(NUM_EXTENDED_INST_CNTS)) {
2167 [[maybe_unused]]
auto SWaitInst =
2174 if (It !=
Block.instr_end())
dbgs() <<
"Old Instr: " << *It;
2175 dbgs() <<
"New Instr: " << *SWaitInst <<
'\n');
2178 if (
Wait.hasWaitDepctr()) {
2183 [[maybe_unused]]
auto SWaitInst =
2189 if (It !=
Block.instr_end())
dbgs() <<
"Old Instr: " << *It;
2190 dbgs() <<
"New Instr: " << *SWaitInst <<
'\n');
2208bool SIInsertWaitcnts::generateWaitcntInstBefore(MachineInstr &
MI,
2209 WaitcntBrackets &ScoreBrackets,
2210 MachineInstr *OldWaitcntInstr,
2212 setForceEmitWaitcnt();
2216 AMDGPU::Waitcnt
Wait;
2217 const unsigned Opc =
MI.getOpcode();
2223 if (
Opc == AMDGPU::BUFFER_WBINVL1 ||
Opc == AMDGPU::BUFFER_WBINVL1_SC ||
2224 Opc == AMDGPU::BUFFER_WBINVL1_VOL ||
Opc == AMDGPU::BUFFER_GL0_INV ||
2225 Opc == AMDGPU::BUFFER_GL1_INV) {
2232 if (
Opc == AMDGPU::SI_RETURN_TO_EPILOG ||
Opc == AMDGPU::SI_RETURN ||
2233 Opc == AMDGPU::SI_WHOLE_WAVE_FUNC_RETURN ||
2234 Opc == AMDGPU::S_SETPC_B64_return) {
2236 AMDGPU::Waitcnt AllZeroWait =
2237 WCG->getAllZeroWaitcnt(
false);
2242 if (
ST->hasExtendedWaitCounts() &&
2243 !ScoreBrackets.hasPendingEvent(VMEM_ACCESS))
2255 else if (
Opc == AMDGPU::S_ENDPGM ||
Opc == AMDGPU::S_ENDPGM_SAVED) {
2256 if (!WCG->isOptNone() &&
2257 (
MI.getMF()->getInfo<SIMachineFunctionInfo>()->isDynamicVGPREnabled() ||
2258 (
ST->getGeneration() >= AMDGPUSubtarget::GFX11 &&
2259 ScoreBrackets.getScoreRange(STORE_CNT) != 0 &&
2260 !ScoreBrackets.hasPendingEvent(SCRATCH_WRITE_ACCESS))))
2264 else if ((
Opc == AMDGPU::S_SENDMSG ||
Opc == AMDGPU::S_SENDMSGHALT) &&
2265 ST->hasLegacyGeometry() &&
2276 if (
MI.modifiesRegister(AMDGPU::EXEC,
TRI)) {
2279 if (ScoreBrackets.hasPendingEvent(EXP_GPR_LOCK) ||
2280 ScoreBrackets.hasPendingEvent(EXP_PARAM_ACCESS) ||
2281 ScoreBrackets.hasPendingEvent(EXP_POS_ACCESS) ||
2282 ScoreBrackets.hasPendingEvent(GDS_GPR_LOCK)) {
2289 if (
TII->isAlwaysGDS(
Opc) && ScoreBrackets.hasPendingGDS())
2290 addWait(
Wait, DS_CNT, ScoreBrackets.getPendingGDSWait());
2297 Wait = AMDGPU::Waitcnt();
2299 const MachineOperand &CallAddrOp =
TII->getCalleeOperand(
MI);
2300 if (CallAddrOp.
isReg()) {
2301 ScoreBrackets.determineWaitForPhysReg(
2304 if (
const auto *RtnAddrOp =
2305 TII->getNamedOperand(
MI, AMDGPU::OpName::dst)) {
2306 ScoreBrackets.determineWaitForPhysReg(
2307 SmemAccessCounter, RtnAddrOp->getReg().asMCReg(),
Wait);
2310 }
else if (
Opc == AMDGPU::S_BARRIER_WAIT) {
2311 ScoreBrackets.tryClearSCCWriteEvent(&
MI);
2327 for (
const MachineMemOperand *Memop :
MI.memoperands()) {
2328 const Value *Ptr = Memop->getValue();
2329 if (Memop->isStore()) {
2330 if (
auto It = SLoadAddresses.
find(Ptr); It != SLoadAddresses.
end()) {
2331 addWait(
Wait, SmemAccessCounter, 0);
2333 SLoadAddresses.
erase(It);
2336 unsigned AS = Memop->getAddrSpace();
2340 if (
TII->mayWriteLDSThroughDMA(
MI))
2344 unsigned TID = LDSDMA_BEGIN;
2345 if (Ptr && Memop->getAAInfo()) {
2346 const auto &LDSDMAStores = ScoreBrackets.getLDSDMAStores();
2347 for (
unsigned I = 0,
E = LDSDMAStores.size();
I !=
E; ++
I) {
2348 if (
MI.mayAlias(AA, *LDSDMAStores[
I],
true)) {
2349 if ((
I + 1) >= NUM_LDSDMA) {
2352 ScoreBrackets.determineWaitForLDSDMA(LOAD_CNT, TID,
Wait);
2356 ScoreBrackets.determineWaitForLDSDMA(LOAD_CNT, TID +
I + 1,
Wait);
2360 ScoreBrackets.determineWaitForLDSDMA(LOAD_CNT, TID,
Wait);
2362 if (Memop->isStore()) {
2363 ScoreBrackets.determineWaitForLDSDMA(EXP_CNT, TID,
Wait);
2368 for (
const MachineOperand &
Op :
MI.operands()) {
2373 if (
Op.isTied() &&
Op.isUse() &&
TII->doesNotReadTiedSource(
MI))
2378 const bool IsVGPR =
TRI->isVectorRegister(*
MRI,
Op.getReg());
2385 if (
Op.isImplicit() &&
MI.mayLoadOrStore())
2388 ScoreBrackets.determineWaitForPhysReg(VA_VDST,
Reg,
Wait);
2390 ScoreBrackets.determineWaitForPhysReg(VM_VSRC,
Reg,
Wait);
2397 if (
Op.isUse() || !updateVMCntOnly(
MI) ||
2398 ScoreBrackets.hasOtherPendingVmemTypes(
Reg, getVmemType(
MI)) ||
2399 ScoreBrackets.hasPointSamplePendingVmemTypes(
MI,
Reg) ||
2400 !
ST->hasVmemWriteVgprInOrder()) {
2401 ScoreBrackets.determineWaitForPhysReg(LOAD_CNT,
Reg,
Wait);
2402 ScoreBrackets.determineWaitForPhysReg(SAMPLE_CNT,
Reg,
Wait);
2403 ScoreBrackets.determineWaitForPhysReg(BVH_CNT,
Reg,
Wait);
2404 ScoreBrackets.clearVgprVmemTypes(
Reg);
2407 if (
Op.isDef() || ScoreBrackets.hasPendingEvent(EXP_LDS_ACCESS)) {
2408 ScoreBrackets.determineWaitForPhysReg(EXP_CNT,
Reg,
Wait);
2410 ScoreBrackets.determineWaitForPhysReg(DS_CNT,
Reg,
Wait);
2411 }
else if (
Op.getReg() == AMDGPU::SCC) {
2412 ScoreBrackets.determineWaitForPhysReg(KM_CNT,
Reg,
Wait);
2414 ScoreBrackets.determineWaitForPhysReg(SmemAccessCounter,
Reg,
Wait);
2417 if (
ST->hasWaitXCnt() &&
Op.isDef())
2418 ScoreBrackets.determineWaitForPhysReg(X_CNT,
Reg,
Wait);
2435 if (
Opc == AMDGPU::S_BARRIER && !
ST->hasAutoWaitcntBeforeBarrier() &&
2436 !
ST->supportsBackOffBarrier()) {
2437 Wait =
Wait.combined(WCG->getAllZeroWaitcnt(
true));
2444 ScoreBrackets.hasPendingEvent(SMEM_ACCESS)) {
2449 ScoreBrackets.simplifyWaitcnt(
Wait);
2455 if (
TII->isVALU(
MI))
2461 if (
Wait.XCnt != ~0u && isVmemAccess(
MI)) {
2462 ScoreBrackets.applyWaitcnt(X_CNT,
Wait.XCnt);
2469 Wait = WCG->getAllZeroWaitcnt(
false);
2471 if (ForceEmitWaitcnt[LOAD_CNT])
2473 if (ForceEmitWaitcnt[EXP_CNT])
2475 if (ForceEmitWaitcnt[DS_CNT])
2477 if (ForceEmitWaitcnt[SAMPLE_CNT])
2479 if (ForceEmitWaitcnt[BVH_CNT])
2481 if (ForceEmitWaitcnt[KM_CNT])
2483 if (ForceEmitWaitcnt[X_CNT])
2487 if (ForceEmitWaitcnt[VA_VDST])
2489 if (ForceEmitWaitcnt[VM_VSRC])
2494 if (ScoreBrackets.hasPendingEvent(LOAD_CNT))
2496 if (ScoreBrackets.hasPendingEvent(SAMPLE_CNT))
2498 if (ScoreBrackets.hasPendingEvent(BVH_CNT))
2505 return generateWaitcnt(
Wait,
MI.getIterator(), *
MI.getParent(), ScoreBrackets,
2509bool SIInsertWaitcnts::generateWaitcnt(AMDGPU::Waitcnt
Wait,
2511 MachineBasicBlock &
Block,
2512 WaitcntBrackets &ScoreBrackets,
2513 MachineInstr *OldWaitcntInstr) {
2516 if (OldWaitcntInstr)
2520 WCG->applyPreexistingWaitcnt(ScoreBrackets, *OldWaitcntInstr,
Wait, It);
2522 AMDGPU::Waitcnt WaitForScore =
Wait;
2525 if (
Wait.ExpCnt != ~0u && It !=
Block.instr_end() &&
2527 MachineOperand *WaitExp =
2528 TII->getNamedOperand(*It, AMDGPU::OpName::waitexp);
2536 <<
"Update Instr: " << *It);
2539 if (WCG->createNewWaitcnt(
Block, It,
Wait, &ScoreBrackets))
2544 ScoreBrackets.applyWaitcnt(WaitForScore);
2549std::optional<WaitEventType>
2550SIInsertWaitcnts::getExpertSchedulingEventType(
const MachineInstr &Inst)
const {
2551 if (
TII->isVALU(Inst)) {
2556 if (
TII->isXDL(Inst))
2557 return VGPR_XDL_WRITE;
2559 if (
TII->isTRANS(Inst))
2560 return VGPR_TRANS_WRITE;
2563 return VGPR_DPMACC_WRITE;
2565 return VGPR_CSMACC_WRITE;
2572 if (
TII->isFLAT(Inst))
2573 return VGPR_FLAT_READ;
2575 if (
TII->isDS(Inst))
2576 return VGPR_LDS_READ;
2578 if (
TII->isVMEM(Inst) ||
TII->isVIMAGE(Inst) ||
TII->isVSAMPLE(Inst))
2579 return VGPR_VMEM_READ;
2586bool SIInsertWaitcnts::isVmemAccess(
const MachineInstr &
MI)
const {
2587 return (
TII->isFLAT(
MI) &&
TII->mayAccessVMEMThroughFlat(
MI)) ||
2594 MachineBasicBlock *
Block)
const {
2595 auto BlockEnd =
Block->getParent()->end();
2596 auto BlockIter =
Block->getIterator();
2600 if (++BlockIter != BlockEnd) {
2601 It = BlockIter->instr_begin();
2608 if (!It->isMetaInstruction())
2616 return It->getOpcode() == AMDGPU::S_ENDPGM;
2620bool SIInsertWaitcnts::insertForcedWaitAfter(MachineInstr &Inst,
2621 MachineBasicBlock &
Block,
2622 WaitcntBrackets &ScoreBrackets) {
2623 AMDGPU::Waitcnt
Wait;
2624 bool NeedsEndPGMCheck =
false;
2632 NeedsEndPGMCheck =
true;
2635 ScoreBrackets.simplifyWaitcnt(
Wait);
2638 bool Result = generateWaitcnt(
Wait, SuccessorIt,
Block, ScoreBrackets,
2641 if (Result && NeedsEndPGMCheck && isNextENDPGM(SuccessorIt, &
Block)) {
2649void SIInsertWaitcnts::updateEventWaitcntAfter(MachineInstr &Inst,
2650 WaitcntBrackets *ScoreBrackets) {
2658 bool IsVMEMAccess =
false;
2659 bool IsSMEMAccess =
false;
2662 if (
const auto ET = getExpertSchedulingEventType(Inst))
2663 ScoreBrackets->updateByEvent(*ET, Inst);
2666 if (
TII->isDS(Inst) &&
TII->usesLGKM_CNT(Inst)) {
2668 TII->hasModifiersSet(Inst, AMDGPU::OpName::gds)) {
2669 ScoreBrackets->updateByEvent(GDS_ACCESS, Inst);
2670 ScoreBrackets->updateByEvent(GDS_GPR_LOCK, Inst);
2671 ScoreBrackets->setPendingGDS();
2673 ScoreBrackets->updateByEvent(LDS_ACCESS, Inst);
2675 }
else if (
TII->isFLAT(Inst)) {
2677 ScoreBrackets->updateByEvent(getVmemWaitEventType(Inst), Inst);
2683 int FlatASCount = 0;
2685 if (
TII->mayAccessVMEMThroughFlat(Inst)) {
2687 IsVMEMAccess =
true;
2688 ScoreBrackets->updateByEvent(getVmemWaitEventType(Inst), Inst);
2691 if (
TII->mayAccessLDSThroughFlat(Inst)) {
2693 ScoreBrackets->updateByEvent(LDS_ACCESS, Inst);
2702 ScoreBrackets->setPendingFlat();
2705 IsVMEMAccess =
true;
2706 ScoreBrackets->updateByEvent(getVmemWaitEventType(Inst), Inst);
2708 if (
ST->vmemWriteNeedsExpWaitcnt() &&
2710 ScoreBrackets->updateByEvent(VMW_GPR_LOCK, Inst);
2712 }
else if (
TII->isSMRD(Inst)) {
2713 IsSMEMAccess =
true;
2714 ScoreBrackets->updateByEvent(SMEM_ACCESS, Inst);
2715 }
else if (Inst.
isCall()) {
2717 ScoreBrackets->applyWaitcnt(WCG->getAllZeroWaitcnt(
false));
2718 ScoreBrackets->setStateOnFunctionEntryOrReturn();
2720 ScoreBrackets->updateByEvent(EXP_LDS_ACCESS, Inst);
2721 }
else if (
TII->isVINTERP(Inst)) {
2722 int64_t
Imm =
TII->getNamedOperand(Inst, AMDGPU::OpName::waitexp)->getImm();
2723 ScoreBrackets->applyWaitcnt(EXP_CNT, Imm);
2725 unsigned Imm =
TII->getNamedOperand(Inst, AMDGPU::OpName::tgt)->getImm();
2727 ScoreBrackets->updateByEvent(EXP_PARAM_ACCESS, Inst);
2729 ScoreBrackets->updateByEvent(EXP_POS_ACCESS, Inst);
2731 ScoreBrackets->updateByEvent(EXP_GPR_LOCK, Inst);
2733 ScoreBrackets->updateByEvent(SCC_WRITE, Inst);
2736 case AMDGPU::S_SENDMSG:
2737 case AMDGPU::S_SENDMSG_RTN_B32:
2738 case AMDGPU::S_SENDMSG_RTN_B64:
2739 case AMDGPU::S_SENDMSGHALT:
2740 ScoreBrackets->updateByEvent(SQ_MESSAGE, Inst);
2742 case AMDGPU::S_MEMTIME:
2743 case AMDGPU::S_MEMREALTIME:
2744 case AMDGPU::S_GET_BARRIER_STATE_M0:
2745 case AMDGPU::S_GET_BARRIER_STATE_IMM:
2746 ScoreBrackets->updateByEvent(SMEM_ACCESS, Inst);
2751 if (!
ST->hasWaitXCnt())
2755 ScoreBrackets->updateByEvent(VMEM_GROUP, Inst);
2758 ScoreBrackets->updateByEvent(SMEM_GROUP, Inst);
2761bool WaitcntBrackets::mergeScore(
const MergeInfo &M,
unsigned &Score,
2762 unsigned OtherScore) {
2763 unsigned MyShifted = Score <=
M.OldLB ? 0 : Score +
M.MyShift;
2764 unsigned OtherShifted =
2765 OtherScore <=
M.OtherLB ? 0 : OtherScore +
M.OtherShift;
2766 Score = std::max(MyShifted, OtherShifted);
2767 return OtherShifted > MyShifted;
2775bool WaitcntBrackets::merge(
const WaitcntBrackets &
Other) {
2776 bool StrictDom =
false;
2780 for (
auto K :
Other.VMem.keys())
2781 VMem.try_emplace(K);
2782 for (
auto K :
Other.SGPRs.keys())
2783 SGPRs.try_emplace(K);
2785 for (
auto T : inst_counter_types(
Context->MaxCounter)) {
2787 const unsigned *WaitEventMaskForInst =
Context->WaitEventMaskForInst;
2788 const unsigned OldEvents = PendingEvents & WaitEventMaskForInst[
T];
2789 const unsigned OtherEvents =
Other.PendingEvents & WaitEventMaskForInst[
T];
2790 if (OtherEvents & ~OldEvents)
2792 PendingEvents |= OtherEvents;
2795 const unsigned MyPending = ScoreUBs[
T] - ScoreLBs[
T];
2796 const unsigned OtherPending =
Other.ScoreUBs[
T] -
Other.ScoreLBs[
T];
2797 const unsigned NewUB = ScoreLBs[
T] + std::max(MyPending, OtherPending);
2798 if (NewUB < ScoreLBs[
T])
2802 M.OldLB = ScoreLBs[
T];
2803 M.OtherLB =
Other.ScoreLBs[
T];
2804 M.MyShift = NewUB - ScoreUBs[
T];
2805 M.OtherShift = NewUB -
Other.ScoreUBs[
T];
2807 ScoreUBs[
T] = NewUB;
2809 StrictDom |= mergeScore(M, LastFlat[
T],
Other.LastFlat[
T]);
2812 StrictDom |= mergeScore(M, LastGDS,
Other.LastGDS);
2815 StrictDom |= mergeScore(M, SCCScore,
Other.SCCScore);
2816 if (
Other.hasPendingEvent(SCC_WRITE)) {
2817 unsigned OldEventsHasSCCWrite = OldEvents & (1 << SCC_WRITE);
2818 if (!OldEventsHasSCCWrite) {
2819 PendingSCCWrite =
Other.PendingSCCWrite;
2820 }
else if (PendingSCCWrite !=
Other.PendingSCCWrite) {
2821 PendingSCCWrite =
nullptr;
2826 for (
auto &[RegID,
Info] : VMem)
2827 StrictDom |= mergeScore(M,
Info.Scores[
T],
Other.getVMemScore(RegID,
T));
2829 if (isSmemCounter(
T)) {
2830 unsigned Idx = getSgprScoresIdx(
T);
2831 for (
auto &[RegID,
Info] : SGPRs) {
2832 auto It =
Other.SGPRs.find(RegID);
2833 unsigned OtherScore =
2834 (It !=
Other.SGPRs.end()) ? It->second.Scores[Idx] : 0;
2835 StrictDom |= mergeScore(M,
Info.Scores[Idx], OtherScore);
2840 for (
auto &[TID,
Info] : VMem) {
2841 if (
auto It =
Other.VMem.find(TID); It !=
Other.VMem.end()) {
2842 unsigned char NewVmemTypes =
Info.VMEMTypes | It->second.VMEMTypes;
2843 StrictDom |= NewVmemTypes !=
Info.VMEMTypes;
2844 Info.VMEMTypes = NewVmemTypes;
2848 purgeEmptyTrackingData();
2854 return Opcode == AMDGPU::S_WAITCNT ||
2857 Opcode == AMDGPU::S_WAIT_LOADCNT_DSCNT ||
2858 Opcode == AMDGPU::S_WAIT_STORECNT_DSCNT ||
2859 Opcode == AMDGPU::S_WAITCNT_lds_direct ||
2863void SIInsertWaitcnts::setSchedulingMode(MachineBasicBlock &
MBB,
2865 bool ExpertMode)
const {
2869 .
addImm(ExpertMode ? 2 : 0)
2874bool SIInsertWaitcnts::insertWaitcntInBlock(MachineFunction &MF,
2875 MachineBasicBlock &
Block,
2876 WaitcntBrackets &ScoreBrackets) {
2880 dbgs() <<
"*** Begin Block: ";
2882 ScoreBrackets.dump();
2888 bool VCCZCorrect =
true;
2889 if (
ST->hasReadVCCZBug()) {
2892 VCCZCorrect =
false;
2893 }
else if (!
ST->partialVCCWritesUpdateVCCZ()) {
2896 VCCZCorrect =
false;
2900 MachineInstr *OldWaitcntInstr =
nullptr;
2905 MachineInstr &Inst = *Iter;
2914 (IsExpertMode && Inst.
getOpcode() == AMDGPU::S_WAITCNT_DEPCTR)) {
2915 if (!OldWaitcntInstr)
2916 OldWaitcntInstr = &Inst;
2921 bool FlushVmCnt =
Block.getFirstTerminator() == Inst &&
2922 isPreheaderToFlush(
Block, ScoreBrackets);
2925 Modified |= generateWaitcntInstBefore(Inst, ScoreBrackets, OldWaitcntInstr,
2927 OldWaitcntInstr =
nullptr;
2933 if (
ST->hasReadVCCZBug() || !
ST->partialVCCWritesUpdateVCCZ()) {
2937 if (!
ST->partialVCCWritesUpdateVCCZ())
2938 VCCZCorrect =
false;
2947 if (
ST->hasReadVCCZBug() &&
2948 ScoreBrackets.hasPendingEvent(SMEM_ACCESS)) {
2951 VCCZCorrect =
false;
2959 if (
TII->isSMRD(Inst)) {
2960 for (
const MachineMemOperand *Memop : Inst.
memoperands()) {
2963 if (!Memop->isInvariant()) {
2964 const Value *Ptr = Memop->getValue();
2968 if (
ST->hasReadVCCZBug()) {
2970 VCCZCorrect =
false;
2974 updateEventWaitcntAfter(Inst, &ScoreBrackets);
2976 Modified |= insertForcedWaitAfter(Inst,
Block, ScoreBrackets);
2980 ScoreBrackets.dump();
2990 TII->get(
ST->isWave32() ? AMDGPU::S_MOV_B32 : AMDGPU::S_MOV_B64),
3002 AMDGPU::Waitcnt
Wait;
3003 if (
Block.getFirstTerminator() ==
Block.end() &&
3004 isPreheaderToFlush(
Block, ScoreBrackets)) {
3005 if (ScoreBrackets.hasPendingEvent(LOAD_CNT))
3007 if (ScoreBrackets.hasPendingEvent(SAMPLE_CNT))
3009 if (ScoreBrackets.hasPendingEvent(BVH_CNT))
3018 dbgs() <<
"*** End Block: ";
3020 ScoreBrackets.dump();
3028bool SIInsertWaitcnts::isPreheaderToFlush(
3029 MachineBasicBlock &
MBB,
const WaitcntBrackets &ScoreBrackets) {
3030 auto [Iterator, IsInserted] = PreheadersToFlush.
try_emplace(&
MBB,
false);
3032 return Iterator->second;
3043 shouldFlushVmCnt(Loop, ScoreBrackets)) {
3044 Iterator->second =
true;
3051bool SIInsertWaitcnts::isVMEMOrFlatVMEM(
const MachineInstr &
MI)
const {
3053 return TII->mayAccessVMEMThroughFlat(
MI);
3065bool SIInsertWaitcnts::shouldFlushVmCnt(MachineLoop *
ML,
3066 const WaitcntBrackets &Brackets) {
3067 bool HasVMemLoad =
false;
3068 bool HasVMemStore =
false;
3069 bool UsesVgprLoadedOutside =
false;
3070 DenseSet<MCRegUnit> VgprUse;
3071 DenseSet<MCRegUnit> VgprDef;
3073 for (MachineBasicBlock *
MBB :
ML->blocks()) {
3074 for (MachineInstr &
MI : *
MBB) {
3075 if (isVMEMOrFlatVMEM(
MI)) {
3076 HasVMemLoad |=
MI.mayLoad();
3077 HasVMemStore |=
MI.mayStore();
3080 for (
const MachineOperand &
Op :
MI.all_uses()) {
3081 if (
Op.isDebug() || !
TRI->isVectorRegister(*
MRI,
Op.getReg()))
3084 for (MCRegUnit RU :
TRI->regunits(
Op.getReg().asMCReg())) {
3092 VMEMID
ID = toVMEMID(RU);
3093 if (Brackets.getVMemScore(
ID, LOAD_CNT) >
3094 Brackets.getScoreLB(LOAD_CNT) ||
3095 Brackets.getVMemScore(
ID, SAMPLE_CNT) >
3096 Brackets.getScoreLB(SAMPLE_CNT) ||
3097 Brackets.getVMemScore(
ID, BVH_CNT) >
3098 Brackets.getScoreLB(BVH_CNT)) {
3099 UsesVgprLoadedOutside =
true;
3106 if (isVMEMOrFlatVMEM(
MI) &&
MI.mayLoad()) {
3107 for (
const MachineOperand &
Op :
MI.all_defs()) {
3108 for (MCRegUnit RU :
TRI->regunits(
Op.getReg().asMCReg())) {
3119 if (!
ST->hasVscnt() && HasVMemStore && !HasVMemLoad && UsesVgprLoadedOutside)
3121 return HasVMemLoad && UsesVgprLoadedOutside &&
ST->hasVmemWriteVgprInOrder();
3124bool SIInsertWaitcntsLegacy::runOnMachineFunction(MachineFunction &MF) {
3125 auto *MLI = &getAnalysis<MachineLoopInfoWrapperPass>().getLI();
3127 &getAnalysis<MachinePostDominatorTreeWrapperPass>().getPostDomTree();
3129 if (
auto *AAR = getAnalysisIfAvailable<AAResultsWrapperPass>())
3130 AA = &AAR->getAAResults();
3132 return SIInsertWaitcnts(MLI, PDT, AA).run(MF);
3144 if (!SIInsertWaitcnts(MLI, PDT,
AA).
run(MF))
3149 .preserve<AAManager>();
3154 TII = ST->getInstrInfo();
3155 TRI = &
TII->getRegisterInfo();
3164 if (ST->hasExtendedWaitCounts()) {
3165 IsExpertMode = ST->hasExpertSchedulingMode() &&
3171 MaxCounter = IsExpertMode ? NUM_EXPERT_INST_CNTS : NUM_EXTENDED_INST_CNTS;
3173 WaitcntGeneratorGFX12Plus(MF, MaxCounter, &Limits, IsExpertMode);
3174 WCG = &WCGGFX12Plus;
3176 MaxCounter = NUM_NORMAL_INST_CNTS;
3177 WCGPreGFX12 = WaitcntGeneratorPreGFX12(MF, &Limits);
3181 for (
auto T : inst_counter_types())
3182 ForceEmitWaitcnt[
T] =
false;
3184 WaitEventMaskForInst = WCG->getWaitEventMask();
3186 SmemAccessCounter = eventCounter(WaitEventMaskForInst, SMEM_ACCESS);
3191 MachineBasicBlock &EntryBB = MF.
front();
3201 while (
I != EntryBB.
end() &&
I->isMetaInstruction())
3204 if (
ST->hasExtendedWaitCounts()) {
3207 for (
auto CT : inst_counter_types(NUM_EXTENDED_INST_CNTS)) {
3208 if (CT == LOAD_CNT || CT == DS_CNT || CT == STORE_CNT || CT == X_CNT)
3211 if (!
ST->hasImageInsts() &&
3212 (CT == EXP_CNT || CT == SAMPLE_CNT || CT == BVH_CNT))
3216 TII->get(instrsForExtendedCounterTypes[CT]))
3229 auto NonKernelInitialState = std::make_unique<WaitcntBrackets>(
this);
3230 NonKernelInitialState->setStateOnFunctionEntryOrReturn();
3231 BlockInfos[&EntryBB].Incoming = std::move(NonKernelInitialState);
3238 for (
auto *
MBB : ReversePostOrderTraversal<MachineFunction *>(&MF))
3241 std::unique_ptr<WaitcntBrackets> Brackets;
3246 for (
auto BII = BlockInfos.
begin(), BIE = BlockInfos.
end(); BII != BIE;
3248 MachineBasicBlock *
MBB = BII->first;
3249 BlockInfo &BI = BII->second;
3255 Brackets = std::make_unique<WaitcntBrackets>(*BI.Incoming);
3257 *Brackets = *BI.Incoming;
3260 Brackets = std::make_unique<WaitcntBrackets>(
this);
3262 *Brackets = WaitcntBrackets(
this);
3265 Modified |= insertWaitcntInBlock(MF, *
MBB, *Brackets);
3268 if (Brackets->hasPendingEvent()) {
3269 BlockInfo *MoveBracketsToSucc =
nullptr;
3271 auto *SuccBII = BlockInfos.
find(Succ);
3272 BlockInfo &SuccBI = SuccBII->second;
3273 if (!SuccBI.Incoming) {
3274 SuccBI.Dirty =
true;
3275 if (SuccBII <= BII) {
3279 if (!MoveBracketsToSucc) {
3280 MoveBracketsToSucc = &SuccBI;
3282 SuccBI.Incoming = std::make_unique<WaitcntBrackets>(*Brackets);
3284 }
else if (SuccBI.Incoming->merge(*Brackets)) {
3285 SuccBI.Dirty =
true;
3286 if (SuccBII <= BII) {
3292 if (MoveBracketsToSucc)
3293 MoveBracketsToSucc->Incoming = std::move(Brackets);
3298 if (
ST->hasScalarStores()) {
3299 SmallVector<MachineBasicBlock *, 4> EndPgmBlocks;
3300 bool HaveScalarStores =
false;
3302 for (MachineBasicBlock &
MBB : MF) {
3303 for (MachineInstr &
MI :
MBB) {
3304 if (!HaveScalarStores &&
TII->isScalarStore(
MI))
3305 HaveScalarStores =
true;
3307 if (
MI.getOpcode() == AMDGPU::S_ENDPGM ||
3308 MI.getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG)
3313 if (HaveScalarStores) {
3322 for (MachineBasicBlock *
MBB : EndPgmBlocks) {
3323 bool SeenDCacheWB =
false;
3327 if (
I->getOpcode() == AMDGPU::S_DCACHE_WB)
3328 SeenDCacheWB =
true;
3329 else if (
TII->isScalarStore(*
I))
3330 SeenDCacheWB =
false;
3333 if ((
I->getOpcode() == AMDGPU::S_ENDPGM ||
3334 I->getOpcode() == AMDGPU::SI_RETURN_TO_EPILOG) &&
3350 while (
I != EntryBB.
end() &&
I->isMetaInstruction())
3352 setSchedulingMode(EntryBB,
I,
true);
3354 for (MachineInstr *
MI : CallInsts) {
3355 MachineBasicBlock &
MBB = *
MI->getParent();
3356 setSchedulingMode(
MBB,
MI,
false);
3357 setSchedulingMode(
MBB, std::next(
MI->getIterator()),
true);
3360 for (MachineInstr *
MI : ReturnInsts)
3361 setSchedulingMode(*
MI->getParent(),
MI,
false);
3372 for (MachineInstr *
MI : ReleaseVGPRInsts) {
3374 TII->get(AMDGPU::S_ALLOC_VGPR))
3379 if (!ReleaseVGPRInsts.empty() &&
3380 (MF.getFrameInfo().hasCalls() ||
3381 ST->getOccupancyWithNumVGPRs(
3382 TRI->getNumUsedPhysRegs(*
MRI, AMDGPU::VGPR_32RegClass),
3385 for (MachineInstr *
MI : ReleaseVGPRInsts) {
3386 if (
ST->requiresNopBeforeDeallocVGPRs()) {
3388 TII->get(AMDGPU::S_NOP))
3392 TII->get(AMDGPU::S_SENDMSG))
3400 ReturnInsts.
clear();
3401 ReleaseVGPRInsts.clear();
3402 PreheadersToFlush.
clear();
3403 SLoadAddresses.
clear();
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
Provides AMDGPU specific target descriptions.
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Analysis containing CSE Info
This file provides an implementation of debug counters.
#define DEBUG_COUNTER(VARNAME, COUNTERNAME, DESC)
AMD GCN specific subclass of TargetSubtarget.
const HexagonInstrInfo * TII
static bool isOptNone(const MachineFunction &MF)
static LoopDeletionResult merge(LoopDeletionResult A, LoopDeletionResult B)
Register const TargetRegisterInfo * TRI
This file implements a map that provides insertion order iteration.
static bool isReg(const MCInst &MI, unsigned OpNo)
MachineInstr unsigned OpIdx
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
This file builds on the ADT/GraphTraits.h file to build a generic graph post order iterator.
static cl::opt< bool > ForceEmitZeroLoadFlag("amdgpu-waitcnt-load-forcezero", cl::desc("Force all waitcnt load counters to wait until 0"), cl::init(false), cl::Hidden)
#define AMDGPU_EVENT_NAME(Name)
static bool updateOperandIfDifferent(MachineInstr &MI, AMDGPU::OpName OpName, unsigned NewEnc)
static bool isWaitInstr(MachineInstr &Inst)
static std::optional< InstCounterType > counterTypeForInstr(unsigned Opcode)
Determine if MI is a gfx12+ single-counter S_WAIT_*CNT instruction, and if so, which counter it is wa...
static cl::opt< bool > ExpertSchedulingModeFlag("amdgpu-expert-scheduling-mode", cl::desc("Enable expert scheduling mode 2 for all functions (GFX12+ only)"), cl::init(false), cl::Hidden)
static cl::opt< bool > ForceEmitZeroFlag("amdgpu-waitcnt-forcezero", cl::desc("Force all waitcnt instrs to be emitted as " "s_waitcnt vmcnt(0) expcnt(0) lgkmcnt(0)"), cl::init(false), cl::Hidden)
#define AMDGPU_DECLARE_WAIT_EVENTS(DECL)
#define AMDGPU_EVENT_ENUM(Name)
Provides some synthesis utilities to produce sequences of values.
static Function * getFunction(FunctionType *Ty, const Twine &Name, Module *M)
static const uint32_t IV[8]
A manager for alias analyses.
bool isEntryFunction() const
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addUsedIfAvailable()
Add the specified Pass class to the set of analyses used by this pass.
AnalysisUsage & addRequired()
AnalysisUsage & addPreserved()
Add the specified Pass class to the set of analyses preserved by this pass.
LLVM_ABI void setPreservesCFG()
This function should be called by the pass, iff they do not:
LLVM_ABI bool getValueAsBool() const
Return the attribute's value as a boolean.
Represents analyses that only rely on functions' control flow.
static bool shouldExecute(CounterInfo &Counter)
static bool isCounterSet(CounterInfo &Info)
iterator find(const_arg_type_t< KeyT > Val)
std::pair< iterator, bool > try_emplace(KeyT &&Key, Ts &&...Args)
bool erase(const KeyT &Val)
std::pair< iterator, bool > insert(const std::pair< KeyT, ValueT > &KV)
bool dominates(const DomTreeNodeBase< NodeT > *A, const DomTreeNodeBase< NodeT > *B) const
dominates - Returns true iff A dominates B.
FunctionPass class - This class is used to implement most global optimizations.
Attribute getFnAttribute(Attribute::AttrKind Kind) const
Return the attribute for the given attribute kind.
BlockT * getLoopPreheader() const
If there is a preheader for this loop, return it.
LoopT * getLoopFor(const BlockT *BB) const
Return the inner most loop that BB lives in.
LLVM_ABI const MachineBasicBlock * getSingleSuccessor() const
Return the successor of this block if it has a single successor.
LLVM_ABI DebugLoc findDebugLoc(instr_iterator MBBI)
Find the next valid DebugLoc starting at MBBI, skipping any debug instructions.
Instructions::iterator instr_iterator
iterator_range< succ_iterator > successors()
MachineInstrBundleIterator< MachineInstr > iterator
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.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineBasicBlock & front() const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
mop_range defs()
Returns all explicit operands that are register definitions.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
bool mayLoadOrStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read or modify memory.
const MachineBasicBlock * getParent() const
filtered_mop_range all_defs()
Returns an iterator range over all operands that are (explicit or implicit) register defs.
bool isCall(QueryType Type=AnyInBundle) const
bool mayLoad(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly read memory.
bool definesRegister(Register Reg, const TargetRegisterInfo *TRI) const
Return true if the MachineInstr fully defines the specified register.
LLVM_ABI void setDesc(const MCInstrDesc &TID)
Replace the instruction descriptor (thus opcode) of the current instruction with a new one.
ArrayRef< MachineMemOperand * > memoperands() const
Access to memory operands of the instruction.
LLVM_ABI void print(raw_ostream &OS, bool IsStandalone=true, bool SkipOpers=false, bool SkipDebugLoc=false, bool AddNewLine=true, const TargetInstrInfo *TII=nullptr) const
Print this MI to OS.
bool mayStore(QueryType Type=AnyInBundle) const
Return true if this instruction could possibly modify memory.
const DebugLoc & getDebugLoc() const
Returns the debug location id of this MachineInstr.
LLVM_ABI void eraseFromParent()
Unlink 'this' from the containing basic block and delete it.
filtered_mop_range all_uses()
Returns an iterator range over all operands that are (explicit or implicit) register uses.
const MachineOperand & getOperand(unsigned i) const
bool isMetaInstruction(QueryType Type=IgnoreBundle) const
Return true if this instruction doesn't produce any output in the form of executable instructions.
Analysis pass that exposes the MachineLoopInfo for a machine function.
MachineOperand class - Representation of each machine instruction operand.
void setImm(int64_t immVal)
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
iterator find(const KeyT &Key)
std::pair< iterator, bool > try_emplace(const KeyT &Key, Ts &&...Args)
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
MCRegister asMCReg() const
Utility to check-convert this value to a MCRegister.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
static bool isCBranchVCCZRead(const MachineInstr &MI)
static bool isVMEM(const MachineInstr &MI)
static bool isFLATScratch(const MachineInstr &MI)
static bool isEXP(const MachineInstr &MI)
static bool mayWriteLDSThroughDMA(const MachineInstr &MI)
static bool isLDSDIR(const MachineInstr &MI)
static bool isGWS(const MachineInstr &MI)
static bool isFLATGlobal(const MachineInstr &MI)
static bool isVSAMPLE(const MachineInstr &MI)
static bool isAtomicRet(const MachineInstr &MI)
static bool isImage(const MachineInstr &MI)
static unsigned getNonSoftWaitcntOpcode(unsigned Opcode)
static bool isVINTERP(const MachineInstr &MI)
static bool isGFX12CacheInvOrWBInst(unsigned Opc)
static bool isSBarrierSCCWrite(unsigned Opcode)
static bool isMIMG(const MachineInstr &MI)
static bool isFLAT(const MachineInstr &MI)
static bool isLDSDMA(const MachineInstr &MI)
static bool isAtomicNoRet(const MachineInstr &MI)
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
bool isDynamicVGPREnabled() const
void push_back(const T &Elt)
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
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.
self_iterator getIterator()
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
@ LOCAL_ADDRESS
Address space for local memory.
@ FLAT_ADDRESS
Address space for flat memory.
unsigned encodeFieldVaVdst(unsigned Encoded, unsigned VaVdst)
unsigned encodeFieldVmVsrc(unsigned Encoded, unsigned VmVsrc)
unsigned decodeFieldVaVdst(unsigned Encoded)
int getDefaultDepCtrEncoding(const MCSubtargetInfo &STI)
unsigned decodeFieldVmVsrc(unsigned Encoded)
unsigned getMaxWavesPerEU(const MCSubtargetInfo *STI)
@ ID_DEALLOC_VGPRS_GFX11Plus
LLVM_READONLY const MIMGInfo * getMIMGInfo(unsigned Opc)
void decodeWaitcnt(const IsaVersion &Version, unsigned Waitcnt, unsigned &Vmcnt, unsigned &Expcnt, unsigned &Lgkmcnt)
Decodes Vmcnt, Expcnt and Lgkmcnt from given Waitcnt for given isa Version, and writes decoded values...
bool isDPMACCInstruction(unsigned Opc)
LLVM_ABI IsaVersion getIsaVersion(StringRef GPU)
unsigned encodeWaitcnt(const IsaVersion &Version, unsigned Vmcnt, unsigned Expcnt, unsigned Lgkmcnt)
Encodes Vmcnt, Expcnt and Lgkmcnt into Waitcnt for given isa Version.
Waitcnt decodeStorecntDscnt(const IsaVersion &Version, unsigned StorecntDscnt)
Waitcnt decodeLoadcntDscnt(const IsaVersion &Version, unsigned LoadcntDscnt)
static unsigned encodeStorecntDscnt(const IsaVersion &Version, unsigned Storecnt, unsigned Dscnt)
bool getMUBUFIsBufferInv(unsigned Opc)
LLVM_READONLY const MIMGBaseOpcodeInfo * getMIMGBaseOpcodeInfo(unsigned BaseOpcode)
static unsigned encodeLoadcntDscnt(const IsaVersion &Version, unsigned Loadcnt, unsigned Dscnt)
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.
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
@ Undef
Value of the register doesn't matter.
initializer< Ty > init(const Ty &Val)
PointerTypeMap run(const Module &M)
Compute the PointerTypeMap for the module M.
This is an optimization pass for GlobalISel generic memory operations.
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
FunctionAddr VTableAddr Value
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Printable print(const GCNRegPressure &RP, const GCNSubtarget *ST=nullptr, unsigned DynamicVGPRBlockSize=0)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
auto enum_seq(EnumT Begin, EnumT End)
Iterate over an enum type from Begin up to - but not including - End.
static StringRef getCPU(StringRef CPU)
Processes a CPU name.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
iterator_range< early_inc_iterator_impl< detail::IterOfRange< RangeT > > > make_early_inc_range(RangeT &&Range)
Make a range that does early increment to allow mutation of the underlying range without disrupting i...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
char & SIInsertWaitcntsID
void sort(IteratorTy Start, IteratorTy End)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
FunctionAddr VTableAddr Count
CodeGenOptLevel
Code generation optimization level.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
DWARFExpression::Operation Op
FunctionPass * createSIInsertWaitcntsPass()
AAResults AliasAnalysis
Temporary typedef for legacy code that uses a generic AliasAnalysis pointer or reference.
static constexpr ValueType Default
static constexpr uint64_t encode(Fields... Values)
Represents the hardware counter limits for different wait count types.
Instruction set architecture version.
Represents the counter values to wait for in an s_waitcnt instruction.
static constexpr bool is_iterable