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