Skip to content

[PartialApplyCombiner] Handled moved PAIs. #65369

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 24, 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
7 changes: 4 additions & 3 deletions lib/SILOptimizer/Utils/InstOptUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -820,9 +820,10 @@ static bool useHasTransitiveOwnership(const SILInstruction *inst) {
if (isa<ConvertEscapeToNoEscapeInst>(inst))
return true;

// Look through copy_value, begin_borrow. They are inert for our purposes, but
// we need to look through it.
return isa<CopyValueInst>(inst) || isa<BeginBorrowInst>(inst);
// Look through copy_value, begin_borrow, move_value. They are inert for our
// purposes, but we need to look through it.
return isa<CopyValueInst>(inst) || isa<BeginBorrowInst>(inst) ||
isa<MoveValueInst>(inst);
}

static bool shouldDestroyPartialApplyCapturedArg(SILValue arg,
Expand Down
9 changes: 5 additions & 4 deletions lib/SILOptimizer/Utils/PartialApplyCombiner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,11 @@ bool PartialApplyCombiner::combine() {
auto *use = worklist.pop_back_val();
auto *user = use->getUser();

// Recurse through copy_value
if (isa<CopyValueInst>(user) || isa<BeginBorrowInst>(user)) {
for (auto *copyUse : cast<SingleValueInstruction>(user)->getUses())
worklist.push_back(copyUse);
// Recurse through ownership instructions.
if (isa<CopyValueInst>(user) || isa<BeginBorrowInst>(user) ||
isa<MoveValueInst>(user)) {
for (auto *ownershipUse : cast<SingleValueInstruction>(user)->getUses())
worklist.push_back(ownershipUse);
continue;
}

Expand Down
22 changes: 21 additions & 1 deletion test/SILOptimizer/mandatory_generic_specialization.sil
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import Builtin

sil @paable : $@convention(thin) (Builtin.Int64) -> ()
sil @moved_pai_callee : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()

sil [ossa] [transparent] @partial_apply_on_stack_nesting_violator : $@convention(thin) <T> () -> () {
%paable = function_ref @paable : $@convention(thin) (Builtin.Int64) -> ()
Expand Down Expand Up @@ -34,4 +35,23 @@ sil [no_locks] @test_inline_stack_violating_ossa_func : $@convention(thin) () ->
return %retval : $()
}


// CHECK-LABEL: sil hidden [no_allocation] [ossa] @moved_pai : {{.*}} {
// CHECK-NOT: partial_apply
// CHECK-LABEL: } // end sil function 'moved_pai'
sil hidden [no_allocation] [ossa] @moved_pai : $@convention(thin) () -> Builtin.Int64 {
bb0:
%addr = alloc_stack $Builtin.Int64
%42 = integer_literal $Builtin.Int64, 42
store %42 to [trivial] %addr : $*Builtin.Int64
%callee = function_ref @moved_pai_callee : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%closure = partial_apply [callee_guaranteed] %callee(%addr) : $@convention(thin) (@inout_aliasable Builtin.Int64) -> ()
%closure_lifetime = move_value [lexical] %closure : $@callee_guaranteed () -> ()
debug_value %closure_lifetime : $@callee_guaranteed () -> ()
%copy = copy_value %closure_lifetime : $@callee_guaranteed () -> ()
apply %copy() : $@callee_guaranteed () -> ()
destroy_value %copy : $@callee_guaranteed () -> ()
%retval = load [trivial] %addr : $*Builtin.Int64
destroy_value %closure_lifetime : $@callee_guaranteed () -> ()
dealloc_stack %addr : $*Builtin.Int64
return %retval : $Builtin.Int64
}