Skip to content

Commit 6db4c37

Browse files
authored
Merge pull request #80594 from slavapestov/sil-abstract-conformance-cleanup
SIL: Tiny conformance substitution cleanups
2 parents 504e83c + be5d03f commit 6db4c37

File tree

18 files changed

+85
-185
lines changed

18 files changed

+85
-185
lines changed

SwiftCompilerSources/Sources/Optimizer/Utilities/GenericSpecialization.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func specializeWitnessTable(forConformance conformance: Conformance,
144144
case .associatedType(let requirement, let witness):
145145
let substType = witness.subst(with: conformance.specializedSubstitutions)
146146
return .associatedType(requirement: requirement, witness: substType)
147-
case .associatedConformance(let requirement, let substType, let assocConf):
147+
case .associatedConformance(let requirement, let assocConf):
148148
// FIXME: let concreteAssociateConf = assocConf.subst(with: conformance.specializedSubstitutions)
149149
let concreteAssociateConf = conformance.getAssociatedConformance(ofAssociatedType: requirement.rawType, to: assocConf.proto)
150150
if concreteAssociateConf.isSpecialized {
@@ -153,7 +153,6 @@ func specializeWitnessTable(forConformance conformance: Conformance,
153153
context, notifyNewWitnessTable)
154154
}
155155
return .associatedConformance(requirement: requirement,
156-
substType: substType.subst(with: conformance.specializedSubstitutions),
157156
witness: concreteAssociateConf)
158157
}
159158
}

SwiftCompilerSources/Sources/SIL/WitnessTable.swift

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public struct WitnessTable : CustomStringConvertible, NoReflectionChildren {
3131
case associatedType(requirement: AssociatedTypeDecl, witness: CanonicalType)
3232

3333
/// A witness table entry describing the witness for an associated type's protocol requirement.
34-
case associatedConformance(requirement: CanonicalType, substType: CanonicalType, witness: Conformance)
34+
case associatedConformance(requirement: CanonicalType, witness: Conformance)
3535

3636
/// A witness table entry referencing the protocol conformance for a refined base protocol.
3737
case baseProtocol(requirement: ProtocolDecl, witness: Conformance)
@@ -48,7 +48,6 @@ public struct WitnessTable : CustomStringConvertible, NoReflectionChildren {
4848
witness: CanonicalType(bridged: bridged.getAssociatedTypeWitness()))
4949
case .associatedConformance:
5050
self = .associatedConformance(requirement: CanonicalType(bridged: bridged.getAssociatedConformanceRequirement()),
51-
substType: CanonicalType(bridged: bridged.getAssociatedConformanceSubstType()),
5251
witness: Conformance(bridged: bridged.getAssociatedConformanceWitness()))
5352
case .baseProtocol:
5453
self = .baseProtocol(requirement: bridged.getBaseProtocolRequirement().getAs(ProtocolDecl.self),
@@ -71,9 +70,8 @@ public struct WitnessTable : CustomStringConvertible, NoReflectionChildren {
7170
OptionalBridgedFunction(obj: witness?.bridged.obj))
7271
case .associatedType(let requirement, let witness):
7372
return BridgedWitnessTableEntry.createAssociatedType(requirement.bridged, witness.bridged)
74-
case .associatedConformance(let requirement, let substType, let witness):
73+
case .associatedConformance(let requirement, let witness):
7574
return BridgedWitnessTableEntry.createAssociatedConformance(requirement.bridged,
76-
substType.bridged,
7775
witness.bridged)
7876
case .baseProtocol(let requirement, let witness):
7977
return BridgedWitnessTableEntry.createBaseProtocol(requirement.bridged, witness.bridged)

include/swift/SIL/SILBridging.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,7 +1062,6 @@ struct BridgedWitnessTableEntry {
10621062
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj getAssociatedTypeRequirement() const;
10631063
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getAssociatedTypeWitness() const;
10641064
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getAssociatedConformanceRequirement() const;
1065-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedCanType getAssociatedConformanceSubstType() const;
10661065
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance getAssociatedConformanceWitness() const;
10671066
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedDeclObj getBaseProtocolRequirement() const;
10681067
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance getBaseProtocolWitness() const;
@@ -1077,7 +1076,6 @@ struct BridgedWitnessTableEntry {
10771076
BridgedCanType witness);
10781077
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
10791078
static BridgedWitnessTableEntry createAssociatedConformance(BridgedCanType requirement,
1080-
BridgedCanType substType,
10811079
BridgedConformance witness);
10821080
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
10831081
static BridgedWitnessTableEntry createBaseProtocol(BridgedDeclObj requirement,

include/swift/SIL/SILBridgingImpl.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1922,10 +1922,6 @@ BridgedCanType BridgedWitnessTableEntry::getAssociatedConformanceRequirement() c
19221922
return unbridged().getAssociatedConformanceWitness().Requirement;
19231923
}
19241924

1925-
BridgedCanType BridgedWitnessTableEntry::getAssociatedConformanceSubstType() const {
1926-
return {unbridged().getAssociatedConformanceWitness().SubstType};
1927-
}
1928-
19291925
BridgedConformance BridgedWitnessTableEntry::getAssociatedConformanceWitness() const {
19301926
return {unbridged().getAssociatedConformanceWitness().Witness};
19311927
}
@@ -1957,11 +1953,9 @@ BridgedWitnessTableEntry BridgedWitnessTableEntry::createAssociatedType(BridgedD
19571953
}
19581954

19591955
BridgedWitnessTableEntry BridgedWitnessTableEntry::createAssociatedConformance(BridgedCanType requirement,
1960-
BridgedCanType substType,
19611956
BridgedConformance witness) {
19621957
return bridge(swift::SILWitnessTable::Entry(
19631958
swift::SILWitnessTable::AssociatedConformanceWitness{requirement.unbridged(),
1964-
substType.unbridged(),
19651959
witness.unbridged()}));
19661960
}
19671961

include/swift/SIL/SILCloner.h

Lines changed: 37 additions & 57 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,35 @@ 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 substituted conformance in SIL cloner:\n";
558+
Functor.dump(llvm::errs());
559+
llvm::errs() << "\noriginal conformance:\n";
560+
conformance.dump(llvm::errs());
561+
abort();
570562
}
571563

572564
if (asImpl().shouldSubstOpaqueArchetypes()) {
573565
auto context = getBuilder().getTypeExpansionContext();
574566

575-
if (!Ty->hasOpaqueArchetype() ||
576-
!context.shouldLookThroughOpaqueTypeArchetypes())
577-
return C;
578-
579-
return substOpaqueTypesWithUnderlyingTypes(C, context);
567+
if (substConf.getType()->hasOpaqueArchetype() &&
568+
context.shouldLookThroughOpaqueTypeArchetypes()) {
569+
return substOpaqueTypesWithUnderlyingTypes(substConf, context);
570+
}
580571
}
581572

582-
return C;
573+
return substConf;
583574
}
584575

585576
SubstitutionMap remapSubstitutionMap(SubstitutionMap Subs) {
@@ -599,11 +590,7 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
599590
!context.shouldLookThroughOpaqueTypeArchetypes())
600591
return Subs;
601592

602-
ReplaceOpaqueTypesWithUnderlyingTypes replacer(
603-
context.getContext(), context.getResilienceExpansion(),
604-
context.isWholeModuleContext());
605-
return Subs.subst(replacer, replacer,
606-
SubstFlags::SubstituteOpaqueArchetypes);
593+
return Subs.mapIntoTypeExpansionContext(context);
607594
}
608595

609596
return Subs;
@@ -1135,8 +1122,7 @@ SILCloner<ImplClass>::visitAllocExistentialBoxInst(
11351122
auto origExistentialType = Inst->getExistentialType();
11361123
auto origFormalType = Inst->getFormalConcreteType();
11371124

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

11411127
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
11421128
recordClonedInstruction(
@@ -2659,29 +2645,28 @@ SILCloner<ImplClass>::visitObjCSuperMethodInst(ObjCSuperMethodInst *Inst) {
26592645
template<typename ImplClass>
26602646
void
26612647
SILCloner<ImplClass>::visitWitnessMethodInst(WitnessMethodInst *Inst) {
2662-
auto lookupType = Inst->getLookupType();
2663-
auto conformance = getOpConformance(lookupType, Inst->getConformance());
2664-
auto newLookupType = getOpASTType(lookupType);
2648+
auto conformance = getOpConformance(Inst->getConformance());
2649+
auto lookupType = getOpASTType(Inst->getLookupType());
26652650

26662651
if (conformance.isConcrete()) {
2667-
CanType Ty = conformance.getConcrete()->getType()->getCanonicalType();
2652+
auto conformingType = conformance.getConcrete()->getType()->getCanonicalType();
26682653

2669-
if (Ty != newLookupType) {
2654+
if (conformingType != lookupType) {
26702655
assert(
2671-
(Ty->isExactSuperclassOf(newLookupType) ||
2656+
(conformingType->isExactSuperclassOf(lookupType) ||
26722657
getBuilder().getModule().Types.getLoweredRValueType(
2673-
getBuilder().getTypeExpansionContext(), Ty) == newLookupType) &&
2658+
getBuilder().getTypeExpansionContext(), conformingType) == lookupType) &&
26742659
"Should only create upcasts for sub class.");
26752660

26762661
// We use the super class as the new look up type.
2677-
newLookupType = Ty;
2662+
lookupType = conformingType;
26782663
}
26792664
}
26802665

26812666
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
26822667
recordClonedInstruction(Inst,
26832668
getBuilder().createWitnessMethod(
2684-
getOpLocation(Inst->getLoc()), newLookupType,
2669+
getOpLocation(Inst->getLoc()), lookupType,
26852670
conformance, Inst->getMember(), getOpType(Inst->getType())));
26862671
}
26872672

@@ -2790,8 +2775,7 @@ void
27902775
SILCloner<ImplClass>::visitInitExistentialAddrInst(InitExistentialAddrInst *Inst) {
27912776
CanType origFormalType = Inst->getFormalConcreteType();
27922777

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

27962780
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
27972781
recordClonedInstruction(
@@ -2806,8 +2790,7 @@ void SILCloner<ImplClass>::visitInitExistentialValueInst(
28062790
InitExistentialValueInst *Inst) {
28072791
CanType origFormalType = Inst->getFormalConcreteType();
28082792

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

28122795
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
28132796
recordClonedInstruction(
@@ -2821,9 +2804,7 @@ template<typename ImplClass>
28212804
void
28222805
SILCloner<ImplClass>::
28232806
visitInitExistentialMetatypeInst(InitExistentialMetatypeInst *Inst) {
2824-
auto origFormalType = Inst->getFormalErasedObjectType();
2825-
auto conformances = getOpConformances(origFormalType,
2826-
Inst->getConformances());
2807+
auto conformances = getOpConformances(Inst->getConformances());
28272808

28282809
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
28292810
recordClonedInstruction(Inst, getBuilder().createInitExistentialMetatype(
@@ -2837,8 +2818,7 @@ void
28372818
SILCloner<ImplClass>::
28382819
visitInitExistentialRefInst(InitExistentialRefInst *Inst) {
28392820
CanType origFormalType = Inst->getFormalConcreteType();
2840-
auto conformances = getOpConformances(origFormalType,
2841-
Inst->getConformances());
2821+
auto conformances = getOpConformances(Inst->getConformances());
28422822

28432823
getBuilder().setCurrentDebugScope(getOpScope(Inst->getDebugScope()));
28442824
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

include/swift/SIL/SILWitnessTable.h

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,6 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
6666
struct AssociatedConformanceWitness {
6767
/// The subject type of the associated requirement.
6868
CanType Requirement;
69-
/// FIXME: Temporary.
70-
CanType SubstType;
7169
/// The ProtocolConformanceRef satisfying the requirement.
7270
ProtocolConformanceRef Witness;
7371
};
@@ -154,15 +152,6 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
154152
const PrintOptions &options) const;
155153
};
156154

157-
/// An entry for a conformance requirement that makes the requirement
158-
/// conditional. These aren't public, but any witness thunks need to feed them
159-
/// into the true witness functions.
160-
struct ConditionalConformance {
161-
/// FIXME: Temporary.
162-
CanType Requirement;
163-
ProtocolConformanceRef Conformance;
164-
};
165-
166155
private:
167156
/// The module which contains the SILWitnessTable.
168157
SILModule &Mod;
@@ -186,7 +175,7 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
186175
///
187176
/// (If other private entities are introduced this could/should be switched
188177
/// into a private version of Entries.)
189-
MutableArrayRef<ConditionalConformance> ConditionalConformances;
178+
MutableArrayRef<ProtocolConformanceRef> ConditionalConformances;
190179

191180
/// Whether or not this witness table is a declaration. This is separate from
192181
/// whether or not entries is empty since you can have an empty witness table
@@ -201,7 +190,7 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
201190
SILWitnessTable(SILModule &M, SILLinkage Linkage, SerializedKind_t Serialized,
202191
StringRef name, ProtocolConformance *conformance,
203192
ArrayRef<Entry> entries,
204-
ArrayRef<ConditionalConformance> conditionalConformances);
193+
ArrayRef<ProtocolConformanceRef> conditionalConformances);
205194

206195
/// Private constructor for making SILWitnessTable declarations.
207196
SILWitnessTable(SILModule &M, SILLinkage Linkage, StringRef Name,
@@ -214,7 +203,7 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
214203
static SILWitnessTable *
215204
create(SILModule &M, SILLinkage Linkage, SerializedKind_t SerializedKind,
216205
ProtocolConformance *conformance, ArrayRef<Entry> entries,
217-
ArrayRef<ConditionalConformance> conditionalConformances);
206+
ArrayRef<ProtocolConformanceRef> conditionalConformances);
218207

219208
/// Create a new SILWitnessTable declaration.
220209
static SILWitnessTable *create(SILModule &M, SILLinkage Linkage,
@@ -271,7 +260,7 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
271260
ArrayRef<Entry> getEntries() const { return Entries; }
272261

273262
/// Return all of the conditional conformances.
274-
ArrayRef<ConditionalConformance> getConditionalConformances() const {
263+
ArrayRef<ProtocolConformanceRef> getConditionalConformances() const {
275264
return ConditionalConformances;
276265
}
277266

@@ -300,7 +289,7 @@ class SILWitnessTable : public llvm::ilist_node<SILWitnessTable>,
300289
/// Change a SILWitnessTable declaration into a SILWitnessTable definition.
301290
void
302291
convertToDefinition(ArrayRef<Entry> newEntries,
303-
ArrayRef<ConditionalConformance> conditionalConformances,
292+
ArrayRef<ProtocolConformanceRef> conditionalConformances,
304293
SerializedKind_t serializedKind);
305294

306295
// Gets conformance serialized kind.

lib/IRGen/GenProto.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1522,8 +1522,7 @@ class AccessorConformanceInfo : public ConformanceInfo {
15221522
SpecializedBaseConformances;
15231523

15241524
ArrayRef<SILWitnessTable::Entry> SILEntries;
1525-
ArrayRef<SILWitnessTable::ConditionalConformance>
1526-
SILConditionalConformances;
1525+
ArrayRef<ProtocolConformanceRef> SILConditionalConformances;
15271526

15281527
const ProtocolInfo &PI;
15291528

@@ -2107,10 +2106,10 @@ llvm::Function *FragileWitnessTableBuilder::buildInstantiationFunction() {
21072106
const auto &condConformance = SILConditionalConformances[idx];
21082107
CanType reqTypeInContext =
21092108
Conformance.getDeclContext()
2110-
->mapTypeIntoContext(condConformance.Requirement)
2109+
->mapTypeIntoContext(condConformance.getType())
21112110
->getCanonicalType();
21122111
if (auto archetype = dyn_cast<ArchetypeType>(reqTypeInContext)) {
2113-
auto condProto = condConformance.Conformance.getProtocol();
2112+
auto condProto = condConformance.getProtocol();
21142113
IGF.setUnscopedLocalTypeData(
21152114
archetype,
21162115
LocalTypeDataKind::forAbstractProtocolWitnessTable(condProto),

0 commit comments

Comments
 (0)