Skip to content

Commit bd041cf

Browse files
committed
[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.
1 parent 65f60fd commit bd041cf

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

llvm/utils/TableGen/Common/CodeGenInstruction.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,8 @@ class CodeGenInstruction {
320320
return RV && isa<DagInit>(RV->getValue());
321321
}
322322

323+
StringRef getName() const { return TheDef->getName(); }
324+
323325
private:
324326
bool isOperandImpl(StringRef OpListName, unsigned i,
325327
StringRef PropertyName) const;

llvm/utils/TableGen/InstrInfoEmitter.cpp

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include "llvm/ADT/SmallVector.h"
2626
#include "llvm/ADT/StringExtras.h"
2727
#include "llvm/Support/Casting.h"
28+
#include "llvm/Support/FormattedStream.h"
29+
#include "llvm/Support/SourceMgr.h"
2830
#include "llvm/Support/raw_ostream.h"
2931
#include "llvm/TableGen/Error.h"
3032
#include "llvm/TableGen/Record.h"
@@ -1289,8 +1291,9 @@ void InstrInfoEmitter::emitRecord(
12891291

12901292
// emitEnums - Print out enum values for all of the instructions.
12911293
void InstrInfoEmitter::emitEnums(
1292-
raw_ostream &OS,
1294+
raw_ostream &RawOS,
12931295
ArrayRef<const CodeGenInstruction *> NumberedInstructions) {
1296+
formatted_raw_ostream OS(RawOS);
12941297
OS << "#ifdef GET_INSTRINFO_ENUM\n";
12951298
OS << "#undef GET_INSTRINFO_ENUM\n";
12961299

@@ -1302,18 +1305,29 @@ void InstrInfoEmitter::emitEnums(
13021305

13031306
OS << "namespace llvm::" << Namespace << " {\n";
13041307

1308+
auto II = llvm::max_element(
1309+
NumberedInstructions,
1310+
[](const CodeGenInstruction *InstA, const CodeGenInstruction *InstB) {
1311+
return InstA->getName().size() < InstB->getName().size();
1312+
});
1313+
size_t MaxNameSize = (*II)->getName().size();
1314+
13051315
OS << " enum {\n";
1306-
for (const CodeGenInstruction *Inst : NumberedInstructions)
1307-
OS << " " << Inst->TheDef->getName()
1308-
<< "\t= " << Target.getInstrIntValue(Inst->TheDef) << ",\n";
1316+
for (const CodeGenInstruction *Inst : NumberedInstructions) {
1317+
OS << " " << Inst->TheDef->getName();
1318+
OS.PadToColumn(MaxNameSize + 5);
1319+
OS << " = " << Target.getInstrIntValue(Inst->TheDef) << ", // (";
1320+
OS << SrcMgr.getFormattedLocationNoOffset(Inst->TheDef->getLoc().front())
1321+
<< ")\n";
1322+
}
13091323
OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << '\n';
1310-
OS << " };\n\n";
1324+
OS << " };\n";
13111325
OS << "} // end namespace llvm::" << Namespace << '\n';
13121326
OS << "#endif // GET_INSTRINFO_ENUM\n\n";
13131327

13141328
OS << "#ifdef GET_INSTRINFO_SCHED_ENUM\n";
13151329
OS << "#undef GET_INSTRINFO_SCHED_ENUM\n";
1316-
OS << "namespace llvm::" << Namespace << "::Sched {\n\n";
1330+
OS << "namespace llvm::" << Namespace << "::Sched {\n";
13171331
OS << " enum {\n";
13181332
auto ExplictClasses = SchedModels.explicitSchedClasses();
13191333
for (const auto &[Idx, Class] : enumerate(ExplictClasses))

0 commit comments

Comments
 (0)