Skip to content

GenericSpecializer: fix an ownership verification failure when removing a partial_apply of a thunk #78351

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
Jan 2, 2025
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: 7 additions & 0 deletions lib/SILOptimizer/Utils/Generics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3507,6 +3507,13 @@ void swift::trySpecializeApplyOfGeneric(
}
if (auto *PAI = dyn_cast<PartialApplyInst>(User)) {
SILValue result = NewPAI;
if (NewPAI->getFunction()->hasOwnership()) {
auto convention = ApplySite(PAI).getCaptureConvention(*Use);
if (convention == SILArgumentConvention::Direct_Guaranteed) {
SILBuilderWithScope builder(Apply.getInstruction());
result = builder.createCopyValue(Apply.getLoc(), result);
}
}
if (SpecializedF.hasTypeReplacements()) {
SILBuilderWithScope builder(Apply.getInstruction());
auto fnType = PAI->getType();
Expand Down
31 changes: 31 additions & 0 deletions test/SILOptimizer/specialize_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1634,3 +1634,34 @@ bb0:
return %5 : $()
}

sil [ossa] @callee : $@convention(thin) <Result> (@inout Result, @in_guaranteed Int, Int) -> () {
bb0(%0 : $*Result, %1 : $*Int, %2 : $Int):
unreachable
}

sil [reabstraction_thunk] [ossa] @thunk : $@convention(thin) <τ_0_0> (@inout τ_0_0, Int, @guaranteed @noescape @callee_guaranteed (@inout τ_0_0, @in_guaranteed Int) -> ()) -> ()

// CHECK-LABEL: sil [ossa] @insert_copy_when_removing_thunk :
// CHECK: [[P:%.*]] = partial_apply [callee_guaranteed] [on_stack] %2(%0)
// CHECK: [[C:%.*]] = copy_value [[P]]
// CHECK: [[V:%.*]] = convert_function [[C]]
// CHECK: apply [[V]]
// CHECK: destroy_value [[V]]
// CHECK: destroy_value [[P]]
// CHECK: } // end sil function 'insert_copy_when_removing_thunk'
sil [ossa] @insert_copy_when_removing_thunk : $@convention(method) (Int) -> Int {
bb0(%0 : $Int):
%1 = alloc_stack $Int
%2 = function_ref @callee : $@convention(thin) <τ_0_0> (@inout τ_0_0, @in_guaranteed Int, Int) -> ()
%3 = partial_apply [callee_guaranteed] [on_stack] %2<Int>(%0) : $@convention(thin) <τ_0_0> (@inout τ_0_0, @in_guaranteed Int, Int) -> ()
%4 = function_ref @thunk : $@convention(thin) <τ_0_0> (@inout τ_0_0, Int, @guaranteed @noescape @callee_guaranteed (@inout τ_0_0, @in_guaranteed Int) -> ()) -> ()
%5 = partial_apply [callee_guaranteed] [on_stack] %4<Int>(%3) : $@convention(thin) <τ_0_0> (@inout τ_0_0, Int, @guaranteed @noescape @callee_guaranteed (@inout τ_0_0, @in_guaranteed Int) -> ()) -> ()
%6 = convert_function %5 to $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0, Int) -> () for <Int>
store %0 to [trivial] %1
%8 = apply %6(%1, %0) : $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0, Int) -> () for <Int>
destroy_value %6
destroy_value %3
%11 = load [trivial] %1
dealloc_stack %1
return %11
}