Skip to content

Commit 3e3be76

Browse files
authored
Merge pull request #34646 from artemcm/BatchScanShouldShareCache
[Dependency Scanner] Share `ModuleDependenciesCache` as a part of the CompilerInstance
2 parents 9a67f53 + 10b6b9d commit 3e3be76

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

include/swift/Frontend/Frontend.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,6 +416,10 @@ class CompilerInstance {
416416
std::unique_ptr<Lowering::TypeConverter> TheSILTypes;
417417
std::unique_ptr<DiagnosticVerifier> DiagVerifier;
418418

419+
/// A cache describing the set of inter-module dependencies that have been queried.
420+
/// Null if not present.
421+
std::unique_ptr<ModuleDependenciesCache> ModDepCache;
422+
419423
/// Null if no tracker.
420424
std::unique_ptr<DependencyTracker> DepTracker;
421425
/// If there is no stats output directory by the time the
@@ -487,6 +491,8 @@ class CompilerInstance {
487491
DependencyTracker *getDependencyTracker() { return DepTracker.get(); }
488492
const DependencyTracker *getDependencyTracker() const { return DepTracker.get(); }
489493

494+
ModuleDependenciesCache *getModuleDependencyCache() { return ModDepCache.get(); }
495+
490496
UnifiedStatsReporter *getStatsReporter() const { return Stats.get(); }
491497

492498
/// Retrieve the main module containing the files being compiled.
@@ -552,6 +558,7 @@ class CompilerInstance {
552558
bool setUpVirtualFileSystemOverlays();
553559
void setUpLLVMArguments();
554560
void setUpDiagnosticOptions();
561+
void setUpModuleDependencyCacheIfNeeded();
555562
bool setUpModuleLoaders();
556563
bool setUpInputs();
557564
bool setUpASTContextIfNeeded();

lib/Frontend/Frontend.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,11 +321,22 @@ void CompilerInstance::setupDependencyTrackerIfNeeded() {
321321
DepTracker = std::make_unique<DependencyTracker>(*collectionMode);
322322
}
323323

324+
void CompilerInstance::setUpModuleDependencyCacheIfNeeded() {
325+
const auto &Invocation = getInvocation();
326+
const auto &opts = Invocation.getFrontendOptions();
327+
if (opts.RequestedAction == FrontendOptions::ActionType::ScanDependencies ||
328+
opts.RequestedAction == FrontendOptions::ActionType::ScanClangDependencies) {
329+
ModDepCache = std::make_unique<ModuleDependenciesCache>();
330+
}
331+
}
332+
324333
bool CompilerInstance::setup(const CompilerInvocation &Invok) {
325334
Invocation = Invok;
326335

327336
setupDependencyTrackerIfNeeded();
328337

338+
setUpModuleDependencyCacheIfNeeded();
339+
329340
// If initializing the overlay file system fails there's no sense in
330341
// continuing because the compiler will read the wrong files.
331342
if (setUpVirtualFileSystemOverlays())

lib/FrontendTool/ScanDependencies.cpp

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -680,8 +680,10 @@ static bool scanModuleDependencies(CompilerInstance &instance,
680680

681681
llvm::SetVector<ModuleDependencyID, std::vector<ModuleDependencyID>,
682682
std::set<ModuleDependencyID>> allModules;
683-
// Create the module dependency cache.
684-
ModuleDependenciesCache cache;
683+
// Retrieve the instance's module dependency cache.
684+
ModuleDependenciesCache *cache = instance.getModuleDependencyCache();
685+
assert(cache &&
686+
"Dependency Scanner expected a ModuleDependenciesCache on a compiler instance.");
685687
InterfaceSubContextDelegateImpl ASTDelegate(ctx.SourceMgr, ctx.Diags,
686688
ctx.SearchPathOpts, ctx.LangOpts,
687689
ctx.ClangImporterOpts,
@@ -697,11 +699,11 @@ static bool scanModuleDependencies(CompilerInstance &instance,
697699
if (isClang) {
698700
// Loading the clang module using Clang importer.
699701
// This action will populate the cache with the main module's dependencies.
700-
rootDeps = ctx.getModuleDependencies(moduleName, /*IsClang*/true, cache,
702+
rootDeps = ctx.getModuleDependencies(moduleName, /*IsClang*/true, *cache,
701703
ASTDelegate);
702704
} else {
703705
// Loading the swift module's dependencies.
704-
rootDeps = ctx.getSwiftModuleDependencies(moduleName, cache, ASTDelegate);
706+
rootDeps = ctx.getSwiftModuleDependencies(moduleName, *cache, ASTDelegate);
705707
}
706708
if (!rootDeps.hasValue()) {
707709
// We cannot find the clang module, abort.
@@ -723,11 +725,11 @@ static bool scanModuleDependencies(CompilerInstance &instance,
723725
++currentModuleIdx) {
724726
auto module = allModules[currentModuleIdx];
725727
auto discoveredModules =
726-
resolveDirectDependencies(instance, module, cache, ASTDelegate);
728+
resolveDirectDependencies(instance, module, *cache, ASTDelegate);
727729
allModules.insert(discoveredModules.begin(), discoveredModules.end());
728730
}
729731
// Write out the JSON description.
730-
writeJSON(out, instance, cache, ASTDelegate, allModules.getArrayRef());
732+
writeJSON(out, instance, *cache, ASTDelegate, allModules.getArrayRef());
731733
return false;
732734
}
733735

@@ -889,9 +891,11 @@ bool swift::scanDependencies(CompilerInstance &instance) {
889891

890892
allModules.insert({mainModuleName.str(), mainDependencies.getKind()});
891893

892-
// Create the module dependency cache.
893-
ModuleDependenciesCache cache;
894-
cache.recordDependencies(mainModuleName, std::move(mainDependencies));
894+
// Retrieve the instance's module dependency cache.
895+
ModuleDependenciesCache *cache = instance.getModuleDependencyCache();
896+
assert(cache &&
897+
"Dependency Scanner expected a ModuleDependenciesCache on a compiler instance.");
898+
cache->recordDependencies(mainModuleName, std::move(mainDependencies));
895899

896900
auto &ctx = instance.getASTContext();
897901
auto ModuleCachePath = getModuleCachePathFromClang(ctx
@@ -914,28 +918,28 @@ bool swift::scanDependencies(CompilerInstance &instance) {
914918
++currentModuleIdx) {
915919
auto module = allModules[currentModuleIdx];
916920
auto discoveredModules =
917-
resolveDirectDependencies(instance, module, cache, ASTDelegate);
921+
resolveDirectDependencies(instance, module, *cache, ASTDelegate);
918922
allModules.insert(discoveredModules.begin(), discoveredModules.end());
919923
}
920924

921925
// We have all explicit imports now, resolve cross import overlays.
922926
discoverCrosssImportOverlayDependencies(instance, mainModuleName,
923-
/*All transitive dependencies*/allModules.getArrayRef().slice(1), cache,
927+
/*All transitive dependencies*/allModules.getArrayRef().slice(1), *cache,
924928
ASTDelegate, [&](ModuleDependencyID id) {
925929
allModules.insert(id);
926930
});
927931

928932
// Dignose cycle in dependency graph.
929-
if (diagnoseCycle(instance, cache, /*MainModule*/allModules.front(), ASTDelegate))
933+
if (diagnoseCycle(instance, *cache, /*MainModule*/allModules.front(), ASTDelegate))
930934
return true;
931935

932936
// Write out the JSON description.
933-
writeJSON(out, instance, cache, ASTDelegate, allModules.getArrayRef());
937+
writeJSON(out, instance, *cache, ASTDelegate, allModules.getArrayRef());
934938

935939
// Update the dependency tracker.
936940
if (auto depTracker = instance.getDependencyTracker()) {
937941
for (auto module : allModules) {
938-
auto deps = cache.findDependencies(module.first, module.second);
942+
auto deps = cache->findDependencies(module.first, module.second);
939943
if (!deps)
940944
continue;
941945

0 commit comments

Comments
 (0)