Skip to content

Commit 3d42557

Browse files
authored
[RemoveDI] Handle DPValues in SROA (#74089)
Handle dbg.declares in SROA using DPValues. In order to reduce duplication, the migrate-debug-info loop has been changed to a generic lambda with some helper function overloads, which is called for dbg.declares, dbg.assigns, and DPValues alike. The tests will become "live" once #74090 lands (see for more info).
1 parent 32532c2 commit 3d42557

File tree

13 files changed

+67
-33
lines changed

13 files changed

+67
-33
lines changed

llvm/lib/Transforms/Scalar/SROA.cpp

Lines changed: 53 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -4838,6 +4838,35 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
48384838
return NewAI;
48394839
}
48404840

4841+
static void insertNewDbgInst(DIBuilder &DIB, DbgDeclareInst *Orig,
4842+
AllocaInst *NewAddr, DIExpression *NewFragmentExpr,
4843+
Instruction *BeforeInst) {
4844+
DIB.insertDeclare(NewAddr, Orig->getVariable(), NewFragmentExpr,
4845+
Orig->getDebugLoc(), BeforeInst);
4846+
}
4847+
static void insertNewDbgInst(DIBuilder &DIB, DbgAssignIntrinsic *Orig,
4848+
AllocaInst *NewAddr, DIExpression *NewFragmentExpr,
4849+
Instruction *BeforeInst) {
4850+
(void)BeforeInst;
4851+
if (!NewAddr->hasMetadata(LLVMContext::MD_DIAssignID)) {
4852+
NewAddr->setMetadata(LLVMContext::MD_DIAssignID,
4853+
DIAssignID::getDistinct(NewAddr->getContext()));
4854+
}
4855+
auto *NewAssign = DIB.insertDbgAssign(
4856+
NewAddr, Orig->getValue(), Orig->getVariable(), NewFragmentExpr, NewAddr,
4857+
Orig->getAddressExpression(), Orig->getDebugLoc());
4858+
LLVM_DEBUG(dbgs() << "Created new assign intrinsic: " << *NewAssign << "\n");
4859+
}
4860+
static void insertNewDbgInst(DIBuilder &DIB, DPValue *Orig, AllocaInst *NewAddr,
4861+
DIExpression *NewFragmentExpr,
4862+
Instruction *BeforeInst) {
4863+
(void)DIB;
4864+
DPValue *New = new DPValue(ValueAsMetadata::get(NewAddr), Orig->getVariable(),
4865+
NewFragmentExpr, Orig->getDebugLoc(),
4866+
DPValue::LocationType::Declare);
4867+
BeforeInst->getParent()->insertDPValueBefore(New, BeforeInst->getIterator());
4868+
}
4869+
48414870
/// Walks the slices of an alloca and form partitions based on them,
48424871
/// rewriting each of their uses.
48434872
bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
@@ -4939,15 +4968,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
49394968

49404969
// Migrate debug information from the old alloca to the new alloca(s)
49414970
// and the individual partitions.
4942-
TinyPtrVector<DbgVariableIntrinsic *> DbgVariables;
4943-
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
4944-
findDbgDeclares(DbgDeclares, &AI);
4945-
for (auto *DbgDeclare : DbgDeclares)
4946-
DbgVariables.push_back(DbgDeclare);
4947-
for (auto *DbgAssign : at::getAssignmentMarkers(&AI))
4948-
DbgVariables.push_back(DbgAssign);
4949-
4950-
for (DbgVariableIntrinsic *DbgVariable : DbgVariables) {
4971+
auto MigrateOne = [&](auto *DbgVariable) {
49514972
auto *Expr = DbgVariable->getExpression();
49524973
DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false);
49534974
uint64_t AllocaSize =
@@ -5001,37 +5022,33 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
50015022
// Remove any existing intrinsics on the new alloca describing
50025023
// the variable fragment.
50035024
SmallVector<DbgDeclareInst *, 1> FragDbgDeclares;
5004-
findDbgDeclares(FragDbgDeclares, Fragment.Alloca);
5005-
for (DbgDeclareInst *OldDII : FragDbgDeclares) {
5006-
auto SameVariableFragment = [](const DbgVariableIntrinsic *LHS,
5007-
const DbgVariableIntrinsic *RHS) {
5025+
SmallVector<DPValue *, 1> FragDPVs;
5026+
findDbgDeclares(FragDbgDeclares, Fragment.Alloca, &FragDPVs);
5027+
auto RemoveOne = [DbgVariable](auto *OldDII) {
5028+
auto SameVariableFragment = [](const auto *LHS, const auto *RHS) {
50085029
return LHS->getVariable() == RHS->getVariable() &&
50095030
LHS->getDebugLoc()->getInlinedAt() ==
50105031
RHS->getDebugLoc()->getInlinedAt();
50115032
};
50125033
if (SameVariableFragment(OldDII, DbgVariable))
50135034
OldDII->eraseFromParent();
5014-
}
5035+
};
5036+
for_each(FragDbgDeclares, RemoveOne);
5037+
for_each(FragDPVs, RemoveOne);
50155038

5016-
if (auto *DbgAssign = dyn_cast<DbgAssignIntrinsic>(DbgVariable)) {
5017-
if (!Fragment.Alloca->hasMetadata(LLVMContext::MD_DIAssignID)) {
5018-
Fragment.Alloca->setMetadata(
5019-
LLVMContext::MD_DIAssignID,
5020-
DIAssignID::getDistinct(AI.getContext()));
5021-
}
5022-
auto *NewAssign = DIB.insertDbgAssign(
5023-
Fragment.Alloca, DbgAssign->getValue(), DbgAssign->getVariable(),
5024-
FragmentExpr, Fragment.Alloca, DbgAssign->getAddressExpression(),
5025-
DbgAssign->getDebugLoc());
5026-
NewAssign->setDebugLoc(DbgAssign->getDebugLoc());
5027-
LLVM_DEBUG(dbgs() << "Created new assign intrinsic: " << *NewAssign
5028-
<< "\n");
5029-
} else {
5030-
DIB.insertDeclare(Fragment.Alloca, DbgVariable->getVariable(),
5031-
FragmentExpr, DbgVariable->getDebugLoc(), &AI);
5032-
}
5039+
insertNewDbgInst(DIB, DbgVariable, Fragment.Alloca, FragmentExpr, &AI);
50335040
}
5034-
}
5041+
};
5042+
5043+
// Migrate debug information from the old alloca to the new alloca(s)
5044+
// and the individual partitions.
5045+
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
5046+
SmallVector<DPValue *, 1> DPValues;
5047+
findDbgDeclares(DbgDeclares, &AI, &DPValues);
5048+
for_each(DbgDeclares, MigrateOne);
5049+
for_each(DPValues, MigrateOne);
5050+
for_each(at::getAssignmentMarkers(&AI), MigrateOne);
5051+
50355052
return Changed;
50365053
}
50375054

@@ -5153,9 +5170,12 @@ bool SROA::deleteDeadInstructions(
51535170
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
51545171
DeletedAllocas.insert(AI);
51555172
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
5156-
findDbgDeclares(DbgDeclares, AI);
5173+
SmallVector<DPValue *, 1> DPValues;
5174+
findDbgDeclares(DbgDeclares, AI, &DPValues);
51575175
for (DbgDeclareInst *OldDII : DbgDeclares)
51585176
OldDII->eraseFromParent();
5177+
for (DPValue *OldDII : DPValues)
5178+
OldDII->eraseFromParent();
51595179
}
51605180

51615181
at::deleteAssignmentMarkers(I);

llvm/test/DebugInfo/ARM/sroa-complex.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -passes='sroa' -S -o - %s | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -passes='sroa' -S -o - %s | FileCheck %s
23
target datalayout = "e-m:o-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
34
target triple = "thumbv7-apple-unknown-macho"
45

llvm/test/DebugInfo/Generic/sroa-larger.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -passes='sroa' -S -o - %s | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -passes='sroa' -S -o - %s | FileCheck %s
23
; Generated from clang -c -O2 -g -target x86_64-pc-windows-msvc
34
; struct A {
45
; int _Myval2;

llvm/test/DebugInfo/Generic/sroa-samesize.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -passes='sroa' -S -o - %s | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -passes='sroa' -S -o - %s | FileCheck %s
23
; Generated from clang -c -O2 -g -target x86_64-pc-windows-msvc
34
; struct A { double x1[]; };
45
; struct x2 {

llvm/test/DebugInfo/X86/sroa-after-inlining.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt %s -passes='cgscc(function(sroa,instcombine),inline),function(instcombine,sroa),verify' -S -o - | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='cgscc(function(sroa,instcombine),inline),function(instcombine,sroa),verify' -S -o - | FileCheck %s
23
;
34
; This test checks that SROA pass processes debug info correctly if applied twice.
45
; Specifically, after SROA works first time, instcombine converts dbg.declare

llvm/test/DebugInfo/X86/sroasplit-1.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt %s -passes='sroa,verify' -S -o - | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,verify' -S -o - | FileCheck %s
23
;
34
; Test that we can partial emit debug info for aggregates repeatedly
45
; split up by SROA.

llvm/test/DebugInfo/X86/sroasplit-2.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt %s -passes='sroa,verify' -S -o - | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,verify' -S -o - | FileCheck %s
23
;
34
; Test that we can partial emit debug info for aggregates repeatedly
45
; split up by SROA.

llvm/test/DebugInfo/X86/sroasplit-3.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
; RUN: opt %s -passes='sroa,verify' -S -o - | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,verify' -S -o - | FileCheck %s
3+
24
; ModuleID = 'test.c'
35
; Test that SROA updates the debug info correctly if an alloca was rewritten but
46
; not partitioned into multiple allocas.

llvm/test/DebugInfo/X86/sroasplit-4.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -passes='sroa' < %s -S -o - | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -passes='sroa' < %s -S -o - | FileCheck %s
23
;
34
; Test that recursively splitting an alloca updates the debug info correctly.
45
; CHECK: %[[T:.*]] = load i64, ptr @t, align 8

llvm/test/DebugInfo/X86/sroasplit-5.ll

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
; RUN: opt %s -passes='sroa,verify' -S -o - | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,verify' -S -o - | FileCheck %s
3+
24
; From:
35
; struct prog_src_register {
46
; unsigned : 4;

llvm/test/DebugInfo/X86/sroasplit-dbg-declare.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt -S -passes='sroa' -o - %s | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators -S -passes='sroa' -o - %s | FileCheck %s
23

34
; SROA should split the alloca in two new ones, each with its own dbg.declare.
45
; The original alloca and dbg.declare should be removed.

llvm/test/DebugInfo/salvage-overflow.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
; RUN: opt %s -passes='sroa,early-cse' -S | FileCheck %s
2+
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,early-cse' -S | FileCheck %s
23
; CHECK: DIExpression(DW_OP_constu, 9223372036854775808, DW_OP_minus, DW_OP_stack_value)
34
; Created from the following C input (and then delta-reduced the IR):
45
;

llvm/test/Transforms/Util/dbg-user-of-aext.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
; (here exposed through the SROA) pass refers to [s|z]exts of values (as
33
; opposed to the operand of a [s|z]ext).
44
; RUN: opt -S -passes='sroa' %s | FileCheck %s
5+
; RUN: opt --try-experimental-debuginfo-iterators -S -passes='sroa' %s | FileCheck %s
56

67
; Built from:
78
; struct foo { bool b; long i; };

0 commit comments

Comments
 (0)