[llvm] [NFCI][TableGen][DecoderEmitter] Cull `TryDecode` handling when possible (PR #142974)

via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 5 14:26:22 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

<details>
<summary>Changes</summary>

`TryDecode` MCD ops are not used by many targets (only AArch64 seems to generate it). Track whether any `TryDecode` or `TryDecodeOrFail` ops are generated and emit the code for handling them only in that case.

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


1 Files Affected:

- (modified) llvm/utils/TableGen/DecoderEmitter.cpp (+21-10) 


``````````diff
diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 3990836e9077f..ec12eab8ac4ca 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -223,8 +223,9 @@ class DecoderEmitter {
   DecoderEmitter(const RecordKeeper &R, StringRef PredicateNamespace)
       : RK(R), Target(R), PredicateNamespace(PredicateNamespace) {}
 
-  // Emit the decoder state machine table.
-  void emitTable(formatted_raw_ostream &OS, DecoderTable &Table, indent Indent,
+  // Emit the decoder state machine table. Return true if any `TryDecode` ops
+  // were generated.
+  bool emitTable(formatted_raw_ostream &OS, DecoderTable &Table, indent Indent,
                  unsigned BitWidth, StringRef Namespace,
                  const EncodingIDsVec &EncodingIDs) const;
   void emitInstrLenTable(formatted_raw_ostream &OS,
@@ -828,7 +829,7 @@ unsigned Filter::usefulness() const {
 //////////////////////////////////
 
 // Emit the decoder state machine table.
-void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
+bool DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
                                indent Indent, unsigned BitWidth,
                                StringRef Namespace,
                                const EncodingIDsVec &EncodingIDs) const {
@@ -885,6 +886,8 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
       OS << " (Fail)";
   };
 
+  bool HasTryDecode = false;
+
   while (I != E) {
     assert(I < E && "incomplete decode table entry!");
 
@@ -965,6 +968,7 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
     case MCD::OPC_TryDecodeOrFail: {
       bool IsFail = DecoderOp == MCD::OPC_TryDecodeOrFail;
       bool IsTry = DecoderOp == MCD::OPC_TryDecode || IsFail;
+      HasTryDecode |= IsTry;
       // Decode the Opcode value.
       const char *ErrMsg = nullptr;
       unsigned Opc = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
@@ -1027,6 +1031,8 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
   Indent -= 2;
 
   OS << Indent << "};\n\n";
+
+  return HasTryDecode;
 }
 
 void DecoderEmitter::emitInstrLenTable(formatted_raw_ostream &OS,
@@ -2217,8 +2223,8 @@ static void insertBits(InsnType &field, InsnType bits, unsigned startBit,
 
 // emitDecodeInstruction - Emit the templated helper function
 // decodeInstruction().
-static void emitDecodeInstruction(formatted_raw_ostream &OS,
-                                  bool IsVarLenInst) {
+static void emitDecodeInstruction(formatted_raw_ostream &OS, bool IsVarLenInst,
+                                  bool HasTryDecode) {
   OS << R"(
 static unsigned decodeNumToSkip(const uint8_t *&Ptr) {
   unsigned NumToSkip = *Ptr++;
@@ -2363,7 +2369,9 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
                    << ", using decoder " << DecodeIdx << ": "
                    << (S != MCDisassembler::Fail ? "PASS\n" : "FAIL\n"));
       return S;
-    }
+    })";
+  if (HasTryDecode) {
+    OS << R"(
     case MCD::OPC_TryDecode:
     case MCD::OPC_TryDecodeOrFail: {
       bool IsFail = DecoderOp == MCD::OPC_TryDecodeOrFail;
@@ -2398,7 +2406,9 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
       // set before the decode attempt.
       S = MCDisassembler::Success;
       break;
-    }
+    })";
+  }
+  OS << R"(
     case MCD::OPC_SoftFail: {
       // Decode the mask values.
       uint64_t PositiveMask = decodeULEB128AndIncUnsafe(Ptr);
@@ -2608,6 +2618,7 @@ namespace {
   }
 
   DecoderTableInfo TableInfo;
+  bool HasTryDecode = false;
   for (const auto &Opc : OpcMap) {
     // Emit the decoder for this namespace+width combination.
     ArrayRef<EncodingAndInst> NumberedEncodingsRef(NumberedEncodings.data(),
@@ -2633,8 +2644,8 @@ namespace {
     TableInfo.Table.push_back(MCD::OPC_Fail);
 
     // Print the table to the output stream.
-    emitTable(OS, TableInfo.Table, indent(0), FC.getBitWidth(), Opc.first.first,
-              Opc.second);
+    HasTryDecode |= emitTable(OS, TableInfo.Table, indent(0), FC.getBitWidth(),
+                              Opc.first.first, Opc.second);
   }
 
   // For variable instruction, we emit a instruction length table
@@ -2649,7 +2660,7 @@ namespace {
   emitDecoderFunction(OS, TableInfo.Decoders, indent(0));
 
   // Emit the main entry point for the decoder, decodeInstruction().
-  emitDecodeInstruction(OS, IsVarLenInst);
+  emitDecodeInstruction(OS, IsVarLenInst, HasTryDecode);
 
   OS << "\n} // namespace\n";
 }

``````````

</details>


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


More information about the llvm-commits mailing list