Skip to content

Commit 5bed6c4

Browse files
[CAS] Allow SwiftDependencyScanningService be shared
When the CASOptions are the same, SwiftDependencyScanningService can be shared with multiple swift driver invocation for scanning.
1 parent 61c65fb commit 5bed6c4

File tree

4 files changed

+31
-7
lines changed

4 files changed

+31
-7
lines changed

include/swift/AST/ModuleDependencies.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
#include "swift/Basic/LLVM.h"
2222
#include "swift/AST/Import.h"
23+
#include "clang/CAS/CASOptions.h"
2324
#include "clang/Tooling/DependencyScanning/DependencyScanningService.h"
2425
#include "clang/Tooling/DependencyScanning/DependencyScanningTool.h"
2526
#include "llvm/ADT/ArrayRef.h"
@@ -767,6 +768,9 @@ class SwiftDependencyScanningService {
767768
ModuleDependenciesKindMap ModuleDependenciesMap;
768769
};
769770

771+
/// The CASOption created the Scanning Service if used.
772+
llvm::Optional<clang::CASOptions> CASOpts;
773+
770774
/// The persistent Clang dependency scanner service
771775
Optional<clang::tooling::dependencies::DependencyScanningService>
772776
ClangScanningService;
@@ -863,7 +867,7 @@ class SwiftDependencyScanningService {
863867
void overlaySharedFilesystemCacheForCompilation(CompilerInstance &Instance);
864868

865869
/// Setup caching service.
866-
void setupCachingDependencyScanningService(CompilerInstance &Instance);
870+
bool setupCachingDependencyScanningService(CompilerInstance &Instance);
867871
private:
868872
/// Enforce clients not being allowed to query this cache directly, it must be
869873
/// wrapped in an instance of `ModuleDependenciesCache`.

lib/AST/ModuleDependencies.cpp

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -417,12 +417,25 @@ void SwiftDependencyScanningService::overlaySharedFilesystemCacheForCompilation(
417417
Instance.getSourceMgr().setFileSystem(depFS);
418418
}
419419

420-
void SwiftDependencyScanningService::setupCachingDependencyScanningService(
420+
bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
421421
CompilerInstance &Instance) {
422422
if (!Instance.getInvocation().getFrontendOptions().EnableCAS)
423-
return;
423+
return false;
424+
425+
if (CASOpts) {
426+
// If CASOption matches, the service is initialized already.
427+
if (*CASOpts == Instance.getInvocation().getFrontendOptions().CASOpts)
428+
return false;
429+
430+
// CASOption mismatch, return error.
431+
Instance.getDiags().diagnose(
432+
SourceLoc(), diag::error_cas,
433+
"conflicting CAS options used in scanning service");
434+
return true;
435+
}
424436

425437
// Setup CAS.
438+
CASOpts = Instance.getInvocation().getFrontendOptions().CASOpts;
426439
CAS = Instance.getSharedCASInstance();
427440

428441
// Add SDKSetting file.
@@ -456,7 +469,7 @@ void SwiftDependencyScanningService::setupCachingDependencyScanningService(
456469
if (!CachingFS) {
457470
Instance.getDiags().diagnose(SourceLoc(), diag::error_cas,
458471
toString(CachingFS.takeError()));
459-
return;
472+
return true;
460473
}
461474
CacheFS = std::move(*CachingFS);
462475

@@ -474,6 +487,8 @@ void SwiftDependencyScanningService::setupCachingDependencyScanningService(
474487
Instance.getSharedCASInstance(), Instance.getSharedCacheInstance(),
475488
UseClangIncludeTree ? nullptr : CacheFS,
476489
/* ReuseFileManager */ false, /* OptimizeArgs */ false);
490+
491+
return false;
477492
}
478493

479494
SwiftDependencyScanningService::ContextSpecificGlobalCacheState *

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,8 @@ DependencyScanningTool::initCompilerInstanceForScan(
279279
}
280280

281281
// Setup the caching service after the instance finishes setup.
282-
ScanningService->setupCachingDependencyScanningService(*Instance);
282+
if (ScanningService->setupCachingDependencyScanningService(*Instance))
283+
return std::make_error_code(std::errc::invalid_argument);
283284

284285
(void)Instance->getMainModule();
285286

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,7 +1764,9 @@ bool swift::dependencies::scanDependencies(CompilerInstance &instance) {
17641764
deserializeDependencyCache(instance, service);
17651765
// Wrap the filesystem with a caching `DependencyScanningWorkerFilesystem`
17661766
service.overlaySharedFilesystemCacheForCompilation(instance);
1767-
service.setupCachingDependencyScanningService(instance);
1767+
if (service.setupCachingDependencyScanningService(instance))
1768+
return true;
1769+
17681770
ModuleDependenciesCache cache(service,
17691771
instance.getMainModule()->getNameStr().str(),
17701772
instance.getInvocation().getModuleScanningHash());
@@ -1832,7 +1834,9 @@ bool swift::dependencies::batchScanDependencies(
18321834

18331835
SwiftDependencyScanningService singleUseService;
18341836
singleUseService.overlaySharedFilesystemCacheForCompilation(instance);
1835-
singleUseService.setupCachingDependencyScanningService(instance);
1837+
if (singleUseService.setupCachingDependencyScanningService(instance))
1838+
return true;
1839+
18361840
ModuleDependenciesCache cache(singleUseService,
18371841
instance.getMainModule()->getNameStr().str(),
18381842
instance.getInvocation().getModuleScanningHash());

0 commit comments

Comments
 (0)