From 3c9af320ed16deb22f010a6e731d0a458bbe2e23 Mon Sep 17 00:00:00 2001 From: Rahul Joshi Date: Thu, 25 Sep 2025 16:56:03 -0700 Subject: [PATCH] [NFC][TableGen] Print topmost def location for Intrinsics/Insts When printing location for an intrinsic or an instruction in the generated file, print the location of the topmost/outermost defm. The intent of this location is to easily trace the origin of that particular record, and currently we print the location of the innermost def for records defined using defm/multiclasses, which is not that useful. Printing the location of the topmost defm can allow someone to drill down the defm hierarchy. --- llvm/include/llvm/TableGen/Record.h | 6 ++++++ llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp | 2 +- llvm/utils/TableGen/InstrInfoEmitter.cpp | 2 +- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/llvm/include/llvm/TableGen/Record.h b/llvm/include/llvm/TableGen/Record.h index d4fa1e5d65749..18aa313a88500 100644 --- a/llvm/include/llvm/TableGen/Record.h +++ b/llvm/include/llvm/TableGen/Record.h @@ -1720,6 +1720,12 @@ class Record { ArrayRef getLoc() const { return Locs; } void appendLoc(SMLoc Loc) { Locs.push_back(Loc); } + // Returns the location of the "top" def or defm that instantiated this + // concrete record. For a record defined using `def`, this is the location of + // the def. For a record defined using `defm`, this is the location of the + // topmost/outermost defm that lead to the instantiation of this record. + SMLoc getTopDefLoc() const { return Locs.back(); } + ArrayRef getForwardDeclarationLocs() const { return ForwardDeclarationLocs; } diff --git a/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp b/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp index 559868dd54efe..1ae035434d9e5 100644 --- a/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp +++ b/llvm/utils/TableGen/Basic/IntrinsicEmitter.cpp @@ -170,7 +170,7 @@ void IntrinsicEmitter::EmitEnumInfo(const CodeGenIntrinsicTable &Ints, OS.indent(40 - Int.EnumName.size()); OS << formatv( " // {} ({})\n", Int.Name, - SrcMgr.getFormattedLocationNoOffset(Int.TheDef->getLoc().front())); + SrcMgr.getFormattedLocationNoOffset(Int.TheDef->getTopDefLoc())); } // Emit num_intrinsics into the target neutral enum. diff --git a/llvm/utils/TableGen/InstrInfoEmitter.cpp b/llvm/utils/TableGen/InstrInfoEmitter.cpp index 176e4b250b82a..9b92aeb082dbc 100644 --- a/llvm/utils/TableGen/InstrInfoEmitter.cpp +++ b/llvm/utils/TableGen/InstrInfoEmitter.cpp @@ -1389,7 +1389,7 @@ void InstrInfoEmitter::emitEnums( for (const CodeGenInstruction *Inst : NumberedInstructions) { OS << " " << left_justify(Inst->getName(), MaxNameSize) << " = " << Target.getInstrIntValue(Inst->TheDef) << ", // " - << SrcMgr.getFormattedLocationNoOffset(Inst->TheDef->getLoc().front()) + << SrcMgr.getFormattedLocationNoOffset(Inst->TheDef->getTopDefLoc()) << '\n'; } OS << " INSTRUCTION_LIST_END = " << NumberedInstructions.size() << '\n';