Skip to content

[RISCV] Reduce size of CSR lookup tables. NFC #121606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 16 additions & 9 deletions llvm/lib/Target/RISCV/AsmParser/RISCVAsmParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,8 @@ ParseStatus RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
// Accept an immediate representing a named Sys Reg if it satisfies the
// the required features.
for (auto &Reg : Range) {
if (Reg.IsAltName || Reg.IsDeprecatedName)
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't strictly needed, but ensures we use the required features from the primary name and not the alternate or deprecated names.

continue;
if (Reg.haveRequiredFeatures(STI->getFeatureBits()))
return RISCVOperand::createSysReg(Reg.Name, S, Imm);
}
Expand Down Expand Up @@ -1952,22 +1954,27 @@ ParseStatus RISCVAsmParser::parseCSRSystemRegister(OperandVector &Operands) {
return ParseStatus::Failure;

const auto *SysReg = RISCVSysReg::lookupSysRegByName(Identifier);
if (!SysReg)
SysReg = RISCVSysReg::lookupSysRegByAltName(Identifier);
if (!SysReg)
if ((SysReg = RISCVSysReg::lookupSysRegByDeprecatedName(Identifier)))
Warning(S, "'" + Identifier + "' is a deprecated alias for '" +
SysReg->Name + "'");

// Accept a named Sys Reg if the required features are present.

if (SysReg) {
if (SysReg->IsDeprecatedName) {
// Lookup the undeprecated name.
auto Range = RISCVSysReg::lookupSysRegByEncoding(SysReg->Encoding);
for (auto &Reg : Range) {
if (Reg.IsAltName || Reg.IsDeprecatedName)
continue;
Warning(S, "'" + Identifier + "' is a deprecated alias for '" +
Reg.Name + "'");
}
}

// Accept a named Sys Reg if the required features are present.
const auto &FeatureBits = getSTI().getFeatureBits();
if (!SysReg->haveRequiredFeatures(FeatureBits)) {
const auto *Feature = llvm::find_if(RISCVFeatureKV, [&](auto Feature) {
return SysReg->FeaturesRequired[Feature.Value];
});
auto ErrorMsg = std::string("system register '") + SysReg->Name + "' ";
if (SysReg->isRV32Only && FeatureBits[RISCV::Feature64Bit]) {
if (SysReg->IsRV32Only && FeatureBits[RISCV::Feature64Bit]) {
ErrorMsg += "is RV32 only";
if (Feature != std::end(RISCVFeatureKV))
ErrorMsg += " and ";
Expand Down
8 changes: 4 additions & 4 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVBaseInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,6 @@ int getLoadFPImm(APFloat FPImm);
namespace RISCVSysReg {
struct SysReg {
const char Name[32];
const char AltName[32];
const char DeprecatedName[32];
unsigned Encoding;
// FIXME: add these additional fields when needed.
// Privilege Access: Read, Write, Read-Only.
Expand All @@ -467,11 +465,13 @@ struct SysReg {
// Register number without the privilege bits.
// unsigned Number;
FeatureBitset FeaturesRequired;
bool isRV32Only;
bool IsRV32Only;
bool IsAltName;
bool IsDeprecatedName;

bool haveRequiredFeatures(const FeatureBitset &ActiveFeatures) const {
// Not in 32-bit mode.
if (isRV32Only && ActiveFeatures[RISCV::Feature64Bit])
if (IsRV32Only && ActiveFeatures[RISCV::Feature64Bit])
return false;
// No required feature associated with the system register.
if (FeaturesRequired.none())
Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/Target/RISCV/MCTargetDesc/RISCVInstPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ void RISCVInstPrinter::printCSRSystemRegister(const MCInst *MI, unsigned OpNo,
unsigned Imm = MI->getOperand(OpNo).getImm();
auto Range = RISCVSysReg::lookupSysRegByEncoding(Imm);
for (auto &Reg : Range) {
if (Reg.IsAltName || Reg.IsDeprecatedName)
continue;
if (Reg.haveRequiredFeatures(STI.getFeatureBits())) {
markup(O, Markup::Register) << Reg.Name;
return;
Expand Down
37 changes: 14 additions & 23 deletions llvm/lib/Target/RISCV/RISCVSystemOperands.td
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,6 @@ include "llvm/TableGen/SearchableTable.td"

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

def SysRegsList : GenericTable {
let FilterClass = "SysReg";
// FIXME: add "ReadWrite", "Mode", "Extra", "Number" fields when needed.
let Fields = [
"Name", "AltName", "DeprecatedName", "Encoding", "FeaturesRequired",
"isRV32Only",
"Name", "Encoding", "FeaturesRequired",
"isRV32Only", "isAltName", "isDeprecatedName"
];

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

def lookupSysRegByAltName : SearchIndex {
let Table = SysRegsList;
let Key = [ "AltName" ];
}

def lookupSysRegByDeprecatedName : SearchIndex {
let Table = SysRegsList;
let Key = [ "DeprecatedName" ];
}

// The following CSR encodings match those given in Tables 2.2,
// 2.3, 2.4, 2.5 and 2.6 in the RISC-V Instruction Set Manual
// Volume II: Privileged Architecture.
Expand Down Expand Up @@ -123,15 +109,17 @@ def : SysReg<"senvcfg", 0x10A>;
def : SysReg<"sscratch", 0x140>;
def : SysReg<"sepc", 0x141>;
def : SysReg<"scause", 0x142>;
let DeprecatedName = "sbadaddr" in
def : SysReg<"stval", 0x143>;
let isDeprecatedName = 1 in
def : SysReg<"sbadaddr", 0x143>;
def : SysReg<"sip", 0x144>;

//===----------------------------------------------------------------------===//
// Supervisor Protection and Translation
//===----------------------------------------------------------------------===//
let DeprecatedName = "sptbr" in
def : SysReg<"satp", 0x180>;
let isDeprecatedName = 1 in
def : SysReg<"sptbr", 0x180>;

//===----------------------------------------------------------------------===//
// Quality-of-Service(QoS) Identifiers (Ssqosid)
Expand Down Expand Up @@ -245,8 +233,9 @@ def : SysReg<"mstatush", 0x310>;
def : SysReg<"mscratch", 0x340>;
def : SysReg<"mepc", 0x341>;
def : SysReg<"mcause", 0x342>;
let DeprecatedName = "mbadaddr" in
def : SysReg<"mtval", 0x343>;
let isDeprecatedName = 1 in
def : SysReg<"mbadaddr", 0x343>;
def : SysReg<"mip", 0x344>;
def : SysReg<"mtinst", 0x34A>;
def : SysReg<"mtval2", 0x34B>;
Expand Down Expand Up @@ -298,8 +287,9 @@ foreach i = 3...31 in
//===----------------------------------------------------------------------===//
// Machine Counter Setup
//===----------------------------------------------------------------------===//
let AltName = "mucounteren" in // Privileged spec v1.9.1 Name
def : SysReg<"mcountinhibit", 0x320>;
let isAltName = 1 in
def : SysReg<"mucounteren", 0x320>;

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

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

//===----------------------------------------------------------------------===//
Expand Down
Loading