Skip to content

Commit 705b1a6

Browse files
authored
Merge pull request #82429 from artemcm/ScannerInvalidBinaryModuleRefactor
[Dependency Scanning] Refactor the scanner to simplify layering
2 parents 180693b + 39c096c commit 705b1a6

16 files changed

+568
-651
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 4 additions & 22 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<
@@ -1069,13 +1069,10 @@ class SwiftDependencyScanningService {
10691069

10701070
// MARK: ModuleDependenciesCache
10711071
/// This "local" dependencies cache persists only for the duration of a given
1072-
/// scanning action, and wraps an instance of a `SwiftDependencyScanningService`
1073-
/// which may carry cached scanning information from prior scanning actions.
1074-
/// This cache maintains a store of references to all dependencies found within
1075-
/// the current scanning action (with their values stored in the global Cache).
1072+
/// scanning action, and maintains a store of references to all dependencies
1073+
/// found within the current scanning action.
10761074
class ModuleDependenciesCache {
10771075
private:
1078-
SwiftDependencyScanningService &globalScanningService;
10791076
/// Discovered dependencies
10801077
ModuleDependenciesKindMap ModuleDependenciesMap;
10811078
/// Set containing all of the Clang modules that have already been seen.
@@ -1084,10 +1081,6 @@ class ModuleDependenciesCache {
10841081
std::string mainScanModuleName;
10851082
/// The context hash of the current scanning invocation
10861083
std::string scannerContextHash;
1087-
/// The location of where the explicitly-built modules will be output to
1088-
std::string moduleOutputPath;
1089-
/// The location of where the explicitly-built SDK modules will be output to
1090-
std::string sdkModuleOutputPath;
10911084
/// The timestamp of the beginning of the scanning query action
10921085
/// using this cache
10931086
const llvm::sys::TimePoint<> scanInitializationTime;
@@ -1100,10 +1093,7 @@ class ModuleDependenciesCache {
11001093
getDependencyReferencesMap(ModuleDependencyKind kind) const;
11011094

11021095
public:
1103-
ModuleDependenciesCache(SwiftDependencyScanningService &globalScanningService,
1104-
const std::string &mainScanModuleName,
1105-
const std::string &moduleOutputPath,
1106-
const std::string &sdkModuleOutputPath,
1096+
ModuleDependenciesCache(const std::string &mainScanModuleName,
11071097
const std::string &scanningContextHash);
11081098
ModuleDependenciesCache(const ModuleDependenciesCache &) = delete;
11091099
ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete;
@@ -1124,21 +1114,13 @@ class ModuleDependenciesCache {
11241114
/// Whether we have cached dependency information for the given Swift module.
11251115
bool hasSwiftDependency(StringRef moduleName) const;
11261116

1127-
SwiftDependencyScanningService &getScanService() {
1128-
return globalScanningService;
1129-
}
1130-
const SwiftDependencyScanningService &getScanService() const {
1131-
return globalScanningService;
1132-
}
11331117
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &
11341118
getAlreadySeenClangModules() const {
11351119
return alreadySeenClangModules;
11361120
}
11371121
void addSeenClangModule(clang::tooling::dependencies::ModuleID newModule) {
11381122
alreadySeenClangModules.insert(newModule);
11391123
}
1140-
StringRef getModuleOutputPath() const { return moduleOutputPath; }
1141-
StringRef getSDKModuleOutputPath() const { return sdkModuleOutputPath; }
11421124

11431125
/// Query all dependencies
11441126
ModuleDependencyIDSetVector

include/swift/AST/ModuleLoader.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,6 @@ class ModuleLoader {
367367
/// Discover overlays declared alongside this file and add information about
368368
/// them to it.
369369
void findOverlayFiles(SourceLoc diagLoc, ModuleDecl *module, FileUnit *file);
370-
371-
/// Retrieve the dependencies for the given, named module, or \c None
372-
/// if no such module exists.
373-
virtual llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
374-
getModuleDependencies(Identifier moduleName,
375-
StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
376-
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
377-
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
378-
InterfaceSubContextDelegate &delegate,
379-
llvm::PrefixMapper *mapper = nullptr,
380-
bool isTestableImport = false) = 0;
381370
};
382371

383372
} // namespace swift

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -497,24 +497,15 @@ class ClangImporter final : public ClangModuleLoader {
497497
using LookupModuleOutputCallback =
498498
llvm::function_ref<std::string(const clang::tooling::dependencies::ModuleDeps &,
499499
clang::tooling::dependencies::ModuleOutputKind)>;
500-
500+
501501
static llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
502502
bridgeClangModuleDependencies(
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

510-
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
511-
getModuleDependencies(Identifier moduleName, StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
512-
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
513-
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
514-
InterfaceSubContextDelegate &delegate,
515-
llvm::PrefixMapper *mapper,
516-
bool isTestableImport = false) override;
517-
518509
static void getBridgingHeaderOptions(
519510
const ASTContext &ctx,
520511
const clang::tooling::dependencies::TranslationUnitDeps &deps,

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#include "swift/AST/Identifier.h"
1616
#include "swift/AST/ModuleDependencies.h"
1717
#include "swift/Frontend/ModuleInterfaceLoader.h"
18-
#include "swift/Serialization/SerializedModuleLoader.h"
18+
#include "swift/Serialization/ScanningLoaders.h"
1919
#include "llvm/CAS/CASReference.h"
2020
#include "llvm/Support/ThreadPool.h"
2121

@@ -38,17 +38,13 @@ class ModuleDependencyScanningWorker {
3838
private:
3939
/// Retrieve the module dependencies for the Clang module with the given name.
4040
ModuleDependencyVector scanFilesystemForClangModuleDependency(
41-
Identifier moduleName, StringRef moduleOutputPath,
42-
StringRef sdkModuleOutputPath,
41+
Identifier moduleName,
4342
const llvm::DenseSet<clang::tooling::dependencies::ModuleID>
44-
&alreadySeenModules,
45-
llvm::PrefixMapper *prefixMapper);
43+
&alreadySeenModules);
4644

4745
/// Retrieve the module dependencies for the Swift module with the given name.
4846
ModuleDependencyVector scanFilesystemForSwiftModuleDependency(
49-
Identifier moduleName, StringRef moduleOutputPath,
50-
StringRef sdkModuleOutputPath, llvm::PrefixMapper *prefixMapper,
51-
bool isTestableImport = false);
47+
Identifier moduleName, bool isTestableImport = false);
5248

5349
/// Query dependency information for header dependencies
5450
/// of a binary Swift module.
@@ -90,7 +86,18 @@ class ModuleDependencyScanningWorker {
9086
// The Clang scanner tool used by this worker.
9187
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
9288
// Swift and Clang module loaders acting as scanners.
93-
std::unique_ptr<ModuleInterfaceLoader> swiftScannerModuleLoader;
89+
std::unique_ptr<SwiftModuleScanner> swiftModuleScannerLoader;
90+
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;
94101

95102
// Base command line invocation for clang scanner queries (both module and header)
96103
std::vector<std::string> clangScanningBaseCommandLineArgs;
@@ -101,10 +108,6 @@ class ModuleDependencyScanningWorker {
101108
std::vector<std::string> swiftModuleClangCC1CommandLineArgs;
102109
// Working directory for clang module lookup queries
103110
std::string clangScanningWorkingDirectoryPath;
104-
105-
// CAS instance.
106-
std::shared_ptr<llvm::cas::ObjectStore> CAS;
107-
std::shared_ptr<llvm::cas::ActionCache> ActionCache;
108111
// Restrict access to the parent scanner class.
109112
friend class ModuleDependencyScanner;
110113
};
@@ -219,6 +222,8 @@ class ModuleDependencyScanner {
219222
unsigned NumThreads;
220223
std::list<std::unique_ptr<ModuleDependencyScanningWorker>> Workers;
221224
llvm::DefaultThreadPool ScanningThreadPool;
225+
/// File prefix mapper.
226+
std::shared_ptr<llvm::PrefixMapper> PrefixMapper;
222227
/// Protect worker access.
223228
std::mutex WorkersLock;
224229
/// Count of filesystem queries performed

include/swift/DependencyScan/ScanDependencies.h

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,17 @@ 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,
62-
DependencyScanDiagnosticCollector *diagnostics,
63-
ModuleDependenciesCache &cache);
61+
performModuleScan(SwiftDependencyScanningService &service,
62+
CompilerInstance &instance,
63+
ModuleDependenciesCache &cache,
64+
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-
DependencyScanDiagnosticCollector *diagnostics,
69-
ModuleDependenciesCache &cache);
68+
performModulePrescan(SwiftDependencyScanningService &service,
69+
CompilerInstance &instance,
70+
ModuleDependenciesCache &cache,
71+
DependencyScanDiagnosticCollector *diagnostics = nullptr);
7072

7173
namespace incremental {
7274
/// For the given module dependency graph captured in the 'cache',
@@ -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);

include/swift/Sema/SourceLoader.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,6 @@ class SourceLoader : public ModuleLoader {
9797
{
9898
// Parsing populates the Objective-C method tables.
9999
}
100-
101-
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
102-
getModuleDependencies(Identifier moduleName, StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
103-
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
104-
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
105-
InterfaceSubContextDelegate &delegate,
106-
llvm::PrefixMapper *mapper,
107-
bool isTestableImport) override;
108100
};
109101
}
110102

include/swift/Serialization/ScanningLoaders.h

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,35 @@ namespace swift {
2222
/// for the purpose of determining dependencies, but does not attempt to
2323
/// load the module files.
2424
class SwiftModuleScanner : public SerializedModuleLoaderBase {
25-
public:
26-
enum ScannerKind { MDS_plain, MDS_placeholder };
27-
2825
private:
29-
/// The kind of scanner this is (LLVM-style RTTI)
30-
const ScannerKind kind;
31-
32-
/// The module we're scanning dependencies of.
33-
Identifier moduleName;
34-
3526
/// Scan the given interface file to determine dependencies.
3627
llvm::ErrorOr<ModuleDependencyInfo>
37-
scanInterfaceFile(Twine moduleInterfacePath, bool isFramework,
38-
bool isTestableImport);
28+
scanInterfaceFile(Identifier moduleID, Twine moduleInterfacePath,
29+
bool isFramework, bool isTestableImport);
3930

40-
InterfaceSubContextDelegate &astDelegate;
31+
/// Scan the given serialized module file to determine dependencies.
32+
llvm::ErrorOr<ModuleDependencyInfo>
33+
scanBinaryModuleFile(Identifier moduleID, Twine binaryModulePath,
34+
bool isFramework, bool isTestableImport,
35+
bool isCandidateForTextualModule);
36+
37+
std::error_code findModuleFilesInDirectory(
38+
ImportPath::Element ModuleID, const SerializedModuleBaseName &BaseName,
39+
SmallVectorImpl<char> *ModuleInterfacePath,
40+
SmallVectorImpl<char> *ModuleInterfaceSourcePath,
41+
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
42+
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
43+
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
44+
bool SkipBuildingInterface, bool IsFramework,
45+
bool IsTestableDependencyLookup) override;
46+
47+
virtual void collectVisibleTopLevelModuleNames(
48+
SmallVectorImpl<Identifier> &names) const override {
49+
llvm_unreachable("Not used");
50+
}
4151

52+
/// AST delegate to be used for textual interface scanning
53+
InterfaceSubContextDelegate &astDelegate;
4254
/// Location where pre-built modules are to be built into.
4355
std::string moduleOutputPath;
4456
/// Location where pre-built SDK modules are to be built into.
@@ -51,37 +63,19 @@ class SwiftModuleScanner : public SerializedModuleLoaderBase {
5163
std::optional<ModuleDependencyInfo> dependencies;
5264

5365
SwiftModuleScanner(ASTContext &ctx, ModuleLoadingMode LoadMode,
54-
Identifier moduleName,
5566
InterfaceSubContextDelegate &astDelegate,
5667
StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
57-
std::vector<std::string> swiftModuleClangCC1CommandLineArgs,
58-
ScannerKind kind = MDS_plain)
68+
std::vector<std::string> swiftModuleClangCC1CommandLineArgs)
5969
: SerializedModuleLoaderBase(ctx, nullptr, LoadMode,
6070
/*IgnoreSwiftSourceInfoFile=*/true),
61-
kind(kind), moduleName(moduleName), astDelegate(astDelegate),
71+
astDelegate(astDelegate),
6272
moduleOutputPath(moduleOutputPath),
6373
sdkModuleOutputPath(sdkModuleOutputPath),
6474
swiftModuleClangCC1CommandLineArgs(swiftModuleClangCC1CommandLineArgs) {}
6575

66-
std::error_code findModuleFilesInDirectory(
67-
ImportPath::Element ModuleID, const SerializedModuleBaseName &BaseName,
68-
SmallVectorImpl<char> *ModuleInterfacePath,
69-
SmallVectorImpl<char> *ModuleInterfaceSourcePath,
70-
std::unique_ptr<llvm::MemoryBuffer> *ModuleBuffer,
71-
std::unique_ptr<llvm::MemoryBuffer> *ModuleDocBuffer,
72-
std::unique_ptr<llvm::MemoryBuffer> *ModuleSourceInfoBuffer,
73-
bool SkipBuildingInterface, bool IsFramework,
74-
bool IsTestableDependencyLookup) override;
75-
76-
virtual void collectVisibleTopLevelModuleNames(
77-
SmallVectorImpl<Identifier> &names) const override {
78-
llvm_unreachable("Not used");
79-
}
80-
81-
ScannerKind getKind() const { return kind; }
82-
static bool classof(const SwiftModuleScanner *MDS) {
83-
return MDS->getKind() == MDS_plain;
84-
}
76+
/// Perform a filesystem search for a Swift module with a given name
77+
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
78+
lookupSwiftModule(Identifier moduleName, bool isTestableImport);
8579
};
8680
} // namespace swift
8781

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -164,11 +164,6 @@ class SerializedModuleLoaderBase : public ModuleLoader {
164164
return false;
165165
}
166166

167-
/// Scan the given serialized module file to determine dependencies.
168-
llvm::ErrorOr<ModuleDependencyInfo>
169-
scanModuleFile(Twine modulePath, bool isFramework,
170-
bool isTestableImport, bool isCandidateForTextualModule);
171-
172167
struct BinaryModuleImports {
173168
std::vector<ScannerImportStatementInfo> moduleImports;
174169
std::string headerImport;
@@ -262,14 +257,6 @@ class SerializedModuleLoaderBase : public ModuleLoader {
262257

263258
virtual void verifyAllModules() override;
264259

265-
virtual llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
266-
getModuleDependencies(Identifier moduleName, StringRef moduleOutputPath, StringRef sdkModuleOutputPath,
267-
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
268-
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
269-
InterfaceSubContextDelegate &delegate,
270-
llvm::PrefixMapper *mapper,
271-
bool isTestableImport) override;
272-
273260
/// A textual reason why the compiler rejected a binary module load
274261
/// attempt with a given status, to be used for diagnostic output.
275262
static std::optional<std::string> invalidModuleReason(serialization::Status status);

lib/AST/ModuleDependencies.cpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -782,14 +782,9 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
782782
}
783783

784784
ModuleDependenciesCache::ModuleDependenciesCache(
785-
SwiftDependencyScanningService &globalScanningService,
786-
const std::string &mainScanModuleName, const std::string &moduleOutputPath,
787-
const std::string &sdkModuleOutputPath, const std::string &scannerContextHash)
788-
: globalScanningService(globalScanningService),
789-
mainScanModuleName(mainScanModuleName),
785+
const std::string &mainScanModuleName, const std::string &scannerContextHash)
786+
: mainScanModuleName(mainScanModuleName),
790787
scannerContextHash(scannerContextHash),
791-
moduleOutputPath(moduleOutputPath),
792-
sdkModuleOutputPath(sdkModuleOutputPath),
793788
scanInitializationTime(std::chrono::system_clock::now()) {
794789
for (auto kind = ModuleDependencyKind::FirstKind;
795790
kind != ModuleDependencyKind::LastKind; ++kind)

0 commit comments

Comments
 (0)