Skip to content

Commit 4742968

Browse files
committed
Serialization support for SIL substituted function types
1 parent 84e20b6 commit 4742968

File tree

7 files changed

+40
-15
lines changed

7 files changed

+40
-15
lines changed

lib/AST/Type.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4843,3 +4843,14 @@ CanType swift::substOpaqueTypesWithUnderlyingTypes(CanType ty,
48434843
SubstFlags::SubstituteOpaqueArchetypes | SubstFlags::AllowLoweredTypes;
48444844
return ty.subst(replacer, replacer, flags)->getCanonicalType();
48454845
}
4846+
4847+
CanSILFunctionType
4848+
SILFunctionType::withSubstitutions(SubstitutionMap subs) const {
4849+
return SILFunctionType::get(getSubstGenericSignature(),
4850+
getExtInfo(), getCoroutineKind(),
4851+
getCalleeConvention(),
4852+
getParameters(), getYields(), getResults(),
4853+
getOptionalErrorResult(),
4854+
subs, isGenericSignatureImplied(),
4855+
const_cast<SILFunctionType*>(this)->getASTContext());
4856+
}

lib/SIL/SILFunctionType.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3334,17 +3334,6 @@ StringRef SILFunctionType::ABICompatibilityCheckResult::getMessage() const {
33343334
llvm_unreachable("Covered switch isn't completely covered?!");
33353335
}
33363336

3337-
CanSILFunctionType
3338-
SILFunctionType::withSubstitutions(SubstitutionMap subs) const {
3339-
return SILFunctionType::get(getSubstGenericSignature(),
3340-
getExtInfo(), getCoroutineKind(),
3341-
getCalleeConvention(),
3342-
getParameters(), getYields(), getResults(),
3343-
getOptionalErrorResult(),
3344-
subs, isGenericSignatureImplied(),
3345-
const_cast<SILFunctionType*>(this)->getASTContext());
3346-
}
3347-
33483337
static DeclContext *getDeclContextForExpansion(const SILFunction &f) {
33493338
auto *dc = f.getDeclContext();
33503339
if (!dc)

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2744,7 +2744,8 @@ Type TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
27442744
subsMap.insert({params[i], resolved->getCanonicalType()});
27452745
}
27462746
subs = SubstitutionMap::get(sig, QueryTypeSubstitutionMap{subsMap},
2747-
TypeChecker::LookUpConformance(DC));
2747+
TypeChecker::LookUpConformance(DC))
2748+
.getCanonical();
27482749
}
27492750

27502751
if (hasError) {

lib/Serialization/Deserialization.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5029,7 +5029,9 @@ class swift::TypeDeserializer {
50295029
unsigned numParams;
50305030
unsigned numYields;
50315031
unsigned numResults;
5032+
bool isGenericSignatureImplied;
50325033
GenericSignatureID rawGenericSig;
5034+
SubstitutionMapID rawSubs;
50335035
ArrayRef<uint64_t> variableData;
50345036
clang::FunctionType *clangFunctionType = nullptr;
50355037

@@ -5046,7 +5048,9 @@ class swift::TypeDeserializer {
50465048
numParams,
50475049
numYields,
50485050
numResults,
5051+
isGenericSignatureImplied,
50495052
rawGenericSig,
5053+
rawSubs,
50505054
variableData);
50515055

50525056
// Process the ExtInfo.
@@ -5166,11 +5170,13 @@ class swift::TypeDeserializer {
51665170
}
51675171

51685172
GenericSignature genericSig = MF.getGenericSignature(rawGenericSig);
5173+
SubstitutionMap subs = MF.getSubstitutionMap(rawSubs).getCanonical();
5174+
51695175
return SILFunctionType::get(genericSig, extInfo, coroutineKind.getValue(),
51705176
calleeConvention.getValue(),
51715177
allParams, allYields, allResults,
51725178
errorResult,
5173-
SubstitutionMap(), false,
5179+
subs, isGenericSignatureImplied,
51745180
ctx, witnessMethodConformance);
51755181
}
51765182

lib/Serialization/ModuleFormat.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
5757
/// Don't worry about adhering to the 80-column limit for this line.
58-
const uint16_t SWIFTMODULE_VERSION_MINOR = 528; // @derivative attribute
58+
const uint16_t SWIFTMODULE_VERSION_MINOR = 529; // SILFunctionType substitutions
5959

6060
/// A standard hash seed used for all string hashes in a serialized module.
6161
///
@@ -990,11 +990,14 @@ namespace decls_block {
990990
BCVBR<6>, // number of parameters
991991
BCVBR<5>, // number of yields
992992
BCVBR<5>, // number of results
993+
BCFixed<1>, // generic signature implied
993994
GenericSignatureIDField, // generic signature
995+
SubstitutionMapIDField, // substitutions
994996
BCArray<TypeIDField> // parameter types/conventions, alternating
995997
// followed by result types/conventions, alternating
996998
// followed by error result type/convention
997999
// Optionally a protocol conformance (for witness_methods)
1000+
// Optionally a substitution map (for substituted function types)
9981001
>;
9991002

10001003
using SILBlockStorageTypeLayout = BCRecordLayout<

lib/Serialization/Serialization.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4091,7 +4091,10 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
40914091
stableRepresentation, fnTy->isPseudogeneric(), fnTy->isNoEscape(),
40924092
stableDiffKind, fnTy->hasErrorResult(), fnTy->getParameters().size(),
40934093
fnTy->getNumYields(), fnTy->getNumResults(),
4094-
S.addGenericSignatureRef(sig), variableData);
4094+
fnTy->isGenericSignatureImplied(),
4095+
S.addGenericSignatureRef(sig),
4096+
S.addSubstitutionMapRef(fnTy->getSubstitutions()),
4097+
variableData);
40954098

40964099
if (auto conformance = fnTy->getWitnessMethodConformanceOrInvalid())
40974100
S.writeConformance(conformance, S.DeclTypeAbbrCodes);

test/SIL/Serialization/basic.sil

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,12 @@ struct Int32 {
3838
var x: Builtin.Int32
3939
}
4040

41+
// CHECK-LABEL: sil [serialized] [ossa] @test_subst_function_type : $@convention(thin) (@guaranteed @callee_guaranteed <τ_0_0, τ_0_1> in (@in τ_0_0, @in τ_0_1) -> () for <Builtin.Int32, Builtin.Int64>) -> ()
42+
sil [serialized] [ossa] @test_subst_function_type : $@convention(thin) (@guaranteed @callee_guaranteed <A, B> in (@in A, @in B) -> () for <Builtin.Int32, Builtin.Int64>) -> () {
43+
entry(%0 : @guaranteed $@callee_guaranteed <C, D> in (@in C, @in D) -> () for <Builtin.Int32, Builtin.Int64>):
44+
return undef : $()
45+
}
46+
4147
sil [ossa] @test_destructure_struct_tuple : $@convention(thin) (@owned (Builtin.NativeObject, Builtin.Int32), @owned TestArray2) -> @owned (Builtin.NativeObject, Builtin.Int32, TestArrayStorage, Int32, TestArrayStorage) {
4248
bb0(%0 : @owned $(Builtin.NativeObject, Builtin.Int32), %1 : @owned $TestArray2):
4349
(%2, %3) = destructure_tuple %0 : $(Builtin.NativeObject, Builtin.Int32)
@@ -46,6 +52,12 @@ bb0(%0 : @owned $(Builtin.NativeObject, Builtin.Int32), %1 : @owned $TestArray2)
4652
return %7 : $(Builtin.NativeObject, Builtin.Int32, TestArrayStorage, Int32, TestArrayStorage)
4753
}
4854

55+
// CHECK-LABEL: sil [serialized] [ossa] @test_subst_function_type_generic_context : $@convention(thin) <X, Y> (@guaranteed @callee_guaranteed <τ_0_0, τ_0_1> in (@in τ_0_0, @in τ_0_1) -> () for <X, Y>) -> ()
56+
sil [serialized] [ossa] @test_subst_function_type_generic_context : $@convention(thin) <X, Y> (@guaranteed @callee_guaranteed <A, B> in (@in A, @in B) -> () for <X, Y>) -> () {
57+
entry(%0 : @guaranteed $@callee_guaranteed <C, D> in (@in C, @in D) -> () for <X, Y>):
58+
return undef : $()
59+
}
60+
4961
struct EmptyStruct {}
5062

5163
sil @test_empty_destructure : $@convention(thin) () -> () {

0 commit comments

Comments
 (0)