Skip to content

Commit 02784b3

Browse files
authored
Merge pull request #36025 from gottesmm/pr-36fe6ec4772e3d5e09f5031a505217e7d7aba8dc
[ownership] On non-Darwin platforms, serialize the stdlib in OSSA form.
2 parents 15da94f + 57654da commit 02784b3

15 files changed

+281
-270
lines changed

include/swift/AST/SILOptions.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,8 @@ class SILOptions {
154154
bool VerifyExclusivity = false;
155155

156156
/// When building the stdlib with opts should we lower ownership after
157-
/// serialization? Otherwise we do before. Only set to true on Darwin for now.
158-
///
159-
bool SerializeStdlibWithOwnershipWithOpts = false;
157+
/// serialization? Otherwise we do before.
158+
bool SerializeStdlibWithOwnershipWithOpts = true;
160159

161160
/// Calls to the replaced method inside of the replacement method will call
162161
/// the previous implementation.

include/swift/Serialization/SerializedSILLoader.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class ModuleDecl;
2929
class SILDeserializer;
3030
class SILFunction;
3131
class SILGlobalVariable;
32+
class SILProperty;
3233
class SILModule;
3334
class SILVTable;
3435
class SILWitnessTable;
@@ -69,10 +70,16 @@ class SerializedSILLoader {
6970
SILDifferentiabilityWitness *
7071
lookupDifferentiabilityWitness(SILDifferentiabilityWitnessKey key);
7172

72-
/// Invalidate the cached entries for deserialized SILFunctions.
73-
void invalidateCaches();
74-
75-
bool invalidateFunction(SILFunction *F);
73+
/// Invalidate the cached entries for deserialized state. Must be
74+
/// called when erasing deserialized state in the SILModule.
75+
void invalidateAllCaches();
76+
bool invalidateFunction(SILFunction *f);
77+
bool invalidateGlobalVariable(SILGlobalVariable *gv);
78+
bool invalidateVTable(SILVTable *vt);
79+
bool invalidateWitnessTable(SILWitnessTable *wt);
80+
bool invalidateDefaultWitnessTable(SILDefaultWitnessTable *wt);
81+
bool invalidateProperty(SILProperty *p);
82+
bool invalidateDifferentiabilityWitness(SILDifferentiabilityWitness *w);
7683

7784
/// Deserialize all SILFunctions, VTables, and WitnessTables for
7885
/// a given Module.

lib/Frontend/CompilerInvocation.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,11 +1201,6 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
12011201
Args.hasArg(OPT_sil_stop_optzns_before_lowering_ownership);
12021202
if (const Arg *A = Args.getLastArg(OPT_external_pass_pipeline_filename))
12031203
Opts.ExternalPassPipelineFilename = A->getValue();
1204-
// If our triple is a darwin triple, lower ownership on the stdlib after we
1205-
// serialize.
1206-
if (Triple.isOSDarwin()) {
1207-
Opts.SerializeStdlibWithOwnershipWithOpts = true;
1208-
}
12091204

12101205
Opts.GenerateProfile |= Args.hasArg(OPT_profile_generate);
12111206
const Arg *ProfileUse = Args.getLastArg(OPT_profile_use);

lib/SIL/IR/SILModule.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,7 @@ SILModule::createDefaultWitnessTableDeclaration(const ProtocolDecl *Protocol,
320320
void SILModule::deleteWitnessTable(SILWitnessTable *Wt) {
321321
auto Conf = Wt->getConformance();
322322
assert(lookUpWitnessTable(Conf, false) == Wt);
323+
getSILLoader()->invalidateWitnessTable(Wt);
323324
WitnessTableMap.erase(Conf);
324325
witnessTables.erase(Wt);
325326
}
@@ -474,7 +475,7 @@ bool SILModule::hasFunction(StringRef Name) {
474475
}
475476

476477
void SILModule::invalidateSILLoaderCaches() {
477-
getSILLoader()->invalidateCaches();
478+
getSILLoader()->invalidateAllCaches();
478479
}
479480

480481
SILFunction *SILModule::removeFromZombieList(StringRef Name) {
@@ -526,9 +527,10 @@ void SILModule::invalidateFunctionInSILCache(SILFunction *F) {
526527
}
527528

528529
/// Erase a global SIL variable from the module.
529-
void SILModule::eraseGlobalVariable(SILGlobalVariable *G) {
530-
GlobalVariableMap.erase(G->getName());
531-
getSILGlobalList().erase(G);
530+
void SILModule::eraseGlobalVariable(SILGlobalVariable *gv) {
531+
getSILLoader()->invalidateGlobalVariable(gv);
532+
GlobalVariableMap.erase(gv->getName());
533+
getSILGlobalList().erase(gv);
532534
}
533535

534536
SILVTable *SILModule::lookUpVTable(const ClassDecl *C,

lib/Serialization/DeserializeSIL.cpp

Lines changed: 133 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "swift/SIL/SILBuilder.h"
2929
#include "swift/SIL/SILDebugScope.h"
3030
#include "swift/SIL/SILModule.h"
31+
#include "swift/SIL/SILProperty.h"
3132
#include "swift/SIL/SILUndef.h"
3233

3334
#include "llvm/ADT/Statistic.h"
@@ -2954,11 +2955,11 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
29542955

29552956
assert(VId <= GlobalVars.size() && "invalid GlobalVar ID");
29562957
auto &globalVarOrOffset = GlobalVars[VId-1];
2957-
if (globalVarOrOffset.isComplete())
2958-
return globalVarOrOffset;
2958+
if (globalVarOrOffset.isFullyDeserialized())
2959+
return globalVarOrOffset.get();
29592960

29602961
BCOffsetRAII restoreOffset(SILCursor);
2961-
if (llvm::Error Err = SILCursor.JumpToBit(globalVarOrOffset))
2962+
if (llvm::Error Err = SILCursor.JumpToBit(globalVarOrOffset.getOffset()))
29622963
MF->fatal(std::move(Err));
29632964
llvm::Expected<llvm::BitstreamEntry> maybeEntry =
29642965
SILCursor.advance(AF_DontPopBlockAtEnd);
@@ -3005,7 +3006,7 @@ SILGlobalVariable *SILDeserializer::readGlobalVar(StringRef Name) {
30053006
None,
30063007
dID ? cast<VarDecl>(MF->getDecl(dID)): nullptr);
30073008
v->setLet(IsLet);
3008-
globalVarOrOffset = v;
3009+
globalVarOrOffset.set(v, true /*isFullyDeserialized*/);
30093010
v->setDeclaration(IsDeclaration);
30103011

30113012
if (Callback)
@@ -3104,11 +3105,11 @@ SILVTable *SILDeserializer::readVTable(DeclID VId) {
31043105
assert(VId <= VTables.size() && "invalid VTable ID");
31053106
auto &vTableOrOffset = VTables[VId-1];
31063107

3107-
if (vTableOrOffset.isComplete())
3108-
return vTableOrOffset;
3108+
if (vTableOrOffset.isFullyDeserialized())
3109+
return vTableOrOffset.get();
31093110

31103111
BCOffsetRAII restoreOffset(SILCursor);
3111-
if (llvm::Error Err = SILCursor.JumpToBit(vTableOrOffset))
3112+
if (llvm::Error Err = SILCursor.JumpToBit(vTableOrOffset.getOffset()))
31123113
MF->fatal(std::move(Err));
31133114
llvm::Expected<llvm::BitstreamEntry> maybeEntry =
31143115
SILCursor.advance(AF_DontPopBlockAtEnd);
@@ -3205,7 +3206,7 @@ SILVTable *SILDeserializer::readVTable(DeclID VId) {
32053206
SILMod, theClass,
32063207
Serialized ? IsSerialized : IsNotSerialized,
32073208
vtableEntries);
3208-
vTableOrOffset = vT;
3209+
vTableOrOffset.set(vT, true /*isFullyDeserialized*/);
32093210

32103211
if (Callback) Callback->didDeserialize(MF->getAssociatedModule(), vT);
32113212
return vT;
@@ -3839,3 +3840,127 @@ bool SILDeserializer::invalidateFunction(SILFunction *F) {
38393840
}
38403841
return false;
38413842
}
3843+
3844+
// Invalidate all cached SILGlobalVariable.
3845+
void SILDeserializer::invalidateGlobalVariableCache() {
3846+
for (auto &entry : GlobalVars) {
3847+
if (entry.isDeserialized()) {
3848+
entry.reset();
3849+
}
3850+
}
3851+
}
3852+
3853+
// Invalidate a specific cached GlobalVariable.
3854+
bool SILDeserializer::invalidateGlobalVariable(SILGlobalVariable *gv) {
3855+
for (auto &entry : GlobalVars) {
3856+
if (entry.isDeserialized() && entry.get() == gv) {
3857+
entry.reset();
3858+
return true;
3859+
}
3860+
}
3861+
3862+
return false;
3863+
}
3864+
3865+
// Invalidate all cached SILVTable.
3866+
void SILDeserializer::invalidateVTableCache() {
3867+
for (auto &entry : VTables) {
3868+
if (entry.isDeserialized()) {
3869+
entry.reset();
3870+
}
3871+
}
3872+
}
3873+
3874+
// Invalidate a specific cached SILVTable.
3875+
bool SILDeserializer::invalidateVTable(SILVTable *v) {
3876+
for (auto &entry : VTables) {
3877+
if (entry.isDeserialized() && entry.get() == v) {
3878+
entry.reset();
3879+
return true;
3880+
}
3881+
}
3882+
3883+
return false;
3884+
}
3885+
3886+
// Invalidate all cached SILWitnessTable.
3887+
void SILDeserializer::invalidateWitnessTableCache() {
3888+
for (auto &entry : WitnessTables) {
3889+
if (entry.isDeserialized()) {
3890+
entry.reset();
3891+
}
3892+
}
3893+
}
3894+
3895+
// Invalidate a specific cached SILWitnessTable.
3896+
bool SILDeserializer::invalidateWitnessTable(SILWitnessTable *wt) {
3897+
for (auto &entry : WitnessTables) {
3898+
if (entry.isDeserialized() && entry.get() == wt) {
3899+
entry.reset();
3900+
return true;
3901+
}
3902+
}
3903+
return false;
3904+
}
3905+
3906+
// Invalidate all cached SILDefaultWitnessTable.
3907+
void SILDeserializer::invalidateDefaultWitnessTableCache() {
3908+
for (auto &entry : DefaultWitnessTables) {
3909+
if (entry.isDeserialized()) {
3910+
entry.reset();
3911+
}
3912+
}
3913+
}
3914+
3915+
// Invalidate a specific cached SILDefaultWitnessTable.
3916+
bool SILDeserializer::invalidateDefaultWitnessTable(
3917+
SILDefaultWitnessTable *wt) {
3918+
for (auto &entry : DefaultWitnessTables) {
3919+
if (entry.isDeserialized() && entry.get() == wt) {
3920+
entry.reset();
3921+
return true;
3922+
}
3923+
}
3924+
return false;
3925+
}
3926+
3927+
// Invalidate all cached SILProperty.
3928+
void SILDeserializer::invalidatePropertyCache() {
3929+
for (auto &entry : Properties) {
3930+
if (entry.isDeserialized()) {
3931+
entry.reset();
3932+
}
3933+
}
3934+
}
3935+
3936+
// Invalidate a specific cached SILProperty.
3937+
bool SILDeserializer::invalidateProperty(SILProperty *p) {
3938+
for (auto &entry : Properties) {
3939+
if (entry.isDeserialized() && entry.get() == p) {
3940+
entry.reset();
3941+
return true;
3942+
}
3943+
}
3944+
return false;
3945+
}
3946+
3947+
// Invalidate all cached SILDifferentiabilityWitness.
3948+
void SILDeserializer::invalidateDifferentiabilityWitnessCache() {
3949+
for (auto &entry : DifferentiabilityWitnesses) {
3950+
if (entry.isDeserialized()) {
3951+
entry.reset();
3952+
}
3953+
}
3954+
}
3955+
3956+
// Invalidate a specific cached SILDifferentiabilityWitness.
3957+
bool SILDeserializer::invalidateDifferentiabilityWitness(
3958+
SILDifferentiabilityWitness *w) {
3959+
for (auto &entry : DifferentiabilityWitnesses) {
3960+
if (entry.isDeserialized() && entry.get() == w) {
3961+
entry.reset();
3962+
return true;
3963+
}
3964+
}
3965+
return false;
3966+
}

lib/Serialization/DeserializeSIL.h

Lines changed: 67 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,24 @@ namespace swift {
3838
using SerializedFuncTable =
3939
llvm::OnDiskIterableChainedHashTable<FuncTableInfo>;
4040

41+
//-----
42+
// Deserialization Caches
43+
//
44+
// NOTE: When adding more serialized tables to the deserializer,
45+
// always add invalidate methods and make sure SILModule always
46+
// invalidates the deserializer appropriately when it erases a
47+
// value that we deserialized here. Otherwise, memory corruption
48+
// may result.
49+
4150
std::unique_ptr<SerializedFuncTable> FuncTable;
4251
MutableArrayRef<ModuleFile::PartiallySerialized<SILFunction*>> Funcs;
4352

44-
std::unique_ptr<SerializedFuncTable> VTableList;
45-
MutableArrayRef<ModuleFile::Serialized<SILVTable*>> VTables;
46-
4753
std::unique_ptr<SerializedFuncTable> GlobalVarList;
48-
MutableArrayRef<ModuleFile::Serialized<SILGlobalVariable*>> GlobalVars;
54+
MutableArrayRef<ModuleFile::PartiallySerialized<SILGlobalVariable *>>
55+
GlobalVars;
56+
57+
std::unique_ptr<SerializedFuncTable> VTableList;
58+
MutableArrayRef<ModuleFile::PartiallySerialized<SILVTable *>> VTables;
4959

5060
std::unique_ptr<SerializedFuncTable> WitnessTableList;
5161
MutableArrayRef<ModuleFile::PartiallySerialized<SILWitnessTable *>>
@@ -63,6 +73,12 @@ namespace swift {
6373
ModuleFile::PartiallySerialized<SILDifferentiabilityWitness *>>
6474
DifferentiabilityWitnesses;
6575

76+
//-----
77+
// End Deserialization Caches
78+
//
79+
// Before adding a new cache here, please read the comment at the
80+
// beginning of the deserialization cache section.
81+
6682
/// A declaration will only
6783
llvm::DenseMap<NormalProtocolConformance *, SILWitnessTable *>
6884
ConformanceToWitnessTableMap;
@@ -176,6 +192,53 @@ namespace swift {
176192
/// Invalidate a specific cached SILFunction.
177193
bool invalidateFunction(SILFunction *F);
178194

195+
/// Invalidate all cached SILGlobalVariable.
196+
void invalidateGlobalVariableCache();
197+
198+
/// Invalidate a specific cached GlobalVariable.
199+
bool invalidateGlobalVariable(SILGlobalVariable *gv);
200+
201+
/// Invalidate all cached SILVTable.
202+
void invalidateVTableCache();
203+
204+
/// Invalidate a specific cached SILVTable.
205+
bool invalidateVTable(SILVTable *v);
206+
207+
/// Invalidate all cached SILWitnessTable.
208+
void invalidateWitnessTableCache();
209+
210+
/// Invalidate a specific cached SILWitnessTable.
211+
bool invalidateWitnessTable(SILWitnessTable *v);
212+
213+
/// Invalidate all cached SILDefaultWitnessTable.
214+
void invalidateDefaultWitnessTableCache();
215+
216+
/// Invalidate a specific cached SILDefaultWitnessTable.
217+
bool invalidateDefaultWitnessTable(SILDefaultWitnessTable *v);
218+
219+
/// Invalidate all cached SILProperty.
220+
void invalidatePropertyCache();
221+
222+
/// Invalidate a specific cached SILProperty.
223+
bool invalidateProperty(SILProperty *v);
224+
225+
/// Invalidate all cached SILDifferentiabilityWitness.
226+
void invalidateDifferentiabilityWitnessCache();
227+
228+
/// Invalidate a specific cached SILDifferentiabilityWitness.
229+
bool invalidateDifferentiabilityWitness(SILDifferentiabilityWitness *v);
230+
231+
/// Invalidate all caches in this deserializer.
232+
void invalidateAllCaches() {
233+
invalidateFunctionCache();
234+
invalidateGlobalVariableCache();
235+
invalidateVTableCache();
236+
invalidateWitnessTableCache();
237+
invalidateDefaultWitnessTableCache();
238+
invalidatePropertyCache();
239+
invalidateDifferentiabilityWitnessCache();
240+
}
241+
179242
/// Deserialize all SILFunctions, VTables, WitnessTables, and
180243
/// DefaultWitnessTables inside the module, and add them to SILMod.
181244
///

0 commit comments

Comments
 (0)