Skip to content
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

[TableGen] Simplify generated code for isSubclass #117351

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

jayfoad
Copy link
Contributor

@jayfoad jayfoad commented Nov 22, 2024

Implement isSubclass with some tables and a single call to lower_bound
instead of nested switches.

Part of the motivation for this is improving compile time when clang-18
is used as a host compiler, since it seems to have trouble with very
large switch statements.

Implement isSubclass with some tables and a single call to lower_bound
instead of nested switches.

Part of the motivation for this is improving compile time when clang-18
is used as a host compiler, since it seems to have trouble with very
large switch statements.
@llvmbot
Copy link
Member

llvmbot commented Nov 22, 2024

@llvm/pr-subscribers-tablegen

Author: Jay Foad (jayfoad)

Changes

Implement isSubclass with some tables and a single call to lower_bound
instead of nested switches.

Part of the motivation for this is improving compile time when clang-18
is used as a host compiler, since it seems to have trouble with very
large switch statements.


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

1 Files Affected:

  • (modified) llvm/utils/TableGen/AsmMatcherEmitter.cpp (+18-32)
diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index ade393c11b7a24..68e9b434312a80 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -2541,7 +2541,6 @@ static void emitIsSubclass(CodeGenTarget &Target,
   OS << "  if (A == B)\n";
   OS << "    return true;\n\n";
 
-  bool EmittedSwitch = false;
   for (const auto &A : Infos) {
     std::vector<StringRef> SuperClasses;
     if (A.IsOptional)
@@ -2551,42 +2550,29 @@ static void emitIsSubclass(CodeGenTarget &Target,
         SuperClasses.push_back(B.Name);
     }
 
-    if (SuperClasses.empty())
+    if (SuperClasses.empty()) {
+      OS << "  static constexpr ArrayRef<uint16_t> " << A.Name << "_SuperClasses;\n";
       continue;
-
-    // If this is the first SuperClass, emit the switch header.
-    if (!EmittedSwitch) {
-      OS << "  switch (A) {\n";
-      OS << "  default:\n";
-      OS << "    return false;\n";
-      EmittedSwitch = true;
     }
 
-    OS << "\n  case " << A.Name << ":\n";
-
-    if (SuperClasses.size() == 1) {
-      OS << "    return B == " << SuperClasses.back() << ";\n";
-      continue;
-    }
-
-    if (!SuperClasses.empty()) {
-      OS << "    switch (B) {\n";
-      OS << "    default: return false;\n";
-      for (StringRef SC : SuperClasses)
-        OS << "    case " << SC << ": return true;\n";
-      OS << "    }\n";
-    } else {
-      // No case statement to emit
-      OS << "    return false;\n";
-    }
+    OS << "  static constexpr uint16_t " << A.Name << "_SuperClasses[] = {";
+    ListSeparator LS;
+    for (auto &SC : SuperClasses)
+      OS << LS << SC;
+    OS << "};\n";
   }
+  OS << "\n";
 
-  // If there were case statements emitted into the string stream write the
-  // default.
-  if (EmittedSwitch)
-    OS << "  }\n";
-  else
-    OS << "  return false;\n";
+  OS << "  static constexpr ArrayRef<uint16_t> SuperClassTable[] = {\n";
+  OS << "    {}, // InvalidMatchClass\n";
+  OS << "    {}, // OptionalMatchClass\n";
+  for (const auto &A : Infos)
+    OS << "    " << A.Name << "_SuperClasses,\n";
+  OS << "  };\n\n";
+
+  OS << "  ArrayRef<uint16_t> SuperClasses = SuperClassTable[(unsigned)A];\n";
+  OS << "  const uint16_t *It = lower_bound(SuperClasses, (unsigned)B);\n";
+  OS << "  return It != SuperClasses.end() && *It == (unsigned)B;\n";
 
   OS << "}\n\n";
 }

Copy link

⚠️ C/C++ code formatter, clang-format found issues in your code. ⚠️

You can test this locally with the following command:
git-clang-format --diff 294c5cb2bea88fa048e00757188749f074c5b09f 86cadae79ae9c89ca93dc0862dbbb983ca1d3085 --extensions cpp -- llvm/utils/TableGen/AsmMatcherEmitter.cpp
View the diff from clang-format here.
diff --git a/llvm/utils/TableGen/AsmMatcherEmitter.cpp b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
index 68e9b43431..df9cd93883 100644
--- a/llvm/utils/TableGen/AsmMatcherEmitter.cpp
+++ b/llvm/utils/TableGen/AsmMatcherEmitter.cpp
@@ -2551,7 +2551,8 @@ static void emitIsSubclass(CodeGenTarget &Target,
     }
 
     if (SuperClasses.empty()) {
-      OS << "  static constexpr ArrayRef<uint16_t> " << A.Name << "_SuperClasses;\n";
+      OS << "  static constexpr ArrayRef<uint16_t> " << A.Name
+         << "_SuperClasses;\n";
       continue;
     }
 


OS << " ArrayRef<uint16_t> SuperClasses = SuperClassTable[(unsigned)A];\n";
OS << " const uint16_t *It = lower_bound(SuperClasses, (unsigned)B);\n";
OS << " return It != SuperClasses.end() && *It == (unsigned)B;\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

binary_search?

Comment on lines +2573 to +2574
OS << " ArrayRef<uint16_t> SuperClasses = SuperClassTable[(unsigned)A];\n";
OS << " const uint16_t *It = lower_bound(SuperClasses, (unsigned)B);\n";
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) Define the enum as : unsigned and remove casts?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants