[llvm] [NFC][DecoderEmitter] Code cleanup in `DecoderEmitter::emitTable` (PR #158014)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 11 09:35:34 PDT 2025


================
@@ -775,141 +795,153 @@ unsigned DecoderEmitter::emitTable(formatted_raw_ostream &OS,
   DecoderTable::const_iterator E = Table.end();
   const uint8_t *const EndPtr = Table.data() + Table.size();
 
+  constexpr uint32_t StartColumn = 14;
+
+  auto StartComment = [&OS]() {
+    constexpr uint32_t CommentColumn = 60;
+    OS.PadToColumn(CommentColumn);
+    OS << " // ";
+  };
+
   auto emitNumToSkipComment = [&](uint32_t NumToSkip, bool InComment = false) {
     uint32_t Index = ((I - Table.begin()) + NumToSkip);
-    OS << (InComment ? ", " : "// ");
+    if (InComment)
+      OS << ", ";
+    else
+      StartComment();
     OS << "Skip to: " << Index;
   };
 
   // The first entry when specializing decoders per bitwidth is the bitwidth.
   // This will be used for additional checks in `decodeInstruction`.
   if (SpecializeDecodersPerBitwidth) {
     OS << "/* 0  */";
-    OS.PadToColumn(14);
+    OS.PadToColumn(StartColumn);
     emitULEB128(I, OS);
     OS << " // Bitwidth " << BitWidth << '\n';
   }
 
+  auto DecodeAndEmitULEB128 = [EndPtr,
+                               &emitULEB128](DecoderTable::const_iterator &I,
+                                             formatted_raw_ostream &OS) {
+    const char *ErrMsg = nullptr;
+    uint64_t Value = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
+    assert(ErrMsg == nullptr && "ULEB128 value too large!");
+
+    emitULEB128(I, OS);
+    return Value;
+  };
+
   unsigned OpcodeMask = 0;
 
   while (I != E) {
     assert(I < E && "incomplete decode table entry!");
 
     uint64_t Pos = I - Table.begin();
     OS << "/* " << Pos << " */";
-    OS.PadToColumn(12);
+    OS.PadToColumn(StartColumn);
 
     const uint8_t DecoderOp = *I++;
     OpcodeMask |= (1 << DecoderOp);
+    OS << "MCD::" << getDecoderOpName(DecoderOp) << ", ";
     switch (DecoderOp) {
     default:
       PrintFatalError("Invalid decode table opcode: " + Twine((int)DecoderOp) +
                       " at index " + Twine(Pos));
     case MCD::OPC_Scope: {
-      OS << "  MCD::OPC_Scope, ";
       uint32_t NumToSkip = emitNumToSkip(I, OS);
       emitNumToSkipComment(NumToSkip);
-      OS << '\n';
       break;
     }
     case MCD::OPC_ExtractField: {
-      OS << "  MCD::OPC_ExtractField, ";
-
       // ULEB128 encoded start value.
-      const char *ErrMsg = nullptr;
-      unsigned Start = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
-      assert(ErrMsg == nullptr && "ULEB128 value too large!");
-      emitULEB128(I, OS);
-
+      unsigned Start = DecodeAndEmitULEB128(I, OS);
       unsigned Len = *I++;
-      OS << Len << ",  // Inst{";
+      OS << Len << ',';
+      StartComment();
+      OS << "Inst{";
----------------
jurahul wrote:

How about this?

```
static const uint8_t DecoderTable32[106857] = {
/* 0 */       MCD::OPC_ExtractField, 26, 3,                  // CurFieldVal = Inst{28-26}
/* 3 */       MCD::OPC_FilterValueOrSkip, 0, 51, 74,         // if (CurFieldVal != 0x0) Skip to 19002
/* 7 */       MCD::OPC_ExtractField, 29, 3,                  // CurFieldVal = Inst{31-29}
/* 10 */      MCD::OPC_FilterValueOrSkip, 0, 8, 0,           // if (CurFieldVal != 0x0) Skip to 22
/* 14 */      MCD::OPC_CheckField, 16, 10, 0,                // if (Inst{25-16} != 0x0) pop_scope()
/* 18 */      MCD::OPC_Decode, 172, 62, 0,                   // Opcode: UDF, DecodeIdx: 0
/* 22 */      MCD::OPC_FilterValueOrSkip, 4, 135, 7,         // if (CurFieldVal != 0x4) Skip to 1953
/* 26 */      MCD::OPC_ExtractField, 21, 5,                  // CurFieldVal = Inst{25-21}
/* 29 */      MCD::OPC_FilterValueOrSkip, 0, 65, 1,          // if (CurFieldVal != 0x0) Skip to 354
/* 33 */      MCD::OPC_ExtractField, 2, 4,                   // CurFieldVal = Inst{5-2}
/* 36 */      MCD::OPC_FilterValueOrSkip, 0, 101, 0,         // if (CurFieldVal != 0x0) Skip to 141
/* 40 */      MCD::OPC_ExtractField, 9, 8,                   // CurFieldVal = Inst{16-9}
/* 43 */      MCD::OPC_FilterValueOrSkip, 0, 21, 0,          // if (CurFieldVal != 0x0) Skip to 68
/* 47 */      MCD::OPC_ExtractField, 20, 1,                  // CurFieldVal = Inst{20}
/* 50 */      MCD::OPC_FilterValueOrSkip, 0, 6, 0,           // if (CurFieldVal != 0x0) Skip to 60
```

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


More information about the llvm-commits mailing list