Skip to content

Conversation

jurahul
Copy link
Contributor

@jurahul jurahul commented Sep 3, 2025

When decoders are specialized per-bitwidth:

  • Encode the bit width of a decoder table as a ULEB128 encoded entry at the start of each decoder table.
  • Add a fatal error in decodeInstruction if the bit width for InsnType does not match the bit width encoded in the decoder table.

@jurahul jurahul force-pushed the decoder_emitter_encode_bitwidth_in_table branch from 759c69f to 61a5f82 Compare September 3, 2025 20:28
@jurahul jurahul changed the title [LLVM][MC][DecoderEmitter] Encode BitWidth at the start of the table [LLVM][MC][DecoderEmitter] Add early failure for Insn and decoder table bitwidth mismatch Sep 3, 2025
@jurahul jurahul changed the title [LLVM][MC][DecoderEmitter] Add early failure for Insn and decoder table bitwidth mismatch [LLVM][MC][DecoderEmitter] Add early failure if Insn and decoder table bitwidths mismatch Sep 3, 2025
@jurahul jurahul force-pushed the decoder_emitter_encode_bitwidth_in_table branch from 61a5f82 to d587418 Compare September 3, 2025 21:34
@jurahul jurahul marked this pull request as ready for review September 3, 2025 21:34
@jurahul jurahul requested a review from s-barannikov September 3, 2025 21:34
@jurahul jurahul requested a review from topperc September 3, 2025 21:34
@llvmbot
Copy link
Member

llvmbot commented Sep 3, 2025

@llvm/pr-subscribers-tablegen

Author: Rahul Joshi (jurahul)

Changes

When decoders are specialized per-bitwidth:

  • Encode the bit width of a decoder table as a ULEB128 encoded entry at the start of each decoder table.
  • Add an early failure at the start of the decodeInstruction if the bit width for InsnType does not match the bit width encoded in the decoder table.

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

2 Files Affected:

  • (modified) llvm/test/TableGen/DecoderEmitterBitwidthSpecialization.td (+18-4)
  • (modified) llvm/utils/TableGen/DecoderEmitter.cpp (+29-3)
diff --git a/llvm/test/TableGen/DecoderEmitterBitwidthSpecialization.td b/llvm/test/TableGen/DecoderEmitterBitwidthSpecialization.td
index b4142e983ef77..701f5ce430bc0 100644
--- a/llvm/test/TableGen/DecoderEmitterBitwidthSpecialization.td
+++ b/llvm/test/TableGen/DecoderEmitterBitwidthSpecialization.td
@@ -105,7 +105,8 @@ def Inst3 : Instruction16Bit<3> {
 // When we specialize per bitwidth, we emit 2 decodeToMCInst functions and
 // DecodeIdx is assigned per bit width.
 
-// CHECK-SPECIALIZE-NO-TABLE-LABEL:   DecoderTable8[25]
+// CHECK-SPECIALIZE-NO-TABLE-LABEL:   DecoderTable8[26]
+// CHECK-SPECIALIZE-NO-TABLE:           /* 0 */ 8, // Bitwidth 8
 // CHECK-SPECIALIZE-NO-TABLE:           DecodeIdx: 0
 // CHECK-SPECIALIZE-NO-TABLE:           DecodeIdx: 1
 // CHECK-SPECIALIZE-NO-TABLE:         };
@@ -116,7 +117,8 @@ def Inst3 : Instruction16Bit<3> {
 // CHECK-SPECIALIZE-NO-TABLE:           case 0
 // CHECK-SPECIALIZE-NO-TABLE:           case 1
 
-// CHECK-SPECIALIZE-NO-TABLE-LABEL:   DecoderTable16[25]
+// CHECK-SPECIALIZE-NO-TABLE-LABEL:   DecoderTable16[26]
+// CHECK-SPECIALIZE-NO-TABLE:           /* 0 */ 16, // Bitwidth 16
 // CHECK-SPECIALIZE-NO-TABLE:           DecodeIdx: 0
 // CHECK-SPECIALIZE-NO-TABLE:           DecodeIdx: 1
 // CHECK-SPECIALIZE-NO-TABLE:         };
@@ -127,11 +129,17 @@ def Inst3 : Instruction16Bit<3> {
 // CHECK-SPECIALIZE-NO-TABLE:           case 0
 // CHECK-SPECIALIZE-NO-TABLE:           case 1
 
+// CHECK-SPECIALIZE-NO-TABLE-LABEL:   template <typename InsnType>
+// CHECK-SPECIALIZE-NO-TABLE-NEXT:    decodeInstruction
+// CHECK-SPECIALIZE-NO-TABLE:         uint32_t BitWidth = decodeULEB128AndIncUnsafe(Ptr);
+// CHECK-SPECIALIZE-NO-TABLE-NEXT:    if (InsnBitWidth<InsnType> != BitWidth)
+
 // -----------------------------------------------------------------------------
 // Per bitwidth specialization with function table.
 
 // 8 bit deccoder table, functions, and function table.
-// CHECK-SPECIALIZE-TABLE-LABEL:    DecoderTable8[25]
+// CHECK-SPECIALIZE-TABLE-LABEL:    DecoderTable8[26]
+// CHECK-SPECIALIZE-TABLE:           /* 0 */ 8, // Bitwidth 8
 // CHECK-SPECIALIZE-TABLE:            DecodeIdx: 0
 // CHECK-SPECIALIZE-TABLE:            DecodeIdx: 1
 // CHECK-SPECIALIZE-TABLE:          };
@@ -153,7 +161,8 @@ def Inst3 : Instruction16Bit<3> {
 // CHECK-SPECIALIZE-TABLE-NEXT:     };
 
 // 16 bit deccoder table, functions, and function table.
-// CHECK-SPECIALIZE-TABLE-LABEL:    DecoderTable16[25]
+// CHECK-SPECIALIZE-TABLE-LABEL:    DecoderTable16[26]
+// CHECK-SPECIALIZE-TABLE:            /* 0 */ 16, // Bitwidth 16
 // CHECK-SPECIALIZE-TABLE:            DecodeIdx: 0
 // CHECK-SPECIALIZE-TABLE:            DecodeIdx: 1
 // CHECK-SPECIALIZE-TABLE:          };
@@ -173,3 +182,8 @@ def Inst3 : Instruction16Bit<3> {
 // CHECK-SPECIALIZE-TABLE-NEXT:       decodeFn_16bit_0,
 // CHECK-SPECIALIZE-TABLE-NEXT:       decodeFn_16bit_1,
 // CHECK-SPECIALIZE-TABLE-NEXT:     };
+
+// CHECK-SPECIALIZE-TABLE-LABEL:    template <typename InsnType>
+// CHECK-SPECIALIZE-TABLE-NEXT:     decodeInstruction
+// CHECK-SPECIALIZE-TABLE:          uint32_t BitWidth = decodeULEB128AndIncUnsafe(Ptr);
+// CHECK-SPECIALIZE-TABLE-NEXT:     if (InsnBitWidth<InsnType> != BitWidth)
diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index fcaf433918092..8479e228d38cd 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -611,7 +611,11 @@ class DecoderTableBuilder {
                       DecoderTableInfo &TableInfo)
       : Target(Target), Encodings(Encodings), TableInfo(TableInfo) {}
 
-  void buildTable(const FilterChooser &FC) const {
+  void buildTable(const FilterChooser &FC, unsigned BitWidth) const {
+    // When specializing decoders per bit width, each decoder table will begin
+    // with the bitwidth for that table.
+    if (SpecializeDecodersPerBitwidth)
+      TableInfo.Table.insertULEB128(BitWidth);
     emitTableEntries(FC);
     assert(TableInfo.FixupStack.empty() && "Fixup stack phasing error!");
   }
@@ -774,6 +778,15 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
     OS << "Skip to: " << Index;
   };
 
+  // The first entry when specializing decoders per bitwidth is the bitwidth.
+  // This will be used for early return in `decodeInstruction`.
+  if (SpecializeDecodersPerBitwidth) {
+    OS << "/* 0  */";
+    OS.PadToColumn(14);
+    emitULEB128(I, OS);
+    OS << " // Bitwidth " << BitWidth << '\n';
+  }
+
   unsigned OpcodeMask = 0;
 
   while (I != E) {
@@ -2108,9 +2121,22 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
   if (HasCheckPredicate)
     OS << "  const FeatureBitset &Bits = STI.getFeatureBits();\n";
   OS << "  using namespace llvm::MCD;\n";
+  OS << "  const uint8_t *Ptr = DecodeTable;\n";
+
+  if (SpecializeDecodersPerBitwidth) {
+    // Return early if the decoder table's bitwidth does not match `InsnType`
+    // bitwidth.
+    OS << R"(
+  uint32_t BitWidth = decodeULEB128AndIncUnsafe(Ptr);
+  if (InsnBitWidth<InsnType> != BitWidth) {
+    LLVM_DEBUG({
+      dbgs() << "Mismatch between table bitwidth and instruction bitwidth";
+    });
+    return MCDisassembler::Fail;
+  })";
+  }
 
   OS << R"(
-  const uint8_t *Ptr = DecodeTable;
   uint64_t CurFieldValue = 0;
   DecodeStatus S = MCDisassembler::Success;
   while (true) {
@@ -2554,7 +2580,7 @@ template <typename T> constexpr uint32_t InsnBitWidth = 0;
       //    across all decoder tables.
       //  - predicates are shared across all decoder tables.
       TableInfo.Table.clear();
-      TableBuilder.buildTable(FC);
+      TableBuilder.buildTable(FC, BitWidth);
 
       // Print the table to the output stream.
       OpcodeMask |= emitTable(OS, TableInfo.Table, DecoderNamespace, HwModeID,

@jurahul jurahul changed the title [LLVM][MC][DecoderEmitter] Add early failure if Insn and decoder table bitwidths mismatch [LLVM][MC][DecoderEmitter] Fail fatally if Insn and decoder table bitwidths mismatch Sep 3, 2025
@jurahul jurahul requested a review from s-barannikov September 3, 2025 21:50
@jurahul jurahul force-pushed the decoder_emitter_encode_bitwidth_in_table branch from 7028f48 to 34be441 Compare September 4, 2025 00:38
@jurahul jurahul merged commit c8e760e into llvm:main Sep 4, 2025
9 checks passed
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