Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.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
20#include "llvm/Support/Compiler.h"
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
47namespacellvm {
48classTwine;
49
50/// Returns true if LLVM is compiled with support for multi-threading, and
51/// false otherwise.
52constexprboolllvm_is_multithreaded() {return LLVM_ENABLE_THREADS; }
53
54#if LLVM_THREADING_USE_STD_CALL_ONCE
55
56typedef std::once_flag once_flag;
57
58#else
59
60enumInitStatus {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.
67structonce_flag {
68volatilesys::cas_flagstatus =Uninitialized;
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.
85template <typenameFunction,typename... Args>
86voidcall_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.
93sys::cas_flag old_val =sys::CompareAndSwap(&flag.status,Wait,Uninitialized);
94if (old_val ==Uninitialized) {
95 std::forward<Function>(F)(std::forward<Args>(ArgList)...);
96sys::MemoryFence();
97TsanIgnoreWritesBegin();
98TsanHappensBefore(&flag.status);
99 flag.status =Done;
100TsanIgnoreWritesEnd();
101 }else {
102// Wait until any thread doing the call has finished.
103sys::cas_flag tmp = flag.status;
104sys::MemoryFence();
105while (tmp !=Done) {
106 tmp = flag.status;
107sys::MemoryFence();
108 }
109 }
110TsanHappensAfter(&flag.status);
111#endif
112 }
113
114 /// This tells how a thread pool will be used
115classThreadPoolStrategy {
116public:
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).
121unsignedThreadsRequested = 0;
122
123// If SMT is active, use hyper threads. If false, there will be only one
124// std::thread per core.
125boolUseHyperThreads =true;
126
127// If set, will constrain 'ThreadsRequested' to the number of hardware
128// threads, or hardware cores.
129boolLimit =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.
133unsignedcompute_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()).
139voidapply_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 std::optional<unsigned>compute_cpu_socket(unsigned ThreadPoolNum)const;
144 };
145
146 /// Build a strategy from a number of threads as a string provided in \p Num.
147 /// When Num is above the max number of threads specified by the \p Default
148 /// strategy, we attempt to equally allocate the threads on all CPU sockets.
149 /// "0" or an empty string will return the \p Default strategy.
150 /// "all" for using all hardware threads.
151 std::optional<ThreadPoolStrategy>
152get_threadpool_strategy(StringRef Num,ThreadPoolStrategyDefault = {});
153
154 /// Returns a thread strategy for tasks requiring significant memory or other
155 /// resources. To be used for workloads where hardware_concurrency() proves to
156 /// be less efficient. Avoid this strategy if doing lots of I/O. Currently
157 /// based on physical cores, if available for the host system, otherwise falls
158 /// back to hardware_concurrency(). Returns 1 when LLVM is configured with
159 /// LLVM_ENABLE_THREADS = OFF.
160inline ThreadPoolStrategy
161heavyweight_hardware_concurrency(unsignedThreadCount = 0) {
162ThreadPoolStrategy S;
163 S.UseHyperThreads =false;
164 S.ThreadsRequested =ThreadCount;
165return S;
166 }
167
168 /// Like heavyweight_hardware_concurrency() above, but builds a strategy
169 /// based on the rules described for get_threadpool_strategy().
170 /// If \p Num is invalid, returns a default strategy where one thread per
171 /// hardware core is used.
172inlineThreadPoolStrategyheavyweight_hardware_concurrency(StringRef Num) {
173 std::optional<ThreadPoolStrategy> S =
174get_threadpool_strategy(Num,heavyweight_hardware_concurrency());
175if (S)
176return *S;
177returnheavyweight_hardware_concurrency();
178 }
179
180 /// Returns a default thread strategy where all available hardware resources
181 /// are to be used, except for those initially excluded by an affinity mask.
182 /// This function takes affinity into consideration. Returns 1 when LLVM is
183 /// configured with LLVM_ENABLE_THREADS=OFF.
184inlineThreadPoolStrategyhardware_concurrency(unsignedThreadCount = 0) {
185ThreadPoolStrategy S;
186 S.ThreadsRequested =ThreadCount;
187return S;
188 }
189
190 /// Like hardware_concurrency() above, but builds a strategy
191 /// based on the rules described for get_threadpool_strategy().
192 /// If \p Num is invalid, returns a default strategy where one thread per
193 /// hardware core is used.
194inlineThreadPoolStrategyhardware_concurrency(StringRef Num) {
195 std::optional<ThreadPoolStrategy> S =
196get_threadpool_strategy(Num,hardware_concurrency());
197if (S)
198return *S;
199returnhardware_concurrency();
200 }
201
202 /// Returns an optimal thread strategy to execute specified amount of tasks.
203 /// This strategy should prevent us from creating too many threads if we
204 /// occasionaly have an unexpectedly small amount of tasks.
205inlineThreadPoolStrategyoptimal_concurrency(unsigned TaskCount = 0) {
206ThreadPoolStrategy S;
207 S.Limit =true;
208 S.ThreadsRequested = TaskCount;
209return S;
210 }
211
212 /// Return the current thread id, as used in various OS system calls.
213 /// Note that not all platforms guarantee that the value returned will be
214 /// unique across the entire system, so portable code should not assume
215 /// this.
216uint64_tget_threadid();
217
218 /// Get the maximum length of a thread name on this platform.
219 /// A value of 0 means there is no limit.
220uint32_tget_max_thread_name_length();
221
222 /// Set the name of the current thread. Setting a thread's name can
223 /// be helpful for enabling useful diagnostics under a debugger or when
224 /// logging. The level of support for setting a thread's name varies
225 /// wildly across operating systems, and we only make a best effort to
226 /// perform the operation on supported platforms. No indication of success
227 /// or failure is returned.
228voidset_thread_name(const Twine &Name);
229
230 /// Get the name of the current thread. The level of support for
231 /// getting a thread's name varies wildly across operating systems, and it
232 /// is not even guaranteed that if you can successfully set a thread's name
233 /// that you can later get it back. This function is intended for diagnostic
234 /// purposes, and as with setting a thread's name no indication of whether
235 /// the operation succeeded or failed is returned.
236voidget_thread_name(SmallVectorImpl<char> &Name);
237
238 /// Returns a mask that represents on which hardware thread, core, CPU, NUMA
239 /// group, the calling thread can be executed. On Windows, threads cannot
240 /// cross CPU sockets boundaries.
241llvm::BitVectorget_thread_affinity_mask();
242
243 /// Returns how many physical CPUs or NUMA groups the system has.
244unsignedget_cpus();
245
246 /// Returns how many physical cores (as opposed to logical cores returned from
247 /// thread::hardware_concurrency(), which includes hyperthreads).
248 /// Returns -1 if unknown for the current host system.
249intget_physical_cores();
250
251enum classThreadPriority {
252 /// Lower the current thread's priority as much as possible. Can be used
253 /// for long-running tasks that are not time critical; more energy-
254 /// efficient than Low.
255Background = 0,
256
257 /// Lower the current thread's priority such that it does not affect
258 /// foreground tasks significantly. This is a good default for long-
259 /// running, latency-insensitive tasks to make sure cpu is not hogged
260 /// by this task.
261Low = 1,
262
263 /// Restore the current thread's priority to default scheduling priority.
264Default = 2,
265 };
266enum classSetThreadPriorityResult {FAILURE,SUCCESS };
267SetThreadPriorityResultset_thread_priority(ThreadPriority Priority);
268}
269
270#endif
BitVector.h
This file implements the BitVector class.
Compiler.h
TsanHappensBefore
#define TsanHappensBefore(cv)
Definition:Compiler.h:601
TsanHappensAfter
#define TsanHappensAfter(cv)
Definition:Compiler.h:602
TsanIgnoreWritesEnd
#define TsanIgnoreWritesEnd()
Definition:Compiler.h:604
TsanIgnoreWritesBegin
#define TsanIgnoreWritesBegin()
Definition:Compiler.h:603
Name
std::string Name
Definition:ELFObjHandler.cpp:77
F
#define F(x, y, z)
Definition:MD5.cpp:55
StringRef.h
Atomic.h
ThreadCount
static cl::opt< int > ThreadCount("threads", cl::init(0))
llvm::BitVector
Definition:BitVector.h:82
llvm::Function
Definition:Function.h:63
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition:StringRef.h:51
llvm::ThreadPoolStrategy
This tells how a thread pool will be used.
Definition:Threading.h:115
llvm::ThreadPoolStrategy::Limit
bool Limit
Definition:Threading.h:129
llvm::ThreadPoolStrategy::compute_cpu_socket
std::optional< unsigned > compute_cpu_socket(unsigned ThreadPoolNum) const
Finds the CPU socket where a thread should go.
llvm::ThreadPoolStrategy::ThreadsRequested
unsigned ThreadsRequested
Definition:Threading.h:121
llvm::ThreadPoolStrategy::UseHyperThreads
bool UseHyperThreads
Definition:Threading.h:125
llvm::ThreadPoolStrategy::apply_thread_strategy
void apply_thread_strategy(unsigned ThreadPoolNum) const
Assign the current thread to an ideal hardware CPU or NUMA node.
llvm::ThreadPoolStrategy::compute_thread_count
unsigned compute_thread_count() const
Retrieves the max available threads for the current strategy.
Definition:Threading.cpp:41
uint32_t
uint64_t
llvm::sys::MemoryFence
void MemoryFence()
Definition:Atomic.cpp:30
llvm::sys::CompareAndSwap
cas_flag CompareAndSwap(volatile cas_flag *ptr, cas_flag new_value, cas_flag old_value)
Definition:Atomic.cpp:44
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::hardware_concurrency
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition:Threading.h:184
llvm::heavyweight_hardware_concurrency
ThreadPoolStrategy heavyweight_hardware_concurrency(unsigned ThreadCount=0)
Returns a thread strategy for tasks requiring significant memory or other resources.
Definition:Threading.h:161
llvm::ThreadPriority
ThreadPriority
Definition:Threading.h:251
llvm::ThreadPriority::Low
@ Low
Lower the current thread's priority such that it does not affect foreground tasks significantly.
llvm::ThreadPriority::Background
@ Background
Lower the current thread's priority as much as possible.
llvm::set_thread_priority
SetThreadPriorityResult set_thread_priority(ThreadPriority Priority)
llvm::llvm_is_multithreaded
constexpr bool llvm_is_multithreaded()
Returns true if LLVM is compiled with support for multi-threading, and false otherwise.
Definition:Threading.h:52
llvm::InitStatus
InitStatus
Definition:Threading.h:60
llvm::Uninitialized
@ Uninitialized
Definition:Threading.h:60
llvm::Wait
@ Wait
Definition:Threading.h:60
llvm::Done
@ Done
Definition:Threading.h:60
llvm::get_thread_affinity_mask
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:39
llvm::get_max_thread_name_length
uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
Definition:Threading.cpp:33
llvm::get_cpus
unsigned get_cpus()
Returns how many physical CPUs or NUMA groups the system has.
llvm::optimal_concurrency
ThreadPoolStrategy optimal_concurrency(unsigned TaskCount=0)
Returns an optimal thread strategy to execute specified amount of tasks.
Definition:Threading.h:205
llvm::set_thread_name
void set_thread_name(const Twine &Name)
Set the name of the current thread.
Definition:Threading.cpp:35
llvm::SetThreadPriorityResult
SetThreadPriorityResult
Definition:Threading.h:266
llvm::SetThreadPriorityResult::FAILURE
@ FAILURE
llvm::SetThreadPriorityResult::SUCCESS
@ SUCCESS
llvm::get_thread_name
void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
Definition:Threading.cpp:37
llvm::get_physical_cores
int get_physical_cores()
Returns how many physical cores (as opposed to logical cores returned from thread::hardware_concurren...
Definition:Threading.cpp:47
llvm::get_threadpool_strategy
std::optional< ThreadPoolStrategy > get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default={})
Build a strategy from a number of threads as a string provided in Num.
Definition:Threading.cpp:97
llvm::get_threadid
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
Definition:Threading.cpp:31
llvm::call_once
void call_once(once_flag &flag, Function &&F, Args &&... ArgList)
Execute the function specified as a parameter once.
Definition:Threading.h:86
llvm::InstructionUniformity::Default
@ Default
The result values are uniform if and only if all operands are uniform.
llvm::once_flag
The llvm::once_flag structure.
Definition:Threading.h:67
llvm::once_flag::status
volatile sys::cas_flag status
Definition:Threading.h:68

Generated on Thu Jul 17 2025 10:23:01 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp