LLVM 18.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 <mutex>
27#include <thread>
28#endif
29
30namespace llvm {
31namespace orc {
32
33/// Represents an abstract task for ORC to run.
34class Task : public RTTIExtends<Task, RTTIRoot> {
35public:
36 static char ID;
37
38 virtual ~Task() = default;
39
40 /// Description of the task to be performed. Used for logging.
41 virtual void printDescription(raw_ostream &OS) = 0;
42
43 /// Run the task.
44 virtual void run() = 0;
45
46private:
47 void anchor() override;
48};
49
50/// Base class for generic tasks.
51class GenericNamedTask : public RTTIExtends<GenericNamedTask, Task> {
52public:
53 static char ID;
54 static const char *DefaultDescription;
55};
56
57/// Generic task implementation.
58template <typename FnT> class GenericNamedTaskImpl : public GenericNamedTask {
59public:
60 GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
61 : Fn(std::forward<FnT>(Fn)), Desc(DescBuffer.c_str()),
62 DescBuffer(std::move(DescBuffer)) {}
63 GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
64 : Fn(std::forward<FnT>(Fn)), Desc(Desc) {
65 assert(Desc && "Description cannot be null");
66 }
67 void printDescription(raw_ostream &OS) override { OS << Desc; }
68 void run() override { Fn(); }
69
70private:
71 FnT Fn;
72 const char *Desc;
73 std::string DescBuffer;
74};
75
76/// Create a generic named task from a std::string description.
77template <typename FnT>
78std::unique_ptr<GenericNamedTask> makeGenericNamedTask(FnT &&Fn,
79 std::string Desc) {
80 return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
81 std::move(Desc));
82}
83
84/// Create a generic named task from a const char * description.
85template <typename FnT>
86std::unique_ptr<GenericNamedTask>
87makeGenericNamedTask(FnT &&Fn, const char *Desc = nullptr) {
88 if (!Desc)
90 return std::make_unique<GenericNamedTaskImpl<FnT>>(std::forward<FnT>(Fn),
91 Desc);
92}
93
94/// Abstract base for classes that dispatch ORC Tasks.
96public:
97 virtual ~TaskDispatcher();
98
99 /// Run the given task.
100 virtual void dispatch(std::unique_ptr<Task> T) = 0;
101
102 /// Called by ExecutionSession. Waits until all tasks have completed.
103 virtual void shutdown() = 0;
104};
105
106/// Runs all tasks on the current thread.
108public:
109 void dispatch(std::unique_ptr<Task> T) override;
110 void shutdown() override;
111};
112
113#if LLVM_ENABLE_THREADS
114
115class DynamicThreadPoolTaskDispatcher : public TaskDispatcher {
116public:
117 void dispatch(std::unique_ptr<Task> T) override;
118 void shutdown() override;
119private:
120 std::mutex DispatchMutex;
121 bool Running = true;
122 size_t Outstanding = 0;
123 std::condition_variable OutstandingCV;
124};
125
126#endif // LLVM_ENABLE_THREADS
127
128} // End namespace orc
129} // End namespace llvm
130
131#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:58
GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
Definition: TaskDispatch.h:63
GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
Definition: TaskDispatch.h:60
void printDescription(raw_ostream &OS) override
Definition: TaskDispatch.h:67
Base class for generic tasks.
Definition: TaskDispatch.h:51
static const char * DefaultDescription
Definition: TaskDispatch.h:54
Runs all tasks on the current thread.
Definition: TaskDispatch.h:107
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:95
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:34
virtual ~Task()=default
static char ID
Definition: TaskDispatch.h:36
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:78
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:1854
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.