Skip to content

Commit 95cd8ed

Browse files
committed
SILCloner: Stop passing around origType for conformance substitution
1 parent 97be02d commit 95cd8ed

File tree

2 files changed

+42
-66
lines changed

2 files changed

+42
-66
lines changed

include/swift/SIL/SILCloner.h

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -409,32 +409,16 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
409409
VarInfo->Scope = getOpScope(VarInfo->Scope);
410410
}
411411

412-
ProtocolConformanceRef getOpConformance(Type ty,
413-
ProtocolConformanceRef conformance) {
414-
auto substConf = asImpl().remapConformance(ty, conformance);
415-
416-
#ifndef NDEBUG
417-
if (substConf.isInvalid()) {
418-
llvm::errs() << "Invalid conformance in SIL cloner:\n";
419-
Functor.dump(llvm::errs());
420-
llvm::errs() << "\nconformance:\n";
421-
conformance.dump(llvm::errs());
422-
llvm::errs() << "\noriginal type:\n";
423-
ty.dump(llvm::errs());
424-
abort();
425-
}
426-
#endif
427-
428-
return substConf;
412+
ProtocolConformanceRef getOpConformance(ProtocolConformanceRef conformance) {
413+
return asImpl().remapConformance(conformance);
429414
}
430415

431416
ArrayRef<ProtocolConformanceRef>
432-
getOpConformances(Type ty,
433-
ArrayRef<ProtocolConformanceRef> conformances) {
417+
getOpConformances(ArrayRef<ProtocolConformanceRef> conformances) {
434418
SmallVector<ProtocolConformanceRef, 4> newConformances;
435419
for (auto conformance : conformances)
436-
newConformances.push_back(getOpConformance(ty, conformance));
437-
return ty->getASTContext().AllocateCopy(newConformances);
420+
newConformances.push_back(getOpConformance(conformance));
421+
return getBuilder().getASTContext().AllocateCopy(newConformances);
438422
}
439423

440424
bool isValueCloned(SILValue OrigValue) const {
@@ -558,28 +542,41 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
558542
return ty;
559543
}
560544

561-
ProtocolConformanceRef remapConformance(Type Ty, ProtocolConformanceRef C) {
562-
if (Functor.SubsMap || Ty->hasLocalArchetype()) {
545+
ProtocolConformanceRef remapConformance(ProtocolConformanceRef conformance) {
546+
auto substConf = conformance;
547+
548+
if (Functor.SubsMap || substConf.getType()->hasLocalArchetype()) {
563549
SubstOptions options = SubstFlags::SubstitutePrimaryArchetypes;
564550
if (Functor.hasLocalArchetypes())
565551
options |= SubstFlags::SubstituteLocalArchetypes;
566552

567-
C = C.subst(Functor, Functor, options);
568-
if (asImpl().shouldSubstOpaqueArchetypes())
569-
Ty = Ty.subst(Functor, Functor, options);
553+
substConf = substConf.subst(Functor, Functor, options);
554+
}
555+
556+
if (substConf.isInvalid()) {
557+
llvm::errs() << "Invalid conformance in SIL cloner:\n";
558+
Functor.dump(llvm::errs());
559+
llvm::errs() << "\nconformance:\n";
560+
conformance.dump(llvm::errs());
561+
llvm::errs() << "\noriginal conformance:\n";
562+
substConf.dump(llvm::errs());
563+
if (Functor.SubsMap) {
564+
llvm::errs() << "\nsubstitution map:\n";
565+
Functor.SubsMap->dump(llvm::errs());
566+
}
567+
abort();
570568
}
571569

572570
if (asImpl().shouldSubstOpaqueArchetypes()) {
573571
auto context = getBuilder().getTypeExpansionContext();
574572

575-
if (!Ty->hasOpaqueArchetype() ||
576-
!context.shouldLookThroughOpaqueTypeArchetypes())
577-
return C;
578-
579-
return substOpaqueTypesWithUnderlyingTypes(C, context);
573+
if (substConf.getType()->hasOpaqueArchetype() &&
574+
context.shouldLookThroughOpaqueTypeArchetypes()) {
575+
return substOpaqueTypesWithUnderlyingTypes(substConf, context);
576+
}
580577
}
581578

582-
return C;
579+
return substConf;
583580
}
584581

585582
SubstitutionMap remapSubstitutionMap(SubstitutionMap Subs) {
@@ -1135,8 +1132,7 @@ SILCloner<ImplClass>::visitAllocExistentialBoxInst(
11351132
auto origExistentialType = Inst->getExistentialType();
11361133
auto origFormalType = Inst->getFormalConcreteType();
11371134

1138-
auto conformances = getOpConformances(origFormalType,
1139-
Inst->getConformances());
1135+
auto conformances = getOpConformances(Inst->getConformances());
11401136

11411137
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
11421138
recordClonedInstruction(
@@ -2659,29 +2655,28 @@ SILCloner<ImplClass>::visitObjCSuperMethodInst(ObjCSuperMethodInst *Inst) {
26592655
template<typename ImplClass>
26602656
void
26612657
SILCloner<ImplClass>::visitWitnessMethodInst(WitnessMethodInst *Inst) {
2662-
auto lookupType = Inst->getLookupType();
2663-
auto conformance = getOpConformance(lookupType, Inst->getConformance());
2664-
auto newLookupType = getOpASTType(lookupType);
2658+
auto conformance = getOpConformance(Inst->getConformance());
2659+
auto lookupType = getOpASTType(Inst->getLookupType());
26652660

26662661
if (conformance.isConcrete()) {
2667-
CanType Ty = conformance.getConcrete()->getType()->getCanonicalType();
2662+
auto conformingType = conformance.getConcrete()->getType()->getCanonicalType();
26682663

2669-
if (Ty != newLookupType) {
2664+
if (conformingType != lookupType) {
26702665
assert(
2671-
(Ty->isExactSuperclassOf(newLookupType) ||
2666+
(conformingType->isExactSuperclassOf(lookupType) ||
26722667
getBuilder().getModule().Types.getLoweredRValueType(
2673-
getBuilder().getTypeExpansionContext(), Ty) == newLookupType) &&
2668+
getBuilder().getTypeExpansionContext(), conformingType) == lookupType) &&
26742669
"Should only create upcasts for sub class.");
26752670

26762671
// We use the super class as the new look up type.
2677-
newLookupType = Ty;
2672+
lookupType = conformingType;
26782673
}
26792674
}
26802675

26812676
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
26822677
recordClonedInstruction(Inst,
26832678
getBuilder().createWitnessMethod(
2684-
getOpLocation(Inst->getLoc()), newLookupType,
2679+
getOpLocation(Inst->getLoc()), lookupType,
26852680
conformance, Inst->getMember(), getOpType(Inst->getType())));
26862681
}
26872682

@@ -2790,8 +2785,7 @@ void
27902785
SILCloner<ImplClass>::visitInitExistentialAddrInst(InitExistentialAddrInst *Inst) {
27912786
CanType origFormalType = Inst->getFormalConcreteType();
27922787

2793-
auto conformances = getOpConformances(origFormalType,
2794-
Inst->getConformances());
2788+
auto conformances = getOpConformances(Inst->getConformances());
27952789

27962790
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
27972791
recordClonedInstruction(
@@ -2806,8 +2800,7 @@ void SILCloner<ImplClass>::visitInitExistentialValueInst(
28062800
InitExistentialValueInst *Inst) {
28072801
CanType origFormalType = Inst->getFormalConcreteType();
28082802

2809-
auto conformances = getOpConformances(origFormalType,
2810-
Inst->getConformances());
2803+
auto conformances = getOpConformances(Inst->getConformances());
28112804

28122805
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
28132806
recordClonedInstruction(
@@ -2821,9 +2814,7 @@ template<typename ImplClass>
28212814
void
28222815
SILCloner<ImplClass>::
28232816
visitInitExistentialMetatypeInst(InitExistentialMetatypeInst *Inst) {
2824-
auto origFormalType = Inst->getFormalErasedObjectType();
2825-
auto conformances = getOpConformances(origFormalType,
2826-
Inst->getConformances());
2817+
auto conformances = getOpConformances(Inst->getConformances());
28272818

28282819
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
28292820
recordClonedInstruction(Inst, getBuilder().createInitExistentialMetatype(
@@ -2837,8 +2828,7 @@ void
28372828
SILCloner<ImplClass>::
28382829
visitInitExistentialRefInst(InitExistentialRefInst *Inst) {
28392830
CanType origFormalType = Inst->getFormalConcreteType();
2840-
auto conformances = getOpConformances(origFormalType,
2841-
Inst->getConformances());
2831+
auto conformances = getOpConformances(Inst->getConformances());
28422832

28432833
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
28442834
recordClonedInstruction(

include/swift/SIL/SILInstruction.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8077,20 +8077,6 @@ class InitExistentialMetatypeInst final
80778077
SILFunction *parent);
80788078

80798079
public:
8080-
/// Return the object type which was erased. That is, if this
8081-
/// instruction erases Decoder<T>.Type.Type to Printable.Type.Type,
8082-
/// this method returns Decoder<T>.
8083-
CanType getFormalErasedObjectType() const {
8084-
auto exType = getType().getASTType();
8085-
auto concreteType = getOperand()->getType().getASTType();
8086-
while (auto exMetatype = dyn_cast<ExistentialMetatypeType>(exType)) {
8087-
exType = exMetatype->getExistentialInstanceType()->getCanonicalType();
8088-
concreteType = cast<MetatypeType>(concreteType).getInstanceType();
8089-
}
8090-
assert(exType.isExistentialType());
8091-
return concreteType;
8092-
}
8093-
80948080
ArrayRef<ProtocolConformanceRef> getConformances() const;
80958081
};
80968082

0 commit comments

Comments
 (0)