Skip to content

Commit 0d25417

Browse files
authored
Merge pull request swiftlang#75146 from artemcm/DepScanNoGlobalLLVMOpts
[Dependency Scanning] Avoid configuration (and reset) of LLVM Options by the scanner
2 parents 156b0a9 + 6d0f521 commit 0d25417

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) {
@@ -411,8 +406,7 @@ DependencyScanningTool::initCompilerInstanceForScan(
411406
StringRef WorkingDir,
412407
std::shared_ptr<DependencyScanDiagnosticCollector> scannerDiagnosticsCollector) {
413408
// The remainder of this method operates on shared state in the
414-
// scanning service and global LLVM state with:
415-
// llvm::cl::ResetAllOptionOccurrences
409+
// scanning service
416410
llvm::sys::SmartScopedLock<true> Lock(DependencyScanningToolStateLock);
417411
// FIXME: Instead, target-info and supported-features queries must use
418412
// `DependencyScanningToolStateLock`, but this currently requires further
@@ -438,11 +432,6 @@ DependencyScanningTool::initCompilerInstanceForScan(
438432
if (WorkingDirectory.empty())
439433
llvm::sys::fs::current_path(WorkingDirectory);
440434

441-
// We must reset option occurrences because we are handling an unrelated
442-
// command-line to those possibly parsed before using the same tool.
443-
// We must do so because LLVM options parsing is done using a managed
444-
// static `GlobalParser`.
445-
llvm::cl::ResetAllOptionOccurrences();
446435
// Parse/tokenize arguments.
447436
std::string CommandString;
448437
for (const auto *c : CommandArgs) {

lib/DependencyScan/ScanDependencies.cpp

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

lib/Frontend/Frontend.cpp

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

693693
void CompilerInstance::setUpLLVMArguments() {
694-
// Honor -Xllvm.
695-
if (!Invocation.getFrontendOptions().LLVMArgs.empty()) {
696-
llvm::SmallVector<const char *, 4> Args;
697-
Args.push_back("swift (LLVM option parsing)");
698-
for (unsigned i = 0, e = Invocation.getFrontendOptions().LLVMArgs.size();
699-
i != e; ++i)
700-
Args.push_back(Invocation.getFrontendOptions().LLVMArgs[i].c_str());
701-
Args.push_back(nullptr);
702-
llvm::cl::ParseCommandLineOptions(Args.size()-1, Args.data());
694+
// Dependency scanning has no need for LLVM options, and
695+
// must not use `llvm::cl::` utilities operating on global state
696+
// since dependency scanning is multi-threaded.
697+
if (Invocation.getFrontendOptions().RequestedAction !=
698+
FrontendOptions::ActionType::ScanDependencies) {
699+
// Honor -Xllvm.
700+
if (!Invocation.getFrontendOptions().LLVMArgs.empty()) {
701+
llvm::SmallVector<const char *, 4> Args;
702+
Args.push_back("swift (LLVM option parsing)");
703+
for (unsigned i = 0, e = Invocation.getFrontendOptions().LLVMArgs.size();
704+
i != e; ++i)
705+
Args.push_back(Invocation.getFrontendOptions().LLVMArgs[i].c_str());
706+
Args.push_back(nullptr);
707+
llvm::cl::ParseCommandLineOptions(Args.size()-1, Args.data());
708+
}
703709
}
704710
}
705711

0 commit comments

Comments
 (0)