Skip to content

Commit f4acced

Browse files
authored
Merge pull request #77794 from artemcm/RefactorGlobalDependencyScanningCache
[Dependency Scanner] Refactor the global scanning service to no longer maintain scanner cache state
2 parents 70b07af + 9055a40 commit f4acced

13 files changed

+276
-537
lines changed

include/swift-c/DependencyScan/DependencyScan.h

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
/// The version constants for the SwiftDependencyScan C API.
2525
/// SWIFTSCAN_VERSION_MINOR should increase when there are API additions.
2626
/// SWIFTSCAN_VERSION_MAJOR is intended for "major" source/ABI breaking changes.
27-
#define SWIFTSCAN_VERSION_MAJOR 0
28-
#define SWIFTSCAN_VERSION_MINOR 10
27+
#define SWIFTSCAN_VERSION_MAJOR 1
28+
#define SWIFTSCAN_VERSION_MINOR 0
2929

3030
SWIFTSCAN_BEGIN_DECLS
3131

@@ -481,17 +481,6 @@ swiftscan_source_location_get_column_number(swiftscan_source_location_t source_l
481481
// to allow clients to perform incremental dependency scans by having the
482482
// scanner's state be serializable and re-usable.
483483

484-
/// For the specified \c scanner instance, serialize its state to the specified file-system \c path .
485-
SWIFTSCAN_PUBLIC void
486-
swiftscan_scanner_cache_serialize(swiftscan_scanner_t scanner,
487-
const char * path);
488-
489-
/// For the specified \c scanner instance, load in scanner state from a file at
490-
/// the specified file-system \c path .
491-
SWIFTSCAN_PUBLIC bool
492-
swiftscan_scanner_cache_load(swiftscan_scanner_t scanner,
493-
const char * path);
494-
495484
/// For the specified \c scanner instance, reset its internal state, ensuring subsequent
496485
/// scanning queries are done "from-scratch".
497486
SWIFTSCAN_PUBLIC void

include/swift/AST/ModuleDependencies.h

Lines changed: 14 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -971,10 +971,6 @@ using ModuleNameToDependencyMap = llvm::StringMap<ModuleDependencyInfo>;
971971
using ModuleDependenciesKindMap =
972972
std::unordered_map<ModuleDependencyKind, ModuleNameToDependencyMap,
973973
ModuleDependencyKindHash>;
974-
using ModuleDependenciesKindRefMap =
975-
std::unordered_map<ModuleDependencyKind,
976-
llvm::StringMap<const ModuleDependencyInfo *>,
977-
ModuleDependencyKindHash>;
978974

979975
// MARK: SwiftDependencyTracker
980976
/// Track swift dependency
@@ -1005,23 +1001,8 @@ class SwiftDependencyTracker {
10051001

10061002
// MARK: SwiftDependencyScanningService
10071003
/// A carrier of state shared among possibly multiple invocations of the
1008-
/// dependency scanner. Acts as a global cache of discovered module dependencies
1009-
/// and filesystem state. It is not to be queried directly, but is rather meant
1010-
/// to be wrapped in an instance of `ModuleDependenciesCache`, responsible for
1011-
/// recording new dependencies and answering cache queries in a given scan.
1004+
/// dependency scanner.
10121005
class SwiftDependencyScanningService {
1013-
/// Global cache contents specific to a specific scanner invocation context
1014-
struct ContextSpecificGlobalCacheState {
1015-
/// All cached module dependencies, in the order in which they were
1016-
/// encountered.
1017-
std::vector<ModuleDependencyID> AllModules;
1018-
1019-
/// Dependencies for modules that have already been computed.
1020-
/// This maps a dependency kind to a map of a module's name to a Dependency
1021-
/// object
1022-
ModuleDependenciesKindMap ModuleDependenciesMap;
1023-
};
1024-
10251006
/// The CASOption created the Scanning Service if used.
10261007
std::optional<clang::CASOptions> CASOpts;
10271008

@@ -1046,27 +1027,9 @@ class SwiftDependencyScanningService {
10461027
clang::tooling::dependencies::DependencyScanningFilesystemSharedCache>
10471028
SharedFilesystemCache;
10481029

1049-
/// A map from a String representing the target triple of a scanner invocation
1050-
/// to the corresponding cached dependencies discovered so far when using this
1051-
/// triple.
1052-
llvm::StringMap<std::unique_ptr<ContextSpecificGlobalCacheState>>
1053-
ContextSpecificCacheMap;
1054-
1055-
/// The context hashes used by scanners using this cache, in the order in
1056-
/// which they were used
1057-
std::vector<std::string> AllContextHashes;
1058-
10591030
/// Shared state mutual-exclusivity lock
10601031
mutable llvm::sys::SmartMutex<true> ScanningServiceGlobalLock;
10611032

1062-
/// Retrieve the dependencies map that corresponds to the given dependency
1063-
/// kind.
1064-
ModuleNameToDependencyMap &getDependenciesMap(ModuleDependencyKind kind,
1065-
StringRef scanContextHash);
1066-
const ModuleNameToDependencyMap &
1067-
getDependenciesMap(ModuleDependencyKind kind,
1068-
StringRef scanContextHash) const;
1069-
10701033
public:
10711034
SwiftDependencyScanningService();
10721035
SwiftDependencyScanningService(const SwiftDependencyScanningService &) =
@@ -1136,54 +1099,6 @@ class SwiftDependencyScanningService {
11361099
friend class ModuleDependenciesCache;
11371100
friend class ModuleDependencyScanner;
11381101
friend class ModuleDependencyScanningWorker;
1139-
friend class ModuleDependenciesCacheDeserializer;
1140-
friend class ModuleDependenciesCacheSerializer;
1141-
1142-
/// Configure the current state of the cache to respond to queries
1143-
/// for the specified scanning context hash.
1144-
void configureForContextHash(StringRef scanningContextHash);
1145-
1146-
/// Return context hashes of all scanner invocations that have used
1147-
/// this cache instance.
1148-
const std::vector<std::string> &getAllContextHashes() const {
1149-
return AllContextHashes;
1150-
}
1151-
1152-
/// Whether we have cached dependency information for the given module.
1153-
bool hasDependency(StringRef moduleName,
1154-
std::optional<ModuleDependencyKind> kind,
1155-
StringRef scanContextHash) const;
1156-
1157-
/// Return a pointer to the cache state of the specified context hash.
1158-
ContextSpecificGlobalCacheState *
1159-
getCacheForScanningContextHash(StringRef scanContextHash) const;
1160-
1161-
/// Look for module dependencies for a module with the given name
1162-
///
1163-
/// \returns the cached result, or \c None if there is no cached entry.
1164-
std::optional<const ModuleDependencyInfo *>
1165-
findDependency(StringRef moduleName, std::optional<ModuleDependencyKind> kind,
1166-
StringRef scanContextHash) const;
1167-
1168-
/// Record dependencies for the given module.
1169-
const ModuleDependencyInfo *
1170-
recordDependency(StringRef moduleName, ModuleDependencyInfo dependencies,
1171-
StringRef scanContextHash);
1172-
1173-
/// Update stored dependencies for the given module.
1174-
const ModuleDependencyInfo *
1175-
updateDependency(ModuleDependencyID moduleID,
1176-
ModuleDependencyInfo dependencies,
1177-
StringRef scanContextHash);
1178-
1179-
/// Reference the list of all module dependency infos for a given scanning
1180-
/// context
1181-
const std::vector<ModuleDependencyID> &
1182-
getAllModules(StringRef scanningContextHash) const {
1183-
auto contextSpecificCache =
1184-
getCacheForScanningContextHash(scanningContextHash);
1185-
return contextSpecificCache->AllModules;
1186-
}
11871102
};
11881103

11891104
// MARK: ModuleDependenciesCache
@@ -1195,11 +1110,10 @@ class SwiftDependencyScanningService {
11951110
class ModuleDependenciesCache {
11961111
private:
11971112
SwiftDependencyScanningService &globalScanningService;
1198-
/// References to data in the `globalScanningService` for module dependencies
1199-
ModuleDependenciesKindRefMap ModuleDependenciesMap;
1113+
/// Discovered dependencies
1114+
ModuleDependenciesKindMap ModuleDependenciesMap;
12001115
/// Set containing all of the Clang modules that have already been seen.
1201-
llvm::DenseSet<clang::tooling::dependencies::ModuleID>
1202-
alreadySeenClangModules;
1116+
llvm::DenseSet<clang::tooling::dependencies::ModuleID> alreadySeenClangModules;
12031117
/// Name of the module under scan
12041118
std::string mainScanModuleName;
12051119
/// The context hash of the current scanning invocation
@@ -1223,6 +1137,11 @@ class ModuleDependenciesCache {
12231137
ModuleDependenciesCache &operator=(const ModuleDependenciesCache &) = delete;
12241138

12251139
public:
1140+
/// Retrieve the dependencies map that corresponds to the given dependency
1141+
/// kind.
1142+
ModuleNameToDependencyMap &getDependenciesMap(ModuleDependencyKind kind);
1143+
const ModuleNameToDependencyMap &getDependenciesMap(ModuleDependencyKind kind) const;
1144+
12261145
/// Whether we have cached dependency information for the given module.
12271146
bool hasDependency(const ModuleDependencyID &moduleID) const;
12281147
/// Whether we have cached dependency information for the given module.
@@ -1283,13 +1202,7 @@ class ModuleDependenciesCache {
12831202
/// \returns the cached result, or \c None if there is no cached entry.
12841203
std::optional<const ModuleDependencyInfo *>
12851204
findDependency(StringRef moduleName,
1286-
std::optional<ModuleDependencyKind> kind) const;
1287-
1288-
/// Look for module dependencies for a module with the given name
1289-
///
1290-
/// \returns the cached result, or \c None if there is no cached entry.
1291-
std::optional<const ModuleDependencyInfo *>
1292-
findDependency(StringRef moduleName) const;
1205+
std::optional<ModuleDependencyKind> kind = std::nullopt) const;
12931206

12941207
/// Look for Swift module dependencies for a module with the given name
12951208
///
@@ -1340,6 +1253,10 @@ class ModuleDependenciesCache {
13401253
const ArrayRef<ModuleDependencyID> dependencyIDs);
13411254

13421255
StringRef getMainModuleName() const { return mainScanModuleName; }
1256+
1257+
private:
1258+
friend class ModuleDependenciesCacheDeserializer;
1259+
friend class ModuleDependenciesCacheSerializer;
13431260
};
13441261
} // namespace swift
13451262

include/swift/DependencyScan/DependencyScanningTool.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,6 @@ class DependencyScanningTool {
108108
const llvm::StringSet<> &PlaceholderModules,
109109
StringRef WorkingDirectory);
110110

111-
/// Writes the current `SharedCache` instance to a specified FileSystem path.
112-
void serializeCache(llvm::StringRef path);
113-
/// Loads an instance of a `SwiftDependencyScanningService` to serve as the `SharedCache`
114-
/// from a specified FileSystem path.
115-
bool loadCache(llvm::StringRef path);
116111
/// Discard the tool's current `SharedCache` and start anew.
117112
void resetCache();
118113
/// Query diagnostics consumed so far.

include/swift/DependencyScan/SerializedModuleDependencyCacheFormat.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -209,24 +209,24 @@ using ClangModuleDetailsLayout =
209209
/// Tries to read the dependency graph from the given buffer.
210210
/// Returns \c true if there was an error.
211211
bool readInterModuleDependenciesCache(llvm::MemoryBuffer &buffer,
212-
SwiftDependencyScanningService &cache);
212+
ModuleDependenciesCache &cache);
213213

214214
/// Tries to read the dependency graph from the given path name.
215215
/// Returns true if there was an error.
216216
bool readInterModuleDependenciesCache(llvm::StringRef path,
217-
SwiftDependencyScanningService &cache);
217+
ModuleDependenciesCache &cache);
218218

219219
/// Tries to write the dependency graph to the given path name.
220220
/// Returns true if there was an error.
221221
bool writeInterModuleDependenciesCache(DiagnosticEngine &diags,
222222
llvm::vfs::OutputBackend &backend,
223223
llvm::StringRef path,
224-
const SwiftDependencyScanningService &cache);
224+
const ModuleDependenciesCache &cache);
225225

226226
/// Tries to write out the given dependency cache with the given
227227
/// bitstream writer.
228228
void writeInterModuleDependenciesCache(llvm::BitstreamWriter &Out,
229-
const SwiftDependencyScanningService &cache);
229+
const ModuleDependenciesCache &cache);
230230

231231
} // end namespace module_dependency_cache_serialization
232232
} // end namespace dependencies

0 commit comments

Comments
 (0)