Skip to content

Commit 95f1992

Browse files
authored
Merge pull request #78351 from eeckstein/fix-specializer
GenericSpecializer: fix an ownership verification failure when removing a partial_apply of a thunk
2 parents 1bdb51e + e6f161c commit 95f1992

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

lib/SILOptimizer/Utils/Generics.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -3507,6 +3507,13 @@ void swift::trySpecializeApplyOfGeneric(
35073507
}
35083508
if (auto *PAI = dyn_cast<PartialApplyInst>(User)) {
35093509
SILValue result = NewPAI;
3510+
if (NewPAI->getFunction()->hasOwnership()) {
3511+
auto convention = ApplySite(PAI).getCaptureConvention(*Use);
3512+
if (convention == SILArgumentConvention::Direct_Guaranteed) {
3513+
SILBuilderWithScope builder(Apply.getInstruction());
3514+
result = builder.createCopyValue(Apply.getLoc(), result);
3515+
}
3516+
}
35103517
if (SpecializedF.hasTypeReplacements()) {
35113518
SILBuilderWithScope builder(Apply.getInstruction());
35123519
auto fnType = PAI->getType();

test/SILOptimizer/specialize_ossa.sil

+31
Original file line numberDiff line numberDiff line change
@@ -1634,3 +1634,34 @@ bb0:
16341634
return %5 : $()
16351635
}
16361636

1637+
sil [ossa] @callee : $@convention(thin) <Result> (@inout Result, @in_guaranteed Int, Int) -> () {
1638+
bb0(%0 : $*Result, %1 : $*Int, %2 : $Int):
1639+
unreachable
1640+
}
1641+
1642+
sil [reabstraction_thunk] [ossa] @thunk : $@convention(thin) <τ_0_0> (@inout τ_0_0, Int, @guaranteed @noescape @callee_guaranteed (@inout τ_0_0, @in_guaranteed Int) -> ()) -> ()
1643+
1644+
// CHECK-LABEL: sil [ossa] @insert_copy_when_removing_thunk :
1645+
// CHECK: [[P:%.*]] = partial_apply [callee_guaranteed] [on_stack] %2(%0)
1646+
// CHECK: [[C:%.*]] = copy_value [[P]]
1647+
// CHECK: [[V:%.*]] = convert_function [[C]]
1648+
// CHECK: apply [[V]]
1649+
// CHECK: destroy_value [[V]]
1650+
// CHECK: destroy_value [[P]]
1651+
// CHECK: } // end sil function 'insert_copy_when_removing_thunk'
1652+
sil [ossa] @insert_copy_when_removing_thunk : $@convention(method) (Int) -> Int {
1653+
bb0(%0 : $Int):
1654+
%1 = alloc_stack $Int
1655+
%2 = function_ref @callee : $@convention(thin) <τ_0_0> (@inout τ_0_0, @in_guaranteed Int, Int) -> ()
1656+
%3 = partial_apply [callee_guaranteed] [on_stack] %2<Int>(%0) : $@convention(thin) <τ_0_0> (@inout τ_0_0, @in_guaranteed Int, Int) -> ()
1657+
%4 = function_ref @thunk : $@convention(thin) <τ_0_0> (@inout τ_0_0, Int, @guaranteed @noescape @callee_guaranteed (@inout τ_0_0, @in_guaranteed Int) -> ()) -> ()
1658+
%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) -> ()) -> ()
1659+
%6 = convert_function %5 to $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0, Int) -> () for <Int>
1660+
store %0 to [trivial] %1
1661+
%8 = apply %6(%1, %0) : $@noescape @callee_guaranteed @substituted <τ_0_0> (@inout τ_0_0, Int) -> () for <Int>
1662+
destroy_value %6
1663+
destroy_value %3
1664+
%11 = load [trivial] %1
1665+
dealloc_stack %1
1666+
return %11
1667+
}

0 commit comments

Comments
 (0)