Skip to content

Commit f694bf5

Browse files
committed
Plumb GenericSignature of SIL Function Through IRGen
1 parent ab44a07 commit f694bf5

12 files changed

+133
-58
lines changed

include/swift/SIL/SILCloner.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -252,12 +252,13 @@ class SILCloner : protected SILInstructionVisitor<ImplClass> {
252252
void remapRootOpenedType(CanOpenedArchetypeType archetypeTy) {
253253
assert(archetypeTy->isRoot());
254254

255-
auto sig = archetypeTy->getGenericEnvironment()->getGenericSignature();
255+
auto sig = Builder.getFunction().getGenericSignature();
256256
auto existentialTy = archetypeTy->getExistentialType()->getCanonicalType();
257-
auto env = GenericEnvironment::forOpenedArchetypeSignature(
257+
auto env = GenericEnvironment::forOpenedExistential(
258258
getOpASTType(existentialTy), sig, UUID::fromTime());
259+
auto interfaceTy = OpenedArchetypeType::getSelfInterfaceTypeFromContext(sig, existentialTy->getASTContext());
259260
auto replacementTy =
260-
env->mapTypeIntoContext(archetypeTy->getInterfaceType())
261+
env->mapTypeIntoContext(interfaceTy)
261262
->template castTo<OpenedArchetypeType>();
262263
registerOpenedExistentialRemapping(archetypeTy, replacementTy);
263264
}

lib/IRGen/GenCast.cpp

+12-5
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,8 @@ llvm::Value *irgen::emitCheckedCast(IRGenFunction &IGF,
8888
FailableCastResult irgen::emitClassIdenticalCast(IRGenFunction &IGF,
8989
llvm::Value *from,
9090
SILType fromType,
91-
SILType toType) {
91+
SILType toType,
92+
GenericSignature fnSig) {
9293
// Check metatype objects directly. Don't try to find their meta-metatype.
9394
auto isMetatype = false;
9495
if (auto metaType = toType.getAs<MetatypeType>()) {
@@ -125,7 +126,7 @@ FailableCastResult irgen::emitClassIdenticalCast(IRGenFunction &IGF,
125126
// =>
126127
// icmp eq %1, @metadata.Sub
127128
llvm::Value *objectMetadata = isMetatype ? from :
128-
emitHeapMetadataRefForHeapObject(IGF, from, fromType);
129+
emitHeapMetadataRefForHeapObject(IGF, from, fromType, fnSig);
129130

130131
objectMetadata = IGF.Builder.CreateBitCast(objectMetadata,
131132
targetMetadata->getType());
@@ -547,6 +548,7 @@ void irgen::emitScalarExistentialDowncast(IRGenFunction &IGF,
547548
SILType destType,
548549
CheckedCastMode mode,
549550
Optional<MetatypeRepresentation> metatypeKind,
551+
GenericSignature fnSig,
550552
Explosion &ex) {
551553
auto srcInstanceType = srcType.getASTType();
552554
auto destInstanceType = destType.getASTType();
@@ -783,7 +785,8 @@ void irgen::emitScalarExistentialDowncast(IRGenFunction &IGF,
783785
// Get the type metadata for the instance.
784786
metadataValue = emitDynamicTypeOfHeapObject(IGF, value,
785787
MetatypeRepresentation::Thick,
786-
srcType);
788+
srcType,
789+
fnSig);
787790
}
788791

789792
// Look up witness tables for the protocols that need them.
@@ -866,6 +869,7 @@ void irgen::emitScalarCheckedCast(IRGenFunction &IGF,
866869
SILType targetLoweredType,
867870
CanType targetFormalType,
868871
CheckedCastMode mode,
872+
GenericSignature fnSig,
869873
Explosion &out) {
870874
assert(sourceLoweredType.isObject());
871875
assert(targetLoweredType.isObject());
@@ -949,6 +953,7 @@ void irgen::emitScalarCheckedCast(IRGenFunction &IGF,
949953
emitScalarExistentialDowncast(IGF, metatypeVal, sourceLoweredType,
950954
targetLoweredType, mode,
951955
existential->getRepresentation(),
956+
fnSig,
952957
out);
953958
return;
954959

@@ -1017,7 +1022,7 @@ void irgen::emitScalarCheckedCast(IRGenFunction &IGF,
10171022
llvm::Value *instance;
10181023
if (sourceLoweredType.isExistentialType()) {
10191024
instance = emitClassExistentialProjection(IGF, value, sourceLoweredType,
1020-
CanArchetypeType());
1025+
CanArchetypeType(), fnSig);
10211026
} else {
10221027
instance = value.claimNext();
10231028
}
@@ -1026,7 +1031,9 @@ void irgen::emitScalarCheckedCast(IRGenFunction &IGF,
10261031
Explosion outRes;
10271032
emitScalarExistentialDowncast(IGF, instance, sourceLoweredType,
10281033
targetLoweredType, mode,
1029-
/*not a metatype*/ None, outRes);
1034+
/*not a metatype*/ None,
1035+
fnSig,
1036+
outRes);
10301037
returnNilCheckedResult(IGF.Builder, outRes);
10311038
return;
10321039
}

lib/IRGen/GenCast.h

+8-4
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ namespace irgen {
5252
CanType sourceFormalType,
5353
SILType targetLoweredType,
5454
CanType targetFormalType,
55-
CheckedCastMode mode, Explosion &out);
55+
CheckedCastMode mode,
56+
GenericSignature fnSig,
57+
Explosion &out);
5658

5759
/// Convert a class object to the given destination type,
5860
/// using a runtime-checked cast.
@@ -71,9 +73,10 @@ namespace irgen {
7173

7274
/// Convert the given value to the exact destination type.
7375
FailableCastResult emitClassIdenticalCast(IRGenFunction &IGF,
74-
llvm::Value *from,
75-
SILType fromType,
76-
SILType toType);
76+
llvm::Value *from,
77+
SILType fromType,
78+
SILType toType,
79+
GenericSignature fnSig);
7780

7881
/// Emit a checked cast of a metatype.
7982
void emitMetatypeDowncast(IRGenFunction &IGF,
@@ -93,6 +96,7 @@ namespace irgen {
9396
SILType destType,
9497
CheckedCastMode mode,
9598
Optional<MetatypeRepresentation> metatypeKind,
99+
GenericSignature fnSig,
96100
Explosion &ex);
97101

98102
/// Emit a checked cast from a metatype to AnyObject.

lib/IRGen/GenClass.cpp

+20-12
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,8 @@ OwnedAddress irgen::projectPhysicalClassMemberAddress(IRGenFunction &IGF,
569569
llvm::Value *base,
570570
SILType baseType,
571571
SILType fieldType,
572-
VarDecl *field) {
572+
VarDecl *field,
573+
GenericSignature fnSig) {
573574
// If the field is empty, its address doesn't matter.
574575
auto &fieldTI = IGF.getTypeInfo(fieldType);
575576
if (fieldTI.isKnownEmpty(ResilienceExpansion::Maximal)) {
@@ -603,7 +604,7 @@ OwnedAddress irgen::projectPhysicalClassMemberAddress(IRGenFunction &IGF,
603604
}
604605

605606
case FieldAccess::ConstantIndirect: {
606-
auto metadata = emitHeapMetadataRefForHeapObject(IGF, base, baseType);
607+
auto metadata = emitHeapMetadataRefForHeapObject(IGF, base, baseType, fnSig);
607608
auto offset = emitClassFieldOffset(IGF, baseClass, field, metadata);
608609
return emitAddressAtOffset(IGF, baseType, base, offset, field);
609610
}
@@ -644,8 +645,9 @@ irgen::getPhysicalClassMemberAccessStrategy(IRGenModule &IGM,
644645
}
645646

646647
Address irgen::emitTailProjection(IRGenFunction &IGF, llvm::Value *Base,
647-
SILType ClassType,
648-
SILType TailType) {
648+
SILType ClassType,
649+
SILType TailType,
650+
GenericSignature fnSig) {
649651
const ClassTypeInfo &classTI = IGF.getTypeInfo(ClassType).as<ClassTypeInfo>();
650652

651653
llvm::Value *Offset = nullptr;
@@ -661,7 +663,8 @@ Address irgen::emitTailProjection(IRGenFunction &IGF, llvm::Value *Base,
661663
Align = HeapObjAlign.alignmentAtOffset(ClassSize);
662664
} else {
663665
llvm::Value *metadata = emitHeapMetadataRefForHeapObject(IGF, Base,
664-
ClassType);
666+
ClassType,
667+
fnSig);
665668
Offset = emitClassResilientInstanceSizeAndAlignMask(IGF,
666669
ClassType.getClassOrBoundGenericClass(),
667670
metadata).first;
@@ -876,6 +879,7 @@ static void getInstanceSizeAndAlignMask(IRGenFunction &IGF,
876879
SILType selfType,
877880
ClassDecl *selfClass,
878881
llvm::Value *selfValue,
882+
GenericSignature fnSig,
879883
llvm::Value *&size,
880884
llvm::Value *&alignMask) {
881885
// Try to determine the size of the object we're deallocating.
@@ -892,7 +896,7 @@ static void getInstanceSizeAndAlignMask(IRGenFunction &IGF,
892896

893897
// Otherwise, get them from the metadata.
894898
llvm::Value *metadata =
895-
emitHeapMetadataRefForHeapObject(IGF, selfValue, selfType);
899+
emitHeapMetadataRefForHeapObject(IGF, selfValue, selfType, fnSig);
896900
std::tie(size, alignMask)
897901
= emitClassResilientInstanceSizeAndAlignMask(IGF, selfClass, metadata);
898902
}
@@ -902,8 +906,10 @@ static llvm::Value *emitCastToHeapObject(IRGenFunction &IGF,
902906
return IGF.Builder.CreateBitCast(value, IGF.IGM.RefCountedPtrTy);
903907
}
904908

905-
void irgen::emitClassDeallocation(IRGenFunction &IGF, SILType selfType,
906-
llvm::Value *selfValue) {
909+
void irgen::emitClassDeallocation(IRGenFunction &IGF,
910+
SILType selfType,
911+
llvm::Value *selfValue,
912+
GenericSignature fnSig) {
907913
auto *theClass = selfType.getClassOrBoundGenericClass();
908914

909915
// We want to deallocate default actors or potential default
@@ -934,7 +940,7 @@ void irgen::emitClassDeallocation(IRGenFunction &IGF, SILType selfType,
934940
}
935941

936942
llvm::Value *size, *alignMask;
937-
getInstanceSizeAndAlignMask(IGF, selfType, theClass, selfValue,
943+
getInstanceSizeAndAlignMask(IGF, selfType, theClass, selfValue, fnSig,
938944
size, alignMask);
939945

940946
selfValue = emitCastToHeapObject(IGF, selfValue);
@@ -944,12 +950,13 @@ void irgen::emitClassDeallocation(IRGenFunction &IGF, SILType selfType,
944950
void irgen::emitPartialClassDeallocation(IRGenFunction &IGF,
945951
SILType selfType,
946952
llvm::Value *selfValue,
947-
llvm::Value *metadataValue) {
953+
llvm::Value *metadataValue,
954+
GenericSignature fnSig) {
948955
auto *theClass = selfType.getClassOrBoundGenericClass();
949956
assert(theClass->getForeignClassKind() == ClassDecl::ForeignKind::Normal);
950957

951958
llvm::Value *size, *alignMask;
952-
getInstanceSizeAndAlignMask(IGF, selfType, theClass, selfValue,
959+
getInstanceSizeAndAlignMask(IGF, selfType, theClass, selfValue, fnSig,
953960
size, alignMask);
954961

955962
selfValue = IGF.Builder.CreateBitCast(selfValue, IGF.IGM.RefCountedPtrTy);
@@ -2815,6 +2822,7 @@ irgen::emitVirtualMethodValue(IRGenFunction &IGF,
28152822
SILType baseType,
28162823
SILDeclRef method,
28172824
CanSILFunctionType methodType,
2825+
GenericSignature fnSig,
28182826
bool useSuperVTable) {
28192827
// Find the metadata.
28202828
llvm::Value *metadata;
@@ -2833,7 +2841,7 @@ irgen::emitVirtualMethodValue(IRGenFunction &IGF,
28332841
metadata = base;
28342842
} else {
28352843
// Otherwise, load the class metadata from the 'self' value's isa pointer.
2836-
metadata = emitHeapMetadataRefForHeapObject(IGF, base, baseType,
2844+
metadata = emitHeapMetadataRefForHeapObject(IGF, base, baseType, fnSig,
28372845
/*suppress cast*/ true);
28382846
}
28392847
}

lib/IRGen/GenClass.h

+11-5
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ namespace irgen {
5757

5858
OwnedAddress projectPhysicalClassMemberAddress(
5959
IRGenFunction &IGF, llvm::Value *base,
60-
SILType baseType, SILType fieldType, VarDecl *field);
60+
SILType baseType, SILType fieldType, VarDecl *field,
61+
GenericSignature fnSig);
6162

6263
/// Return a strategy for accessing the given stored class property.
6364
///
@@ -145,7 +146,8 @@ namespace irgen {
145146
/// Emit a projection from a class instance to the first tail allocated
146147
/// element.
147148
Address emitTailProjection(IRGenFunction &IGF, llvm::Value *Base,
148-
SILType ClassType, SILType TailType);
149+
SILType ClassType, SILType TailType,
150+
GenericSignature fnSig);
149151

150152
using TailArraysRef = llvm::ArrayRef<std::pair<SILType, llvm::Value *>>;
151153

@@ -175,14 +177,17 @@ namespace irgen {
175177
TailArraysRef TailArrays);
176178

177179
/// Emit class deallocation.
178-
void emitClassDeallocation(IRGenFunction &IGF, SILType selfType,
179-
llvm::Value *selfValue);
180+
void emitClassDeallocation(IRGenFunction &IGF,
181+
SILType selfType,
182+
llvm::Value *selfValue,
183+
GenericSignature fnSig);
180184

181185
/// Emit class deallocation.
182186
void emitPartialClassDeallocation(IRGenFunction &IGF,
183187
SILType selfType,
184188
llvm::Value *selfValue,
185-
llvm::Value *metadataValue);
189+
llvm::Value *metadataValue,
190+
GenericSignature fnSig);
186191

187192
/// Emit the constant fragile offset of the given property inside an instance
188193
/// of the class.
@@ -218,6 +223,7 @@ namespace irgen {
218223
FunctionPointer emitVirtualMethodValue(IRGenFunction &IGF, llvm::Value *base,
219224
SILType baseType, SILDeclRef method,
220225
CanSILFunctionType methodType,
226+
GenericSignature fnSig,
221227
bool useSuperVTable);
222228

223229
/// Is the given class known to have Swift-compatible metadata?

lib/IRGen/GenExistential.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -1699,7 +1699,8 @@ Address irgen::emitOpenExistentialBox(IRGenFunction &IGF,
16991699
OwnedAddress irgen::emitBoxedExistentialContainerAllocation(IRGenFunction &IGF,
17001700
SILType destType,
17011701
CanType formalSrcType,
1702-
ArrayRef<ProtocolConformanceRef> conformances) {
1702+
ArrayRef<ProtocolConformanceRef> conformances,
1703+
GenericSignature sig) {
17031704
// TODO: Non-Error boxed existentials.
17041705
assert(destType.canUseExistentialRepresentation(
17051706
ExistentialRepresentation::Boxed, Type()));
@@ -1727,7 +1728,7 @@ OwnedAddress irgen::emitBoxedExistentialContainerAllocation(IRGenFunction &IGF,
17271728
auto addr = IGF.Builder.CreateExtractValue(result, 1);
17281729

17291730
auto archetype =
1730-
OpenedArchetypeType::get(destType.getASTType(), IGF.IGM.getSwiftModule());
1731+
OpenedArchetypeType::get(destType.getASTType(), sig);
17311732
auto &srcTI = IGF.getTypeInfoForUnlowered(AbstractionPattern(archetype),
17321733
formalSrcType);
17331734
addr = IGF.Builder.CreateBitCast(addr,
@@ -1943,6 +1944,7 @@ void irgen::emitMetatypeOfBoxedExistential(IRGenFunction &IGF, Explosion &value,
19431944
void irgen::emitMetatypeOfClassExistential(IRGenFunction &IGF, Explosion &value,
19441945
SILType metatypeTy,
19451946
SILType existentialTy,
1947+
GenericSignature fnSig,
19461948
Explosion &out) {
19471949
assert(existentialTy.isClassExistentialType());
19481950
auto &baseTI = IGF.getTypeInfo(existentialTy).as<ClassExistentialTypeInfo>();
@@ -1962,6 +1964,7 @@ void irgen::emitMetatypeOfClassExistential(IRGenFunction &IGF, Explosion &value,
19621964

19631965
auto dynamicType = emitDynamicTypeOfHeapObject(IGF, instance, repr,
19641966
existentialTy,
1967+
fnSig,
19651968
/*allow artificial*/ false);
19661969
out.add(dynamicType);
19671970

@@ -1996,7 +1999,8 @@ llvm::Value *
19961999
irgen::emitClassExistentialProjection(IRGenFunction &IGF,
19972000
Explosion &base,
19982001
SILType baseTy,
1999-
CanArchetypeType openedArchetype) {
2002+
CanArchetypeType openedArchetype,
2003+
GenericSignature sigFn) {
20002004
assert(baseTy.isClassExistentialType());
20012005
auto &baseTI = IGF.getTypeInfo(baseTy).as<ClassExistentialTypeInfo>();
20022006

@@ -2011,6 +2015,7 @@ irgen::emitClassExistentialProjection(IRGenFunction &IGF,
20112015
auto metadata = emitDynamicTypeOfHeapObject(IGF, value,
20122016
MetatypeRepresentation::Thick,
20132017
baseTy,
2018+
sigFn,
20142019
/*allow artificial*/ false);
20152020
IGF.bindArchetype(openedArchetype, metadata, MetadataState::Complete,
20162021
wtables);
@@ -2344,7 +2349,8 @@ getProjectBoxedOpaqueExistentialFunction(IRGenFunction &IGF,
23442349

23452350
Address irgen::emitOpaqueBoxedExistentialProjection(
23462351
IRGenFunction &IGF, OpenedExistentialAccess accessKind, Address base,
2347-
SILType existentialTy, CanArchetypeType openedArchetype) {
2352+
SILType existentialTy, CanArchetypeType openedArchetype,
2353+
GenericSignature fnSig) {
23482354

23492355
assert(existentialTy.isExistentialType());
23502356
if (existentialTy.isClassExistentialType()) {
@@ -2355,6 +2361,7 @@ Address irgen::emitOpaqueBoxedExistentialProjection(
23552361
auto metadata = emitDynamicTypeOfHeapObject(IGF, value,
23562362
MetatypeRepresentation::Thick,
23572363
existentialTy,
2364+
fnSig,
23582365
/*allow artificial*/ false);
23592366

23602367
// If we are projecting into an opened archetype, capture the

0 commit comments

Comments
 (0)