[llvm] [NFC][InstrInfoEmitter] Include location of inst definition in comment (PR #156927)

Rahul Joshi via llvm-commits llvm-commits at lists.llvm.org
Thu Sep 4 09:50:56 PDT 2025


https://github.com/jurahul created https://github.com/llvm/llvm-project/pull/156927

Print the source location of the instruction definition in comment next to the enum value for each instruction. To make this more readable, change formatting of the instruction enums to be better aligned.

Example output:

```
    VLD4qWB_register_Asm_8                 = 573, // (ARMInstrNEON.td:8849)
    VMOVD0                                 = 574, // (ARMInstrNEON.td:6337)
    VMOVDcc                                = 575, // (ARMInstrVFP.td:2466)
    VMOVHcc                                = 576, // (ARMInstrVFP.td:2474)
    VMOVQ0                                 = 577, // (ARMInstrNEON.td:6341)
```

>From 9a5b6391133b0423d603a268ff0c4fbc8d4c100d Mon Sep 17 00:00:00 2001
From: Rahul Joshi <rjoshi at nvidia.com>
Date: Thu, 4 Sep 2025 09:46:40 -0700
Subject: [PATCH] [NFC][InstrInfoEmitter] Include location of inst definition
 in comment

Print the source location of the instruction definition in comment
next to the enum value for each instruction. To make this more
readable, change formatting of the instruction enums to be better
aligned.
---
 .../TableGen/Common/CodeGenInstruction.h      |  2 ++
 llvm/utils/TableGen/InstrInfoEmitter.cpp      | 22 +++++++++++++++----
 2 files changed, 20 insertions(+), 4 deletions(-)

diff --git a/llvm/utils/TableGen/Common/CodeGenInstruction.h b/llvm/utils/TableGen/Common/CodeGenInstruction.h
index 9372614f26e1a..ed0bfa7098eb7 100644
--- a/llvm/utils/TableGen/Common/CodeGenInstruction.h
+++ b/llvm/utils/TableGen/Common/CodeGenInstruction.h
@@ -320,6 +320,8 @@ class CodeGenInstruction {
     return RV && isa<DagInit>(RV->getValue());
   }
 
+  StringRef getName() const { return TheDef->getName(); }
+
 private:
   bool isOperandImpl(StringRef OpListName, unsigned i,
                      StringRef PropertyName) const;
diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp
index 26d93fc13c9ba..5bfde1c0b5d89 100644
--- a/llvm/utils/TableGen/InstrInfoEmitter.cpp
+++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp
@@ -25,6 +25,8 @@
 #include "llvm/ADT/SmallVector.h"
 #include "llvm/ADT/StringExtras.h"
 #include "llvm/Support/Casting.h"
+#include "llvm/Support/FormattedStream.h"
+#include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/raw_ostream.h"
 #include "llvm/TableGen/Error.h"
 #include "llvm/TableGen/Record.h"
@@ -1289,8 +1291,9 @@ void InstrInfoEmitter::emitRecord(
 
 // emitEnums - Print out enum values for all of the instructions.
 void InstrInfoEmitter::emitEnums(
-    raw_ostream &OS,
+    raw_ostream &RawOS,
     ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
+  formatted_raw_ostream OS(RawOS);
   OS << "#ifdef GET_INSTRINFO_ENUM\n";
   OS << "#undef GET_INSTRINFO_ENUM\n";
 
@@ -1302,10 +1305,21 @@ void InstrInfoEmitter::emitEnums(
 
   OS << "namespace llvm::" << Namespace << " {\n";
 
+  auto II = llvm::max_element(
+      NumberedInstructions,
+      [](const CodeGenInstruction *InstA, const CodeGenInstruction *InstB) {
+        return InstA->getName().size() < InstB->getName().size();
+      });
+  size_t MaxNameSize = (*II)->getName().size();
+
   OS << "  enum {\n";
-  for (const CodeGenInstruction *Inst : NumberedInstructions)
-    OS << "    " << Inst->TheDef->getName()
-       << "\t= " << Target.getInstrIntValue(Inst->TheDef) << ",\n";
+  for (const CodeGenInstruction *Inst : NumberedInstructions) {
+    OS << "    " << Inst->TheDef->getName();
+    OS.PadToColumn(MaxNameSize + 5);
+    OS << " = " << Target.getInstrIntValue(Inst->TheDef) << ", // (";
+    OS << SrcMgr.getFormattedLocationNoOffset(Inst->TheDef->getLoc().front())
+       << ")\n";
+  }
   OS << "    INSTRUCTION_LIST_END = " << NumberedInstructions.size() << '\n';
   OS << "  };\n\n";
   OS << "} // end namespace llvm::" << Namespace << '\n';



More information about the llvm-commits mailing list