Skip to content

Commit 8ab726c

Browse files
authored
Merge pull request #81361 from artemcm/DepScanFewerClangs
[Dependency Scanning][Refactor] Reduce the number of `ClangImporter` instances used by the scanner
2 parents 3b3d13c + 80c71fd commit 8ab726c

File tree

12 files changed

+357
-311
lines changed

12 files changed

+357
-311
lines changed

include/swift/AST/ModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ class ModuleLoader {
374374
getModuleDependencies(Identifier moduleName,
375375
StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
376376
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
377-
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
377+
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
378378
InterfaceSubContextDelegate &delegate,
379379
llvm::PrefixMapper *mapper = nullptr,
380380
bool isTestableImport = false) = 0;

include/swift/ClangImporter/ClangImporter.h

Lines changed: 12 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ namespace tooling {
6565
namespace dependencies {
6666
struct ModuleDeps;
6767
struct TranslationUnitDeps;
68+
enum class ModuleOutputKind;
6869
using ModuleDepsGraph = std::vector<ModuleDeps>;
6970
}
7071
}
@@ -211,8 +212,7 @@ class ClangImporter final : public ClangModuleLoader {
211212
bool ignoreClangTarget = false);
212213

213214
std::vector<std::string>
214-
getClangDepScanningInvocationArguments(ASTContext &ctx,
215-
std::optional<StringRef> sourceFileName = std::nullopt);
215+
getClangDepScanningInvocationArguments(ASTContext &ctx);
216216

217217
static std::unique_ptr<clang::CompilerInvocation>
218218
createClangInvocation(ClangImporter *importer,
@@ -499,54 +499,32 @@ class ClangImporter final : public ClangModuleLoader {
499499
void verifyAllModules() override;
500500

501501
using RemapPathCallback = llvm::function_ref<std::string(StringRef)>;
502-
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
502+
using LookupModuleOutputCallback =
503+
llvm::function_ref<std::string(const clang::tooling::dependencies::ModuleDeps &,
504+
clang::tooling::dependencies::ModuleOutputKind)>;
505+
506+
static llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
503507
bridgeClangModuleDependencies(
508+
const ASTContext &ctx,
504509
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
505510
clang::tooling::dependencies::ModuleDepsGraph &clangDependencies,
506511
StringRef moduleOutputPath, StringRef stableModuleOutputPath,
512+
LookupModuleOutputCallback LookupModuleOutput,
507513
RemapPathCallback remapPath = nullptr);
508514

509515
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
510516
getModuleDependencies(Identifier moduleName, StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
511517
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
512-
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
518+
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
513519
InterfaceSubContextDelegate &delegate,
514520
llvm::PrefixMapper *mapper,
515521
bool isTestableImport = false) override;
516522

517-
void recordBridgingHeaderOptions(
518-
ModuleDependencyInfo &MDI,
519-
const clang::tooling::dependencies::TranslationUnitDeps &deps);
520-
521-
void getBridgingHeaderOptions(
523+
static void getBridgingHeaderOptions(
524+
const ASTContext &ctx,
522525
const clang::tooling::dependencies::TranslationUnitDeps &deps,
523526
std::vector<std::string> &swiftArgs);
524527

525-
/// Query dependency information for header dependencies
526-
/// of a binary Swift module.
527-
///
528-
/// \param moduleID the name of the Swift module whose dependency
529-
/// information will be augmented with information about the given
530-
/// textual header inputs.
531-
///
532-
/// \param headerPath the path to the header to be scanned.
533-
///
534-
/// \param clangScanningTool The clang dependency scanner.
535-
///
536-
/// \param cache The module dependencies cache to update, with information
537-
/// about new Clang modules discovered along the way.
538-
///
539-
/// \returns \c true if an error occurred, \c false otherwise
540-
bool getHeaderDependencies(
541-
ModuleDependencyID moduleID, std::optional<StringRef> headerPath,
542-
std::optional<llvm::MemoryBufferRef> sourceBuffer,
543-
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
544-
ModuleDependenciesCache &cache,
545-
ModuleDependencyIDSetVector &headerClangModuleDependencies,
546-
std::vector<std::string> &headerFileInputs,
547-
std::vector<std::string> &bridgingHeaderCommandLine,
548-
std::optional<std::string> &includeTreeID);
549-
550528
clang::TargetInfo &getModuleAvailabilityTarget() const override;
551529
clang::ASTContext &getClangASTContext() const override;
552530
clang::Preprocessor &getClangPreprocessor() const override;

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,32 @@ class ModuleDependencyScanningWorker {
5050
StringRef sdkModuleOutputPath, llvm::PrefixMapper *prefixMapper,
5151
bool isTestableImport = false);
5252

53+
/// Query dependency information for header dependencies
54+
/// of a binary Swift module.
55+
///
56+
/// \param moduleID the name of the Swift module whose dependency
57+
/// information will be augmented with information about the given
58+
/// textual header inputs.
59+
///
60+
/// \param headerPath the path to the header to be scanned.
61+
///
62+
/// \param clangScanningTool The clang dependency scanner.
63+
///
64+
/// \param cache The module dependencies cache to update, with information
65+
/// about new Clang modules discovered along the way.
66+
///
67+
/// \returns \c true if an error occurred, \c false otherwise
68+
bool scanHeaderDependenciesOfSwiftModule(
69+
const ASTContext &ctx,
70+
ModuleDependencyID moduleID, std::optional<StringRef> headerPath,
71+
std::optional<llvm::MemoryBufferRef> sourceBuffer,
72+
ModuleDependenciesCache &cache,
73+
ModuleDependencyIDSetVector &headerClangModuleDependencies,
74+
std::vector<std::string> &headerFileInputs,
75+
std::vector<std::string> &bridgingHeaderCommandLine,
76+
std::optional<std::string> &includeTreeID);
77+
78+
5379
/// Store cache entry for include tree.
5480
llvm::Error
5581
createCacheKeyForEmbeddedHeader(std::string embeddedHeaderIncludeTree,
@@ -65,7 +91,17 @@ class ModuleDependencyScanningWorker {
6591
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
6692
// Swift and Clang module loaders acting as scanners.
6793
std::unique_ptr<ModuleInterfaceLoader> swiftScannerModuleLoader;
68-
std::unique_ptr<ClangImporter> clangScannerModuleLoader;
94+
95+
// Base command line invocation for clang scanner queries (both module and header)
96+
std::vector<std::string> clangScanningBaseCommandLineArgs;
97+
// Command line invocation for clang by-name module lookups
98+
std::vector<std::string> clangScanningModuleCommandLineArgs;
99+
// Clang-specific (-Xcc) command-line flags to include on
100+
// Swift module compilation commands
101+
std::vector<std::string> swiftModuleClangCC1CommandLineArgs;
102+
// Working directory for clang module lookup queries
103+
std::string clangScanningWorkingDirectoryPath;
104+
69105
// CAS instance.
70106
std::shared_ptr<llvm::cas::ObjectStore> CAS;
71107
std::shared_ptr<llvm::cas::ActionCache> ActionCache;

include/swift/Sema/SourceLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ class SourceLoader : public ModuleLoader {
101101
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
102102
getModuleDependencies(Identifier moduleName, StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
103103
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
104-
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
104+
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
105105
InterfaceSubContextDelegate &delegate,
106106
llvm::PrefixMapper *mapper,
107107
bool isTestableImport) override;

include/swift/Serialization/ScanningLoaders.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ class SwiftModuleScanner : public SerializedModuleLoaderBase {
4343
std::string moduleOutputPath;
4444
/// Location where pre-built SDK modules are to be built into.
4545
std::string sdkModuleOutputPath;
46+
/// Clang-specific (-Xcc) command-line flags to include on
47+
/// Swift module compilation commands
48+
std::vector<std::string> swiftModuleClangCC1CommandLineArgs;
4649

4750
public:
4851
std::optional<ModuleDependencyInfo> dependencies;
@@ -51,12 +54,14 @@ class SwiftModuleScanner : public SerializedModuleLoaderBase {
5154
Identifier moduleName,
5255
InterfaceSubContextDelegate &astDelegate,
5356
StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
57+
std::vector<std::string> swiftModuleClangCC1CommandLineArgs,
5458
ScannerKind kind = MDS_plain)
5559
: SerializedModuleLoaderBase(ctx, nullptr, LoadMode,
5660
/*IgnoreSwiftSourceInfoFile=*/true),
5761
kind(kind), moduleName(moduleName), astDelegate(astDelegate),
5862
moduleOutputPath(moduleOutputPath),
59-
sdkModuleOutputPath(sdkModuleOutputPath) {}
63+
sdkModuleOutputPath(sdkModuleOutputPath),
64+
swiftModuleClangCC1CommandLineArgs(swiftModuleClangCC1CommandLineArgs) {}
6065

6166
std::error_code findModuleFilesInDirectory(
6267
ImportPath::Element ModuleID, const SerializedModuleBaseName &BaseName,
@@ -100,9 +105,8 @@ class PlaceholderSwiftModuleScanner : public SwiftModuleScanner {
100105
StringRef moduleOutputPath,
101106
StringRef sdkModuleOutputPath)
102107
: SwiftModuleScanner(ctx, LoadMode, moduleName, astDelegate,
103-
moduleOutputPath, sdkModuleOutputPath,
108+
moduleOutputPath, sdkModuleOutputPath, {},
104109
MDS_placeholder) {
105-
106110
// FIXME: Find a better place for this map to live, to avoid
107111
// doing the parsing on every module.
108112
if (!PlaceholderDependencyModuleMap.empty()) {

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
266266
virtual llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
267267
getModuleDependencies(Identifier moduleName, StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
268268
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
269-
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
269+
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
270270
InterfaceSubContextDelegate &delegate,
271271
llvm::PrefixMapper *mapper,
272272
bool isTestableImport) override;

0 commit comments

Comments
 (0)