Skip to content

Commit 754ff0f

Browse files
authored
[TableGen][RISCV] Use getAllDerivedDefinitionsIfDefined in RISCVTargetDefEmitter (llvm#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.
1 parent d94e0a1 commit 754ff0f

File tree

1 file changed

+33
-27
lines changed

1 file changed

+33
-27
lines changed

llvm/utils/TableGen/RISCVTargetDefEmitter.cpp

Lines changed: 33 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -48,37 +48,41 @@ static void emitRISCVExtensions(RecordKeeper &Records, raw_ostream &OS) {
4848
OS << "#undef GET_SUPPORTED_EXTENSIONS\n\n";
4949

5050
std::vector<Record *> Extensions =
51-
Records.getAllDerivedDefinitions("RISCVExtension");
51+
Records.getAllDerivedDefinitionsIfDefined("RISCVExtension");
5252
llvm::sort(Extensions, [](const Record *Rec1, const Record *Rec2) {
5353
return getExtensionName(Rec1) < getExtensionName(Rec2);
5454
});
5555

56-
printExtensionTable(OS, Extensions, /*Experimental=*/false);
57-
printExtensionTable(OS, Extensions, /*Experimental=*/true);
56+
if (!Extensions.empty()) {
57+
printExtensionTable(OS, Extensions, /*Experimental=*/false);
58+
printExtensionTable(OS, Extensions, /*Experimental=*/true);
59+
}
5860

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

6163
OS << "#ifdef GET_IMPLIED_EXTENSIONS\n";
6264
OS << "#undef GET_IMPLIED_EXTENSIONS\n\n";
6365

64-
OS << "\nstatic constexpr ImpliedExtsEntry ImpliedExts[] = {\n";
65-
for (Record *Ext : Extensions) {
66-
auto ImpliesList = Ext->getValueAsListOfDefs("Implies");
67-
if (ImpliesList.empty())
68-
continue;
66+
if (!Extensions.empty()) {
67+
OS << "\nstatic constexpr ImpliedExtsEntry ImpliedExts[] = {\n";
68+
for (Record *Ext : Extensions) {
69+
auto ImpliesList = Ext->getValueAsListOfDefs("Implies");
70+
if (ImpliesList.empty())
71+
continue;
6972

70-
StringRef Name = getExtensionName(Ext);
73+
StringRef Name = getExtensionName(Ext);
7174

72-
for (auto *ImpliedExt : ImpliesList) {
73-
if (!ImpliedExt->isSubClassOf("RISCVExtension"))
74-
continue;
75+
for (auto *ImpliedExt : ImpliesList) {
76+
if (!ImpliedExt->isSubClassOf("RISCVExtension"))
77+
continue;
7578

76-
OS << " { {\"" << Name << "\"}, \"" << getExtensionName(ImpliedExt)
77-
<< "\"},\n";
79+
OS << " { {\"" << Name << "\"}, \"" << getExtensionName(ImpliedExt)
80+
<< "\"},\n";
81+
}
7882
}
79-
}
8083

81-
OS << "};\n\n";
84+
OS << "};\n\n";
85+
}
8286

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

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

127-
auto Profiles = Records.getAllDerivedDefinitions("RISCVProfile");
128-
llvm::sort(Profiles, LessRecordFieldName());
131+
if (!Profiles.empty()) {
132+
llvm::sort(Profiles, LessRecordFieldName());
133+
OS << "static constexpr RISCVProfile SupportedProfiles[] = {\n";
134+
for (const Record *Rec : Profiles) {
135+
OS.indent(4) << "{\"" << Rec->getValueAsString("Name") << "\",\"";
136+
printMArch(OS, Rec->getValueAsListOfDefs("Implies"));
137+
OS << "\"},\n";
138+
}
129139

130-
for (const Record *Rec : Profiles) {
131-
OS.indent(4) << "{\"" << Rec->getValueAsString("Name") << "\",\"";
132-
printMArch(OS, Rec->getValueAsListOfDefs("Implies"));
133-
OS << "\"},\n";
140+
OS << "};\n\n";
134141
}
135142

136-
OS << "};\n\n";
137-
138143
OS << "#endif // GET_SUPPORTED_PROFILES\n\n";
139144
}
140145

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

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

179185
for (const Record *Rec :
180-
RK.getAllDerivedDefinitions("RISCVTuneProcessorModel")) {
186+
RK.getAllDerivedDefinitionsIfDefined("RISCVTuneProcessorModel")) {
181187
OS << "TUNE_PROC(" << Rec->getName() << ", "
182188
<< "\"" << Rec->getValueAsString("Name") << "\")\n";
183189
}

0 commit comments

Comments
 (0)