Skip to content

Commit 68883a1

Browse files
committed
[Dependency Scanning] Refactor Swift Scanner loader to be standalone
- 'SwiftModuleScanner' will now be owned directly by the 'ModuleDependencyScanningWorker' and will contain all the necessary custom logic, instead of being instantiated by the module interface loader for each query - Moves ownership over module output path and sdk module output path directly into the scanning worker, instead of the cache
1 parent 5eb85ac commit 68883a1

16 files changed

+445
-537
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1069,10 +1069,8 @@ 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:
10781076
SwiftDependencyScanningService &globalScanningService;
@@ -1084,10 +1082,6 @@ class ModuleDependenciesCache {
10841082
std::string mainScanModuleName;
10851083
/// The context hash of the current scanning invocation
10861084
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;
10911085
/// The timestamp of the beginning of the scanning query action
10921086
/// using this cache
10931087
const llvm::sys::TimePoint<> scanInitializationTime;
@@ -1102,8 +1096,6 @@ class ModuleDependenciesCache {
11021096
public:
11031097
ModuleDependenciesCache(SwiftDependencyScanningService &globalScanningService,
11041098
const std::string &mainScanModuleName,
1105-
const std::string &moduleOutputPath,
1106-
const std::string &sdkModuleOutputPath,
11071099
const std::string &scanningContextHash);
11081100
ModuleDependenciesCache(const ModuleDependenciesCache &) = delete;
11091101
ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete;
@@ -1137,8 +1129,6 @@ class ModuleDependenciesCache {
11371129
void addSeenClangModule(clang::tooling::dependencies::ModuleID newModule) {
11381130
alreadySeenClangModules.insert(newModule);
11391131
}
1140-
StringRef getModuleOutputPath() const { return moduleOutputPath; }
1141-
StringRef getSDKModuleOutputPath() const { return sdkModuleOutputPath; }
11421132

11431133
/// Query all dependencies
11441134
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 & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ 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,
@@ -507,14 +507,6 @@ class ClangImporter final : public ClangModuleLoader {
507507
LookupModuleOutputCallback LookupModuleOutput,
508508
RemapPathCallback remapPath = nullptr);
509509

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-
518510
static void getBridgingHeaderOptions(
519511
const ASTContext &ctx,
520512
const clang::tooling::dependencies::TranslationUnitDeps &deps,

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 9 additions & 7 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,14 @@ 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>
4443
&alreadySeenModules,
4544
llvm::PrefixMapper *prefixMapper);
4645

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

5350
/// Query dependency information for header dependencies
5451
/// of a binary Swift module.
@@ -90,7 +87,7 @@ class ModuleDependencyScanningWorker {
9087
// The Clang scanner tool used by this worker.
9188
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
9289
// Swift and Clang module loaders acting as scanners.
93-
std::unique_ptr<ModuleInterfaceLoader> swiftScannerModuleLoader;
90+
std::unique_ptr<SwiftModuleScanner> swiftModuleScannerLoader;
9491

9592
// Base command line invocation for clang scanner queries (both module and header)
9693
std::vector<std::string> clangScanningBaseCommandLineArgs;
@@ -102,6 +99,11 @@ class ModuleDependencyScanningWorker {
10299
// Working directory for clang module lookup queries
103100
std::string clangScanningWorkingDirectoryPath;
104101

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+
105107
// CAS instance.
106108
std::shared_ptr<llvm::cas::ObjectStore> CAS;
107109
std::shared_ptr<llvm::cas::ActionCache> ActionCache;

include/swift/DependencyScan/ScanDependencies.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ bool prescanDependencies(CompilerInstance &instance);
5959
/// Scans the dependencies of the main module of \c instance.
6060
llvm::ErrorOr<swiftscan_dependency_graph_t>
6161
performModuleScan(CompilerInstance &instance,
62-
DependencyScanDiagnosticCollector *diagnostics,
63-
ModuleDependenciesCache &cache);
62+
ModuleDependenciesCache &cache,
63+
DependencyScanDiagnosticCollector *diagnostics = nullptr);
6464

6565
/// Scans the main module of \c instance for all direct module imports
6666
llvm::ErrorOr<swiftscan_import_set_t>
6767
performModulePrescan(CompilerInstance &instance,
68-
DependencyScanDiagnosticCollector *diagnostics,
69-
ModuleDependenciesCache &cache);
68+
ModuleDependenciesCache &cache,
69+
DependencyScanDiagnosticCollector *diagnostics = nullptr);
7070

7171
namespace incremental {
7272
/// For the given module dependency graph captured in the 'cache',

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: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -782,13 +782,10 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
782782

783783
ModuleDependenciesCache::ModuleDependenciesCache(
784784
SwiftDependencyScanningService &globalScanningService,
785-
const std::string &mainScanModuleName, const std::string &moduleOutputPath,
786-
const std::string &sdkModuleOutputPath, const std::string &scannerContextHash)
785+
const std::string &mainScanModuleName, const std::string &scannerContextHash)
787786
: globalScanningService(globalScanningService),
788787
mainScanModuleName(mainScanModuleName),
789788
scannerContextHash(scannerContextHash),
790-
moduleOutputPath(moduleOutputPath),
791-
sdkModuleOutputPath(sdkModuleOutputPath),
792789
scanInitializationTime(std::chrono::system_clock::now()) {
793790
for (auto kind = ModuleDependencyKind::FirstKind;
794791
kind != ModuleDependencyKind::LastKind; ++kind)

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -267,15 +267,3 @@ void ClangImporter::getBridgingHeaderOptions(
267267
swiftArgs.push_back(*Tree);
268268
}
269269
}
270-
271-
ModuleDependencyVector
272-
ClangImporter::getModuleDependencies(Identifier moduleName,
273-
StringRef moduleOutputPath,
274-
StringRef sdkModuleOutputPath,
275-
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
276-
const std::vector<std::string> &swiftModuleClangCC1CommandLineArgs,
277-
InterfaceSubContextDelegate &delegate,
278-
llvm::PrefixMapper *mapper,
279-
bool isTestableImport) {
280-
return {};
281-
}

0 commit comments

Comments
 (0)