From bd041cf0c0b86c2f28df8bebf3076214d38e9926 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Thu, 4 Sep 2025 09:46:40 -0700 Subject: [PATCH 1/3] [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 | 26 ++++++++++++++----- 2 files changed, 22 insertions(+), 6 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(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..afbd64e4c7012 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 NumberedInstructions) { + formatted_raw_ostream OS(RawOS); OS << "#ifdef GET_INSTRINFO_ENUM\n"; OS << "#undef GET_INSTRINFO_ENUM\n"; @@ -1302,18 +1305,29 @@ 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 << " };\n"; OS << "} // end namespace llvm::" << Namespace << '\n'; OS << "#endif // GET_INSTRINFO_ENUM\n\n"; OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n"; OS << "#undef GET_INSTRINFO_SCHED_ENUM\n"; - OS << "namespace llvm::" << Namespace << "::Sched {\n\n"; + OS << "namespace llvm::" << Namespace << "::Sched {\n"; OS << " enum {\n"; auto ExplictClasses = SchedModels.explicitSchedClasses(); for (const auto &[Idx, Class] : enumerate(ExplictClasses)) From 8faca4a9c6841fbb4a2af1ee7257fcacc2bec065 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Thu, 4 Sep 2025 13:09:32 -0700 Subject: [PATCH 2/3] Review feedback, drop () --- llvm/utils/TableGen/InstrInfoEmitter.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp index afbd64e4c7012..fb3280b948e64 100644 --- a/llvm/utils/TableGen/InstrInfoEmitter.cpp +++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp @@ -1316,9 +1316,9 @@ void InstrInfoEmitter::emitEnums( 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 << " = " << Target.getInstrIntValue(Inst->TheDef) << ", // "; + OS << SrcMgr.getFormattedLocationNoOffset(Inst->TheDef->getLoc().front()); + OS << '\n'; } OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << '\n'; OS << " };\n"; From 619151bdbee8007558932753d6410be751cc67f2 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Thu, 4 Sep 2025 14:33:08 -0700 Subject: [PATCH 3/3] Review feedback: use left_justify --- llvm/utils/TableGen/InstrInfoEmitter.cpp | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp index fb3280b948e64..6b7c78a314448 100644 --- a/llvm/utils/TableGen/InstrInfoEmitter.cpp +++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp @@ -25,7 +25,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/Support/Casting.h" -#include "llvm/Support/FormattedStream.h" +#include "llvm/Support/Format.h" #include "llvm/Support/SourceMgr.h" #include "llvm/Support/raw_ostream.h" #include "llvm/TableGen/Error.h" @@ -1291,9 +1291,8 @@ void InstrInfoEmitter::emitRecord( // emitEnums - Print out enum values for all of the instructions. void InstrInfoEmitter::emitEnums( - raw_ostream &RawOS, + raw_ostream &OS, ArrayRef NumberedInstructions) { - formatted_raw_ostream OS(RawOS); OS << "#ifdef GET_INSTRINFO_ENUM\n"; OS << "#undef GET_INSTRINFO_ENUM\n"; @@ -1314,11 +1313,10 @@ void InstrInfoEmitter::emitEnums( OS << " enum {\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()); - OS << '\n'; + OS << " " << left_justify(Inst->TheDef->getName(), MaxNameSize) << " = " + << Target.getInstrIntValue(Inst->TheDef) << ", // " + << SrcMgr.getFormattedLocationNoOffset(Inst->TheDef->getLoc().front()) + << '\n'; } OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << '\n'; OS << " };\n";