Skip to content

Commit 02b42e2

Browse files
authored
Merge pull request #35356 from artemcm/DependencyScannerUseCacheWhenAppropriate
[Dependency Scanning] Optionally, resolve direct dependencies of a module using only the `ModuleDependenciesCache`.
2 parents 399af3f + 8ecdfcb commit 02b42e2

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
@@ -1528,15 +1528,35 @@ void ASTContext::addModuleInterfaceChecker(
15281528

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

1537-
if (auto dependencies = loader->getModuleDependencies(moduleName, cache,
1538-
delegate))
1539-
return dependencies;
1556+
if (auto dependencies = loader->getModuleDependencies(moduleName, cache,
1557+
delegate))
1558+
return dependencies;
1559+
}
15401560
}
15411561

15421562
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 isSwiftInterface = knownDependencies.isSwiftTextualModule();
@@ -164,10 +165,8 @@ resolveDirectDependencies(CompilerInstance &instance, ModuleDependencyID module,
164165
for (auto dependsOn : knownDependencies.getModuleDependencies()) {
165166
// Figure out what kind of module we need.
166167
bool onlyClangModule = !isSwift || module.first == dependsOn;
167-
168-
// Retrieve the dependencies for this module.
169168
if (auto found = ctx.getModuleDependencies(dependsOn, onlyClangModule,
170-
cache, ASTDelegate)) {
169+
cache, ASTDelegate, cacheOnly)) {
171170
result.insert({dependsOn, found->getKind()});
172171
}
173172
}
@@ -210,7 +209,7 @@ resolveDirectDependencies(CompilerInstance &instance, ModuleDependencyID module,
210209
// directly depends on these.
211210
for (const auto &clangDep : allClangModules) {
212211
if (auto found = ctx.getModuleDependencies(
213-
clangDep, /*onlyClangModule=*/false, cache, ASTDelegate)) {
212+
clangDep, /*onlyClangModule=*/false, cache, ASTDelegate, cacheOnly)) {
214213
// ASTContext::getModuleDependencies returns dependencies for a module
215214
// with a given name. This Clang module may have the same name as the
216215
// Swift module we are resolving, so we need to make sure we don't add a
@@ -762,7 +761,7 @@ generateFullDependencyGraph(CompilerInstance &instance,
762761
// DirectDependencies
763762
auto directDependencies = resolveDirectDependencies(
764763
instance, ModuleDependencyID(module.first, module.second), cache,
765-
ASTDelegate);
764+
ASTDelegate, /*cacheOnly*/ true);
766765

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

0 commit comments

Comments
 (0)