Skip to content

Commit 2fce87c

Browse files
committed
EscapeAnalysis: fix a wrong use-point detection.
This caused redundant load elimination to remove a load although the value is overwritten in a called function. Most likely this could only occur if the load address is a block argument. fixes SR-4393
1 parent e822ca3 commit 2fce87c

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

lib/SILOptimizer/Analysis/EscapeAnalysis.cpp

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1818,9 +1818,16 @@ bool EscapeAnalysis::canObjectOrContentEscapeTo(SILValue V, FullApplySite FAS) {
18181818
if (ConGraph->isUsePoint(UsePoint, Node))
18191819
return true;
18201820

1821-
if (hasReferenceSemantics(V->getType())) {
1822-
// Check if the object "content", i.e. a pointer to one of its stored
1823-
// properties, can escape to the called function.
1821+
if (isPointer(V)) {
1822+
// Check if the object "content" can escape to the called function.
1823+
// This will catch cases where V is a reference and a pointer to a stored
1824+
// property escapes.
1825+
// It's also important in case of a pointer assignment, e.g.
1826+
// V = V1
1827+
// apply(V1)
1828+
// In this case the apply is only a use-point for V1 and V1's content node.
1829+
// As V1's content node is the same as V's content node, we also make the
1830+
// check for the content node.
18241831
CGNode *ContentNode = ConGraph->getContentNode(Node);
18251832
if (ContentNode->escapesInsideFunction(false))
18261833
return true;

test/SILOptimizer/redundant_load_elim.sil

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,3 +1166,27 @@ bb0(%0 : $Builtin.RawPointer):
11661166
return %6 : $Int
11671167
}
11681168

1169+
sil @overwrite_int : $@convention(thin) (@inout Int, Int) -> ()
1170+
1171+
// CHECK-LABEL: sil @test_address_block_args
1172+
// CHECK: bb2({{.*}}):
1173+
// CHECK: apply
1174+
// CHECK: [[L:%.*]] = load
1175+
// CHECK: return [[L]]
1176+
sil @test_address_block_args : $@convention(thin) (Int) -> Int {
1177+
bb0(%0 : $Int):
1178+
%4 = alloc_stack $Int
1179+
store %0 to %4 : $*Int
1180+
cond_br undef, bb1(%4 : $*Int), bb2(%4 : $*Int)
1181+
1182+
bb1(%a1 : $*Int):
1183+
br bb2(%a1 : $*Int)
1184+
1185+
bb2(%a : $*Int):
1186+
%l1 = load %a : $*Int
1187+
%60 = function_ref @overwrite_int : $@convention(thin) (@inout Int, Int) -> ()
1188+
%61 = apply %60(%4, %l1) : $@convention(thin) (@inout Int, Int) -> ()
1189+
%r = load %a : $*Int
1190+
dealloc_stack %4 : $*Int
1191+
return %r : $Int
1192+
}

0 commit comments

Comments
 (0)