From e3b557809604d036af6e00c60f012c2025b59a5e Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Sat, 11 Feb 2023 13:38:04 +0100 Subject: Vendor import of llvm-project main llvmorg-16-init-18548-gb0daacf58f41, the last commit before the upstream release/17.x branch was created. --- llvm/lib/Support/Parallel.cpp | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) (limited to 'llvm/lib/Support/Parallel.cpp') diff --git a/llvm/lib/Support/Parallel.cpp b/llvm/lib/Support/Parallel.cpp index 798d7124e7e9..23ed9d813548 100644 --- a/llvm/lib/Support/Parallel.cpp +++ b/llvm/lib/Support/Parallel.cpp @@ -19,10 +19,18 @@ llvm::ThreadPoolStrategy llvm::parallel::strategy; -#if LLVM_ENABLE_THREADS - namespace llvm { namespace parallel { +#if LLVM_ENABLE_THREADS + +#ifdef _WIN32 +static thread_local unsigned threadIndex; + +unsigned getThreadIndex() { return threadIndex; } +#else +thread_local unsigned threadIndex; +#endif + namespace detail { namespace { @@ -96,6 +104,7 @@ public: private: void work(ThreadPoolStrategy S, unsigned ThreadID) { + threadIndex = ThreadID; S.apply_thread_strategy(ThreadID); while (true) { std::unique_lock Lock(Mutex); @@ -143,6 +152,8 @@ Executor *Executor::getDefaultExecutor() { return Exec.get(); } } // namespace +} // namespace detail +#endif static std::atomic TaskGroupInstances; @@ -159,21 +170,27 @@ TaskGroup::~TaskGroup() { } void TaskGroup::spawn(std::function F) { +#if LLVM_ENABLE_THREADS if (Parallel) { L.inc(); - Executor::getDefaultExecutor()->add([&, F = std::move(F)] { + detail::Executor::getDefaultExecutor()->add([&, F = std::move(F)] { F(); L.dec(); }); - } else { - F(); + return; } +#endif + F(); } -} // namespace detail +void TaskGroup::execute(std::function F) { + if (parallel::strategy.ThreadsRequested == 1) + F(); + else + spawn(F); +} } // namespace parallel } // namespace llvm -#endif // LLVM_ENABLE_THREADS void llvm::parallelFor(size_t Begin, size_t End, llvm::function_ref Fn) { @@ -190,7 +207,7 @@ void llvm::parallelFor(size_t Begin, size_t End, if (TaskSize == 0) TaskSize = 1; - parallel::detail::TaskGroup TG; + parallel::TaskGroup TG; for (; Begin + TaskSize < End; Begin += TaskSize) { TG.spawn([=, &Fn] { for (size_t I = Begin, E = Begin + TaskSize; I != E; ++I) -- cgit v1.2.3