Skip to content

Commit 6cad1c2

Browse files
authored
Merge pull request #33742 from eeckstein/fix-existential-propagation
SILCombine: fix an assertion crash in SILCombine when casting AnyClass to Any
2 parents 413eba8 + b9612b2 commit 6cad1c2

File tree

5 files changed

+74
-1
lines changed

5 files changed

+74
-1
lines changed

include/swift/SILOptimizer/Utils/Existential.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ struct OpenedArchetypeInfo {
4949
assert(!OpenedArchetype || (OpenedArchetypeValue && ExistentialValue));
5050
return OpenedArchetype;
5151
}
52+
53+
void dump() const;
5254
};
5355

5456
/// Record conformance and concrete type info derived from an init_existential
@@ -102,6 +104,8 @@ struct ConcreteExistentialInfo {
102104
CanType selfTy = P->getSelfInterfaceType()->getCanonicalType();
103105
return ExistentialSubs.lookupConformance(selfTy, P);
104106
}
107+
108+
void dump() const;
105109

106110
private:
107111
void initializeSubstitutionMap(
@@ -131,6 +135,8 @@ struct ConcreteOpenedExistentialInfo {
131135
assert(CEI->isValid());
132136
return true;
133137
}
138+
139+
void dump() const;
134140
};
135141

136142
} // end namespace swift

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);

lib/SILOptimizer/Utils/Existential.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,40 @@ ConcreteOpenedExistentialInfo::ConcreteOpenedExistentialInfo(
433433
}
434434
CEI->isConcreteValueCopied |= OAI.isOpenedValueCopied;
435435
}
436+
437+
void LLVM_ATTRIBUTE_USED OpenedArchetypeInfo::dump() const {
438+
if (!isValid()) {
439+
llvm::dbgs() << "invalid OpenedArchetypeInfo\n";
440+
return;
441+
}
442+
llvm::dbgs() << "OpendArchetype: ";
443+
OpenedArchetype->dump(llvm::dbgs());
444+
llvm::dbgs() << "OpendArchetypeValue: ";
445+
OpenedArchetypeValue->dump();
446+
llvm::dbgs() << (isOpenedValueCopied ? "copied " : "") << "ExistentialValue: ";
447+
ExistentialValue->dump();
448+
}
449+
450+
void LLVM_ATTRIBUTE_USED ConcreteExistentialInfo::dump() const {
451+
llvm::dbgs() << "ExistentialType: ";
452+
ExistentialType->dump(llvm::dbgs());
453+
llvm::dbgs() << "ConcreteType: ";
454+
ConcreteType->dump(llvm::dbgs());
455+
llvm::dbgs() << (isConcreteValueCopied ? "copied " : "") << "ConcreteValue: ";
456+
ConcreteValue->dump();
457+
if (ConcreteTypeDef) {
458+
llvm::dbgs() << "ConcreteTypeDef: ";
459+
ConcreteTypeDef->dump();
460+
}
461+
ExistentialSubs.dump(llvm::dbgs());
462+
llvm::dbgs() << '\n';
463+
}
464+
465+
void LLVM_ATTRIBUTE_USED ConcreteOpenedExistentialInfo::dump() const {
466+
OAI.dump();
467+
if (CEI) {
468+
CEI->dump();
469+
} else {
470+
llvm::dbgs() << "no ConcreteExistentialInfo\n";
471+
}
472+
}

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)