LLVM 22.0.0git
Threading.h
Go to the documentation of this file.
1//===-- llvm/Support/Threading.h - Control multithreading mode --*- 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// This file declares helper functions for running LLVM in a multi-threaded
10// environment.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_SUPPORT_THREADING_H
15#define LLVM_SUPPORT_THREADING_H
16
17#include "llvm/ADT/BitVector.h"
18#include "llvm/ADT/StringRef.h"
19#include "llvm/Config/llvm-config.h" // for LLVM_ON_UNIX
21#include <optional>
22
23#if defined(_MSC_VER)
24// MSVC's call_once implementation worked since VS 2015, which is the minimum
25// supported version as of this writing.
26#define LLVM_THREADING_USE_STD_CALL_ONCE 1
27#elif defined(LLVM_ON_UNIX) && \
28 (defined(_LIBCPP_VERSION) || \
29 !(defined(__NetBSD__) || defined(__OpenBSD__) || defined(__powerpc__)))
30// std::call_once from libc++ is used on all Unix platforms. Other
31// implementations like libstdc++ are known to have problems on NetBSD,
32// OpenBSD and PowerPC.
33#define LLVM_THREADING_USE_STD_CALL_ONCE 1
34#elif defined(LLVM_ON_UNIX) && \
35 (defined(__powerpc__) && defined(__LITTLE_ENDIAN__))
36#define LLVM_THREADING_USE_STD_CALL_ONCE 1
37#else
38#define LLVM_THREADING_USE_STD_CALL_ONCE 0
39#endif
40
41#if LLVM_THREADING_USE_STD_CALL_ONCE
42#include <mutex>
43#else
44#include "llvm/Support/Atomic.h"
45#endif
46
47namespace llvm {
48class Twine;
49
50/// Returns true if LLVM is compiled with support for multi-threading, and
51/// false otherwise.
52constexpr bool llvm_is_multithreaded() { return LLVM_ENABLE_THREADS; }
53
54#if LLVM_THREADING_USE_STD_CALL_ONCE
55
56 typedef std::once_flag once_flag;
57
58#else
59
60 enum InitStatus { Uninitialized = 0, Wait = 1, Done = 2 };
61
62 /// The llvm::once_flag structure
63 ///
64 /// This type is modeled after std::once_flag to use with llvm::call_once.
65 /// This structure must be used as an opaque object. It is a struct to force
66 /// autoinitialization and behave like std::once_flag.
67 struct once_flag {
69 };
70
71#endif
72
73 /// Execute the function specified as a parameter once.
74 ///
75 /// Typical usage:
76 /// \code
77 /// void foo() {...};
78 /// ...
79 /// static once_flag flag;
80 /// call_once(flag, foo);
81 /// \endcode
82 ///
83 /// \param flag Flag used for tracking whether or not this has run.
84 /// \param F Function to call once.
85 template <typename Function, typename... Args>
86 void call_once(once_flag &flag, Function &&F, Args &&... ArgList) {
87#if LLVM_THREADING_USE_STD_CALL_ONCE
88 std::call_once(flag, std::forward<Function>(F),
89 std::forward<Args>(ArgList)...);
90#else
91 // For other platforms we use a generic (if brittle) version based on our
92 // atomics.
94 if (old_val == Uninitialized) {
95 std::forward<Function>(F)(std::forward<Args>(ArgList)...);
99 flag.status = Done;
101 } else {
102 // Wait until any thread doing the call has finished.
103 sys::cas_flag tmp = flag.status;
105 while (tmp != Done) {
106 tmp = flag.status;
108 }
109 }
111#endif
112 }
113
114 /// This tells how a thread pool will be used
116 public:
117 // The default value (0) means all available threads should be used,
118 // taking the affinity mask into account. If set, this value only represents
119 // a suggested high bound, the runtime might choose a lower value (not
120 // higher).
121 unsigned ThreadsRequested = 0;
122
123 // If SMT is active, use hyper threads. If false, there will be only one
124 // std::thread per core.
125 bool UseHyperThreads = true;
126
127 // If set, will constrain 'ThreadsRequested' to the number of hardware
128 // threads, or hardware cores.
129 bool Limit = false;
130
131 /// Retrieves the max available threads for the current strategy. This
132 /// accounts for affinity masks and takes advantage of all CPU sockets.
133 LLVM_ABI unsigned compute_thread_count() const;
134
135 /// Assign the current thread to an ideal hardware CPU or NUMA node. In a
136 /// multi-socket system, this ensures threads are assigned to all CPU
137 /// sockets. \p ThreadPoolNum represents a number bounded by [0,
138 /// compute_thread_count()).
139 LLVM_ABI void apply_thread_strategy(unsigned ThreadPoolNum) const;
140
141 /// Finds the CPU socket where a thread should go. Returns 'std::nullopt' if
142 /// the thread shall remain on the actual CPU socket.
143 LLVM_ABI std::optional<unsigned>
144 compute_cpu_socket(unsigned ThreadPoolNum) const;
145
146 /// If true, the thread pool will attempt to coordinate with a GNU Make
147 /// jobserver, acquiring a job slot before processing a task. If no
148 /// jobserver is found in the environment, this is ignored.
149 bool UseJobserver = false;
150 };
151
152 /// Build a strategy from a number of threads as a string provided in \p Num.
153 /// When Num is above the max number of threads specified by the \p Default
154 /// strategy, we attempt to equally allocate the threads on all CPU sockets.
155 /// "0" or an empty string will return the \p Default strategy.
156 /// "all" for using all hardware threads.
157 LLVM_ABI std::optional<ThreadPoolStrategy>
159
160 /// Returns a thread strategy for tasks requiring significant memory or other
161 /// resources. To be used for workloads where hardware_concurrency() proves to
162 /// be less efficient. Avoid this strategy if doing lots of I/O. Currently
163 /// based on physical cores, if available for the host system, otherwise falls
164 /// back to hardware_concurrency(). Returns 1 when LLVM is configured with
165 /// LLVM_ENABLE_THREADS = OFF.
166 inline ThreadPoolStrategy
169 S.UseHyperThreads = false;
171 return S;
172 }
173
174 /// Like heavyweight_hardware_concurrency() above, but builds a strategy
175 /// based on the rules described for get_threadpool_strategy().
176 /// If \p Num is invalid, returns a default strategy where one thread per
177 /// hardware core is used.
179 std::optional<ThreadPoolStrategy> S =
181 if (S)
182 return *S;
184 }
185
186 /// Returns a default thread strategy where all available hardware resources
187 /// are to be used, except for those initially excluded by an affinity mask.
188 /// This function takes affinity into consideration. Returns 1 when LLVM is
189 /// configured with LLVM_ENABLE_THREADS=OFF.
193 return S;
194 }
195
196 /// Like hardware_concurrency() above, but builds a strategy
197 /// based on the rules described for get_threadpool_strategy().
198 /// If \p Num is invalid, returns a default strategy where one thread per
199 /// hardware core is used.
201 std::optional<ThreadPoolStrategy> S =
203 if (S)
204 return *S;
205 return hardware_concurrency();
206 }
207
208 /// Returns an optimal thread strategy to execute specified amount of tasks.
209 /// This strategy should prevent us from creating too many threads if we
210 /// occasionaly have an unexpectedly small amount of tasks.
211 inline ThreadPoolStrategy optimal_concurrency(unsigned TaskCount = 0) {
213 S.Limit = true;
214 S.ThreadsRequested = TaskCount;
215 return S;
216 }
217
218 /// Returns a thread strategy that attempts to coordinate with a GNU Make
219 /// jobserver. The number of active threads will be limited by the number of
220 /// available job slots. If no jobserver is detected in the environment, this
221 /// strategy falls back to the default hardware_concurrency() behavior.
224 S.UseJobserver = true;
225 // We can still request all threads be created, as they will simply
226 // block waiting for a job slot if the jobserver is the limiting factor.
227 S.ThreadsRequested = 0; // 0 means 'use all available'
228 return S;
229 }
230
231 /// Return the current thread id, as used in various OS system calls.
232 /// Note that not all platforms guarantee that the value returned will be
233 /// unique across the entire system, so portable code should not assume
234 /// this.
236
237 /// Get the maximum length of a thread name on this platform.
238 /// A value of 0 means there is no limit.
240
241 /// Set the name of the current thread. Setting a thread's name can
242 /// be helpful for enabling useful diagnostics under a debugger or when
243 /// logging. The level of support for setting a thread's name varies
244 /// wildly across operating systems, and we only make a best effort to
245 /// perform the operation on supported platforms. No indication of success
246 /// or failure is returned.
247 LLVM_ABI void set_thread_name(const Twine &Name);
248
249 /// Get the name of the current thread. The level of support for
250 /// getting a thread's name varies wildly across operating systems, and it
251 /// is not even guaranteed that if you can successfully set a thread's name
252 /// that you can later get it back. This function is intended for diagnostic
253 /// purposes, and as with setting a thread's name no indication of whether
254 /// the operation succeeded or failed is returned.
255 LLVM_ABI void get_thread_name(SmallVectorImpl<char> &Name);
256
257 /// Returns a mask that represents on which hardware thread, core, CPU, NUMA
258 /// group, the calling thread can be executed. On Windows, threads cannot
259 /// cross CPU sockets boundaries.
261
262 /// Returns how many physical CPUs or NUMA groups the system has.
263 LLVM_ABI unsigned get_cpus();
264
265 /// Returns how many physical cores (as opposed to logical cores returned from
266 /// thread::hardware_concurrency(), which includes hyperthreads).
267 /// Returns -1 if unknown for the current host system.
269
270 enum class ThreadPriority {
271 /// Lower the current thread's priority as much as possible. Can be used
272 /// for long-running tasks that are not time critical; more energy-
273 /// efficient than Low.
275
276 /// Lower the current thread's priority such that it does not affect
277 /// foreground tasks significantly. This is a good default for long-
278 /// running, latency-insensitive tasks to make sure cpu is not hogged
279 /// by this task.
280 Low = 1,
281
282 /// Restore the current thread's priority to default scheduling priority.
284 };
287}
288
289#endif
This file implements the BitVector class.
#define TsanHappensBefore(cv)
Definition Compiler.h:617
#define TsanHappensAfter(cv)
Definition Compiler.h:618
#define LLVM_ABI
Definition Compiler.h:213
#define TsanIgnoreWritesEnd()
Definition Compiler.h:620
#define TsanIgnoreWritesBegin()
Definition Compiler.h:619
#define F(x, y, z)
Definition MD5.cpp:55
static cl::opt< int > ThreadCount("threads", cl::init(0))
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
This tells how a thread pool will be used.
Definition Threading.h:115
LLVM_ABI void apply_thread_strategy(unsigned ThreadPoolNum) const
Assign the current thread to an ideal hardware CPU or NUMA node.
LLVM_ABI std::optional< unsigned > compute_cpu_socket(unsigned ThreadPoolNum) const
Finds the CPU socket where a thread should go.
LLVM_ABI unsigned compute_thread_count() const
Retrieves the max available threads for the current strategy.
Definition Threading.cpp:42
bool UseJobserver
If true, the thread pool will attempt to coordinate with a GNU Make jobserver, acquiring a job slot b...
Definition Threading.h:149
uint32_t cas_flag
Definition Atomic.h:35
LLVM_ABI void MemoryFence()
Definition Atomic.cpp:30
LLVM_ABI cas_flag CompareAndSwap(volatile cas_flag *ptr, cas_flag new_value, cas_flag old_value)
Definition Atomic.cpp:44
This is an optimization pass for GlobalISel generic memory operations.
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition Threading.h:190
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition Threading.h:167
ThreadPoolStrategy jobserver_concurrency()
Returns a thread strategy that attempts to coordinate with a GNU Make jobserver.
Definition Threading.h:222
ThreadPriority
Definition Threading.h:270
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
Definition Threading.h:280
@ Background
Lower the current thread's priority as much as possible.
Definition Threading.h:274
constexpr bool llvm_is_multithreaded()
Returns true if LLVM is compiled with support for multi-threading, and false otherwise.
Definition Threading.h:52
InitStatus
Definition Threading.h:60
@ Uninitialized
Definition Threading.h:60
@ Wait
Definition Threading.h:60
@ Done
Definition Threading.h:60
LLVM_ABI llvm::BitVector get_thread_affinity_mask()
Returns a mask that represents on which hardware thread, core, CPU, NUMA group, the calling thread ca...
Definition Threading.cpp:40
LLVM_ABI uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
Definition Threading.cpp:34
LLVM_ABI SetThreadPriorityResult set_thread_priority(ThreadPriority Priority)
LLVM_ABI unsigned get_cpus()
Returns how many physical CPUs or NUMA groups the system has.
ThreadPoolStrategy optimal_concurrency(unsigned TaskCount=0)
Returns an optimal thread strategy to execute specified amount of tasks.
Definition Threading.h:211
LLVM_ABI void set_thread_name(const Twine &Name)
Set the name of the current thread.
Definition Threading.cpp:36
SetThreadPriorityResult
Definition Threading.h:285
LLVM_ABI void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
Definition Threading.cpp:38
LLVM_ABI int get_physical_cores()
Returns how many physical cores (as opposed to logical cores returned from thread::hardware_concurren...
Definition Threading.cpp:48
LLVM_ABI std::optional< ThreadPoolStrategy > get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default={})
Build a strategy from a number of threads as a string provided in Num.
LLVM_ABI uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
Definition Threading.cpp:32
void call_once(once_flag &flag, Function &&F, Args &&... ArgList)
Execute the function specified as a parameter once.
Definition Threading.h:86
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20
The llvm::once_flag structure.
Definition Threading.h:67
volatile sys::cas_flag status
Definition Threading.h:68