Skip to content

Commit db89c6a

Browse files
authored
Merge pull request #77933 from meg-gupta/fixexistential
Fix existential specializer for unowned ownership
2 parents 0dcd87b + d033fe8 commit db89c6a

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

lib/SILOptimizer/FunctionSignatureTransforms/ExistentialTransform.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,21 @@ void ExistentialSpecializerCloner::cloneArguments(
221221
NewFBuilder.emitLoadValueOperation(InsertLoc, NewArg, qual);
222222
}
223223

224+
if (NewFBuilder.hasOwnership() &&
225+
NewArg->getOwnershipKind() == OwnershipKind::Unowned) {
226+
NewArgValue = NewFBuilder.emitCopyValueOperation(InsertLoc, NewArg);
227+
}
224228
/// Simple case: Create an init_existential.
225229
/// %5 = init_existential_ref %0 : $T : $T, $P
226230
SILValue InitRef = NewFBuilder.createInitExistentialRef(
227231
InsertLoc, ArgDesc.Arg->getType().getObjectType(),
228232
NewArg->getType().getASTType(),
229233
NewArgValue, Conformances);
230234

235+
if (NewFBuilder.hasOwnership() &&
236+
NewArg->getOwnershipKind() == OwnershipKind::Unowned) {
237+
CleanupValues.push_back(InitRef);
238+
}
231239
// If we don't have an object and we are in ossa, the store will consume
232240
// the InitRef.
233241
if (!NewArg->getType().isObject()) {

test/SILOptimizer/existential_transform_extras_ossa.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,3 +313,29 @@ sil_witness_table hidden Klass4: P module devirtualize_protocol_composition {
313313
method #P.foo: <Self where Self : P> (Self) -> () -> Int32 : @Klass4foo
314314
}
315315

316+
// CHECK-LABEL: sil shared [ossa] @$s32testExistentialSpecializeUnownedTf4e_n :
317+
// CHECK: [[VAL0:%.*]] = copy_value %0
318+
// CHECK: [[VAL1:%.*]] = init_existential_ref [[VAL0]]
319+
// CHECK: destroy_value [[VAL1]]
320+
// CHECK-LABEL: } // end sil function '$s32testExistentialSpecializeUnownedTf4e_n'
321+
sil hidden [ossa] @testExistentialSpecializeUnowned : $@convention(thin) (any Klass3 & P) -> Int32 {
322+
bb0(%0 : @unowned $Klass3 & P):
323+
%1 = copy_value %0 : $Klass3 & P
324+
%2 = open_existential_ref %1 : $Klass3 & P to $@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self
325+
%3 = alloc_stack $@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self
326+
%4 = store_borrow %2 to %3 : $*@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self
327+
%5 = witness_method $@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self, #P.foo : <Self where Self : P> (Self) -> () -> Int32, %2 : $@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int32
328+
%6 = apply %5<@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self>(%4) : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> Int32
329+
end_borrow %4 : $*@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self
330+
dealloc_stack %3 : $*@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self
331+
destroy_value %2 : $@opened("77949BFA-77BC-11EB-BC0E-F2189810406F", Klass3 & P) Self
332+
return %6 : $Int32
333+
}
334+
335+
sil hidden [ossa] @entrypoint_unowned : $@convention(thin) (@guaranteed Klass4) -> Int32 {
336+
bb0(%0 : @guaranteed $Klass4):
337+
%1 = init_existential_ref %0 : $Klass4 : $Klass4, $any Klass3 & P
338+
%func = function_ref @testExistentialSpecializeUnowned : $@convention(thin) (any Klass3 & P) -> Int32
339+
%res = apply %func(%1) : $@convention(thin) (any Klass3 & P) -> Int32
340+
return %res : $Int32
341+
}

0 commit comments

Comments
 (0)