Skip to content

Commit

Permalink
[RemoveDIs] Handle DPValues in LowerDbgDeclare (#73504)
Browse files Browse the repository at this point in the history
The tests will become "live" once #74090 lands (see for more info).
  • Loading branch information
OCHyams committed Dec 12, 2023
1 parent 7de592b commit cb8690f
Show file tree
Hide file tree
Showing 13 changed files with 43 additions and 17 deletions.
47 changes: 30 additions & 17 deletions llvm/lib/Transforms/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1724,16 +1724,19 @@ void llvm::ConvertDebugDeclareToDebugValue(DbgVariableIntrinsic *DII,
SI->getIterator());
}

namespace llvm {
// RemoveDIs: duplicate the getDebugValueLoc method using DPValues instead of
// dbg.value intrinsics.
static DebugLoc getDebugValueLocDPV(DPValue *DPV) {
// dbg.value intrinsics. In llvm namespace so that it overloads the
// DbgVariableIntrinsic version.
static DebugLoc getDebugValueLoc(DPValue *DPV) {
// Original dbg.declare must have a location.
const DebugLoc &DeclareLoc = DPV->getDebugLoc();
MDNode *Scope = DeclareLoc.getScope();
DILocation *InlinedAt = DeclareLoc.getInlinedAt();
// Produce an unknown location with the correct scope / inlinedAt fields.
return DILocation::get(DPV->getContext(), 0, 0, Scope, InlinedAt);
}
} // namespace llvm

/// Inserts a llvm.dbg.value intrinsic before a load of an alloca'd value
/// that has an associated llvm.dbg.declare intrinsic.
Expand Down Expand Up @@ -1770,7 +1773,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DPValue *DPV, StoreInst *SI,
auto *DIExpr = DPV->getExpression();
Value *DV = SI->getValueOperand();

DebugLoc NewLoc = getDebugValueLocDPV(DPV);
DebugLoc NewLoc = getDebugValueLoc(DPV);

if (!valueCoversEntireFragment(DV->getType(), DPV)) {
// FIXME: If storing to a part of the variable described by the dbg.declare,
Expand Down Expand Up @@ -1842,7 +1845,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DPValue *DPV, LoadInst *LI,
return;
}

DebugLoc NewLoc = getDebugValueLocDPV(DPV);
DebugLoc NewLoc = getDebugValueLoc(DPV);

// We are now tracking the loaded value instead of the address. In the
// future if multi-location support is added to the IR, it might be
Expand Down Expand Up @@ -1887,7 +1890,7 @@ void llvm::ConvertDebugDeclareToDebugValue(DPValue *DPV, PHINode *APN,
BasicBlock *BB = APN->getParent();
auto InsertionPt = BB->getFirstInsertionPt();

DebugLoc NewLoc = getDebugValueLocDPV(DPV);
DebugLoc NewLoc = getDebugValueLoc(DPV);

// The block may be a catchswitch block, which does not have a valid
// insertion point.
Expand All @@ -1903,25 +1906,32 @@ bool llvm::LowerDbgDeclare(Function &F) {
bool Changed = false;
DIBuilder DIB(*F.getParent(), /*AllowUnresolved*/ false);
SmallVector<DbgDeclareInst *, 4> Dbgs;
for (auto &FI : F)
for (Instruction &BI : FI)
if (auto DDI = dyn_cast<DbgDeclareInst>(&BI))
SmallVector<DPValue *> DPVs;
for (auto &FI : F) {
for (Instruction &BI : FI) {
if (auto *DDI = dyn_cast<DbgDeclareInst>(&BI))
Dbgs.push_back(DDI);
for (DPValue &DPV : BI.getDbgValueRange()) {
if (DPV.getType() == DPValue::LocationType::Declare)
DPVs.push_back(&DPV);
}
}
}

if (Dbgs.empty())
if (Dbgs.empty() && DPVs.empty())
return Changed;

for (auto &I : Dbgs) {
DbgDeclareInst *DDI = I;
AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress());
auto LowerOne = [&](auto *DDI) {
AllocaInst *AI =
dyn_cast_or_null<AllocaInst>(DDI->getVariableLocationOp(0));
// If this is an alloca for a scalar variable, insert a dbg.value
// at each load and store to the alloca and erase the dbg.declare.
// The dbg.values allow tracking a variable even if it is not
// 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))
continue;
return;

// A volatile load/store means that the alloca can't be elided anyway.
if (llvm::any_of(AI->users(), [](User *U) -> bool {
Expand All @@ -1931,7 +1941,7 @@ bool llvm::LowerDbgDeclare(Function &F) {
return SI->isVolatile();
return false;
}))
continue;
return;

SmallVector<const Value *, 8> WorkList;
WorkList.push_back(AI);
Expand Down Expand Up @@ -1963,11 +1973,14 @@ bool llvm::LowerDbgDeclare(Function &F) {
}
DDI->eraseFromParent();
Changed = true;
}
};

for_each(Dbgs, LowerOne);
for_each(DPVs, LowerOne);

if (Changed)
for (BasicBlock &BB : F)
RemoveRedundantDbgInstrs(&BB);
for (BasicBlock &BB : F)
RemoveRedundantDbgInstrs(&BB);

return Changed;
}
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/ARM/lowerbdgdeclare_vla.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -passes=instcombine %s -S | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -passes=instcombine %s -S | FileCheck %s
;
; Generate me from:
; clang -cc1 -triple thumbv7-apple-ios7.0.0 -S -target-abi apcs-gnu -gdwarf-2 -Os test.c -o test.ll -emit-llvm
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/Generic/dbg-value-lower-linenos.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt < %s -S -passes=mem2reg,instcombine | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators < %s -S -passes=mem2reg,instcombine | FileCheck %s

; The '%bar' alloca will be promoted to an SSA register by mem2reg: test that
; zero line number are assigned to the dbg.value intrinsics that are inserted
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/X86/array2.ll
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
; }
;
; RUN: opt %s -O2 -S -o - | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators %s -O2 -S -o - | FileCheck %s
; Test that we correctly lower dbg.declares for arrays.
;
; CHECK: define i32 @main
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/X86/formal_parameter.ll
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ target triple = "x86_64-apple-macosx10.9.0"
; RUN: opt %s -O2 -S -o %t
; RUN: cat %t | FileCheck --check-prefix=LOWERING %s
; RUN: llc -filetype=obj %t -o - | llvm-dwarfdump -debug-info - | FileCheck %s
; RUN: llc --try-experimental-debuginfo-iterators -filetype=obj %t -o - | llvm-dwarfdump -debug-info - | FileCheck %s
; Test that we only emit only one DW_AT_formal_parameter "map" for this function.
; rdar://problem/14874886
;
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/X86/instcombine-instrinsics.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt %s -O2 -S -o - | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators %s -O2 -S -o - | FileCheck %s
; Verify that we emit the same intrinsic at most once.
; rdar://problem/13056109
;
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/duplicate_dbgvalue.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -passes=instcombine -S -o - < %s | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -passes=instcombine -S -o - < %s | FileCheck %s

; CHECK-LABEL: %3 = load i32, ptr %i1_311
; CHECK: call void @llvm.dbg.value(metadata i32 %3
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/DebugInfo/verify-di-preserve.ll
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
; RUN: opt %s -verify-debuginfo-preserve -passes=instcombine -disable-output 2>&1 | FileCheck --check-prefix=VERIFY %s
; RUN: opt --try-experimental-debuginfo-iterators %s -verify-debuginfo-preserve -passes=instcombine -disable-output 2>&1 | FileCheck --check-prefix=VERIFY %s

; VERIFY: CheckModuleDebugify (original debuginfo):

; RUN: opt %s -verify-each-debuginfo-preserve -O2 -disable-output 2>&1 | FileCheck --check-prefix=VERIFY-EACH %s
; RUN: opt %s --try-experimental-debuginfo-iterators -verify-each-debuginfo-preserve -O2 -disable-output 2>&1 | FileCheck --check-prefix=VERIFY-EACH %s

; VERIFY-EACH: DeadArgumentEliminationPass
; VERIFY-EACH: GlobalDCEPass
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
; RUN: opt < %s -passes='instcombine' -S | FileCheck %s
; RUN: opt < %s -passes='instcombine' -S --try-experimental-debuginfo-iterators | FileCheck %s


define i32 @foo(<vscale x 2 x i32> %x) {
; CHECK-LABEL: @foo(
; CHECK-NEXT: entry:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -S --passes=instcombine %s | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -S --passes=instcombine %s | FileCheck %s

; https://github.com/llvm/llvm-project/issues/56807
declare void @foo(ptr %pixels)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -passes=instcombine -S < %s | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -passes=instcombine -S < %s | FileCheck %s

; This test is defending against a TypeSize message raised in the method
; `valueCoversEntireFragment` in Local.cpp because of an implicit cast from
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/InstCombine/lifetime-no-null-opt.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators < %s -passes=instcombine -S | FileCheck %s

declare void @llvm.dbg.declare(metadata, metadata, metadata)
declare void @llvm.lifetime.start.p0(i64, ptr nocapture)
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/InstCombine/lifetime.ll
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt < %s -passes=instcombine -S | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators < %s -passes=instcombine -S | FileCheck %s

declare void @llvm.dbg.declare(metadata, metadata, metadata)
declare void @llvm.lifetime.start.p0(i64, ptr nocapture)
Expand Down

0 comments on commit cb8690f

Please sign in to comment.