[llvm] [DebugInfo] Treat tag offsets as non-emitting (PR #211969)
Eric Christopher via llvm-commits
llvm-commits at lists.llvm.org
Fri Jul 24 20:31:45 PDT 2026
https://github.com/echristo updated https://github.com/llvm/llvm-project/pull/211969
>From 43e353f1e741d935d32288639c7cc0a506f4549b Mon Sep 17 00:00:00 2001
From: Eric Christopher <echristopher at nvidia.com>
Date: Fri, 24 Jul 2026 00:31:44 -0700
Subject: [PATCH 1/2] [DebugInfo] Treat tag offsets as non-emitting
DW_OP_LLVM_tag_offset doesn't add anything to the DWARF expression, so
look past it when deciding whether a register expression is complex or
whether appendToStack needs to add a dereference.
---
llvm/include/llvm/IR/DebugInfoMetadata.h | 4 ++
.../CodeGen/AsmPrinter/DwarfExpression.cpp | 25 +++++----
llvm/lib/IR/DebugInfoMetadata.cpp | 35 ++++++++-----
.../X86/di-expression-tag-offset-register.ll | 51 +++++++++++++++++++
llvm/unittests/IR/MetadataTest.cpp | 41 +++++++++++++++
5 files changed, 133 insertions(+), 23 deletions(-)
create mode 100644 llvm/test/DebugInfo/X86/di-expression-tag-offset-register.ll
diff --git a/llvm/include/llvm/IR/DebugInfoMetadata.h b/llvm/include/llvm/IR/DebugInfoMetadata.h
index 846342bee6071..33a1a6e482555 100644
--- a/llvm/include/llvm/IR/DebugInfoMetadata.h
+++ b/llvm/include/llvm/IR/DebugInfoMetadata.h
@@ -3551,6 +3551,10 @@ class DIExpression : public MDNode {
/// Return the number of elements in the operand (1 + args).
LLVM_ABI unsigned getSize() const;
+ /// Return true if CodeGen handles this operand without adding bytes to the
+ /// DWARF expression.
+ LLVM_ABI bool isNonEmitting() const;
+
/// Append the elements of this operand to \p V.
void appendToVector(SmallVectorImpl<uint64_t> &V) const {
V.append(get(), get() + getSize());
diff --git a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
index 9b676f88a0cff..69240df145ac5 100644
--- a/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
+++ b/llvm/lib/CodeGen/AsmPrinter/DwarfExpression.cpp
@@ -26,6 +26,19 @@ using namespace llvm;
#define DEBUG_TYPE "dwarfdebug"
+/// Return whether the rest of the expression needs the complex register path.
+/// We use this to decide whether we can emit a simple register location and
+/// whether a subregister needs to be masked. Non-emitting operations don't
+/// affect either decision, so look past them.
+static bool isRemainingExpressionComplex(const DIExpressionCursor &ExprCursor) {
+ for (DIExpression::ExprOperand Op : ExprCursor) {
+ if (Op.isNonEmitting())
+ continue;
+ return Op.getOp() != dwarf::DW_OP_LLVM_fragment;
+ }
+ return false;
+}
+
void DwarfExpression::emitConstu(uint64_t Value) {
if (Value < 32)
emitOp(dwarf::DW_OP_lit0 + Value);
@@ -295,10 +308,8 @@ bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
return false;
}
- bool HasComplexExpression = false;
auto Op = ExprCursor.peek();
- if (Op && Op->getOp() != dwarf::DW_OP_LLVM_fragment)
- HasComplexExpression = true;
+ bool HasComplexExpression = isRemainingExpressionComplex(ExprCursor);
// If the register can only be described by a complex expression (i.e.,
// multiple subregisters) it doesn't safely compose with another complex
@@ -348,9 +359,7 @@ bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
DwarfRegs.clear();
// If we need to mask out a subregister, do it now, unless the next
// operation would emit an OpPiece anyway.
- auto NextOp = ExprCursor.peek();
- if (SubRegisterSizeInBits && NextOp &&
- (NextOp->getOp() != dwarf::DW_OP_LLVM_fragment))
+ if (SubRegisterSizeInBits && isRemainingExpressionComplex(ExprCursor))
maskSubRegister();
return true;
}
@@ -415,9 +424,7 @@ bool DwarfExpression::addMachineRegExpression(const TargetRegisterInfo &TRI,
// If we need to mask out a subregister, do it now, unless the next
// operation would emit an OpPiece anyway.
- auto NextOp = ExprCursor.peek();
- if (SubRegisterSizeInBits && NextOp &&
- (NextOp->getOp() != dwarf::DW_OP_LLVM_fragment))
+ if (SubRegisterSizeInBits && isRemainingExpressionComplex(ExprCursor))
maskSubRegister();
return true;
diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp b/llvm/lib/IR/DebugInfoMetadata.cpp
index 88f7f2f6240e0..67bac5f785310 100644
--- a/llvm/lib/IR/DebugInfoMetadata.cpp
+++ b/llvm/lib/IR/DebugInfoMetadata.cpp
@@ -1757,6 +1757,10 @@ unsigned DIExpression::ExprOperand::getSize() const {
}
}
+bool DIExpression::ExprOperand::isNonEmitting() const {
+ return getOp() == dwarf::DW_OP_LLVM_tag_offset;
+}
+
bool DIExpression::isValid() const {
for (auto I = expr_op_begin(), E = expr_op_end(); I != E; ++I) {
// Check that there's space for the operand.
@@ -1878,11 +1882,12 @@ bool DIExpression::isComplex() const {
if (getNumElements() == 0)
return false;
- // If there are any elements other than fragment or tag_offset, then some
- // kind of complex computation occurs.
+ // Tag offsets are non-emitting. They, fragments, and location operands don't
+ // perform a computation by themselves.
for (const auto &It : expr_ops()) {
+ if (It.isNonEmitting())
+ continue;
switch (It.getOp()) {
- case dwarf::DW_OP_LLVM_tag_offset:
case dwarf::DW_OP_LLVM_fragment:
case dwarf::DW_OP_LLVM_arg:
continue;
@@ -2323,17 +2328,19 @@ DIExpression *DIExpression::appendToStack(const DIExpression *Expr,
}) &&
"Can't append this op");
- // Append a DW_OP_deref after Expr's current op list if it's non-empty and
- // has no DW_OP_stack_value.
- //
- // Match .* DW_OP_stack_value (DW_OP_LLVM_fragment A B)?.
- std::optional<FragmentInfo> FI = Expr->getFragmentInfo();
- unsigned DropUntilStackValue = FI ? 3 : 0;
- ArrayRef<uint64_t> ExprOpsBeforeFragment =
- Expr->getElements().drop_back(DropUntilStackValue);
- bool NeedsDeref = (Expr->getNumElements() > DropUntilStackValue) &&
- (ExprOpsBeforeFragment.back() != dwarf::DW_OP_stack_value);
- bool NeedsStackValue = NeedsDeref || ExprOpsBeforeFragment.empty();
+ // DIExpression stores opcodes and their arguments in a flat array. Walk the
+ // parsed operations to find the last opcode that determines whether the
+ // expression already ends in DW_OP_stack_value.
+ std::optional<uint64_t> LastValueOp;
+ for (auto Op : Expr->expr_ops()) {
+ if (Op.isNonEmitting() ||
+ Op.getOp() == dwarf::DW_OP_LLVM_fragment)
+ continue;
+ LastValueOp = Op.getOp();
+ }
+ bool NeedsDeref =
+ LastValueOp && *LastValueOp != dwarf::DW_OP_stack_value;
+ bool NeedsStackValue = NeedsDeref || !LastValueOp;
// Append a DW_OP_deref after Expr's current op list if needed, then append
// the new ops, and finally ensure that a single DW_OP_stack_value is present.
diff --git a/llvm/test/DebugInfo/X86/di-expression-tag-offset-register.ll b/llvm/test/DebugInfo/X86/di-expression-tag-offset-register.ll
new file mode 100644
index 0000000000000..d3cf0b6169474
--- /dev/null
+++ b/llvm/test/DebugInfo/X86/di-expression-tag-offset-register.ll
@@ -0,0 +1,51 @@
+; RUN: llc -mtriple=x86_64-unknown-linux-gnu -filetype=obj -o - %s | llvm-dwarfdump - | FileCheck %s
+
+; Tag offsets emit no expression bytes, so skip them when deciding whether a
+; register expression is complex.
+
+define void @registers(i64 %tag_only, i32 %simple_subreg,
+ i32 %complex_subreg) !dbg !5 {
+ #dbg_value(i64 %tag_only, !9,
+ !DIExpression(DW_OP_LLVM_tag_offset, 7), !12)
+ #dbg_value(i32 %simple_subreg, !10,
+ !DIExpression(DW_OP_LLVM_tag_offset, 8,
+ DW_OP_LLVM_fragment, 0, 32), !12)
+ #dbg_value(i32 %complex_subreg, !14,
+ !DIExpression(DW_OP_LLVM_tag_offset, 9,
+ DW_OP_plus_uconst, 1), !12)
+ ret void, !dbg !12
+}
+
+; A tag-only expression stays in a register.
+; CHECK: DW_OP_reg5 RDI)
+; CHECK: DW_AT_LLVM_tag_offset (0x07)
+; CHECK: DW_AT_name ("tag_only")
+
+; A tag before a fragment keeps the subregister location simple.
+; CHECK: DW_OP_reg4 RSI, DW_OP_piece 0x4)
+; CHECK: DW_AT_LLVM_tag_offset (0x08)
+; CHECK: DW_AT_name ("simple_subreg")
+
+; A real operation after the tag uses the complex register path and masks the
+; 32-bit value before applying the operation.
+; CHECK: DW_OP_breg1 RDX+0, DW_OP_constu 0xffffffff, DW_OP_and, DW_OP_plus_uconst 0x1)
+; CHECK: DW_AT_LLVM_tag_offset (0x09)
+; CHECK: DW_AT_name ("complex_subreg")
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!3}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C, file: !1,
+ emissionKind: FullDebug)
+!1 = !DIFile(filename: "test.c", directory: "/")
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!5 = distinct !DISubprogram(name: "registers", scope: !1, file: !1,
+ type: !6, spFlags: DISPFlagDefinition, unit: !0)
+!6 = !DISubroutineType(types: !7)
+!7 = !{null}
+!9 = !DILocalVariable(name: "tag_only", scope: !5, type: !11)
+!10 = !DILocalVariable(name: "simple_subreg", scope: !5, type: !11)
+!11 = !DIBasicType(name: "long", size: 64, encoding: DW_ATE_signed)
+!12 = !DILocation(line: 1, scope: !5)
+!14 = !DILocalVariable(name: "complex_subreg", scope: !5, type: !15)
+!15 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
diff --git a/llvm/unittests/IR/MetadataTest.cpp b/llvm/unittests/IR/MetadataTest.cpp
index 65398b5ca2b1f..574c4afc470d2 100644
--- a/llvm/unittests/IR/MetadataTest.cpp
+++ b/llvm/unittests/IR/MetadataTest.cpp
@@ -4795,6 +4795,47 @@ TEST_F(DIExpressionTest, appendToStackAssert) {
EXPECT_EQ(Expr->getElements(), ArrayRef<uint64_t>(Expected));
}
+TEST_F(DIExpressionTest, appendToStackSkipsTagOffset) {
+ // DW_OP_LLVM_tag_offset doesn't emit anything, so it shouldn't make
+ // appendToStack add a DW_OP_deref.
+ DIExpression *Expr =
+ DIExpression::get(Context, {dwarf::DW_OP_LLVM_tag_offset, uint64_t{3}});
+ uint64_t Ops[] = {
+ dwarf::DW_OP_LLVM_convert, 32, dwarf::DW_ATE_signed,
+ dwarf::DW_OP_LLVM_convert, 64, dwarf::DW_ATE_signed,
+ };
+ Expr = DIExpression::appendToStack(Expr, Ops);
+
+ uint64_t Expected[] = {dwarf::DW_OP_LLVM_tag_offset,
+ 3,
+ dwarf::DW_OP_LLVM_convert,
+ 32,
+ dwarf::DW_ATE_signed,
+ dwarf::DW_OP_LLVM_convert,
+ 64,
+ dwarf::DW_ATE_signed,
+ dwarf::DW_OP_stack_value};
+ EXPECT_EQ(Expr->getElements(), ArrayRef<uint64_t>(Expected));
+}
+
+TEST_F(DIExpressionTest, TagOffsetsAreNotComplex) {
+ // Tag offsets don't emit anything, so they don't make an expression complex.
+ // Keep looking past them for an operation that does.
+ auto *TagOffset =
+ DIExpression::get(Context, {dwarf::DW_OP_LLVM_tag_offset, uint64_t{1}});
+ EXPECT_FALSE(TagOffset->isComplex());
+
+ auto *TagOffsetAndFragment = DIExpression::get(
+ Context, {dwarf::DW_OP_LLVM_tag_offset, uint64_t{1},
+ dwarf::DW_OP_LLVM_fragment, uint64_t{0}, uint64_t{32}});
+ EXPECT_FALSE(TagOffsetAndFragment->isComplex());
+
+ auto *TagOffsetAndOperation =
+ DIExpression::get(Context, {dwarf::DW_OP_LLVM_tag_offset, uint64_t{1},
+ dwarf::DW_OP_plus_uconst, uint64_t{1}});
+ EXPECT_TRUE(TagOffsetAndOperation->isComplex());
+}
+
typedef MetadataTest DIObjCPropertyTest;
TEST_F(DIObjCPropertyTest, get) {
>From 27ebd4508ceb94880939ea205eed01b8f44938ad Mon Sep 17 00:00:00 2001
From: Eric Christopher <echristopher at nvidia.com>
Date: Fri, 24 Jul 2026 20:30:35 -0700
Subject: [PATCH 2/2] [DebugInfo] Format tag offset handling
---
llvm/lib/IR/DebugInfoMetadata.cpp | 6 ++----
1 file changed, 2 insertions(+), 4 deletions(-)
diff --git a/llvm/lib/IR/DebugInfoMetadata.cpp b/llvm/lib/IR/DebugInfoMetadata.cpp
index 67bac5f785310..ceb4ec1af9c2c 100644
--- a/llvm/lib/IR/DebugInfoMetadata.cpp
+++ b/llvm/lib/IR/DebugInfoMetadata.cpp
@@ -2333,13 +2333,11 @@ DIExpression *DIExpression::appendToStack(const DIExpression *Expr,
// expression already ends in DW_OP_stack_value.
std::optional<uint64_t> LastValueOp;
for (auto Op : Expr->expr_ops()) {
- if (Op.isNonEmitting() ||
- Op.getOp() == dwarf::DW_OP_LLVM_fragment)
+ if (Op.isNonEmitting() || Op.getOp() == dwarf::DW_OP_LLVM_fragment)
continue;
LastValueOp = Op.getOp();
}
- bool NeedsDeref =
- LastValueOp && *LastValueOp != dwarf::DW_OP_stack_value;
+ bool NeedsDeref = LastValueOp && *LastValueOp != dwarf::DW_OP_stack_value;
bool NeedsStackValue = NeedsDeref || !LastValueOp;
// Append a DW_OP_deref after Expr's current op list if needed, then append
More information about the llvm-commits
mailing list