Skip to content

Commit

Permalink
[llvm-jitlink] Fix llvm-jitlink for LLVM_ENABLE_THREADS=Off.
Browse files Browse the repository at this point in the history
Commit edca1d9 enabled threaded linking by default in llvm-jitlink, but we
need to handle the case where LLVM is built with -DLLVM_ENABLE_THREADS=Off.

This patch updates the llvm-jitlink tool to switch back to materialization on
the main thread (equivalent to llvm-jitlink -num-threads=0 ...) when LLVM is
built without thread support.
  • Loading branch information
lhames committed Jan 2, 2025
1 parent bc87a53 commit 0c68155
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions llvm/tools/llvm-jitlink/llvm-jitlink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -996,9 +996,14 @@ Expected<std::unique_ptr<Session>> Session::Create(Triple TT,
std::unique_ptr<TaskDispatcher> Dispatcher;
if (MaterializationThreads == 0)
Dispatcher = std::make_unique<InPlaceTaskDispatcher>();
else
else {
#if LLVM_ENABLE_THREADS
Dispatcher = std::make_unique<DynamicThreadPoolTaskDispatcher>(
MaterializationThreads);
#else
llvm_unreachable("MaterializationThreads should be 0");
#endif
}

EPC = std::make_unique<SelfExecutorProcessControl>(
std::make_shared<SymbolStringPool>(), std::move(Dispatcher),
Expand Down Expand Up @@ -1628,15 +1633,24 @@ static Error sanitizeArguments(const Triple &TT, const char *ArgV0) {
}
}

#if LLVM_ENABLE_THREADS
if (MaterializationThreads == std::numeric_limits<size_t>::max()) {
if (auto HC = std::thread::hardware_concurrency())
MaterializationThreads = HC;
else {
errs() << "Warning: std::thread::hardware_concurrency() returned 0, "
"defaulting to -threads=1.\n";
"defaulting to -num-threads=1.\n";
MaterializationThreads = 1;
}
}
#else
if (MaterializationThreads.getNumOccurrences() &&
MaterializationThreads != 0) {
errs() << "Warning: -num-threads was set, but LLVM was built with threads "
"disabled. Resetting to -num-threads=0\n";
}
MaterializationThreads = 0;
#endif

if (!!OutOfProcessExecutor.getNumOccurrences() ||
!!OutOfProcessExecutorConnect.getNumOccurrences()) {
Expand Down

0 comments on commit 0c68155

Please sign in to comment.