@@ -251,13 +251,6 @@ void SwiftDependencyScanningService::overlaySharedFilesystemCacheForCompilation(
251251 Instance.getSourceMgr ().setFileSystem (depFS);
252252}
253253
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- 
261254SwiftDependencyScanningService::ContextSpecificGlobalCacheState *
262255SwiftDependencyScanningService::getCacheForScanningContextHash (StringRef scanningContextHash) const  {
263256  auto  contextSpecificCache = ContextSpecificCacheMap.find (scanningContextHash);
@@ -269,8 +262,8 @@ SwiftDependencyScanningService::getCacheForScanningContextHash(StringRef scannin
269262
270263const  ModuleNameToDependencyMap &
271264SwiftDependencyScanningService::getDependenciesMap (
272-     ModuleDependencyKind kind) const  {
273-   auto  contextSpecificCache = getCurrentCache ( );
265+     ModuleDependencyKind kind, StringRef scanContextHash ) const  {
266+   auto  contextSpecificCache = getCacheForScanningContextHash (scanContextHash );
274267  auto  it = contextSpecificCache->ModuleDependenciesMap .find (kind);
275268  assert (it != contextSpecificCache->ModuleDependenciesMap .end () &&
276269         " invalid dependency kind"  );
@@ -279,40 +272,38 @@ SwiftDependencyScanningService::getDependenciesMap(
279272
280273ModuleNameToDependencyMap &
281274SwiftDependencyScanningService::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);
284278  auto  it = contextSpecificCache->ModuleDependenciesMap .find (kind);
285279  assert (it != contextSpecificCache->ModuleDependenciesMap .end () &&
286280         " invalid dependency kind"  );
287281  return  it->second ;
288282}
289283
290- void  SwiftDependencyScanningService::configureForContextHash (std::string  scanningContextHash) {
284+ void  SwiftDependencyScanningService::configureForContextHash (StringRef  scanningContextHash) {
291285  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.
297288    std::unique_ptr<ContextSpecificGlobalCacheState> contextSpecificCache =
298289        std::make_unique<ContextSpecificGlobalCacheState>();
299290    for  (auto  kind = ModuleDependencyKind::FirstKind;
300291         kind != ModuleDependencyKind::LastKind; ++kind) {
301292      contextSpecificCache->ModuleDependenciesMap .insert ({kind, ModuleNameToDependencyMap ()});
302293    }
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 ());
307297  }
308298}
309299
310300Optional<const  ModuleDependencyInfo*> SwiftDependencyScanningService::findDependency (
311-     StringRef moduleName, Optional<ModuleDependencyKind> kind) const  {
301+     StringRef moduleName, Optional<ModuleDependencyKind> kind,
302+     StringRef scanningContextHash) const  {
312303  if  (!kind) {
313304    for  (auto  kind = ModuleDependencyKind::FirstKind;
314305         kind != ModuleDependencyKind::LastKind; ++kind) {
315-       auto  dep = findDependency (moduleName, kind);
306+       auto  dep = findDependency (moduleName, kind, scanningContextHash );
316307      if  (dep.has_value ())
317308        return  dep.value ();
318309    }
@@ -324,7 +315,7 @@ Optional<const ModuleDependencyInfo*> SwiftDependencyScanningService::findDepend
324315    return  findSourceModuleDependency (moduleName);
325316  }
326317
327-   const  auto  &map = getDependenciesMap (kind.value ());
318+   const  auto  &map = getDependenciesMap (kind.value (), scanningContextHash );
328319  auto  known = map.find (moduleName);
329320  if  (known != map.end ())
330321    return  &(known->second );
@@ -343,44 +334,55 @@ SwiftDependencyScanningService::findSourceModuleDependency(
343334}
344335
345336bool  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 ();
348340}
349341
350342const  ModuleDependencyInfo *SwiftDependencyScanningService::recordDependency (
351-     StringRef moduleName, ModuleDependencyInfo dependencies) {
343+     StringRef moduleName, ModuleDependencyInfo dependencies,
344+     StringRef scanContextHash) {
352345  auto  kind = dependencies.getKind ();
353346  //  Source-based dependencies are recorded independently of the invocation's
354347  //  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));
363350
364351  //  All other dependencies are recorded according to the target triple of the
365352  //  scanning invocation that discovers them.
366-   auto  &map = getDependenciesMap (kind);
353+   auto  &map = getDependenciesMap (kind, scanContextHash );
367354  map.insert ({moduleName, dependencies});
368355  return  &(map[moduleName]);
369356}
370357
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+ 
371371const  ModuleDependencyInfo *SwiftDependencyScanningService::updateDependency (
372-     ModuleDependencyID moduleID, ModuleDependencyInfo dependencies) {
372+     ModuleDependencyID moduleID, ModuleDependencyInfo dependencies,
373+     StringRef scanningContextHash) {
373374  auto  kind = dependencies.getKind ();
374375  //  Source-based dependencies
375376  if  (kind == swift::ModuleDependencyKind::SwiftSource) {
377+     llvm::sys::SmartScopedLock<true > Lock (ScanningServiceGlobalLock);
376378    assert (SwiftSourceModuleDependenciesMap.count (moduleID.first ) == 1  &&
377379           " Attempting to update non-existing Swift Source dependency."  );
378380    auto  known = SwiftSourceModuleDependenciesMap.find (moduleID.first );
379381    known->second  = std::move (dependencies);
380382    return  &(known->second );
381383  }
382384
383-   auto  &map = getDependenciesMap (moduleID.second );
385+   auto  &map = getDependenciesMap (moduleID.second , scanningContextHash );
384386  auto  known = map.find (moduleID.first );
385387  assert (known != map.end () && " Not yet added to map"  );
386388  known->second  = std::move (dependencies);
@@ -422,9 +424,10 @@ ModuleDependenciesCache::ModuleDependenciesCache(
422424Optional<const  ModuleDependencyInfo*>
423425ModuleDependenciesCache::findDependency (
424426    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.
428431  if  (optionalDep.hasValue ()) {
429432    auto  dep = optionalDep.getValue ();
430433    if  (dep->getAsSwiftSourceModule () &&
@@ -446,7 +449,8 @@ void ModuleDependenciesCache::recordDependency(
446449    StringRef moduleName, ModuleDependencyInfo dependencies) {
447450  auto  dependenciesKind = dependencies.getKind ();
448451  const  ModuleDependencyInfo *recordedDependencies =
449-         globalScanningService.recordDependency (moduleName, dependencies);
452+         globalScanningService.recordDependency (moduleName, dependencies,
453+                                                scannerContextHash);
450454
451455  auto  &map = getDependencyReferencesMap (dependenciesKind);
452456  assert (map.count (moduleName) == 0  && " Already added to map"  );
@@ -455,7 +459,9 @@ void ModuleDependenciesCache::recordDependency(
455459
456460void  ModuleDependenciesCache::updateDependency (
457461    ModuleDependencyID moduleID, ModuleDependencyInfo dependencyInfo) {
458-   const  ModuleDependencyInfo *updatedDependencies = globalScanningService.updateDependency (moduleID, dependencyInfo);
462+   const  ModuleDependencyInfo *updatedDependencies =
463+     globalScanningService.updateDependency (moduleID, dependencyInfo,
464+                                            scannerContextHash);
459465  auto  &map = getDependencyReferencesMap (moduleID.second );
460466  auto  known = map.find (moduleID.first );
461467  if  (known != map.end ())
0 commit comments