Skip to content

Commit a7e0798

Browse files
authored
[RISCV] Use binary search to look up supported profiles. (#90767)
As the list of profiles grow, this will be a more efficient lookup. Because the profile name is a prefix of the Arch string, we use upper_bound to find the first profile that definitely comes after the Arch string. If that isn't the first supported profile, we move back 1 profile and see if that profile is a prefix of our Arch string.
1 parent 41466a1 commit a7e0798

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

llvm/lib/TargetParser/RISCVISAInfo.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -598,14 +598,14 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
598598
XLen = 64;
599599
} else {
600600
// Try parsing as a profile.
601-
const auto *FoundProfile =
602-
llvm::find_if(SupportedProfiles, [Arch](const RISCVProfile &Profile) {
603-
return Arch.starts_with(Profile.Name);
604-
});
605-
606-
if (FoundProfile != std::end(SupportedProfiles)) {
607-
std::string NewArch = FoundProfile->MArch.str();
608-
StringRef ArchWithoutProfile = Arch.drop_front(FoundProfile->Name.size());
601+
auto I = llvm::upper_bound(SupportedProfiles, Arch,
602+
[](StringRef Arch, const RISCVProfile &Profile) {
603+
return Arch < Profile.Name;
604+
});
605+
606+
if (I != std::begin(SupportedProfiles) && Arch.starts_with((--I)->Name)) {
607+
std::string NewArch = I->MArch.str();
608+
StringRef ArchWithoutProfile = Arch.drop_front(I->Name.size());
609609
if (!ArchWithoutProfile.empty()) {
610610
if (ArchWithoutProfile.front() != '_')
611611
return createStringError(

llvm/utils/TableGen/RISCVTargetDefEmitter.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,10 @@ static void emitRISCVProfiles(RecordKeeper &Records, raw_ostream &OS) {
124124

125125
OS << "static constexpr RISCVProfile SupportedProfiles[] = {\n";
126126

127-
for (const Record *Rec : Records.getAllDerivedDefinitions("RISCVProfile")) {
127+
auto Profiles = Records.getAllDerivedDefinitions("RISCVProfile");
128+
llvm::sort(Profiles, LessRecordFieldName());
129+
130+
for (const Record *Rec : Profiles) {
128131
OS.indent(4) << "{\"" << Rec->getValueAsString("Name") << "\",\"";
129132
printMArch(OS, Rec->getValueAsListOfDefs("Implies"));
130133
OS << "\"},\n";

0 commit comments

Comments
 (0)