Skip to content

Commit 914376d

Browse files
committed
[Dependency Scanning] Do not disambiguate 'GlobalModuleDependenciesCache' by search path set
This is no longer necessary since the cache is always configured for the current scanning context hash, which includes the search path set.
1 parent abda4e8 commit 914376d

File tree

8 files changed

+186
-373
lines changed

8 files changed

+186
-373
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 35 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,6 @@ struct ModuleDependenciesKindHash {
7676
}
7777
};
7878

79-
/// Details of a given module used for dependency scanner cache queries.
80-
struct ModuleLookupSpecifics {
81-
Optional<ModuleDependenciesKind> kind;
82-
llvm::StringSet<> currentSearchPaths;
83-
};
84-
8579
/// Base class for the variant storage of ModuleDependencies.
8680
///
8781
/// This class is mostly an implementation detail for \c ModuleDependencies.
@@ -331,6 +325,7 @@ class ModuleDependencies {
331325
: storage(std::move(storage)) { }
332326

333327
public:
328+
ModuleDependencies() = default;
334329
ModuleDependencies(const ModuleDependencies &other)
335330
: storage(other.storage->clone()) { }
336331
ModuleDependencies(ModuleDependencies &&other) = default;
@@ -481,9 +476,10 @@ class ModuleDependencies {
481476

482477
using ModuleDependencyID = std::pair<std::string, ModuleDependenciesKind>;
483478
using ModuleDependenciesVector = llvm::SmallVector<ModuleDependencies, 1>;
479+
using ModuleNameToDependencyMap = llvm::StringMap<ModuleDependencies>;
484480
using ModuleDependenciesKindMap =
485481
std::unordered_map<ModuleDependenciesKind,
486-
llvm::StringMap<ModuleDependenciesVector>,
482+
ModuleNameToDependencyMap,
487483
ModuleDependenciesKindHash>;
488484
using ModuleDependenciesKindRefMap =
489485
std::unordered_map<ModuleDependenciesKind,
@@ -497,9 +493,6 @@ using ModuleDependenciesKindRefMap =
497493
/// `DependencyScanningTool`). It is not to be queried directly, but is rather
498494
/// meant to be wrapped in an instance of `ModuleDependenciesCache`, responsible
499495
/// for recording new dependencies and answering cache queries in a given scan.
500-
/// Queries to this cache must be disambiguated with a set of search paths to
501-
/// ensure that the returned cached dependency was one that can be found in the
502-
/// current scanning action's filesystem view.
503496
class GlobalModuleDependenciesCache {
504497
/// Global cache contents specific to a specific scanner invocation context
505498
struct ContextSpecificGlobalCacheState {
@@ -508,9 +501,7 @@ class GlobalModuleDependenciesCache {
508501
std::vector<ModuleDependencyID> AllModules;
509502

510503
/// Dependencies for modules that have already been computed.
511-
/// This maps a dependency kind to a map of a module's name to a vector of Dependency objects,
512-
/// which correspond to instances of the same module that may have been found
513-
/// in different sets of search paths.
504+
/// This maps a dependency kind to a map of a module's name to a Dependency object
514505
ModuleDependenciesKindMap ModuleDependenciesMap;
515506
};
516507

@@ -522,7 +513,7 @@ class GlobalModuleDependenciesCache {
522513

523514
/// Dependencies for all Swift source-based modules discovered. Each one is the main
524515
/// module of a prior invocation of the scanner.
525-
llvm::StringMap<ModuleDependencies> SwiftSourceModuleDependenciesMap;
516+
ModuleNameToDependencyMap SwiftSourceModuleDependenciesMap;
526517

527518
/// A map from a String representing the target triple of a scanner invocation to the corresponding
528519
/// cached dependencies discovered so far when using this triple.
@@ -536,9 +527,9 @@ class GlobalModuleDependenciesCache {
536527

537528
/// Retrieve the dependencies map that corresponds to the given dependency
538529
/// kind.
539-
llvm::StringMap<ModuleDependenciesVector> &
530+
ModuleNameToDependencyMap &
540531
getDependenciesMap(ModuleDependenciesKind kind);
541-
const llvm::StringMap<ModuleDependenciesVector> &
532+
const ModuleNameToDependencyMap &
542533
getDependenciesMap(ModuleDependenciesKind kind) const;
543534

544535
public:
@@ -548,8 +539,6 @@ class GlobalModuleDependenciesCache {
548539
operator=(const GlobalModuleDependenciesCache &) = delete;
549540
virtual ~GlobalModuleDependenciesCache() {}
550541

551-
void configureForContextHash(std::string scanningContextHash);
552-
553542
const std::vector<std::string>& getAllContextHashes() const {
554543
return AllContextHashes;
555544
}
@@ -558,38 +547,34 @@ class GlobalModuleDependenciesCache {
558547
/// Enforce clients not being allowed to query this cache directly, it must be
559548
/// wrapped in an instance of `ModuleDependenciesCache`.
560549
friend class ModuleDependenciesCache;
550+
friend class ModuleDependenciesCacheDeserializer;
551+
friend class ModuleDependenciesCacheSerializer;
552+
553+
/// Configure the current state of the cache to respond to queries
554+
/// for the specified scanning context hash.
555+
void configureForContextHash(std::string scanningContextHash);
561556

562557
/// Whether we have cached dependency information for the given module.
563558
bool hasDependencies(StringRef moduleName,
564-
ModuleLookupSpecifics details) const;
565-
566-
/// Look for module dependencies for a module with the given name given
567-
/// current search paths.
568-
///
569-
/// \returns the cached result, or \c None if there is no cached entry.
570-
Optional<ModuleDependencies>
571-
findDependencies(StringRef moduleName, ModuleLookupSpecifics details) const;
559+
Optional<ModuleDependenciesKind> kind) const;
572560

573561
/// Return a pointer to the context-specific cache state of the current scanning action.
574562
ContextSpecificGlobalCacheState* getCurrentCache() const;
575563

576564
/// Return a pointer to the cache state of the specified context hash.
577565
ContextSpecificGlobalCacheState* getCacheForScanningContextHash(StringRef scanningContextHash) const;
578566

579-
public:
580-
/// Look for module dependencies for a module with the given name.
581-
/// This method has a deliberately-obtuse name to indicate that it is not to
582-
/// be used for general queries.
583-
///
584-
/// \returns the cached result, or \c None if there is no cached entry.
585-
Optional<ModuleDependenciesVector>
586-
findAllDependenciesIrrespectiveOfSearchPaths(
587-
StringRef moduleName, Optional<ModuleDependenciesKind> kind) const;
588-
589567
/// Look for source-based module dependency details
590568
Optional<ModuleDependencies>
591569
findSourceModuleDependency(StringRef moduleName) const;
592570

571+
/// Look for module dependencies for a module with the given name
572+
///
573+
/// \returns the cached result, or \c None if there is no cached entry.
574+
Optional<ModuleDependencies>
575+
findDependencies(StringRef moduleName,
576+
Optional<ModuleDependenciesKind> kind) const;
577+
593578
/// Record dependencies for the given module.
594579
const ModuleDependencies *recordDependencies(StringRef moduleName,
595580
ModuleDependencies dependencies);
@@ -616,8 +601,7 @@ class GlobalModuleDependenciesCache {
616601
/// scanning action, and wraps an instance of a `GlobalModuleDependenciesCache`
617602
/// which may carry cached scanning information from prior scanning actions.
618603
/// This cache maintains a store of references to all dependencies found within
619-
/// the current scanning action (with their values stored in the global Cache),
620-
/// since these do not require clients to disambiguate them with search paths.
604+
/// the current scanning action (with their values stored in the global Cache).
621605
class ModuleDependenciesCache {
622606
private:
623607
GlobalModuleDependenciesCache &globalCache;
@@ -629,11 +613,13 @@ class ModuleDependenciesCache {
629613

630614
/// Name of the module under scan
631615
StringRef mainScanModuleName;
616+
/// The context hash of the current scanning invocation
617+
std::string scannerContextHash;
618+
632619
/// Set containing all of the Clang modules that have already been seen.
633620
llvm::StringSet<> alreadySeenClangModules;
634621
/// The Clang dependency scanner tool
635622
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
636-
637623
/// Discovered Clang modules are only cached locally.
638624
llvm::StringMap<ModuleDependenciesVector> clangModuleDependencies;
639625

@@ -646,25 +632,26 @@ class ModuleDependenciesCache {
646632

647633
/// Local cache results lookup, only for modules which were discovered during
648634
/// the current scanner invocation.
649-
bool hasDependencies(StringRef moduleName,
650-
Optional<ModuleDependenciesKind> kind) const;
635+
bool hasDependenciesLocalOnly(StringRef moduleName,
636+
Optional<ModuleDependenciesKind> kind) const;
651637

652638
/// Local cache results lookup, only for modules which were discovered during
653639
/// the current scanner invocation.
654640
Optional<const ModuleDependencies *>
655-
findDependencies(StringRef moduleName,
656-
Optional<ModuleDependenciesKind> kind) const;
641+
findDependenciesLocalOnly(StringRef moduleName,
642+
Optional<ModuleDependenciesKind> kind) const;
657643

658644
public:
659645
ModuleDependenciesCache(GlobalModuleDependenciesCache &globalCache,
660-
StringRef mainScanModuleName);
646+
std::string mainScanModuleName,
647+
std::string scanningContextHash);
661648
ModuleDependenciesCache(const ModuleDependenciesCache &) = delete;
662649
ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete;
663650

664651
public:
665652
/// Whether we have cached dependency information for the given module.
666653
bool hasDependencies(StringRef moduleName,
667-
ModuleLookupSpecifics details) const;
654+
Optional<ModuleDependenciesKind> kind) const;
668655

669656
/// Produce a reference to the Clang scanner tool associated with this cache
670657
clang::tooling::dependencies::DependencyScanningTool& getClangScannerTool() {
@@ -674,24 +661,12 @@ class ModuleDependenciesCache {
674661
return alreadySeenClangModules;
675662
}
676663

677-
/// Look for module dependencies for a module with the given name given
678-
/// current search paths.
664+
/// Look for module dependencies for a module with the given name
679665
///
680666
/// \returns the cached result, or \c None if there is no cached entry.
681667
Optional<ModuleDependencies>
682-
findDependencies(StringRef moduleName, ModuleLookupSpecifics details) const;
683-
684-
/// Look for module dependencies for a module with the given name.
685-
/// This method has a deliberately-obtuse name to indicate that it is not to
686-
/// be used for general queries.
687-
///
688-
/// \returns the cached result, or \c None if there is no cached entry.
689-
Optional<ModuleDependenciesVector>
690-
findAllDependenciesIrrespectiveOfSearchPaths(
691-
StringRef moduleName, Optional<ModuleDependenciesKind> kind) const {
692-
return globalCache.findAllDependenciesIrrespectiveOfSearchPaths(moduleName,
693-
kind);
694-
}
668+
findDependencies(StringRef moduleName,
669+
Optional<ModuleDependenciesKind> kind) const;
695670

696671
/// Record dependencies for the given module.
697672
void recordDependencies(StringRef moduleName,

lib/AST/ASTContext.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1883,21 +1883,23 @@ Identifier ASTContext::getRealModuleName(Identifier key, ModuleAliasLookupOption
18831883
return value.first;
18841884
}
18851885

1886-
using ModuleDependencyIDSet = std::unordered_set<ModuleDependencyID, llvm::pair_hash<std::string, ModuleDependenciesKind>>;
1886+
using ModuleDependencyIDSet =
1887+
std::unordered_set<ModuleDependencyID,
1888+
llvm::pair_hash<std::string,
1889+
ModuleDependenciesKind>>;
18871890
static void findPath_dfs(ModuleDependencyID X,
18881891
ModuleDependencyID Y,
18891892
ModuleDependencyIDSet &visited,
18901893
std::vector<ModuleDependencyID> &stack,
18911894
std::vector<ModuleDependencyID> &result,
1892-
const ModuleDependenciesCache &cache,
1893-
const llvm::StringSet<> &searchPathSet) {
1895+
const ModuleDependenciesCache &cache) {
18941896
stack.push_back(X);
18951897
if (X == Y) {
18961898
copy(stack.begin(), stack.end(), std::back_inserter(result));
18971899
return;
18981900
}
18991901
visited.insert(X);
1900-
auto node = cache.findDependencies(X.first, {X.second, searchPathSet});
1902+
auto node = cache.findDependencies(X.first, X.second);
19011903
assert(node.has_value() && "Expected cache value for dependency.");
19021904
for (const auto &dep : node->getModuleDependencies()) {
19031905
Optional<ModuleDependenciesKind> lookupKind = None;
@@ -1906,24 +1908,22 @@ static void findPath_dfs(ModuleDependencyID X,
19061908
if ((dep == X.first && node->isSwiftModule()) || node->isClangModule())
19071909
lookupKind = ModuleDependenciesKind::Clang;
19081910

1909-
auto depNode = cache.findDependencies(dep, {lookupKind, searchPathSet});
1911+
auto depNode = cache.findDependencies(dep, lookupKind);
19101912
if (!depNode.has_value())
19111913
continue;
19121914
auto depID = std::make_pair(dep, depNode->getKind());
19131915
if (!visited.count(depID)) {
1914-
findPath_dfs(depID, Y, visited, stack, result, cache, searchPathSet);
1916+
findPath_dfs(depID, Y, visited, stack, result, cache);
19151917
}
19161918
}
19171919
stack.pop_back();
19181920
}
19191921

19201922
static std::vector<ModuleDependencyID>
19211923
findPathToDependency(ModuleDependencyID dependency,
1922-
const ModuleDependenciesCache &cache,
1923-
const llvm::StringSet<> &searchPathSet) {
1924+
const ModuleDependenciesCache &cache) {
19241925
auto mainModuleDep = cache.findDependencies(cache.getMainModuleName(),
1925-
{ModuleDependenciesKind::SwiftSource,
1926-
searchPathSet});
1926+
ModuleDependenciesKind::SwiftSource);
19271927
// We may be in a batch scan instance which does not have this dependency
19281928
if (!mainModuleDep.has_value())
19291929
return {};
@@ -1932,7 +1932,7 @@ findPathToDependency(ModuleDependencyID dependency,
19321932
auto visited = ModuleDependencyIDSet();
19331933
auto stack = std::vector<ModuleDependencyID>();
19341934
auto dependencyPath = std::vector<ModuleDependencyID>();
1935-
findPath_dfs(mainModuleID, dependency, visited, stack, dependencyPath, cache, searchPathSet);
1935+
findPath_dfs(mainModuleID, dependency, visited, stack, dependencyPath, cache);
19361936
return dependencyPath;
19371937
}
19381938

@@ -1941,19 +1941,18 @@ findPathToDependency(ModuleDependencyID dependency,
19411941
static void diagnoseScannerFailure(StringRef moduleName,
19421942
DiagnosticEngine &Diags,
19431943
const ModuleDependenciesCache &cache,
1944-
const llvm::StringSet<> &searchPathSet,
19451944
llvm::Optional<ModuleDependencyID> dependencyOf) {
19461945
Diags.diagnose(SourceLoc(), diag::dependency_scan_module_not_found, moduleName);
19471946
if (dependencyOf.has_value()) {
1948-
auto path = findPathToDependency(dependencyOf.value(), cache, searchPathSet);
1947+
auto path = findPathToDependency(dependencyOf.value(), cache);
19491948
// We may fail to construct a path in some cases, such as a Swift overlay of a Clang
19501949
// module dependnecy.
19511950
if (path.empty())
19521951
path = {dependencyOf.value()};
19531952

19541953
for (auto it = path.rbegin(), end = path.rend(); it != end; ++it) {
19551954
const auto &entry = *it;
1956-
auto entryNode = cache.findDependencies(entry.first, {entry.second, searchPathSet});
1955+
auto entryNode = cache.findDependencies(entry.first, entry.second);
19571956
assert(entryNode.has_value());
19581957
std::string moduleFilePath = "";
19591958
bool isClang = false;
@@ -1996,23 +1995,20 @@ Optional<ModuleDependencies> ASTContext::getModuleDependencies(
19961995
// Check whether we've cached this result.
19971996
if (!isUnderlyingClangModule) {
19981997
if (auto found = cache.findDependencies(
1999-
moduleName,
2000-
{ModuleDependenciesKind::SwiftSource, searchPathSet}))
1998+
moduleName, ModuleDependenciesKind::SwiftSource))
20011999
return found;
20022000
if (auto found = cache.findDependencies(
2003-
moduleName,
2004-
{ModuleDependenciesKind::SwiftInterface, searchPathSet}))
2001+
moduleName, ModuleDependenciesKind::SwiftInterface))
20052002
return found;
20062003
if (auto found = cache.findDependencies(
2007-
moduleName, {ModuleDependenciesKind::SwiftBinary, searchPathSet}))
2004+
moduleName, ModuleDependenciesKind::SwiftBinary))
20082005
return found;
20092006
if (auto found = cache.findDependencies(
2010-
moduleName,
2011-
{ModuleDependenciesKind::SwiftPlaceholder, searchPathSet}))
2007+
moduleName, ModuleDependenciesKind::SwiftPlaceholder))
20122008
return found;
20132009
}
20142010
if (auto found = cache.findDependencies(
2015-
moduleName, {ModuleDependenciesKind::Clang, searchPathSet}))
2011+
moduleName, ModuleDependenciesKind::Clang))
20162012
return found;
20172013
} else {
20182014
for (auto &loader : getImpl().ModuleLoaders) {
@@ -2025,7 +2021,7 @@ Optional<ModuleDependencies> ASTContext::getModuleDependencies(
20252021
return dependencies;
20262022
}
20272023

2028-
diagnoseScannerFailure(moduleName, Diags, cache, searchPathSet,
2024+
diagnoseScannerFailure(moduleName, Diags, cache,
20292025
dependencyOf);
20302026
}
20312027

0 commit comments

Comments
 (0)