Skip to content

ValueLifetimeAnalysis: fix the case if the last use of the value is a… #2254

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 1 commit into from
Apr 21, 2016
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
13 changes: 10 additions & 3 deletions lib/SILOptimizer/Utils/Local.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,9 +1158,16 @@ bool ValueLifetimeAnalysis::computeFrontier(Frontier &Fr, Mode mode) {
// The value is not live in any of the successor blocks. This means the
// block contains a last use of the value. The next instruction after
// the last use is part of the frontier.
SILBasicBlock::iterator Iter(findLastUserInBlock(BB));
Fr.push_back(&*next(Iter));
} else if (DeadInSucc && mode != IgnoreExitEdges) {
SILInstruction *LastUser = findLastUserInBlock(BB);
if (!isa<TermInst>(LastUser)) {
Fr.push_back(&*next(LastUser->getIterator()));
continue;
}
// In case the last user is a TermInst we add all successor blocks to the
// frontier (see below).
assert(DeadInSucc && "The final using TermInst must have successors");
}
if (DeadInSucc && mode != IgnoreExitEdges) {
// The value is not live in some of the successor blocks.
LiveOutBlocks.insert(BB);
for (const SILSuccessor &Succ : BB->getSuccessors()) {
Expand Down
48 changes: 48 additions & 0 deletions test/SILOptimizer/closure_specialize.sil
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,15 @@ bb0(%0 : $@callee_owned (@owned A) -> ()):
return %3 : $()
}

// CHECK-LABEL: sil shared {{.*}} @_TTS{{.*}}use_closure_throw : $@convention(thin) (@owned A) -> @error ErrorProtocol {
sil hidden [noinline] @use_closure_throw : $@convention(thin) (@owned @callee_owned (@owned A) -> ()) -> @error ErrorProtocol {
bb0(%0 : $@callee_owned (@owned A) -> ()):
%1 = alloc_ref $A
%2 = apply %0(%1) : $@callee_owned (@owned A) -> ()
%3 = tuple ()
return %3 : $()
}

// CHECK-LABEL: sil {{.*}} @different_execution_counts
// CHECK: bb0([[ARG:%.*]] : $A
// CHECK: strong_retain [[ARG]]
Expand Down Expand Up @@ -373,6 +382,45 @@ bb3:
return %11 : $()
}

// CHECK-LABEL: sil @insert_release_after_try_apply
// CHECK: bb0(%0 : $A):
// CHECK: retain_value %0
// CHECK: bb1:
// CHECK: retain_value %0
// CHECK-NEXT: try_apply
// CHECK: bb2(%{{[0-9]+}} : $()):
// CHECK-NEXT: strong_release %0
// CHECK-NEXT: release_value %0
// CHECK-NEXT: br bb4
// CHECK: bb3(%{{[0-9]+}} : $ErrorProtocol):
// CHECK-NEXT: strong_release %0
// CHECK-NEXT: release_value %0
// CHECK-NEXT: br bb4
// CHECK: bb4:
// CHECK-NOT: %0
// CHECK: return
sil @insert_release_after_try_apply : $@convention(thin) (@guaranteed A) -> () {
bb0(%0 : $A):
%2 = function_ref @closure : $@convention(thin) (@owned A, @owned A) -> ()
%3 = partial_apply %2(%0) : $@convention(thin) (@owned A, @owned A) -> ()
%8 = function_ref @use_closure_throw : $@convention(thin) (@owned @callee_owned (@owned A) -> ()) -> @error ErrorProtocol
br bb1

bb1:
strong_retain %3 : $@callee_owned (@owned A) -> ()
try_apply %8(%3) : $@convention(thin) (@owned @callee_owned (@owned A) -> ()) -> @error ErrorProtocol, normal bb2, error bb3

bb2(%n : $()):
br bb4

bb3(%e : $ErrorProtocol):
br bb4

bb4:
%11 = tuple ()
return %11 : $()
}


// Ensure that we can specialize and properly mangle functions that take closures with @box arguments.

Expand Down