Skip to content

[Dependency Scanning][Refactor] Reduce the number of ClangImporter instances used by the scanner #81361

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 2 commits into from
May 12, 2025

Conversation

artemcm
Copy link
Contributor

@artemcm artemcm commented May 7, 2025

This is a refactoring PR that includes two changes meant to reduce the overall number of times that ClangImporter is instantiated during a given dependency scanning action:

  • Remove 'ClangImporter' instance from dependency scanning worker
    This change moves getModuleDependency and getHeaderDependency functionality from ClangImporter directly into Swift's ModuleDependencyScanningWorker using the clangScanningTool it already has, removing the need to instantiate an instance of ClangImporter per-worker.

  • Reduce number of module loaders instantiated for textual interface scanning sub-instance
    When creating a compilation sub-instance for scanning a textual Swift module interface for dependencies, in setupModuleLoaders only configure the strictly-necessary module loaders, skipping ClangImporter in particular.

On a large benchmark scan (e.g. importing every Swift module in the macOS SDK yielding ~270 Swift modules and ~530 Clang modules) this change results in a ~5% speedup.

@artemcm
Copy link
Contributor Author

artemcm commented May 7, 2025

@swift-ci test

@artemcm artemcm force-pushed the DepScanFewerClangs branch from 634bafc to 92632e6 Compare May 7, 2025 22:22
@artemcm
Copy link
Contributor Author

artemcm commented May 7, 2025

@swift-ci test

… scanning worker

Move relevant logic directly into the worker
@artemcm artemcm force-pushed the DepScanFewerClangs branch from 92632e6 to 5b13c94 Compare May 7, 2025 23:48

// When caching is enabled, we rely on ClangImporter for
// 'addClangInvovcationDependencies'
if (Invocation.getCASOptions().EnableCaching) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cc @cachemeifyoucan
This is due to:

auto clangImporter =

I imagine we ought to be able to factor that out to something that happens once at worker creation, too.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a feeling that I might be able to remove that function now. Let me check (Oh, there was a bad typo in function name)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is a draft: #81389

I need to do more clean up before commit that PR, will comment in that PR about background

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Could I get a review of this change in the meantime? :)
We can land #81389 as a follow-up.

@artemcm
Copy link
Contributor Author

artemcm commented May 7, 2025

@swift-ci test

@artemcm
Copy link
Contributor Author

artemcm commented May 8, 2025

@swift-ci test Linux platform

Copy link
Contributor

@cachemeifyoucan cachemeifyoucan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM with some nits.

Args.push_back("-Xcc");
Args.push_back("-fno-implicit-module-maps");
}
for (const auto &arg : swiftModuleClangCC1CommandLineArgs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: llvm::append_range(Args, swiftModuleClangCC1CommandLineArgs);

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat!

// with `-direct-clang-cc1-module-build`.
if (ScanASTContext.ClangImporterOpts.ClangImporterDirectCC1Scan) {
swiftModuleClangCC1CommandLineArgs.push_back("-direct-clang-cc1-module-build");
auto *importer =
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This importer is not different from scanClangImporter above right?

ModuleDependencyScanningWorker::ModuleDependencyScanningWorker(
SwiftDependencyScanningService &globalScanningService,
const CompilerInvocation &ScanCompilerInvocation,
const SILOptions &SILOptions, ASTContext &ScanASTContext,
swift::DependencyTracker &DependencyTracker, DiagnosticEngine &Diagnostics)
: clangScanningTool(*globalScanningService.ClangScanningService,
globalScanningService.getClangScanningFS()) {
globalScanningService.getClangScanningFS()),
swiftModuleClangCC1CommandLineArgs() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This default initialization is not needed.

@artemcm artemcm force-pushed the DepScanFewerClangs branch from 5b13c94 to 80c71fd Compare May 9, 2025 17:23
@artemcm
Copy link
Contributor Author

artemcm commented May 9, 2025

@swift-ci test

@artemcm artemcm merged commit 8ab726c into swiftlang:main May 12, 2025
5 checks passed
@artemcm artemcm deleted the DepScanFewerClangs branch May 12, 2025 16:57
hamishknight added a commit to hamishknight/swift that referenced this pull request May 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants