Skip to content

Commit 39c096c

Browse files
committed
[Dependency Scanning] Refactor 'ModuleDependenciesCache' to not hold a reference to the global 'SwiftDependencyScanningService'
While this made sense in the distant past where the scanning service provided backing storage for the dependency cache, it no longer does so and now makes for awkard layering where clients get at the service via the cache. Now the cache is a simple data structure while all the clients that need access to the scanning service will get it explicitly.
1 parent 68883a1 commit 39c096c

File tree

9 files changed

+149
-140
lines changed

9 files changed

+149
-140
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -992,7 +992,7 @@ class SwiftDependencyScanningService {
992992
std::shared_ptr<llvm::cas::ActionCache> ActionCache;
993993

994994
/// File prefix mapper.
995-
std::unique_ptr<llvm::PrefixMapper> Mapper;
995+
std::shared_ptr<llvm::PrefixMapper> Mapper;
996996

997997
/// The global file system cache.
998998
std::optional<
@@ -1073,7 +1073,6 @@ class SwiftDependencyScanningService {
10731073
/// found within the current scanning action.
10741074
class ModuleDependenciesCache {
10751075
private:
1076-
SwiftDependencyScanningService &globalScanningService;
10771076
/// Discovered dependencies
10781077
ModuleDependenciesKindMap ModuleDependenciesMap;
10791078
/// Set containing all of the Clang modules that have already been seen.
@@ -1094,8 +1093,7 @@ class ModuleDependenciesCache {
10941093
getDependencyReferencesMap(ModuleDependencyKind kind) const;
10951094

10961095
public:
1097-
ModuleDependenciesCache(SwiftDependencyScanningService &globalScanningService,
1098-
const std::string &mainScanModuleName,
1096+
ModuleDependenciesCache(const std::string &mainScanModuleName,
10991097
const std::string &scanningContextHash);
11001098
ModuleDependenciesCache(const ModuleDependenciesCache &) = delete;
11011099
ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete;
@@ -1116,12 +1114,6 @@ class ModuleDependenciesCache {
11161114
/// Whether we have cached dependency information for the given Swift module.
11171115
bool hasSwiftDependency(StringRef moduleName) const;
11181116

1119-
SwiftDependencyScanningService &getScanService() {
1120-
return globalScanningService;
1121-
}
1122-
const SwiftDependencyScanningService &getScanService() const {
1123-
return globalScanningService;
1124-
}
11251117
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &
11261118
getAlreadySeenClangModules() const {
11271119
return alreadySeenClangModules;

include/swift/ClangImporter/ClangImporter.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,7 +503,6 @@ class ClangImporter final : public ClangModuleLoader {
503503
const ASTContext &ctx,
504504
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
505505
clang::tooling::dependencies::ModuleDepsGraph &clangDependencies,
506-
StringRef moduleOutputPath, StringRef stableModuleOutputPath,
507506
LookupModuleOutputCallback LookupModuleOutput,
508507
RemapPathCallback remapPath = nullptr);
509508

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,7 @@ class ModuleDependencyScanningWorker {
4040
ModuleDependencyVector scanFilesystemForClangModuleDependency(
4141
Identifier moduleName,
4242
const llvm::DenseSet<clang::tooling::dependencies::ModuleID>
43-
&alreadySeenModules,
44-
llvm::PrefixMapper *prefixMapper);
43+
&alreadySeenModules);
4544

4645
/// Retrieve the module dependencies for the Swift module with the given name.
4746
ModuleDependencyVector scanFilesystemForSwiftModuleDependency(
@@ -89,6 +88,17 @@ class ModuleDependencyScanningWorker {
8988
// Swift and Clang module loaders acting as scanners.
9089
std::unique_ptr<SwiftModuleScanner> swiftModuleScannerLoader;
9190

91+
/// The location of where the explicitly-built modules will be output to
92+
std::string moduleOutputPath;
93+
/// The location of where the explicitly-built SDK modules will be output to
94+
std::string sdkModuleOutputPath;
95+
96+
// CAS instance.
97+
std::shared_ptr<llvm::cas::ObjectStore> CAS;
98+
std::shared_ptr<llvm::cas::ActionCache> ActionCache;
99+
/// File prefix mapper.
100+
std::shared_ptr<llvm::PrefixMapper> PrefixMapper;
101+
92102
// Base command line invocation for clang scanner queries (both module and header)
93103
std::vector<std::string> clangScanningBaseCommandLineArgs;
94104
// Command line invocation for clang by-name module lookups
@@ -98,15 +108,6 @@ class ModuleDependencyScanningWorker {
98108
std::vector<std::string> swiftModuleClangCC1CommandLineArgs;
99109
// Working directory for clang module lookup queries
100110
std::string clangScanningWorkingDirectoryPath;
101-
102-
/// The location of where the explicitly-built modules will be output to
103-
std::string moduleOutputPath;
104-
/// The location of where the explicitly-built SDK modules will be output to
105-
std::string sdkModuleOutputPath;
106-
107-
// CAS instance.
108-
std::shared_ptr<llvm::cas::ObjectStore> CAS;
109-
std::shared_ptr<llvm::cas::ActionCache> ActionCache;
110111
// Restrict access to the parent scanner class.
111112
friend class ModuleDependencyScanner;
112113
};
@@ -221,6 +222,8 @@ class ModuleDependencyScanner {
221222
unsigned NumThreads;
222223
std::list<std::unique_ptr<ModuleDependencyScanningWorker>> Workers;
223224
llvm::DefaultThreadPool ScanningThreadPool;
225+
/// File prefix mapper.
226+
std::shared_ptr<llvm::PrefixMapper> PrefixMapper;
224227
/// Protect worker access.
225228
std::mutex WorkersLock;
226229
/// Count of filesystem queries performed

include/swift/DependencyScan/ScanDependencies.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,15 @@ bool prescanDependencies(CompilerInstance &instance);
5858
// MARK: Dependency scanning execution
5959
/// Scans the dependencies of the main module of \c instance.
6060
llvm::ErrorOr<swiftscan_dependency_graph_t>
61-
performModuleScan(CompilerInstance &instance,
61+
performModuleScan(SwiftDependencyScanningService &service,
62+
CompilerInstance &instance,
6263
ModuleDependenciesCache &cache,
6364
DependencyScanDiagnosticCollector *diagnostics = nullptr);
6465

6566
/// Scans the main module of \c instance for all direct module imports
6667
llvm::ErrorOr<swiftscan_import_set_t>
67-
performModulePrescan(CompilerInstance &instance,
68+
performModulePrescan(SwiftDependencyScanningService &service,
69+
CompilerInstance &instance,
6870
ModuleDependenciesCache &cache,
6971
DependencyScanDiagnosticCollector *diagnostics = nullptr);
7072

@@ -75,6 +77,7 @@ namespace incremental {
7577
/// than the serialized dependency graph, it is considered invalidated and must
7678
/// be re-scanned.
7779
void validateInterModuleDependenciesCache(
80+
const SwiftDependencyScanningService &service,
7881
const ModuleDependencyID &rootModuleID, ModuleDependenciesCache &cache,
7982
const llvm::sys::TimePoint<> &cacheTimeStamp, llvm::vfs::FileSystem &fs,
8083
DiagnosticEngine &diags, bool emitRemarks = false);
@@ -83,7 +86,8 @@ void validateInterModuleDependenciesCache(
8386
/// with respect to their inputs. Upon encountering such a module, add it to the
8487
/// set of invalidated modules, along with the path from the root to this
8588
/// module.
86-
void outOfDateModuleScan(const ModuleDependencyID &sourceModuleID,
89+
void outOfDateModuleScan(const SwiftDependencyScanningService &service,
90+
const ModuleDependencyID &sourceModuleID,
8791
const ModuleDependenciesCache &cache,
8892
const llvm::sys::TimePoint<> &cacheTimeStamp,
8993
llvm::vfs::FileSystem &fs, DiagnosticEngine &diags,
@@ -93,6 +97,7 @@ void outOfDateModuleScan(const ModuleDependencyID &sourceModuleID,
9397
/// Validate whether all inputs of a given module dependency
9498
/// are older than the cache serialization time.
9599
bool verifyModuleDependencyUpToDate(
100+
const SwiftDependencyScanningService &service,
96101
const ModuleDependencyID &moduleID, const ModuleDependenciesCache &cache,
97102
const llvm::sys::TimePoint<> &cacheTimeStamp, llvm::vfs::FileSystem &fs,
98103
DiagnosticEngine &diags, bool emitRemarks);

lib/AST/ModuleDependencies.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -781,10 +781,8 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
781781
}
782782

783783
ModuleDependenciesCache::ModuleDependenciesCache(
784-
SwiftDependencyScanningService &globalScanningService,
785784
const std::string &mainScanModuleName, const std::string &scannerContextHash)
786-
: globalScanningService(globalScanningService),
787-
mainScanModuleName(mainScanModuleName),
785+
: mainScanModuleName(mainScanModuleName),
788786
scannerContextHash(scannerContextHash),
789787
scanInitializationTime(std::chrono::system_clock::now()) {
790788
for (auto kind = ModuleDependencyKind::FirstKind;

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,6 @@ ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies(
8585
const ASTContext &ctx,
8686
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
8787
clang::tooling::dependencies::ModuleDepsGraph &clangDependencies,
88-
StringRef moduleOutputPath, StringRef stableModuleOutputPath,
8988
LookupModuleOutputCallback lookupModuleOutput,
9089
RemapPathCallback callback) {
9190
ModuleDependencyVector result;

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,10 +288,11 @@ DependencyScanningTool::getDependencies(
288288

289289
// Local scan cache instance, wrapping the shared global cache.
290290
ModuleDependenciesCache cache(
291-
*ScanningService, QueryContext.ScanInstance->getMainModule()->getNameStr().str(),
291+
QueryContext.ScanInstance->getMainModule()->getNameStr().str(),
292292
QueryContext.ScanInstance->getInvocation().getModuleScanningHash());
293293
// Execute the scanning action, retrieving the in-memory result
294-
auto DependenciesOrErr = performModuleScan(*QueryContext.ScanInstance.get(),
294+
auto DependenciesOrErr = performModuleScan(*ScanningService,
295+
*QueryContext.ScanInstance.get(),
295296
cache,
296297
QueryContext.ScanDiagnostics.get());
297298
if (DependenciesOrErr.getError())
@@ -317,9 +318,10 @@ DependencyScanningTool::getImports(ArrayRef<const char *> Command,
317318

318319
// Local scan cache instance, wrapping the shared global cache.
319320
ModuleDependenciesCache cache(
320-
*ScanningService, QueryContext.ScanInstance->getMainModule()->getNameStr().str(),
321+
QueryContext.ScanInstance->getMainModule()->getNameStr().str(),
321322
QueryContext.ScanInstance->getInvocation().getModuleScanningHash());
322-
auto DependenciesOrErr = performModulePrescan(*QueryContext.ScanInstance.get(),
323+
auto DependenciesOrErr = performModulePrescan(*ScanningService,
324+
*QueryContext.ScanInstance.get(),
323325
cache,
324326
QueryContext.ScanDiagnostics.get());
325327
if (DependenciesOrErr.getError())

0 commit comments

Comments
 (0)