Skip to content

Commit 66d4815

Browse files
committed
[RISCV] Flatten the ImpliedExts table in RISCVISAInfo.cpp
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 80628ee commit 66d4815

File tree

3 files changed

+43
-69
lines changed

3 files changed

+43
-69
lines changed

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 38 additions & 40 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,17 +886,17 @@ 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-
}
889+
auto Range = std::equal_range(std::begin(ImpliedExts),
890+
std::end(ImpliedExts), ExtName);
891+
for (auto I = Range.first, E = Range.second; I != E; ++I) {
892+
const char *ImpliedExt = I->ImpliedExt;
893+
if (WorkList.count(ImpliedExt))
894+
continue;
895+
if (Exts.count(ImpliedExt))
896+
continue;
897+
auto Version = findDefaultVersion(ImpliedExt);
898+
addExtension(ImpliedExt, Version.value());
899+
WorkList.insert(ImpliedExt);
894900
}
895901
}
896902

@@ -902,42 +908,34 @@ void RISCVISAInfo::updateImplication() {
902908
}
903909
}
904910

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}},
911+
static constexpr StringLiteral CombineIntoExts[] = {
912+
{"zk"}, {"zkn"}, {"zks"}, {"zvkn"}, {"zvknc"},
913+
{"zvkng"}, {"zvks"}, {"zvksc"}, {"zvksg"},
920914
};
921915

922916
void RISCVISAInfo::updateCombination() {
923-
bool IsNewCombine = false;
917+
bool MadeChange = false;
924918
do {
925-
IsNewCombine = false;
926-
for (CombinedExtsEntry CombineIntoExt : CombineIntoExts) {
927-
auto CombineExt = CombineIntoExt.CombineExt;
928-
auto RequiredExts = CombineIntoExt.RequiredExts;
919+
MadeChange = false;
920+
for (StringRef CombineExt : CombineIntoExts) {
929921
if (hasExtension(CombineExt))
930922
continue;
931-
bool IsAllRequiredFeatureExist = true;
932-
for (const char *Ext : RequiredExts)
933-
IsAllRequiredFeatureExist &= hasExtension(Ext);
934-
if (IsAllRequiredFeatureExist) {
923+
924+
// Look up the extension in the ImpliesExt table to find everything it
925+
// depends on.
926+
auto Range = std::equal_range(std::begin(ImpliedExts),
927+
std::end(ImpliedExts), CombineExt);
928+
bool HasAllRequiredFeatures = std::all_of(
929+
Range.first, Range.second, [&](const ImpliedExtsEntry &Implied) {
930+
return hasExtension(Implied.ImpliedExt);
931+
});
932+
if (HasAllRequiredFeatures) {
935933
auto Version = findDefaultVersion(CombineExt);
936934
addExtension(CombineExt, Version.value());
937-
IsNewCombine = true;
935+
MadeChange = true;
938936
}
939937
}
940-
} while (IsNewCombine);
938+
} while (MadeChange);
941939
}
942940

943941
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)