From bd62420fd3873086cc5e369bbedaecc68caffeff Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Wed, 29 Mar 2023 02:39:46 +0300 Subject: [PATCH 1/4] cudaPackages: use same libstdc++ as the rest of nixpkgs --- .../compilers/cudatoolkit/common.nix | 5 +++-- .../compilers/cudatoolkit/extension.nix | 14 ++++++++++---- .../redist/build-cuda-redist-package.nix | 9 +++++---- .../compilers/cudatoolkit/stdenv.nix | 17 +++++++++++++++++ 4 files changed, 35 insertions(+), 10 deletions(-) create mode 100644 pkgs/development/compilers/cudatoolkit/stdenv.nix diff --git a/pkgs/development/compilers/cudatoolkit/common.nix b/pkgs/development/compilers/cudatoolkit/common.nix index 8082756b1edb..e4ad19406b72 100644 --- a/pkgs/development/compilers/cudatoolkit/common.nix +++ b/pkgs/development/compilers/cudatoolkit/common.nix @@ -29,6 +29,7 @@ args@ , python3 # FIXME: CUDAToolkit 10 may still need python27 , pulseaudio , requireFile +, stdenv , backendStdenv # E.g. gcc11Stdenv, set in extension.nix , unixODBC , wayland @@ -121,8 +122,8 @@ backendStdenv.mkDerivation rec { (placeholder "lib") (placeholder "out") "${placeholder "out"}/nvvm" - # Is it not handled by autoPatchelf automatically? - "${lib.getLib backendStdenv.cc.cc}/lib64" + # NOTE: use the same libstdc++ as the rest of nixpkgs, not from backendStdenv + "${lib.getLib stdenv.cc.cc}/lib64" "${placeholder "out"}/jre/lib/amd64/jli" "${placeholder "out"}/lib64" "${placeholder "out"}/nvvm/lib64" diff --git a/pkgs/development/compilers/cudatoolkit/extension.nix b/pkgs/development/compilers/cudatoolkit/extension.nix index dd6f7ff2abe7..7ed3e0426180 100644 --- a/pkgs/development/compilers/cudatoolkit/extension.nix +++ b/pkgs/development/compilers/cudatoolkit/extension.nix @@ -10,11 +10,17 @@ final: prev: let finalVersion = cudatoolkitVersions.${final.cudaVersion}; # Exposed as cudaPackages.backendStdenv. - # We don't call it just "stdenv" to avoid confusion: e.g. this toolchain doesn't contain nvcc. - # Instead, it's the back-end toolchain for nvcc to use. - # We also use this to link a compatible libstdc++ (backendStdenv.cc.cc.lib) + # This is what nvcc uses as a backend, + # and it has to be an officially supported one (e.g. gcc11 for cuda11). + # + # It, however, propagates current stdenv's libstdc++ to avoid "GLIBCXX_* not found errors" + # when linked with other C++ libraries. + # E.g. for cudaPackages_11_8 we use gcc11 with gcc12's libstdc++ # Cf. https://github.com/NixOS/nixpkgs/pull/218265 for context - backendStdenv = prev.pkgs."${finalVersion.gcc}Stdenv"; + backendStdenv = final.callPackage ./stdenv.nix { + nixpkgsStdenv = prev.pkgs.stdenv; + nvccCompatibleStdenv = prev.pkgs.buildPackages."${finalVersion.gcc}Stdenv"; + }; ### Add classic cudatoolkit package cudatoolkit = diff --git a/pkgs/development/compilers/cudatoolkit/redist/build-cuda-redist-package.nix b/pkgs/development/compilers/cudatoolkit/redist/build-cuda-redist-package.nix index 1b216ee625a8..a0242a91a03b 100644 --- a/pkgs/development/compilers/cudatoolkit/redist/build-cuda-redist-package.nix +++ b/pkgs/development/compilers/cudatoolkit/redist/build-cuda-redist-package.nix @@ -1,4 +1,5 @@ { lib +, stdenv , backendStdenv , fetchurl , autoPatchelfHook @@ -30,11 +31,11 @@ backendStdenv.mkDerivation { ]; buildInputs = [ - # autoPatchelfHook will search for a libstdc++ and we're giving it a - # "compatible" libstdc++ from the same toolchain that NVCC uses. - # + # autoPatchelfHook will search for a libstdc++ and we're giving it + # one that is compatible with the rest of nixpkgs, even when + # nvcc forces us to use an older gcc # NB: We don't actually know if this is the right thing to do - backendStdenv.cc.cc.lib + stdenv.cc.cc.lib ]; dontBuild = true; diff --git a/pkgs/development/compilers/cudatoolkit/stdenv.nix b/pkgs/development/compilers/cudatoolkit/stdenv.nix new file mode 100644 index 000000000000..42ee7f7b3317 --- /dev/null +++ b/pkgs/development/compilers/cudatoolkit/stdenv.nix @@ -0,0 +1,17 @@ +{ nixpkgsStdenv +, nvccCompatibleStdenv +, overrideCC +, wrapCCWith +}: + +overrideCC nixpkgsStdenv (wrapCCWith { + cc = nvccCompatibleStdenv.cc.cc; + + # This option is for clang's libcxx, but we (ab)use it for gcc's libstdc++. + # Note that libstdc++ maintains forward-compatibility: if we load a newer + # libstdc++ into the process, we can still use libraries built against an + # older libstdc++. This, in practice, means that we should use libstdc++ from + # the same stdenv that the rest of nixpkgs uses. + # We currently do not try to support anything other than gcc and linux. + libcxx = nixpkgsStdenv.cc.cc.lib; +}) From ab9c46d27ee48291f4bc7547b3dc1aed1c650334 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Tue, 4 Apr 2023 18:35:47 +0300 Subject: [PATCH 2/4] cudaPackages.cudnn: fix libstdc++ mismatch --- pkgs/development/libraries/science/math/cudnn/generic.nix | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/pkgs/development/libraries/science/math/cudnn/generic.nix b/pkgs/development/libraries/science/math/cudnn/generic.nix index b2844ae6b074..cdfa924b2242 100644 --- a/pkgs/development/libraries/science/math/cudnn/generic.nix +++ b/pkgs/development/libraries/science/math/cudnn/generic.nix @@ -1,4 +1,4 @@ -{ +{ stdenv, backendStdenv, lib, zlib, @@ -26,7 +26,6 @@ maxCudaVersion, }: assert useCudatoolkitRunfile || (libcublas != null); let - inherit (backendStdenv) cc; inherit (lib) lists strings trivial versions; # majorMinorPatch :: String -> String @@ -63,7 +62,10 @@ in # Used by autoPatchelfHook buildInputs = [ - cc.cc.lib # libstdc++ + # Note this libstdc++ isn't from the (possibly older) nvcc-compatible + # stdenv, but from the (newer) stdenv that the rest of nixpkgs uses + stdenv.cc.cc.lib + zlib cudatoolkit_root ]; From 13c47d64aa7b3374cf414ae7d362d0d003cacb4b Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Wed, 5 Apr 2023 19:04:42 +0300 Subject: [PATCH 3/4] faissWithCuda: do not leak the old libstdc++ --- pkgs/development/libraries/science/math/faiss/default.nix | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/pkgs/development/libraries/science/math/faiss/default.nix b/pkgs/development/libraries/science/math/faiss/default.nix index 2a6f144d862f..9d0265a9ce88 100644 --- a/pkgs/development/libraries/science/math/faiss/default.nix +++ b/pkgs/development/libraries/science/math/faiss/default.nix @@ -25,7 +25,7 @@ builtins.head optLevels , faiss # To run demos in the tests , runCommand -}: +}@inputs: assert cudaSupport -> nvidia-thrust.cudaSupport; @@ -33,9 +33,11 @@ let pname = "faiss"; version = "1.7.2"; - inherit (cudaPackages) cudaFlags; + inherit (cudaPackages) cudaFlags backendStdenv; inherit (cudaFlags) cudaCapabilities dropDot; + stdenv = if cudaSupport then backendStdenv else inputs.stdenv; + cudaJoined = symlinkJoin { name = "cuda-packages-unsplit"; paths = with cudaPackages; [ From 3af299f0148ebc81d9f628bbaa8aedba78b57314 Mon Sep 17 00:00:00 2001 From: Someone Serge Date: Tue, 4 Apr 2023 17:51:43 +0300 Subject: [PATCH 4/4] cudaPackages.cudatoolkit: hotfix the libstdc++ mismatch caused by cudatoolkit-vendored copy of the library --- pkgs/development/compilers/cudatoolkit/common.nix | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/development/compilers/cudatoolkit/common.nix b/pkgs/development/compilers/cudatoolkit/common.nix index e4ad19406b72..8b3ceff890e9 100644 --- a/pkgs/development/compilers/cudatoolkit/common.nix +++ b/pkgs/development/compilers/cudatoolkit/common.nix @@ -205,6 +205,7 @@ backendStdenv.mkDerivation rec { mv pkg/builds/nsight_systems/target-linux-x64 $out/target-linux-x64 mv pkg/builds/nsight_systems/host-linux-x64 $out/host-linux-x64 + rm $out/host-linux-x64/libstdc++.so* ''} rm -f $out/tools/CUDA_Occupancy_Calculator.xls # FIXME: why?