Skip to content

Commit 62f947d

Browse files
authored
[Serialization] Drop GenericEnvironmentID for GenericSignatureID (swiftlang#26862)
A generic environment is always serialized as a GenericSignature with a lazily-recreated environment, though sometimes it has to include extra info specifically for generic environments used by SIL. The code that was doing this claimed a bit for disambiguating between the two, shrinking the permitted size of a compiled module from 2^31 bits to 2^30. (The code isn't just needlessly complicated; GenericEnvironments used to be serialized with more information.) Rather than have two representations for GenericEnvironmentID, this commit just drops it altogether in favor of referencing GenericSignatures directly. This causes a negligible file size shrinkage for swiftmodules in addition to eliminating the problematic disambiguation bit. For now, the Deserialization logic will continue to cache GenericEnvironments that are used directly by Deserialization, but really that should probably be done at the AST level. Then we can simplify further to ModuleFile tracking a plain list of GenericSignatures.
1 parent 10fd6fb commit 62f947d

File tree

10 files changed

+160
-268
lines changed

10 files changed

+160
-268
lines changed

include/swift/Serialization/DeclTypeRecordNodes.def

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ OTHER(GENERIC_SIGNATURE, 231)
167167
TRAILING_INFO(GENERIC_REQUIREMENT)
168168
TRAILING_INFO(LAYOUT_REQUIREMENT)
169169
// 234 is unused
170-
OTHER(SIL_GENERIC_ENVIRONMENT, 235)
170+
OTHER(SIL_GENERIC_SIGNATURE, 235)
171171
OTHER(SUBSTITUTION_MAP, 236)
172172

173173
OTHER(LOCAL_DISCRIMINATOR, 237)

include/swift/Serialization/ModuleFile.h

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -210,14 +210,13 @@ class ModuleFile
210210
return Value.template get<serialization::BitOffset>();
211211
}
212212

213-
template <typename Derived>
214-
Serialized &operator=(Derived deserialized) {
213+
Serialized &operator=(T deserialized) {
215214
assert(!isComplete() || ImplTy(deserialized) == Value);
216215
Value = deserialized;
217216
return *this;
218217
}
219218

220-
void unsafeOverwrite(T t) {
219+
void uncheckedOverwrite(T t) {
221220
Value = t;
222221
}
223222
};
@@ -317,11 +316,17 @@ class ModuleFile
317316
/// Types referenced by this module.
318317
MutableArrayRef<Serialized<Type>> Types;
319318

320-
/// Generic signatures referenced by this module.
321-
MutableArrayRef<Serialized<GenericSignature *>> GenericSignatures;
319+
using GenericSignatureOrEnvironment =
320+
llvm::PointerUnion<GenericSignature *, GenericEnvironment *>;
322321

323-
/// Generic environments referenced by this module.
324-
MutableArrayRef<Serialized<GenericEnvironment *>> GenericEnvironments;
322+
/// Generic signatures and environments referenced by this module.
323+
///
324+
/// Technically only the GenericSignatures are encoded, but storing the
325+
/// environment here too allows caching them.
326+
// FIXME: That caching should be done at the AST level; it's not specific to
327+
// Serialization.
328+
MutableArrayRef<Serialized<GenericSignatureOrEnvironment>>
329+
GenericSignaturesAndEnvironments;
325330

326331
/// Substitution maps referenced by this module.
327332
MutableArrayRef<Serialized<SubstitutionMap>> SubstitutionMaps;
@@ -560,9 +565,8 @@ class ModuleFile
560565

561566
/// Set up a (potentially lazy) generic environment for the given type,
562567
/// function or extension.
563-
void configureGenericEnvironment(
564-
GenericContext *genericDecl,
565-
serialization::GenericEnvironmentID envID);
568+
void configureGenericEnvironment(GenericContext *genericDecl,
569+
serialization::GenericSignatureID envID);
566570

567571
/// Populates the protocol's default witness table.
568572
///
@@ -879,14 +883,14 @@ class ModuleFile
879883
/// \param wantEnvironment If true, always return the full generic
880884
/// environment. Otherwise, only return the generic environment if it's
881885
/// already been constructed, and the signature in other cases.
882-
llvm::PointerUnion<GenericSignature *, GenericEnvironment *>
883-
getGenericSignatureOrEnvironment(serialization::GenericEnvironmentID ID,
886+
GenericSignatureOrEnvironment
887+
getGenericSignatureOrEnvironment(serialization::GenericSignatureID ID,
884888
bool wantEnvironment = false);
885889

886890
/// Returns the generic environment for the given ID, deserializing it if
887891
/// needed.
888-
GenericEnvironment *getGenericEnvironment(
889-
serialization::GenericEnvironmentID ID);
892+
GenericEnvironment *
893+
getGenericEnvironment(serialization::GenericSignatureID ID);
890894

891895
/// Returns the substitution map for the given ID, deserializing it if
892896
/// needed.

include/swift/Serialization/ModuleFormat.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5252
/// describe what change you made. The content of this comment isn't important;
5353
/// it just ensures a conflict if two people change the module format.
5454
/// Don't worry about adhering to the 80-column limit for this line.
55-
const uint16_t SWIFTMODULE_VERSION_MINOR = 515; // remove DeclContext indirection
55+
const uint16_t SWIFTMODULE_VERSION_MINOR = 516; // encode GenericSignature and GenericEnvironment together
5656

5757
using DeclIDField = BCFixed<31>;
5858

@@ -129,11 +129,6 @@ using NormalConformanceIDField = DeclIDField;
129129
using GenericSignatureID = DeclID;
130130
using GenericSignatureIDField = DeclIDField;
131131

132-
// GenericEnvironmentID must be the same as DeclID because it is stored in the
133-
// same way.
134-
using GenericEnvironmentID = DeclID;
135-
using GenericEnvironmentIDField = DeclIDField;
136-
137132
// SubstitutionMapID must be the same as DeclID because it is stored in the
138133
// same way.
139134
using SubstitutionMapID = DeclID;
@@ -843,7 +838,7 @@ namespace decls_block {
843838

844839
using PrimaryArchetypeTypeLayout = BCRecordLayout<
845840
PRIMARY_ARCHETYPE_TYPE,
846-
GenericEnvironmentIDField, // generic environment
841+
GenericSignatureIDField, // generic environment
847842
BCVBR<4>, // generic type parameter depth
848843
BCVBR<4> // index + 1, or zero if we have a generic type parameter decl
849844
>;
@@ -963,7 +958,7 @@ namespace decls_block {
963958
TypeIDField, // underlying type
964959
TypeIDField, // interface type (no longer used)
965960
BCFixed<1>, // implicit flag
966-
GenericEnvironmentIDField, // generic environment
961+
GenericSignatureIDField, // generic environment
967962
AccessLevelField, // access level
968963
BCArray<TypeIDField> // dependency types
969964
// Trailed by generic parameters (if any).
@@ -992,7 +987,7 @@ namespace decls_block {
992987
DeclContextIDField, // context decl
993988
BCFixed<1>, // implicit flag
994989
BCFixed<1>, // isObjC
995-
GenericEnvironmentIDField, // generic environment
990+
GenericSignatureIDField, // generic environment
996991
AccessLevelField, // access level
997992
BCVBR<4>, // number of conformances
998993
BCVBR<4>, // number of inherited types
@@ -1007,7 +1002,7 @@ namespace decls_block {
10071002
DeclContextIDField, // context decl
10081003
BCFixed<1>, // implicit flag
10091004
BCFixed<1>, // isObjC
1010-
GenericEnvironmentIDField, // generic environment
1005+
GenericSignatureIDField, // generic environment
10111006
TypeIDField, // raw type
10121007
AccessLevelField, // access level
10131008
BCVBR<4>, // number of conformances
@@ -1024,7 +1019,7 @@ namespace decls_block {
10241019
BCFixed<1>, // implicit?
10251020
BCFixed<1>, // explicitly objc?
10261021
BCFixed<1>, // inherits convenience initializers from its superclass?
1027-
GenericEnvironmentIDField, // generic environment
1022+
GenericSignatureIDField, // generic environment
10281023
TypeIDField, // superclass
10291024
AccessLevelField, // access level
10301025
BCVBR<4>, // number of conformances
@@ -1066,7 +1061,7 @@ namespace decls_block {
10661061
BCFixed<1>, // stub implementation?
10671062
BCFixed<1>, // throws?
10681063
CtorInitializerKindField, // initializer kind
1069-
GenericEnvironmentIDField, // generic environment
1064+
GenericSignatureIDField, // generic environment
10701065
DeclIDField, // overridden decl
10711066
AccessLevelField, // access level
10721067
BCFixed<1>, // requires a new vtable slot
@@ -1134,7 +1129,7 @@ namespace decls_block {
11341129
SelfAccessKindField, // self access kind
11351130
BCFixed<1>, // has forced static dispatch?
11361131
BCFixed<1>, // throws?
1137-
GenericEnvironmentIDField, // generic environment
1132+
GenericSignatureIDField, // generic environment
11381133
TypeIDField, // result interface type
11391134
BCFixed<1>, // IUO result?
11401135
DeclIDField, // operator decl
@@ -1160,7 +1155,7 @@ namespace decls_block {
11601155
DeclIDField, // naming decl
11611156
GenericSignatureIDField, // interface generic signature
11621157
TypeIDField, // interface type for opaque type
1163-
GenericEnvironmentIDField, // generic environment
1158+
GenericSignatureIDField, // generic environment
11641159
SubstitutionMapIDField // optional substitution map for underlying type
11651160
// trailed by generic parameters
11661161
>;
@@ -1176,7 +1171,7 @@ namespace decls_block {
11761171
SelfAccessKindField, // self access kind
11771172
BCFixed<1>, // has forced static dispatch?
11781173
BCFixed<1>, // throws?
1179-
GenericEnvironmentIDField, // generic environment
1174+
GenericSignatureIDField, // generic environment
11801175
TypeIDField, // result interface type
11811176
BCFixed<1>, // IUO result?
11821177
DeclIDField, // overridden function
@@ -1263,7 +1258,7 @@ namespace decls_block {
12631258
WriteImplKindField, // write implementation
12641259
ReadWriteImplKindField, // read-write implementation
12651260
AccessorCountField, // number of accessors
1266-
GenericEnvironmentIDField, // generic environment
1261+
GenericSignatureIDField, // generic environment
12671262
TypeIDField, // element interface type
12681263
BCFixed<1>, // IUO element?
12691264
DeclIDField, // overridden decl
@@ -1286,7 +1281,7 @@ namespace decls_block {
12861281
TypeIDField, // base type
12871282
DeclContextIDField, // context decl
12881283
BCFixed<1>, // implicit flag
1289-
GenericEnvironmentIDField, // generic environment
1284+
GenericSignatureIDField, // generic environment
12901285
BCVBR<4>, // # of protocol conformances
12911286
BCVBR<4>, // number of inherited types
12921287
BCArray<TypeIDField> // inherited types, followed by TypeID dependencies
@@ -1299,7 +1294,7 @@ namespace decls_block {
12991294
DeclContextIDField, // context decl
13001295
BCFixed<1>, // implicit?
13011296
BCFixed<1>, // objc?
1302-
GenericEnvironmentIDField // generic environment
1297+
GenericSignatureIDField // generic environment
13031298
// This record is trailed by its inlinable body text
13041299
>;
13051300

@@ -1379,8 +1374,8 @@ namespace decls_block {
13791374
// Conformances trail the record.
13801375
>;
13811376

1382-
using SILGenericEnvironmentLayout = BCRecordLayout<
1383-
SIL_GENERIC_ENVIRONMENT,
1377+
using SILGenericSignatureLayout = BCRecordLayout<
1378+
SIL_GENERIC_SIGNATURE,
13841379
BCArray<TypeIDField> // (generic parameter name, sugared interface
13851380
// type) pairs
13861381
>;
@@ -1780,7 +1775,7 @@ namespace index_block {
17801775
LOCAL_DECL_CONTEXT_OFFSETS,
17811776
LOCAL_TYPE_DECLS,
17821777
OPAQUE_RETURN_TYPE_DECLS,
1783-
GENERIC_ENVIRONMENT_OFFSETS,
1778+
GENERIC_SIGNATURE_OFFSETS,
17841779
NORMAL_CONFORMANCE_OFFSETS,
17851780
SIL_LAYOUT_OFFSETS,
17861781

@@ -1790,7 +1785,6 @@ namespace index_block {
17901785

17911786
ORDERED_TOP_LEVEL_DECLS,
17921787

1793-
GENERIC_SIGNATURE_OFFSETS,
17941788
SUBSTITUTION_MAP_OFFSETS,
17951789
LastRecordKind = SUBSTITUTION_MAP_OFFSETS,
17961790
};

0 commit comments

Comments
 (0)