Skip to content

Commit f39c638

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 990b95e commit f39c638

File tree

8 files changed

+192
-373
lines changed

8 files changed

+192
-373
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 41 additions & 64 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,48 +539,44 @@ class GlobalModuleDependenciesCache {
548539
operator=(const GlobalModuleDependenciesCache &) = delete;
549540
virtual ~GlobalModuleDependenciesCache() {}
550541

542+
private:
543+
/// Enforce clients not being allowed to query this cache directly, it must be
544+
/// wrapped in an instance of `ModuleDependenciesCache`.
545+
friend class ModuleDependenciesCache;
546+
friend class ModuleDependenciesCacheDeserializer;
547+
friend class ModuleDependenciesCacheSerializer;
548+
549+
/// Configure the current state of the cache to respond to queries
550+
/// for the specified scanning context hash.
551551
void configureForContextHash(std::string scanningContextHash);
552552

553+
/// Return context hashes of all scanner invocations that have used
554+
/// this cache instance.
553555
const std::vector<std::string>& getAllContextHashes() const {
554556
return AllContextHashes;
555557
}
556558

557-
private:
558-
/// Enforce clients not being allowed to query this cache directly, it must be
559-
/// wrapped in an instance of `ModuleDependenciesCache`.
560-
friend class ModuleDependenciesCache;
561-
562559
/// Whether we have cached dependency information for the given module.
563560
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;
561+
Optional<ModuleDependenciesKind> kind) const;
572562

573563
/// Return a pointer to the context-specific cache state of the current scanning action.
574564
ContextSpecificGlobalCacheState* getCurrentCache() const;
575565

576566
/// Return a pointer to the cache state of the specified context hash.
577567
ContextSpecificGlobalCacheState* getCacheForScanningContextHash(StringRef scanningContextHash) const;
578568

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-
589569
/// Look for source-based module dependency details
590570
Optional<ModuleDependencies>
591571
findSourceModuleDependency(StringRef moduleName) const;
592572

573+
/// Look for module dependencies for a module with the given name
574+
///
575+
/// \returns the cached result, or \c None if there is no cached entry.
576+
Optional<ModuleDependencies>
577+
findDependencies(StringRef moduleName,
578+
Optional<ModuleDependenciesKind> kind) const;
579+
593580
/// Record dependencies for the given module.
594581
const ModuleDependencies *recordDependencies(StringRef moduleName,
595582
ModuleDependencies dependencies);
@@ -616,8 +603,7 @@ class GlobalModuleDependenciesCache {
616603
/// scanning action, and wraps an instance of a `GlobalModuleDependenciesCache`
617604
/// which may carry cached scanning information from prior scanning actions.
618605
/// 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.
606+
/// the current scanning action (with their values stored in the global Cache).
621607
class ModuleDependenciesCache {
622608
private:
623609
GlobalModuleDependenciesCache &globalCache;
@@ -628,12 +614,14 @@ class ModuleDependenciesCache {
628614
ModuleDependenciesKindRefMap ModuleDependenciesMap;
629615

630616
/// Name of the module under scan
631-
StringRef mainScanModuleName;
617+
std::string mainScanModuleName;
618+
/// The context hash of the current scanning invocation
619+
std::string scannerContextHash;
620+
632621
/// Set containing all of the Clang modules that have already been seen.
633622
llvm::StringSet<> alreadySeenClangModules;
634623
/// The Clang dependency scanner tool
635624
clang::tooling::dependencies::DependencyScanningTool clangScanningTool;
636-
637625
/// Discovered Clang modules are only cached locally.
638626
llvm::StringMap<ModuleDependenciesVector> clangModuleDependencies;
639627

@@ -646,25 +634,26 @@ class ModuleDependenciesCache {
646634

647635
/// Local cache results lookup, only for modules which were discovered during
648636
/// the current scanner invocation.
649-
bool hasDependencies(StringRef moduleName,
650-
Optional<ModuleDependenciesKind> kind) const;
637+
bool hasDependenciesLocalOnly(StringRef moduleName,
638+
Optional<ModuleDependenciesKind> kind) const;
651639

652640
/// Local cache results lookup, only for modules which were discovered during
653641
/// the current scanner invocation.
654642
Optional<const ModuleDependencies *>
655-
findDependencies(StringRef moduleName,
656-
Optional<ModuleDependenciesKind> kind) const;
643+
findDependenciesLocalOnly(StringRef moduleName,
644+
Optional<ModuleDependenciesKind> kind) const;
657645

658646
public:
659647
ModuleDependenciesCache(GlobalModuleDependenciesCache &globalCache,
660-
StringRef mainScanModuleName);
648+
std::string mainScanModuleName,
649+
std::string scanningContextHash);
661650
ModuleDependenciesCache(const ModuleDependenciesCache &) = delete;
662651
ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete;
663652

664653
public:
665654
/// Whether we have cached dependency information for the given module.
666655
bool hasDependencies(StringRef moduleName,
667-
ModuleLookupSpecifics details) const;
656+
Optional<ModuleDependenciesKind> kind) const;
668657

669658
/// Produce a reference to the Clang scanner tool associated with this cache
670659
clang::tooling::dependencies::DependencyScanningTool& getClangScannerTool() {
@@ -674,24 +663,12 @@ class ModuleDependenciesCache {
674663
return alreadySeenClangModules;
675664
}
676665

677-
/// Look for module dependencies for a module with the given name given
678-
/// current search paths.
666+
/// Look for module dependencies for a module with the given name
679667
///
680668
/// \returns the cached result, or \c None if there is no cached entry.
681669
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-
}
670+
findDependencies(StringRef moduleName,
671+
Optional<ModuleDependenciesKind> kind) const;
695672

696673
/// Record dependencies for the given module.
697674
void recordDependencies(StringRef moduleName,

lib/AST/ASTContext.cpp

Lines changed: 19 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1879,21 +1879,23 @@ Identifier ASTContext::getRealModuleName(Identifier key, ModuleAliasLookupOption
18791879
return value.first;
18801880
}
18811881

1882-
using ModuleDependencyIDSet = std::unordered_set<ModuleDependencyID, llvm::pair_hash<std::string, ModuleDependenciesKind>>;
1882+
using ModuleDependencyIDSet =
1883+
std::unordered_set<ModuleDependencyID,
1884+
llvm::pair_hash<std::string,
1885+
ModuleDependenciesKind>>;
18831886
static void findPath_dfs(ModuleDependencyID X,
18841887
ModuleDependencyID Y,
18851888
ModuleDependencyIDSet &visited,
18861889
std::vector<ModuleDependencyID> &stack,
18871890
std::vector<ModuleDependencyID> &result,
1888-
const ModuleDependenciesCache &cache,
1889-
const llvm::StringSet<> &searchPathSet) {
1891+
const ModuleDependenciesCache &cache) {
18901892
stack.push_back(X);
18911893
if (X == Y) {
18921894
copy(stack.begin(), stack.end(), std::back_inserter(result));
18931895
return;
18941896
}
18951897
visited.insert(X);
1896-
auto node = cache.findDependencies(X.first, {X.second, searchPathSet});
1898+
auto node = cache.findDependencies(X.first, X.second);
18971899
assert(node.has_value() && "Expected cache value for dependency.");
18981900
for (const auto &dep : node->getModuleDependencies()) {
18991901
Optional<ModuleDependenciesKind> lookupKind = None;
@@ -1902,24 +1904,22 @@ static void findPath_dfs(ModuleDependencyID X,
19021904
if ((dep == X.first && node->isSwiftModule()) || node->isClangModule())
19031905
lookupKind = ModuleDependenciesKind::Clang;
19041906

1905-
auto depNode = cache.findDependencies(dep, {lookupKind, searchPathSet});
1907+
auto depNode = cache.findDependencies(dep, lookupKind);
19061908
if (!depNode.has_value())
19071909
continue;
19081910
auto depID = std::make_pair(dep, depNode->getKind());
19091911
if (!visited.count(depID)) {
1910-
findPath_dfs(depID, Y, visited, stack, result, cache, searchPathSet);
1912+
findPath_dfs(depID, Y, visited, stack, result, cache);
19111913
}
19121914
}
19131915
stack.pop_back();
19141916
}
19151917

19161918
static std::vector<ModuleDependencyID>
19171919
findPathToDependency(ModuleDependencyID dependency,
1918-
const ModuleDependenciesCache &cache,
1919-
const llvm::StringSet<> &searchPathSet) {
1920+
const ModuleDependenciesCache &cache) {
19201921
auto mainModuleDep = cache.findDependencies(cache.getMainModuleName(),
1921-
{ModuleDependenciesKind::SwiftSource,
1922-
searchPathSet});
1922+
ModuleDependenciesKind::SwiftSource);
19231923
// We may be in a batch scan instance which does not have this dependency
19241924
if (!mainModuleDep.has_value())
19251925
return {};
@@ -1928,7 +1928,7 @@ findPathToDependency(ModuleDependencyID dependency,
19281928
auto visited = ModuleDependencyIDSet();
19291929
auto stack = std::vector<ModuleDependencyID>();
19301930
auto dependencyPath = std::vector<ModuleDependencyID>();
1931-
findPath_dfs(mainModuleID, dependency, visited, stack, dependencyPath, cache, searchPathSet);
1931+
findPath_dfs(mainModuleID, dependency, visited, stack, dependencyPath, cache);
19321932
return dependencyPath;
19331933
}
19341934

@@ -1937,19 +1937,18 @@ findPathToDependency(ModuleDependencyID dependency,
19371937
static void diagnoseScannerFailure(StringRef moduleName,
19381938
DiagnosticEngine &Diags,
19391939
const ModuleDependenciesCache &cache,
1940-
const llvm::StringSet<> &searchPathSet,
19411940
llvm::Optional<ModuleDependencyID> dependencyOf) {
19421941
Diags.diagnose(SourceLoc(), diag::dependency_scan_module_not_found, moduleName);
19431942
if (dependencyOf.has_value()) {
1944-
auto path = findPathToDependency(dependencyOf.value(), cache, searchPathSet);
1943+
auto path = findPathToDependency(dependencyOf.value(), cache);
19451944
// We may fail to construct a path in some cases, such as a Swift overlay of a Clang
19461945
// module dependnecy.
19471946
if (path.empty())
19481947
path = {dependencyOf.value()};
19491948

19501949
for (auto it = path.rbegin(), end = path.rend(); it != end; ++it) {
19511950
const auto &entry = *it;
1952-
auto entryNode = cache.findDependencies(entry.first, {entry.second, searchPathSet});
1951+
auto entryNode = cache.findDependencies(entry.first, entry.second);
19531952
assert(entryNode.has_value());
19541953
std::string moduleFilePath = "";
19551954
bool isClang = false;
@@ -1992,23 +1991,20 @@ Optional<ModuleDependencies> ASTContext::getModuleDependencies(
19921991
// Check whether we've cached this result.
19931992
if (!isUnderlyingClangModule) {
19941993
if (auto found = cache.findDependencies(
1995-
moduleName,
1996-
{ModuleDependenciesKind::SwiftSource, searchPathSet}))
1994+
moduleName, ModuleDependenciesKind::SwiftSource))
19971995
return found;
19981996
if (auto found = cache.findDependencies(
1999-
moduleName,
2000-
{ModuleDependenciesKind::SwiftInterface, searchPathSet}))
1997+
moduleName, ModuleDependenciesKind::SwiftInterface))
20011998
return found;
20021999
if (auto found = cache.findDependencies(
2003-
moduleName, {ModuleDependenciesKind::SwiftBinary, searchPathSet}))
2000+
moduleName, ModuleDependenciesKind::SwiftBinary))
20042001
return found;
20052002
if (auto found = cache.findDependencies(
2006-
moduleName,
2007-
{ModuleDependenciesKind::SwiftPlaceholder, searchPathSet}))
2003+
moduleName, ModuleDependenciesKind::SwiftPlaceholder))
20082004
return found;
20092005
}
20102006
if (auto found = cache.findDependencies(
2011-
moduleName, {ModuleDependenciesKind::Clang, searchPathSet}))
2007+
moduleName, ModuleDependenciesKind::Clang))
20122008
return found;
20132009
} else {
20142010
for (auto &loader : getImpl().ModuleLoaders) {
@@ -2021,7 +2017,7 @@ Optional<ModuleDependencies> ASTContext::getModuleDependencies(
20212017
return dependencies;
20222018
}
20232019

2024-
diagnoseScannerFailure(moduleName, Diags, cache, searchPathSet,
2020+
diagnoseScannerFailure(moduleName, Diags, cache,
20252021
dependencyOf);
20262022
}
20272023

0 commit comments

Comments
 (0)