Skip to content

Commit

Permalink
[TableGen][RISCV] Use getAllDerivedDefinitionsIfDefined in RISCVTarge…
Browse files Browse the repository at this point in the history
…tDefEmitter (#91941)

getAllDerivedDefinitions produces a fatal error if there are no
definitions. In practice this isn't much of a problem for
llvm/lib/Target/RISCV/*.td where it's hard to imagine not having at
least one of the required defitions. But it limits our ability to
structure and maintain tests (which is how I came across this issue).

This commit moves to using getAllDerivedDefinitionsIfDefined and aims to
skip emission of data structures that make no sense if no definitions
were found.
  • Loading branch information
asb committed May 13, 2024
1 parent d94e0a1 commit 754ff0f
Showing 1 changed file with 33 additions and 27 deletions.
60 changes: 33 additions & 27 deletions llvm/utils/TableGen/RISCVTargetDefEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,37 +48,41 @@ static void emitRISCVExtensions(RecordKeeper &Records, raw_ostream &OS) {
OS << "#undef GET_SUPPORTED_EXTENSIONS\n\n";

std::vector<Record *> Extensions =
Records.getAllDerivedDefinitions("RISCVExtension");
Records.getAllDerivedDefinitionsIfDefined("RISCVExtension");
llvm::sort(Extensions, [](const Record *Rec1, const Record *Rec2) {
return getExtensionName(Rec1) < getExtensionName(Rec2);
});

printExtensionTable(OS, Extensions, /*Experimental=*/false);
printExtensionTable(OS, Extensions, /*Experimental=*/true);
if (!Extensions.empty()) {
printExtensionTable(OS, Extensions, /*Experimental=*/false);
printExtensionTable(OS, Extensions, /*Experimental=*/true);
}

OS << "#endif // GET_SUPPORTED_EXTENSIONS\n\n";

OS << "#ifdef GET_IMPLIED_EXTENSIONS\n";
OS << "#undef GET_IMPLIED_EXTENSIONS\n\n";

OS << "\nstatic constexpr ImpliedExtsEntry ImpliedExts[] = {\n";
for (Record *Ext : Extensions) {
auto ImpliesList = Ext->getValueAsListOfDefs("Implies");
if (ImpliesList.empty())
continue;
if (!Extensions.empty()) {
OS << "\nstatic constexpr ImpliedExtsEntry ImpliedExts[] = {\n";
for (Record *Ext : Extensions) {
auto ImpliesList = Ext->getValueAsListOfDefs("Implies");
if (ImpliesList.empty())
continue;

StringRef Name = getExtensionName(Ext);
StringRef Name = getExtensionName(Ext);

for (auto *ImpliedExt : ImpliesList) {
if (!ImpliedExt->isSubClassOf("RISCVExtension"))
continue;
for (auto *ImpliedExt : ImpliesList) {
if (!ImpliedExt->isSubClassOf("RISCVExtension"))
continue;

OS << " { {\"" << Name << "\"}, \"" << getExtensionName(ImpliedExt)
<< "\"},\n";
OS << " { {\"" << Name << "\"}, \"" << getExtensionName(ImpliedExt)
<< "\"},\n";
}
}
}

OS << "};\n\n";
OS << "};\n\n";
}

OS << "#endif // GET_IMPLIED_EXTENSIONS\n\n";
}
Expand Down Expand Up @@ -122,19 +126,20 @@ static void emitRISCVProfiles(RecordKeeper &Records, raw_ostream &OS) {
OS << "#ifdef GET_SUPPORTED_PROFILES\n";
OS << "#undef GET_SUPPORTED_PROFILES\n\n";

OS << "static constexpr RISCVProfile SupportedProfiles[] = {\n";
auto Profiles = Records.getAllDerivedDefinitionsIfDefined("RISCVProfile");

auto Profiles = Records.getAllDerivedDefinitions("RISCVProfile");
llvm::sort(Profiles, LessRecordFieldName());
if (!Profiles.empty()) {
llvm::sort(Profiles, LessRecordFieldName());
OS << "static constexpr RISCVProfile SupportedProfiles[] = {\n";
for (const Record *Rec : Profiles) {
OS.indent(4) << "{\"" << Rec->getValueAsString("Name") << "\",\"";
printMArch(OS, Rec->getValueAsListOfDefs("Implies"));
OS << "\"},\n";
}

for (const Record *Rec : Profiles) {
OS.indent(4) << "{\"" << Rec->getValueAsString("Name") << "\",\"";
printMArch(OS, Rec->getValueAsListOfDefs("Implies"));
OS << "\"},\n";
OS << "};\n\n";
}

OS << "};\n\n";

OS << "#endif // GET_SUPPORTED_PROFILES\n\n";
}

Expand All @@ -144,7 +149,8 @@ static void emitRISCVProcs(RecordKeeper &RK, raw_ostream &OS) {
<< "#endif\n\n";

// Iterate on all definition records.
for (const Record *Rec : RK.getAllDerivedDefinitions("RISCVProcessorModel")) {
for (const Record *Rec :
RK.getAllDerivedDefinitionsIfDefined("RISCVProcessorModel")) {
const std::vector<Record *> &Features =
Rec->getValueAsListOfDefs("Features");
bool FastScalarUnalignedAccess = any_of(Features, [&](auto &Feature) {
Expand Down Expand Up @@ -177,7 +183,7 @@ static void emitRISCVProcs(RecordKeeper &RK, raw_ostream &OS) {
<< "#endif\n\n";

for (const Record *Rec :
RK.getAllDerivedDefinitions("RISCVTuneProcessorModel")) {
RK.getAllDerivedDefinitionsIfDefined("RISCVTuneProcessorModel")) {
OS << "TUNE_PROC(" << Rec->getName() << ", "
<< "\"" << Rec->getValueAsString("Name") << "\")\n";
}
Expand Down

0 comments on commit 754ff0f

Please sign in to comment.