Skip to content

Commit

Permalink
[RISCV] Use binary search to look up supported profiles. (#90767)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
topperc committed May 1, 2024
1 parent 41466a1 commit a7e0798
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
16 changes: 8 additions & 8 deletions llvm/lib/TargetParser/RISCVISAInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -598,14 +598,14 @@ RISCVISAInfo::parseArchString(StringRef Arch, bool EnableExperimentalExtension,
XLen = 64;
} else {
// Try parsing as a profile.
const auto *FoundProfile =
llvm::find_if(SupportedProfiles, [Arch](const RISCVProfile &Profile) {
return Arch.starts_with(Profile.Name);
});

if (FoundProfile != std::end(SupportedProfiles)) {
std::string NewArch = FoundProfile->MArch.str();
StringRef ArchWithoutProfile = Arch.drop_front(FoundProfile->Name.size());
auto I = llvm::upper_bound(SupportedProfiles, Arch,
[](StringRef Arch, const RISCVProfile &Profile) {
return Arch < Profile.Name;
});

if (I != std::begin(SupportedProfiles) && Arch.starts_with((--I)->Name)) {
std::string NewArch = I->MArch.str();
StringRef ArchWithoutProfile = Arch.drop_front(I->Name.size());
if (!ArchWithoutProfile.empty()) {
if (ArchWithoutProfile.front() != '_')
return createStringError(
Expand Down
5 changes: 4 additions & 1 deletion llvm/utils/TableGen/RISCVTargetDefEmitter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,10 @@ static void emitRISCVProfiles(RecordKeeper &Records, raw_ostream &OS) {

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

for (const Record *Rec : Records.getAllDerivedDefinitions("RISCVProfile")) {
auto Profiles = Records.getAllDerivedDefinitions("RISCVProfile");
llvm::sort(Profiles, LessRecordFieldName());

for (const Record *Rec : Profiles) {
OS.indent(4) << "{\"" << Rec->getValueAsString("Name") << "\",\"";
printMArch(OS, Rec->getValueAsListOfDefs("Implies"));
OS << "\"},\n";
Expand Down

0 comments on commit a7e0798

Please sign in to comment.