Skip to content

Commit 19655a1

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 627f445 commit 19655a1

File tree

2 files changed

+42
-66
lines changed

2 files changed

+42
-66
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/utils/TableGen/RISCVTargetDefEmitter.cpp

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,16 +53,6 @@ static void printExtensionTable(raw_ostream &OS,
5353
OS << "};\n\n";
5454
}
5555

56-
// Get the extension name from the Record name. This gives the canonical
57-
// capitalization.
58-
static StringRef getExtensionNameFromRecordName(const Record *R) {
59-
StringRef Name = R->getName();
60-
if (!Name.consume_front("FeatureStdExt"))
61-
Name.consume_front("FeatureVendor");
62-
63-
return Name;
64-
}
65-
6656
static void emitRISCVExtensions(RecordKeeper &Records, raw_ostream &OS) {
6757
OS << "#ifdef GET_SUPPORTED_EXTENSIONS\n";
6858
OS << "#undef GET_SUPPORTED_EXTENSIONS\n";
@@ -79,33 +69,21 @@ static void emitRISCVExtensions(RecordKeeper &Records, raw_ostream &OS) {
7969
OS << "#ifdef GET_IMPLIED_EXTENSIONS\n";
8070
OS << "#undef GET_IMPLIED_EXTENSIONS\n";
8171

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

87-
OS << "static const char *ImpliedExts"
88-
<< getExtensionNameFromRecordName(Ext) << "[] = {";
78+
StringRef Name = getExtensionName(Ext);
8979

90-
ListSeparator LS(", ");
9180
for (auto *ImpliedExt : ImpliesList) {
9281
if (!ImpliedExt->isSubClassOf("RISCVExtension"))
9382
continue;
9483

95-
OS << LS << '"' << getExtensionName(ImpliedExt) << '"';
84+
OS << " {{\"" << Name << "\"}, \"" << getExtensionName(ImpliedExt)
85+
<< "\"},\n";
9686
}
97-
98-
OS << "};\n";
99-
}
100-
101-
OS << "\nstatic constexpr ImpliedExtsEntry ImpliedExts[] = {\n";
102-
for (Record *Ext : Extensions) {
103-
auto ImpliesList = Ext->getValueAsListOfDefs("Implies");
104-
if (ImpliesList.empty())
105-
continue;
106-
107-
OS << " {{\"" << getExtensionName(Ext) << "\"}, {ImpliedExts"
108-
<< getExtensionNameFromRecordName(Ext) << "}},\n";
10987
}
11088

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

0 commit comments

Comments
 (0)