From aaf3aa3a7c02bdf31706dc98d51e49c6e7e08feb Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Fri, 23 Oct 2026 18:14:32 +0200 Subject: [PATCH] metal: the last twelve CUDA names (dim3, funnel shift, transcendentals, ldg/ldcv, shfl_up) - no undeclared name left A full-list compile (-ferror-limit=0) found 350 errors behind the 20-error display cap, and they collapsed into twelve missing families. With dim3, __threadfence_system, __funnelshift_r/l, rsqrtf, __expf/__logf/__sinf/__cosf, sincosf, __ldg, __ldcv, __float2int_* and __shfl_up_sync in cuda_device_builtins.h, plus the C math names in scope, the counts are ds4_iq 52, ds4_gguf 42, ds4_kernels 132, ds4_vision 27 (was 66/67/185/32) AND the distinct-names list is now EMPTY: what is left is not a missing name but device construct - the <<>> launch syntax and shared-memory declarations that a C++ compiler cannot parse as written. That is the boundary of the host-compile audit, which was its purpose: the API surface is complete, and the remaining work is the MSL implementation itself. --- .../metal_compat/cuda_device_builtins.h | 31 +++++++++++++++++++ metal/include/metal_compat/cuda_runtime.h | 9 ++++++ 2 files changed, 40 insertions(+) diff --git a/metal/include/metal_compat/cuda_device_builtins.h b/metal/include/metal_compat/cuda_device_builtins.h index b9d74463..95c9b9fd 100644 --- a/metal/include/metal_compat/cuda_device_builtins.h +++ b/metal/include/metal_compat/cuda_device_builtins.h @@ -7,6 +7,10 @@ #include +struct dim3 { + unsigned x, y, z; + dim3(unsigned _x = 1, unsigned _y = 1, unsigned _z = 1) : x(_x), y(_y), z(_z) {} +}; struct strata_dim3 { unsigned x, y, z; }; static strata_dim3 threadIdx{0, 0, 0}, blockIdx{0, 0, 0}, blockDim{1, 1, 1}, gridDim{1, 1, 1}; @@ -116,3 +120,30 @@ static inline float __fdiv_rn(float a, float b) { return a / b; } static inline float __frcp_rn_(float a) { return 1.0f / a; } template static inline T strata_min(T a, T b) { return a < b ? a : b; } template static inline T strata_max(T a, T b) { return a > b ? a : b; } + +// the last names the kernel sources use (a full-list compile found twelve families, 2026-10-23): the grid type above, a +// system fence, the funnel shift, the hardware transcendentals and the read-only/volatile loads. +static inline void __threadfence_system() {} +static inline unsigned __funnelshift_r(unsigned lo, unsigned hi, unsigned shift) { + const unsigned long long v = ((unsigned long long) hi << 32) | lo; + return (unsigned) ((v >> (shift & 31)) & 0xffffffffu); +} +static inline unsigned __funnelshift_l(unsigned lo, unsigned hi, unsigned shift) { + const unsigned long long v = ((unsigned long long) hi << 32) | lo; + return (unsigned) (((v << (shift & 31)) >> 32) & 0xffffffffu); +} +static inline float rsqrtf(float a) { return 1.0f / __builtin_sqrtf(a); } +static inline float __expf(float a) { return __builtin_expf(a); } +static inline float __logf(float a) { return __builtin_logf(a); } +static inline float __sinf(float a) { return __builtin_sinf(a); } +static inline float __cosf(float a) { return __builtin_cosf(a); } +static inline void sincosf(float a, float* s, float* c) { *s = __builtin_sinf(a); *c = __builtin_cosf(a); } +static inline int __float2int_rn(float a) { return (int) __builtin_roundf(a); } +static inline int __float2int_rz(float a) { return (int) a; } +static inline int __float2int_rd(float a) { return (int) __builtin_floorf(a); } +static inline int __float2int_ru(float a) { return (int) __builtin_ceilf(a); } +template static inline T __ldg(const T* p) { return *p; } +template static inline T __ldcv(const T* p) { return *p; } +static inline unsigned __shfl_up_sync(unsigned, unsigned v, int) { return v; } +static inline float __shfl_up_sync(unsigned, float v, int) { return v; } +static inline unsigned __shfl_sync(unsigned, unsigned v, int) { return v; } diff --git a/metal/include/metal_compat/cuda_runtime.h b/metal/include/metal_compat/cuda_runtime.h index 281ffe77..fd7cb93c 100644 --- a/metal/include/metal_compat/cuda_runtime.h +++ b/metal/include/metal_compat/cuda_runtime.h @@ -42,6 +42,15 @@ #endif #include "cuda_device_builtins.h" +#include + +using std::fmaxf; +using std::fminf; +using std::sqrtf; +using std::fabsf; +using std::powf; +using std::expf; +using std::logf; // CUDA's headers put min/max in scope for integers; the sources rely on it template static inline T min(T a, T b) { return strata_min(a, b); }