Skip to content

Commit 0286461

Browse files
authored
Merge pull request #62439 from artemcm/Part2-BetterDependencyCaching-FileSystemCache
[Dependency Scanning] Use persistent dependency scanning filesystem in the Swift dependency scanner
2 parents 9cd004c + 9f62b19 commit 0286461

File tree

5 files changed

+71
-34
lines changed

5 files changed

+71
-34
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 44 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class ClangModuleDependenciesCacheImpl;
3535
class SourceFile;
3636
class ASTContext;
3737
class Identifier;
38+
class CompilerInstance;
3839

3940
/// Which kind of module dependencies we are looking for.
4041
enum class ModuleDependenciesKind : int8_t {
@@ -501,34 +502,40 @@ class GlobalModuleDependenciesCache {
501502
std::vector<ModuleDependencyID> AllModules;
502503

503504
/// Dependencies for modules that have already been computed.
504-
/// This maps a dependency kind to a map of a module's name to a Dependency object
505+
/// This maps a dependency kind to a map of a module's name to a Dependency
506+
/// object
505507
ModuleDependenciesKindMap ModuleDependenciesMap;
506508
};
507509

508-
/// The 'persistent' Clang dependency scanner service
509-
clang::tooling::dependencies::DependencyScanningService clangScanningService;
510+
/// The persistent Clang dependency scanner service
511+
clang::tooling::dependencies::DependencyScanningService ClangScanningService;
512+
/// The global file system cache.
513+
Optional<
514+
clang::tooling::dependencies::DependencyScanningFilesystemSharedCache>
515+
SharedFilesystemCache;
510516

511-
/// All cached Swift source module dependencies, in the order in which they were encountered
517+
/// All cached Swift source module dependencies, in the order in which they
518+
/// were encountered
512519
std::vector<ModuleDependencyID> AllSourceModules;
513-
514-
/// Dependencies for all Swift source-based modules discovered. Each one is the main
515-
/// module of a prior invocation of the scanner.
520+
/// Dependencies for all Swift source-based modules discovered. Each one is
521+
/// the main module of a prior invocation of the scanner.
516522
ModuleNameToDependencyMap SwiftSourceModuleDependenciesMap;
517523

518-
/// A map from a String representing the target triple of a scanner invocation to the corresponding
519-
/// cached dependencies discovered so far when using this triple.
520-
llvm::StringMap<std::unique_ptr<ContextSpecificGlobalCacheState>> ContextSpecificCacheMap;
524+
/// A map from a String representing the target triple of a scanner invocation
525+
/// to the corresponding cached dependencies discovered so far when using this
526+
/// triple.
527+
llvm::StringMap<std::unique_ptr<ContextSpecificGlobalCacheState>>
528+
ContextSpecificCacheMap;
521529

522530
/// The current context hash configuration
523531
Optional<std::string> CurrentContextHash;
524-
525-
/// The context hashes used by scanners using this cache, in the order in which they were used
532+
/// The context hashes used by scanners using this cache, in the order in
533+
/// which they were used
526534
std::vector<std::string> AllContextHashes;
527535

528536
/// Retrieve the dependencies map that corresponds to the given dependency
529537
/// kind.
530-
ModuleNameToDependencyMap &
531-
getDependenciesMap(ModuleDependenciesKind kind);
538+
ModuleNameToDependencyMap &getDependenciesMap(ModuleDependenciesKind kind);
532539
const ModuleNameToDependencyMap &
533540
getDependenciesMap(ModuleDependenciesKind kind) const;
534541

@@ -539,6 +546,16 @@ class GlobalModuleDependenciesCache {
539546
operator=(const GlobalModuleDependenciesCache &) = delete;
540547
virtual ~GlobalModuleDependenciesCache() {}
541548

549+
/// Query the service's filesystem cache
550+
clang::tooling::dependencies::DependencyScanningFilesystemSharedCache &
551+
getSharedFilesystemCache() {
552+
assert(SharedFilesystemCache && "Expected a shared cache");
553+
return *SharedFilesystemCache;
554+
}
555+
556+
/// Wrap the filesystem on the specified `CompilerInstance` with a
557+
/// caching `DependencyScanningWorkerFilesystem`
558+
void overlaySharedFilesystemCacheForCompilation(CompilerInstance &Instance);
542559
private:
543560
/// Enforce clients not being allowed to query this cache directly, it must be
544561
/// wrapped in an instance of `ModuleDependenciesCache`.
@@ -552,19 +569,21 @@ class GlobalModuleDependenciesCache {
552569

553570
/// Return context hashes of all scanner invocations that have used
554571
/// this cache instance.
555-
const std::vector<std::string>& getAllContextHashes() const {
572+
const std::vector<std::string> &getAllContextHashes() const {
556573
return AllContextHashes;
557574
}
558575

559576
/// Whether we have cached dependency information for the given module.
560577
bool hasDependencies(StringRef moduleName,
561578
Optional<ModuleDependenciesKind> kind) const;
562579

563-
/// Return a pointer to the context-specific cache state of the current scanning action.
564-
ContextSpecificGlobalCacheState* getCurrentCache() const;
580+
/// Return a pointer to the context-specific cache state of the current
581+
/// scanning action.
582+
ContextSpecificGlobalCacheState *getCurrentCache() const;
565583

566584
/// Return a pointer to the cache state of the specified context hash.
567-
ContextSpecificGlobalCacheState* getCacheForScanningContextHash(StringRef scanningContextHash) const;
585+
ContextSpecificGlobalCacheState *
586+
getCacheForScanningContextHash(StringRef scanningContextHash) const;
568587

569588
/// Look for source-based module dependency details
570589
Optional<ModuleDependencies>
@@ -585,10 +604,13 @@ class GlobalModuleDependenciesCache {
585604
const ModuleDependencies *updateDependencies(ModuleDependencyID moduleID,
586605
ModuleDependencies dependencies);
587606

588-
/// Reference the list of all module dependencies that are not source-based modules
589-
/// (i.e. interface dependencies, binary dependencies, clang dependencies).
590-
const std::vector<ModuleDependencyID> &getAllNonSourceModules(StringRef scanningContextHash) const {
591-
auto contextSpecificCache = getCacheForScanningContextHash(scanningContextHash);
607+
/// Reference the list of all module dependencies that are not source-based
608+
/// modules (i.e. interface dependencies, binary dependencies, clang
609+
/// dependencies).
610+
const std::vector<ModuleDependencyID> &
611+
getAllNonSourceModules(StringRef scanningContextHash) const {
612+
auto contextSpecificCache =
613+
getCacheForScanningContextHash(scanningContextHash);
592614
return contextSpecificCache->AllModules;
593615
}
594616

include/swift/DependencyScan/ScanDependencies.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,6 @@ class GlobalModuleDependenciesCache;
3131

3232
namespace dependencies {
3333

34-
//using CompilerArgInstanceCacheMap =
35-
// llvm::StringMap<std::pair<std::unique_ptr<CompilerInstance>,
36-
// std::unique_ptr<ModuleDependenciesCache>>>;
37-
3834
using CompilerArgInstanceCacheMap =
3935
llvm::StringMap<std::tuple<std::unique_ptr<CompilerInstance>,
4036
std::unique_ptr<GlobalModuleDependenciesCache>,

lib/AST/ModuleDependencies.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include "swift/AST/ModuleDependencies.h"
1717
#include "swift/AST/Decl.h"
1818
#include "swift/AST/SourceFile.h"
19+
#include "swift/Frontend/Frontend.h"
1920
using namespace swift;
2021

2122
ModuleDependenciesStorageBase::~ModuleDependenciesStorageBase() {}
@@ -226,14 +227,26 @@ void ModuleDependencies::addBridgingModuleDependency(
226227
}
227228

228229
GlobalModuleDependenciesCache::GlobalModuleDependenciesCache()
229-
: clangScanningService(
230-
clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,
230+
: ClangScanningService(clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,
231231
clang::tooling::dependencies::ScanningOutputFormat::Full,
232232
clang::CASOptions(),
233233
/* Cache (llvm::cas::ActionCache) */ nullptr,
234234
/* SharedFS */ nullptr,
235235
/* ReuseFileManager */ false,
236-
/* OptimizeArgs */ false) {}
236+
/* OptimizeArgs */ false) {
237+
SharedFilesystemCache.emplace();
238+
}
239+
240+
void GlobalModuleDependenciesCache::overlaySharedFilesystemCacheForCompilation(CompilerInstance &Instance) {
241+
auto existingFS = Instance.getSourceMgr().getFileSystem();
242+
llvm::IntrusiveRefCntPtr<
243+
clang::tooling::dependencies::DependencyScanningWorkerFilesystem>
244+
depFS =
245+
new clang::tooling::dependencies::DependencyScanningWorkerFilesystem(
246+
getSharedFilesystemCache(), existingFS);
247+
Instance.getSourceMgr().setFileSystem(depFS);
248+
}
249+
237250
GlobalModuleDependenciesCache::ContextSpecificGlobalCacheState *
238251
GlobalModuleDependenciesCache::getCurrentCache() const {
239252
assert(CurrentContextHash.has_value() &&
@@ -429,7 +442,7 @@ ModuleDependenciesCache::ModuleDependenciesCache(
429442
std::string scanningContextHash)
430443
: globalCache(globalCache),
431444
mainScanModuleName(mainScanModuleName),
432-
clangScanningTool(globalCache.clangScanningService) {
445+
clangScanningTool(globalCache.ClangScanningService) {
433446
globalCache.configureForContextHash(scannerContextHash);
434447
for (auto kind = ModuleDependenciesKind::FirstKind;
435448
kind != ModuleDependenciesKind::LastKind; ++kind) {

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ DependencyScanningTool::initCompilerInstanceForScan(
217217
auto Instance = std::make_unique<CompilerInstance>();
218218
Instance->addDiagnosticConsumer(&CDC);
219219

220+
// Wrap the filesystem with a caching `DependencyScanningWorkerFilesystem`
221+
SharedCache->overlaySharedFilesystemCacheForCompilation(*Instance);
222+
220223
// Basic error checking on the arguments
221224
if (CommandArgs.empty()) {
222225
Instance->getDiags().diagnose(SourceLoc(), diag::error_no_frontend_args);

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1242,13 +1242,13 @@ bool swift::dependencies::scanDependencies(CompilerInstance &instance) {
12421242
GlobalModuleDependenciesCache globalCache;
12431243
if (opts.ReuseDependencyScannerCache)
12441244
deserializeDependencyCache(instance, globalCache);
1245-
1246-
auto ModuleCachePath = getModuleCachePathFromClang(
1247-
Context.getClangModuleLoader()->getClangInstance());
1248-
1245+
// Wrap the filesystem with a caching `DependencyScanningWorkerFilesystem`
1246+
globalCache.overlaySharedFilesystemCacheForCompilation(instance);
12491247
ModuleDependenciesCache cache(globalCache,
12501248
instance.getMainModule()->getNameStr().str(),
12511249
instance.getInvocation().getModuleScanningHash());
1250+
auto ModuleCachePath = getModuleCachePathFromClang(
1251+
Context.getClangModuleLoader()->getClangInstance());
12521252

12531253
// Execute scan
12541254
auto dependenciesOrErr = performModuleScan(instance, cache);
@@ -1312,6 +1312,9 @@ bool swift::dependencies::batchScanDependencies(
13121312
// The primary cache used for scans carried out with the compiler instance
13131313
// we have created
13141314
GlobalModuleDependenciesCache singleUseGlobalCache;
1315+
// Wrap the filesystem with a caching `DependencyScanningWorkerFilesystem`
1316+
// Wrap the filesystem with a caching `DependencyScanningWorkerFilesystem`
1317+
singleUseGlobalCache.overlaySharedFilesystemCacheForCompilation(instance);
13151318
ModuleDependenciesCache cache(singleUseGlobalCache,
13161319
instance.getMainModule()->getNameStr().str(),
13171320
instance.getInvocation().getModuleScanningHash());

0 commit comments

Comments
 (0)