xref: /llvm-project/llvm/lib/Support/Threading.cpp (revision 48c68a630e06666101c16aa371f9202a4a53438b)
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 #include "llvm/Support/thread.h"
19 
20 #include <cassert>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 using namespace llvm;
26 
27 //===----------------------------------------------------------------------===//
28 //=== WARNING: Implementation here must contain only TRULY operating system
29 //===          independent code.
30 //===----------------------------------------------------------------------===//
31 
32 bool llvm::llvm_is_multithreaded() {
33 #if LLVM_ENABLE_THREADS != 0
34   return true;
35 #else
36   return false;
37 #endif
38 }
39 
40 #if LLVM_ENABLE_THREADS == 0 ||                                                \
41     (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
42 uint64_t llvm::get_threadid() { return 0; }
43 
44 uint32_t llvm::get_max_thread_name_length() { return 0; }
45 
46 void llvm::set_thread_name(const Twine &Name) {}
47 
48 void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
49 
50 llvm::BitVector llvm::get_thread_affinity_mask() { return {}; }
51 
52 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
53   // When threads are disabled, ensure clients will loop at least once.
54   return 1;
55 }
56 
57 #else
58 
59 int computeHostNumHardwareThreads();
60 
61 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
62   int MaxThreadCount = UseHyperThreads ? computeHostNumHardwareThreads()
63                                        : sys::getHostNumPhysicalCores();
64   if (MaxThreadCount <= 0)
65     MaxThreadCount = 1;
66   if (ThreadsRequested == 0)
67     return MaxThreadCount;
68   if (!Limit)
69     return ThreadsRequested;
70   return std::min((unsigned)MaxThreadCount, ThreadsRequested);
71 }
72 
73 // Include the platform-specific parts of this class.
74 #ifdef LLVM_ON_UNIX
75 #include "Unix/Threading.inc"
76 #endif
77 #ifdef _WIN32
78 #include "Windows/Threading.inc"
79 #endif
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.
86 const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024;
87 #else
88 const llvm::Optional<unsigned> llvm::thread::DefaultStackSize = None;
89 #endif
90 
91 
92 #endif
93 
94 Optional<ThreadPoolStrategy>
95 llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) {
96   if (Num == "all")
97     return llvm::hardware_concurrency();
98   if (Num.empty())
99     return Default;
100   unsigned V;
101   if (Num.getAsInteger(10, V))
102     return None; // malformed 'Num' value
103   if (V == 0)
104     return Default;
105 
106   // Do not take the Default into account. This effectively disables
107   // heavyweight_hardware_concurrency() if the user asks for any number of
108   // threads on the cmd-line.
109   ThreadPoolStrategy S = llvm::hardware_concurrency();
110   S.ThreadsRequested = V;
111   return S;
112 }
113