LLVM  16.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 
14 #include "llvm/Support/Threading.h"
15 #include "llvm/ADT/Optional.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Config/llvm-config.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 #if LLVM_ENABLE_THREADS == 0 || \
32  (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
33 uint64_t llvm::get_threadid() { return 0; }
34 
36 
37 void llvm::set_thread_name(const Twine &Name) {}
38 
39 void 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
49 int llvm::get_physical_cores() { return -1; }
50 
51 #else
52 
54 
56  int MaxThreadCount =
58  if (MaxThreadCount <= 0)
59  MaxThreadCount = 1;
60  if (ThreadsRequested == 0)
61  return MaxThreadCount;
62  if (!Limit)
63  return ThreadsRequested;
64  return std::min((unsigned)MaxThreadCount, ThreadsRequested);
65 }
66 
67 // Include the platform-specific parts of this class.
68 #ifdef LLVM_ON_UNIX
69 #include "Unix/Threading.inc"
70 #endif
71 #ifdef _WIN32
72 #include "Windows/Threading.inc"
73 #endif
74 
75 // Must be included after Threading.inc to provide definition for llvm::thread
76 // because FreeBSD's condvar.h (included by user.h) misuses the "thread"
77 // keyword.
78 #include "llvm/Support/thread.h"
79 
80 #if defined(__APPLE__)
81  // Darwin's default stack size for threads except the main one is only 512KB,
82  // which is not enough for some/many normal LLVM compilations. This implements
83  // the same interface as std::thread but requests the same stack size as the
84  // main thread (8MB) before creation.
86 #else
88 #endif
89 
90 
91 #endif
92 
93 std::optional<ThreadPoolStrategy>
95  if (Num == "all")
97  if (Num.empty())
98  return Default;
99  unsigned V;
100  if (Num.getAsInteger(10, V))
101  return std::nullopt; // malformed 'Num' value
102  if (V == 0)
103  return Default;
104 
105  // Do not take the Default into account. This effectively disables
106  // heavyweight_hardware_concurrency() if the user asks for any number of
107  // threads on the cmd-line.
109  S.ThreadsRequested = V;
110  return S;
111 }
Threading.inc
llvm::ThreadPoolStrategy::compute_thread_count
unsigned compute_thread_count() const
Retrieves the max available threads for the current strategy.
Definition: Threading.cpp:55
llvm::hardware_concurrency
ThreadPoolStrategy hardware_concurrency(unsigned ThreadCount=0)
Returns a default thread strategy where all available hardware resources are to be used,...
Definition: Threading.h:185
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
Optional.h
llvm::ThreadPoolStrategy::UseHyperThreads
bool UseHyperThreads
Definition: Threading.h:126
llvm::get_threadpool_strategy
std::optional< ThreadPoolStrategy > get_threadpool_strategy(StringRef Num, ThreadPoolStrategy Default={})
Build a strategy from a number of threads as a string provided in Num.
Definition: Threading.cpp:94
llvm::ThreadPoolStrategy
This tells how a thread pool will be used.
Definition: Threading.h:116
llvm::Optional< unsigned >
llvm::get_threadid
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
llvm::StringRef::getAsInteger
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:474
llvm::get_max_thread_name_length
uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
llvm::get_thread_name
void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
llvm::BitVector
Definition: BitVector.h:75
llvm::StringRef::empty
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
Threading.h
uint64_t
llvm::ThreadPoolStrategy::ThreadsRequested
unsigned ThreadsRequested
Definition: Threading.h:122
llvm::get_physical_cores
int get_physical_cores()
Returns how many physical cores (as opposed to logical cores returned from thread::hardware_concurren...
Threading.inc
llvm::min
Expected< ExpressionValue > min(const ExpressionValue &Lhs, const ExpressionValue &Rhs)
Definition: FileCheck.cpp:357
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
uint32_t
S
add sub stmia L5 ldr r0 bl L_printf $stub Instead of a and a wouldn t it be better to do three moves *Return an aggregate type is even return S
Definition: README.txt:210
llvm::thread::DefaultStackSize
static const llvm::Optional< unsigned > DefaultStackSize
Definition: thread.h:75
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
llvm::GraphProgram::Name
Name
Definition: GraphWriter.h:50
llvm::set_thread_name
void set_thread_name(const Twine &Name)
Set the name of the current thread.
llvm::ThreadPoolStrategy::Limit
bool Limit
Definition: Threading.h:130
llvm::get_thread_affinity_mask
llvm::BitVector get_thread_affinity_mask()
Returns a mask that represents on which hardware thread, core, CPU, NUMA group, the calling thread ca...
llvm::SmallVectorImpl< char >
thread.h
computeHostNumHardwareThreads
static int computeHostNumHardwareThreads()