LLVM 20.0.0git
TaskDispatch.h
Go to the documentation of this file.
1//===--------- TaskDispatch.h - ORC task dispatch utils ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// Task and TaskDispatch classes.
10//
11//===----------------------------------------------------------------------===//
12
13#ifndef LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
14#define LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
15
16#include "llvm/Config/llvm-config.h"
17#include "llvm/Support/Debug.h"
20
21#include <cassert>
22#include <string>
23
24#if LLVM_ENABLE_THREADS
25#include <condition_variable>
26#include <deque>
27#include <mutex>
28#include <thread>
29#endif
30
31namespace llvm {
32namespace orc {
33
34/// Represents an abstract task for ORC to run.
35class Task : public RTTIExtends<Task, RTTIRoot> {
36public:
37 static char ID;
38
39 virtual ~Task() = default;
40
41 /// Description of the task to be performed. Used for logging.
42 virtual void printDescription(raw_ostream &OS) = 0;
43
44 /// Run the task.
45 virtual void run() = 0;
46
47private:
48 void anchor() override;
49};
50
51/// Base class for generic tasks.
52class GenericNamedTask : public RTTIExtends<GenericNamedTask, Task> {
53public:
54 static char ID;
55 static const char *DefaultDescription;
56};
57
58/// Generic task implementation.
59template <typename FnT> class GenericNamedTaskImpl : public GenericNamedTask {
60public:
61 GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
62 : Fn(std::forward<FnT>(Fn)), Desc(DescBuffer.c_str()),
63 DescBuffer(std::move(DescBuffer)) {}
64 GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
65 : Fn(std::forward<FnT>(Fn)), Desc(Desc) {
66 assert(Desc && "Description cannot be null");
67 }
68 void printDescription(raw_ostream &OS) override { OS << Desc; }
69 void run() override { Fn(); }
70
71private:
72 FnT Fn;
73 const char *Desc;
74 std::string DescBuffer;
75};
76
77/// Create a generic named task from a std::string description.
78template <typename FnT>
79std::unique_ptr<GenericNamedTask> makeGenericNamedTask(FnT &&Fn,
80 std::string Desc) {
81 return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
82 std::move(Desc));
83}
84
85/// Create a generic named task from a const char * description.
86template <typename FnT>
87std::unique_ptr<GenericNamedTask>
88makeGenericNamedTask(FnT &&Fn, const char *Desc = nullptr) {
89 if (!Desc)
91 return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
92 Desc);
93}
94
95/// Abstract base for classes that dispatch ORC Tasks.
97public:
98 virtual ~TaskDispatcher();
99
100 /// Run the given task.
101 virtual void dispatch(std::unique_ptr<Task> T) = 0;
102
103 /// Called by ExecutionSession. Waits until all tasks have completed.
104 virtual void shutdown() = 0;
105};
106
107/// Runs all tasks on the current thread.
109public:
110 void dispatch(std::unique_ptr<Task> T) override;
111 void shutdown() override;
112};
113
114#if LLVM_ENABLE_THREADS
115
116class DynamicThreadPoolTaskDispatcher : public TaskDispatcher {
117public:
118 DynamicThreadPoolTaskDispatcher(
119 std::optional<size_t> MaxMaterializationThreads)
120 : MaxMaterializationThreads(MaxMaterializationThreads) {}
121 void dispatch(std::unique_ptr<Task> T) override;
122 void shutdown() override;
123private:
124 std::mutex DispatchMutex;
125 bool Running = true;
126 size_t Outstanding = 0;
127 std::condition_variable OutstandingCV;
128
129 std::optional<size_t> MaxMaterializationThreads;
130 size_t NumMaterializationThreads = 0;
131 std::deque<std::unique_ptr<Task>> MaterializationTaskQueue;
132};
133
134#endif // LLVM_ENABLE_THREADS
135
136} // End namespace orc
137} // End namespace llvm
138
139#endif // LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
raw_pwrite_stream & OS
Inheritance utility for extensible RTTI.
Generic task implementation.
Definition: TaskDispatch.h:59
GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
Definition: TaskDispatch.h:64
GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
Definition: TaskDispatch.h:61
void printDescription(raw_ostream &OS) override
Definition: TaskDispatch.h:68
Base class for generic tasks.
Definition: TaskDispatch.h:52
static const char * DefaultDescription
Definition: TaskDispatch.h:55
Runs all tasks on the current thread.
Definition: TaskDispatch.h:108
void shutdown() override
Called by ExecutionSession. Waits until all tasks have completed.
void dispatch(std::unique_ptr< Task > T) override
Run the given task.
Abstract base for classes that dispatch ORC Tasks.
Definition: TaskDispatch.h:96
virtual void shutdown()=0
Called by ExecutionSession. Waits until all tasks have completed.
virtual void dispatch(std::unique_ptr< Task > T)=0
Run the given task.
Represents an abstract task for ORC to run.
Definition: TaskDispatch.h:35
virtual ~Task()=default
static char ID
Definition: TaskDispatch.h:37
virtual void run()=0
Run the task.
virtual void printDescription(raw_ostream &OS)=0
Description of the task to be performed. Used for logging.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:52
std::unique_ptr< GenericNamedTask > makeGenericNamedTask(FnT &&Fn, std::string Desc)
Create a generic named task from a std::string description.
Definition: TaskDispatch.h:79
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Op::Description Desc
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1873
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:858
Description of the encoding of one expression Op.