[llvm] [Support] Avoid misguided FreeBSD hack (PR #177508)

Jessica Clarke via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 23 10:14:34 PST 2026


https://github.com/jrtc27 updated https://github.com/llvm/llvm-project/pull/177508

>From 65df18ebe3c71997dc054249bbce9482f76d0cb2 Mon Sep 17 00:00:00 2001
From: Jessica Clarke <jrtc27 at jrtc27.com>
Date: Fri, 23 Jan 2026 04:36:53 +0000
Subject: [PATCH] [Support] Avoid misguided FreeBSD hack

FreeBSD doesn't do anything wrong here, it just happens to define and use a
struct thread in its own headers. The problems arise because here in LLVM we
have using namespace llvm prior to including system headers, which is bad
practice for precisely this reason. If we instead play by the rules and defer
our using namespace llvm until after we've included the system headers then we
no longer need this hack.

This hack is particularly problematic by being conditional on __FreeBSD__ as of
9093ba9f7ee5 ("[Support] Include Support/thread.h before api implementations
(#111175)"), since on non-FreeBSD Threading.inc can reference anything in
Support/thread.h, only causing errors on FreeBSD, which is precisely what
happened in 64be34c562a2 ("Enable using threads on z/OS (#171847)").

By deferring the using namespace llvm until after Threading.inc is included
there may be build failures introduced on untested platforms due to needing to
replace unqualified identifiers with qualified ones by prepending llvm::.
---
 llvm/lib/Support/Threading.cpp         | 28 ++++++++++++--------------
 llvm/lib/Support/Unix/Threading.inc    | 14 +++++--------
 llvm/lib/Support/Windows/Threading.inc |  9 +++++----
 3 files changed, 23 insertions(+), 28 deletions(-)

diff --git a/llvm/lib/Support/Threading.cpp b/llvm/lib/Support/Threading.cpp
index 9da357a7ebb91..90352d2f212a4 100644
--- a/llvm/lib/Support/Threading.cpp
+++ b/llvm/lib/Support/Threading.cpp
@@ -20,8 +20,6 @@
 #include <optional>
 #include <stdlib.h>
 
-using namespace llvm;
-
 //===----------------------------------------------------------------------===//
 //=== WARNING: Implementation here must contain only TRULY operating system
 //===          independent code.
@@ -29,6 +27,9 @@ using namespace llvm;
 
 #if LLVM_ENABLE_THREADS == 0 ||                                                \
     (!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
+
+using namespace llvm;
+
 uint64_t llvm::get_threadid() { return 0; }
 
 uint32_t llvm::get_max_thread_name_length() { return 0; }
@@ -51,6 +52,16 @@ int llvm::get_physical_cores() { return -1; }
 
 static int computeHostNumHardwareThreads();
 
+// Include the platform-specific parts of this class.
+#ifdef LLVM_ON_UNIX
+#include "Unix/Threading.inc"
+#endif
+#ifdef _WIN32
+#include "Windows/Threading.inc"
+#endif
+
+using namespace llvm;
+
 unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
   if (UseJobserver)
     if (auto JS = JobserverClient::getInstance())
@@ -67,19 +78,6 @@ unsigned llvm::ThreadPoolStrategy::compute_thread_count() const {
   return std::min((unsigned)MaxThreadCount, ThreadsRequested);
 }
 
-// Include the platform-specific parts of this class.
-#ifdef LLVM_ON_UNIX
-#include "Unix/Threading.inc"
-#endif
-#ifdef _WIN32
-#include "Windows/Threading.inc"
-#endif
-
-// Must be included after Threading.inc to provide definition for llvm::thread
-// because FreeBSD's condvar.h (included by user.h) misuses the "thread"
-// keyword.
-#include "llvm/Support/thread.h"
-
 #if defined(__APPLE__)
   // Darwin's default stack size for threads except the main one is only 512KB,
   // which is not enough for some/many normal LLVM compilations. This implements
diff --git a/llvm/lib/Support/Unix/Threading.inc b/llvm/lib/Support/Unix/Threading.inc
index 27cbb180e57a7..4967d3f8c0ef5 100644
--- a/llvm/lib/Support/Unix/Threading.inc
+++ b/llvm/lib/Support/Unix/Threading.inc
@@ -33,12 +33,7 @@
 #include <pthread_np.h> // For pthread_getthreadid_np() / pthread_set_name_np()
 #endif
 
-// Must be included after Threading.inc to provide definition for llvm::thread
-// because FreeBSD's condvar.h (included by user.h) misuses the "thread"
-// keyword.
-#ifndef __FreeBSD__
 #include "llvm/Support/thread.h"
-#endif
 
 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
 #include <errno.h>
@@ -278,7 +273,8 @@ void llvm::get_thread_name(SmallVectorImpl<char> &Name) {
 #endif
 }
 
-SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
+llvm::SetThreadPriorityResult
+llvm::set_thread_priority(ThreadPriority Priority) {
 #if (defined(__linux__) || defined(__CYGWIN__)) && defined(SCHED_IDLE)
   // Some *really* old glibcs are missing SCHED_IDLE.
   // http://man7.org/linux/man-pages/man3/pthread_setschedparam.3.html
@@ -376,15 +372,15 @@ static int computeHostNumPhysicalCores() {
                  << "/proc/cpuinfo: " << EC.message() << "\n";
     return -1;
   }
-  SmallVector<StringRef, 8> strs;
+  llvm::SmallVector<llvm::StringRef, 8> strs;
   (*Text)->getBuffer().split(strs, "\n", /*MaxSplit=*/-1,
                              /*KeepEmpty=*/false);
   int CurProcessor = -1;
   int CurPhysicalId = -1;
   int CurSiblings = -1;
   int CurCoreId = -1;
-  for (StringRef Line : strs) {
-    std::pair<StringRef, StringRef> Data = Line.split(':');
+  for (llvm::StringRef Line : strs) {
+    std::pair<llvm::StringRef, llvm::StringRef> Data = Line.split(':');
     auto Name = Data.first.trim();
     auto Val = Data.second.trim();
     // These fields are available if the kernel is configured with CONFIG_SMP.
diff --git a/llvm/lib/Support/Windows/Threading.inc b/llvm/lib/Support/Windows/Threading.inc
index 968423b98486c..6b45a3591e690 100644
--- a/llvm/lib/Support/Windows/Threading.inc
+++ b/llvm/lib/Support/Windows/Threading.inc
@@ -134,7 +134,8 @@ HMODULE loadSystemModuleSecure(LPCWSTR lpModuleName) {
 }
 } // namespace llvm::sys::windows
 
-SetThreadPriorityResult llvm::set_thread_priority(ThreadPriority Priority) {
+llvm::SetThreadPriorityResult
+llvm::set_thread_priority(ThreadPriority Priority) {
 #ifdef THREAD_POWER_THROTTLING_CURRENT_VERSION
   HMODULE kernelM = llvm::sys::windows::loadSystemModuleSecure(L"kernel32.dll");
   if (kernelM) {
@@ -237,9 +238,9 @@ static std::optional<std::vector<USHORT>> getActiveGroups() {
   return Groups;
 }
 
-static ArrayRef<ProcessorGroup> getProcessorGroups() {
+static llvm::ArrayRef<ProcessorGroup> getProcessorGroups() {
   auto computeGroups = []() {
-    SmallVector<ProcessorGroup, 4> Groups;
+    llvm::SmallVector<ProcessorGroup, 4> Groups;
 
     auto HandleGroup = [&](SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX *ProcInfo) {
       GROUP_RELATIONSHIP &El = ProcInfo->Group;
@@ -303,7 +304,7 @@ static ArrayRef<ProcessorGroup> getProcessorGroups() {
     return std::vector<ProcessorGroup>(Groups.begin(), Groups.end());
   };
   static auto Groups = computeGroups();
-  return ArrayRef<ProcessorGroup>(Groups);
+  return llvm::ArrayRef<ProcessorGroup>(Groups);
 }
 
 template <typename R, typename UnaryPredicate>



More information about the llvm-commits mailing list