From 390adc38fc112be360bd15499e5241bf4e675b6f Mon Sep 17 00:00:00 2001 From: Dimitry Andric Date: Thu, 27 Jan 2022 23:17:16 +0100 Subject: Merge llvm-project main llvmorg-14-init-17616-g024a1fab5c35 This updates llvm, clang, compiler-rt, libc++, libunwind, lld, lldb and openmp to llvmorg-14-init-17616-g024a1fab5c35. PR: 261742 MFC after: 2 weeks (cherry picked from commit 04eeddc0aa8e0a417a16eaf9d7d095207f4a8623) --- contrib/llvm-project/llvm/lib/Support/Parallel.cpp | 32 ++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'contrib/llvm-project/llvm/lib/Support/Parallel.cpp') diff --git a/contrib/llvm-project/llvm/lib/Support/Parallel.cpp b/contrib/llvm-project/llvm/lib/Support/Parallel.cpp index 71e3a1362f7e..4977c188f934 100644 --- a/contrib/llvm-project/llvm/lib/Support/Parallel.cpp +++ b/contrib/llvm-project/llvm/lib/Support/Parallel.cpp @@ -174,3 +174,35 @@ void TaskGroup::spawn(std::function F) { } // namespace parallel } // namespace llvm #endif // LLVM_ENABLE_THREADS + +void llvm::parallelForEachN(size_t Begin, size_t End, + llvm::function_ref Fn) { + // If we have zero or one items, then do not incur the overhead of spinning up + // a task group. They are surprisingly expensive, and because they do not + // support nested parallelism, a single entry task group can block parallel + // execution underneath them. +#if LLVM_ENABLE_THREADS + auto NumItems = End - Begin; + if (NumItems > 1 && parallel::strategy.ThreadsRequested != 1) { + // Limit the number of tasks to MaxTasksPerGroup to limit job scheduling + // overhead on large inputs. + auto TaskSize = NumItems / parallel::detail::MaxTasksPerGroup; + if (TaskSize == 0) + TaskSize = 1; + + parallel::detail::TaskGroup TG; + for (; Begin + TaskSize < End; Begin += TaskSize) { + TG.spawn([=, &Fn] { + for (size_t I = Begin, E = Begin + TaskSize; I != E; ++I) + Fn(I); + }); + } + for (; Begin != End; ++Begin) + Fn(Begin); + return; + } +#endif + + for (; Begin != End; ++Begin) + Fn(Begin); +} -- cgit v1.2.3