Skip to content

[5.3] SILCombine: fix an assertion crash in SILCombine when casting AnyClass to Any #33785

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
Sep 3, 2020
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
2 changes: 2 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerApplyVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,6 +584,8 @@ void SILCombiner::buildConcreteOpenedExistentialInfos(
// BuilderContext before rewriting any uses of the ConcreteType.
OpenedArchetypesTracker.addOpenedArchetypeDef(
cast<ArchetypeType>(CEI.ConcreteType), CEI.ConcreteTypeDef);
} else if (auto *I = CEI.ConcreteValue->getDefiningInstruction()) {
OpenedArchetypesTracker.registerUsedOpenedArchetypes(I);
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,8 @@ SILInstruction *SILCombiner::visitAllocStackInst(AllocStackInst *AS) {
// Be careful with open archetypes, because they cannot be moved before
// their definitions.
if (IEI && !OEI &&
!IEI->getLoweredConcreteType().isOpenedExistential()) {
!IEI->getLoweredConcreteType().hasOpenedExistential()) {
assert(!IEI->getLoweredConcreteType().isOpenedExistential());
auto *ConcAlloc = Builder.createAllocStack(
AS->getLoc(), IEI->getLoweredConcreteType(), AS->getVarInfo());
IEI->replaceAllUsesWith(ConcAlloc);
Expand Down
27 changes: 27 additions & 0 deletions test/SILOptimizer/sil_combine.sil
Original file line number Diff line number Diff line change
Expand Up @@ -3189,6 +3189,33 @@ bb0(%0 : $VV):
return %26 : $()
}

sil @any_to_object : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned AnyObject

// CHECK-LABEL: sil @dont_crash_when_propagating_existentials
// CHECK: [[EM:%[0-9]+]] = init_existential_metatype %0
// CHECK: [[S:%[0-9]+]] = alloc_stack $Any
// CHECK: [[M:%[0-9]+]] = open_existential_metatype [[EM]]
// CHECK: [[E:%[0-9]+]] = init_existential_addr [[S]]
// CHECK: store [[M]] to [[E]]
// CHECK: apply {{%[0-9]+}}<(@opened("5F99B72C-EC40-11EA-9534-8C8590A6A134") AnyObject).Type>([[E]])
// CHECK: } // end sil function 'dont_crash_when_propagating_existentials'
sil @dont_crash_when_propagating_existentials : $@convention(thin) () -> @owned AnyObject {
bb0:
%0 = metatype $@thick C.Type
%1 = init_existential_metatype %0 : $@thick C.Type, $@thick AnyObject.Type
%3 = alloc_stack $Any, let, name "object"
%4 = open_existential_metatype %1 : $@thick AnyObject.Type to $@thick (@opened("5F99B72C-EC40-11EA-9534-8C8590A6A134") AnyObject).Type
%5 = init_existential_addr %3 : $*Any, $(@opened("5F99B72C-EC40-11EA-9534-8C8590A6A134") AnyObject).Type
store %4 to %5 : $*@thick (@opened("5F99B72C-EC40-11EA-9534-8C8590A6A134") AnyObject).Type
%7 = open_existential_addr immutable_access %3 : $*Any to $*@opened("5F9F1B04-EC40-11EA-9534-8C8590A6A134") Any
%8 = function_ref @any_to_object : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned AnyObject
%9 = apply %8<@opened("5F9F1B04-EC40-11EA-9534-8C8590A6A134") Any>(%7) : $@convention(thin) <τ_0_0> (@in_guaranteed τ_0_0) -> @owned AnyObject
destroy_addr %3 : $*Any
dealloc_stack %3 : $*Any
return %9 : $AnyObject
}


// CHECK-LABEL: sil @remove_unused_alloc_ref
// CHECK-NEXT: bb0
// CHECK-NEXT: %0 = tuple ()
Expand Down