Skip to content

Commit a2b9058

Browse files
authored
[RISCV] Reduce size of CSR lookup tables. NFC (#121606)
Instead of storing 3 different names in each row of the table, use a separate row for each name and use a flag to indicate what type of name it is. The AltName and DeprecatedName weren't used often enough to justify storing them as a possibility for every register. This reduces the .rodata size by 27k and reduces the number of dynamic relocations since we now only need 1 lookup by name function. The lookup by name function each contained a ~400 entry table of const char* pointing to constant strings. Each of those requires a dynamic relocation. I also capitalized IsRV32Only in the C++ code to match coding standards.
1 parent 7c86ab8 commit a2b9058

File tree

4 files changed

+36
-36
lines changed

4 files changed

+36
-36
lines changed

llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,8 @@ ParseStatus RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
19151915
// Accept an immediate representing a named Sys Reg if it satisfies the
19161916
// the required features.
19171917
for (auto &Reg : Range) {
1918+
if (Reg.IsAltName || Reg.IsDeprecatedName)
1919+
continue;
19181920
if (Reg.haveRequiredFeatures(STI->getFeatureBits()))
19191921
return RISCVOperand::createSysReg(Reg.Name, S, Imm);
19201922
}
@@ -1952,22 +1954,27 @@ ParseStatus RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
19521954
return ParseStatus::Failure;
19531955

19541956
const auto *SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
1955-
if (!SysReg)
1956-
SysReg = RISCVSysReg::lookupSysRegByAltName(Identifier);
1957-
if (!SysReg)
1958-
if ((SysReg = RISCVSysReg::lookupSysRegByDeprecatedName(Identifier)))
1959-
Warning(S, "'" + Identifier + "' is a deprecated alias for '" +
1960-
SysReg->Name + "'");
1961-
1962-
// Accept a named Sys Reg if the required features are present.
1957+
19631958
if (SysReg) {
1959+
if (SysReg->IsDeprecatedName) {
1960+
// Lookup the undeprecated name.
1961+
auto Range = RISCVSysReg::lookupSysRegByEncoding(SysReg->Encoding);
1962+
for (auto &Reg : Range) {
1963+
if (Reg.IsAltName || Reg.IsDeprecatedName)
1964+
continue;
1965+
Warning(S, "'" + Identifier + "' is a deprecated alias for '" +
1966+
Reg.Name + "'");
1967+
}
1968+
}
1969+
1970+
// Accept a named Sys Reg if the required features are present.
19641971
const auto &FeatureBits = getSTI().getFeatureBits();
19651972
if (!SysReg->haveRequiredFeatures(FeatureBits)) {
19661973
const auto *Feature = llvm::find_if(RISCVFeatureKV, [&](auto Feature) {
19671974
return SysReg->FeaturesRequired[Feature.Value];
19681975
});
19691976
auto ErrorMsg = std::string("system register '") + SysReg->Name + "' ";
1970-
if (SysReg->isRV32Only && FeatureBits[RISCV::Feature64Bit]) {
1977+
if (SysReg->IsRV32Only && FeatureBits[RISCV::Feature64Bit]) {
19711978
ErrorMsg += "is RV32 only";
19721979
if (Feature != std::end(RISCVFeatureKV))
19731980
ErrorMsg += " and ";

llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -454,8 +454,6 @@ int getLoadFPImm(APFloat FPImm);
454454
namespace RISCVSysReg {
455455
struct SysReg {
456456
const char Name[32];
457-
const char AltName[32];
458-
const char DeprecatedName[32];
459457
unsigned Encoding;
460458
// FIXME: add these additional fields when needed.
461459
// Privilege Access: Read, Write, Read-Only.
@@ -467,11 +465,13 @@ struct SysReg {
467465
// Register number without the privilege bits.
468466
// unsigned Number;
469467
FeatureBitset FeaturesRequired;
470-
bool isRV32Only;
468+
bool IsRV32Only;
469+
bool IsAltName;
470+
bool IsDeprecatedName;
471471

472472
bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const {
473473
// Not in 32-bit mode.
474-
if (isRV32Only && ActiveFeatures[RISCV::Feature64Bit])
474+
if (IsRV32Only && ActiveFeatures[RISCV::Feature64Bit])
475475
return false;
476476
// No required feature associated with the system register.
477477
if (FeaturesRequired.none())

llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,8 @@ void RISCVInstPrinter::printCSRSystemRegister(const MCInst *MI, unsigned OpNo,
121121
unsigned Imm = MI->getOperand(OpNo).getImm();
122122
auto Range = RISCVSysReg::lookupSysRegByEncoding(Imm);
123123
for (auto &Reg : Range) {
124+
if (Reg.IsAltName || Reg.IsDeprecatedName)
125+
continue;
124126
if (Reg.haveRequiredFeatures(STI.getFeatureBits())) {
125127
markup(O, Markup::Register) << Reg.Name;
126128
return;

llvm/lib/Target/RISCV/RISCVSystemOperands.td

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,6 @@ include "llvm/TableGen/SearchableTable.td"
1919

2020
class SysReg<string name, bits<12> op> {
2121
string Name = name;
22-
// A maximum of one alias is supported right now.
23-
string AltName = name;
24-
// A maximum of one deprecated name is supported right now. Unlike the
25-
// `AltName` alias, a `DeprecatedName` generates a diagnostic when the name is
26-
// used to encourage software to migrate away from the name.
27-
string DeprecatedName = "";
2822
bits<12> Encoding = op;
2923
// FIXME: add these additional fields when needed.
3024
// Privilege Access: Read and Write = 0, 1, 2; Read-Only = 3.
@@ -37,14 +31,16 @@ class SysReg<string name, bits<12> op> {
3731
// bits<6> Number = op{5 - 0};
3832
code FeaturesRequired = [{ {} }];
3933
bit isRV32Only = 0;
34+
bit isAltName = 0;
35+
bit isDeprecatedName = 0;
4036
}
4137

4238
def SysRegsList : GenericTable {
4339
let FilterClass = "SysReg";
4440
// FIXME: add "ReadWrite", "Mode", "Extra", "Number" fields when needed.
4541
let Fields = [
46-
"Name", "AltName", "DeprecatedName", "Encoding", "FeaturesRequired",
47-
"isRV32Only",
42+
"Name", "Encoding", "FeaturesRequired",
43+
"isRV32Only", "isAltName", "isDeprecatedName"
4844
];
4945

5046
let PrimaryKey = [ "Encoding" ];
@@ -57,16 +53,6 @@ def lookupSysRegByName : SearchIndex {
5753
let Key = [ "Name" ];
5854
}
5955

60-
def lookupSysRegByAltName : SearchIndex {
61-
let Table = SysRegsList;
62-
let Key = [ "AltName" ];
63-
}
64-
65-
def lookupSysRegByDeprecatedName : SearchIndex {
66-
let Table = SysRegsList;
67-
let Key = [ "DeprecatedName" ];
68-
}
69-
7056
// The following CSR encodings match those given in Tables 2.2,
7157
// 2.3, 2.4, 2.5 and 2.6 in the RISC-V Instruction Set Manual
7258
// Volume II: Privileged Architecture.
@@ -123,15 +109,17 @@ def : SysReg<"senvcfg", 0x10A>;
123109
def : SysReg<"sscratch", 0x140>;
124110
def : SysReg<"sepc", 0x141>;
125111
def : SysReg<"scause", 0x142>;
126-
let DeprecatedName = "sbadaddr" in
127112
def : SysReg<"stval", 0x143>;
113+
let isDeprecatedName = 1 in
114+
def : SysReg<"sbadaddr", 0x143>;
128115
def : SysReg<"sip", 0x144>;
129116

130117
//===----------------------------------------------------------------------===//
131118
// Supervisor Protection and Translation
132119
//===----------------------------------------------------------------------===//
133-
let DeprecatedName = "sptbr" in
134120
def : SysReg<"satp", 0x180>;
121+
let isDeprecatedName = 1 in
122+
def : SysReg<"sptbr", 0x180>;
135123

136124
//===----------------------------------------------------------------------===//
137125
// Quality-of-Service(QoS) Identifiers (Ssqosid)
@@ -245,8 +233,9 @@ def : SysReg<"mstatush", 0x310>;
245233
def : SysReg<"mscratch", 0x340>;
246234
def : SysReg<"mepc", 0x341>;
247235
def : SysReg<"mcause", 0x342>;
248-
let DeprecatedName = "mbadaddr" in
249236
def : SysReg<"mtval", 0x343>;
237+
let isDeprecatedName = 1 in
238+
def : SysReg<"mbadaddr", 0x343>;
250239
def : SysReg<"mip", 0x344>;
251240
def : SysReg<"mtinst", 0x34A>;
252241
def : SysReg<"mtval2", 0x34B>;
@@ -298,8 +287,9 @@ foreach i = 3...31 in
298287
//===----------------------------------------------------------------------===//
299288
// Machine Counter Setup
300289
//===----------------------------------------------------------------------===//
301-
let AltName = "mucounteren" in // Privileged spec v1.9.1 Name
302290
def : SysReg<"mcountinhibit", 0x320>;
291+
let isAltName = 1 in
292+
def : SysReg<"mucounteren", 0x320>;
303293

304294
// mhpmevent3-mhpmevent31 at 0x323-0x33F.
305295
foreach i = 3...31 in
@@ -336,8 +326,9 @@ def : SysReg<"dpc", 0x7B1>;
336326

337327
// "dscratch" is an alternative name for "dscratch0" which appeared in earlier
338328
// drafts of the RISC-V debug spec
339-
let AltName = "dscratch" in
340329
def : SysReg<"dscratch0", 0x7B2>;
330+
let isAltName = 1 in
331+
def : SysReg<"dscratch", 0x7B2>;
341332
def : SysReg<"dscratch1", 0x7B3>;
342333

343334
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)