LLVM 22.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"
18#include "llvm/Support/Debug.h"
21
22#include <cassert>
23#include <string>
24
25#if LLVM_ENABLE_THREADS
26#include <condition_variable>
27#include <deque>
28#include <mutex>
29#include <thread>
30#endif
31
32namespace llvm {
33namespace orc {
34
35/// Represents an abstract task for ORC to run.
36class LLVM_ABI Task : public RTTIExtends<Task, RTTIRoot> {
37public:
38 static char ID;
39
40 virtual ~Task() = default;
41
42 /// Description of the task to be performed. Used for logging.
43 virtual void printDescription(raw_ostream &OS) = 0;
44
45 /// Run the task.
46 virtual void run() = 0;
47
48private:
49 void anchor() override;
50};
51
52/// Base class for generic tasks.
53class GenericNamedTask : public RTTIExtends<GenericNamedTask, Task> {
54public:
55 LLVM_ABI static char ID;
56 LLVM_ABI static const char *DefaultDescription;
57};
58
59/// Generic task implementation.
60template <typename FnT> class GenericNamedTaskImpl : public GenericNamedTask {
61public:
62 GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
63 : Fn(std::forward<FnT>(Fn)), Desc(DescBuffer.c_str()),
64 DescBuffer(std::move(DescBuffer)) {}
65 GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
66 : Fn(std::forward<FnT>(Fn)), Desc(Desc) {
67 assert(Desc && "Description cannot be null");
68 }
69 void printDescription(raw_ostream &OS) override { OS << Desc; }
70 void run() override { Fn(); }
71
72private:
73 FnT Fn;
74 const char *Desc;
75 std::string DescBuffer;
76};
77
78/// Create a generic named task from a std::string description.
79template <typename FnT>
80std::unique_ptr<GenericNamedTask> makeGenericNamedTask(FnT &&Fn,
81 std::string Desc) {
82 return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
83 std::move(Desc));
84}
85
86/// Create a generic named task from a const char * description.
87template <typename FnT>
88std::unique_ptr<GenericNamedTask>
89makeGenericNamedTask(FnT &&Fn, const char *Desc = nullptr) {
90 if (!Desc)
92 return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
93 Desc);
94}
95
96/// IdleTask can be used as the basis for low-priority tasks, e.g. speculative
97/// lookup.
98class LLVM_ABI IdleTask : public RTTIExtends<IdleTask, Task> {
99public:
100 static char ID;
101
102private:
103 void anchor() override;
104};
105
106/// Abstract base for classes that dispatch ORC Tasks.
108public:
110
111 /// Run the given task.
112 virtual void dispatch(std::unique_ptr<Task> T) = 0;
113
114 /// Called by ExecutionSession. Waits until all tasks have completed.
115 virtual void shutdown() = 0;
116};
117
118/// Runs all tasks on the current thread.
120public:
121 void dispatch(std::unique_ptr<Task> T) override;
122 void shutdown() override;
123};
124
125#if LLVM_ENABLE_THREADS
126
127class LLVM_ABI DynamicThreadPoolTaskDispatcher : public TaskDispatcher {
128public:
129 DynamicThreadPoolTaskDispatcher(
130 std::optional<size_t> MaxMaterializationThreads)
131 : MaxMaterializationThreads(MaxMaterializationThreads) {}
132
133 void dispatch(std::unique_ptr<Task> T) override;
134 void shutdown() override;
135private:
136 bool canRunMaterializationTaskNow();
137 bool canRunIdleTaskNow();
138
139 std::mutex DispatchMutex;
140 bool Shutdown = false;
141 size_t Outstanding = 0;
142 std::condition_variable OutstandingCV;
143
144 std::optional<size_t> MaxMaterializationThreads;
145 size_t NumMaterializationThreads = 0;
146 std::deque<std::unique_ptr<Task>> MaterializationTaskQueue;
147 std::deque<std::unique_ptr<Task>> IdleTaskQueue;
148};
149
150#endif // LLVM_ENABLE_THREADS
151
152} // End namespace orc
153} // End namespace llvm
154
155#endif // LLVM_EXECUTIONENGINE_ORC_TASKDISPATCH_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define LLVM_ABI
Definition: Compiler.h:213
raw_pwrite_stream & OS
Inheritance utility for extensible RTTI.
Generic task implementation.
Definition: TaskDispatch.h:60
GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
Definition: TaskDispatch.h:65
GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
Definition: TaskDispatch.h:62
void printDescription(raw_ostream &OS) override
Definition: TaskDispatch.h:69
Base class for generic tasks.
Definition: TaskDispatch.h:53
static LLVM_ABI char ID
Definition: TaskDispatch.h:55
static LLVM_ABI const char * DefaultDescription
Definition: TaskDispatch.h:56
IdleTask can be used as the basis for low-priority tasks, e.g.
Definition: TaskDispatch.h:98
Runs all tasks on the current thread.
Definition: TaskDispatch.h:119
Abstract base for classes that dispatch ORC Tasks.
Definition: TaskDispatch.h:107
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:36
virtual ~Task()=default
static char ID
Definition: TaskDispatch.h:38
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:53
std::unique_ptr< GenericNamedTask > makeGenericNamedTask(FnT &&Fn, std::string Desc)
Create a generic named task from a std::string description.
Definition: TaskDispatch.h:80
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:1886
Implement std::hash so that hash_code can be used in STL containers.
Definition: BitVector.h:856
Description of the encoding of one expression Op.