17#ifndef MLIR_IR_THREADING_H
18#define MLIR_IR_THREADING_H
21#include "llvm/ADT/Sequence.h"
22#include "llvm/Support/ThreadPool.h"
41template <
typename IteratorT,
typename FuncT>
44 IteratorT end, FuncT &&
func) {
45 unsigned numElements =
static_cast<unsigned>(std::distance(begin, end));
52 for (; begin != end; ++begin)
53 if (failed(
func(*begin, 0)))
61 std::atomic<unsigned> curIndex(0);
62 std::atomic<bool> processingFailed(
false);
63 auto processFn = [&](
unsigned workerId) {
64 while (!processingFailed) {
65 unsigned index = curIndex++;
66 if (
index >= numElements)
69 if (failed(
func(*std::next(begin,
index), workerId)))
70 processingFailed =
true;
76 llvm::ThreadPoolInterface &threadPool = context->
getThreadPool();
77 llvm::ThreadPoolTaskGroup tasksGroup(threadPool);
78 size_t numActions = std::min(numElements, threadPool.getMaxConcurrency());
79 for (
unsigned i = 0; i < numActions; ++i)
80 tasksGroup.async([&, i] { processFn(i); });
85 return failure(processingFailed);
90template <
typename IteratorT,
typename FuncT>
92 IteratorT end, FuncT &&
func) {
94 context, begin, end, [&](
auto &&value,
unsigned) {
95 return func(std::forward<
decltype(value)>(value));
107template <
typename RangeT,
typename FuncT>
111 std::forward<FuncT>(
func));
122template <
typename FuncT>
124 size_t end, FuncT &&
func) {
126 std::forward<FuncT>(
func));
134template <
typename IteratorT,
typename FuncT>
138 return func(std::forward<
decltype(value)>(value)),
success();
147template <
typename RangeT,
typename FuncT>
150 std::forward<FuncT>(
func));
158template <
typename FuncT>
MLIRContext is the top-level object for a collection of MLIR operations.
llvm::ThreadPoolInterface & getThreadPool()
Return the thread pool used by this context.
bool isMultithreadingEnabled()
Return true if multi-threading is enabled by the context.
This class is a utility diagnostic handler for use when multi-threading some part of the compiler whe...
void eraseOrderIDForThread()
Remove the order id for the current thread.
void setOrderIDForThread(size_t orderID)
Set the order id for the current thread.
Include the generated interface declarations.
LogicalResult failableParallelForEachWithWorkerId(MLIRContext *context, IteratorT begin, IteratorT end, FuncT &&func)
Invoke the given function on the elements between [begin, end) asynchronously, passing each element a...
LogicalResult failableParallelForEach(MLIRContext *context, IteratorT begin, IteratorT end, FuncT &&func)
Invoke the given function on each element, with ordered diagnostics and early termination on failure.
void parallelForEach(MLIRContext *context, IteratorT begin, IteratorT end, FuncT &&func)
Invoke the given function on the elements between [begin, end) asynchronously.
void parallelFor(MLIRContext *context, size_t begin, size_t end, FuncT &&func)
Invoke the given function on the elements between [begin, end) asynchronously.
LogicalResult failableParallelForEachN(MLIRContext *context, size_t begin, size_t end, FuncT &&func)
Invoke the given function on the elements between [begin, end) asynchronously.