Skip to content

Commit 899e785

Browse files
committed
[Dependency Scanning] Avoid configuration (and reset) of LLVM Options by the scanner
The scanning action does not have any need for handling `-llvm` options, since it will never perform any code-gen. LLVM option processing relies on global option parsing structures, and the scanner has needed to carefully attempt to synchronize access to them. This change guards the configuration of LLVM options to not happen at all for dependency scanning actions, and removes calls to `llvm::cl::ResetAllOptionOccurrences()` that were previously needed. Resolves rdar://120754696
1 parent 6741653 commit 899e785

File tree

3 files changed

+16
-26
lines changed

3 files changed

+16
-26
lines changed

lib/DependencyScan/DependencyScanningTool.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,6 @@ llvm::ErrorOr<swiftscan_string_ref_t> getTargetInfo(ArrayRef<const char *> Comma
3535
const char *main_executable_path) {
3636
llvm::sys::SmartScopedLock<true> Lock(TargetInfoMutex);
3737

38-
// We must reset option occurrences because we are handling an unrelated
39-
// command-line to those possibly parsed before using the same tool.
40-
// We must do so because LLVM options parsing is done using a managed
41-
// static `GlobalParser`.
42-
llvm::cl::ResetAllOptionOccurrences();
4338
// Parse arguments.
4439
std::string CommandString;
4540
for (const auto *c : Command) {
@@ -267,8 +262,7 @@ llvm::ErrorOr<ScanQueryInstance>
267262
DependencyScanningTool::initScannerForAction(
268263
ArrayRef<const char *> Command, StringRef WorkingDirectory) {
269264
// The remainder of this method operates on shared state in the
270-
// scanning service and global LLVM state with:
271-
// llvm::cl::ResetAllOptionOccurrences
265+
// scanning service
272266
llvm::sys::SmartScopedLock<true> Lock(DependencyScanningToolStateLock);
273267
return initCompilerInstanceForScan(Command, WorkingDirectory);
274268
}
@@ -296,11 +290,6 @@ DependencyScanningTool::initCompilerInstanceForScan(
296290
if (WorkingDirectory.empty())
297291
llvm::sys::fs::current_path(WorkingDirectory);
298292

299-
// We must reset option occurrences because we are handling an unrelated
300-
// command-line to those possibly parsed before using the same tool.
301-
// We must do so because LLVM options parsing is done using a managed
302-
// static `GlobalParser`.
303-
llvm::cl::ResetAllOptionOccurrences();
304293
// Parse/tokenize arguments.
305294
std::string CommandString;
306295
for (const auto *c : CommandArgs) {

lib/DependencyScan/ScanDependencies.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1136,11 +1136,6 @@ forEachBatchEntry(CompilerInstance &invocationInstance,
11361136
// those of the current scanner invocation.
11371137
updateCachedInstanceOpts(*pInstance, invocationInstance, entry.arguments);
11381138
} else {
1139-
// We must reset option occurrences because we are handling an unrelated command-line
1140-
// to those parsed before. We must do so because LLVM options parsing is done
1141-
// using a managed static `GlobalParser`.
1142-
llvm::cl::ResetAllOptionOccurrences();
1143-
11441139
// Create a new instance by the arguments and save it in the map.
11451140
auto newService = std::make_unique<SwiftDependencyScanningService>();
11461141
auto newInstance = std::make_unique<CompilerInstance>();

lib/Frontend/Frontend.cpp

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,21 @@ bool CompilerInstance::setUpVirtualFileSystemOverlays() {
659659
}
660660

661661
void CompilerInstance::setUpLLVMArguments() {
662-
// Honor -Xllvm.
663-
if (!Invocation.getFrontendOptions().LLVMArgs.empty()) {
664-
llvm::SmallVector<const char *, 4> Args;
665-
Args.push_back("swift (LLVM option parsing)");
666-
for (unsigned i = 0, e = Invocation.getFrontendOptions().LLVMArgs.size();
667-
i != e; ++i)
668-
Args.push_back(Invocation.getFrontendOptions().LLVMArgs[i].c_str());
669-
Args.push_back(nullptr);
670-
llvm::cl::ParseCommandLineOptions(Args.size()-1, Args.data());
662+
// Dependency scanning has no need for LLVM options, and
663+
// must not use `llvm::cl::` utilities operating on global state
664+
// since dependency scanning is multi-threaded.
665+
if (Invocation.getFrontendOptions().RequestedAction !=
666+
FrontendOptions::ActionType::ScanDependencies) {
667+
// Honor -Xllvm.
668+
if (!Invocation.getFrontendOptions().LLVMArgs.empty()) {
669+
llvm::SmallVector<const char *, 4> Args;
670+
Args.push_back("swift (LLVM option parsing)");
671+
for (unsigned i = 0, e = Invocation.getFrontendOptions().LLVMArgs.size();
672+
i != e; ++i)
673+
Args.push_back(Invocation.getFrontendOptions().LLVMArgs[i].c_str());
674+
Args.push_back(nullptr);
675+
llvm::cl::ParseCommandLineOptions(Args.size()-1, Args.data());
676+
}
671677
}
672678
}
673679

0 commit comments

Comments
 (0)