Skip to content

Commit 86b6db9

Browse files
committed
[DebugInfo] Add special cases for copy forwarding
1 parent f2cbf59 commit 86b6db9

File tree

3 files changed

+77
-5
lines changed

3 files changed

+77
-5
lines changed

lib/SILOptimizer/Transforms/CopyForwarding.cpp

Lines changed: 20 additions & 4 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
826+
swift::salvageStoreDebugInfo(srcCopy, srcCopy->getSrc(), srcCopy->getDest());
827+
820828
// Delete all dead debug_value instructions
821829
for (auto *deadDebugUser : debugInstsToDelete) {
822830
deadDebugUser->eraseFromParent();
@@ -1061,9 +1069,13 @@ bool CopyForwarding::forwardPropagateCopy() {
10611069
// Now that a deinit was found, it is safe to substitute all recorded uses
10621070
// with the copy's source.
10631071
for (auto *Oper : ValueUses) {
1064-
Oper->set(CurrentCopy->getSrc());
1065-
if (isa<CopyAddrInst>(Oper->getUser()))
1072+
if (isa<CopyAddrInst>(Oper->getUser())) {
10661073
HasForwardedToCopy = true;
1074+
// This instruction gets "replaced", so we need to salvage its previous
1075+
// debug info
1076+
// swift::salvageDebugInfo(Oper->getUser());
1077+
}
1078+
Oper->set(CurrentCopy->getSrc());
10671079
}
10681080
return true;
10691081
}
@@ -1190,9 +1202,13 @@ bool CopyForwarding::backwardPropagateCopy() {
11901202
// Now that an init was found, it is safe to substitute all recorded uses
11911203
// with the copy's dest.
11921204
for (auto *Oper : ValueUses) {
1193-
Oper->set(CurrentCopy->getDest());
1194-
if (isa<CopyAddrInst>(Oper->getUser()))
1205+
if (auto *SI = dyn_cast<CopyAddrInst>(Oper->getUser())) {
11951206
HasForwardedToCopy = true;
1207+
// This instruction gets "replaced", so we need to salvage its previous
1208+
// debug info
1209+
swift::salvageStoreDebugInfo(SI, SI->getSrc(), SI->getDest());
1210+
}
1211+
Oper->set(CurrentCopy->getDest());
11961212
}
11971213
return true;
11981214
}

test/DebugInfo/copyforward.sil

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
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), 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'

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)