Skip to content

[LLVM][TableGen] Change VarLenCodeEmitterGen to use const RecordKeeper #108960

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
Sep 17, 2024
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
75 changes: 36 additions & 39 deletions llvm/utils/TableGen/Common/VarLenCodeEmitterGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
};
Expand Down Expand Up @@ -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();

Expand All @@ -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)});
Expand All @@ -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)});
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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");
Expand All @@ -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;
Expand All @@ -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";
Expand All @@ -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;
Expand Down Expand Up @@ -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);
}
Expand All @@ -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
2 changes: 1 addition & 1 deletion llvm/utils/TableGen/Common/VarLenCodeEmitterGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading