Skip to content

Commit 1a65e15

Browse files
jurahultmsri
authored andcommitted
[LLVM][TableGen] Change VarLenCodeEmitterGen to use const RecordKeeper (llvm#108960)
Change VarLenCodeEmitterGen to use const RecordKeeper. This is a part of effort to have better const correctness in TableGen backends: https://discourse.llvm.org/t/psa-planned-changes-to-tablegen-getallderiveddefinitions-api-potential-downstream-breakages/81089
1 parent 506d993 commit 1a65e15

File tree

2 files changed

+37
-40
lines changed

2 files changed

+37
-40
lines changed

llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ using namespace llvm;
6767
namespace {
6868

6969
class VarLenCodeEmitterGen {
70-
RecordKeeper &Records;
70+
const RecordKeeper &Records;
7171

7272
// Representaton of alternative encodings used for HwModes.
7373
using AltEncodingTy = int;
@@ -83,15 +83,16 @@ class VarLenCodeEmitterGen {
8383
void emitInstructionBaseValues(
8484
raw_ostream &OS,
8585
ArrayRef<const CodeGenInstruction *> NumberedInstructions,
86-
CodeGenTarget &Target, AltEncodingTy Mode);
86+
const CodeGenTarget &Target, AltEncodingTy Mode);
8787

88-
std::string getInstructionCases(Record *R, CodeGenTarget &Target);
89-
std::string getInstructionCaseForEncoding(Record *R, AltEncodingTy Mode,
88+
std::string getInstructionCases(const Record *R, const CodeGenTarget &Target);
89+
std::string getInstructionCaseForEncoding(const Record *R, AltEncodingTy Mode,
9090
const VarLenInst &VLI,
91-
CodeGenTarget &Target, int I);
91+
const CodeGenTarget &Target,
92+
int Indent);
9293

9394
public:
94-
explicit VarLenCodeEmitterGen(RecordKeeper &R) : Records(R) {}
95+
explicit VarLenCodeEmitterGen(const RecordKeeper &R) : Records(R) {}
9596

9697
void run(raw_ostream &OS);
9798
};
@@ -222,7 +223,6 @@ void VarLenInst::buildRec(const DagInit *DI) {
222223

223224
void VarLenCodeEmitterGen::run(raw_ostream &OS) {
224225
CodeGenTarget Target(Records);
225-
auto Insts = Records.getAllDerivedDefinitions("Instruction");
226226

227227
auto NumberedInstructions = Target.getInstructionsByEnumValue();
228228

@@ -238,10 +238,8 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
238238
if (auto *DI = dyn_cast_or_null<DefInit>(RV->getValue())) {
239239
const CodeGenHwModes &HWM = Target.getHwModes();
240240
EncodingInfoByHwMode EBM(DI->getDef(), HWM);
241-
for (auto &KV : EBM) {
242-
AltEncodingTy Mode = KV.first;
241+
for (const auto [Mode, EncodingDef] : EBM) {
243242
Modes.insert({Mode, "_" + HWM.getMode(Mode).Name.str()});
244-
const Record *EncodingDef = KV.second;
245243
const RecordVal *RV = EncodingDef->getValue("Inst");
246244
DagInit *DI = cast<DagInit>(RV->getValue());
247245
VarLenInsts[R].insert({Mode, VarLenInst(DI, RV)});
@@ -250,7 +248,7 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
250248
}
251249
}
252250
const RecordVal *RV = R->getValue("Inst");
253-
DagInit *DI = cast<DagInit>(RV->getValue());
251+
const DagInit *DI = cast<DagInit>(RV->getValue());
254252
VarLenInsts[R].insert({Universal, VarLenInst(DI, RV)});
255253
}
256254

@@ -291,7 +289,7 @@ void VarLenCodeEmitterGen::run(raw_ostream &OS) {
291289
std::map<std::string, std::vector<std::string>> CaseMap;
292290

293291
// Construct all cases statement for each opcode
294-
for (Record *R : Insts) {
292+
for (const Record *R : Records.getAllDerivedDefinitions("Instruction")) {
295293
if (R->getValueAsString("Namespace") == "TargetOpcode" ||
296294
R->getValueAsBit("isPseudo"))
297295
continue;
@@ -347,7 +345,7 @@ static void emitInstBits(raw_ostream &IS, raw_ostream &SS, const APInt &Bits,
347345

348346
void VarLenCodeEmitterGen::emitInstructionBaseValues(
349347
raw_ostream &OS, ArrayRef<const CodeGenInstruction *> NumberedInstructions,
350-
CodeGenTarget &Target, AltEncodingTy Mode) {
348+
const CodeGenTarget &Target, AltEncodingTy Mode) {
351349
std::string IndexArray, StorageArray;
352350
raw_string_ostream IS(IndexArray), SS(StorageArray);
353351

@@ -408,8 +406,9 @@ void VarLenCodeEmitterGen::emitInstructionBaseValues(
408406
OS << IndexArray << StorageArray;
409407
}
410408

411-
std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
412-
CodeGenTarget &Target) {
409+
std::string
410+
VarLenCodeEmitterGen::getInstructionCases(const Record *R,
411+
const CodeGenTarget &Target) {
413412
auto It = VarLenInsts.find(R);
414413
if (It == VarLenInsts.end())
415414
PrintFatalError(R, "Parsed encoding record not found");
@@ -421,7 +420,8 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
421420
// Universal, just pick the first mode.
422421
AltEncodingTy Mode = Modes.begin()->first;
423422
const auto &Encoding = Map.begin()->second;
424-
return getInstructionCaseForEncoding(R, Mode, Encoding, Target, 6);
423+
return getInstructionCaseForEncoding(R, Mode, Encoding, Target,
424+
/*Indent=*/6);
425425
}
426426

427427
std::string Case;
@@ -434,8 +434,8 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
434434
Case +=
435435
" llvm_unreachable(\"Undefined encoding in this mode\");\n";
436436
} else {
437-
Case +=
438-
getInstructionCaseForEncoding(R, It->first, It->second, Target, 8);
437+
Case += getInstructionCaseForEncoding(R, It->first, It->second, Target,
438+
/*Indent=*/8);
439439
}
440440
Case += " break;\n";
441441
Case += " }\n";
@@ -445,15 +445,14 @@ std::string VarLenCodeEmitterGen::getInstructionCases(Record *R,
445445
}
446446

447447
std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
448-
Record *R, AltEncodingTy Mode, const VarLenInst &VLI, CodeGenTarget &Target,
449-
int I) {
450-
448+
const Record *R, AltEncodingTy Mode, const VarLenInst &VLI,
449+
const CodeGenTarget &Target, int Indent) {
451450
CodeGenInstruction &CGI = Target.getInstruction(R);
452451

453452
std::string Case;
454453
raw_string_ostream SS(Case);
455454
// Populate based value.
456-
SS.indent(I) << "Inst = getInstBits" << Modes[Mode] << "(opcode);\n";
455+
SS.indent(Indent) << "Inst = getInstBits" << Modes[Mode] << "(opcode);\n";
457456

458457
// Process each segment in VLI.
459458
size_t Offset = 0U;
@@ -482,19 +481,21 @@ std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
482481
if (ES.CustomEncoder.size())
483482
CustomEncoder = ES.CustomEncoder;
484483

485-
SS.indent(I) << "Scratch.clearAllBits();\n";
486-
SS.indent(I) << "// op: " << OperandName.drop_front(1) << "\n";
484+
SS.indent(Indent) << "Scratch.clearAllBits();\n";
485+
SS.indent(Indent) << "// op: " << OperandName.drop_front(1) << "\n";
487486
if (CustomEncoder.empty())
488-
SS.indent(I) << "getMachineOpValue(MI, MI.getOperand("
489-
<< utostr(FlatOpIdx) << ")";
487+
SS.indent(Indent) << "getMachineOpValue(MI, MI.getOperand("
488+
<< utostr(FlatOpIdx) << ")";
490489
else
491-
SS.indent(I) << CustomEncoder << "(MI, /*OpIdx=*/" << utostr(FlatOpIdx);
490+
SS.indent(Indent) << CustomEncoder << "(MI, /*OpIdx=*/"
491+
<< utostr(FlatOpIdx);
492492

493493
SS << ", /*Pos=*/" << utostr(Offset) << ", Scratch, Fixups, STI);\n";
494494

495-
SS.indent(I) << "Inst.insertBits(" << "Scratch.extractBits("
496-
<< utostr(NumBits) << ", " << utostr(LoBit) << ")" << ", "
497-
<< Offset << ");\n";
495+
SS.indent(Indent) << "Inst.insertBits("
496+
<< "Scratch.extractBits(" << utostr(NumBits) << ", "
497+
<< utostr(LoBit) << ")"
498+
<< ", " << Offset << ");\n";
498499

499500
HighScratchAccess = std::max(HighScratchAccess, NumBits + LoBit);
500501
}
@@ -503,24 +504,20 @@ std::string VarLenCodeEmitterGen::getInstructionCaseForEncoding(
503504

504505
StringRef PostEmitter = R->getValueAsString("PostEncoderMethod");
505506
if (!PostEmitter.empty())
506-
SS.indent(I) << "Inst = " << PostEmitter << "(MI, Inst, STI);\n";
507+
SS.indent(Indent) << "Inst = " << PostEmitter << "(MI, Inst, STI);\n";
507508

508509
// Resize the scratch buffer if it's to small.
509510
std::string ScratchResizeStr;
510511
if (VLI.size() && !VLI.isFixedValueOnly()) {
511512
raw_string_ostream RS(ScratchResizeStr);
512-
RS.indent(I) << "if (Scratch.getBitWidth() < " << HighScratchAccess
513-
<< ") { Scratch = Scratch.zext(" << HighScratchAccess
514-
<< "); }\n";
513+
RS.indent(Indent) << "if (Scratch.getBitWidth() < " << HighScratchAccess
514+
<< ") { Scratch = Scratch.zext(" << HighScratchAccess
515+
<< "); }\n";
515516
}
516517

517518
return ScratchResizeStr + Case;
518519
}
519520

520-
namespace llvm {
521-
522-
void emitVarLenCodeEmitter(RecordKeeper &R, raw_ostream &OS) {
521+
void llvm::emitVarLenCodeEmitter(const RecordKeeper &R, raw_ostream &OS) {
523522
VarLenCodeEmitterGen(R).run(OS);
524523
}
525-
526-
} // end namespace llvm

llvm/utils/TableGen/Common/VarLenCodeEmitterGen.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ class VarLenInst {
5353
bool isFixedValueOnly() const { return !HasDynamicSegment; }
5454
};
5555

56-
void emitVarLenCodeEmitter(RecordKeeper &R, raw_ostream &OS);
56+
void emitVarLenCodeEmitter(const RecordKeeper &R, raw_ostream &OS);
5757

5858
} // end namespace llvm
5959

0 commit comments

Comments
 (0)