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; });
100 void spawn(std::function<
void()> f);
102 void sync()
const { L.sync(); }
109#if LLVM_ENABLE_THREADS
113template <
class RandomAccessIterator,
class Comparator>
114RandomAccessIterator medianOf3(RandomAccessIterator Start,
115 RandomAccessIterator
End,
116 const Comparator &Comp) {
117 RandomAccessIterator Mid = Start + (std::distance(Start,
End) / 2);
118 return Comp(*Start, *(
End - 1))
119 ? (Comp(*Mid, *(
End - 1)) ? (Comp(*Start, *Mid) ? Mid : Start)
121 : (Comp(*Mid, *Start) ? (Comp(*(
End - 1), *Mid) ? Mid :
End - 1)
125template <
class RandomAccessIterator,
class Comparator>
126void parallel_quick_sort(RandomAccessIterator Start, RandomAccessIterator
End,
127 const Comparator &Comp, TaskGroup &TG,
size_t Depth) {
129 if (std::distance(Start,
End) < detail::MinParallelSize ||
Depth == 0) {
135 auto Pivot = medianOf3(Start,
End, Comp);
138 Pivot = std::partition(Start,
End - 1, [&Comp,
End](
decltype(*Start) V) {
139 return Comp(V, *(
End - 1));
145 TG.spawn([=, &Comp, &TG] {
146 parallel_quick_sort(Start, Pivot, Comp, TG,
Depth - 1);
148 parallel_quick_sort(Pivot + 1,
End, Comp, TG,
Depth - 1);
151template <
class RandomAccessIterator,
class Comparator>
152void parallel_sort(RandomAccessIterator Start, RandomAccessIterator
End,
153 const Comparator &Comp) {
155 parallel_quick_sort(Start,
End, Comp, TG,
163enum { MaxTasksPerGroup = 1024 };
165template <
class IterTy,
class ResultTy,
class ReduceFuncTy,
166 class TransformFuncTy>
167ResultTy parallel_transform_reduce(IterTy Begin, IterTy
End, ResultTy Init,
169 TransformFuncTy Transform) {
172 size_t NumInputs = std::distance(Begin,
End);
174 return std::move(Init);
175 size_t NumTasks = std::min(
static_cast<size_t>(MaxTasksPerGroup), NumInputs);
176 std::vector<ResultTy>
Results(NumTasks, Init);
182 size_t TaskSize = NumInputs / NumTasks;
183 size_t RemainingInputs = NumInputs % NumTasks;
184 IterTy TBegin = Begin;
185 for (
size_t TaskId = 0; TaskId < NumTasks; ++TaskId) {
186 IterTy TEnd = TBegin + TaskSize + (TaskId < RemainingInputs ? 1 : 0);
187 TG.spawn([=, &Transform, &Reduce, &
Results] {
190 for (IterTy It = TBegin; It != TEnd; ++It)
191 R = Reduce(R, Transform(*It));
202 ResultTy FinalResult = std::move(
Results.front());
203 for (ResultTy &PartialResult :
205 FinalResult = Reduce(FinalResult, std::move(PartialResult));
206 return std::move(FinalResult);
214template <
class RandomAccessIterator,
215 class Comparator = std::less<
216 typename std::iterator_traits<RandomAccessIterator>::value_type>>
218 const Comparator &Comp = Comparator()) {
219#if LLVM_ENABLE_THREADS
221 parallel::detail::parallel_sort(Start,
End, Comp);
228void parallelFor(
size_t Begin,
size_t End, function_ref<
void(
size_t)> Fn);
230template <
class IterTy,
class FuncTy>
235template <
class IterTy,
class ResultTy,
class ReduceFuncTy,
236 class TransformFuncTy>
239 TransformFuncTy Transform) {
240#if LLVM_ENABLE_THREADS
242 return parallel::detail::parallel_transform_reduce(Begin,
End,
Init, Reduce,
246 for (IterTy
I = Begin;
I !=
End; ++
I)
247 Init = Reduce(std::move(
Init), Transform(*
I));
248 return std::move(
Init);
252template <
class RangeTy,
253 class Comparator = std::less<
decltype(*std::begin(RangeTy()))>>
258template <
class RangeTy,
class FuncTy>
263template <
class RangeTy,
class ResultTy,
class ReduceFuncTy,
264 class TransformFuncTy>
267 TransformFuncTy Transform) {
273template <
class RangeTy,
class FuncTy>
285 [&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)
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.