Skip to content

[Dependency Scanning] Enable parallel dependency scanning by-default #72854

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion include/swift/DependencyScan/ModuleDependencyScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,12 @@ class ModuleDependencyScanningWorker {
scanFilesystemForSwiftModuleDependency(Identifier moduleName,
const ModuleDependenciesCache &cache);

// Worker-specific instance of CompilerInvocation
std::unique_ptr<CompilerInvocation> workerCompilerInvocation;
// Worker-specific instance of ASTContext
std::unique_ptr<ASTContext> workerASTContext;
// An AST delegate for interface scanning.
std::unique_ptr<InterfaceSubContextDelegateImpl> ScanningASTDelegate;
std::unique_ptr<InterfaceSubContextDelegateImpl> scanningASTDelegate;
// The Clang scanner tool used by this worker.
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
// Swift and Clang module loaders acting as scanners.
Expand Down
2 changes: 1 addition & 1 deletion include/swift/Frontend/FrontendOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ class FrontendOptions {

/// Whether the dependency scanner invocation should resolve imports
/// to filesystem modules in parallel.
bool ParallelDependencyScan = false;
bool ParallelDependencyScan = true;

/// When performing an incremental build, ensure that cross-module incremental
/// build metadata is available in any swift modules emitted by this frontend
Expand Down
2 changes: 2 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,8 @@ def dependency_scan_cache_remarks : Flag<["-"], "Rdependency-scan-cache">,

def parallel_scan : Flag<["-"], "parallel-scan">,
HelpText<"Perform dependency scanning in-parallel.">;
def no_parallel_scan : Flag<["-"], "-no-parallel-scan">,
HelpText<"Perform dependency scanning in a single-threaded fashion.">;

def enable_copy_propagation : Flag<["-"], "enable-copy-propagation">,
HelpText<"Run SIL copy propagation with lexical lifetimes to shorten object "
Expand Down
48 changes: 29 additions & 19 deletions lib/DependencyScan/ModuleDependencyScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,38 +166,48 @@ ModuleDependencyScanningWorker::ModuleDependencyScanningWorker(
const CompilerInvocation &ScanCompilerInvocation,
const SILOptions &SILOptions, ASTContext &ScanASTContext,
swift::DependencyTracker &DependencyTracker, DiagnosticEngine &Diagnostics)
: // Diagnostics(Diagnostics),
clangScanningTool(*globalScanningService.ClangScanningService,
: clangScanningTool(*globalScanningService.ClangScanningService,
globalScanningService.getClangScanningFS()) {
// Configure the interface scanning AST delegate
// Create a scanner-specific Invocation and ASTContext.
workerCompilerInvocation =
std::make_unique<CompilerInvocation>(ScanCompilerInvocation);
workerASTContext = std::unique_ptr<ASTContext>(
ASTContext::get(workerCompilerInvocation->getLangOptions(),
workerCompilerInvocation->getTypeCheckerOptions(),
workerCompilerInvocation->getSILOptions(),
workerCompilerInvocation->getSearchPathOptions(),
workerCompilerInvocation->getClangImporterOptions(),
workerCompilerInvocation->getSymbolGraphOptions(),
workerCompilerInvocation->getCASOptions(),
ScanASTContext.SourceMgr, Diagnostics));

// Configure the interface scanning AST delegate.
auto ClangModuleCachePath = getModuleCachePathFromClang(
ScanASTContext.getClangModuleLoader()->getClangInstance());
auto &FEOpts = ScanCompilerInvocation.getFrontendOptions();
ModuleInterfaceLoaderOptions LoaderOpts(FEOpts);
ScanningASTDelegate = std::make_unique<InterfaceSubContextDelegateImpl>(
ScanASTContext.SourceMgr, &ScanASTContext.Diags,
ScanASTContext.SearchPathOpts, ScanASTContext.LangOpts,
ScanASTContext.ClangImporterOpts, ScanASTContext.CASOpts, LoaderOpts,
auto &FEOpts = workerCompilerInvocation->getFrontendOptions();
scanningASTDelegate = std::make_unique<InterfaceSubContextDelegateImpl>(
workerASTContext->SourceMgr, &workerASTContext->Diags,
workerASTContext->SearchPathOpts, workerASTContext->LangOpts,
workerASTContext->ClangImporterOpts, workerASTContext->CASOpts, FEOpts,
/*buildModuleCacheDirIfAbsent*/ false, ClangModuleCachePath,
FEOpts.PrebuiltModuleCachePath, FEOpts.BackupModuleInterfaceDir,
FEOpts.SerializeModuleInterfaceDependencyHashes,
FEOpts.shouldTrackSystemDependencies(), RequireOSSAModules_t(SILOptions));

// Set up the Clang importer.
// Set up the ClangImporter.
clangScannerModuleLoader = ClangImporter::create(
ScanASTContext, ScanCompilerInvocation.getPCHHash(), &DependencyTracker);
*workerASTContext, workerCompilerInvocation->getPCHHash(),
&DependencyTracker);
if (!clangScannerModuleLoader)
Diagnostics.diagnose(SourceLoc(), diag::error_clang_importer_create_fail);

// Set up the Swift interface loader for Swift scanning.
swiftScannerModuleLoader = ModuleInterfaceLoader::create(
ScanASTContext,
*workerASTContext,
*static_cast<ModuleInterfaceCheckerImpl *>(
ScanASTContext.getModuleInterfaceChecker()),
&DependencyTracker,
ScanCompilerInvocation.getSearchPathOptions().ModuleLoadMode);

llvm::cl::ResetAllOptionOccurrences();
workerCompilerInvocation->getSearchPathOptions().ModuleLoadMode);
}

ModuleDependencyVector
Expand All @@ -210,15 +220,15 @@ ModuleDependencyScanningWorker::scanFilesystemForModuleDependency(
moduleName, cache.getModuleOutputPath(),
cache.getScanService().getCachingFS(),
cache.getAlreadySeenClangModules(), clangScanningTool,
*ScanningASTDelegate, cache.getScanService().getPrefixMapper(),
*scanningASTDelegate, cache.getScanService().getPrefixMapper(),
isTestableImport);

if (moduleDependencies.empty())
moduleDependencies = clangScannerModuleLoader->getModuleDependencies(
moduleName, cache.getModuleOutputPath(),
cache.getScanService().getCachingFS(),
cache.getAlreadySeenClangModules(), clangScanningTool,
*ScanningASTDelegate, cache.getScanService().getPrefixMapper(),
*scanningASTDelegate, cache.getScanService().getPrefixMapper(),
isTestableImport);

return moduleDependencies;
Expand All @@ -230,7 +240,7 @@ ModuleDependencyScanningWorker::scanFilesystemForSwiftModuleDependency(
return swiftScannerModuleLoader->getModuleDependencies(
moduleName, cache.getModuleOutputPath(),
cache.getScanService().getCachingFS(), cache.getAlreadySeenClangModules(),
clangScanningTool, *ScanningASTDelegate,
clangScanningTool, *scanningASTDelegate,
cache.getScanService().getPrefixMapper(), false);
}

Expand All @@ -240,7 +250,7 @@ ModuleDependencyScanningWorker::scanFilesystemForClangModuleDependency(
return clangScannerModuleLoader->getModuleDependencies(
moduleName, cache.getModuleOutputPath(),
cache.getScanService().getCachingFS(), cache.getAlreadySeenClangModules(),
clangScanningTool, *ScanningASTDelegate,
clangScanningTool, *scanningASTDelegate,
cache.getScanService().getPrefixMapper(), false);
}

Expand Down
4 changes: 3 additions & 1 deletion lib/Frontend/ArgsToFrontendOptionsConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,9 @@ bool ArgsToFrontendOptionsConverter::convert(
Opts.SerializeDependencyScannerCache |= Args.hasArg(OPT_serialize_dependency_scan_cache);
Opts.ReuseDependencyScannerCache |= Args.hasArg(OPT_reuse_dependency_scan_cache);
Opts.EmitDependencyScannerCacheRemarks |= Args.hasArg(OPT_dependency_scan_cache_remarks);
Opts.ParallelDependencyScan |= Args.hasArg(OPT_parallel_scan);
Opts.ParallelDependencyScan = Args.hasArg(OPT_parallel_scan,
OPT_no_parallel_scan,
true);
if (const Arg *A = Args.getLastArg(OPT_dependency_scan_cache_path)) {
Opts.SerializedDependencyScannerCachePath = A->getValue();
}
Expand Down