LLVM 23.0.0git
Threading.cpp
Go to the documentation of this file.
1//===-- llvm/Support/Threading.cpp- 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 defines helper functions for running LLVM in a multi-threaded
10// environment.
11//
12//===----------------------------------------------------------------------===//
13
15#include "llvm/Config/config.h"
16#include "llvm/Config/llvm-config.h"
18
19#include <cassert>
20#include <optional>
21#include <stdlib.h>
22
23//===----------------------------------------------------------------------===//
24//=== WARNING: Implementation here must contain only TRULY operating system
25//=== independent code.
26//===----------------------------------------------------------------------===//
27
28#if LLVM_ENABLE_THREADS == 0 || \
29 (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
30
31using namespace llvm;
32
34
36
37void llvm::set_thread_name(const Twine &Name) {}
38
39void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
40
42
44 // When threads are disabled, ensure clients will loop at least once.
45 return 1;
46}
47
48// Unknown if threading turned off
49int llvm::get_physical_cores() { return -1; }
50
51#else
52
53static int computeHostNumHardwareThreads();
54
55// Include the platform-specific parts of this class.
56#ifdef LLVM_ON_UNIX
57#include "Unix/Threading.inc"
58#endif
59#ifdef _WIN32
60#include "Windows/Threading.inc"
61#endif
62
63using namespace llvm;
64
66 if (UseJobserver)
67 if (auto JS = JobserverClient::getInstance())
68 return JS->getNumJobs();
69
70 int MaxThreadCount =
71 UseHyperThreads ? computeHostNumHardwareThreads() : get_physical_cores();
72 if (MaxThreadCount <= 0)
73 MaxThreadCount = 1;
74 if (ThreadsRequested == 0)
75 return MaxThreadCount;
76 if (!Limit)
77 return ThreadsRequested;
78 return std::min((unsigned)MaxThreadCount, ThreadsRequested);
79}
80
81#if defined(__APPLE__)
82 // Darwin's default stack size for threads except the main one is only 512KB,
83 // which is not enough for some/many normal LLVM compilations. This implements
84 // the same interface as std::thread but requests the same stack size as the
85 // main thread (8MB) before creation.
86const std::optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024;
87#elif defined(_AIX)
88 // On AIX, the default pthread stack size limit is ~192k for 64-bit programs.
89 // This limit is easily reached when doing link-time thinLTO. AIX library
90 // developers have used 4MB, so we'll do the same.
91const std::optional<unsigned> llvm::thread::DefaultStackSize = 4 * 1024 * 1024;
92#else
93const std::optional<unsigned> llvm::thread::DefaultStackSize;
94#endif
95
96
97#endif
98
99std::optional<ThreadPoolStrategy>
101 if (Num == "all")
103 if (Num.empty())
104 return Default;
105 unsigned V;
106 if (Num.getAsInteger(10, V))
107 return std::nullopt; // malformed 'Num' value
108 if (V == 0)
109 return Default;
110
111 // Do not take the Default into account. This effectively disables
112 // heavyweight_hardware_concurrency() if the user asks for any number of
113 // threads on the cmd-line.
115 S.ThreadsRequested = V;
116 return S;
117}
static LLVM_ABI_FOR_TEST JobserverClient * getInstance()
Returns the singleton instance of the JobserverClient.
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition StringRef.h:472
constexpr bool empty() const
empty - Check if the string is empty.
Definition StringRef.h:143
This tells how a thread pool will be used.
Definition Threading.h:115
LLVM_ABI unsigned compute_thread_count() const
Retrieves the max available threads for the current strategy.
Definition Threading.cpp:43
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
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition Threading.h:190
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:41
LLVM_ABI uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
Definition Threading.cpp:35
LLVM_ABI void set_thread_name(const Twine &Name)
Set the name of the current thread.
Definition Threading.cpp:37
LLVM_ABI void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
Definition Threading.cpp:39
LLVM_ABI int get_physical_cores()
Returns how many physical cores (as opposed to logical cores returned from thread::hardware_concurren...
Definition Threading.cpp:49
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:33
@ Default
The result values are uniform if and only if all operands are uniform.
Definition Uniformity.h:20