Skip to content

Commit 3e28874

Browse files
authored
Merge pull request #8535 from eeckstein/fix-vla
ValueLifetimeAnalysis: fix the lifetime computation in case the value definition is in a single-block loop.
2 parents 63d064f + 805960b commit 3e28874

File tree

3 files changed

+42
-3
lines changed

3 files changed

+42
-3
lines changed

include/swift/SILOptimizer/Utils/Local.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,11 @@ class ValueLifetimeAnalysis {
277277

278278
/// Returns the last use of the value in the live block \p BB.
279279
SILInstruction *findLastUserInBlock(SILBasicBlock *BB);
280+
281+
/// Returns true if the value is alive at the begin of block \p BB.
282+
bool isAliveAtBeginOfBlock(SILBasicBlock *BB) {
283+
return LiveBlocks.count(BB) && BB != DefValue->getParentBlock();
284+
}
280285
};
281286

282287
/// Base class for BB cloners.

lib/SILOptimizer/Utils/Local.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1082,7 +1082,7 @@ bool ValueLifetimeAnalysis::computeFrontier(Frontier &Fr, Mode mode) {
10821082
bool LiveInSucc = false;
10831083
bool DeadInSucc = false;
10841084
for (const SILSuccessor &Succ : BB->getSuccessors()) {
1085-
if (LiveBlocks.count(Succ)) {
1085+
if (isAliveAtBeginOfBlock(Succ)) {
10861086
LiveInSucc = true;
10871087
} else {
10881088
DeadInSucc = true;
@@ -1105,7 +1105,7 @@ bool ValueLifetimeAnalysis::computeFrontier(Frontier &Fr, Mode mode) {
11051105
// The value is not live in some of the successor blocks.
11061106
LiveOutBlocks.insert(BB);
11071107
for (const SILSuccessor &Succ : BB->getSuccessors()) {
1108-
if (!LiveBlocks.count(Succ)) {
1108+
if (!isAliveAtBeginOfBlock(Succ)) {
11091109
// It's an "exit" edge from the lifetime region.
11101110
FrontierBlocks.insert(Succ);
11111111
}
@@ -1167,7 +1167,7 @@ bool ValueLifetimeAnalysis::isWithinLifetime(SILInstruction *Inst) {
11671167
// live at the end of BB and therefore Inst is definitely in the lifetime
11681168
// region (Note that we don't check in upward direction against the value's
11691169
// definition).
1170-
if (LiveBlocks.count(Succ))
1170+
if (isAliveAtBeginOfBlock(Succ))
11711171
return true;
11721172
}
11731173
// The value is live in the block but not at the end of the block. Check if

test/SILOptimizer/dead_array_elim.sil

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,37 @@ bb3:
216216
return %18 : $()
217217
}
218218

219+
// CHECK-LABEL: sil @dead_array_in_single_cycle_loop
220+
// CHECK: bb0(%0 : $TrivialDestructor):
221+
// CHECK-NEXT: br bb1
222+
// CHECK: bb1:
223+
// CHECK-NEXT: strong_retain %0
224+
// CHECK-NEXT: strong_release %0
225+
// CHECK-NEXT: cond_br
226+
// CHECK: bb2:
227+
// CHECK-NEXT: tuple
228+
// CHECK-NEXT: return
229+
sil @dead_array_in_single_cycle_loop : $@convention(thin) (@guaranteed TrivialDestructor) -> () {
230+
bb0(%0 : $TrivialDestructor):
231+
br bb1
232+
233+
bb1:
234+
%2 = integer_literal $Builtin.Word, 2
235+
%3 = function_ref @allocArray : $@convention(thin) <τ_0_0> (Builtin.Word) -> @owned (Array<τ_0_0>, Builtin.RawPointer)
236+
%4 = apply %3<TrivialDestructor>(%2) : $@convention(thin) <τ_0_0> (Builtin.Word) -> @owned (Array<τ_0_0>, Builtin.RawPointer)
237+
%5 = tuple_extract %4 : $(Array<TrivialDestructor>, Builtin.RawPointer), 0
238+
%6 = tuple_extract %4 : $(Array<TrivialDestructor>, Builtin.RawPointer), 1
239+
%7 = pointer_to_address %6 : $Builtin.RawPointer to [strict] $*TrivialDestructor
240+
%13 = struct_extract %5 : $Array<TrivialDestructor>, #Array._buffer
241+
%14 = struct_extract %13 : $_ArrayBuffer<TrivialDestructor>, #_ArrayBuffer._storage
242+
%15 = struct_extract %14 : $_BridgeStorage<_ContiguousArrayStorageBase, _NSArrayCore>, #_BridgeStorage.rawValue
243+
strong_retain %0 : $TrivialDestructor
244+
store %0 to %7 : $*TrivialDestructor
245+
strong_release %15 : $Builtin.BridgeObject
246+
cond_br undef, bb1, bb2
247+
248+
bb2:
249+
%18 = tuple ()
250+
return %18 : $()
251+
}
252+

0 commit comments

Comments
 (0)