Skip to content

[RemoveDI] Handle DPValues in SROA #74089

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
Dec 12, 2023
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
86 changes: 53 additions & 33 deletions llvm/lib/Transforms/Scalar/SROA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4838,6 +4838,35 @@ AllocaInst *SROA::rewritePartition(AllocaInst &AI, AllocaSlices &AS,
return NewAI;
}

static void insertNewDbgInst(DIBuilder &DIB, DbgDeclareInst *Orig,
AllocaInst *NewAddr, DIExpression *NewFragmentExpr,
Instruction *BeforeInst) {
DIB.insertDeclare(NewAddr, Orig->getVariable(), NewFragmentExpr,
Orig->getDebugLoc(), BeforeInst);
}
static void insertNewDbgInst(DIBuilder &DIB, DbgAssignIntrinsic *Orig,
AllocaInst *NewAddr, DIExpression *NewFragmentExpr,
Instruction *BeforeInst) {
(void)BeforeInst;
if (!NewAddr->hasMetadata(LLVMContext::MD_DIAssignID)) {
NewAddr->setMetadata(LLVMContext::MD_DIAssignID,
DIAssignID::getDistinct(NewAddr->getContext()));
}
auto *NewAssign = DIB.insertDbgAssign(
NewAddr, Orig->getValue(), Orig->getVariable(), NewFragmentExpr, NewAddr,
Orig->getAddressExpression(), Orig->getDebugLoc());
LLVM_DEBUG(dbgs() << "Created new assign intrinsic: " << *NewAssign << "\n");
}
static void insertNewDbgInst(DIBuilder &DIB, DPValue *Orig, AllocaInst *NewAddr,
DIExpression *NewFragmentExpr,
Instruction *BeforeInst) {
(void)DIB;
DPValue *New = new DPValue(ValueAsMetadata::get(NewAddr), Orig->getVariable(),
NewFragmentExpr, Orig->getDebugLoc(),
DPValue::LocationType::Declare);
BeforeInst->getParent()->insertDPValueBefore(New, BeforeInst->getIterator());
}

/// Walks the slices of an alloca and form partitions based on them,
/// rewriting each of their uses.
bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
Expand Down Expand Up @@ -4939,15 +4968,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {

// Migrate debug information from the old alloca to the new alloca(s)
// and the individual partitions.
TinyPtrVector<DbgVariableIntrinsic *> DbgVariables;
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
findDbgDeclares(DbgDeclares, &AI);
for (auto *DbgDeclare : DbgDeclares)
DbgVariables.push_back(DbgDeclare);
for (auto *DbgAssign : at::getAssignmentMarkers(&AI))
DbgVariables.push_back(DbgAssign);

for (DbgVariableIntrinsic *DbgVariable : DbgVariables) {
auto MigrateOne = [&](auto *DbgVariable) {
auto *Expr = DbgVariable->getExpression();
DIBuilder DIB(*AI.getModule(), /*AllowUnresolved*/ false);
uint64_t AllocaSize =
Expand Down Expand Up @@ -5001,37 +5022,33 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
// Remove any existing intrinsics on the new alloca describing
// the variable fragment.
SmallVector<DbgDeclareInst *, 1> FragDbgDeclares;
findDbgDeclares(FragDbgDeclares, Fragment.Alloca);
for (DbgDeclareInst *OldDII : FragDbgDeclares) {
auto SameVariableFragment = [](const DbgVariableIntrinsic *LHS,
const DbgVariableIntrinsic *RHS) {
SmallVector<DPValue *, 1> FragDPVs;
findDbgDeclares(FragDbgDeclares, Fragment.Alloca, &FragDPVs);
auto RemoveOne = [DbgVariable](auto *OldDII) {
auto SameVariableFragment = [](const auto *LHS, const auto *RHS) {
return LHS->getVariable() == RHS->getVariable() &&
LHS->getDebugLoc()->getInlinedAt() ==
RHS->getDebugLoc()->getInlinedAt();
};
if (SameVariableFragment(OldDII, DbgVariable))
OldDII->eraseFromParent();
}
};
for_each(FragDbgDeclares, RemoveOne);
for_each(FragDPVs, RemoveOne);

if (auto *DbgAssign = dyn_cast<DbgAssignIntrinsic>(DbgVariable)) {
if (!Fragment.Alloca->hasMetadata(LLVMContext::MD_DIAssignID)) {
Fragment.Alloca->setMetadata(
LLVMContext::MD_DIAssignID,
DIAssignID::getDistinct(AI.getContext()));
}
auto *NewAssign = DIB.insertDbgAssign(
Fragment.Alloca, DbgAssign->getValue(), DbgAssign->getVariable(),
FragmentExpr, Fragment.Alloca, DbgAssign->getAddressExpression(),
DbgAssign->getDebugLoc());
NewAssign->setDebugLoc(DbgAssign->getDebugLoc());
LLVM_DEBUG(dbgs() << "Created new assign intrinsic: " << *NewAssign
<< "\n");
} else {
DIB.insertDeclare(Fragment.Alloca, DbgVariable->getVariable(),
FragmentExpr, DbgVariable->getDebugLoc(), &AI);
}
insertNewDbgInst(DIB, DbgVariable, Fragment.Alloca, FragmentExpr, &AI);
}
}
};

// Migrate debug information from the old alloca to the new alloca(s)
// and the individual partitions.
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
SmallVector<DPValue *, 1> DPValues;
findDbgDeclares(DbgDeclares, &AI, &DPValues);
for_each(DbgDeclares, MigrateOne);
for_each(DPValues, MigrateOne);
for_each(at::getAssignmentMarkers(&AI), MigrateOne);

return Changed;
}

Expand Down Expand Up @@ -5153,9 +5170,12 @@ bool SROA::deleteDeadInstructions(
if (AllocaInst *AI = dyn_cast<AllocaInst>(I)) {
DeletedAllocas.insert(AI);
SmallVector<DbgDeclareInst *, 1> DbgDeclares;
findDbgDeclares(DbgDeclares, AI);
SmallVector<DPValue *, 1> DPValues;
findDbgDeclares(DbgDeclares, AI, &DPValues);
for (DbgDeclareInst *OldDII : DbgDeclares)
OldDII->eraseFromParent();
for (DPValue *OldDII : DPValues)
OldDII->eraseFromParent();
}

at::deleteAssignmentMarkers(I);
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/ARM/sroa-complex.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -passes='sroa' -S -o - %s | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -passes='sroa' -S -o - %s | FileCheck %s
target datalayout = "e-m:o-p:32:32-i64:64-v128:64:128-a:0:32-n32-S64"
target triple = "thumbv7-apple-unknown-macho"

Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/Generic/sroa-larger.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -passes='sroa' -S -o - %s | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -passes='sroa' -S -o - %s | FileCheck %s
; Generated from clang -c -O2 -g -target x86_64-pc-windows-msvc
; struct A {
; int _Myval2;
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/Generic/sroa-samesize.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -passes='sroa' -S -o - %s | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -passes='sroa' -S -o - %s | FileCheck %s
; Generated from clang -c -O2 -g -target x86_64-pc-windows-msvc
; struct A { double x1[]; };
; struct x2 {
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/X86/sroa-after-inlining.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt %s -passes='cgscc(function(sroa,instcombine),inline),function(instcombine,sroa),verify' -S -o - | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='cgscc(function(sroa,instcombine),inline),function(instcombine,sroa),verify' -S -o - | FileCheck %s
;
; This test checks that SROA pass processes debug info correctly if applied twice.
; Specifically, after SROA works first time, instcombine converts dbg.declare
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/X86/sroasplit-1.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt %s -passes='sroa,verify' -S -o - | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,verify' -S -o - | FileCheck %s
;
; Test that we can partial emit debug info for aggregates repeatedly
; split up by SROA.
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/X86/sroasplit-2.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt %s -passes='sroa,verify' -S -o - | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,verify' -S -o - | FileCheck %s
;
; Test that we can partial emit debug info for aggregates repeatedly
; split up by SROA.
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/DebugInfo/X86/sroasplit-3.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
; RUN: opt %s -passes='sroa,verify' -S -o - | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,verify' -S -o - | FileCheck %s

; ModuleID = 'test.c'
; Test that SROA updates the debug info correctly if an alloca was rewritten but
; not partitioned into multiple allocas.
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/X86/sroasplit-4.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -passes='sroa' < %s -S -o - | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -passes='sroa' < %s -S -o - | FileCheck %s
;
; Test that recursively splitting an alloca updates the debug info correctly.
; CHECK: %[[T:.*]] = load i64, ptr @t, align 8
Expand Down
2 changes: 2 additions & 0 deletions llvm/test/DebugInfo/X86/sroasplit-5.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
; RUN: opt %s -passes='sroa,verify' -S -o - | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,verify' -S -o - | FileCheck %s

; From:
; struct prog_src_register {
; unsigned : 4;
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/X86/sroasplit-dbg-declare.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt -S -passes='sroa' -o - %s | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -S -passes='sroa' -o - %s | FileCheck %s

; SROA should split the alloca in two new ones, each with its own dbg.declare.
; The original alloca and dbg.declare should be removed.
Expand Down
1 change: 1 addition & 0 deletions llvm/test/DebugInfo/salvage-overflow.ll
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
; RUN: opt %s -passes='sroa,early-cse' -S | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators %s -passes='sroa,early-cse' -S | FileCheck %s
; CHECK: DIExpression(DW_OP_constu, 9223372036854775808, DW_OP_minus, DW_OP_stack_value)
; Created from the following C input (and then delta-reduced the IR):
;
Expand Down
1 change: 1 addition & 0 deletions llvm/test/Transforms/Util/dbg-user-of-aext.ll
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
; (here exposed through the SROA) pass refers to [s|z]exts of values (as
; opposed to the operand of a [s|z]ext).
; RUN: opt -S -passes='sroa' %s | FileCheck %s
; RUN: opt --try-experimental-debuginfo-iterators -S -passes='sroa' %s | FileCheck %s

; Built from:
; struct foo { bool b; long i; };
Expand Down