9#ifndef LLVM_SUPPORT_PARALLEL_H
10#define LLVM_SUPPORT_PARALLEL_H
13#include "llvm/Config/llvm-config.h"
19#include <condition_variable>
32#if LLVM_ENABLE_THREADS
33#define GET_THREAD_INDEX_IMPL \
34 if (parallel::strategy.ThreadsRequested == 1) \
36 assert((threadIndex != UINT_MAX) && \
37 "getThreadIndex() must be called from a thread created by " \
38 "ThreadPoolExecutor"); \
47extern thread_local unsigned threadIndex;
61 mutable std::mutex
Mutex;
62 mutable std::condition_variable Cond;
72 std::lock_guard<std::mutex> lock(
Mutex);
77 std::lock_guard<std::mutex> lock(
Mutex);
83 std::unique_lock<std::mutex> lock(
Mutex);
84 Cond.wait(lock, [&] {
return Count == 0; });
102 void spawn(std::function<
void()> f,
bool Sequential =
false);
104 void sync()
const { L.sync(); }
111#if LLVM_ENABLE_THREADS
115template <
class RandomAccessIterator,
class Comparator>
116RandomAccessIterator medianOf3(RandomAccessIterator Start,
117 RandomAccessIterator
End,
118 const Comparator &Comp) {
119 RandomAccessIterator Mid = Start + (std::distance(Start,
End) / 2);
120 return Comp(*Start, *(
End - 1))
121 ? (Comp(*Mid, *(
End - 1)) ? (Comp(*Start, *Mid) ? Mid : Start)
123 : (Comp(*Mid, *Start) ? (Comp(*(
End - 1), *Mid) ? Mid :
End - 1)
127template <
class RandomAccessIterator,
class Comparator>
128void parallel_quick_sort(RandomAccessIterator Start, RandomAccessIterator
End,
129 const Comparator &Comp, TaskGroup &TG,
size_t Depth) {
131 if (std::distance(Start,
End) < detail::MinParallelSize ||
Depth == 0) {
137 auto Pivot = medianOf3(Start,
End, Comp);
140 Pivot = std::partition(Start,
End - 1, [&Comp,
End](
decltype(*Start) V) {
141 return Comp(V, *(
End - 1));
147 TG.spawn([=, &Comp, &TG] {
148 parallel_quick_sort(Start, Pivot, Comp, TG,
Depth - 1);
150 parallel_quick_sort(Pivot + 1,
End, Comp, TG,
Depth - 1);
153template <
class RandomAccessIterator,
class Comparator>
154void parallel_sort(RandomAccessIterator Start, RandomAccessIterator
End,
155 const Comparator &Comp) {
157 parallel_quick_sort(Start,
End, Comp, TG,
165enum { MaxTasksPerGroup = 1024 };
167template <
class IterTy,
class ResultTy,
class ReduceFuncTy,
168 class TransformFuncTy>
169ResultTy parallel_transform_reduce(IterTy Begin, IterTy
End, ResultTy Init,
171 TransformFuncTy Transform) {
174 size_t NumInputs = std::distance(Begin,
End);
176 return std::move(Init);
177 size_t NumTasks = std::min(
static_cast<size_t>(MaxTasksPerGroup), NumInputs);
178 std::vector<ResultTy>
Results(NumTasks, Init);
184 size_t TaskSize = NumInputs / NumTasks;
185 size_t RemainingInputs = NumInputs % NumTasks;
186 IterTy TBegin = Begin;
187 for (
size_t TaskId = 0; TaskId < NumTasks; ++TaskId) {
188 IterTy TEnd = TBegin + TaskSize + (TaskId < RemainingInputs ? 1 : 0);
189 TG.spawn([=, &Transform, &Reduce, &
Results] {
192 for (IterTy It = TBegin; It != TEnd; ++It)
193 R = Reduce(R, Transform(*It));
204 ResultTy FinalResult = std::move(
Results.front());
205 for (ResultTy &PartialResult :
207 FinalResult = Reduce(FinalResult, std::move(PartialResult));
208 return std::move(FinalResult);
216template <
class RandomAccessIterator,
217 class Comparator = std::less<
218 typename std::iterator_traits<RandomAccessIterator>::value_type>>
220 const Comparator &Comp = Comparator()) {
221#if LLVM_ENABLE_THREADS
223 parallel::detail::parallel_sort(Start,
End, Comp);
230void parallelFor(
size_t Begin,
size_t End, function_ref<
void(
size_t)> Fn);
232template <
class IterTy,
class FuncTy>
237template <
class IterTy,
class ResultTy,
class ReduceFuncTy,
238 class TransformFuncTy>
241 TransformFuncTy Transform) {
242#if LLVM_ENABLE_THREADS
244 return parallel::detail::parallel_transform_reduce(Begin,
End,
Init, Reduce,
248 for (IterTy
I = Begin;
I !=
End; ++
I)
249 Init = Reduce(std::move(
Init), Transform(*
I));
250 return std::move(
Init);
254template <
class RangeTy,
255 class Comparator = std::less<
decltype(*std::begin(RangeTy()))>>
260template <
class RangeTy,
class FuncTy>
265template <
class RangeTy,
class ResultTy,
class ReduceFuncTy,
266 class TransformFuncTy>
269 TransformFuncTy Transform) {
275template <
class RangeTy,
class FuncTy>
287 [&Fn](
auto &&V) {
return wrap(Fn(V)); }));
Function Alias Analysis Results
const SmallVectorImpl< MachineOperand > & Cond
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
This tells how a thread pool will be used.
void spawn(std::function< void()> f, bool Sequential=false)
struct LLVMOpaqueError * LLVMErrorRef
Opaque reference to an error instance.
ThreadPoolStrategy strategy
unsigned getThreadIndex()
This is an optimization pass for GlobalISel generic memory operations.
unsigned Log2_64(uint64_t Value)
Return the floor log base 2 of the specified value, -1 if the value is zero.
void parallelSort(RandomAccessIterator Start, RandomAccessIterator End, const Comparator &Comp=Comparator())
Error joinErrors(Error E1, Error E2)
Concatenate errors.
void sort(IteratorTy Start, IteratorTy End)
MutableArrayRef(T &OneElt) -> MutableArrayRef< T >
Attribute unwrap(LLVMAttributeRef Attr)
LLVMAttributeRef wrap(Attribute Attr)
void parallelFor(size_t Begin, size_t End, function_ref< void(size_t)> Fn)
ResultTy parallelTransformReduce(IterTy Begin, IterTy End, ResultTy Init, ReduceFuncTy Reduce, TransformFuncTy Transform)
void parallelForEach(IterTy Begin, IterTy End, FuncTy Fn)
Error parallelForEachError(RangeTy &&R, FuncTy Fn)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.