Skip to content

Commit 8ecdfcb

Browse files
committed
[Dependency Scanning] Optionally, resolve direct dependencies of a module using only the ModuleDependenciesCache.
This speeds up contexts where we need to resolve dependencies after the main scanning action is complete. For example: libSwiftScan binary graph generation and cycle detection. This is necessary because ordinarily, ModuleDependenciesCache must be queried on a per-loader basis. For example, we first search for Swift modules with the SerializedModuleLoaderBase, at which point we check the cache for previously-found Swift modules. If cache is not hit, we search the filesystem. Then we search for Clang moduels with the ClangLoader, at which point we check the cache for previously-found Clang modules. If cache is not hit, we search the filesystem. This change allows to eliminate a bunch of filesystem searching in step (1) when we know we have completed the scan and no longer need to be concerned with the correctness of the cache contents.
1 parent 7e6536d commit 8ecdfcb

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

include/swift/AST/ASTContext.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -795,7 +795,8 @@ class ASTContext final {
795795
StringRef moduleName,
796796
bool isUnderlyingClangModule,
797797
ModuleDependenciesCache &cache,
798-
InterfaceSubContextDelegate &delegate);
798+
InterfaceSubContextDelegate &delegate,
799+
bool cacheOnly = false);
799800

800801
/// Retrieve the module dependencies for the Swift module with the given name.
801802
Optional<ModuleDependencies> getSwiftModuleDependencies(

lib/AST/ASTContext.cpp

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1533,15 +1533,35 @@ void ASTContext::addModuleInterfaceChecker(
15331533

15341534
Optional<ModuleDependencies> ASTContext::getModuleDependencies(
15351535
StringRef moduleName, bool isUnderlyingClangModule,
1536-
ModuleDependenciesCache &cache, InterfaceSubContextDelegate &delegate) {
1537-
for (auto &loader : getImpl().ModuleLoaders) {
1538-
if (isUnderlyingClangModule &&
1539-
loader.get() != getImpl().TheClangModuleLoader)
1540-
continue;
1536+
ModuleDependenciesCache &cache, InterfaceSubContextDelegate &delegate,
1537+
bool cacheOnly) {
1538+
// Retrieve the dependencies for this module.
1539+
if (cacheOnly) {
1540+
// Check whether we've cached this result.
1541+
if (!isUnderlyingClangModule) {
1542+
if (auto found = cache.findDependencies(moduleName,
1543+
ModuleDependenciesKind::SwiftTextual))
1544+
return found;
1545+
if (auto found = cache.findDependencies(moduleName,
1546+
ModuleDependenciesKind::SwiftTextual))
1547+
return found;
1548+
if (auto found = cache.findDependencies(moduleName,
1549+
ModuleDependenciesKind::SwiftPlaceholder))
1550+
return found;
1551+
}
1552+
if (auto found = cache.findDependencies(moduleName,
1553+
ModuleDependenciesKind::Clang))
1554+
return found;
1555+
} else {
1556+
for (auto &loader : getImpl().ModuleLoaders) {
1557+
if (isUnderlyingClangModule &&
1558+
loader.get() != getImpl().TheClangModuleLoader)
1559+
continue;
15411560

1542-
if (auto dependencies = loader->getModuleDependencies(moduleName, cache,
1543-
delegate))
1544-
return dependencies;
1561+
if (auto dependencies = loader->getModuleDependencies(moduleName, cache,
1562+
delegate))
1563+
return dependencies;
1564+
}
15451565
}
15461566

15471567
return None;

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ static void findAllImportedClangModules(ASTContext &ctx, StringRef moduleName,
153153
static std::vector<ModuleDependencyID>
154154
resolveDirectDependencies(CompilerInstance &instance, ModuleDependencyID module,
155155
ModuleDependenciesCache &cache,
156-
InterfaceSubContextDelegate &ASTDelegate) {
156+
InterfaceSubContextDelegate &ASTDelegate,
157+
bool cacheOnly = false) {
157158
auto &ctx = instance.getASTContext();
158159
auto knownDependencies = *cache.findDependencies(module.first, module.second);
159160
auto isSwift = knownDependencies.isSwiftTextualModule();
@@ -163,10 +164,8 @@ resolveDirectDependencies(CompilerInstance &instance, ModuleDependencyID module,
163164
for (auto dependsOn : knownDependencies.getModuleDependencies()) {
164165
// Figure out what kind of module we need.
165166
bool onlyClangModule = !isSwift || module.first == dependsOn;
166-
167-
// Retrieve the dependencies for this module.
168167
if (auto found = ctx.getModuleDependencies(dependsOn, onlyClangModule,
169-
cache, ASTDelegate)) {
168+
cache, ASTDelegate, cacheOnly)) {
170169
result.insert({dependsOn, found->getKind()});
171170
}
172171
}
@@ -209,7 +208,7 @@ resolveDirectDependencies(CompilerInstance &instance, ModuleDependencyID module,
209208
// directly depends on these.
210209
for (const auto &clangDep : allClangModules) {
211210
if (auto found = ctx.getModuleDependencies(
212-
clangDep, /*onlyClangModule=*/false, cache, ASTDelegate)) {
211+
clangDep, /*onlyClangModule=*/false, cache, ASTDelegate, cacheOnly)) {
213212
// ASTContext::getModuleDependencies returns dependencies for a module
214213
// with a given name. This Clang module may have the same name as the
215214
// Swift module we are resolving, so we need to make sure we don't add a
@@ -761,7 +760,7 @@ generateFullDependencyGraph(CompilerInstance &instance,
761760
// DirectDependencies
762761
auto directDependencies = resolveDirectDependencies(
763762
instance, ModuleDependencyID(module.first, module.second), cache,
764-
ASTDelegate);
763+
ASTDelegate, /*cacheOnly*/ true);
765764

766765
// Generate a swiftscan_clang_details_t object based on the dependency kind
767766
auto getModuleDetails = [&]() -> swiftscan_module_details_t {
@@ -871,7 +870,7 @@ static bool diagnoseCycle(CompilerInstance &instance,
871870
auto &lastOpen = openSet.back();
872871
auto beforeSize = openSet.size();
873872
for (auto dep :
874-
resolveDirectDependencies(instance, lastOpen, cache, astDelegate)) {
873+
resolveDirectDependencies(instance, lastOpen, cache, astDelegate, /*cacheOnly*/ true)) {
875874
if (closeSet.count(dep))
876875
continue;
877876
if (openSet.insert(dep)) {

0 commit comments

Comments
 (0)