52 cl::desc(
"Verify stall cycles in the regbanks reassign pass"),
56 #define DEBUG_TYPE "amdgpu-regbanks-reassign" 58 #define NUM_VGPR_BANKS 4 59 #define NUM_SGPR_BANKS 8 60 #define NUM_BANKS (NUM_VGPR_BANKS + NUM_SGPR_BANKS) 61 #define SGPR_BANK_OFFSET NUM_VGPR_BANKS 62 #define VGPR_BANK_MASK 0xf 63 #define SGPR_BANK_MASK 0xff0 64 #define SGPR_BANK_SHIFTED_MASK (SGPR_BANK_MASK >> SGPR_BANK_OFFSET) 67 "Number of operand read stalls detected");
69 "Number of operand read stalls recovered");
77 OperandMask(
unsigned r,
unsigned s,
unsigned m)
86 Candidate(
MachineInstr *mi,
unsigned reg,
unsigned freebanks,
88 :
MI(mi),
Reg(reg), FreeBanks(freebanks), Weight(weight) {}
90 bool operator< (
const Candidate& RHS)
const {
return Weight < RHS.Weight; }
92 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 93 void dump(
const GCNRegBankReassign *
P)
const {
95 dbgs() << P->printReg(
Reg) <<
" to banks ";
96 dumpFreeBanks(FreeBanks);
97 dbgs() <<
" weight " << Weight <<
'\n';
107 class CandidateList :
public std::list<Candidate> {
110 void push(
const Candidate&&
C) {
111 if (
C.Weight) push_back(
C);
126 StringRef getPassName()
const override {
return "GCN RegBank Reassign"; }
152 unsigned MaxNumVGPRs;
154 unsigned MaxNumSGPRs;
160 CandidateList Candidates;
165 unsigned getPhysRegBank(
unsigned Reg)
const;
171 unsigned getRegBankMask(
unsigned Reg,
unsigned SubReg,
int Bank);
177 unsigned Reg = AMDGPU::NoRegister,
int Bank = -1);
181 bool isReassignable(
unsigned Reg)
const;
189 unsigned StallCycles)
const;
193 unsigned getFreeBanks(
unsigned Mask,
unsigned UsedBanks)
const;
198 unsigned getFreeBanks(
unsigned Reg,
unsigned SubReg,
unsigned Mask,
199 unsigned UsedBanks)
const;
203 unsigned StallCycles);
210 void removeCandidates(
unsigned Reg);
215 unsigned computeStallCycles(
unsigned SrcReg,
216 unsigned Reg = AMDGPU::NoRegister,
217 int Bank = -1,
bool Collect =
false);
221 unsigned scavengeReg(
LiveInterval& LI,
unsigned Bank)
const;
224 unsigned tryReassign(Candidate &
C);
227 unsigned OriginalCycles,
unsigned CyclesSaved);
230 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 244 OS <<
':' << TRI->getSubRegIndexName(
SubReg);
248 static Printable printBank(
unsigned Bank) {
254 static void dumpFreeBanks(
unsigned FreeBanks) {
256 if (FreeBanks & (1 << L))
257 dbgs() << printBank(L) <<
' ';
274 char GCNRegBankReassign::ID = 0;
278 unsigned GCNRegBankReassign::getPhysRegBank(
unsigned Reg)
const {
282 unsigned Size = TRI->getRegSizeInBits(*RC);
284 Reg = TRI->getSubReg(Reg, AMDGPU::sub0);
287 Reg -= AMDGPU::VGPR0;
291 Reg = TRI->getEncodingValue(Reg) / 2;
295 unsigned GCNRegBankReassign::getRegBankMask(
unsigned Reg,
unsigned SubReg,
305 Reg = TRI->getSubReg(Reg, SubReg);
309 unsigned Size = TRI->getRegSizeInBits(*RC) / 32;
311 Reg = TRI->getSubReg(Reg, AMDGPU::sub0);
315 Reg -= AMDGPU::VGPR0;
319 for (
unsigned I = 0;
I <
Size; ++
I)
320 if (RegsUsed.
test(Reg +
I))
322 RegsUsed.
set(Reg, Reg + Size);
329 Reg = TRI->getEncodingValue(Reg) / 2;
330 unsigned StartBit = AMDGPU::VGPR_32RegClass.getNumRegs();
331 if (Reg + StartBit >= RegsUsed.
size())
338 for (
unsigned I = 0;
I <
Size; ++
I)
339 if (RegsUsed.
test(StartBit + Reg +
I))
341 RegsUsed.
set(StartBit + Reg, StartBit + Reg + Size);
354 unsigned StallCycles = 0;
361 OperandMasks.
clear();
365 if (!
Op.isReg() ||
Op.isUndef())
372 unsigned ShiftedBank = Bank;
374 if (Bank != -1 && R == Reg &&
Op.getSubReg()) {
375 unsigned LM = TRI->getSubRegIndexLaneMask(
Op.getSubReg()).getAsInteger();
391 unsigned Mask = getRegBankMask(R,
Op.getSubReg(),
392 (Reg == R) ? ShiftedBank : -1);
401 unsigned GCNRegBankReassign::getOperandGatherWeight(
const MachineInstr& MI,
404 unsigned StallCycles)
const 409 for (
unsigned S = StallCycles; S &&
Def !=
B && Defs != 3; --S) {
413 if (
Def->getOpcode() == TargetOpcode::IMPLICIT_DEF)
415 if (
Def->modifiesRegister(Reg1, TRI))
417 if (
Def->modifiesRegister(Reg2, TRI))
423 bool GCNRegBankReassign::isReassignable(
unsigned Reg)
const {
446 unsigned Size = TRI->getRegSizeInBits(*RC);
448 PhysReg = TRI->getSubReg(PhysReg, AMDGPU::sub0);
450 return AMDGPU::SGPR_32RegClass.contains(PhysReg);
453 unsigned GCNRegBankReassign::getFreeBanks(
unsigned Mask,
454 unsigned UsedBanks)
const {
456 unsigned FreeBanks = 0;
466 unsigned NewMask = ((1 <<
Size) - 1) <<
I;
468 if (!(UsedBanks & NewMask))
481 unsigned NewMask = ((1 <<
Size) - 1) <<
I;
490 unsigned GCNRegBankReassign::getFreeBanks(
unsigned Reg,
493 unsigned UsedBanks)
const {
494 if (!isReassignable(Reg))
497 unsigned FreeBanks = getFreeBanks(Mask, UsedBanks);
499 unsigned LM = TRI->getSubRegIndexLaneMask(SubReg).getAsInteger();
518 dbgs() <<
"Potential reassignments of " <<
printReg(Reg, SubReg)
519 <<
" to banks: "; dumpFreeBanks(FreeBanks);
525 void GCNRegBankReassign::collectCandidates(
MachineInstr& MI,
527 unsigned StallCycles) {
535 for (
unsigned I = 0,
E = OperandMasks.
size();
I + 1 <
E; ++
I) {
536 for (
unsigned J =
I + 1; J !=
E; ++J) {
537 if (!(OperandMasks[
I].Mask & OperandMasks[J].Mask))
540 unsigned Reg1 = OperandMasks[
I].Reg;
541 unsigned Reg2 = OperandMasks[J].Reg;
542 unsigned SubReg1 = OperandMasks[
I].SubReg;
543 unsigned SubReg2 = OperandMasks[J].SubReg;
544 unsigned Mask1 = OperandMasks[
I].Mask;
545 unsigned Mask2 = OperandMasks[J].Mask;
550 " and " <<
printReg(Reg2, SubReg2) <<
'\n');
552 unsigned Weight = getOperandGatherWeight(MI, Reg1, Reg2, StallCycles);
557 unsigned FreeBanks1 = getFreeBanks(Reg1, SubReg1, Mask1, UsedBanks);
558 unsigned FreeBanks2 = getFreeBanks(Reg2, SubReg2, Mask2, UsedBanks);
560 Candidates.push(Candidate(&MI, Reg1, FreeBanks1, Weight
561 + ((Size2 > Size1) ? 1 : 0)));
563 Candidates.push(Candidate(&MI, Reg2, FreeBanks2, Weight
564 + ((Size1 > Size2) ? 1 : 0)));
569 unsigned GCNRegBankReassign::computeStallCycles(
unsigned SrcReg,
570 unsigned Reg,
int Bank,
572 unsigned TotalStallCycles = 0;
573 unsigned UsedBanks = 0;
579 if (!Visited.
insert(&MI).second)
581 unsigned StallCycles = analyzeInst(MI, UsedBanks, Reg, Bank);
582 TotalStallCycles += StallCycles;
584 collectCandidates(MI, UsedBanks, StallCycles);
587 return TotalStallCycles;
590 unsigned GCNRegBankReassign::scavengeReg(
LiveInterval& LI,
591 unsigned Bank)
const {
595 unsigned MaxReg = MaxNumRegs + (Bank <
NUM_VGPR_BANKS ? AMDGPU::VGPR0
600 if (TRI->isSubRegisterEq(Reg, MaxReg))
606 for (
unsigned I = 0; CSRegs[
I]; ++
I)
607 if (TRI->isSubRegisterEq(Reg, CSRegs[
I]) &&
609 return AMDGPU::NoRegister;
617 return AMDGPU::NoRegister;
620 unsigned GCNRegBankReassign::tryReassign(Candidate &
C) {
632 unsigned OrigStalls = computeStallCycles(C.Reg);
633 LLVM_DEBUG(
dbgs() <<
"--- Stall cycles in range = " << OrigStalls <<
'\n');
638 BankStall(
unsigned b,
unsigned s) : Bank(b), Stalls(s) {};
639 bool operator< (
const BankStall &RHS)
const {
return Stalls > RHS.Stalls; }
645 for (
int Bank = 0; Bank <
NUM_BANKS; ++Bank) {
646 if (C.FreeBanks & (1 << Bank)) {
648 unsigned Stalls = computeStallCycles(C.Reg, C.Reg, Bank);
649 if (Stalls < OrigStalls) {
652 BankStalls.
push_back(BankStall((
unsigned)Bank, Stalls));
660 while (!BankStalls.
empty()) {
662 unsigned Reg = scavengeReg(LI, BS.Bank);
663 if (Reg == AMDGPU::NoRegister) {
664 LLVM_DEBUG(
dbgs() <<
"No free registers in bank " << printBank(BS.Bank)
670 <<
" in bank " << printBank(BS.Bank) <<
'\n');
674 LLVM_DEBUG(
dbgs() <<
"--- Cycles saved: " << OrigStalls - BS.Stalls <<
'\n');
676 return OrigStalls - BS.Stalls;
685 unsigned TotalStallCycles = 0;
690 if (MBB.getName().empty())
dbgs() <<
"bb." << MBB.getNumber();
691 else dbgs() << MBB.getName();
dbgs() <<
":\n";
698 unsigned UsedBanks = 0;
699 unsigned StallCycles = analyzeInst(MI, UsedBanks);
702 collectCandidates(MI, UsedBanks, StallCycles);
704 TotalStallCycles += StallCycles;
710 return TotalStallCycles;
713 void GCNRegBankReassign::removeCandidates(
unsigned Reg) {
714 Candidates.remove_if([Reg,
this](
const Candidate& C) {
715 return C.MI->readsRegister(Reg, TRI);
720 unsigned OriginalCycles,
721 unsigned CyclesSaved) {
722 unsigned StallCycles = collectCandidates(MF,
false);
724 <<
" stall cycles left\n");
725 return StallCycles + CyclesSaved == OriginalCycles;
735 MLI = &getAnalysis<MachineLoopInfo>();
736 VRM = &getAnalysis<VirtRegMap>();
737 LRM = &getAnalysis<LiveRegMatrix>();
738 LIS = &getAnalysis<LiveIntervals>();
744 MaxNumVGPRs = std::min(ST->
getMaxNumVGPRs(Occupancy), MaxNumVGPRs);
745 MaxNumSGPRs = std::min(ST->
getMaxNumSGPRs(Occupancy,
true), MaxNumSGPRs);
749 RegsUsed.
resize(AMDGPU::VGPR_32RegClass.getNumRegs() +
750 TRI->getEncodingValue(AMDGPU::SGPR_NULL) / 2 + 1);
755 unsigned StallCycles = collectCandidates(MF);
756 NumStallsDetected += StallCycles;
758 LLVM_DEBUG(
dbgs() <<
"=== " << StallCycles <<
" stall cycles detected in " 759 "function " << MF.
getName() <<
'\n');
764 for (
auto C : Candidates) C.dump(
this);
767 unsigned CyclesSaved = 0;
768 while (!Candidates.empty()) {
769 Candidate C = Candidates.back();
770 unsigned LocalCyclesSaved = tryReassign(C);
771 CyclesSaved += LocalCyclesSaved;
776 Candidates.pop_back();
777 if (LocalCyclesSaved) {
778 removeCandidates(C.Reg);
779 computeStallCycles(C.Reg, AMDGPU::NoRegister, -1,
true);
783 for (
auto C : Candidates)
788 NumStallsRecovered += CyclesSaved;
791 <<
" cycles saved in function " << MF.
getName() <<
'\n');
800 return CyclesSaved > 0;
void resize(unsigned N, bool t=false)
resize - Grow or shrink the bitvector.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
instr_iterator instr_begin()
AMDGPU specific subclass of TargetSubtarget.
bool isAllocatable(unsigned PhysReg) const
isAllocatable - Returns true when PhysReg belongs to an allocatable register class and it hasn't been...
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
InterferenceKind checkInterference(LiveInterval &VirtReg, unsigned PhysReg)
Check for interference before assigning VirtReg to PhysReg.
INITIALIZE_PASS_BEGIN(GCNRegBankReassign, DEBUG_TYPE, "GCN RegBank Reassign", false, false) INITIALIZE_PASS_END(GCNRegBankReassign
void push_back(const T &Elt)
static bool isPhysicalRegister(unsigned Reg)
Return true if the specified register number is in the physical register namespace.
LiveInterval - This class represents the liveness of a register, or stack slot.
iterator_range< use_nodbg_iterator > use_nodbg_operands(unsigned Reg) const
bool test(unsigned Idx) const
unsigned getLoopDepth(const MachineBasicBlock *BB) const
Return the loop nesting level of the specified block.
STATISTIC(NumFunctions, "Total number of functions")
unsigned const TargetRegisterInfo * TRI
Printable printReg(Register Reg, const TargetRegisterInfo *TRI=nullptr, unsigned SubIdx=0, const MachineRegisterInfo *MRI=nullptr)
Prints virtual and physical registers with or without a TRI instance.
const TargetRegisterClass * getRegClass(Register Reg) const
Return the register class of the specified virtual register.
unsigned countTrailingZeros(T Val, ZeroBehavior ZB=ZB_Width)
Count number of 0's from the least significant bit to the most stopping at the first 1...
AnalysisUsage & addRequired()
#define INITIALIZE_PASS_DEPENDENCY(depName)
void clear()
clear - Removes all bits from the bitvector. Does not change capacity.
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
bool isPhysRegUsed(unsigned PhysReg) const
Returns true if the given PhysReg has any live intervals assigned.
bool hasInterval(Register Reg) const
void assign(LiveInterval &VirtReg, unsigned PhysReg)
Assign VirtReg to PhysReg.
#define SGPR_BANK_SHIFTED_MASK
bool isAssignedReg(Register virtReg) const
returns true if the specified virtual register is not mapped to a stack slot or rematerialized.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
uint16_t MCPhysReg
An unsigned integer type large enough to represent all physical registers, but not necessarily virtua...
unsigned getMaxNumSGPRs(unsigned WavesPerEU, bool Addressable) const
initializer< Ty > init(const Ty &Val)
iterator_range< SmallVectorImpl< MCPhysReg >::const_iterator > getRegisters() const
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
void dump(const SparseBitVector< ElementSize > &LHS, raw_ostream &out)
unsigned const MachineRegisterInfo * MRI
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
static cl::opt< unsigned > VerifyStallCycles("amdgpu-verify-regbanks-reassign", cl::desc("Verify stall cycles in the regbanks reassign pass"), cl::value_desc("0|1|2"), cl::init(0), cl::Hidden)
unsigned getOccupancy() const
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
bool hasVGPRs(const TargetRegisterClass *RC) const
Represent the analysis usage information of a pass.
bool hasRegisterBanking() const
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
self_iterator getIterator()
LiveInterval & getInterval(Register Reg)
std::pair< NoneType, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
void unassign(LiveInterval &VirtReg)
Unassign VirtReg from its PhysReg.
T findFirstSet(T Val, ZeroBehavior ZB=ZB_Max)
Get the index of the first set bit starting from the least significant bit.
bool isDebugInstr() const
INITIALIZE_PASS_END(RegBankSelect, DEBUG_TYPE, "Assign register bank of generic virtual registers", false, false) RegBankSelect
iterator_range< mop_iterator > explicit_uses()
void sort(IteratorTy Start, IteratorTy End)
void initializeGCNRegBankReassignPass(PassRegistry &)
Iterator for intrusive lists based on ilist_node.
unsigned countPopulation(T Value)
Count the number of set bits in a value.
bool hasAGPRs(const TargetRegisterClass *RC) const
bool isDebugValue() const
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
LLVM_NODISCARD T pop_back_val()
MachineInstr * getUniqueVRegDef(unsigned Reg) const
getUniqueVRegDef - Return the unique machine instr that defines the specified virtual register or nul...
const Function & getFunction() const
Return the LLVM function that this machine code represents.
const TargetRegisterClass * getRegClassForReg(const MachineRegisterInfo &MRI, unsigned Reg) const
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
void setPreservesAll()
Set by analyses that do not transform their input at all.
const MachineBasicBlock * getParent() const
MachineRegisterInfo - Keep track of information for virtual and physical registers, including vreg register classes, use/def chains for registers, etc.
Provides AMDGPU specific target descriptions.
Representation of each machine instruction.
This class keeps track of the SPI_SP_INPUT_ADDR config register, which tells the hardware which inter...
Interface definition for SIInstrInfo.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Register getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
LLVM_NODISCARD bool empty() const
size_type size() const
size - Returns the number of bits in this bitvector.
char & GCNRegBankReassignID
unsigned getMaxNumVGPRs(unsigned WavesPerEU) const
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool operator<(int64_t V1, const APSInt &V2)
static bool isVirtualRegister(unsigned Reg)
Return true if the specified register number is in the virtual register namespace.
const MCPhysReg * getCalleeSavedRegs() const
Returns list of callee saved registers.
std::underlying_type< E >::type Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
This class implements an extremely fast bulk output stream that can only output to a stream...
StringRef - Represent a constant reference to a string, i.e.
Register getReg() const
getReg - Returns the register number.
Simple wrapper around std::function<void(raw_ostream&)>.
const MachineOperand & getOperand(unsigned i) const
iterator_range< use_instr_nodbg_iterator > use_nodbg_instructions(unsigned Reg) const
Wrapper class representing virtual and physical registers.
const SIRegisterInfo * getRegisterInfo() const override