Backport some patches for highway to add thread naming, futexes and
runtime dispatch support.

The ARM support temporarily (locally) is not enabled as the SVE code
triggers the same compiler issue Chrome does. Once the compiler is
fixed then it can be easily enabled.


Index: Makefile
===================================================================
RCS file: /cvs/ports/devel/highway/Makefile,v
retrieving revision 1.10
diff -u -p -u -p -r1.10 Makefile
--- Makefile    7 Jun 2026 16:01:27 -0000       1.10
+++ Makefile    18 Jul 2026 04:49:31 -0000
@@ -4,6 +4,8 @@ GH_ACCOUNT =    google
 GH_PROJECT =   highway
 GH_TAGNAME =   1.4.0
 
+REVISION =     0
+
 CATEGORIES =   devel
 
 SHARED_LIBS += hwy             0.3 # 0.0
Index: patches/patch-CMakeLists_txt
===================================================================
RCS file: patches/patch-CMakeLists_txt
diff -N patches/patch-CMakeLists_txt
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-CMakeLists_txt        18 Jul 2026 04:49:31 -0000
@@ -0,0 +1,26 @@
+- Add runtime dispatch on FreeBSD and OpenBSD
+  e91b42110588eb0c3acda53465070afe6c6b471d
+
+Index: CMakeLists.txt
+--- CMakeLists.txt.orig
++++ CMakeLists.txt
+@@ -511,6 +511,9 @@ include(CheckIncludeFile)
+ check_include_file(sys/auxv.h  HAVE_SYS_AUXV_H)
+ check_include_file(asm/hwcap.h HAVE_ASM_HWCAP_H)
+ 
++include(CheckSymbolExists)
++check_symbol_exists(elf_aux_info sys/auxv.h HAVE_ELF_AUX_INFO)
++
+ # By default prefer STATIC build (legacy behavior)
+ option(BUILD_SHARED_LIBS "Build shared libraries" OFF)
+ option(HWY_FORCE_STATIC_LIBS "Ignore BUILD_SHARED_LIBS" OFF)
+@@ -540,6 +543,9 @@ if(NOT HAVE_SYS_AUXV_H)
+ endif()
+ if(NOT HAVE_ASM_HWCAP_H)
+   target_compile_definitions(hwy PUBLIC TOOLCHAIN_MISS_ASM_HWCAP_H)
++endif()
++if(HAVE_ELF_AUX_INFO)
++  target_compile_definitions(hwy PUBLIC HWY_HAVE_ELF_AUX_INFO)
+ endif()
+ target_compile_definitions(hwy PUBLIC "${DLLEXPORT_TO_DEFINE}")
+ target_compile_options(hwy PRIVATE ${HWY_FLAGS})
Index: patches/patch-hwy_contrib_thread_pool_futex_h
===================================================================
RCS file: patches/patch-hwy_contrib_thread_pool_futex_h
diff -N patches/patch-hwy_contrib_thread_pool_futex_h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-hwy_contrib_thread_pool_futex_h       18 Jul 2026 04:49:31 
-0000
@@ -0,0 +1,73 @@
+- Add native futex support for OpenBSD via futex()
+  2efdba2c1a9948a0fb5ef34f7e7d577d0e0b6c98
+- Use process-private futexs on OpenBSD
+  2dd02de79314b099eef766d794d3a87b86ef18a9
+
+Index: hwy/contrib/thread_pool/futex.h
+--- hwy/contrib/thread_pool/futex.h.orig
++++ hwy/contrib/thread_pool/futex.h
+@@ -21,8 +21,8 @@
+ // use with shared-memory mappings).
+ //
+ // Futex equivalents: https://outerproduct.net/futex-dictionary.html; we
+-// support Linux/Emscripten/FreeBSD/Apple/Windows and C++20 std::atomic::wait,
+-// plus a NanoSleep fallback.
++// support Linux/Emscripten/FreeBSD/OpenBSD/Apple/Windows and C++20
++// std::atomic::wait, plus a NanoSleep fallback.
+ 
+ #include <time.h>
+ 
+@@ -86,6 +86,10 @@
+ #define HWY_DISABLE_FUTEX
+ #endif
+ 
++#elif HWY_OS_OPENBSD && !defined(HWY_DISABLE_FUTEX)
++#include <sys/time.h>
++#include <sys/futex.h>
++
+ #elif HWY_OS_APPLE && !defined(HWY_DISABLE_FUTEX)
+ // These are private APIs, so add an opt-out.
+ extern "C" {
+@@ -196,6 +200,26 @@ static inline uint32_t BlockUntilDifferent(
+     }
+   }
+ 
++#elif HWY_OS_OPENBSD && !defined(HWY_DISABLE_FUTEX)
++  // Safe to cast because std::atomic is a standard layout type.
++  volatile uint32_t* address =
++      const_cast<volatile uint32_t*>(
++          reinterpret_cast<const volatile uint32_t*>(&current));
++  // _PRIVATE requires this only be used in the same process, and avoids
++  // virtual->physical lookups.
++  const int op = FUTEX_WAIT_PRIVATE;
++  for (;;) {
++    const uint32_t next = current.load(acq);
++    if (next != prev) return next;
++    // timeout=nullptr means wait indefinitely.
++    const int ret = futex(address, op, static_cast<int>(prev),
++                          nullptr, nullptr);
++    if (ret == -1) {
++      HWY_DASSERT(errno == EAGAIN || errno == EINTR ||
++                  errno == ECANCELED);
++    }
++  }
++
+ #elif HWY_OS_WIN && !defined(HWY_DISABLE_FUTEX)
+   // It is always safe to cast to void.
+   volatile void* address =
+@@ -263,6 +287,15 @@ static inline void WakeAll(std::atomic<uint32_t>& curr
+   const int ret = _umtx_op(address, UMTX_OP_WAKE_PRIVATE, INT_MAX, nullptr,
+                            nullptr);
+   HWY_DASSERT(ret >= 0);
++  (void)ret;
++
++#elif HWY_OS_OPENBSD && !defined(HWY_DISABLE_FUTEX)
++  // Safe to cast because std::atomic is a standard layout type.
++  volatile uint32_t* address = reinterpret_cast<volatile uint32_t*>(&current);
++  const int max_to_wake = INT_MAX;  // actually signed
++  const int ret = futex(address, FUTEX_WAKE_PRIVATE, max_to_wake, nullptr,
++                        nullptr);
++  HWY_DASSERT(ret >= 0);  // number woken
+   (void)ret;
+ 
+ #elif HWY_OS_WIN && !defined(HWY_DISABLE_FUTEX)
Index: patches/patch-hwy_contrib_thread_pool_thread_pool_h
===================================================================
RCS file: patches/patch-hwy_contrib_thread_pool_thread_pool_h
diff -N patches/patch-hwy_contrib_thread_pool_thread_pool_h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-hwy_contrib_thread_pool_thread_pool_h 18 Jul 2026 04:49:31 
-0000
@@ -0,0 +1,26 @@
+- Add OpenBSD support to SetThreadName
+  aad3891ad8034231e3d420cdb9e0ed4264353389
+
+Index: hwy/contrib/thread_pool/thread_pool.h
+--- hwy/contrib/thread_pool/thread_pool.h.orig
++++ hwy/contrib/thread_pool/thread_pool.h
+@@ -46,6 +46,10 @@
+ #include <AvailabilityMacros.h>
+ #endif
+ 
++#if HWY_OS_OPENBSD
++#include <pthread_np.h>
++#endif
++
+ #if PROFILER_ENABLED
+ #include <algorithm>  // std::sort
+ 
+@@ -70,6 +74,8 @@ static inline void SetThreadName(const char* format, i
+ #elif HWY_OS_APPLE && (MAC_OS_X_VERSION_MIN_REQUIRED >= 1060)
+   // Different interface: single argument, current thread only.
+   HWY_ASSERT(0 == pthread_setname_np(buf));
++#elif HWY_OS_OPENBSD
++  pthread_set_name_np(pthread_self(), buf);
+ #elif defined(__EMSCRIPTEN__)
+   emscripten_set_thread_name(pthread_self(), buf);
+ #else
Index: patches/patch-hwy_detect_compiler_arch_h
===================================================================
RCS file: /cvs/ports/devel/highway/patches/patch-hwy_detect_compiler_arch_h,v
retrieving revision 1.1
diff -u -p -u -p -r1.1 patch-hwy_detect_compiler_arch_h
--- patches/patch-hwy_detect_compiler_arch_h    7 Jun 2026 16:01:27 -0000       
1.1
+++ patches/patch-hwy_detect_compiler_arch_h    18 Jul 2026 04:49:31 -0000
@@ -1,4 +1,7 @@
-Fix build on sparc64 by not erroring out
+- Add OpenBSD detection
+  2ddca0e08c7dbc7a7c794e96c959acc2a84301bb
+
+- Fix build on sparc64 by not erroring out
 
 Index: hwy/detect_compiler_arch.h
 --- hwy/detect_compiler_arch.h.orig
@@ -12,3 +15,21 @@ Index: hwy/detect_compiler_arch.h
  #endif
  
  
//------------------------------------------------------------------------------
+@@ -473,9 +471,16 @@
+ #define HWY_OS_FREEBSD 0
+ #endif
+ 
++#if defined(__OpenBSD__)
++#define HWY_OS_OPENBSD 1
++#else
++#define HWY_OS_OPENBSD 0
++#endif
++
+ // It is an error to detect multiple OSes at the same time, but OK to
+ // detect none of the above.
+-#if (HWY_OS_WIN + HWY_OS_LINUX + HWY_OS_APPLE + HWY_OS_FREEBSD) > 1
++#if (HWY_OS_WIN + HWY_OS_LINUX + HWY_OS_APPLE + HWY_OS_FREEBSD +             \
++     HWY_OS_OPENBSD) > 1
+ #error "Must not detect more than one OS"
+ #endif
+ 
Index: patches/patch-hwy_detect_targets_h
===================================================================
RCS file: patches/patch-hwy_detect_targets_h
diff -N patches/patch-hwy_detect_targets_h
--- /dev/null   1 Jan 1970 00:00:00 -0000
+++ patches/patch-hwy_detect_targets_h  18 Jul 2026 04:49:31 -0000
@@ -0,0 +1,36 @@
+- Add runtime dispatch on FreeBSD and OpenBSD
+  e91b42110588eb0c3acda53465070afe6c6b471d
+
+- XXX: ARM runtime dispatch support is disabled
+  until LLVM 22 is fixed to build the SVE code.
+
+Index: hwy/detect_targets.h
+--- hwy/detect_targets.h.orig
++++ hwy/detect_targets.h
+@@ -826,6 +826,16 @@
+ #endif
+ #endif  // HWY_HAVE_RUNTIME_DISPATCH_LINUX
+ 
++#ifndef HWY_HAVE_RUNTIME_DISPATCH_FREEBSD_OPENBSD  // allow override
++#if (HWY_ARCH_PPC) && (HWY_OS_FREEBSD || HWY_OS_OPENBSD) && \
++    (HWY_COMPILER_GCC_ACTUAL || HWY_COMPILER_CLANG >= 1700) && \
++    HWY_HAVE_ELF_AUX_INFO
++#define HWY_HAVE_RUNTIME_DISPATCH_FREEBSD_OPENBSD 1
++#else
++#define HWY_HAVE_RUNTIME_DISPATCH_FREEBSD_OPENBSD 0
++#endif
++#endif  // HWY_HAVE_RUNTIME_DISPATCH_FREEBSD_OPENBSD
++
+ // Allow opting out, and without a guarantee of success, opting-in.
+ #ifndef HWY_HAVE_RUNTIME_DISPATCH
+ // Clang, GCC and MSVC allow OS-independent runtime dispatch on x86.
+@@ -834,7 +844,8 @@
+ // is to build two binaries, one with the -msimd128 flag.
+ #if HWY_ARCH_X86 || HWY_HAVE_RUNTIME_DISPATCH_RVV ||                          
\
+     HWY_HAVE_RUNTIME_DISPATCH_APPLE || HWY_HAVE_RUNTIME_DISPATCH_LOONGARCH || 
\
+-    HWY_HAVE_RUNTIME_DISPATCH_LINUX
++    HWY_HAVE_RUNTIME_DISPATCH_LINUX ||                                        
\
++    HWY_HAVE_RUNTIME_DISPATCH_FREEBSD_OPENBSD
+ #define HWY_HAVE_RUNTIME_DISPATCH 1
+ #else
+ #define HWY_HAVE_RUNTIME_DISPATCH 0
Index: patches/patch-hwy_targets_cc
===================================================================
RCS file: /cvs/ports/devel/highway/patches/patch-hwy_targets_cc,v
retrieving revision 1.2
diff -u -p -u -p -r1.2 patch-hwy_targets_cc
--- patches/patch-hwy_targets_cc        6 Jun 2026 12:11:10 -0000       1.2
+++ patches/patch-hwy_targets_cc        18 Jul 2026 04:49:31 -0000
@@ -1,26 +1,30 @@
+- Add runtime dispatch on FreeBSD and OpenBSD
+  e91b42110588eb0c3acda53465070afe6c6b471d
+
 Index: hwy/targets.cc
 --- hwy/targets.cc.orig
 +++ hwy/targets.cc
-@@ -29,7 +29,7 @@
+@@ -39,6 +39,12 @@
+ #include <sys/auxv.h>
+ #endif
  
- #elif (HWY_ARCH_ARM || HWY_ARCH_PPC || HWY_ARCH_S390X || HWY_ARCH_RISCV || \
-        HWY_ARCH_LOONGARCH) &&                                              \
--    HWY_OS_LINUX
-+    (HWY_OS_LINUX || HWY_OS_FREEBSD || defined(__OpenBSD__))
- // sys/auxv.h does not always include asm/hwcap.h, or define HWCAP*, hence we
- // still include this directly. See #1199.
- #if HWY_HAVE_ASM_HWCAP
-@@ -47,6 +47,26 @@
++#elif (HWY_ARCH_ARM || HWY_ARCH_PPC || HWY_ARCH_RISCV) &&                  \
++       (HWY_OS_FREEBSD || HWY_OS_OPENBSD)
++#if HWY_HAVE_ELF_AUX_INFO
++#include <sys/auxv.h>
++#endif
++
+ #endif  // HWY_ARCH_*
+ 
+ #if HWY_OS_APPLE
+@@ -47,6 +53,23 @@
  #endif  // HWY_OS_APPLE
  
  namespace hwy {
 +
-+#if (HWY_ARCH_ARM || HWY_ARCH_PPC || HWY_ARCH_S390X || HWY_ARCH_RISCV || \
-+       HWY_ARCH_LOONGARCH) &&                                            \
-+       (HWY_OS_FREEBSD || defined(__OpenBSD__))
-+#if HWY_HAVE_AUXV
-+// Implement getauxval using elf_aux_info
-+// XXX getauxval and elf_aux_info should be autodetected
++#if (HWY_ARCH_ARM || HWY_ARCH_PPC || HWY_ARCH_RISCV) &&                  \
++       (HWY_OS_FREEBSD || HWY_OS_OPENBSD)
++#if HWY_HAVE_ELF_AUX_INFO
 +static HWY_INLINE HWY_MAYBE_UNUSED unsigned long getauxval(unsigned long 
type) {
 +  unsigned long hwcap = 0;
 +  switch (type) {
@@ -32,7 +36,7 @@ Index: hwy/targets.cc
 +    return 0;
 +  }
 +}
-+#endif // HWY_HAVE_AUXV
++#endif // HWY_HAVE_ELF_AUX_INFO
 +#endif // HWY_ARCH_*
  
  #if HWY_OS_APPLE

Reply via email to