[llvm] [Utils] Examine debug info type instead of alloca type to guess the debug behavior of the alloca uses (PR #177480)
Jameson Nash via llvm-commits
llvm-commits at lists.llvm.org
Thu Jan 22 13:54:22 PST 2026
https://github.com/vtjnash created https://github.com/llvm/llvm-project/pull/177480
Replace `isArray` and `isStructure` helpers that queried alloca IR type with a `isCompositeType` helper that checks the debug variable's source-level type from debug info metadata to decide if this seems perhaps profitable to convert to this debug info from #debug_declare to a #debug_value.
This changes behavior: the lowering decision is now based on the source-level type from debug info rather than the IR alloca type, which is more semantically correct for debug info processing.
Removes all uses of getAllocatedType() from Utils/Local.cpp.
This seemed slightly more semantically correct to me, though it is slightly challenging to enumerate all of the possible scalar debug types. Another alternative perhaps is to actually examine all of the uses and decide if all of them will get successfully handled before the `DDI->eraseFromParent` deletes the debug record, since `ConvertDebugDeclareToDebugValue` also just deletes the debug info (replaces with poison) if it finds a use that doesn't have an easy way to replace. The goals for this pass aren't entirely clear to me, though there are many tests for it to have this behavior.
>From ce7bce01a516f51c0f2f9fc011d5c1739d7629ac Mon Sep 17 00:00:00 2001
From: Jameson Nash <vtjnash+github at gmail.com>
Date: Thu, 22 Jan 2026 21:04:53 +0000
Subject: [PATCH] [Utils] Examine debug info type when deciding the debug info
type kind
Replace `isArray` and `isStructure` helpers that queried alloca IR type
with `isCompositeType` that checks the debug variable's source-level
type from debug info metadata to decide if this seems perhaps
profitable to convert to this #debug_declare to a #debug_value.
This changes behavior: the lowering decision is now based on the
source-level type from debug info rather than the IR alloca type, which
is more semantically correct for debug info processing.
Removes all uses of getAllocatedType() from Utils/Local.cpp.
Co-Authored-By: Claude Sonnet 4.5 <noreply at anthropic.com>
---
llvm/lib/Transforms/Utils/Local.cpp | 44 +++++++++++++++----
.../Generic/dbg-value-lower-linenos.ll | 7 +--
.../dbg-scalable-store-fixed-frag.ll | 2 +-
.../InstCombine/dbg-simplify-alloca-size.ll | 4 +-
4 files changed, 42 insertions(+), 15 deletions(-)
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index f7842a235b20d..5e14438d70076 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1748,16 +1748,39 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR, LoadInst *LI,
LI->getParent()->insertDbgRecordAfter(DV, LI);
}
-/// Determine whether this alloca is either a VLA or an array.
-static bool isArray(AllocaInst *AI) {
- return AI->isArrayAllocation() ||
- (AI->getAllocatedType() && AI->getAllocatedType()->isArrayTy());
+/// Determine whether this debug variable is a not a basic type.
+/// We strip through DIDerivedType modifiers (typedefs, const, etc.)
+/// to find the underlying type to decide if it seems perhaps worthwhile to
+/// do LowerDbgDeclare.
+static bool isCompositeType(DbgVariableRecord *DVR) {
+ DIType *Ty = DVR->getVariable()->getType();
+ if (Ty == nullptr)
+ return true;
+ // Strip through modifier types to find the underlying type.
+ while (auto *DTy = dyn_cast<DIDerivedType>(Ty)) {
+ switch (DTy->getTag()) {
+ case dwarf::DW_TAG_pointer_type:
+ case dwarf::DW_TAG_reference_type:
+ case dwarf::DW_TAG_rvalue_reference_type:
+ case dwarf::DW_TAG_ptr_to_member_type:
+ case dwarf::DW_TAG_LLVM_ptrauth_type:
+ return false;
+ case dwarf::DW_TAG_typedef:
+ case dwarf::DW_TAG_const_type:
+ case dwarf::DW_TAG_volatile_type:
+ case dwarf::DW_TAG_restrict_type:
+ case dwarf::DW_TAG_atomic_type:
+ case dwarf::DW_TAG_immutable_type:
+ Ty = DTy->getBaseType();
+ continue;
+ default:
+ break;
+ }
+ break;
+ }
+ return !isa<DIBasicType>(Ty);
}
-/// Determine whether this alloca is a structure.
-static bool isStructure(AllocaInst *AI) {
- return AI->getAllocatedType() && AI->getAllocatedType()->isStructTy();
-}
void llvm::ConvertDebugDeclareToDebugValue(DbgVariableRecord *DVR, PHINode *APN,
DIBuilder &Builder) {
auto *DIVar = DVR->getVariable();
@@ -1820,10 +1843,13 @@ bool llvm::LowerDbgDeclare(Function &F) {
// stored on the stack, while the dbg.declare can only describe
// the stack slot (and at a lexical-scope granularity). Later
// passes will attempt to elide the stack slot.
- if (!AI || isArray(AI) || isStructure(AI))
+ // Skip VLAs (dynamic allocas) and composite types (arrays/structs) since
+ // they can't be represented as a single dbg.value.
+ if (!AI || !isa<Constant>(AI->getArraySize()) || isCompositeType(DDI))
return;
// A volatile load/store means that the alloca can't be elided anyway.
+ // Just look at direct uses however, and ignore any other instructions.
if (llvm::any_of(AI->users(), [](User *U) -> bool {
if (LoadInst *LI = dyn_cast<LoadInst>(U))
return LI->isVolatile();
diff --git a/llvm/test/DebugInfo/Generic/dbg-value-lower-linenos.ll b/llvm/test/DebugInfo/Generic/dbg-value-lower-linenos.ll
index b6879e3f827a8..3fe91530714d9 100644
--- a/llvm/test/DebugInfo/Generic/dbg-value-lower-linenos.ll
+++ b/llvm/test/DebugInfo/Generic/dbg-value-lower-linenos.ll
@@ -87,13 +87,13 @@ declare i32 @lookup(...)
!llvm.module.flags = !{!4}
!llvm.dbg.cu = !{!2}
-!1 = !DILocalVariable(name: "bees", scope: !5, type: null)
+!1 = !DILocalVariable(name: "bees", scope: !5, type: !17)
!2 = distinct !DICompileUnit(language: DW_LANG_C_plus_plus, file: !3, producer: "beards", isOptimized: true, runtimeVersion: 4, emissionKind: FullDebug)
!3 = !DIFile(filename: "bees.cpp", directory: "")
!4 = !{i32 2, !"Debug Info Version", i32 3}
!5 = distinct !DISubprogram(name: "nope", scope: !3, file: !3, line: 1, unit: !2)
!6 = !DILocation(line: 1, scope: !5)
-!7 = !DILocalVariable(name: "flannel", scope: !5, type: null)
+!7 = !DILocalVariable(name: "flannel", scope: !5, type: !17)
!8 = !DILocation(line: 2, scope: !5)
!9 = !DILocation(line: 3, scope: !5)
!10 = !DILocation(line: 4, scope: !5)
@@ -103,8 +103,9 @@ declare i32 @lookup(...)
!14 = !DILocation(line: 8, scope: !5)
!15 = distinct !DISubprogram(name: "wat", scope: !2, file: !3, line: 10, unit: !2)
!16 = !DILocation(line: 9, scope: !15, inlinedAt: !14)
+!17 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!20 = distinct !DISubprogram(name: "thin", scope: !3, file: !3, line: 20, unit: !2)
-!21 = !DILocalVariable(name: "floogie", scope: !20, type: null)
+!21 = !DILocalVariable(name: "floogie", scope: !20, type: !17)
!22 = !DILocation(line: 21, scope: !20)
!23 = !DILocation(line: 22, scope: !20)
!24 = !DILocation(line: 23, scope: !20)
diff --git a/llvm/test/Transforms/InstCombine/dbg-scalable-store-fixed-frag.ll b/llvm/test/Transforms/InstCombine/dbg-scalable-store-fixed-frag.ll
index 679a224b1a03e..512d2fd62571c 100644
--- a/llvm/test/Transforms/InstCombine/dbg-scalable-store-fixed-frag.ll
+++ b/llvm/test/Transforms/InstCombine/dbg-scalable-store-fixed-frag.ll
@@ -6,7 +6,7 @@ define i32 @foo(<vscale x 2 x i32> %x) {
; CHECK-LABEL: @foo(
; CHECK-NEXT: entry:
; CHECK-NEXT: [[ARR:%.*]] = alloca i32, align 4
-; CHECK-NEXT: #dbg_value(<vscale x 2 x i32> poison, [[META8:![0-9]+]], !DIExpression(), [[META14:![0-9]+]])
+; CHECK-NEXT: #dbg_declare(ptr [[ARR]], [[META8:![0-9]+]], !DIExpression(), [[META14:![0-9]+]])
; CHECK-NEXT: store <vscale x 2 x i32> [[X:%.*]], ptr [[ARR]], align 4
; CHECK-NEXT: [[RES:%.*]] = load i32, ptr [[ARR]], align 4
; CHECK-NEXT: ret i32 [[RES]]
diff --git a/llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll b/llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll
index e9099f6635c53..df95d65a0b1e2 100644
--- a/llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll
+++ b/llvm/test/Transforms/InstCombine/dbg-simplify-alloca-size.ll
@@ -8,7 +8,7 @@ declare void @llvm.dbg.declare(metadata, metadata, metadata)
; CHECK-LABEL: @toplevel(
; CHECK: entry:
; CHECK-NEXT: %pixels1 = alloca [3 x i8], align 1
-; CHECK-NEXT: #dbg_declare(ptr %pixels1, ![[MD:[0-9]+]], !DIExpression(), ![[DBG:[0-9]+]]
+; CHECK-NEXT: #dbg_value(ptr %pixels1, ![[MD:[0-9]+]], !DIExpression(DW_OP_deref), ![[DBG:[0-9]+]]
; CHECK-NEXT: call void @foo(ptr nonnull %pixels1)
; CHECK-NEXT: ret void
define dso_local void @toplevel() {
@@ -34,6 +34,6 @@ entry:
!10 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
; CHECK: ![[MD]] = !DILocalVariable(name: "pixels"
!11 = !DILocalVariable(name: "pixels", arg: 1, scope: !7, file: !1, line: 9, type: !10)
-; CHECK: ![[DBG]] = !DILocation(line: 9, column: 16,
+; CHECK: ![[DBG]] = !DILocation(line: 0,
!12 = !DILocation(line: 9, column: 16, scope: !7)
More information about the llvm-commits
mailing list