From 0db351113204a31905e7f0bb118ddd5f2178f484 Mon Sep 17 00:00:00 2001 From: Gaetan Lepage Date: Thu, 23 Apr 2026 13:14:02 +0000 Subject: [PATCH] cudaPackages.cuda_crt: patch math_functions.h signatures --- .../cuda-modules/packages/cuda_crt.nix | 31 ++++++++++++++++++- .../cuda-modules/packages/cuda_nvcc.nix | 6 ++++ 2 files changed, 36 insertions(+), 1 deletion(-) diff --git a/pkgs/development/cuda-modules/packages/cuda_crt.nix b/pkgs/development/cuda-modules/packages/cuda_crt.nix index 3cf2246235bd..914967d7cc8d 100644 --- a/pkgs/development/cuda-modules/packages/cuda_crt.nix +++ b/pkgs/development/cuda-modules/packages/cuda_crt.nix @@ -1,10 +1,39 @@ -{ backendStdenv, buildRedist }: +{ + lib, + backendStdenv, + buildRedist, + glibc, +}: buildRedist { redistName = "cuda"; pname = "cuda_crt"; outputs = [ "out" ]; + # Fix compatibility with glibc 2.42: + # CUDA >= 13.0 fixed sinpi/cospi (using __NV_IEC_60559_FUNCS_EXCEPTION_SPECIFIER), but + # rsqrt/rsqrtf in math_functions.h still lack noexcept, conflicting with glibc 2.42's + # declarations. + postInstall = lib.optionalString (lib.versionAtLeast glibc.version "2.42") '' + nixLog "Patching math_functions.h rsqrt signatures to match glibc's ones" + substituteInPlace "''${!outputInclude:?}/include/crt/math_functions.h" \ + --replace-fail \ + "rsqrt(double x);" \ + "rsqrt(double x) noexcept (true);" \ + --replace-fail \ + "rsqrtf(float x);" \ + "rsqrtf(float x) noexcept (true);" + + nixLog "Patching math_functions.hpp rsqrt signatures to match glibc's ones" + substituteInPlace "''${!outputInclude:?}/include/crt/math_functions.hpp" \ + --replace-fail \ + "__func__(double rsqrt(const double a))" \ + "__func__(double rsqrt(const double a) throw())" \ + --replace-fail \ + "__func__(float rsqrtf(const float a))" \ + "__func__(float rsqrtf(const float a) throw())" + ''; + brokenAssertions = [ # TODO(@connorbaker): Build fails on x86 when using pkgsLLVM. # .../include/crt/host_defines.h:67:2: diff --git a/pkgs/development/cuda-modules/packages/cuda_nvcc.nix b/pkgs/development/cuda-modules/packages/cuda_nvcc.nix index 404811ffa2f5..2bb34f2cf84f 100644 --- a/pkgs/development/cuda-modules/packages/cuda_nvcc.nix +++ b/pkgs/development/cuda-modules/packages/cuda_nvcc.nix @@ -168,6 +168,11 @@ buildRedist (finalAttrs: { # The cospi|sinpi|rsqrt function signatures in include/common/math_functions.h do not match # glibc 2.42's. # Indeed, there they are declared with noexcept(true) which is not the case in cuda_nvcc. + # - In CUDA < 13.0, sinpi/cospi/rsqrt all lack exception specifiers. + # - In CUDA >= 13.0, NVIDIA fixed sinpi/cospi (using __NV_IEC_60559_FUNCS_EXCEPTION_SPECIFIER) + # but rsqrt/rsqrtf still lack noexcept, so we only patch those. + # As the CRT headers (including math_functions.h) moved to the cuda_crt package, the glibc + # 2.42 compatibility patch is applied there instead. + lib.optionalString (cudaOlder "13.0" && lib.versionAtLeast glibc.version "2.42") '' nixLog "Patching math_functions.h signatures to match glibc's ones" substituteInPlace "''${!outputInclude:?}/include/crt/math_functions.h" \ @@ -213,6 +218,7 @@ buildRedist (finalAttrs: { "__func__(float cospif(const float a))" \ "__func__(float cospif(const float a) throw())" '' + # Fix clang CUDA compilation: host_defines.h redefines __noinline__ as # __attribute__((noinline)), which conflicts with libstdc++ >=12 using # __attribute__((__noinline__)) — the macro expands to