Skip to content

Commit 190a46c

Browse files
committed
[DebugInfo] Add special cases for copy forwarding
1 parent f3138b7 commit 190a46c

File tree

4 files changed

+89
-3
lines changed

4 files changed

+89
-3
lines changed

lib/SILOptimizer/Transforms/CopyForwarding.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
#include "swift/SILOptimizer/PassManager/Passes.h"
7070
#include "swift/SILOptimizer/PassManager/Transforms.h"
7171
#include "swift/SILOptimizer/Utils/CFGOptUtils.h"
72+
#include "swift/SILOptimizer/Utils/DebugOptUtils.h"
7273
#include "swift/SILOptimizer/Utils/ValueLifetime.h"
7374
#include "llvm/ADT/SetVector.h"
7475
#include "llvm/ADT/Statistic.h"
@@ -698,6 +699,8 @@ propagateCopy(CopyAddrInst *CopyInst) {
698699
SILBuilderWithScope(CurrentCopy)
699700
.createDestroyAddr(CurrentCopy->getLoc(), CurrentCopy->getDest());
700701
}
702+
swift::salvageStoreDebugInfo(CurrentCopy, CurrentCopy->getSrc(),
703+
CurrentCopy->getDest());
701704
CurrentCopy->eraseFromParent();
702705
HasChanged = true;
703706
++NumCopyForward;
@@ -706,6 +709,8 @@ propagateCopy(CopyAddrInst *CopyInst) {
706709
// Forward propagation failed. Attempt to backward propagate.
707710
if (CurrentCopy->isInitializationOfDest() && backwardPropagateCopy()) {
708711
LLVM_DEBUG(llvm::dbgs() << " Reversing Copy:" << *CurrentCopy);
712+
swift::salvageStoreDebugInfo(CurrentCopy, CurrentCopy->getDest(),
713+
CurrentCopy->getSrc());
709714
CurrentCopy->eraseFromParent();
710715
HasChanged = true;
711716
++NumCopyBackward;
@@ -817,6 +822,9 @@ forwardDeadTempCopy(CopyAddrInst *destCopy) {
817822
.createDestroyAddr(srcCopy->getLoc(), srcCopy->getDest());
818823
}
819824

825+
// Salvage debug values before deleting them.
826+
swift::salvageStoreDebugInfo(srcCopy, srcCopy->getSrc(), srcCopy->getDest());
827+
820828
// Delete all dead debug_value instructions
821829
for (auto *deadDebugUser : debugInstsToDelete) {
822830
deadDebugUser->eraseFromParent();
@@ -1190,9 +1198,13 @@ bool CopyForwarding::backwardPropagateCopy() {
11901198
// Now that an init was found, it is safe to substitute all recorded uses
11911199
// with the copy's dest.
11921200
for (auto *Oper : ValueUses) {
1193-
Oper->set(CurrentCopy->getDest());
1194-
if (isa<CopyAddrInst>(Oper->getUser()))
1201+
if (auto *SI = dyn_cast<CopyAddrInst>(Oper->getUser())) {
11951202
HasForwardedToCopy = true;
1203+
// This instruction gets "replaced", so we need to salvage its previous
1204+
// debug info.
1205+
swift::salvageStoreDebugInfo(SI, SI->getSrc(), SI->getDest());
1206+
}
1207+
Oper->set(CurrentCopy->getDest());
11961208
}
11971209
return true;
11981210
}

lib/SILOptimizer/Utils/InstOptUtils.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,9 @@ static void transferStoreDebugValue(DebugVarCarryingInst DefiningInst,
18391839
VarInfo->Scope = DefiningInst->getDebugScope();
18401840
if (!isa<CopyAddrInst>(SI) && VarInfo->DIExpr.startsWithDeref())
18411841
VarInfo->DIExpr.eraseElement(VarInfo->DIExpr.element_begin());
1842+
else if (isa<CopyAddrInst>(SI) && !VarInfo->DIExpr.startsWithDeref())
1843+
VarInfo->DIExpr.prependElements({
1844+
SILDIExprElement::createOperator(SILDIExprOperator::Dereference)});
18421845
// Note: The instruction should logically be in the SI's scope.
18431846
// However, LLVM does not support variables and stores in different scopes,
18441847
// so we use the variable's scope.

test/DebugInfo/copyforward.sil

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
// RUN: %target-sil-opt -enable-sil-verify-all -sil-print-debuginfo -copy-forwarding %s | %FileCheck %s
2+
sil_stage canonical
3+
4+
import Builtin
5+
import Swift
6+
7+
sil_scope 1 { loc "backward.swift":66:24 parent @backward : $@convention(method) <τ_0_0> (@inout CollectionOfOne<τ_0_0>.Iterator) -> @out Optional<τ_0_0> }
8+
sil_scope 2 { loc "backward.swift":67:9 parent 1 }
9+
10+
// CHECK-LABEL: sil {{.+}} @backward
11+
sil [serialized] [ossa] @backward : $@convention(method) <Element> (@inout CollectionOfOne<Element>.Iterator) -> @out Optional<Element> {
12+
[%0: write v**]
13+
[%1: read s0.v**, write s0.v**, copy s0.v**, destroy s0.v**]
14+
[global: read,write,copy,destroy,allocate,deinit_barrier]
15+
bb0(%0 : $*Optional<Element>, %1 : $*CollectionOfOne<Element>.Iterator):
16+
debug_value %1 : $*CollectionOfOne<Element>.Iterator, var, name "self", argno 1, expr op_deref, loc "backward.swift":66:24, scope 1
17+
%3 = alloc_stack [lexical] [var_decl] $Optional<Element>, let, name "result", loc "backward.swift":67:9, scope 2
18+
%4 = struct_element_addr %1 : $*CollectionOfOne<Element>.Iterator, #CollectionOfOne.Iterator._elements, loc "backward.swift":67:18, scope 1
19+
20+
// CHECK: debug_value %{{[0-9]+}} : $*Optional<Element>, let, (name "result", loc "backward.swift":67:9, scope 2), expr op_deref, loc "backward.swift":67:18, scope 2
21+
22+
copy_addr %4 to [init] %3 : $*Optional<Element>, loc "backward.swift":67:18, scope 1
23+
%6 = alloc_stack $Optional<Element>, loc "backward.swift":68:17, scope 2
24+
inject_enum_addr %6 : $*Optional<Element>, #Optional.none!enumelt, loc "backward.swift":68:17, scope 2
25+
%8 = struct_element_addr %1 : $*CollectionOfOne<Element>.Iterator, #CollectionOfOne.Iterator._elements, loc "backward.swift":68:15, scope 2
26+
copy_addr [take] %6 to %8 : $*Optional<Element>, loc "backward.swift":68:15, scope 2
27+
dealloc_stack %6 : $*Optional<Element>, loc "backward.swift":68:17, scope 2
28+
copy_addr [take] %3 to [init] %0 : $*Optional<Element>, loc "backward.swift":69:12, scope 2
29+
dealloc_stack %3 : $*Optional<Element>, loc "backward.swift":70:3, scope 2
30+
%13 = tuple (), loc "backward.swift":70:3, scope 2
31+
return %13 : $(), loc "backward.swift":69:5, scope 2
32+
} // end sil function 'backward'
33+
34+
sil_scope 10 { loc "tempcopy.swift":714:27 parent @tempcopy : $@convention(method) <τ_0_0 where τ_0_0 : Strideable, τ_0_0.Stride : SignedInteger> (@in PartialRangeFrom<τ_0_0>) -> @out PartialRangeFrom<τ_0_0>.Iterator }
35+
sil_scope 12 { loc "tempcopy.swift":715:12 parent 10 }
36+
37+
// CHECK-LABEL: sil {{.+}} @tempcopy
38+
sil [serialized] [ossa] @tempcopy : $@convention(method) <Bound where Bound : Strideable, Bound.Stride : SignedInteger> (@in PartialRangeFrom<Bound>) -> @out PartialRangeFrom<Bound>.Iterator {
39+
[%0: write v**]
40+
[%1: read v**, write v**, copy s0.v**, destroy v**]
41+
[global: read,write,copy,destroy,allocate,deinit_barrier]
42+
bb0(%0 : $*PartialRangeFrom<Bound>.Iterator, %1 : $*PartialRangeFrom<Bound>):
43+
debug_value %1 : $*PartialRangeFrom<Bound>, let, name "self", argno 1, expr op_deref, loc "tempcopy.swift":714:27, scope 10 // id: %2
44+
%3 = struct_element_addr %1 : $*PartialRangeFrom<Bound>, #PartialRangeFrom.lowerBound, loc "tempcopy.swift":715:31, scope 10 // user: %5
45+
%4 = alloc_stack $Bound, loc "tempcopy.swift":715:31, scope 10 // users: %8, %6, %10, %5
46+
copy_addr [take] %3 to [init] %4 : $*Bound, loc "tempcopy.swift":715:31, scope 10 // id: %5
47+
// CHECK: debug_value %{{[0-9]+}} : $*Bound, let, (name "_current", loc "tempcopy.swift":696:17
48+
// CHECK-SAME: expr op_deref, loc "tempcopy.swift":715:31
49+
debug_value %4 : $*Bound, let, name "_current", expr op_deref, loc "tempcopy.swift":696:17, scope 12 // id: %6
50+
%7 = struct_element_addr %0 : $*PartialRangeFrom<Bound>.Iterator, #PartialRangeFrom.Iterator._current, loc "tempcopy.swift":696:50, scope 12 // user: %8
51+
copy_addr [take] %4 to [init] %7 : $*Bound, loc "tempcopy.swift":696:50, scope 12 // id: %8
52+
%9 = tuple (), loc * "tempcopy.swift":696:61, scope 12
53+
dealloc_stack %4 : $*Bound, loc "tempcopy.swift":715:41, scope 10 // id: %10
54+
%11 = tuple (), loc "tempcopy.swift":716:3, scope 10 // user: %12
55+
return %11 : $(), loc "tempcopy.swift":715:5, scope 10 // id: %12
56+
} // end sil function 'tempcopy'
57+
58+
sil @f_in : $@convention(thin) <T> (@in T) -> ()
59+
60+
// CHECK-LABEL: sil hidden [ossa] @forward_takeinit :
61+
sil hidden [ossa] @forward_takeinit : $@convention(thin) <T> (@in T) -> () {
62+
bb0(%0 : $*T):
63+
// CHECK: debug_value %0 : $*T, let, (name "hello",{{.+}} expr op_deref
64+
%l1 = alloc_stack $T, let, name "hello"
65+
copy_addr [take] %0 to [init] %l1 : $*T
66+
%f1 = function_ref @f_in : $@convention(thin) <τ_0_0> (@in τ_0_0) -> ()
67+
%c1 = apply %f1<T>(%l1) : $@convention(thin) <τ_0_0> (@in τ_0_0) -> ()
68+
dealloc_stack %l1 : $*T
69+
%r1 = tuple ()
70+
return %r1 : $()
71+
}

test/SILOptimizer/copyforward_ossa.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ public struct S<T> {
284284
// CHECK-LABEL: sil [ossa] @deadtemp :
285285
// CHECK: %[[G:.*]] = struct_element_addr %0 : $*S<T>, #S.g
286286
// CHECK-NOT: copy_addr
287-
// CHECK-NOT: debug_value {{.*}} expr op_deref
287+
// CHECK: debug_value %[[G:.*]] : $*T, expr op_deref
288288
// CHECK: %[[F:.*]] = struct_element_addr %0 : $*S<T>, #S.f
289289
// CHECK: copy_addr %[[G]] to %[[F]] : $*T
290290
// CHECK-LABEL: } // end sil function 'deadtemp'

0 commit comments

Comments
 (0)