1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-11-22 02:33:06 +01:00

[mlir] Add a ThreadPool to MLIRContext and refactor MLIR threading usage

This revision refactors the usage of multithreaded utilities in MLIR to use a common
thread pool within the MLIR context, in addition to a new utility that makes writing
multi-threaded code in MLIR less error prone. Using a unified thread pool brings about
several advantages:

* Better thread usage and more control
We currently use the static llvm threading utilities, which do not allow multiple
levels of asynchronous scheduling (even if there are open threads). This is due to
how the current TaskGroup structure works, which only allows one truly multithreaded
instance at a time. By having our own ThreadPool we gain more control and flexibility
over our job/thread scheduling, and in a followup can enable threading more parts of
the compiler.

* The static nature of TaskGroup causes issues in certain configurations
Due to the static nature of TaskGroup, there have been quite a few problems related to
destruction that have caused several downstream projects to disable threading. See
D104207 for discussion on some related fallout. By having a ThreadPool scoped to
the context, we don't have to worry about destruction and can ensure that any
additional MLIR thread usage ends when the context is destroyed.

Differential Revision: https://reviews.llvm.org/D104516
This commit is contained in:
River Riddle 2021-06-23 01:16:55 +00:00
parent f2b70884ff
commit deae9b5f50
2 changed files with 11 additions and 0 deletions

View File

@ -70,6 +70,9 @@ public:
unsigned getThreadCount() const { return ThreadCount; }
/// Returns true if the current thread is a worker thread of this thread pool.
bool isWorkerThread() const;
private:
bool workCompletedUnlocked() { return !ActiveThreads && Tasks.empty(); }

View File

@ -72,6 +72,14 @@ void ThreadPool::wait() {
CompletionCondition.wait(LockGuard, [&] { return workCompletedUnlocked(); });
}
bool ThreadPool::isWorkerThread() const {
std::thread::id CurrentThreadId = std::this_thread::get_id();
for (const std::thread &Thread : Threads)
if (CurrentThreadId == Thread.get_id())
return true;
return false;
}
std::shared_future<void> ThreadPool::asyncImpl(TaskTy Task) {
/// Wrap the Task in a packaged_task to return a future object.
PackagedTaskTy PackagedTask(std::move(Task));