Skip to content

[SemanticARCOpts] Don't shorten owned lexical values lifetimes through deinit barriers. #65164

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 14, 2023
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
15 changes: 15 additions & 0 deletions lib/SILOptimizer/SemanticARC/CopyValueOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include "SemanticARCOptVisitor.h"
#include "swift/Basic/Defer.h"
#include "swift/SIL/LinearLifetimeChecker.h"
#include "swift/SIL/MemAccessUtils.h"
#include "swift/SIL/OwnershipUtils.h"
#include "swift/SIL/Projection.h"

Expand Down Expand Up @@ -392,6 +393,20 @@ static bool tryJoinIfDestroyConsumingUseInSameBlock(
return true;
}

// The lifetime of the original ends after the lifetime of the copy. If the
// original is lexical, its lifetime must not be shortened through deinit
// barriers.
if (cvi->getOperand()->isLexical()) {
// At this point, visitedInsts contains all the instructions between the
// consuming use of the copy and the destroy. If any of those instructions
// is a deinit barrier, it would be illegal to shorten the original lexical
// value's lifetime to end at that consuming use. Bail if any are.
if (llvm::any_of(visitedInsts, [](auto *inst) {
return mayBeDeinitBarrierNotConsideringSideEffects(inst);
}))
return false;
}

// If we reached this point, isUseBetweenInstAndBlockEnd succeeded implying
// that we found destroy_value to be after our consuming use. Noting that
// additionally, the routine places all instructions in between consuming use
Expand Down
55 changes: 55 additions & 0 deletions test/SILOptimizer/semantic-arc-opts-lifetime-joining.sil
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,13 @@ struct NativeObjectWrapper {

sil @owned_user_object_pair : $@convention(thin) (@owned NativeObjectPair) -> ()

class X {}
struct S {}

sil @getX : $@convention(thin) () -> @owned X
sil @getS : $@convention(thin) (@owned X) -> @out S
sil @loadWeakX_from : $@convention(thin) (@in_guaranteed S) -> @owned FakeOptional<X>

///////////
// Tests //
///////////
Expand Down Expand Up @@ -923,3 +930,51 @@ bb0:
destroy_value %0 : ${ var Bool }
return %11 : $Bool
}

// Don't do this optimization:
// Eliminate copy of lexical value which ends after lifetime of copy IF there
// may be deinit barriers between the final consume and the copy.
//
// CHECK-LABEL: sil [ossa] @testDestroyedLexicalValue : {{.*}} {
// CHECK: [[GET:%[^,]+]] = function_ref @getX
// CHECK: [[X:%[^,]+]] = apply [[GET]]()
// CHECK: [[MX:%[^,]+]] = move_value [lexical] [[X]]
// CHECK: destroy_value [[MX]] : $X
// CHECK-LABEL: } // end sil function 'testDestroyedLexicalValue'
sil [ossa] @testDestroyedLexicalValue : $@convention(thin) () -> @owned FakeOptional<X> {
bb0:
%getX = function_ref @getX : $@convention(thin) () -> @owned X
%x = apply %getX() : $@convention(thin) () -> @owned X
%mx = move_value [lexical] %x : $X
%a = alloc_stack [lexical] $S, let, name "s"
%c = copy_value %mx : $X
%getS = function_ref @getS : $@convention(thin) (@owned X) -> @out S
%s = apply %getS(%a, %c) : $@convention(thin) (@owned X) -> @out S
%loadWeakX_from = function_ref @loadWeakX_from : $@convention(thin) (@in_guaranteed S) -> @owned FakeOptional<X>
%o = apply %loadWeakX_from(%a) : $@convention(thin) (@in_guaranteed S) -> @owned FakeOptional<X>
// ^^^^^ Deinit barrier
destroy_addr %a : $*S
dealloc_stack %a : $*S
destroy_value %mx : $X
return %o : $FakeOptional<X>
}

// CHECK-LABEL: sil [ossa] @testDestroyedLexicalValueNoBarriers : {{.*}} {
// CHECK-NOT: destroy_value
// CHECK-LABEL: } // end sil function 'testDestroyedLexicalValueNoBarriers'
sil [ossa] @testDestroyedLexicalValueNoBarriers : $@convention(thin) () -> () {
bb0:
%getX = function_ref @getX : $@convention(thin) () -> @owned X
%x = apply %getX() : $@convention(thin) () -> @owned X
%mx = move_value [lexical] %x : $X
%a = alloc_stack [lexical] $S, let, name "s"
%c = copy_value %mx : $X
%getS = function_ref @getS : $@convention(thin) (@owned X) -> @out S
%s = apply %getS(%a, %c) : $@convention(thin) (@owned X) -> @out S
destroy_addr %a : $*S
dealloc_stack %a : $*S
destroy_value %mx : $X
%r = tuple ()
return %r : $()
}