xref: /freebsd/contrib/llvm-project/llvm/lib/Support/Threading.cpp (revision 8a271827e7b5d5310e06df1f9f49ba0ef9efd263)
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 
14 #include "llvm/Support/Threading.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Host.h"
18 
19 #include <cassert>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 using namespace llvm;
25 
26 //===----------------------------------------------------------------------===//
27 //=== WARNING: Implementation here must contain only TRULY operating system
28 //===          independent code.
29 //===----------------------------------------------------------------------===//
30 
31 bool llvm::llvm_is_multithreaded() {
32 #if LLVM_ENABLE_THREADS != 0
33   return true;
34 #else
35   return false;
36 #endif
37 }
38 
39 #if LLVM_ENABLE_THREADS == 0 ||                                                \
40     (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
41 uint64_t llvm::get_threadid() { return 0; }
42 
43 uint32_t llvm::get_max_thread_name_length() { return 0; }
44 
45 void llvm::set_thread_name(const Twine &Name) {}
46 
47 void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
48 
49 llvm::BitVector llvm::get_thread_affinity_mask() { return {}; }
50 
51 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
52   // When threads are disabled, ensure clients will loop at least once.
53   return 1;
54 }
55 
56 #else
57 
58 int computeHostNumHardwareThreads();
59 
60 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
61   int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads()
62                                        : sys::getHostNumPhysicalCores();
63   if (MaxThreadCount <= 0)
64     MaxThreadCount = 1;
65   // Damage control threading.
66   //
67   // There are no heuristics to figure out how many threads makes sense to spawn,
68   // all while rolling with all available hw threads starts being detrimental to
69   // performance really early.
70   //
71   // Work around by putting a hard cap unless the user explicitly requested a certain amount.
72   //
73   // See https://discourse.llvm.org/t/avoidable-overhead-from-threading-by-default/69160
74   // for more details.
75   if (ThreadsRequested == 0) {
76     return std::min(MaxThreadCount, 4);
77   }
78   if (!Limit)
79     return ThreadsRequested;
80   return std::min((unsigned)MaxThreadCount, ThreadsRequested);
81 }
82 
83 // Include the platform-specific parts of this class.
84 #ifdef LLVM_ON_UNIX
85 #include "Unix/Threading.inc"
86 #endif
87 #ifdef _WIN32
88 #include "Windows/Threading.inc"
89 #endif
90 
91 // Must be included after Threading.inc to provide definition for llvm::thread
92 // because FreeBSD's condvar.h (included by user.h) misuses the "thread"
93 // keyword.
94 #include "llvm/Support/thread.h"
95 
96 #if defined(__APPLE__)
97   // Darwin's default stack size for threads except the main one is only 512KB,
98   // which is not enough for some/many normal LLVM compilations. This implements
99   // the same interface as std::thread but requests the same stack size as the
100   // main thread (8MB) before creation.
101 const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024;
102 #else
103 const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = None;
104 #endif
105 
106 
107 #endif
108 
109 Optional<ThreadPoolStrategy>
110 llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) {
111   if (Num == "all")
112     return llvm::hardware_concurrency();
113   if (Num.empty())
114     return Default;
115   unsigned V;
116   if (Num.getAsInteger(10, V))
117     return None; // malformed 'Num' value
118   if (V == 0)
119     return Default;
120 
121   // Do not take the Default into account. This effectively disables
122   // heavyweight_hardware_concurrency() if the user asks for any number of
123   // threads on the cmd-line.
124   ThreadPoolStrategy S = llvm::hardware_concurrency();
125   S.ThreadsRequested = V;
126   return S;
127 }
128