Skip to content

[RISCV] Use binary search to look up supported profiles. #90767

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 1, 2024

Conversation

topperc
Copy link
Collaborator

@topperc topperc commented May 1, 2024

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.

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.
@llvmbot
Copy link
Member

llvmbot commented May 1, 2024

@llvm/pr-subscribers-backend-risc-v

Author: Craig Topper (topperc)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/90767.diff

2 Files Affected:

  • (modified) llvm/lib/TargetParser/RISCVISAInfo.cpp (+8-8)
  • (modified) llvm/utils/TableGen/RISCVTargetDefEmitter.cpp (+4-1)
diff --git a/llvm/lib/TargetParser/RISCVISAInfo.cpp b/llvm/lib/TargetParser/RISCVISAInfo.cpp
index 64405ca8cb9ff1..c763ee73b4b0b7 100644
--- a/llvm/lib/TargetParser/RISCVISAInfo.cpp
+++ b/llvm/lib/TargetParser/RISCVISAInfo.cpp
@@ -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(
diff --git a/llvm/utils/TableGen/RISCVTargetDefEmitter.cpp b/llvm/utils/TableGen/RISCVTargetDefEmitter.cpp
index 097e1deb3ed108..6784514032eb32 100644
--- a/llvm/utils/TableGen/RISCVTargetDefEmitter.cpp
+++ b/llvm/utils/TableGen/RISCVTargetDefEmitter.cpp
@@ -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";

@topperc topperc changed the title [RISCV] Use binary_search to look up supported profiles. [RISCV] Use binary search to look up supported profiles. May 1, 2024
Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@topperc topperc merged commit a7e0798 into llvm:main May 1, 2024
@topperc topperc deleted the pr/sorted-profiles branch May 1, 2024 21:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants