[llvm] [LLVM][TableGen] Change VarLenCodeEmitterGen to use const RecordKeeper (PR #108960)
Rahul Joshi via llvm-commits
llvm-commits at lists.llvm.org
Tue Sep 17 04:14:03 PDT 2024
https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/108960
Change VarLenCodeEmitterGen to use const RecordKeeper.
>From 4a15958eb106930f5edacbcb328f38ff73f91ff3 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Tue, 17 Sep 2024 04:12:28 -0700
Subject: [PATCH] [LLVM][TableGen] Change VarLenCodeEmitterGen to use const
RecordKeeper
---
.../TableGen/Common/VarLenCodeEmitterGen.cpp | 75 +++++++++----------
.../TableGen/Common/VarLenCodeEmitterGen.h | 2 +-
2 files changed, 37 insertions(+), 40 deletions(-)
diff --git a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
index ce4cd354540d04..9b454cf7944620 100644
--- a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
+++ b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
@@ -67,7 +67,7 @@ using namespace llvm;
namespace {
class VarLenCodeEmitterGen {
- RecordKeeper &Records;
+ const RecordKeeper &Records;
// Representaton of alternative encodings used for HwModes.
using AltEncodingTy = int;
@@ -83,15 +83,16 @@ class VarLenCodeEmitterGen {
void emitInstructionBaseValues(
raw_ostream &OS,
ArrayRef<const CodeGenInstruction *> NumberedInstructions,
- CodeGenTarget &Target, AltEncodingTy Mode);
+ const CodeGenTarget &Target, AltEncodingTy Mode);
- std::string getInstructionCases(Record *R, CodeGenTarget &Target);
- std::string getInstructionCaseForEncoding(Record *R, AltEncodingTy Mode,
+ std::string getInstructionCases(const Record *R, const CodeGenTarget &Target);
+ std::string getInstructionCaseForEncoding(const Record *R, AltEncodingTy Mode,
const VarLenInst &VLI,
- CodeGenTarget &Target, int I);
+ const CodeGenTarget &Target,
+ int Indent);
public:
- explicit VarLenCodeEmitterGen(RecordKeeper &R) : Records(R) {}
+ explicit VarLenCodeEmitterGen(const RecordKeeper &R) : Records(R) {}
void run(raw_ostream &OS);
};
@@ -222,7 +223,6 @@ void VarLenInst::buildRec(const DagInit *DI) {
void VarLenCodeEmitterGen::run(raw_ostream &OS) {
CodeGenTarget Target(Records);
- auto Insts = Records.getAllDerivedDefinitions("Instruction");
auto NumberedInstructions = Target.getInstructionsByEnumValue();
@@ -238,10 +238,8 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
const CodeGenHwModes &HWM = Target.getHwModes();
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
- for (auto &KV : EBM) {
- AltEncodingTy Mode = KV.first;
+ for (const auto [Mode, EncodingDef] : EBM) {
Modes.insert({Mode, "_" + HWM.getMode(Mode).Name.str()});
- const Record *EncodingDef = KV.second;
const RecordVal *RV = EncodingDef->getValue("Inst");
DagInit *DI = cast<DagInit>(RV->getValue());
VarLenInsts[R].insert({Mode, VarLenInst(DI, RV)});
@@ -250,7 +248,7 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
}
}
const RecordVal *RV = R->getValue("Inst");
- DagInit *DI = cast<DagInit>(RV->getValue());
+ const DagInit *DI = cast<DagInit>(RV->getValue());
VarLenInsts[R].insert({Universal, VarLenInst(DI, RV)});
}
@@ -291,7 +289,7 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
std::map<std::string, std::vector<std::string>> CaseMap;
// Construct all cases statement for each opcode
- for (Record *R : Insts) {
+ for (const Record *R : Records.getAllDerivedDefinitions("Instruction")) {
if (R->getValueAsString("Namespace") == "TargetOpcode" ||
R->getValueAsBit("isPseudo"))
continue;
@@ -347,7 +345,7 @@ static void emitInstBits(raw_ostream &IS, raw_ostream &SS, const APInt &Bits,
void VarLenCodeEmitterGen::emitInstructionBaseValues(
raw_ostream &OS, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
- CodeGenTarget &Target, AltEncodingTy Mode) {
+ const CodeGenTarget &Target, AltEncodingTy Mode) {
std::string IndexArray, StorageArray;
raw_string_ostream IS(IndexArray), SS(StorageArray);
@@ -408,8 +406,9 @@ void VarLenCodeEmitterGen::emitInstructionBaseValues(
OS << IndexArray << StorageArray;
}
-std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
- CodeGenTarget &Target) {
+std::string
+VarLenCodeEmitterGen::getInstructionCases(const Record *R,
+ const CodeGenTarget &Target) {
auto It = VarLenInsts.find(R);
if (It == VarLenInsts.end())
PrintFatalError(R, "Parsed encoding record not found");
@@ -421,7 +420,8 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
// Universal, just pick the first mode.
AltEncodingTy Mode = Modes.begin()->first;
const auto &Encoding = Map.begin()->second;
- return getInstructionCaseForEncoding(R, Mode, Encoding, Target, 6);
+ return getInstructionCaseForEncoding(R, Mode, Encoding, Target,
+ /*Indent=*/6);
}
std::string Case;
@@ -434,8 +434,8 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
Case +=
" llvm_unreachable(\"Undefined encoding in this mode\");\n";
} else {
- Case +=
- getInstructionCaseForEncoding(R, It->first, It->second, Target, 8);
+ Case += getInstructionCaseForEncoding(R, It->first, It->second, Target,
+ /*Indent=*/8);
}
Case += " break;\n";
Case += " }\n";
@@ -445,15 +445,14 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
}
std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
- Record *R, AltEncodingTy Mode, const VarLenInst &VLI, CodeGenTarget &Target,
- int I) {
-
+ const Record *R, AltEncodingTy Mode, const VarLenInst &VLI,
+ const CodeGenTarget &Target, int Indent) {
CodeGenInstruction &CGI = Target.getInstruction(R);
std::string Case;
raw_string_ostream SS(Case);
// Populate based value.
- SS.indent(I) << "Inst = getInstBits" << Modes[Mode] << "(opcode);\n";
+ SS.indent(Indent) << "Inst = getInstBits" << Modes[Mode] << "(opcode);\n";
// Process each segment in VLI.
size_t Offset = 0U;
@@ -482,19 +481,21 @@ std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
if (ES.CustomEncoder.size())
CustomEncoder = ES.CustomEncoder;
- SS.indent(I) << "Scratch.clearAllBits();\n";
- SS.indent(I) << "// op: " << OperandName.drop_front(1) << "\n";
+ SS.indent(Indent) << "Scratch.clearAllBits();\n";
+ SS.indent(Indent) << "// op: " << OperandName.drop_front(1) << "\n";
if (CustomEncoder.empty())
- SS.indent(I) << "getMachineOpValue(MI, MI.getOperand("
- << utostr(FlatOpIdx) << ")";
+ SS.indent(Indent) << "getMachineOpValue(MI, MI.getOperand("
+ << utostr(FlatOpIdx) << ")";
else
- SS.indent(I) << CustomEncoder << "(MI, /*OpIdx=*/" << utostr(FlatOpIdx);
+ SS.indent(Indent) << CustomEncoder << "(MI, /*OpIdx=*/"
+ << utostr(FlatOpIdx);
SS << ", /*Pos=*/" << utostr(Offset) << ", Scratch, Fixups, STI);\n";
- SS.indent(I) << "Inst.insertBits(" << "Scratch.extractBits("
- << utostr(NumBits) << ", " << utostr(LoBit) << ")" << ", "
- << Offset << ");\n";
+ SS.indent(Indent) << "Inst.insertBits("
+ << "Scratch.extractBits(" << utostr(NumBits) << ", "
+ << utostr(LoBit) << ")"
+ << ", " << Offset << ");\n";
HighScratchAccess = std::max(HighScratchAccess, NumBits + LoBit);
}
@@ -503,24 +504,20 @@ std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
StringRef PostEmitter = R->getValueAsString("PostEncoderMethod");
if (!PostEmitter.empty())
- SS.indent(I) << "Inst = " << PostEmitter << "(MI, Inst, STI);\n";
+ SS.indent(Indent) << "Inst = " << PostEmitter << "(MI, Inst, STI);\n";
// Resize the scratch buffer if it's to small.
std::string ScratchResizeStr;
if (VLI.size() && !VLI.isFixedValueOnly()) {
raw_string_ostream RS(ScratchResizeStr);
- RS.indent(I) << "if (Scratch.getBitWidth() < " << HighScratchAccess
- << ") { Scratch = Scratch.zext(" << HighScratchAccess
- << "); }\n";
+ RS.indent(Indent) << "if (Scratch.getBitWidth() < " << HighScratchAccess
+ << ") { Scratch = Scratch.zext(" << HighScratchAccess
+ << "); }\n";
}
return ScratchResizeStr + Case;
}
-namespace llvm {
-
-void emitVarLenCodeEmitter(RecordKeeper &R, raw_ostream &OS) {
+void llvm::emitVarLenCodeEmitter(const RecordKeeper &R, raw_ostream &OS) {
VarLenCodeEmitterGen(R).run(OS);
}
-
-} // end namespace llvm
diff --git a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.h b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.h
index 48b317be88d018..ca0f9c3f8b4f3f 100644
--- a/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.h
+++ b/llvm/utils/TableGen/Common/VarLenCodeEmitterGen.h
@@ -53,7 +53,7 @@ class VarLenInst {
bool isFixedValueOnly() const { return !HasDynamicSegment; }
};
-void emitVarLenCodeEmitter(RecordKeeper &R, raw_ostream &OS);
+void emitVarLenCodeEmitter(const RecordKeeper &R, raw_ostream &OS);
} // end namespace llvm
More information about the llvm-commits
mailing list