@@ -925,53 +925,59 @@ using CompilerArgInstanceCacheMap =
925
925
std::unique_ptr<ModuleDependenciesCache>>>;
926
926
927
927
static bool
928
- forEachBatchEntry (CompilerInstance &instance, ModuleDependenciesCache &cache,
928
+ forEachBatchEntry (CompilerInstance &invocationInstance,
929
+ ModuleDependenciesCache &invocationCache,
930
+ CompilerArgInstanceCacheMap *versionedPCMInstanceCache,
929
931
llvm::StringSaver &saver,
930
932
const std::vector<BatchScanInput> &batchInput,
931
933
llvm::function_ref<void (BatchScanInput, CompilerInstance &,
932
934
ModuleDependenciesCache &)>
933
935
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 ());
942
948
943
949
for (auto &entry : batchInput) {
944
950
CompilerInstance *pInstance = nullptr ;
945
951
ModuleDependenciesCache *pCache = nullptr ;
946
952
if (entry.arguments .empty ()) {
947
953
// 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 )) {
951
957
// Use the previously created instance if we've seen the arguments
952
958
// 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 ();
955
961
} else {
956
962
// Create a new instance by the arguments and save it in the map.
957
- subInstanceMap. insert (
963
+ subInstanceMap-> insert (
958
964
{entry.arguments ,
959
965
std::make_pair (std::make_unique<CompilerInstance>(),
960
966
std::make_unique<ModuleDependenciesCache>())});
961
967
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 ();
964
970
SmallVector<const char *, 4 > args;
965
971
llvm::cl::TokenizeGNUCommandLine (entry.arguments , saver, args);
966
972
CompilerInvocation subInvok = invok;
967
973
pInstance->addDiagnosticConsumer (&FDC);
968
974
if (subInvok.parseArgs (args, diags)) {
969
- instance .getDiags ().diagnose (
975
+ invocationInstance .getDiags ().diagnose (
970
976
SourceLoc (), diag::scanner_arguments_invalid, entry.arguments );
971
977
return true ;
972
978
}
973
979
if (pInstance->setup (subInvok)) {
974
- instance .getDiags ().diagnose (
980
+ invocationInstance .getDiags ().diagnose (
975
981
SourceLoc (), diag::scanner_arguments_invalid, entry.arguments );
976
982
return true ;
977
983
}
@@ -980,6 +986,9 @@ forEachBatchEntry(CompilerInstance &instance, ModuleDependenciesCache &cache,
980
986
assert (pCache);
981
987
scanningAction (entry, *pInstance, *pCache);
982
988
}
989
+
990
+ if (localSubInstanceMap)
991
+ delete subInstanceMap;
983
992
return false ;
984
993
}
985
994
@@ -1132,8 +1141,9 @@ bool swift::dependencies::batchScanDependencies(
1132
1141
if (!batchInput.hasValue ())
1133
1142
return true ;
1134
1143
1135
- auto batchScanResults =
1136
- performBatchModuleScan (instance, cache, saver, *batchInput);
1144
+ auto batchScanResults = performBatchModuleScan (
1145
+ instance, cache, /* versionedPCMInstanceCache*/ nullptr , saver,
1146
+ *batchInput);
1137
1147
1138
1148
// Write the result JSON to the specified output path, for each entry
1139
1149
auto ientries = batchInput->cbegin ();
@@ -1268,14 +1278,17 @@ swift::dependencies::performModulePrescan(CompilerInstance &instance) {
1268
1278
1269
1279
std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t >>
1270
1280
swift::dependencies::performBatchModuleScan (
1271
- CompilerInstance &instance, ModuleDependenciesCache &cache,
1281
+ CompilerInstance &invocationInstance,
1282
+ ModuleDependenciesCache &invocationCache,
1283
+ CompilerArgInstanceCacheMap *versionedPCMInstanceCache,
1272
1284
llvm::StringSaver &saver, const std::vector<BatchScanInput> &batchInput) {
1273
1285
std::vector<llvm::ErrorOr<swiftscan_dependency_graph_t >> batchScanResult;
1274
1286
batchScanResult.reserve (batchInput.size ());
1275
1287
1276
1288
// Perform a full dependency scan for each batch entry module
1277
1289
forEachBatchEntry (
1278
- instance, cache, saver, batchInput,
1290
+ invocationInstance, invocationCache, versionedPCMInstanceCache, saver,
1291
+ batchInput,
1279
1292
[&batchScanResult](BatchScanInput entry, CompilerInstance &instance,
1280
1293
ModuleDependenciesCache &cache) {
1281
1294
StringRef moduleName = entry.moduleName ;
@@ -1343,7 +1356,7 @@ swift::dependencies::performBatchModulePrescan(
1343
1356
1344
1357
// Perform a full dependency scan for each batch entry module
1345
1358
forEachBatchEntry (
1346
- instance, cache, saver, batchInput,
1359
+ instance, cache, /* versionedPCMInstanceCache */ nullptr , saver, batchInput,
1347
1360
[&batchPrescanResult](BatchScanInput entry, CompilerInstance &instance,
1348
1361
ModuleDependenciesCache &cache) {
1349
1362
StringRef moduleName = entry.moduleName ;
0 commit comments