[llvm] [LLVM][DecoderEmitter] Add option to use lambdas in decodeToMCInst (PR #144814)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 20 09:05:33 PDT 2025


================
@@ -1082,15 +1089,47 @@ void DecoderEmitter::emitDecoderFunction(formatted_raw_ostream &OS,
      << "using TmpType = "
         "std::conditional_t<std::is_integral<InsnType>::"
         "value, InsnType, uint64_t>;\n";
-  OS << Indent << "TmpType tmp;\n";
-  OS << Indent << "switch (Idx) {\n";
-  OS << Indent << "default: llvm_unreachable(\"Invalid index!\");\n";
-  for (const auto &[Index, Decoder] : enumerate(Decoders)) {
-    OS << Indent << "case " << Index << ":\n";
-    OS << Decoder;
-    OS << Indent + 2 << "return S;\n";
+
+  if (UseLambdaInDecodetoMCInst) {
+    // Emit one lambda for each case first.
+    for (const auto &[Index, Decoder] : enumerate(Decoders)) {
+      OS << Indent << "auto decodeLambda" << Index << " = [](DecodeStatus S,\n"
----------------
jurahul wrote:

I think I can do that, I initially had them as capturing everything and discovered that that does not work when I make the table static (I am speculating it's because with the static table, the table is initialized using a lambda which captures local state during the first invocation and then it keep using that stale local state in later invocations). So I changed them to pure-lamba which fixed the test failures. But yeah, with that I can make them static functions and also likely change the table to a constexpr instead of just const.

https://github.com/llvm/llvm-project/pull/144814


More information about the llvm-commits mailing list