Skip to content

Commit b9612b2

Browse files
committed
SILCombine: fix an assertion crash in SILCombine when casting AnyClass to Any
This caused a problem when propagating the concrete type of an existential: if the concrete type is itself an opened existential, it was not added to the OpenedArchetypeTracker. https://bugs.swift.org/browse/SR-13444 rdar://problem/68077098
1 parent b12a705 commit b9612b2

File tree

3 files changed

+31
-1
lines changed

3 files changed

+31
-1
lines changed

lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -806,6 +806,8 @@ void SILCombiner::buildConcreteOpenedExistentialInfos(
806806
// BuilderContext before rewriting any uses of the ConcreteType.
807807
OpenedArchetypesTracker.addOpenedArchetypeDef(
808808
cast<ArchetypeType>(CEI.ConcreteType), CEI.ConcreteTypeDef);
809+
} else if (auto *I = CEI.ConcreteValue->getDefiningInstruction()) {
810+
OpenedArchetypesTracker.registerUsedOpenedArchetypes(I);
809811
}
810812
}
811813
}

lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,8 @@ SILInstruction *SILCombiner::visitAllocStackInst(AllocStackInst *AS) {
554554
// Be careful with open archetypes, because they cannot be moved before
555555
// their definitions.
556556
if (IEI && !OEI &&
557-
!IEI->getLoweredConcreteType().isOpenedExistential()) {
557+
!IEI->getLoweredConcreteType().hasOpenedExistential()) {
558+
assert(!IEI->getLoweredConcreteType().isOpenedExistential());
558559
auto *ConcAlloc = Builder.createAllocStack(
559560
AS->getLoc(), IEI->getLoweredConcreteType(), AS->getVarInfo());
560561
IEI->replaceAllUsesWith(ConcAlloc);

test/SILOptimizer/sil_combine.sil

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3508,6 +3508,33 @@ bb0(%0 : $VV):
35083508
return %26 : $()
35093509
}
35103510

3511+
sil @any_to_object : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned AnyObject
3512+
3513+
// CHECK-LABEL: sil @dont_crash_when_propagating_existentials
3514+
// CHECK: [[EM:%[0-9]+]] = init_existential_metatype %0
3515+
// CHECK: [[S:%[0-9]+]] = alloc_stack $Any
3516+
// CHECK: [[M:%[0-9]+]] = open_existential_metatype [[EM]]
3517+
// CHECK: [[E:%[0-9]+]] = init_existential_addr [[S]]
3518+
// CHECK: store [[M]] to [[E]]
3519+
// CHECK: apply {{%[0-9]+}}<(@opened("5F99B72C-EC40-11EA-9534-8C8590A6A134") AnyObject).Type>([[E]])
3520+
// CHECK: } // end sil function 'dont_crash_when_propagating_existentials'
3521+
sil @dont_crash_when_propagating_existentials : $@convention(thin) () -> @owned AnyObject {
3522+
bb0:
3523+
%0 = metatype $@thick C.Type
3524+
%1 = init_existential_metatype %0 : $@thick C.Type, $@thick AnyObject.Type
3525+
%3 = alloc_stack $Any, let, name "object"
3526+
%4 = open_existential_metatype %1 : $@thick AnyObject.Type to $@thick (@opened("5F99B72C-EC40-11EA-9534-8C8590A6A134") AnyObject).Type
3527+
%5 = init_existential_addr %3 : $*Any, $(@opened("5F99B72C-EC40-11EA-9534-8C8590A6A134") AnyObject).Type
3528+
store %4 to %5 : $*@thick (@opened("5F99B72C-EC40-11EA-9534-8C8590A6A134") AnyObject).Type
3529+
%7 = open_existential_addr immutable_access %3 : $*Any to $*@opened("5F9F1B04-EC40-11EA-9534-8C8590A6A134") Any
3530+
%8 = function_ref @any_to_object : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned AnyObject
3531+
%9 = apply %8<@opened("5F9F1B04-EC40-11EA-9534-8C8590A6A134") Any>(%7) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned AnyObject
3532+
destroy_addr %3 : $*Any
3533+
dealloc_stack %3 : $*Any
3534+
return %9 : $AnyObject
3535+
}
3536+
3537+
35113538
// CHECK-LABEL: sil @remove_unused_alloc_ref
35123539
// CHECK-NEXT: bb0
35133540
// CHECK-NEXT: %0 = tuple ()

0 commit comments

Comments
 (0)