Skip to content

Commit 01e65e1

Browse files
committed
SIL: Share TypeConverter between SILModules in batch mode
1 parent d434188 commit 01e65e1

File tree

16 files changed

+71
-37
lines changed

16 files changed

+71
-37
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ class SerializedModuleLoader;
5555
class MemoryBufferSerializedModuleLoader;
5656
class SILModule;
5757

58+
namespace Lowering {
59+
class TypeConverter;
60+
}
61+
5862
/// The abstract configuration of the compiler, including:
5963
/// - options for all stages of translation,
6064
/// - information about the build environment,
@@ -372,6 +376,7 @@ class CompilerInstance {
372376
SourceManager SourceMgr;
373377
DiagnosticEngine Diagnostics{SourceMgr};
374378
std::unique_ptr<ASTContext> Context;
379+
std::unique_ptr<Lowering::TypeConverter> TheSILTypes;
375380
std::unique_ptr<SILModule> TheSILModule;
376381

377382
std::unique_ptr<PersistentParserState> PersistentState;
@@ -422,8 +427,6 @@ class CompilerInstance {
422427

423428
bool isWholeModuleCompilation() { return PrimaryBufferIDs.empty(); }
424429

425-
void createSILModule();
426-
427430
public:
428431
// Out of line to avoid having to import SILModule.h.
429432
CompilerInstance();
@@ -448,6 +451,10 @@ class CompilerInstance {
448451
SILOptions &getSILOptions() { return Invocation.getSILOptions(); }
449452
const SILOptions &getSILOptions() const { return Invocation.getSILOptions(); }
450453

454+
Lowering::TypeConverter &getSILTypes();
455+
456+
void createSILModule();
457+
451458
void addDiagnosticConsumer(DiagnosticConsumer *DC) {
452459
Diagnostics.addConsumer(*DC);
453460
}

include/swift/SIL/SILModule.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,8 @@ class SILModule {
260260

261261
// Intentionally marked private so that we need to use 'constructSIL()'
262262
// to construct a SILModule.
263-
SILModule(ModuleDecl *M, SILOptions &Options, const DeclContext *associatedDC,
263+
SILModule(ModuleDecl *M, Lowering::TypeConverter &TC,
264+
SILOptions &Options, const DeclContext *associatedDC,
264265
bool wholeModule);
265266

266267
SILModule(const SILModule&) = delete;
@@ -313,7 +314,7 @@ class SILModule {
313314
void serialize();
314315

315316
/// This converts Swift types to SILTypes.
316-
mutable Lowering::TypeConverter Types;
317+
Lowering::TypeConverter &Types;
317318

318319
/// Invalidate cached entries in SIL Loader.
319320
void invalidateSILLoaderCaches();
@@ -340,12 +341,14 @@ class SILModule {
340341
/// If a source file is provided, SIL will only be emitted for decls in that
341342
/// source file.
342343
static std::unique_ptr<SILModule>
343-
constructSIL(ModuleDecl *M, SILOptions &Options, FileUnit *sf = nullptr);
344+
constructSIL(ModuleDecl *M, Lowering::TypeConverter &TC,
345+
SILOptions &Options, FileUnit *sf = nullptr);
344346

345347
/// Create and return an empty SIL module that we can
346348
/// later parse SIL bodies directly into, without converting from an AST.
347349
static std::unique_ptr<SILModule>
348-
createEmptyModule(ModuleDecl *M, SILOptions &Options,
350+
createEmptyModule(ModuleDecl *M, Lowering::TypeConverter &TC,
351+
SILOptions &Options,
349352
bool WholeModule = false);
350353

351354
/// Get the Swift module associated with this SIL module.

include/swift/Subsystems.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ namespace swift {
6969
class UnifiedStatsReporter;
7070
enum class SourceFileKind;
7171

72+
namespace Lowering {
73+
class TypeConverter;
74+
}
75+
7276
/// Used to optionally maintain SIL parsing context for the parser.
7377
///
7478
/// When not parsing SIL, this has no overhead.
@@ -254,11 +258,13 @@ namespace swift {
254258
/// The module must contain source files. The optimizer will assume that the
255259
/// SIL of all files in the module is present in the SILModule.
256260
std::unique_ptr<SILModule>
257-
performSILGeneration(ModuleDecl *M, SILOptions &options);
261+
performSILGeneration(ModuleDecl *M, Lowering::TypeConverter &TC,
262+
SILOptions &options);
258263

259264
/// Turn a source file into SIL IR.
260265
std::unique_ptr<SILModule>
261-
performSILGeneration(FileUnit &SF, SILOptions &options);
266+
performSILGeneration(FileUnit &SF, Lowering::TypeConverter &TC,
267+
SILOptions &options);
262268

263269
using ModuleOrSourceFile = PointerUnion<ModuleDecl *, SourceFile *>;
264270

lib/Frontend/Frontend.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,20 @@ SerializationOptions CompilerInvocation::computeSerializationOptions(
148148
return serializationOpts;
149149
}
150150

151+
Lowering::TypeConverter &CompilerInstance::getSILTypes() {
152+
if (auto *tc = TheSILTypes.get())
153+
return *tc;
154+
155+
auto *tc = new Lowering::TypeConverter(*getMainModule());
156+
TheSILTypes.reset(tc);
157+
return *tc;
158+
}
159+
151160
void CompilerInstance::createSILModule() {
152161
assert(MainModule && "main module not created yet");
153162
// Assume WMO if a -primary-file option was not provided.
154163
TheSILModule = SILModule::createEmptyModule(
155-
getMainModule(), Invocation.getSILOptions(),
164+
getMainModule(), getSILTypes(), Invocation.getSILOptions(),
156165
Invocation.getFrontendOptions().InputsAndOutputs.isWholeModule());
157166
}
158167

@@ -1074,6 +1083,7 @@ void CompilerInstance::performParseOnly(bool EvaluateConditionals,
10741083

10751084
void CompilerInstance::freeASTContext() {
10761085
PersistentState.reset();
1086+
TheSILTypes.reset();
10771087
Context.reset();
10781088
MainModule = nullptr;
10791089
SML = nullptr;

lib/Frontend/ParseableInterfaceModuleLoader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -592,7 +592,8 @@ class swift::ParseableInterfaceBuilder {
592592

593593
SILOptions &SILOpts = subInvocation.getSILOptions();
594594
auto Mod = SubInstance.getMainModule();
595-
auto SILMod = performSILGeneration(Mod, SILOpts);
595+
auto &TC = SubInstance.getSILTypes();
596+
auto SILMod = performSILGeneration(Mod, TC, SILOpts);
596597
if (!SILMod) {
597598
LLVM_DEBUG(llvm::dbgs() << "SILGen did not produce a module\n");
598599
SubError = true;

lib/FrontendTool/FrontendTool.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ generateSILModules(CompilerInvocation &Invocation, CompilerInstance &Instance) {
917917
if (!opts.InputsAndOutputs.hasPrimaryInputs()) {
918918
// If there are no primary inputs the compiler is in WMO mode and builds one
919919
// SILModule for the entire module.
920-
auto SM = performSILGeneration(mod, SILOpts);
920+
auto SM = performSILGeneration(mod, Instance.getSILTypes(), SILOpts);
921921
std::deque<PostSILGenInputs> PSGIs;
922922
const PrimarySpecificPaths PSPs =
923923
Instance.getPrimarySpecificPathsForWholeModuleOptimizationMode();
@@ -930,7 +930,7 @@ generateSILModules(CompilerInvocation &Invocation, CompilerInstance &Instance) {
930930
// once for each such input.
931931
std::deque<PostSILGenInputs> PSGIs;
932932
for (auto *PrimaryFile : Instance.getPrimarySourceFiles()) {
933-
auto SM = performSILGeneration(*PrimaryFile, SILOpts);
933+
auto SM = performSILGeneration(*PrimaryFile, Instance.getSILTypes(), SILOpts);
934934
const PrimarySpecificPaths PSPs =
935935
Instance.getPrimarySpecificPathsForSourceFile(*PrimaryFile);
936936
PSGIs.push_back(PostSILGenInputs{std::move(SM), true, PrimaryFile, PSPs});
@@ -944,7 +944,7 @@ generateSILModules(CompilerInvocation &Invocation, CompilerInstance &Instance) {
944944
if (Invocation.getFrontendOptions().InputsAndOutputs.isInputPrimary(
945945
SASTF->getFilename())) {
946946
assert(PSGIs.empty() && "Can only handle one primary AST input");
947-
auto SM = performSILGeneration(*SASTF, SILOpts);
947+
auto SM = performSILGeneration(*SASTF, Instance.getSILTypes(), SILOpts);
948948
const PrimarySpecificPaths &PSPs =
949949
Instance.getPrimarySpecificPathsForPrimary(SASTF->getFilename());
950950
PSGIs.push_back(

lib/Immediate/REPL.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -874,7 +874,8 @@ class REPLEnvironment {
874874
if (!CI.getASTContext().hadError()) {
875875
// We don't want anything to get stripped, so pretend we're doing a
876876
// non-whole-module generation.
877-
sil = performSILGeneration(*M->getFiles().front(), CI.getSILOptions());
877+
sil = performSILGeneration(*M->getFiles().front(), CI.getSILTypes(),
878+
CI.getSILOptions());
878879
runSILDiagnosticPasses(*sil);
879880
runSILOwnershipEliminatorPass(*sil);
880881
runSILLoweringPasses(*sil);

lib/RemoteAST/RemoteAST.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ namespace {
6161
struct IRGenContext {
6262
IRGenOptions IROpts;
6363
SILOptions SILOpts;
64+
Lowering::TypeConverter TC;
6465
std::unique_ptr<SILModule> SILMod;
6566
llvm::LLVMContext LLVMContext;
6667
irgen::IRGenerator IRGen;
@@ -69,7 +70,8 @@ struct IRGenContext {
6970
private:
7071
IRGenContext(ASTContext &ctx, ModuleDecl *module)
7172
: IROpts(createIRGenOptions()),
72-
SILMod(SILModule::createEmptyModule(module, SILOpts)),
73+
TC(*module),
74+
SILMod(SILModule::createEmptyModule(module, TC, SILOpts)),
7375
IRGen(IROpts, *SILMod),
7476
IGM(IRGen, IRGen.createTargetMachine(), LLVMContext) {}
7577

lib/SIL/SILModule.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,13 @@ class SILModule::SerializationCallback final
8989
}
9090
};
9191

92-
SILModule::SILModule(ModuleDecl *SwiftModule, SILOptions &Options,
93-
const DeclContext *associatedDC, bool wholeModule)
94-
: TheSwiftModule(SwiftModule), AssociatedDeclContext(associatedDC),
92+
SILModule::SILModule(ModuleDecl *SwiftModule, TypeConverter &TC,
93+
SILOptions &Options, const DeclContext *associatedDC,
94+
bool wholeModule)
95+
: TheSwiftModule(SwiftModule),
96+
AssociatedDeclContext(associatedDC),
9597
Stage(SILStage::Raw), wholeModule(wholeModule), Options(Options),
96-
serialized(false), SerializeSILAction(), Types(*SwiftModule) {
98+
serialized(false), SerializeSILAction(), Types(TC) {
9799
// We always add the base SILModule serialization callback.
98100
std::unique_ptr<DeserializationNotificationHandler> callback(
99101
new SILModule::SerializationCallback());
@@ -118,10 +120,10 @@ SILModule::~SILModule() {
118120
}
119121

120122
std::unique_ptr<SILModule>
121-
SILModule::createEmptyModule(ModuleDecl *M, SILOptions &Options,
123+
SILModule::createEmptyModule(ModuleDecl *M, TypeConverter &TC, SILOptions &Options,
122124
bool WholeModule) {
123125
return std::unique_ptr<SILModule>(
124-
new SILModule(M, Options, M, WholeModule));
126+
new SILModule(M, TC, Options, M, WholeModule));
125127
}
126128

127129
ASTContext &SILModule::getASTContext() const {

lib/SILGen/SILGen.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,7 +1662,8 @@ void SILGenModule::emitSourceFile(SourceFile *sf) {
16621662
//===----------------------------------------------------------------------===//
16631663

16641664
std::unique_ptr<SILModule>
1665-
SILModule::constructSIL(ModuleDecl *mod, SILOptions &options, FileUnit *SF) {
1665+
SILModule::constructSIL(ModuleDecl *mod, TypeConverter &tc,
1666+
SILOptions &options, FileUnit *SF) {
16661667
SharedTimer timer("SILGen");
16671668
const DeclContext *DC;
16681669
if (SF) {
@@ -1672,7 +1673,7 @@ SILModule::constructSIL(ModuleDecl *mod, SILOptions &options, FileUnit *SF) {
16721673
}
16731674

16741675
std::unique_ptr<SILModule> M(
1675-
new SILModule(mod, options, DC, /*wholeModule*/ SF == nullptr));
1676+
new SILModule(mod, tc, options, DC, /*wholeModule*/ SF == nullptr));
16761677
SILGenModule SGM(*M, mod);
16771678

16781679
if (SF) {
@@ -1721,11 +1722,13 @@ SILModule::constructSIL(ModuleDecl *mod, SILOptions &options, FileUnit *SF) {
17211722
}
17221723

17231724
std::unique_ptr<SILModule>
1724-
swift::performSILGeneration(ModuleDecl *mod, SILOptions &options) {
1725-
return SILModule::constructSIL(mod, options, nullptr);
1725+
swift::performSILGeneration(ModuleDecl *mod, Lowering::TypeConverter &tc,
1726+
SILOptions &options) {
1727+
return SILModule::constructSIL(mod, tc, options, nullptr);
17261728
}
17271729

17281730
std::unique_ptr<SILModule>
1729-
swift::performSILGeneration(FileUnit &sf, SILOptions &options) {
1730-
return SILModule::constructSIL(sf.getParentModule(), options, &sf);
1731+
swift::performSILGeneration(FileUnit &sf, Lowering::TypeConverter &tc,
1732+
SILOptions &options) {
1733+
return SILModule::constructSIL(sf.getParentModule(), tc, options, &sf);
17311734
}

0 commit comments

Comments
 (0)