Skip to content

Commit

Permalink
[LLVM][TableGen] Change a few emitters to use const Record pointers (l…
Browse files Browse the repository at this point in the history
…lvm#110112)

Change DirectiveEmitter, Option Emitter, and X86 Emitters to use const
Record pointers.

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
  • Loading branch information
jurahul authored Sep 27, 2024
1 parent 22c8b1d commit 32719c4
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 23 deletions.
24 changes: 12 additions & 12 deletions llvm/include/llvm/TableGen/DirectiveEmitter.h
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,24 @@ class Directive : public BaseRecord {
public:
Directive(const Record *Def) : BaseRecord(Def) {}

std::vector<Record *> getAllowedClauses() const {
return Def->getValueAsListOfDefs("allowedClauses");
std::vector<const Record *> getAllowedClauses() const {
return Def->getValueAsListOfConstDefs("allowedClauses");
}

std::vector<Record *> getAllowedOnceClauses() const {
return Def->getValueAsListOfDefs("allowedOnceClauses");
std::vector<const Record *> getAllowedOnceClauses() const {
return Def->getValueAsListOfConstDefs("allowedOnceClauses");
}

std::vector<Record *> getAllowedExclusiveClauses() const {
return Def->getValueAsListOfDefs("allowedExclusiveClauses");
std::vector<const Record *> getAllowedExclusiveClauses() const {
return Def->getValueAsListOfConstDefs("allowedExclusiveClauses");
}

std::vector<Record *> getRequiredClauses() const {
return Def->getValueAsListOfDefs("requiredClauses");
std::vector<const Record *> getRequiredClauses() const {
return Def->getValueAsListOfConstDefs("requiredClauses");
}

std::vector<Record *> getLeafConstructs() const {
return Def->getValueAsListOfDefs("leafConstructs");
std::vector<const Record *> getLeafConstructs() const {
return Def->getValueAsListOfConstDefs("leafConstructs");
}

Record *getAssociation() const { return Def->getValueAsDef("association"); }
Expand Down Expand Up @@ -203,8 +203,8 @@ class Clause : public BaseRecord {
return Def->getValueAsString("enumClauseValue");
}

std::vector<Record *> getClauseVals() const {
return Def->getValueAsListOfDefs("allowedClauseValues");
std::vector<const Record *> getClauseVals() const {
return Def->getValueAsListOfConstDefs("allowedClauseValues");
}

bool isValueOptional() const { return Def->getValueAsBit("isValueOptional"); }
Expand Down
4 changes: 2 additions & 2 deletions llvm/utils/TableGen/CallingConvEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,9 @@ void CallingConvEmitter::EmitAction(const Record *Action, unsigned Indent,
O << IndentStr << "if (";

if (Action->isSubClassOf("CCIfType")) {
ListInit *VTs = Action->getValueAsListInit("VTs");
const ListInit *VTs = Action->getValueAsListInit("VTs");
for (unsigned i = 0, e = VTs->size(); i != e; ++i) {
Record *VT = VTs->getElementAsRecord(i);
const Record *VT = VTs->getElementAsRecord(i);
if (i != 0)
O << " ||\n " << IndentStr;
O << "LocVT == " << getEnumName(getValueType(VT));
Expand Down
9 changes: 5 additions & 4 deletions llvm/utils/TableGen/DirectiveEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,8 +343,9 @@ static void GenerateGetKindClauseVal(const DirectiveLanguage &DirLang,
if (ClauseVals.size() <= 0)
continue;

auto DefaultIt = find_if(
ClauseVals, [](Record *CV) { return CV->getValueAsBit("isDefault"); });
auto DefaultIt = find_if(ClauseVals, [](const Record *CV) {
return CV->getValueAsBit("isDefault");
});

if (DefaultIt == ClauseVals.end()) {
PrintError("At least one val in Clause " + C.getFormattedName() +
Expand Down Expand Up @@ -505,7 +506,7 @@ static void EmitLeafTable(const DirectiveLanguage &DirLang, raw_ostream &OS,
std::vector<LeafList> LeafTable(Directives.size());
for (auto [Idx, Rec] : enumerate(Directives)) {
Directive Dir(Rec);
std::vector<Record *> Leaves = Dir.getLeafConstructs();
std::vector<const Record *> Leaves = Dir.getLeafConstructs();

auto &List = LeafTable[Idx];
List.resize(MaxLeafCount + 2);
Expand Down Expand Up @@ -680,7 +681,7 @@ static void GenerateGetDirectiveAssociation(const DirectiveLanguage &DirLang,
return AS;
}
// Compute the association from leaf constructs.
std::vector<Record *> leaves = D.getLeafConstructs();
std::vector<const Record *> leaves = D.getLeafConstructs();
if (leaves.empty()) {
errs() << D.getName() << '\n';
PrintFatalError(errorPrefixFor(D) +
Expand Down
4 changes: 2 additions & 2 deletions llvm/utils/TableGen/OptionParserEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -471,13 +471,13 @@ static void EmitOptionParser(const RecordKeeper &Records, raw_ostream &OS) {

std::vector<std::pair<std::vector<std::string>, StringRef>>
HelpTextsForVariants;
for (Record *VisibilityHelp :
for (const Record *VisibilityHelp :
R.getValueAsListOfDefs("HelpTextsForVariants")) {
ArrayRef<Init *> Visibilities =
VisibilityHelp->getValueAsListInit("Visibilities")->getValues();

std::vector<std::string> VisibilityNames;
for (Init *Visibility : Visibilities)
for (const Init *Visibility : Visibilities)
VisibilityNames.push_back(Visibility->getAsUnquotedString());

HelpTextsForVariants.push_back(std::make_pair(
Expand Down
1 change: 0 additions & 1 deletion llvm/utils/TableGen/OptionRSTEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ using namespace llvm;
/// and emits a RST man page.
static void EmitOptionRST(const RecordKeeper &Records, raw_ostream &OS) {
llvm::StringMap<std::vector<const Record *>> OptionsByGroup;
std::vector<Record *> OptionsWithoutGroup;

// Get the options.
std::vector<const Record *> Opts = Records.getAllDerivedDefinitions("Option");
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/TableGen/X86FoldTablesEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ void X86FoldTablesEmitter::addEntryWithFlags(FoldTable &Table,
StringRef RegInstName = RegRec->getName();
unsigned DropLen =
RegInstName.ends_with("rkz") ? 2 : (RegInstName.ends_with("rk") ? 1 : 0);
Record *BaseDef =
const Record *BaseDef =
DropLen ? Records.getDef(RegInstName.drop_back(DropLen)) : nullptr;
bool IsMoveReg =
BaseDef ? Target.getInstruction(BaseDef).isMoveReg : RegInst->isMoveReg;
Expand Down
2 changes: 1 addition & 1 deletion llvm/utils/TableGen/X86InstrMappingEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ void X86InstrMappingEmitter::emitCompressEVEXTable(
StringRef Name = Rec->getName();
const CodeGenInstruction *NewInst = nullptr;
if (ManualMap.find(Name) != ManualMap.end()) {
Record *NewRec = Records.getDef(ManualMap.at(Rec->getName()));
const Record *NewRec = Records.getDef(ManualMap.at(Rec->getName()));
assert(NewRec && "Instruction not found!");
NewInst = &Target.getInstruction(NewRec);
} else if (Name.ends_with("_EVEX")) {
Expand Down

0 comments on commit 32719c4

Please sign in to comment.