Skip to content

Commit 87e7c3a

Browse files
committed
Move generation of a named import path 'Identifier' out of the individual scanning workers up into the parent scanner. This operation mutates the scanner ASTContext by potentially adding new identifiers to it and is therefore not thread-safe.
1 parent ff63a90 commit 87e7c3a

File tree

9 files changed

+57
-33
lines changed

9 files changed

+57
-33
lines changed

include/swift/AST/ModuleLoader.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,8 @@ class ModuleLoader {
341341
/// Retrieve the dependencies for the given, named module, or \c None
342342
/// if no such module exists.
343343
virtual llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
344-
getModuleDependencies(StringRef moduleName,
344+
getModuleDependencies(ImportPath::Module namedImport,
345+
StringRef moduleName,
345346
StringRef moduleOutputPath,
346347
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
347348
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,

include/swift/ClangImporter/ClangImporter.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ class ClangImporter final : public ClangModuleLoader {
440440
StringRef moduleOutputPath, RemapPathCallback remapPath = nullptr);
441441

442442
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
443-
getModuleDependencies(StringRef moduleName, StringRef moduleOutputPath,
443+
getModuleDependencies(ImportPath::Module namedImport, StringRef moduleName, StringRef moduleOutputPath,
444444
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
445445
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
446446
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,

include/swift/DependencyScan/ModuleDependencyScanner.h

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,21 @@ class ModuleDependencyScanningWorker {
3636
private:
3737
/// Retrieve the module dependencies for the module with the given name.
3838
ModuleDependencyVector
39-
scanFilesystemForModuleDependency(StringRef moduleName,
39+
scanFilesystemForModuleDependency(ImportPath::Module namedImport,
40+
StringRef moduleName,
4041
const ModuleDependenciesCache &cache,
4142
bool isTestableImport = false);
4243

4344
/// Retrieve the module dependencies for the Clang module with the given name.
4445
ModuleDependencyVector
45-
scanFilesystemForClangModuleDependency(StringRef moduleName,
46+
scanFilesystemForClangModuleDependency(ImportPath::Module namedImport,
47+
StringRef moduleName,
4648
const ModuleDependenciesCache &cache);
4749

4850
/// Retrieve the module dependencies for the Swift module with the given name.
4951
ModuleDependencyVector
50-
scanFilesystemForSwiftModuleDependency(StringRef moduleName,
52+
scanFilesystemForSwiftModuleDependency(ImportPath::Module namedImport,
53+
StringRef moduleName,
5154
const ModuleDependenciesCache &cache);
5255

5356
// An AST delegate for interface scanning.
@@ -135,6 +138,9 @@ class ModuleDependencyScanner {
135138
template <typename Function, typename... Args>
136139
auto withDependencyScanningWorker(Function &&F, Args &&...ArgList);
137140

141+
/// Generate a full named import path
142+
ImportPath::Module getModuleImportIdentifier(StringRef moduleName);
143+
138144
private:
139145
const CompilerInvocation &ScanCompilerInvocation;
140146
ASTContext &ScanASTContext;

include/swift/Sema/SourceLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ class SourceLoader : public ModuleLoader {
9898
}
9999

100100
llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
101-
getModuleDependencies(StringRef moduleName, StringRef moduleOutputPath,
101+
getModuleDependencies(ImportPath::Module namedImport, StringRef moduleName, StringRef moduleOutputPath,
102102
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
103103
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
104104
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,

include/swift/Serialization/SerializedModuleLoader.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ class SerializedModuleLoaderBase : public ModuleLoader {
250250
virtual void verifyAllModules() override;
251251

252252
virtual llvm::SmallVector<std::pair<ModuleDependencyID, ModuleDependencyInfo>, 1>
253-
getModuleDependencies(StringRef moduleName, StringRef moduleOutputPath,
253+
getModuleDependencies(ImportPath::Module namedImport, StringRef moduleName, StringRef moduleOutputPath,
254254
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
255255
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,
256256
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,8 @@ computeClangWorkingDirectory(const std::vector<std::string> &commandLineArgs,
407407
}
408408

409409
ModuleDependencyVector
410-
ClangImporter::getModuleDependencies(StringRef moduleName,
410+
ClangImporter::getModuleDependencies(ImportPath::Module namedImport,
411+
StringRef moduleName,
411412
StringRef moduleOutputPath,
412413
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
413414
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,

lib/DependencyScan/ModuleDependencyScanner.cpp

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -179,20 +179,20 @@ ModuleDependencyScanningWorker::ModuleDependencyScanningWorker(
179179

180180
ModuleDependencyVector
181181
ModuleDependencyScanningWorker::scanFilesystemForModuleDependency(
182-
StringRef moduleName, const ModuleDependenciesCache &cache,
182+
ImportPath::Module namedImport, StringRef moduleName, const ModuleDependenciesCache &cache,
183183
bool isTestableImport) {
184184
// First query a Swift module, otherwise lookup a Clang module
185185
ModuleDependencyVector moduleDependencies =
186186
swiftScannerModuleLoader->getModuleDependencies(
187-
moduleName, cache.getModuleOutputPath(),
187+
namedImport, moduleName, cache.getModuleOutputPath(),
188188
cache.getScanService().getCachingFS(),
189189
cache.getAlreadySeenClangModules(), clangScanningTool,
190190
*ScanningASTDelegate, cache.getScanService().getPrefixMapper(),
191191
isTestableImport);
192192

193193
if (moduleDependencies.empty())
194194
moduleDependencies = clangScannerModuleLoader->getModuleDependencies(
195-
moduleName, cache.getModuleOutputPath(),
195+
namedImport, moduleName, cache.getModuleOutputPath(),
196196
cache.getScanService().getCachingFS(),
197197
cache.getAlreadySeenClangModules(), clangScanningTool,
198198
*ScanningASTDelegate, cache.getScanService().getPrefixMapper(),
@@ -203,19 +203,19 @@ ModuleDependencyScanningWorker::scanFilesystemForModuleDependency(
203203

204204
ModuleDependencyVector
205205
ModuleDependencyScanningWorker::scanFilesystemForSwiftModuleDependency(
206-
StringRef moduleName, const ModuleDependenciesCache &cache) {
206+
ImportPath::Module namedImport, StringRef moduleName, const ModuleDependenciesCache &cache) {
207207
return swiftScannerModuleLoader->getModuleDependencies(
208-
moduleName, cache.getModuleOutputPath(),
208+
namedImport, moduleName, cache.getModuleOutputPath(),
209209
cache.getScanService().getCachingFS(), cache.getAlreadySeenClangModules(),
210210
clangScanningTool, *ScanningASTDelegate,
211211
cache.getScanService().getPrefixMapper(), false);
212212
}
213213

214214
ModuleDependencyVector
215215
ModuleDependencyScanningWorker::scanFilesystemForClangModuleDependency(
216-
StringRef moduleName, const ModuleDependenciesCache &cache) {
216+
ImportPath::Module namedImport, StringRef moduleName, const ModuleDependenciesCache &cache) {
217217
return clangScannerModuleLoader->getModuleDependencies(
218-
moduleName, cache.getModuleOutputPath(),
218+
namedImport, moduleName, cache.getModuleOutputPath(),
219219
cache.getScanService().getCachingFS(), cache.getAlreadySeenClangModules(),
220220
clangScanningTool, *ScanningASTDelegate,
221221
cache.getScanService().getPrefixMapper(), false);
@@ -252,6 +252,11 @@ auto ModuleDependencyScanner::withDependencyScanningWorker(Function &&F,
252252
return result;
253253
}
254254

255+
ImportPath::Module ModuleDependencyScanner::getModuleImportIdentifier(StringRef moduleName) {
256+
ImportPath::Module::Builder builder(ScanASTContext, moduleName, /*separator=*/'.');
257+
return builder.get();
258+
}
259+
255260
ModuleDependencyScanner::ModuleDependencyScanner(
256261
SwiftDependencyScanningService &ScanningService,
257262
const CompilerInvocation &ScanCompilerInvocation,
@@ -443,10 +448,11 @@ ModuleDependencyScanner::getNamedClangModuleDependencyInfo(
443448
return found;
444449

445450
// Otherwise perform filesystem scan
451+
auto namedImportIdentifier = getModuleImportIdentifier(moduleName);
446452
auto moduleDependencies = withDependencyScanningWorker(
447-
[&cache, moduleName](ModuleDependencyScanningWorker *ScanningWorker) {
453+
[&cache, namedImportIdentifier, moduleName](ModuleDependencyScanningWorker *ScanningWorker) {
448454
return ScanningWorker->scanFilesystemForClangModuleDependency(
449-
moduleName, cache);
455+
namedImportIdentifier, moduleName, cache);
450456
});
451457
if (moduleDependencies.empty())
452458
return llvm::None;
@@ -473,11 +479,14 @@ ModuleDependencyScanner::getNamedSwiftModuleDependencyInfo(
473479
ModuleDependencyKind::SwiftPlaceholder))
474480
return found;
475481

482+
ImportPath::Module::Builder builder(ScanASTContext, moduleName, /*separator=*/'.');
483+
auto modulePath = builder.get();
476484
// Otherwise perform filesystem scan
485+
auto namedImportIdentifier = getModuleImportIdentifier(moduleName);
477486
auto moduleDependencies = withDependencyScanningWorker(
478-
[&cache, moduleName](ModuleDependencyScanningWorker *ScanningWorker) {
487+
[&cache, namedImportIdentifier, moduleName](ModuleDependencyScanningWorker *ScanningWorker) {
479488
return ScanningWorker->scanFilesystemForSwiftModuleDependency(
480-
moduleName, cache);
489+
namedImportIdentifier, moduleName, cache);
481490
});
482491
if (moduleDependencies.empty())
483492
return llvm::None;
@@ -552,6 +561,7 @@ void ModuleDependencyScanner::resolveImportDependencies(
552561
// A scanning task to query a module by-name. If the module already exists
553562
// in the cache, do nothing and return.
554563
auto scanForModuleDependency = [this, &cache, &moduleLookupResult](
564+
ImportPath::Module namedImportIdentifier,
555565
StringRef moduleName, bool onlyClangModule,
556566
bool isTestable) {
557567
// If this is already in the cache, no work to do here
@@ -564,13 +574,13 @@ void ModuleDependencyScanner::resolveImportDependencies(
564574
}
565575

566576
auto moduleDependencies = withDependencyScanningWorker(
567-
[&cache, moduleName, onlyClangModule,
577+
[&cache, namedImportIdentifier, moduleName, onlyClangModule,
568578
isTestable](ModuleDependencyScanningWorker *ScanningWorker) {
569579
return onlyClangModule
570580
? ScanningWorker->scanFilesystemForClangModuleDependency(
571-
moduleName, cache)
581+
namedImportIdentifier, moduleName, cache)
572582
: ScanningWorker->scanFilesystemForModuleDependency(
573-
moduleName, cache, isTestable);
583+
namedImportIdentifier, moduleName, cache, isTestable);
574584
});
575585
moduleLookupResult.insert_or_assign(moduleName, moduleDependencies);
576586
};
@@ -579,14 +589,16 @@ void ModuleDependencyScanner::resolveImportDependencies(
579589
for (const auto &dependsOn : moduleDependencyInfo->getModuleImports()) {
580590
bool underlyingClangModuleLookup = moduleID.ModuleName == dependsOn;
581591
bool isTestable = moduleDependencyInfo->isTestableImport(dependsOn);
582-
ScanningThreadPool.async(scanForModuleDependency, dependsOn,
592+
ScanningThreadPool.async(scanForModuleDependency,
593+
getModuleImportIdentifier(dependsOn), dependsOn,
583594
underlyingClangModuleLookup, isTestable);
584595
}
585596
for (const auto &dependsOn :
586597
moduleDependencyInfo->getOptionalModuleImports()) {
587598
bool underlyingClangModuleLookup = moduleID.ModuleName == dependsOn;
588599
bool isTestable = moduleDependencyInfo->isTestableImport(dependsOn);
589-
ScanningThreadPool.async(scanForModuleDependency, dependsOn,
600+
ScanningThreadPool.async(scanForModuleDependency,
601+
getModuleImportIdentifier(dependsOn), dependsOn,
590602
underlyingClangModuleLookup, isTestable);
591603
}
592604
ScanningThreadPool.wait();
@@ -726,23 +738,28 @@ void ModuleDependencyScanner::resolveSwiftOverlayDependencies(
726738
// A scanning task to query a Swift module by-name. If the module already
727739
// exists in the cache, do nothing and return.
728740
auto scanForSwiftDependency = [this, &cache, &swiftOverlayLookupResult](
741+
ImportPath::Module namedImportIdentifier,
729742
StringRef moduleName) {
730743
if (cache.hasDependency(moduleName, ModuleDependencyKind::SwiftInterface) ||
731744
cache.hasDependency(moduleName, ModuleDependencyKind::SwiftBinary) ||
732745
cache.hasDependency(moduleName, ModuleDependencyKind::SwiftPlaceholder))
733746
return;
734747

735748
auto moduleDependencies = withDependencyScanningWorker(
736-
[&cache, moduleName](ModuleDependencyScanningWorker *ScanningWorker) {
737-
return ScanningWorker->scanFilesystemForSwiftModuleDependency(moduleName,
749+
[&cache, namedImportIdentifier, moduleName](ModuleDependencyScanningWorker *ScanningWorker) {
750+
return ScanningWorker->scanFilesystemForSwiftModuleDependency(namedImportIdentifier,
751+
moduleName,
738752
cache);
739753
});
740754
swiftOverlayLookupResult.insert_or_assign(moduleName, moduleDependencies);
741755
};
742756

743757
// Enque asynchronous lookup tasks
744758
for (const auto &clangDep : clangDependencies)
745-
ScanningThreadPool.async(scanForSwiftDependency, clangDep);
759+
ScanningThreadPool.async(scanForSwiftDependency,
760+
getModuleImportIdentifier(clangDep),
761+
clangDep);
762+
746763
ScanningThreadPool.wait();
747764

748765
// Aggregate both previously-cached and freshly-scanned module results

lib/Sema/SourceLoader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ void SourceLoader::loadExtensions(NominalTypeDecl *nominal,
155155
}
156156

157157
ModuleDependencyVector
158-
SourceLoader::getModuleDependencies(StringRef moduleName,
158+
SourceLoader::getModuleDependencies(ImportPath::Module namedImport,
159+
StringRef moduleName,
159160
StringRef moduleOutputPath,
160161
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
161162
const llvm::DenseSet<clang::tooling::dependencies::ModuleID> &alreadySeenClangModules,

lib/Serialization/ScanningLoaders.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -305,16 +305,14 @@ SwiftModuleScanner::scanInterfaceFile(Twine moduleInterfacePath,
305305
}
306306

307307
ModuleDependencyVector SerializedModuleLoaderBase::getModuleDependencies(
308-
StringRef moduleName, StringRef moduleOutputPath,
308+
ImportPath::Module namedImport, StringRef moduleName, StringRef moduleOutputPath,
309309
llvm::IntrusiveRefCntPtr<llvm::cas::CachingOnDiskFileSystem> CacheFS,
310310
const llvm::DenseSet<clang::tooling::dependencies::ModuleID>
311311
&alreadySeenClangModules,
312312
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
313313
InterfaceSubContextDelegate &delegate, llvm::TreePathPrefixMapper *mapper,
314314
bool isTestableDependencyLookup) {
315-
ImportPath::Module::Builder builder(Ctx, moduleName, /*separator=*/'.');
316-
auto modulePath = builder.get();
317-
auto moduleId = modulePath.front().Item;
315+
auto moduleId = namedImport.front().Item;
318316
llvm::Optional<SwiftDependencyTracker> tracker = llvm::None;
319317
if (CacheFS)
320318
tracker = SwiftDependencyTracker(*CacheFS, mapper);
@@ -339,7 +337,7 @@ ModuleDependencyVector SerializedModuleLoaderBase::getModuleDependencies(
339337
"Expected PlaceholderSwiftModuleScanner as the first dependency "
340338
"scanner loader.");
341339
for (auto &scanner : scanners) {
342-
if (scanner->canImportModule(modulePath, nullptr,
340+
if (scanner->canImportModule(namedImport, nullptr,
343341
isTestableDependencyLookup)) {
344342

345343
ModuleDependencyVector moduleDependnecies;

0 commit comments

Comments
 (0)