@@ -251,13 +251,6 @@ void SwiftDependencyScanningService::overlaySharedFilesystemCacheForCompilation(
251
251
Instance.getSourceMgr ().setFileSystem (depFS);
252
252
}
253
253
254
- SwiftDependencyScanningService::ContextSpecificGlobalCacheState *
255
- SwiftDependencyScanningService::getCurrentCache () const {
256
- assert (CurrentContextHash.has_value () &&
257
- " Global Module Dependencies Cache not configured with Triple." );
258
- return getCacheForScanningContextHash (CurrentContextHash.value ());
259
- }
260
-
261
254
SwiftDependencyScanningService::ContextSpecificGlobalCacheState *
262
255
SwiftDependencyScanningService::getCacheForScanningContextHash (StringRef scanningContextHash) const {
263
256
auto contextSpecificCache = ContextSpecificCacheMap.find (scanningContextHash);
@@ -269,8 +262,8 @@ SwiftDependencyScanningService::getCacheForScanningContextHash(StringRef scannin
269
262
270
263
const ModuleNameToDependencyMap &
271
264
SwiftDependencyScanningService::getDependenciesMap (
272
- ModuleDependencyKind kind) const {
273
- auto contextSpecificCache = getCurrentCache ( );
265
+ ModuleDependencyKind kind, StringRef scanContextHash ) const {
266
+ auto contextSpecificCache = getCacheForScanningContextHash (scanContextHash );
274
267
auto it = contextSpecificCache->ModuleDependenciesMap .find (kind);
275
268
assert (it != contextSpecificCache->ModuleDependenciesMap .end () &&
276
269
" invalid dependency kind" );
@@ -279,40 +272,38 @@ SwiftDependencyScanningService::getDependenciesMap(
279
272
280
273
ModuleNameToDependencyMap &
281
274
SwiftDependencyScanningService::getDependenciesMap (
282
- ModuleDependencyKind kind) {
283
- auto contextSpecificCache = getCurrentCache ();
275
+ ModuleDependencyKind kind, StringRef scanContextHash) {
276
+ llvm::sys::SmartScopedLock<true > Lock (ScanningServiceGlobalLock);
277
+ auto contextSpecificCache = getCacheForScanningContextHash (scanContextHash);
284
278
auto it = contextSpecificCache->ModuleDependenciesMap .find (kind);
285
279
assert (it != contextSpecificCache->ModuleDependenciesMap .end () &&
286
280
" invalid dependency kind" );
287
281
return it->second ;
288
282
}
289
283
290
- void SwiftDependencyScanningService::configureForContextHash (std::string scanningContextHash) {
284
+ void SwiftDependencyScanningService::configureForContextHash (StringRef scanningContextHash) {
291
285
auto knownContext = ContextSpecificCacheMap.find (scanningContextHash);
292
- if (knownContext != ContextSpecificCacheMap.end ()) {
293
- // Set the current context and leave the rest as-is
294
- CurrentContextHash = scanningContextHash;
295
- } else {
296
- // First time scanning with this triple, initialize target-specific state.
286
+ if (knownContext == ContextSpecificCacheMap.end ()) {
287
+ // First time scanning with this context, initialize context-specific state.
297
288
std::unique_ptr<ContextSpecificGlobalCacheState> contextSpecificCache =
298
289
std::make_unique<ContextSpecificGlobalCacheState>();
299
290
for (auto kind = ModuleDependencyKind::FirstKind;
300
291
kind != ModuleDependencyKind::LastKind; ++kind) {
301
292
contextSpecificCache->ModuleDependenciesMap .insert ({kind, ModuleNameToDependencyMap ()});
302
293
}
303
-
304
- ContextSpecificCacheMap.insert ({scanningContextHash, std::move (contextSpecificCache)});
305
- CurrentContextHash = scanningContextHash;
306
- AllContextHashes.push_back (scanningContextHash);
294
+ llvm::sys::SmartScopedLock<true > Lock (ScanningServiceGlobalLock);
295
+ ContextSpecificCacheMap.insert ({scanningContextHash.str (), std::move (contextSpecificCache)});
296
+ AllContextHashes.push_back (scanningContextHash.str ());
307
297
}
308
298
}
309
299
310
300
Optional<const ModuleDependencyInfo*> SwiftDependencyScanningService::findDependency (
311
- StringRef moduleName, Optional<ModuleDependencyKind> kind) const {
301
+ StringRef moduleName, Optional<ModuleDependencyKind> kind,
302
+ StringRef scanningContextHash) const {
312
303
if (!kind) {
313
304
for (auto kind = ModuleDependencyKind::FirstKind;
314
305
kind != ModuleDependencyKind::LastKind; ++kind) {
315
- auto dep = findDependency (moduleName, kind);
306
+ auto dep = findDependency (moduleName, kind, scanningContextHash );
316
307
if (dep.has_value ())
317
308
return dep.value ();
318
309
}
@@ -324,7 +315,7 @@ Optional<const ModuleDependencyInfo*> SwiftDependencyScanningService::findDepend
324
315
return findSourceModuleDependency (moduleName);
325
316
}
326
317
327
- const auto &map = getDependenciesMap (kind.value ());
318
+ const auto &map = getDependenciesMap (kind.value (), scanningContextHash );
328
319
auto known = map.find (moduleName);
329
320
if (known != map.end ())
330
321
return &(known->second );
@@ -343,44 +334,55 @@ SwiftDependencyScanningService::findSourceModuleDependency(
343
334
}
344
335
345
336
bool SwiftDependencyScanningService::hasDependency (
346
- StringRef moduleName, Optional<ModuleDependencyKind> kind) const {
347
- return findDependency (moduleName, kind).has_value ();
337
+ StringRef moduleName, Optional<ModuleDependencyKind> kind,
338
+ StringRef scanContextHash) const {
339
+ return findDependency (moduleName, kind, scanContextHash).has_value ();
348
340
}
349
341
350
342
const ModuleDependencyInfo *SwiftDependencyScanningService::recordDependency (
351
- StringRef moduleName, ModuleDependencyInfo dependencies) {
343
+ StringRef moduleName, ModuleDependencyInfo dependencies,
344
+ StringRef scanContextHash) {
352
345
auto kind = dependencies.getKind ();
353
346
// Source-based dependencies are recorded independently of the invocation's
354
347
// target triple.
355
- if (kind == swift::ModuleDependencyKind::SwiftSource) {
356
- assert (SwiftSourceModuleDependenciesMap.count (moduleName) == 0 &&
357
- " Attempting to record duplicate SwiftSource dependency." );
358
- SwiftSourceModuleDependenciesMap.insert (
359
- {moduleName, std::move (dependencies)});
360
- AllSourceModules.push_back ({moduleName.str (), kind});
361
- return &(SwiftSourceModuleDependenciesMap.find (moduleName)->second );
362
- }
348
+ if (kind == swift::ModuleDependencyKind::SwiftSource)
349
+ return recordSourceDependency (moduleName, std::move (dependencies));
363
350
364
351
// All other dependencies are recorded according to the target triple of the
365
352
// scanning invocation that discovers them.
366
- auto &map = getDependenciesMap (kind);
353
+ auto &map = getDependenciesMap (kind, scanContextHash );
367
354
map.insert ({moduleName, dependencies});
368
355
return &(map[moduleName]);
369
356
}
370
357
358
+ const ModuleDependencyInfo *SwiftDependencyScanningService::recordSourceDependency (
359
+ StringRef moduleName, ModuleDependencyInfo dependencies) {
360
+ llvm::sys::SmartScopedLock<true > Lock (ScanningServiceGlobalLock);
361
+ auto kind = dependencies.getKind ();
362
+ assert (kind == swift::ModuleDependencyKind::SwiftSource && " Expected source module dependncy info" );
363
+ assert (SwiftSourceModuleDependenciesMap.count (moduleName) == 0 &&
364
+ " Attempting to record duplicate SwiftSource dependency." );
365
+ SwiftSourceModuleDependenciesMap.insert (
366
+ {moduleName, std::move (dependencies)});
367
+ AllSourceModules.push_back ({moduleName.str (), kind});
368
+ return &(SwiftSourceModuleDependenciesMap.find (moduleName)->second );
369
+ }
370
+
371
371
const ModuleDependencyInfo *SwiftDependencyScanningService::updateDependency (
372
- ModuleDependencyID moduleID, ModuleDependencyInfo dependencies) {
372
+ ModuleDependencyID moduleID, ModuleDependencyInfo dependencies,
373
+ StringRef scanningContextHash) {
373
374
auto kind = dependencies.getKind ();
374
375
// Source-based dependencies
375
376
if (kind == swift::ModuleDependencyKind::SwiftSource) {
377
+ llvm::sys::SmartScopedLock<true > Lock (ScanningServiceGlobalLock);
376
378
assert (SwiftSourceModuleDependenciesMap.count (moduleID.first ) == 1 &&
377
379
" Attempting to update non-existing Swift Source dependency." );
378
380
auto known = SwiftSourceModuleDependenciesMap.find (moduleID.first );
379
381
known->second = std::move (dependencies);
380
382
return &(known->second );
381
383
}
382
384
383
- auto &map = getDependenciesMap (moduleID.second );
385
+ auto &map = getDependenciesMap (moduleID.second , scanningContextHash );
384
386
auto known = map.find (moduleID.first );
385
387
assert (known != map.end () && " Not yet added to map" );
386
388
known->second = std::move (dependencies);
@@ -422,9 +424,10 @@ ModuleDependenciesCache::ModuleDependenciesCache(
422
424
Optional<const ModuleDependencyInfo*>
423
425
ModuleDependenciesCache::findDependency (
424
426
StringRef moduleName, Optional<ModuleDependencyKind> kind) const {
425
- auto optionalDep = globalScanningService.findDependency (moduleName, kind);
426
- // During a scan, only produce the cached source module info for the current module
427
- // under scan.
427
+ auto optionalDep = globalScanningService.findDependency (moduleName, kind,
428
+ scannerContextHash);
429
+ // During a scan, only produce the cached source module info for the current
430
+ // module under scan.
428
431
if (optionalDep.hasValue ()) {
429
432
auto dep = optionalDep.getValue ();
430
433
if (dep->getAsSwiftSourceModule () &&
@@ -446,7 +449,8 @@ void ModuleDependenciesCache::recordDependency(
446
449
StringRef moduleName, ModuleDependencyInfo dependencies) {
447
450
auto dependenciesKind = dependencies.getKind ();
448
451
const ModuleDependencyInfo *recordedDependencies =
449
- globalScanningService.recordDependency (moduleName, dependencies);
452
+ globalScanningService.recordDependency (moduleName, dependencies,
453
+ scannerContextHash);
450
454
451
455
auto &map = getDependencyReferencesMap (dependenciesKind);
452
456
assert (map.count (moduleName) == 0 && " Already added to map" );
@@ -455,7 +459,9 @@ void ModuleDependenciesCache::recordDependency(
455
459
456
460
void ModuleDependenciesCache::updateDependency (
457
461
ModuleDependencyID moduleID, ModuleDependencyInfo dependencyInfo) {
458
- const ModuleDependencyInfo *updatedDependencies = globalScanningService.updateDependency (moduleID, dependencyInfo);
462
+ const ModuleDependencyInfo *updatedDependencies =
463
+ globalScanningService.updateDependency (moduleID, dependencyInfo,
464
+ scannerContextHash);
459
465
auto &map = getDependencyReferencesMap (moduleID.second );
460
466
auto known = map.find (moduleID.first );
461
467
if (known != map.end ())
0 commit comments