Skip to content

Commit 39483f0

Browse files
committed
[Dependency Scanning] Cache compiler instance/module depenency cache used for batched versioned-PCM scans.
1 parent 58e5402 commit 39483f0

File tree

4 files changed

+60
-34
lines changed

4 files changed

+60
-34
lines changed

include/swift/DependencyScan/DependencyScanningTool.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
#define SWIFT_DEPENDENCY_SCANNING_TOOL_H
1515

1616
#include "swift-c/DependencyScan/DependencyScan.h"
17+
#include "swift/Frontend/Frontend.h"
1718
#include "swift/AST/ModuleDependencies.h"
1819
#include "swift/DependencyScan/ScanDependencies.h"
1920
#include "swift/Frontend/PrintingDiagnosticConsumer.h"
20-
#include "llvm/ADT/StringSet.h"
2121
#include "llvm/Support/Error.h"
2222
#include "llvm/Support/StringSaver.h"
2323

@@ -63,10 +63,14 @@ class DependencyScanningTool {
6363
llvm::ErrorOr<std::unique_ptr<CompilerInstance>>
6464
initCompilerInstanceForScan(ArrayRef<const char *> Command);
6565

66-
/// Shared cache of module dependencies, re-used by individual queries
66+
/// Shared cache of module dependencies, re-used by individual full-scan queries
6767
/// during the lifetime of this Tool.
6868
std::unique_ptr<ModuleDependenciesCache> SharedCache;
6969

70+
/// Shared cache of compiler instances created during batch scanning, corresponding to
71+
/// command-line options specified in the batch scan input entry.
72+
std::unique_ptr<CompilerArgInstanceCacheMap> VersionedPCMInstanceCacheCache;
73+
7074
/// A shared consumer that, for now, just prints the encountered diagnostics.
7175
PrintingDiagnosticConsumer PDC;
7276
llvm::BumpPtrAllocator Alloc;

include/swift/DependencyScan/ScanDependencies.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "swift-c/DependencyScan/DependencyScan.h"
1717
#include "llvm/ADT/StringRef.h"
18+
#include "llvm/ADT/StringSet.h"
1819
#include "llvm/Support/Error.h"
1920

2021
namespace llvm {
@@ -29,6 +30,10 @@ class ModuleDependenciesCache;
2930

3031
namespace dependencies {
3132

33+
using CompilerArgInstanceCacheMap =
34+
llvm::StringMap<std::pair<std::unique_ptr<CompilerInstance>,
35+
std::unique_ptr<ModuleDependenciesCache>>>;
36+
3237
struct BatchScanInput {
3338
llvm::StringRef moduleName;
3439
llvm::StringRef arguments;
@@ -64,13 +69,15 @@ performModulePrescan(CompilerInstance &instance);
6469

6570
/// Batch scan the dependencies for modules specified in \c batchInputFile.
6671
std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t>>
67-
performBatchModuleScan(CompilerInstance &instance,
68-
ModuleDependenciesCache &cache, llvm::StringSaver &saver,
72+
performBatchModuleScan(CompilerInstance &invocationInstance,
73+
ModuleDependenciesCache &invocationCache,
74+
CompilerArgInstanceCacheMap *versionedPCMInstanceCache,
75+
llvm::StringSaver &saver,
6976
const std::vector<BatchScanInput> &BatchInput);
7077

7178
/// Batch prescan the imports of modules specified in \c batchInputFile.
7279
std::vector<llvm::ErrorOr<swiftscan_import_set_t>>
73-
performBatchModulePrescan(CompilerInstance &instance,
80+
performBatchModulePrescan(CompilerInstance &invocationInstance,
7481
ModuleDependenciesCache &cache,
7582
llvm::StringSaver &saver,
7683
const std::vector<BatchScanInput> &BatchInput);

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include "swift/AST/DiagnosticsFrontend.h"
1616
#include "swift/Basic/LLVMInitialize.h"
1717
#include "swift/DependencyScan/DependencyScanImpl.h"
18-
#include "swift/Frontend/Frontend.h"
1918
#include "llvm/Support/CommandLine.h"
2019
#include "llvm/Support/FileSystem.h"
2120

@@ -25,8 +24,10 @@ namespace swift {
2524
namespace dependencies {
2625

2726
DependencyScanningTool::DependencyScanningTool()
28-
: SharedCache(std::make_unique<ModuleDependenciesCache>()), PDC(), Alloc(),
29-
Saver(Alloc) {}
27+
: SharedCache(std::make_unique<ModuleDependenciesCache>()),
28+
VersionedPCMInstanceCacheCache(
29+
std::make_unique<CompilerArgInstanceCacheMap>()),
30+
PDC(), Alloc(), Saver(Alloc) {}
3031

3132
llvm::ErrorOr<swiftscan_dependency_graph_t>
3233
DependencyScanningTool::getDependencies(
@@ -76,8 +77,9 @@ DependencyScanningTool::getDependencies(
7677
BatchInput.size(), std::make_error_code(std::errc::invalid_argument));
7778
auto Instance = std::move(*InstanceOrErr);
7879

79-
auto batchScanResults =
80-
performBatchModuleScan(*Instance.get(), *SharedCache, Saver, BatchInput);
80+
auto batchScanResults = performBatchModuleScan(
81+
*Instance.get(), *SharedCache, VersionedPCMInstanceCacheCache.get(),
82+
Saver, BatchInput);
8183

8284
return batchScanResults;
8385
}

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -925,53 +925,59 @@ using CompilerArgInstanceCacheMap =
925925
std::unique_ptr<ModuleDependenciesCache>>>;
926926

927927
static bool
928-
forEachBatchEntry(CompilerInstance &instance, ModuleDependenciesCache &cache,
928+
forEachBatchEntry(CompilerInstance &invocationInstance,
929+
ModuleDependenciesCache &invocationCache,
930+
CompilerArgInstanceCacheMap *versionedPCMInstanceCache,
929931
llvm::StringSaver &saver,
930932
const std::vector<BatchScanInput> &batchInput,
931933
llvm::function_ref<void(BatchScanInput, CompilerInstance &,
932934
ModuleDependenciesCache &)>
933935
scanningAction) {
934-
const CompilerInvocation &invok = instance.getInvocation();
935-
// Keep track of all compiler instances and dependency caches we have
936-
// created.
937-
// TODO: Re-use a single cache across all invocations, once `alreadySeen`
938-
// state is no longer shared.
939-
CompilerArgInstanceCacheMap subInstanceMap;
940-
auto &diags = instance.getDiags();
941-
ForwardingDiagnosticConsumer FDC(instance.getDiags());
936+
const CompilerInvocation &invok = invocationInstance.getInvocation();
937+
bool localSubInstanceMap = false;
938+
CompilerArgInstanceCacheMap *subInstanceMap;
939+
if (versionedPCMInstanceCache)
940+
subInstanceMap = versionedPCMInstanceCache;
941+
else {
942+
subInstanceMap = new CompilerArgInstanceCacheMap;
943+
localSubInstanceMap = true;
944+
}
945+
946+
auto &diags = invocationInstance.getDiags();
947+
ForwardingDiagnosticConsumer FDC(invocationInstance.getDiags());
942948

943949
for (auto &entry : batchInput) {
944950
CompilerInstance *pInstance = nullptr;
945951
ModuleDependenciesCache *pCache = nullptr;
946952
if (entry.arguments.empty()) {
947953
// Use the compiler's instance if no arguments are specified.
948-
pInstance = &instance;
949-
pCache = &cache;
950-
} else if (subInstanceMap.count(entry.arguments)) {
954+
pInstance = &invocationInstance;
955+
pCache = &invocationCache;
956+
} else if (subInstanceMap->count(entry.arguments)) {
951957
// Use the previously created instance if we've seen the arguments
952958
// before.
953-
pInstance = subInstanceMap[entry.arguments].first.get();
954-
pCache = subInstanceMap[entry.arguments].second.get();
959+
pInstance = (*subInstanceMap)[entry.arguments].first.get();
960+
pCache = (*subInstanceMap)[entry.arguments].second.get();
955961
} else {
956962
// Create a new instance by the arguments and save it in the map.
957-
subInstanceMap.insert(
963+
subInstanceMap->insert(
958964
{entry.arguments,
959965
std::make_pair(std::make_unique<CompilerInstance>(),
960966
std::make_unique<ModuleDependenciesCache>())});
961967

962-
pInstance = subInstanceMap[entry.arguments].first.get();
963-
pCache = subInstanceMap[entry.arguments].second.get();
968+
pInstance = (*subInstanceMap)[entry.arguments].first.get();
969+
pCache = (*subInstanceMap)[entry.arguments].second.get();
964970
SmallVector<const char *, 4> args;
965971
llvm::cl::TokenizeGNUCommandLine(entry.arguments, saver, args);
966972
CompilerInvocation subInvok = invok;
967973
pInstance->addDiagnosticConsumer(&FDC);
968974
if (subInvok.parseArgs(args, diags)) {
969-
instance.getDiags().diagnose(
975+
invocationInstance.getDiags().diagnose(
970976
SourceLoc(), diag::scanner_arguments_invalid, entry.arguments);
971977
return true;
972978
}
973979
if (pInstance->setup(subInvok)) {
974-
instance.getDiags().diagnose(
980+
invocationInstance.getDiags().diagnose(
975981
SourceLoc(), diag::scanner_arguments_invalid, entry.arguments);
976982
return true;
977983
}
@@ -980,6 +986,9 @@ forEachBatchEntry(CompilerInstance &instance, ModuleDependenciesCache &cache,
980986
assert(pCache);
981987
scanningAction(entry, *pInstance, *pCache);
982988
}
989+
990+
if (localSubInstanceMap)
991+
delete subInstanceMap;
983992
return false;
984993
}
985994

@@ -1132,8 +1141,9 @@ bool swift::dependencies::batchScanDependencies(
11321141
if (!batchInput.hasValue())
11331142
return true;
11341143

1135-
auto batchScanResults =
1136-
performBatchModuleScan(instance, cache, saver, *batchInput);
1144+
auto batchScanResults = performBatchModuleScan(
1145+
instance, cache, /*versionedPCMInstanceCache*/ nullptr, saver,
1146+
*batchInput);
11371147

11381148
// Write the result JSON to the specified output path, for each entry
11391149
auto ientries = batchInput->cbegin();
@@ -1268,14 +1278,17 @@ swift::dependencies::performModulePrescan(CompilerInstance &instance) {
12681278

12691279
std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t>>
12701280
swift::dependencies::performBatchModuleScan(
1271-
CompilerInstance &instance, ModuleDependenciesCache &cache,
1281+
CompilerInstance &invocationInstance,
1282+
ModuleDependenciesCache &invocationCache,
1283+
CompilerArgInstanceCacheMap *versionedPCMInstanceCache,
12721284
llvm::StringSaver &saver, const std::vector<BatchScanInput> &batchInput) {
12731285
std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t>> batchScanResult;
12741286
batchScanResult.reserve(batchInput.size());
12751287

12761288
// Perform a full dependency scan for each batch entry module
12771289
forEachBatchEntry(
1278-
instance, cache, saver, batchInput,
1290+
invocationInstance, invocationCache, versionedPCMInstanceCache, saver,
1291+
batchInput,
12791292
[&batchScanResult](BatchScanInput entry, CompilerInstance &instance,
12801293
ModuleDependenciesCache &cache) {
12811294
StringRef moduleName = entry.moduleName;
@@ -1343,7 +1356,7 @@ swift::dependencies::performBatchModulePrescan(
13431356

13441357
// Perform a full dependency scan for each batch entry module
13451358
forEachBatchEntry(
1346-
instance, cache, saver, batchInput,
1359+
instance, cache, /*versionedPCMInstanceCache*/ nullptr, saver, batchInput,
13471360
[&batchPrescanResult](BatchScanInput entry, CompilerInstance &instance,
13481361
ModuleDependenciesCache &cache) {
13491362
StringRef moduleName = entry.moduleName;

0 commit comments

Comments
 (0)