brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.9 KiB · 9da357a Raw
120 lines · cpp
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-exception6//7//===----------------------------------------------------------------------===//8//9// This file defines helper functions for running LLVM in a multi-threaded10// environment.11//12//===----------------------------------------------------------------------===//13 14#include "llvm/Support/Threading.h"15#include "llvm/Config/config.h"16#include "llvm/Config/llvm-config.h"17#include "llvm/Support/Jobserver.h"18 19#include <cassert>20#include <optional>21#include <stdlib.h>22 23using namespace llvm;24 25//===----------------------------------------------------------------------===//26//=== WARNING: Implementation here must contain only TRULY operating system27//===          independent code.28//===----------------------------------------------------------------------===//29 30#if LLVM_ENABLE_THREADS == 0 ||                                                \31    (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))32uint64_t llvm::get_threadid() { return 0; }33 34uint32_t llvm::get_max_thread_name_length() { return 0; }35 36void llvm::set_thread_name(const Twine &Name) {}37 38void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }39 40llvm::BitVector llvm::get_thread_affinity_mask() { return {}; }41 42unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {43  // When threads are disabled, ensure clients will loop at least once.44  return 1;45}46 47// Unknown if threading turned off48int llvm::get_physical_cores() { return -1; }49 50#else51 52static int computeHostNumHardwareThreads();53 54unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {55  if (UseJobserver)56    if (auto JS = JobserverClient::getInstance())57      return JS->getNumJobs();58 59  int MaxThreadCount =60      UseHyperThreads ? computeHostNumHardwareThreads() : get_physical_cores();61  if (MaxThreadCount <= 0)62    MaxThreadCount = 1;63  if (ThreadsRequested == 0)64    return MaxThreadCount;65  if (!Limit)66    return ThreadsRequested;67  return std::min((unsigned)MaxThreadCount, ThreadsRequested);68}69 70// Include the platform-specific parts of this class.71#ifdef LLVM_ON_UNIX72#include "Unix/Threading.inc"73#endif74#ifdef _WIN3275#include "Windows/Threading.inc"76#endif77 78// Must be included after Threading.inc to provide definition for llvm::thread79// because FreeBSD's condvar.h (included by user.h) misuses the "thread"80// keyword.81#include "llvm/Support/thread.h"82 83#if defined(__APPLE__)84  // Darwin's default stack size for threads except the main one is only 512KB,85  // which is not enough for some/many normal LLVM compilations. This implements86  // the same interface as std::thread but requests the same stack size as the87  // main thread (8MB) before creation.88const std::optional<unsigned> llvm::thread::DefaultStackSize = 8 * 1024 * 1024;89#elif defined(_AIX)90  // On AIX, the default pthread stack size limit is ~192k for 64-bit programs.91  // This limit is easily reached when doing link-time thinLTO. AIX library92  // developers have used 4MB, so we'll do the same.93const std::optional<unsigned> llvm::thread::DefaultStackSize = 4 * 1024 * 1024;94#else95const std::optional<unsigned> llvm::thread::DefaultStackSize;96#endif97 98 99#endif100 101std::optional<ThreadPoolStrategy>102llvm::get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default) {103  if (Num == "all")104    return llvm::hardware_concurrency();105  if (Num.empty())106    return Default;107  unsigned V;108  if (Num.getAsInteger(10, V))109    return std::nullopt; // malformed 'Num' value110  if (V == 0)111    return Default;112 113  // Do not take the Default into account. This effectively disables114  // heavyweight_hardware_concurrency() if the user asks for any number of115  // threads on the cmd-line.116  ThreadPoolStrategy S = llvm::hardware_concurrency();117  S.ThreadsRequested = V;118  return S;119}120