LLVM  14.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 
30 namespace llvm {
31 namespace orc {
32 
33 /// Represents an abstract task for ORC to run.
34 class Task : public RTTIExtends<Task, RTTIRoot> {
35 public:
36  static char ID;
37 
38  virtual ~Task() {}
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 
46 private:
47  void anchor() override;
48 };
49 
50 /// Base class for generic tasks.
51 class GenericNamedTask : public RTTIExtends<GenericNamedTask, Task> {
52 public:
53  static char ID;
54  static const char *DefaultDescription;
55 };
56 
57 /// Generic task implementation.
58 template <typename FnT> class GenericNamedTaskImpl : public GenericNamedTask {
59 public:
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 
70 private:
71  FnT Fn;
72  const char *Desc;
73  std::string DescBuffer;
74 };
75 
76 /// Create a generic named task from a std::string description.
77 template <typename FnT>
78 std::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.
85 template <typename FnT>
86 std::unique_ptr<GenericNamedTask>
87 makeGenericNamedTask(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.
96 public:
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.
108 public:
109  void dispatch(std::unique_ptr<Task> T) override;
110  void shutdown() override;
111 };
112 
113 #if LLVM_ENABLE_THREADS
114 
116 public:
117  void dispatch(std::unique_ptr<Task> T) override;
118  void shutdown() override;
119 private:
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
llvm::orc::Task::ID
static char ID
Definition: TaskDispatch.h:36
llvm::orc::InPlaceTaskDispatcher::shutdown
void shutdown() override
Called by ExecutionSession. Waits until all tasks have completed.
Definition: TaskDispatch.cpp:23
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AllocatorList.h:23
llvm::orc::InPlaceTaskDispatcher::dispatch
void dispatch(std::unique_ptr< Task > T) override
Run the given task.
Definition: TaskDispatch.cpp:21
llvm::orc::makeGenericNamedTask
std::unique_ptr< GenericNamedTask > makeGenericNamedTask(FnT &&Fn, std::string Desc)
Create a generic named task from a std::string description.
Definition: TaskDispatch.h:78
llvm::orc::GenericNamedTaskImpl::run
void run() override
Definition: TaskDispatch.h:68
llvm::orc::TaskDispatcher
Abstract base for classes that dispatch ORC Tasks.
Definition: TaskDispatch.h:95
llvm::orc::GenericNamedTaskImpl::GenericNamedTaskImpl
GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
Definition: TaskDispatch.h:60
llvm::orc::InPlaceTaskDispatcher
Runs all tasks on the current thread.
Definition: TaskDispatch.h:107
T
#define T
Definition: Mips16ISelLowering.cpp:341
ExtensibleRTTI.h
llvm::orc::GenericNamedTask::ID
static char ID
Definition: TaskDispatch.h:53
llvm::orc::Task::printDescription
virtual void printDescription(raw_ostream &OS)=0
Description of the task to be performed. Used for logging.
llvm::orc::GenericNamedTaskImpl
Generic task implementation.
Definition: TaskDispatch.h:58
llvm::orc::GenericNamedTask::DefaultDescription
static const char * DefaultDescription
Definition: TaskDispatch.h:54
llvm::orc::DynamicThreadPoolTaskDispatcher::shutdown
void shutdown() override
Called by ExecutionSession. Waits until all tasks have completed.
llvm::raw_ostream
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition: raw_ostream.h:53
llvm::RTTIExtends
Inheritance utility for extensible RTTI.
Definition: ExtensibleRTTI.h:65
llvm::orc::Task::run
virtual void run()=0
Run the task.
llvm::orc::Task::~Task
virtual ~Task()
Definition: TaskDispatch.h:38
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::orc::TaskDispatcher::dispatch
virtual void dispatch(std::unique_ptr< Task > T)=0
Run the given task.
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
llvm::move
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:1639
llvm::orc::TaskDispatcher::shutdown
virtual void shutdown()=0
Called by ExecutionSession. Waits until all tasks have completed.
llvm::orc::DynamicThreadPoolTaskDispatcher::dispatch
void dispatch(std::unique_ptr< Task > T) override
Run the given task.
llvm::orc::GenericNamedTaskImpl::GenericNamedTaskImpl
GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
Definition: TaskDispatch.h:63
llvm::orc::DynamicThreadPoolTaskDispatcher
Definition: TaskDispatch.h:115
std
Definition: BitVector.h:838
llvm::orc::Task
Represents an abstract task for ORC to run.
Definition: TaskDispatch.h:34
llvm::c_str
SmallVectorImpl< T >::const_pointer c_str(SmallVectorImpl< T > &str)
Definition: WindowsSupport.h:193
llvm::orc::GenericNamedTask
Base class for generic tasks.
Definition: TaskDispatch.h:51
raw_ostream.h
llvm::orc::GenericNamedTaskImpl::printDescription
void printDescription(raw_ostream &OS) override
Definition: TaskDispatch.h:67
Debug.h
llvm::orc::TaskDispatcher::~TaskDispatcher
virtual ~TaskDispatcher()
Definition: TaskDispatch.cpp:19