xref: /freebsd/contrib/llvm-project/llvm/include/llvm/ExecutionEngine/Orc/TaskDispatch.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
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/Compiler.h"
18 #include "llvm/Support/Debug.h"
19 #include "llvm/Support/ExtensibleRTTI.h"
20 #include "llvm/Support/raw_ostream.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 
32 namespace llvm {
33 namespace orc {
34 
35 /// Represents an abstract task for ORC to run.
36 class LLVM_ABI Task : public RTTIExtends<Task, RTTIRoot> {
37 public:
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 
48 private:
49   void anchor() override;
50 };
51 
52 /// Base class for generic tasks.
53 class GenericNamedTask : public RTTIExtends<GenericNamedTask, Task> {
54 public:
55   LLVM_ABI static char ID;
56   LLVM_ABI static const char *DefaultDescription;
57 };
58 
59 /// Generic task implementation.
60 template <typename FnT> class GenericNamedTaskImpl : public GenericNamedTask {
61 public:
GenericNamedTaskImpl(FnT && Fn,std::string DescBuffer)62   GenericNamedTaskImpl(FnT &&Fn, std::string DescBuffer)
63       : Fn(std::forward<FnT>(Fn)), Desc(DescBuffer.c_str()),
64         DescBuffer(std::move(DescBuffer)) {}
GenericNamedTaskImpl(FnT && Fn,const char * Desc)65   GenericNamedTaskImpl(FnT &&Fn, const char *Desc)
66       : Fn(std::forward<FnT>(Fn)), Desc(Desc) {
67     assert(Desc && "Description cannot be null");
68   }
printDescription(raw_ostream & OS)69   void printDescription(raw_ostream &OS) override { OS << Desc; }
run()70   void run() override { Fn(); }
71 
72 private:
73   FnT Fn;
74   const char *Desc;
75   std::string DescBuffer;
76 };
77 
78 /// Create a generic named task from a std::string description.
79 template <typename FnT>
makeGenericNamedTask(FnT && Fn,std::string Desc)80 std::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.
87 template <typename FnT>
88 std::unique_ptr<GenericNamedTask>
89 makeGenericNamedTask(FnT &&Fn, const char *Desc = nullptr) {
90   if (!Desc)
91     Desc = GenericNamedTask::DefaultDescription;
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.
98 class LLVM_ABI IdleTask : public RTTIExtends<IdleTask, Task> {
99 public:
100   static char ID;
101 
102 private:
103   void anchor() override;
104 };
105 
106 /// Abstract base for classes that dispatch ORC Tasks.
107 class LLVM_ABI TaskDispatcher {
108 public:
109   virtual ~TaskDispatcher();
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.
119 class LLVM_ABI InPlaceTaskDispatcher : public TaskDispatcher {
120 public:
121   void dispatch(std::unique_ptr<Task> T) override;
122   void shutdown() override;
123 };
124 
125 #if LLVM_ENABLE_THREADS
126 
127 class LLVM_ABI DynamicThreadPoolTaskDispatcher : public TaskDispatcher {
128 public:
DynamicThreadPoolTaskDispatcher(std::optional<size_t> MaxMaterializationThreads)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;
135 private:
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
156