15
15
#include " swift/AST/ModuleDependencies.h"
16
16
#include " swift/Frontend/ModuleInterfaceLoader.h"
17
17
#include " swift/Serialization/SerializedModuleLoader.h"
18
+ #include " llvm/Support/ThreadPool.h"
18
19
19
20
namespace swift {
20
21
class DependencyTracker ;
21
22
}
22
23
23
24
namespace swift {
24
25
26
+ // / A dependency scanning worker which performs filesystem lookup
27
+ // / of a named module dependency.
25
28
class ModuleDependencyScanningWorker {
26
- private:
27
- ModuleDependencyScanningWorker (SwiftDependencyScanningService &globalScanningService,
28
- const CompilerInvocation &ScanCompilerInvocation,
29
- const SILOptions &SILOptions,
30
- ASTContext &ScanASTContext,
31
- DependencyTracker &DependencyTracker,
32
- DiagnosticEngine &diags);
29
+ public:
30
+ ModuleDependencyScanningWorker (
31
+ SwiftDependencyScanningService &globalScanningService,
32
+ const CompilerInvocation &ScanCompilerInvocation,
33
+ const SILOptions &SILOptions, ASTContext &ScanASTContext,
34
+ DependencyTracker &DependencyTracker, DiagnosticEngine &diags);
33
35
36
+ private:
34
37
// / Retrieve the module dependencies for the module with the given name.
35
- llvm::Optional< const ModuleDependencyInfo *> scanForModuleDependency (
36
- StringRef moduleName, ModuleDependenciesCache &cache ,
37
- bool optionalDependencyLookup = false , bool isTestableImport = false ,
38
- llvm::Optional<ModuleDependencyID> dependencyOf = llvm::None );
38
+ ModuleDependencyVector
39
+ scanFilesystemForModuleDependency ( StringRef moduleName,
40
+ const ModuleDependenciesCache &cache ,
41
+ bool isTestableImport = false );
39
42
40
43
// / Retrieve the module dependencies for the Clang module with the given name.
41
- llvm::Optional< const ModuleDependencyInfo *>
42
- scanForClangModuleDependency (StringRef moduleName,
43
- ModuleDependenciesCache &cache);
44
+ ModuleDependencyVector
45
+ scanFilesystemForClangModuleDependency (StringRef moduleName,
46
+ const ModuleDependenciesCache &cache);
44
47
45
48
// / Retrieve the module dependencies for the Swift module with the given name.
46
- llvm::Optional<const ModuleDependencyInfo *>
47
- scanForSwiftModuleDependency (StringRef moduleName,
48
- ModuleDependenciesCache &cache);
49
-
49
+ ModuleDependencyVector
50
+ scanFilesystemForSwiftModuleDependency (StringRef moduleName,
51
+ const ModuleDependenciesCache &cache);
50
52
51
- private:
52
- DiagnosticEngine &Diagnostics;
53
-
54
- // An AST delegate for interface scanning
53
+ // An AST delegate for interface scanning.
55
54
std::unique_ptr<InterfaceSubContextDelegateImpl> ScanningASTDelegate;
56
- // Clang scanner tool
55
+ // The Clang scanner tool used by this worker.
57
56
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
58
-
59
- // Swift and Clang module loaders acting as the corresponding
60
- // scanners.
57
+ // Swift and Clang module loaders acting as scanners.
61
58
std::unique_ptr<ModuleInterfaceLoader> swiftScannerModuleLoader;
62
59
std::unique_ptr<ClangImporter> clangScannerModuleLoader;
63
-
64
- // Only the parent scanner can use this
60
+ // Restrict access to the parent scanner class.
65
61
friend class ModuleDependencyScanner ;
66
62
};
67
63
@@ -72,33 +68,59 @@ class ModuleDependencyScanner {
72
68
const SILOptions &SILOptions,
73
69
ASTContext &ScanASTContext,
74
70
DependencyTracker &DependencyTracker,
75
- DiagnosticEngine &diags);
71
+ DiagnosticEngine &diags, bool ParallelScan );
76
72
77
73
// / Identify the scanner invocation's main module's dependencies
78
74
llvm::ErrorOr<ModuleDependencyInfo> getMainModuleDependencyInfo (
79
75
ModuleDecl *mainModule,
80
76
llvm::Optional<SwiftDependencyTracker> tracker = llvm::None);
81
77
82
- // / Resolve module dependencies of the given module, direct and transitive.
78
+ // / Resolve module dependencies of the given module, computing a full
79
+ // / transitive closure dependency graph.
83
80
std::vector<ModuleDependencyID>
84
- resolveDependencies (ModuleDependencyID moduleID,
85
- ModuleDependenciesCache &cache);
81
+ getModuleDependencies (ModuleDependencyID moduleID,
82
+ ModuleDependenciesCache &cache);
86
83
87
- // / Retrieve the module dependencies for the Clang module with the given name.
84
+ // / Query the module dependency info for the Clang module with the given name.
85
+ // / Explicit by-name lookups are useful for batch mode scanning.
88
86
llvm::Optional<const ModuleDependencyInfo *>
89
- scanForClangModuleDependency (StringRef moduleName,
90
- ModuleDependenciesCache &cache);
87
+ getNamedClangModuleDependencyInfo (StringRef moduleName,
88
+ ModuleDependenciesCache &cache);
91
89
92
- // / Retrieve the module dependencies for the Swift module with the given name.
90
+ // / Query the module dependency info for the Swift module with the given name.
91
+ // / Explicit by-name lookups are useful for batch mode scanning.
93
92
llvm::Optional<const ModuleDependencyInfo *>
94
- scanForSwiftModuleDependency (StringRef moduleName,
95
- ModuleDependenciesCache &cache);
93
+ getNamedSwiftModuleDependencyInfo (StringRef moduleName,
94
+ ModuleDependenciesCache &cache);
96
95
97
96
private:
98
97
// / Resolve the direct dependencies of the given module.
99
98
std::vector<ModuleDependencyID>
100
- resolveModuleImports (ModuleDependencyID moduleID,
101
- ModuleDependenciesCache &cache);
99
+ resolveDirectModuleDependencies (ModuleDependencyID moduleID,
100
+ ModuleDependenciesCache &cache);
101
+
102
+ // / Resolve imported module names of a given module to concrete
103
+ // / modules. If `ParallelScan` is enabled, this operation is multithreaded.
104
+ void
105
+ resolveImportDependencies (const ModuleDependencyID &moduleID,
106
+ ModuleDependenciesCache &cache,
107
+ ModuleDependencyIDSetVector &directDependencies);
108
+
109
+ // / If a module has a bridging header, execute a dependency scan
110
+ // / on it and record the dependencies.
111
+ void resolveBridgingHeaderDependencies (
112
+ const ModuleDependencyID &moduleID, ModuleDependenciesCache &cache,
113
+ std::vector<std::string> &allClangModules,
114
+ llvm::StringSet<> &alreadyKnownModules,
115
+ ModuleDependencyIDSetVector &directDependencies);
116
+
117
+ // / Resolve all module dependencies comprised of Swift overlays
118
+ // / of this module's Clang module dependencies.
119
+ void resolveSwiftOverlayDependencies (
120
+ const ModuleDependencyID &moduleID,
121
+ const std::vector<std::string> &clangDependencies,
122
+ ModuleDependenciesCache &cache,
123
+ ModuleDependencyIDSetVector &swiftOverlayDependencies);
102
124
103
125
// / Identify all cross-import overlay modules of the specified
104
126
// / dependency set and apply an action for each.
@@ -107,13 +129,22 @@ class ModuleDependencyScanner {
107
129
ModuleDependenciesCache &cache,
108
130
llvm::function_ref<void (ModuleDependencyID)> action);
109
131
132
+ // / Perform an operation utilizing one of the Scanning workers
133
+ // / available to this scanner.
134
+ template <typename Function, typename ... Args>
135
+ auto withDependencyScanningWorker (Function &&F, Args &&...ArgList);
136
+
110
137
private:
111
138
const CompilerInvocation &ScanCompilerInvocation;
112
139
ASTContext &ScanASTContext;
113
140
DiagnosticEngine &Diagnostics;
114
141
115
- // For now, the sole worker doing the scanning
116
- ModuleDependencyScanningWorker ScanningWorker;
142
+ // / The available pool of workers for filesystem module search
143
+ unsigned NumThreads;
144
+ std::list<std::unique_ptr<ModuleDependencyScanningWorker>> Workers;
145
+ llvm::ThreadPool ScanningThreadPool;
146
+ // / Protect worker access.
147
+ std::mutex WorkersLock;
117
148
};
118
149
119
150
// / A module "loader" that looks for .swiftinterface and .swiftmodule files
0 commit comments