Skip to content

Commit cc78976

Browse files
[DependencyScanning] Use clang scanner callback to speed up bridging
Use the new callback function in clang scanner to transform the build command to speed up the build. Currently, there is quite some overhead to round-trip all the clang module build commands in order to transform them for swift builds using a single thread. The API allows cheap command-line transform through clang::CompilerInvocation interface.
1 parent 5dd244c commit cc78976

File tree

2 files changed

+19
-73
lines changed

2 files changed

+19
-73
lines changed

lib/AST/ModuleDependencies.cpp

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,17 @@ void ModuleDependencyInfo::setOutputPathAndHash(StringRef outputPath,
483483
}
484484
}
485485

486+
static void
487+
swiftScannerClangInvocationTransform(clang::CowCompilerInvocation &invocation) {
488+
invocation.getMutFrontendOpts().ModuleCacheKeys.clear();
489+
invocation.getMutFrontendOpts().PathPrefixMappings.clear();
490+
invocation.getMutFrontendOpts().OutputFile.clear();
491+
492+
// Reset CASOptions since that should be coming from swift.
493+
invocation.getMutCASOpts() = clang::CASOptions();
494+
invocation.getMutFrontendOpts().CASIncludeTreeID.clear();
495+
}
496+
486497
SwiftDependencyScanningService::SwiftDependencyScanningService() {
487498
ClangScanningService.emplace(
488499
clang::tooling::dependencies::ScanningMode::DependencyDirectivesScan,
@@ -496,7 +507,9 @@ SwiftDependencyScanningService::SwiftDependencyScanningService() {
496507
// the build system to handle the optimization safely.
497508
// Swift can handle the working directory optimizaiton
498509
// already so it is safe to turn on all optimizations.
499-
clang::tooling::dependencies::ScanningOptimizations::All);
510+
clang::tooling::dependencies::ScanningOptimizations::All,
511+
/*EagerLoadModules=*/false, /*TraceVFS=*/false,
512+
swiftScannerClangInvocationTransform);
500513
SharedFilesystemCache.emplace();
501514
}
502515

@@ -760,7 +773,9 @@ bool SwiftDependencyScanningService::setupCachingDependencyScanningService(
760773
// The current working directory optimization (off by default)
761774
// should not impact CAS. We set the optization to all to be
762775
// consistent with the non-CAS case.
763-
clang::tooling::dependencies::ScanningOptimizations::All);
776+
clang::tooling::dependencies::ScanningOptimizations::All,
777+
/*EagerLoadModules=*/false, /*TraceVFS=*/false,
778+
swiftScannerClangInvocationTransform);
764779

765780
return false;
766781
}

lib/ClangImporter/ClangModuleDependencyScanner.cpp

Lines changed: 2 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -81,25 +81,6 @@ std::vector<std::string> ClangImporter::getClangDepScanningInvocationArguments(
8181
return commandLineArgs;
8282
}
8383

84-
static std::unique_ptr<llvm::PrefixMapper>
85-
getClangPrefixMapper(DependencyScanningTool &clangScanningTool,
86-
ModuleDeps &clangModuleDep,
87-
clang::CompilerInvocation &depsInvocation) {
88-
std::unique_ptr<llvm::PrefixMapper> Mapper;
89-
if (clangModuleDep.IncludeTreeID) {
90-
Mapper = std::make_unique<llvm::PrefixMapper>();
91-
} else if (clangModuleDep.CASFileSystemRootID) {
92-
assert(clangScanningTool.getCachingFileSystem());
93-
Mapper = std::make_unique<llvm::TreePathPrefixMapper>(
94-
clangScanningTool.getCachingFileSystem());
95-
}
96-
97-
if (Mapper)
98-
DepscanPrefixMapping::configurePrefixMapper(depsInvocation, *Mapper);
99-
100-
return Mapper;
101-
}
102-
10384
ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies(
10485
const ASTContext &ctx,
10586
clang::tooling::dependencies::DependencyScanningTool &clangScanningTool,
@@ -156,54 +137,7 @@ ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies(
156137
}
157138

158139
// Add args reported by the scanner.
159-
160-
// Round-trip clang args to canonicalize and clear the options that swift
161-
// compiler doesn't need.
162-
clang::CompilerInvocation depsInvocation;
163-
clang::DiagnosticsEngine clangDiags(new clang::DiagnosticIDs(),
164-
new clang::DiagnosticOptions(),
165-
new clang::IgnoringDiagConsumer());
166-
167-
llvm::SmallVector<const char*> clangArgs;
168-
llvm::for_each(
169-
clangModuleDep.getBuildArguments(),
170-
[&](const std::string &Arg) { clangArgs.push_back(Arg.c_str()); });
171-
172-
bool success = clang::CompilerInvocation::CreateFromArgs(
173-
depsInvocation, clangArgs, clangDiags);
174-
(void)success;
175-
assert(success && "clang option from dep scanner round trip failed");
176-
177-
// Create a prefix mapper that matches clang's configuration.
178-
auto Mapper =
179-
getClangPrefixMapper(clangScanningTool, clangModuleDep, depsInvocation);
180-
181-
// Clear the cache key for module. The module key is computed from clang
182-
// invocation, not swift invocation.
183-
depsInvocation.getFrontendOpts().ModuleCacheKeys.clear();
184-
depsInvocation.getFrontendOpts().PathPrefixMappings.clear();
185-
depsInvocation.getFrontendOpts().OutputFile.clear();
186-
187-
// Reset CASOptions since that should be coming from swift.
188-
depsInvocation.getCASOpts() = clang::CASOptions();
189-
depsInvocation.getFrontendOpts().CASIncludeTreeID.clear();
190-
191-
// FIXME: workaround for rdar://105684525: find the -ivfsoverlay option
192-
// from clang scanner and pass to swift.
193-
for (auto overlay : depsInvocation.getHeaderSearchOpts().VFSOverlayFiles) {
194-
if (llvm::is_contained(ctx.SearchPathOpts.VFSOverlayFiles, overlay))
195-
continue;
196-
swiftArgs.push_back("-vfsoverlay");
197-
swiftArgs.push_back(overlay);
198-
}
199-
200-
llvm::BumpPtrAllocator allocator;
201-
llvm::StringSaver saver(allocator);
202-
clangArgs.clear();
203-
depsInvocation.generateCC1CommandLine(
204-
clangArgs,
205-
[&saver](const llvm::Twine &T) { return saver.save(T).data(); });
206-
140+
auto &clangArgs = clangModuleDep.getBuildArguments();
207141
llvm::for_each(clangArgs, addClangArg);
208142

209143
// CASFileSystemRootID.
@@ -227,10 +161,7 @@ ModuleDependencyVector ClangImporter::bridgeClangModuleDependencies(
227161
swiftArgs.push_back("-clang-include-tree-root");
228162
swiftArgs.push_back(IncludeTree);
229163
}
230-
231-
std::string mappedPCMPath = pcmPath;
232-
if (Mapper)
233-
Mapper->mapInPlace(mappedPCMPath);
164+
std::string mappedPCMPath = remapPath(pcmPath);
234165

235166
std::vector<LinkLibrary> LinkLibraries;
236167
for (const auto &ll : clangModuleDep.LinkLibraries)

0 commit comments

Comments
 (0)