Unverified Commit 2cbc1ca9 authored by Emily's avatar Emily Committed by GitHub
Browse files

llvmPackages_19: fix building libclc, add aliases (#336465)

parents 062caf0f 6bb12aca
Loading
Loading
Loading
Loading
+31 −0
Original line number Diff line number Diff line
From e8b910246d0c7c3d9fff994f71c6f8a48ec09a50 Mon Sep 17 00:00:00 2001
From: Tristan Ross <tristan.ross@midstall.com>
Date: Sat, 24 Aug 2024 19:56:24 -0700
Subject: [PATCH] [libclc] use default paths with find_program when possible

---
 libclc/CMakeLists.txt | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 02bb859ae8590b..6bcd8ae52a5794 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -55,7 +55,7 @@ if( LIBCLC_STANDALONE_BUILD OR CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DI
   # Import required tools
   if( NOT EXISTS ${LIBCLC_CUSTOM_LLVM_TOOLS_BINARY_DIR} )
     foreach( tool IN ITEMS clang llvm-as llvm-link opt )
-      find_program( LLVM_TOOL_${tool} ${tool} PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
+      find_program( LLVM_TOOL_${tool} ${tool} PATHS ${LLVM_TOOLS_BINARY_DIR} )
       set( ${tool}_exe ${LLVM_TOOL_${tool}} )
       set( ${tool}_target )
     endforeach()
@@ -104,7 +104,7 @@ foreach( tool IN ITEMS clang opt llvm-as llvm-link )
 endforeach()
 
 # llvm-spirv is an optional dependency, used to build spirv-* targets.
-find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )
+find_program( LLVM_SPIRV llvm-spirv PATHS ${LLVM_TOOLS_BINARY_DIR} )
 
 if( LLVM_SPIRV )
   add_executable( libclc::llvm-spirv IMPORTED GLOBAL )
+6 −0
Original line number Diff line number Diff line
@@ -279,6 +279,12 @@ let
                  path = ../14;
                }
              ];
              "libclc/use-default-paths.patch" = [
                {
                  after = "19";
                  path = ../19;
                }
              ];
            };

            constraints = patches."${p}" or null;
+63 −24
Original line number Diff line number Diff line
{ lib, stdenv, version, runCommand, monorepoSrc, llvm, buildPackages, buildLlvmTools, ninja, cmake, python3 }:

{
  lib,
  stdenv,
  version,
  runCommand,
  monorepoSrc,
  llvm,
  buildPackages,
  buildLlvmTools,
  ninja,
  cmake,
  python3,
  release_version,
  getVersionFile,
}:
let
  spirv-llvm-translator = buildPackages.spirv-llvm-translator.override {
    inherit (buildLlvmTools) llvm;
  };
in
stdenv.mkDerivation rec {
  pname = "libclc";
  inherit version;
@@ -12,14 +30,24 @@ stdenv.mkDerivation rec {

  sourceRoot = "${src.name}/${pname}";

  outputs = [ "out" "dev" ];

  patches = [
    ./libclc/libclc-gnu-install-dirs.patch
  outputs = [
    "out"
    "dev"
  ];

  patches =
    [ ./libclc/libclc-gnu-install-dirs.patch ]
    # LLVM 19 changes how host tools are looked up.
    # Need to remove NO_DEFAULT_PATH and the PATHS arguments for find_program
    # so CMake can actually find the tools in nativeBuildInputs.
    # https://github.com/llvm/llvm-project/pull/105969
    ++ lib.optional (lib.versionAtLeast release_version "19") (
      getVersionFile "libclc/use-default-paths.patch"
    );

  # cmake expects all required binaries to be in the same place, so it will not be able to find clang without the patch
  postPatch = ''
  postPatch =
    lib.optionalString (lib.versionOlder release_version "19") ''
      substituteInPlace CMakeLists.txt \
        --replace 'find_program( LLVM_CLANG clang PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
                  'find_program( LLVM_CLANG clang PATHS "${buildLlvmTools.clang.cc}/bin" NO_DEFAULT_PATH )' \
@@ -30,13 +58,24 @@ stdenv.mkDerivation rec {
        --replace 'find_program( LLVM_OPT opt PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
                  'find_program( LLVM_OPT opt PATHS "${buildLlvmTools.llvm}/bin" NO_DEFAULT_PATH )' \
        --replace 'find_program( LLVM_SPIRV llvm-spirv PATHS ''${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH )' \
                'find_program( LLVM_SPIRV llvm-spirv PATHS "${buildPackages.spirv-llvm-translator.override { inherit (buildLlvmTools) llvm; }}/bin" NO_DEFAULT_PATH )'
  '' + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
                  'find_program( LLVM_SPIRV llvm-spirv PATHS "${spirv-llvm-translator}/bin" NO_DEFAULT_PATH )'
    ''
    + lib.optionalString (stdenv.hostPlatform != stdenv.buildPlatform) ''
      substituteInPlace CMakeLists.txt \
        --replace 'COMMAND prepare_builtins' 'COMMAND ${buildLlvmTools.libclc.dev}/bin/prepare_builtins'
    '';

  nativeBuildInputs = [ cmake ninja python3 ];
  nativeBuildInputs =
    [
      cmake
      ninja
      python3
    ]
    ++ lib.optional (lib.versionAtLeast release_version "19") [
      buildLlvmTools.clang.cc
      buildLlvmTools.llvm
      spirv-llvm-translator
    ];
  buildInputs = [ llvm ];
  strictDeps = true;

+122 −85
Original line number Diff line number Diff line
{ lib, stdenv
, fetchFromGitHub
, fetchpatch
, cmake
, pkg-config
, lit
, llvm
, spirv-headers
, spirv-tools
{
  lib,
  stdenv,
  fetchFromGitHub,
  fetchpatch,
  cmake,
  pkg-config,
  lit,
  llvm,
  spirv-headers,
  spirv-tools,
}:

let
@@ -15,31 +17,50 @@ let

  # ROCm, if actively updated will always be at the latest version
  branch =
    if llvmMajor == "18" then rec {
    if llvmMajor == "19" then
      rec {
        version = "19.1.0";
        rev = "dad1f0eaab8047a4f73c50ed5f3d1694b78aae97";
        hash = "sha256-mUvDF5y+cBnqUaHjyiiE8cJGH5MfQMqGFy6bYv9vCVY=";
      }
    else if llvmMajor == "18" then
      rec {
        version = "18.1.0";
        rev = "v${version}";
        hash = "sha256-64guZiuO7VpaX01wNIjV7cnjEAe6ineMdY44S6sA33k=";
    } else if llvmMajor == "17" || isROCm then rec {
      }
    else if llvmMajor == "17" || isROCm then
      rec {
        version = "17.0.0";
        rev = "v${version}";
        hash = "sha256-Rzm5Py9IPFtS9G7kME+uSwZ/0gPGW6MlL35ZWk4LfHM=";
    } else if llvmMajor == "16" then rec {
      }
    else if llvmMajor == "16" then
      rec {
        version = "16.0.0";
        rev = "v${version}";
        hash = "sha256-EUabcYqSjXshbPmcs1DRLvCSL1nd9rEdpqELBrItCW8=";
    } else if llvmMajor == "15" then rec {
      }
    else if llvmMajor == "15" then
      rec {
        version = "15.0.0";
        rev = "v${version}";
        hash = "sha256-OsDohXRxovtEXaWiRGp8gJ0dXmoALyO+ZimeSO8aPVI=";
    } else if llvmMajor == "14" then {
      }
    else if llvmMajor == "14" then
      {
        version = "14.0.0+unstable-2024-07-15";
        rev = "2823e7052b7999c10fff63bc8089e5aa205716f4";
        hash = "sha256-8/4B74hYge6WiH7PzRGEgE3W7f9IkQ4VMmfkWKYA/l4=";
    } else if llvmMajor == "11" then {
      }
    else if llvmMajor == "11" then
      {
        version = "11.0.0+unstable-2022-05-04";
        rev = "4ef524240833abfeee1c5b9fff6b1bd53f4806b3"; # 267 commits ahead of v11.0.0
        hash = "sha256-NoIoa20+2sH41rEnr8lsMhtfesrtdPINiXtUnxYVm8s=";
    } else throw "Incompatible LLVM version.";
      }
    else
      throw "Incompatible LLVM version.";
in
stdenv.mkDerivation {
  pname = "SPIRV-LLVM-Translator";
@@ -51,7 +72,8 @@ stdenv.mkDerivation {
    inherit (branch) rev hash;
  };

  patches = lib.optionals (llvmMajor == "18") [
  patches =
    lib.optionals (llvmMajor == "18") [
      # Fixes build after SPV_INTEL_maximum_registers breaking change
      # TODO: remove on next spirv-headers release
      (fetchpatch {
@@ -59,20 +81,23 @@ stdenv.mkDerivation {
        revert = true;
        hash = "sha256-71sJuGqVjTcB549eIiCO0LoqAgxkdEHCoxh8Pd/Qzz8=";
      })
  ] ++ lib.optionals (lib.versionAtLeast llvmMajor "15" && lib.versionOlder llvmMajor "18") [
    ]
    ++ lib.optionals (lib.versionAtLeast llvmMajor "15" && lib.versionOlder llvmMajor "18") [
      # Fixes build after spirv-headers breaking change
      (fetchpatch {
        url = "https://github.com/KhronosGroup/SPIRV-LLVM-Translator/commit/0166a0fb86dc6c0e8903436bbc3a89bc3273ebc0.patch";
        excludes = [ "spirv-headers-tag.conf" ];
        hash = "sha256-17JJG8eCFVphElY5fVT/79hj0bByWxo8mVp1ZNjQk/M=";
      })
  ] ++ lib.optionals (llvmMajor == "16") [
    ]
    ++ lib.optionals (llvmMajor == "16") [
      # Fixes builds that link against external LLVM dynamic library
      (fetchpatch {
        url = "https://github.com/KhronosGroup/SPIRV-LLVM-Translator/commit/f3b9b604d7eda18d0d1029d94a6eebd33aa3a3fe.patch";
        hash = "sha256-opDjyZcy7O4wcSfm/A51NCIiDyIvbcmbv9ns1njdJbc=";
      })
  ] ++ lib.optionals (llvmMajor == "14") [
    ]
    ++ lib.optionals (llvmMajor == "14") [
      (fetchpatch {
        # tries to install llvm-spirv into llvm nix store path
        url = "https://github.com/KhronosGroup/SPIRV-LLVM-Translator/commit/cce9a2f130070d799000cac42fe24789d2b777ab.patch";
@@ -81,31 +106,43 @@ stdenv.mkDerivation {
      })
    ];

  nativeBuildInputs = [ pkg-config cmake ]
    ++ (if isROCm then [ llvm ] else [ llvm.dev ]);
  nativeBuildInputs = [
    pkg-config
    cmake
  ] ++ (if isROCm then [ llvm ] else [ llvm.dev ]);

  buildInputs = [ spirv-headers spirv-tools ]
    ++ lib.optionals (!isROCm) [ llvm ];
  buildInputs = [
    spirv-headers
    spirv-tools
  ] ++ lib.optionals (!isROCm) [ llvm ];

  nativeCheckInputs = [ lit ];

  cmakeFlags = [
  cmakeFlags =
    [
      "-DLLVM_INCLUDE_TESTS=ON"
      "-DLLVM_DIR=${(if isROCm then llvm else llvm.dev)}"
      "-DBUILD_SHARED_LIBS=YES"
      "-DLLVM_SPIRV_BUILD_EXTERNAL=YES"
      # RPATH of binary /nix/store/.../bin/llvm-spirv contains a forbidden reference to /build/
      "-DCMAKE_SKIP_BUILD_RPATH=ON"
  ] ++ lib.optional (llvmMajor != "11") "-DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=${spirv-headers.src}";
    ]
    ++ lib.optional (llvmMajor != "11") "-DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=${spirv-headers.src}"
    ++ lib.optional (llvmMajor == "19") "-DBASE_LLVM_VERSION=${lib.versions.majorMinor llvm.version}.0";

  # FIXME: CMake tries to run "/llvm-lit" which of course doesn't exist
  doCheck = false;

  makeFlags = [ "all" "llvm-spirv" ];
  makeFlags = [
    "all"
    "llvm-spirv"
  ];

  postInstall = ''
  postInstall =
    ''
      install -D tools/llvm-spirv/llvm-spirv $out/bin/llvm-spirv
  '' + lib.optionalString stdenv.hostPlatform.isDarwin ''
    ''
    + lib.optionalString stdenv.hostPlatform.isDarwin ''
      install_name_tool $out/bin/llvm-spirv \
        -change @rpath/libLLVMSPIRVLib.dylib $out/lib/libLLVMSPIRVLib.dylib
    '';
+2 −0
Original line number Diff line number Diff line
@@ -308,6 +308,7 @@ mapAliases {
  clang16Stdenv = lowPrio llvmPackages_16.stdenv;
  clang17Stdenv = lowPrio llvmPackages_17.stdenv;
  clang18Stdenv = lowPrio llvmPackages_18.stdenv;
  clang19Stdenv = lowPrio llvmPackages_19.stdenv;

  clang-tools_6 = throw "clang-tools_6 has been removed from nixpkgs"; # Added 2024-01-08
  clang-tools_7 = throw "clang-tools_7 has been removed from nixpkgs"; # Added 2023-11-19
@@ -329,6 +330,7 @@ mapAliases {
  clang-tools_16 = llvmPackages_16.clang-tools; # Added 2024-04-22
  clang-tools_17 = llvmPackages_17.clang-tools; # Added 2024-04-22
  clang-tools_18 = llvmPackages_18.clang-tools; # Added 2024-04-22
  clang-tools_19 = llvmPackages_19.clang-tools; # Added 2024-08-21

  cq-editor = throw "cq-editor has been removed, as it use a dependency that was disabled since python 3.8 and was last updated in 2021"; # Added 2024-05-13