Skip to content

Commit 451e853

Browse files
authored
[RISCV] Flatten the ImpliedExts table in RISCVISAInfo.cpp (#89975)
Previously we had an individiaul global array of implied extensions for each extension that needed it. This allowed each array to have a different length. Then we had a sorted table that stored pointers and size for the indivual arrays keyed by the extension name. This patch changes the sorted table to use multiple rows if multiple extensions are implied. We use equal_range instead of lower_bound to find all the rows that apply to a given extension. The CombineIntoExts array was also modified to store only the extension name that need to be combined. This extension name is looked up in the implied table to find all the extensions it depends on.
1 parent 690c929 commit 451e853

File tree

3 files changed

+45
-70
lines changed

3 files changed

+45
-70
lines changed

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -847,15 +847,21 @@ Error RISCVISAInfo::checkDependency() {
847847

848848
struct ImpliedExtsEntry {
849849
StringLiteral Name;
850-
ArrayRef<const char *> Exts;
850+
const char *ImpliedExt;
851851

852852
bool operator<(const ImpliedExtsEntry &Other) const {
853853
return Name < Other.Name;
854854
}
855-
856-
bool operator<(StringRef Other) const { return Name < Other; }
857855
};
858856

857+
static bool operator<(const ImpliedExtsEntry &LHS, StringRef RHS) {
858+
return LHS.Name < RHS;
859+
}
860+
861+
static bool operator<(StringRef LHS, const ImpliedExtsEntry &RHS) {
862+
return LHS < RHS.Name;
863+
}
864+
859865
#define GET_IMPLIED_EXTENSIONS
860866
#include "llvm/TargetParser/RISCVTargetParserDef.inc"
861867

@@ -880,18 +886,19 @@ void RISCVISAInfo::updateImplication() {
880886

881887
while (!WorkList.empty()) {
882888
StringRef ExtName = WorkList.pop_back_val();
883-
auto I = llvm::lower_bound(ImpliedExts, ExtName);
884-
if (I != std::end(ImpliedExts) && I->Name == ExtName) {
885-
for (const char *ImpliedExt : I->Exts) {
886-
if (WorkList.count(ImpliedExt))
887-
continue;
888-
if (Exts.count(ImpliedExt))
889-
continue;
890-
auto Version = findDefaultVersion(ImpliedExt);
891-
addExtension(ImpliedExt, Version.value());
892-
WorkList.insert(ImpliedExt);
893-
}
894-
}
889+
auto Range = std::equal_range(std::begin(ImpliedExts),
890+
std::end(ImpliedExts), ExtName);
891+
std::for_each(Range.first, Range.second,
892+
[&](const ImpliedExtsEntry &Implied) {
893+
const char *ImpliedExt = Implied.ImpliedExt;
894+
if (WorkList.count(ImpliedExt))
895+
return;
896+
if (Exts.count(ImpliedExt))
897+
return;
898+
auto Version = findDefaultVersion(ImpliedExt);
899+
addExtension(ImpliedExt, Version.value());
900+
WorkList.insert(ImpliedExt);
901+
});
895902
}
896903

897904
// Add Zcf if Zce and F are enabled on RV32.
@@ -902,42 +909,34 @@ void RISCVISAInfo::updateImplication() {
902909
}
903910
}
904911

905-
struct CombinedExtsEntry {
906-
StringLiteral CombineExt;
907-
ArrayRef<const char *> RequiredExts;
908-
};
909-
910-
static constexpr CombinedExtsEntry CombineIntoExts[] = {
911-
{{"zk"}, {ImpliedExtsZk}},
912-
{{"zkn"}, {ImpliedExtsZkn}},
913-
{{"zks"}, {ImpliedExtsZks}},
914-
{{"zvkn"}, {ImpliedExtsZvkn}},
915-
{{"zvknc"}, {ImpliedExtsZvknc}},
916-
{{"zvkng"}, {ImpliedExtsZvkng}},
917-
{{"zvks"}, {ImpliedExtsZvks}},
918-
{{"zvksc"}, {ImpliedExtsZvksc}},
919-
{{"zvksg"}, {ImpliedExtsZvksg}},
912+
static constexpr StringLiteral CombineIntoExts[] = {
913+
{"zk"}, {"zkn"}, {"zks"}, {"zvkn"}, {"zvknc"},
914+
{"zvkng"}, {"zvks"}, {"zvksc"}, {"zvksg"},
920915
};
921916

922917
void RISCVISAInfo::updateCombination() {
923-
bool IsNewCombine = false;
918+
bool MadeChange = false;
924919
do {
925-
IsNewCombine = false;
926-
for (CombinedExtsEntry CombineIntoExt : CombineIntoExts) {
927-
auto CombineExt = CombineIntoExt.CombineExt;
928-
auto RequiredExts = CombineIntoExt.RequiredExts;
920+
MadeChange = false;
921+
for (StringRef CombineExt : CombineIntoExts) {
929922
if (hasExtension(CombineExt))
930923
continue;
931-
bool IsAllRequiredFeatureExist = true;
932-
for (const char *Ext : RequiredExts)
933-
IsAllRequiredFeatureExist &= hasExtension(Ext);
934-
if (IsAllRequiredFeatureExist) {
924+
925+
// Look up the extension in the ImpliesExt table to find everything it
926+
// depends on.
927+
auto Range = std::equal_range(std::begin(ImpliedExts),
928+
std::end(ImpliedExts), CombineExt);
929+
bool HasAllRequiredFeatures = std::all_of(
930+
Range.first, Range.second, [&](const ImpliedExtsEntry &Implied) {
931+
return hasExtension(Implied.ImpliedExt);
932+
});
933+
if (HasAllRequiredFeatures) {
935934
auto Version = findDefaultVersion(CombineExt);
936935
addExtension(CombineExt, Version.value());
937-
IsNewCombine = true;
936+
MadeChange = true;
938937
}
939938
}
940-
} while (IsNewCombine);
939+
} while (MadeChange);
941940
}
942941

943942
void RISCVISAInfo::updateFLen() {

llvm/test/TableGen/riscv-target-def.td

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,8 @@ def ROCKET : RISCVTuneProcessorModel<"rocket",
113113
// CHECK: #ifdef GET_IMPLIED_EXTENSIONS
114114
// CHECK-NEXT: #undef GET_IMPLIED_EXTENSIONS
115115

116-
// CHECK: static const char *ImpliedExtsF[] = {"zicsr"};
117-
118116
// CHECK: static constexpr ImpliedExtsEntry ImpliedExts[] = {
119-
// CHECK-NEXT: { {"f"}, {ImpliedExtsF} },
117+
// CHECK-NEXT: { {"f"}, "zicsr"},
120118
// CHECK-NEXT: };
121119

122120
// CHECK: #endif // GET_IMPLIED_EXTENSIONS

llvm/utils/TableGen/RISCVTargetDefEmitter.cpp

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,6 @@ static void printExtensionTable(raw_ostream &OS,
4343
OS << "};\n\n";
4444
}
4545

46-
// Get the extension name from the Record name. This gives the canonical
47-
// capitalization.
48-
static StringRef getExtensionNameFromRecordName(const Record *R) {
49-
StringRef Name = R->getName();
50-
if (!Name.consume_front("FeatureStdExt"))
51-
Name.consume_front("FeatureVendor");
52-
53-
return Name;
54-
}
55-
5646
static void emitRISCVExtensions(RecordKeeper &Records, raw_ostream &OS) {
5747
OS << "#ifdef GET_SUPPORTED_EXTENSIONS\n";
5848
OS << "#undef GET_SUPPORTED_EXTENSIONS\n\n";
@@ -71,33 +61,21 @@ static void emitRISCVExtensions(RecordKeeper &Records, raw_ostream &OS) {
7161
OS << "#ifdef GET_IMPLIED_EXTENSIONS\n";
7262
OS << "#undef GET_IMPLIED_EXTENSIONS\n\n";
7363

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

79-
OS << "static const char *ImpliedExts"
80-
<< getExtensionNameFromRecordName(Ext) << "[] = {";
70+
StringRef Name = getExtensionName(Ext);
8171

82-
ListSeparator LS(", ");
8372
for (auto *ImpliedExt : ImpliesList) {
8473
if (!ImpliedExt->isSubClassOf("RISCVExtension"))
8574
continue;
8675

87-
OS << LS << '"' << getExtensionName(ImpliedExt) << '"';
76+
OS << " { {\"" << Name << "\"}, \"" << getExtensionName(ImpliedExt)
77+
<< "\"},\n";
8878
}
89-
90-
OS << "};\n";
91-
}
92-
93-
OS << "\nstatic constexpr ImpliedExtsEntry ImpliedExts[] = {\n";
94-
for (Record *Ext : Extensions) {
95-
auto ImpliesList = Ext->getValueAsListOfDefs("Implies");
96-
if (ImpliesList.empty())
97-
continue;
98-
99-
OS << " { {\"" << getExtensionName(Ext) << "\"}, {ImpliedExts"
100-
<< getExtensionNameFromRecordName(Ext) << "} },\n";
10179
}
10280

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

0 commit comments

Comments
 (0)