Skip to content

[KeyInstr] Add fields to DILocation behind compile time flag #133477

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 37 additions & 10 deletions llvm/include/llvm/IR/DebugInfoMetadata.h
Original file line number Diff line number Diff line change
Expand Up @@ -2235,44 +2235,70 @@ class DISubprogram : public DILocalScope {
class DILocation : public MDNode {
friend class LLVMContextImpl;
friend class MDNode;
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
uint64_t AtomGroup : 61;
uint8_t AtomRank : 3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You need to use uint64_t for both to get actual bit packing with msvc (https://c.godbolt.org/z/1f556c1zb).

#endif

DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
unsigned Column, ArrayRef<Metadata *> MDs, bool ImplicitCode);
unsigned Column, uint64_t AtomGroup, uint8_t AtomRank,
ArrayRef<Metadata *> MDs, bool ImplicitCode);
~DILocation() { dropAllReferences(); }

static DILocation *getImpl(LLVMContext &Context, unsigned Line,
unsigned Column, Metadata *Scope,
Metadata *InlinedAt, bool ImplicitCode,
uint64_t AtomGroup, uint8_t AtomRank,
StorageType Storage, bool ShouldCreate = true);
static DILocation *getImpl(LLVMContext &Context, unsigned Line,
unsigned Column, DILocalScope *Scope,
DILocation *InlinedAt, bool ImplicitCode,
uint64_t AtomGroup, uint8_t AtomRank,
StorageType Storage, bool ShouldCreate = true) {
return getImpl(Context, Line, Column, static_cast<Metadata *>(Scope),
static_cast<Metadata *>(InlinedAt), ImplicitCode, Storage,
ShouldCreate);
static_cast<Metadata *>(InlinedAt), ImplicitCode, AtomGroup,
AtomRank, Storage, ShouldCreate);
}

TempDILocation cloneImpl() const {
// Get the raw scope/inlinedAt since it is possible to invoke this on
// a DILocation containing temporary metadata.
return getTemporary(getContext(), getLine(), getColumn(), getRawScope(),
getRawInlinedAt(), isImplicitCode());
getRawInlinedAt(), isImplicitCode(), getAtomGroup(),
getAtomRank());
}

public:
uint64_t getAtomGroup() const {
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
return AtomGroup;
#else
return 0;
#endif
}
uint8_t getAtomRank() const {
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
return AtomRank;
#else
return 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto above.

#endif
}

// Disallow replacing operands.
void replaceOperandWith(unsigned I, Metadata *New) = delete;

DEFINE_MDNODE_GET(DILocation,
(unsigned Line, unsigned Column, Metadata *Scope,
Metadata *InlinedAt = nullptr, bool ImplicitCode = false),
(Line, Column, Scope, InlinedAt, ImplicitCode))
Metadata *InlinedAt = nullptr, bool ImplicitCode = false,
uint64_t AtomGroup = 0, uint8_t AtomRank = 0),
(Line, Column, Scope, InlinedAt, ImplicitCode, AtomGroup,
AtomRank))
DEFINE_MDNODE_GET(DILocation,
(unsigned Line, unsigned Column, DILocalScope *Scope,
DILocation *InlinedAt = nullptr,
bool ImplicitCode = false),
(Line, Column, Scope, InlinedAt, ImplicitCode))
DILocation *InlinedAt = nullptr, bool ImplicitCode = false,
uint64_t AtomGroup = 0, uint8_t AtomRank = 0),
(Line, Column, Scope, InlinedAt, ImplicitCode, AtomGroup,
AtomRank))

/// Return a (temporary) clone of this.
TempDILocation clone() const { return cloneImpl(); }
Expand Down Expand Up @@ -2647,7 +2673,8 @@ DILocation::cloneWithDiscriminator(unsigned Discriminator) const {
DILexicalBlockFile *NewScope =
DILexicalBlockFile::get(getContext(), Scope, getFile(), Discriminator);
return DILocation::get(getContext(), getLine(), getColumn(), NewScope,
getInlinedAt());
getInlinedAt(), isImplicitCode(), getAtomGroup(),
getAtomRank());
}

unsigned DILocation::getBaseDiscriminator() const {
Expand Down
12 changes: 7 additions & 5 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5319,20 +5319,22 @@ bool LLParser::parseSpecializedMDNode(MDNode *&N, bool IsDistinct) {

/// parseDILocationFields:
/// ::= !DILocation(line: 43, column: 8, scope: !5, inlinedAt: !6,
/// isImplicitCode: true)
/// isImplicitCode: true, atomGroup: 1, atomRank: 1)
bool LLParser::parseDILocation(MDNode *&Result, bool IsDistinct) {
#define VISIT_MD_FIELDS(OPTIONAL, REQUIRED) \
OPTIONAL(line, LineField, ); \
OPTIONAL(column, ColumnField, ); \
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't comment on the comment 6 lines above, but it'll want updating.

REQUIRED(scope, MDField, (/* AllowNull */ false)); \
OPTIONAL(inlinedAt, MDField, ); \
OPTIONAL(isImplicitCode, MDBoolField, (false));
OPTIONAL(isImplicitCode, MDBoolField, (false)); \
OPTIONAL(atomGroup, MDUnsignedField, (0, UINT64_MAX)); \
OPTIONAL(atomRank, MDUnsignedField, (0, UINT8_MAX));
PARSE_MD_FIELDS();
#undef VISIT_MD_FIELDS

Result =
GET_OR_DISTINCT(DILocation, (Context, line.Val, column.Val, scope.Val,
inlinedAt.Val, isImplicitCode.Val));
Result = GET_OR_DISTINCT(
DILocation, (Context, line.Val, column.Val, scope.Val, inlinedAt.Val,
isImplicitCode.Val, atomGroup.Val, atomRank.Val));
return false;
}

Expand Down
2 changes: 2 additions & 0 deletions llvm/lib/IR/AsmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2103,6 +2103,8 @@ static void writeDILocation(raw_ostream &Out, const DILocation *DL,
Printer.printMetadata("inlinedAt", DL->getRawInlinedAt());
Printer.printBool("isImplicitCode", DL->isImplicitCode(),
/* Default */ false);
Printer.printInt("atomGroup", DL->getAtomGroup());
Printer.printInt<unsigned>("atomRank", DL->getAtomRank());
Out << ")";
}

Expand Down
24 changes: 17 additions & 7 deletions llvm/lib/IR/DebugInfoMetadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,19 @@ DebugVariableAggregate::DebugVariableAggregate(const DbgVariableIntrinsic *DVI)
DVI->getDebugLoc()->getInlinedAt()) {}

DILocation::DILocation(LLVMContext &C, StorageType Storage, unsigned Line,
unsigned Column, ArrayRef<Metadata *> MDs,
bool ImplicitCode)
: MDNode(C, DILocationKind, Storage, MDs) {
unsigned Column, uint64_t AtomGroup, uint8_t AtomRank,
ArrayRef<Metadata *> MDs, bool ImplicitCode)
: MDNode(C, DILocationKind, Storage, MDs)
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
,
AtomGroup(AtomGroup), AtomRank(AtomRank)
#endif
{
#ifdef EXPERIMENTAL_KEY_INSTRUCTIONS
assert(AtomRank <= 7 && "AtomRank number should fit in 3 bits");
#endif
assert((MDs.size() == 1 || MDs.size() == 2) &&
"Expected a scope and optional inlined-at");

// Set line and column.
assert(Column < (1u << 16) && "Expected 16-bit column");

Expand All @@ -87,14 +94,16 @@ static void adjustColumn(unsigned &Column) {
DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line,
unsigned Column, Metadata *Scope,
Metadata *InlinedAt, bool ImplicitCode,
uint64_t AtomGroup, uint8_t AtomRank,
StorageType Storage, bool ShouldCreate) {
// Fixup column.
adjustColumn(Column);

if (Storage == Uniqued) {
if (auto *N = getUniqued(Context.pImpl->DILocations,
DILocationInfo::KeyTy(Line, Column, Scope,
InlinedAt, ImplicitCode)))
InlinedAt, ImplicitCode,
AtomGroup, AtomRank)))
return N;
if (!ShouldCreate)
return nullptr;
Expand All @@ -106,8 +115,9 @@ DILocation *DILocation::getImpl(LLVMContext &Context, unsigned Line,
Ops.push_back(Scope);
if (InlinedAt)
Ops.push_back(InlinedAt);
return storeImpl(new (Ops.size(), Storage) DILocation(
Context, Storage, Line, Column, Ops, ImplicitCode),
return storeImpl(new (Ops.size(), Storage)
DILocation(Context, Storage, Line, Column, AtomGroup,
AtomRank, Ops, ImplicitCode),
Storage, Context.pImpl->DILocations);
}

Expand Down
17 changes: 12 additions & 5 deletions llvm/lib/IR/LLVMContextImpl.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect that unconditionally including these in the MDNode key may be the problem.

Original file line number Diff line number Diff line change
Expand Up @@ -315,23 +315,30 @@ template <> struct MDNodeKeyImpl<DILocation> {
Metadata *Scope;
Metadata *InlinedAt;
bool ImplicitCode;
uint64_t AtomGroup : 61;
uint8_t AtomRank : 3;

MDNodeKeyImpl(unsigned Line, unsigned Column, Metadata *Scope,
Metadata *InlinedAt, bool ImplicitCode)
Metadata *InlinedAt, bool ImplicitCode, uint64_t AtomGroup,
uint8_t AtomRank)
: Line(Line), Column(Column), Scope(Scope), InlinedAt(InlinedAt),
ImplicitCode(ImplicitCode) {}
ImplicitCode(ImplicitCode), AtomGroup(AtomGroup), AtomRank(AtomRank) {}

MDNodeKeyImpl(const DILocation *L)
: Line(L->getLine()), Column(L->getColumn()), Scope(L->getRawScope()),
InlinedAt(L->getRawInlinedAt()), ImplicitCode(L->isImplicitCode()) {}
InlinedAt(L->getRawInlinedAt()), ImplicitCode(L->isImplicitCode()),
AtomGroup(L->getAtomGroup()), AtomRank(L->getAtomRank()) {}

bool isKeyOf(const DILocation *RHS) const {
return Line == RHS->getLine() && Column == RHS->getColumn() &&
Scope == RHS->getRawScope() && InlinedAt == RHS->getRawInlinedAt() &&
ImplicitCode == RHS->isImplicitCode();
ImplicitCode == RHS->isImplicitCode() &&
AtomGroup == RHS->getAtomGroup() && AtomRank == RHS->getAtomRank();
}

unsigned getHashValue() const {
return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode);
return hash_combine(Line, Column, Scope, InlinedAt, ImplicitCode, AtomGroup,
AtomRank);
}
};

Expand Down
1 change: 1 addition & 0 deletions llvm/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ llvm_canonicalize_cmake_booleans(
LLVM_INCLUDE_SPIRV_TOOLS_TESTS
LLVM_APPEND_VC_REV
LLVM_HAS_LOGF128
LLVM_EXPERIMENTAL_KEY_INSTRUCTIONS
)

configure_lit_site_cfg(
Expand Down
19 changes: 19 additions & 0 deletions llvm/test/DebugInfo/KeyInstructions/Generic/parse.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
; RUN: opt %s -o - -S| FileCheck %s

; CHECK: !DILocation(line: 1, column: 11, scope: ![[#]], atomGroup: 1, atomRank: 1)

define dso_local void @f() !dbg !10 {
entry:
ret void, !dbg !13
}

!llvm.module.flags = !{!3}

!0 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1, producer: "clang version 21.0.0git", isOptimized: false, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
!1 = !DIFile(filename: "test.cpp", directory: "/")
!3 = !{i32 2, !"Debug Info Version", i32 3}
!9 = !{!"clang version 21.0.0git"}
!10 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !11, scopeLine: 1, flags: DIFlagPrototyped, spFlags: DISPFlagDefinition, unit: !0)
!11 = !DISubroutineType(types: !12)
!12 = !{null}
!13 = !DILocation(line: 1, column: 11, scope: !10, atomGroup: 1, atomRank: 1)
2 changes: 2 additions & 0 deletions llvm/test/DebugInfo/KeyInstructions/lit.local.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
if not config.has_key_instructions:
config.unsupported = True
1 change: 1 addition & 0 deletions llvm/test/lit.site.cfg.py.in
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ config.spirv_tools_tests = @LLVM_INCLUDE_SPIRV_TOOLS_TESTS@
config.have_vc_rev = @LLVM_APPEND_VC_REV@
config.force_vc_rev = "@LLVM_FORCE_VC_REVISION@"
config.has_logf128 = @LLVM_HAS_LOGF128@
config.has_key_instructions = @LLVM_EXPERIMENTAL_KEY_INSTRUCTIONS@

import lit.llvm
lit.llvm.initialize(lit_config, config)
Expand Down