Skip to content

Commit 80c71fd

Browse files
committed
[Dependency Scanning] Reduce number of module loaders instantiated for textual interface scanning sub-instance
1 parent 8cd193f commit 80c71fd

File tree

3 files changed

+47
-15
lines changed

3 files changed

+47
-15
lines changed

lib/DependencyScan/ModuleDependencyScanner.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -267,8 +267,7 @@ ModuleDependencyScanningWorker::ModuleDependencyScanningWorker(
267267
const SILOptions &SILOptions, ASTContext &ScanASTContext,
268268
swift::DependencyTracker &DependencyTracker, DiagnosticEngine &Diagnostics)
269269
: clangScanningTool(*globalScanningService.ClangScanningService,
270-
globalScanningService.getClangScanningFS()),
271-
swiftModuleClangCC1CommandLineArgs() {
270+
globalScanningService.getClangScanningFS()) {
272271
// Create a scanner-specific Invocation and ASTContext.
273272
workerCompilerInvocation =
274273
std::make_unique<CompilerInvocation>(ScanCompilerInvocation);
@@ -320,9 +319,7 @@ ModuleDependencyScanningWorker::ModuleDependencyScanningWorker(
320319
// with `-direct-clang-cc1-module-build`.
321320
if (ScanASTContext.ClangImporterOpts.ClangImporterDirectCC1Scan) {
322321
swiftModuleClangCC1CommandLineArgs.push_back("-direct-clang-cc1-module-build");
323-
auto *importer =
324-
static_cast<ClangImporter *>(ScanASTContext.getClangModuleLoader());
325-
for (auto &Arg : importer->getSwiftExplicitModuleDirectCC1Args()) {
322+
for (auto &Arg : scanClangImporter->getSwiftExplicitModuleDirectCC1Args()) {
326323
swiftModuleClangCC1CommandLineArgs.push_back("-Xcc");
327324
swiftModuleClangCC1CommandLineArgs.push_back(Arg);
328325
}

lib/Frontend/Frontend.cpp

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -756,16 +756,55 @@ void CompilerInstance::setUpDiagnosticOptions() {
756756
// in the presence of overlays and mixed-source frameworks, we want to prefer
757757
// the overlay or framework module over the underlying Clang module.
758758
bool CompilerInstance::setUpModuleLoaders() {
759+
auto ModuleCachePathFromInvocation = getInvocation().getClangModuleCachePath();
760+
auto &FEOpts = Invocation.getFrontendOptions();
761+
auto MLM = Invocation.getSearchPathOptions().ModuleLoadMode;
762+
auto IgnoreSourceInfoFile = Invocation.getFrontendOptions().IgnoreSwiftSourceInfo;
763+
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
764+
765+
// Dependency scanning sub-invocations only require the loaders necessary
766+
// for locating textual and binary Swift modules.
767+
if (Invocation.getFrontendOptions().DependencyScanningSubInvocation) {
768+
Context->addModuleInterfaceChecker(
769+
std::make_unique<ModuleInterfaceCheckerImpl>(
770+
*Context, ModuleCachePathFromInvocation, FEOpts.PrebuiltModuleCachePath,
771+
FEOpts.BackupModuleInterfaceDir, LoaderOpts,
772+
RequireOSSAModules_t(Invocation.getSILOptions())));
773+
774+
if (MLM != ModuleLoadingMode::OnlySerialized) {
775+
// We only need ModuleInterfaceLoader for implicit modules.
776+
auto PIML = ModuleInterfaceLoader::create(
777+
*Context, *static_cast<ModuleInterfaceCheckerImpl*>(Context
778+
->getModuleInterfaceChecker()), getDependencyTracker(), MLM,
779+
FEOpts.PreferInterfaceForModules, IgnoreSourceInfoFile);
780+
Context->addModuleLoader(std::move(PIML), false, false, true);
781+
}
782+
std::unique_ptr<ImplicitSerializedModuleLoader> ISML =
783+
ImplicitSerializedModuleLoader::create(*Context, getDependencyTracker(), MLM,
784+
IgnoreSourceInfoFile);
785+
this->DefaultSerializedLoader = ISML.get();
786+
Context->addModuleLoader(std::move(ISML));
787+
788+
// When caching is enabled, we rely on ClangImporter for
789+
// 'addClangInvovcationDependencies'
790+
if (Invocation.getCASOptions().EnableCaching) {
791+
std::unique_ptr<ClangImporter> clangImporter =
792+
ClangImporter::create(*Context, Invocation.getPCHHash(),
793+
getDependencyTracker());
794+
Context->addModuleLoader(std::move(clangImporter), /*isClang*/ true);
795+
}
796+
797+
return false;
798+
}
799+
759800
if (hasSourceImport()) {
760801
bool enableLibraryEvolution =
761802
Invocation.getFrontendOptions().EnableLibraryEvolution;
762803
Context->addModuleLoader(SourceLoader::create(*Context,
763804
enableLibraryEvolution,
764805
getDependencyTracker()));
765806
}
766-
auto MLM = Invocation.getSearchPathOptions().ModuleLoadMode;
767-
auto IgnoreSourceInfoFile =
768-
Invocation.getFrontendOptions().IgnoreSwiftSourceInfo;
807+
769808
if (Invocation.getLangOptions().EnableMemoryBufferImporter) {
770809
auto MemoryBufferLoader = MemoryBufferSerializedModuleLoader::create(
771810
*Context, getDependencyTracker(), MLM, IgnoreSourceInfoFile);
@@ -808,13 +847,10 @@ bool CompilerInstance::setUpModuleLoaders() {
808847
}
809848

810849
// Configure ModuleInterfaceChecker for the ASTContext.
811-
auto CacheFromInvocation = getInvocation().getClangModuleCachePath();
812850
auto const &Clang = clangImporter->getClangInstance();
813-
std::string ModuleCachePath = CacheFromInvocation.empty()
851+
std::string ModuleCachePath = ModuleCachePathFromInvocation.empty()
814852
? getModuleCachePathFromClang(Clang)
815-
: CacheFromInvocation.str();
816-
auto &FEOpts = Invocation.getFrontendOptions();
817-
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
853+
: ModuleCachePathFromInvocation.str();
818854
Context->addModuleInterfaceChecker(
819855
std::make_unique<ModuleInterfaceCheckerImpl>(
820856
*Context, ModuleCachePath, FEOpts.PrebuiltModuleCachePath,

lib/Serialization/ScanningLoaders.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,8 +187,7 @@ SwiftModuleScanner::scanInterfaceFile(Twine moduleInterfacePath,
187187

188188
// Handle clang arguments. For caching build, all arguments are passed
189189
// with `-direct-clang-cc1-module-build`.
190-
for (const auto &arg : swiftModuleClangCC1CommandLineArgs)
191-
Args.push_back(arg);
190+
llvm::append_range(Args, swiftModuleClangCC1CommandLineArgs);
192191

193192
for (const auto &candidate : compiledCandidates) {
194193
Args.push_back("-candidate-module-file");

0 commit comments

Comments
 (0)