Skip to content

Commit 1f20cb9

Browse files
authored
[AMDGPU] Simplify GCNRegPressure::RegKind (NFC) (#142682)
This NFC simplifies the `GCNRegPressure::RegKind` enum so that instead of containing a pair of values for each type of register (one for non-tuple registers and one for tuple registers of that type) it only contains one value representing all registers of that type. The `GCNRegPressure::Value` array is still sized as before, though all elements corresponding to tuple-kinds now start after all elements corresponding to non-tuple-kinds instead of the two being interleaved. This allows to simplify the `GCNRegPressure::inc` logic, eliminating the switch entirely.
1 parent 89e06f8 commit 1f20cb9

File tree

2 files changed

+40
-60
lines changed

2 files changed

+40
-60
lines changed

llvm/lib/Target/AMDGPU/GCNRegPressure.cpp

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,9 @@ bool llvm::isEqual(const GCNRPTracker::LiveRegSet &S1,
3535
///////////////////////////////////////////////////////////////////////////////
3636
// GCNRegPressure
3737

38-
unsigned GCNRegPressure::getRegKind(Register Reg,
39-
const MachineRegisterInfo &MRI) {
40-
assert(Reg.isVirtual());
41-
const auto *const RC = MRI.getRegClass(Reg);
42-
const auto *STI =
43-
static_cast<const SIRegisterInfo *>(MRI.getTargetRegisterInfo());
44-
return STI->isSGPRClass(RC)
45-
? (STI->getRegSizeInBits(*RC) == 32 ? SGPR32 : SGPR_TUPLE)
46-
: STI->isAGPRClass(RC)
47-
? (STI->getRegSizeInBits(*RC) == 32 ? AGPR32 : AGPR_TUPLE)
48-
: (STI->getRegSizeInBits(*RC) == 32 ? VGPR32 : VGPR_TUPLE);
38+
unsigned GCNRegPressure::getRegKind(const TargetRegisterClass *RC,
39+
const SIRegisterInfo *STI) {
40+
return STI->isSGPRClass(RC) ? SGPR : (STI->isAGPRClass(RC) ? AGPR : VGPR);
4941
}
5042

5143
void GCNRegPressure::inc(unsigned Reg,
@@ -61,32 +53,22 @@ void GCNRegPressure::inc(unsigned Reg,
6153
std::swap(NewMask, PrevMask);
6254
Sign = -1;
6355
}
56+
assert(PrevMask < NewMask && "prev mask should always be lesser than new");
6457

65-
switch (auto Kind = getRegKind(Reg, MRI)) {
66-
case SGPR32:
67-
case VGPR32:
68-
case AGPR32:
69-
Value[Kind] += Sign;
70-
break;
71-
72-
case SGPR_TUPLE:
73-
case VGPR_TUPLE:
74-
case AGPR_TUPLE:
75-
assert(PrevMask < NewMask);
76-
77-
Value[Kind == SGPR_TUPLE ? SGPR32 : Kind == AGPR_TUPLE ? AGPR32 : VGPR32] +=
78-
Sign * SIRegisterInfo::getNumCoveredRegs(~PrevMask & NewMask);
79-
58+
const TargetRegisterClass *RC = MRI.getRegClass(Reg);
59+
const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
60+
const SIRegisterInfo *STI = static_cast<const SIRegisterInfo *>(TRI);
61+
unsigned RegKind = getRegKind(RC, STI);
62+
if (TRI->getRegSizeInBits(*RC) != 32) {
63+
// Reg is from a tuple register class.
8064
if (PrevMask.none()) {
81-
assert(NewMask.any());
82-
const TargetRegisterInfo *TRI = MRI.getTargetRegisterInfo();
83-
Value[Kind] +=
84-
Sign * TRI->getRegClassWeight(MRI.getRegClass(Reg)).RegWeight;
65+
unsigned TupleIdx = TOTAL_KINDS + RegKind;
66+
Value[TupleIdx] += Sign * TRI->getRegClassWeight(RC).RegWeight;
8567
}
86-
break;
87-
88-
default: llvm_unreachable("Unknown register kind");
68+
// Pressure scales with number of new registers covered by the new mask.
69+
Sign *= SIRegisterInfo::getNumCoveredRegs(~PrevMask & NewMask);
8970
}
71+
Value[RegKind] += Sign;
9072
}
9173

9274
bool GCNRegPressure::less(const MachineFunction &MF, const GCNRegPressure &O,
@@ -226,7 +208,7 @@ bool GCNRegPressure::less(const MachineFunction &MF, const GCNRegPressure &O,
226208

227209
Printable llvm::print(const GCNRegPressure &RP, const GCNSubtarget *ST) {
228210
return Printable([&RP, ST](raw_ostream &OS) {
229-
OS << "VGPRs: " << RP.Value[GCNRegPressure::VGPR32] << ' '
211+
OS << "VGPRs: " << RP.getArchVGPRNum() << ' '
230212
<< "AGPRs: " << RP.getAGPRNum();
231213
if (ST)
232214
OS << "(O"

llvm/lib/Target/AMDGPU/GCNRegPressure.h

Lines changed: 24 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -29,34 +29,26 @@ class raw_ostream;
2929
class SlotIndex;
3030

3131
struct GCNRegPressure {
32-
enum RegKind {
33-
SGPR32,
34-
SGPR_TUPLE,
35-
VGPR32,
36-
VGPR_TUPLE,
37-
AGPR32,
38-
AGPR_TUPLE,
39-
TOTAL_KINDS
40-
};
32+
enum RegKind { SGPR, VGPR, AGPR, TOTAL_KINDS };
4133

4234
GCNRegPressure() {
4335
clear();
4436
}
4537

46-
bool empty() const { return getSGPRNum() == 0 && getVGPRNum(false) == 0; }
38+
bool empty() const { return !Value[SGPR] && !Value[VGPR] && !Value[AGPR]; }
4739

48-
void clear() { std::fill(&Value[0], &Value[TOTAL_KINDS], 0); }
40+
void clear() { std::fill(&Value[0], &Value[ValueArraySize], 0); }
4941

5042
/// \returns the SGPR32 pressure
51-
unsigned getSGPRNum() const { return Value[SGPR32]; }
43+
unsigned getSGPRNum() const { return Value[SGPR]; }
5244
/// \returns the aggregated ArchVGPR32, AccVGPR32 pressure dependent upon \p
5345
/// UnifiedVGPRFile
5446
unsigned getVGPRNum(bool UnifiedVGPRFile) const {
5547
if (UnifiedVGPRFile) {
56-
return Value[AGPR32] ? getUnifiedVGPRNum(Value[VGPR32], Value[AGPR32])
57-
: Value[VGPR32] + Value[AGPR32];
48+
return Value[AGPR] ? getUnifiedVGPRNum(Value[VGPR], Value[AGPR])
49+
: Value[VGPR];
5850
}
59-
return std::max(Value[VGPR32], Value[AGPR32]);
51+
return std::max(Value[VGPR], Value[AGPR]);
6052
}
6153

6254
/// Returns the aggregated VGPR pressure, assuming \p NumArchVGPRs ArchVGPRs
@@ -68,13 +60,14 @@ struct GCNRegPressure {
6860
}
6961

7062
/// \returns the ArchVGPR32 pressure
71-
unsigned getArchVGPRNum() const { return Value[VGPR32]; }
63+
unsigned getArchVGPRNum() const { return Value[VGPR]; }
7264
/// \returns the AccVGPR32 pressure
73-
unsigned getAGPRNum() const { return Value[AGPR32]; }
65+
unsigned getAGPRNum() const { return Value[AGPR]; }
7466

75-
unsigned getVGPRTuplesWeight() const { return std::max(Value[VGPR_TUPLE],
76-
Value[AGPR_TUPLE]); }
77-
unsigned getSGPRTuplesWeight() const { return Value[SGPR_TUPLE]; }
67+
unsigned getVGPRTuplesWeight() const {
68+
return std::max(Value[TOTAL_KINDS + VGPR], Value[TOTAL_KINDS + AGPR]);
69+
}
70+
unsigned getSGPRTuplesWeight() const { return Value[TOTAL_KINDS + SGPR]; }
7871

7972
unsigned getOccupancy(const GCNSubtarget &ST) const {
8073
return std::min(ST.getOccupancyWithNumSGPRs(getSGPRNum()),
@@ -106,31 +99,36 @@ struct GCNRegPressure {
10699
unsigned MaxOccupancy = std::numeric_limits<unsigned>::max()) const;
107100

108101
bool operator==(const GCNRegPressure &O) const {
109-
return std::equal(&Value[0], &Value[TOTAL_KINDS], O.Value);
102+
return std::equal(&Value[0], &Value[ValueArraySize], O.Value);
110103
}
111104

112105
bool operator!=(const GCNRegPressure &O) const {
113106
return !(*this == O);
114107
}
115108

116109
GCNRegPressure &operator+=(const GCNRegPressure &RHS) {
117-
for (unsigned I = 0; I < TOTAL_KINDS; ++I)
110+
for (unsigned I = 0; I < ValueArraySize; ++I)
118111
Value[I] += RHS.Value[I];
119112
return *this;
120113
}
121114

122115
GCNRegPressure &operator-=(const GCNRegPressure &RHS) {
123-
for (unsigned I = 0; I < TOTAL_KINDS; ++I)
116+
for (unsigned I = 0; I < ValueArraySize; ++I)
124117
Value[I] -= RHS.Value[I];
125118
return *this;
126119
}
127120

128121
void dump() const;
129122

130123
private:
131-
unsigned Value[TOTAL_KINDS];
124+
static constexpr unsigned ValueArraySize = TOTAL_KINDS * 2;
125+
126+
/// Pressure for all register kinds (first all regular registers kinds, then
127+
/// all tuple register kinds).
128+
unsigned Value[ValueArraySize];
132129

133-
static unsigned getRegKind(Register Reg, const MachineRegisterInfo &MRI);
130+
static unsigned getRegKind(const TargetRegisterClass *RC,
131+
const SIRegisterInfo *STI);
134132

135133
friend GCNRegPressure max(const GCNRegPressure &P1,
136134
const GCNRegPressure &P2);
@@ -140,7 +138,7 @@ struct GCNRegPressure {
140138

141139
inline GCNRegPressure max(const GCNRegPressure &P1, const GCNRegPressure &P2) {
142140
GCNRegPressure Res;
143-
for (unsigned I = 0; I < GCNRegPressure::TOTAL_KINDS; ++I)
141+
for (unsigned I = 0; I < GCNRegPressure::ValueArraySize; ++I)
144142
Res.Value[I] = std::max(P1.Value[I], P2.Value[I]);
145143
return Res;
146144
}

0 commit comments

Comments
 (0)