Skip to content

Preserve extend_lifetime during dead instruction code elimination. #77575

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 2 commits into from
Nov 13, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,10 @@ extension Instruction {
// Don't remove UncheckedEnumDataInst in OSSA in case it is responsible
// for consuming an enum value.
return !parentFunction.hasOwnership
case is ExtendLifetimeInst:
// An extend_lifetime can only be removed if the operand is also removed.
// If its operand is trivial, it will be removed by MandatorySimplification.
return false
default:
break
}
Expand Down
17 changes: 4 additions & 13 deletions lib/SILOptimizer/Mandatory/MoveOnlyWrappedTypeEliminator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -311,19 +311,6 @@ bool SILMoveOnlyWrappedTypeEliminator::process() {
// - record its users for later visitation
auto visitValue = [&touchedInsts, fn = fn,
trivialOnly = trivialOnly](SILValue value) -> bool {
// Trivial move_value instructions are relevant. After they are stripped,
// any extend_lifetime uses are also stripped.
if (isa<MoveValueInst>(value)
&& value->getOwnershipKind() == OwnershipKind::None) {
for (auto *use : value->getNonTypeDependentUses()) {
auto *user = use->getUser();
if (isa<ExtendLifetimeInst>(user)) {
touchedInsts.insert(user);
}
}
return true;
}

if (!value->getType().hasAnyMoveOnlyWrapping(fn))
return false;

Expand Down Expand Up @@ -366,6 +353,10 @@ bool SILMoveOnlyWrappedTypeEliminator::process() {

touched = true;
}
// delete trivial move_value and extend_lifetime instructions.
if (isa<MoveValueInst>(ii) || isa<ExtendLifetimeInst>(ii)) {
touched = true;
}
if (!touched)
continue;
touchedInsts.insert(&ii);
Expand Down
37 changes: 37 additions & 0 deletions test/SILOptimizer/moveonly_type_eliminator.sil
Original file line number Diff line number Diff line change
Expand Up @@ -637,3 +637,40 @@ bb0:
%13 = tuple ()
return %13 : $()
}

// Remove trivial move_value + extend_lifetime
//
// CHECK-LABEL: sil [ossa] @move_extend_lifetime : $@convention(thin) (@thin Trivial.Type) -> () {
// CHECK: bb0(%0 : $@thin Trivial.Type):
// CHECK-NOT: move_value
// CHECK-NOT: extend_lifetime
// CHECK-LABEL: } // end sil function 'move_extend_lifetime'
sil [ossa] @move_extend_lifetime : $@convention(thin) (@thin Trivial.Type) -> () {
bb0(%0 : $@thin Trivial.Type):
%mv = move_value [var_decl] %0 : $@thin Trivial.Type
extend_lifetime %mv : $@thin Trivial.Type
%13 = tuple ()
return %13 : $()
}

sil @captureType : $@convention(thin) (@thin Trivial.Type) -> ()
sil @takeClosure : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()

// SILGen pattern binding can (incorrectly) create a local variable to hold the value a function argument. This results
// in an extend_lifetime that directly uses the argument value. Ensure that MoveOnlyTypeEliminator erases the
// extend_lifetime.
//
// CHECK-LABEL: sil [ossa] @arg_extend_lifetime : $@convention(thin) (@thin Trivial.Type) -> () {
// CHECK-NOT: extend_lifetime
// CHECK-LABEL: } // end sil function 'arg_extend_lifetime'
sil [ossa] @arg_extend_lifetime : $@convention(thin) (@thin Trivial.Type) -> () {
bb0(%0 : $@thin Trivial.Type):
%cf = function_ref @captureType : $@convention(thin) (@thin Trivial.Type) -> ()
%closure = partial_apply [callee_guaranteed] [on_stack] %cf(%0) : $@convention(thin) (@thin Trivial.Type) -> ()
%f = function_ref @takeClosure : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()
%6 = apply %f(%closure) : $@convention(thin) (@guaranteed @noescape @callee_guaranteed () -> ()) -> ()
destroy_value %closure : $@noescape @callee_guaranteed () -> ()
extend_lifetime %0 : $@thin Trivial.Type
%13 = tuple ()
return %13 : $()
}
21 changes: 21 additions & 0 deletions test/SILOptimizer/simplify_extend_lifetime.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: %target-sil-opt -enable-sil-verify-all %s -onone-simplification | %FileCheck %s

// REQUIRES: swift_in_compiler

sil_stage raw

import Builtin
import Swift
import SwiftShims

// extend_lifetime should not be eliminated as dead code.
//
// CHECK-LABEL: sil [ossa] @do_not_remove_extend : $@convention(thin) (Int) -> () {
// CHECK: extend_lifetime %0 : $Int
// CHECK-LABEL: } // end sil function 'do_not_remove_extend'
sil [ossa] @do_not_remove_extend : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
extend_lifetime %0 : $Int
%4 = tuple ()
return %4 : $()
}