[llvm] [NFC][TableGen] Print DecodeIdx for DecodeOps in DecoderEmitter (PR #142963)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Thu Jun 5 08:05:02 PDT 2025


https://github.com/jurahul updated https://github.com/llvm/llvm-project/pull/142963

>From 33a90f7533a88746243abfe5cb2dc3a2afa96762 Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Thu, 5 Jun 2025 06:30:17 -0700
Subject: [PATCH] [NFC][TableGen] Print DecodeIdx for DecodeOps in
 DecoderEmitter

Print DecodeIdx associated with Decode MCD ops in the generated
decoder tables. This can help in debugging decode failures by
first mapping the Op -> DecodeIdx and then inspecting the code
in `decodeToMCInst` associated with that DecodeIdx.
---
 llvm/utils/TableGen/DecoderEmitter.cpp | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)

diff --git a/llvm/utils/TableGen/DecoderEmitter.cpp b/llvm/utils/TableGen/DecoderEmitter.cpp
index 3990836e9077f..c81b3c9201cf1 100644
--- a/llvm/utils/TableGen/DecoderEmitter.cpp
+++ b/llvm/utils/TableGen/DecoderEmitter.cpp
@@ -975,25 +975,26 @@ void DecoderEmitter::emitTable(formatted_raw_ostream &OS, DecoderTable &Table,
       emitULEB128(I, OS);
 
       // Decoder index.
+      unsigned DecodeIdx = decodeULEB128(&*I, nullptr, EndPtr, &ErrMsg);
+      assert(ErrMsg == nullptr && "ULEB128 value too large!");
       emitULEB128(I, OS);
 
       auto EncI = OpcodeToEncodingID.find(Opc);
       assert(EncI != OpcodeToEncodingID.end() && "no encoding entry");
       auto EncodingID = EncI->second;
 
+      OS << "// Opcode: " << NumberedEncodings[EncodingID]
+         << ", DecodeIdx: " << DecodeIdx;
+
       if (!IsTry) {
-        OS << "// Opcode: " << NumberedEncodings[EncodingID] << "\n";
+        OS << '\n';
         break;
       }
 
       // Fallthrough for OPC_TryDecode.
-
       if (!IsFail) {
         uint32_t NumToSkip = emitNumToSkip(I, OS);
-        OS << "// Opcode: " << NumberedEncodings[EncodingID];
         emitNumToSkipComment(NumToSkip, /*InComment=*/true);
-      } else {
-        OS << "// Opcode: " << NumberedEncodings[EncodingID];
       }
       OS << '\n';
       break;
@@ -2249,7 +2250,8 @@ static DecodeStatus decodeInstruction(const uint8_t DecodeTable[], MCInst &MI,
     const uint8_t DecoderOp = *Ptr++;
     switch (DecoderOp) {
     default:
-      errs() << Loc << ": Unexpected decode table opcode!\n";
+      errs() << Loc << ": Unexpected decode table opcode: "
+             << DecoderOp << '\n';
       return MCDisassembler::Fail;
     case MCD::OPC_ExtractField: {
       // Decode the start value.



More information about the llvm-commits mailing list