diff options
author | Dimitry Andric <dim@FreeBSD.org> | 2020-07-26 19:36:28 +0000 |
---|---|---|
committer | Dimitry Andric <dim@FreeBSD.org> | 2020-07-26 19:36:28 +0000 |
commit | cfca06d7963fa0909f90483b42a6d7d194d01e08 (patch) | |
tree | 209fb2a2d68f8f277793fc8df46c753d31bc853b /llvm/lib/Support/Threading.cpp | |
parent | 706b4fc47bbc608932d3b491ae19a3b9cde9497b (diff) |
Notes
Diffstat (limited to 'llvm/lib/Support/Threading.cpp')
-rw-r--r-- | llvm/lib/Support/Threading.cpp | 67 |
1 files changed, 39 insertions, 28 deletions
diff --git a/llvm/lib/Support/Threading.cpp b/llvm/lib/Support/Threading.cpp index 48750cef5ec2..61f8ee5be5b3 100644 --- a/llvm/lib/Support/Threading.cpp +++ b/llvm/lib/Support/Threading.cpp @@ -45,10 +45,6 @@ void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData, Fn(UserData); } -unsigned llvm::heavyweight_hardware_concurrency() { return 1; } - -unsigned llvm::hardware_concurrency() { return 1; } - uint64_t llvm::get_threadid() { return 0; } uint32_t llvm::get_max_thread_name_length() { return 0; } @@ -57,6 +53,13 @@ void llvm::set_thread_name(const Twine &Name) {} void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); } +llvm::BitVector llvm::get_thread_affinity_mask() { return {}; } + +unsigned llvm::ThreadPoolStrategy::compute_thread_count() const { + // When threads are disabled, ensure clients will loop at least once. + return 1; +} + #if LLVM_ENABLE_THREADS == 0 void llvm::llvm_execute_on_thread_async( llvm::unique_function<void()> Func, @@ -78,30 +81,18 @@ void llvm::llvm_execute_on_thread_async( #else -#include <thread> -unsigned llvm::heavyweight_hardware_concurrency() { - // Since we can't get here unless LLVM_ENABLE_THREADS == 1, it is safe to use - // `std::thread` directly instead of `llvm::thread` (and indeed, doing so - // allows us to not define `thread` in the llvm namespace, which conflicts - // with some platforms such as FreeBSD whose headers also define a struct - // called `thread` in the global namespace which can cause ambiguity due to - // ADL. - int NumPhysical = sys::getHostNumPhysicalCores(); - if (NumPhysical == -1) - return std::thread::hardware_concurrency(); - return NumPhysical; -} - -unsigned llvm::hardware_concurrency() { -#if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT) - cpu_set_t Set; - if (sched_getaffinity(0, sizeof(Set), &Set)) - return CPU_COUNT(&Set); -#endif - // Guard against std::thread::hardware_concurrency() returning 0. - if (unsigned Val = std::thread::hardware_concurrency()) - return Val; - return 1; +int computeHostNumHardwareThreads(); + +unsigned llvm::ThreadPoolStrategy::compute_thread_count() const { + int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads() + : sys::getHostNumPhysicalCores(); + if (MaxThreadCount <= 0) + MaxThreadCount = 1; + if (ThreadsRequested == 0) + return MaxThreadCount; + if (!Limit) + return ThreadsRequested; + return std::min((unsigned)MaxThreadCount, ThreadsRequested); } namespace { @@ -140,3 +131,23 @@ void llvm::llvm_execute_on_thread_async( } #endif + +Optional<ThreadPoolStrategy> +llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) { + if (Num == "all") + return llvm::hardware_concurrency(); + if (Num.empty()) + return Default; + unsigned V; + if (Num.getAsInteger(10, V)) + return None; // malformed 'Num' value + if (V == 0) + return Default; + + // Do not take the Default into account. This effectively disables + // heavyweight_hardware_concurrency() if the user asks for any number of + // threads on the cmd-line. + ThreadPoolStrategy S = llvm::hardware_concurrency(); + S.ThreadsRequested = V; + return S; +} |