|
14 | 14 | #ifndef LLVM_SUPPORT_THREADING_H
|
15 | 15 | #define LLVM_SUPPORT_THREADING_H
|
16 | 16 |
|
| 17 | +#include "llvm/ADT/BitVector.h" |
17 | 18 | #include "llvm/ADT/FunctionExtras.h"
|
18 | 19 | #include "llvm/ADT/SmallVector.h"
|
19 | 20 | #include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
|
@@ -143,20 +144,52 @@ void llvm_execute_on_thread_async(
|
143 | 144 | #endif
|
144 | 145 | }
|
145 | 146 |
|
146 |
| - /// Get the amount of currency to use for tasks requiring significant |
147 |
| - /// memory or other resources. Currently based on physical cores, if |
148 |
| - /// available for the host system, otherwise falls back to |
149 |
| - /// thread::hardware_concurrency(). |
150 |
| - /// Returns 1 when LLVM is configured with LLVM_ENABLE_THREADS=OFF |
151 |
| - unsigned heavyweight_hardware_concurrency(); |
152 |
| - |
153 |
| - /// Get the number of threads that the current program can execute |
154 |
| - /// concurrently. On some systems std::thread::hardware_concurrency() returns |
155 |
| - /// the total number of cores, without taking affinity into consideration. |
156 |
| - /// Returns 1 when LLVM is configured with LLVM_ENABLE_THREADS=OFF. |
157 |
| - /// Fallback to std::thread::hardware_concurrency() if sched_getaffinity is |
158 |
| - /// not available. |
159 |
| - unsigned hardware_concurrency(); |
| 147 | + /// This tells how a thread pool will be used |
| 148 | + class ThreadPoolStrategy { |
| 149 | + public: |
| 150 | + // The default value (0) means all available threads should be used, |
| 151 | + // excluding affinity mask. If set, this value only represents a suggested |
| 152 | + // high bound, the runtime might choose a lower value (not higher). |
| 153 | + unsigned ThreadsRequested = 0; |
| 154 | + |
| 155 | + // If SMT is active, use hyper threads. If false, there will be only one |
| 156 | + // std::thread per core. |
| 157 | + bool UseHyperThreads = true; |
| 158 | + |
| 159 | + /// Retrieves the max available threads for the current strategy. This |
| 160 | + /// accounts for affinity masks and takes advantage of all CPU sockets. |
| 161 | + unsigned compute_thread_count() const; |
| 162 | + |
| 163 | + /// Assign the current thread to an ideal hardware CPU or NUMA node. In a |
| 164 | + /// multi-socket system, this ensures threads are assigned to all CPU |
| 165 | + /// sockets. \p ThreadPoolNum represents a number bounded by [0, |
| 166 | + /// compute_thread_count()). |
| 167 | + void apply_thread_strategy(unsigned ThreadPoolNum) const; |
| 168 | + }; |
| 169 | + |
| 170 | + /// Returns a thread strategy for tasks requiring significant memory or other |
| 171 | + /// resources. To be used for workloads where hardware_concurrency() proves to |
| 172 | + /// be less efficient. Avoid this strategy if doing lots of I/O. Currently |
| 173 | + /// based on physical cores, if available for the host system, otherwise falls |
| 174 | + /// back to hardware_concurrency(). Returns 1 when LLVM is configured with |
| 175 | + /// LLVM_ENABLE_THREADS = OFF |
| 176 | + inline ThreadPoolStrategy |
| 177 | + heavyweight_hardware_concurrency(unsigned ThreadCount = 0) { |
| 178 | + ThreadPoolStrategy S; |
| 179 | + S.UseHyperThreads = false; |
| 180 | + S.ThreadsRequested = ThreadCount; |
| 181 | + return S; |
| 182 | + } |
| 183 | + |
| 184 | + /// Returns a default thread strategy where all available hardware ressources |
| 185 | + /// are to be used, except for those initially excluded by an affinity mask. |
| 186 | + /// This function takes affinity into consideration. Returns 1 when LLVM is |
| 187 | + /// configured with LLVM_ENABLE_THREADS=OFF. |
| 188 | + inline ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount = 0) { |
| 189 | + ThreadPoolStrategy S; |
| 190 | + S.ThreadsRequested = ThreadCount; |
| 191 | + return S; |
| 192 | + } |
160 | 193 |
|
161 | 194 | /// Return the current thread id, as used in various OS system calls.
|
162 | 195 | /// Note that not all platforms guarantee that the value returned will be
|
@@ -184,6 +217,14 @@ void llvm_execute_on_thread_async(
|
184 | 217 | /// the operation succeeded or failed is returned.
|
185 | 218 | void get_thread_name(SmallVectorImpl<char> &Name);
|
186 | 219 |
|
| 220 | + /// Returns a mask that represents on which hardware thread, core, CPU, NUMA |
| 221 | + /// group, the calling thread can be executed. On Windows, threads cannot |
| 222 | + /// cross CPU boundaries. |
| 223 | + llvm::BitVector get_thread_affinity_mask(); |
| 224 | + |
| 225 | + /// Returns how many physical CPUs or NUMA groups the system has. |
| 226 | + unsigned get_cpus(); |
| 227 | + |
187 | 228 | enum class ThreadPriority {
|
188 | 229 | Background = 0,
|
189 | 230 | Default = 1,
|
|
0 commit comments