git: dfff2e2d5c36 - stable/12 - For llvm's internal function which retrieves the number of available "hardware threads", use cpuset_getaffinity(2) on FreeBSD, so it will honor processor sets configured by the cpuset(1) command.

From: Dimitry Andric <dim_at_FreeBSD.org>
Date: Wed, 22 Dec 2021 10:06:24 UTC
The branch stable/12 has been updated by dim:

URL: https://cgit.FreeBSD.org/src/commit/?id=dfff2e2d5c3629ed88f3dcdce79a88653ca36f23

commit dfff2e2d5c3629ed88f3dcdce79a88653ca36f23
Author:     Dimitry Andric <dim@FreeBSD.org>
AuthorDate: 2020-11-18 19:55:24 +0000
Commit:     Dimitry Andric <dim@FreeBSD.org>
CommitDate: 2021-12-22 10:01:22 +0000

    For llvm's internal function which retrieves the number of available
    "hardware threads", use cpuset_getaffinity(2) on FreeBSD, so it will
    honor processor sets configured by the cpuset(1) command.
    
    This should make it possible to avoid e.g. lld creating a huge number of
    threads on a machine with many cores, even for linking simple programs.
    
    This will also be submitted upstream.
    
    Submitted by:   mjg
    MFC after:      1 week
    
    (cherry picked from commit 991f6e7534a9b1a99b7da711676e6714e2cf6680)
---
 contrib/llvm-project/llvm/lib/Support/Unix/Threading.inc | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/contrib/llvm-project/llvm/lib/Support/Unix/Threading.inc b/contrib/llvm-project/llvm/lib/Support/Unix/Threading.inc
index 2d0aacabf092..df290d2262ea 100644
--- a/contrib/llvm-project/llvm/lib/Support/Unix/Threading.inc
+++ b/contrib/llvm-project/llvm/lib/Support/Unix/Threading.inc
@@ -26,6 +26,10 @@
 #include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np()
 #endif
 
+#if defined(__FreeBSD__)
+#include <sys/cpuset.h>
+#endif
+
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 #include <errno.h>
 #include <sys/sysctl.h>
@@ -282,6 +286,13 @@ SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
 #include <thread>
 
 int computeHostNumHardwareThreads() {
+#ifdef __FreeBSD__
+  cpuset_t mask;
+  CPU_ZERO(&mask);
+  if (cpuset_getaffinity(CPU_LEVEL_WHICH, CPU_WHICH_TID, -1, sizeof(mask),
+                         &mask) == 0)
+    return CPU_COUNT(&mask);
+#endif
 #ifdef __linux__
   cpu_set_t Set;
   if (sched_getaffinity(0, sizeof(Set), &Set) == 0)