Commit eada89c31 for llama.cpp

commit eada89c3153ff7da97828187da98cf4c1f6dc7db
Author: Titaniumtown <titaniumtown@proton.me>
Date:   Tue Nov 17 01:04:12 2026 -0800

    nix: expose a SYCL package (#31066)

diff --git a/.devops/nix/package.nix b/.devops/nix/package.nix
index 71b2c2f08..021063276 100644
--- a/.devops/nix/package.nix
+++ b/.devops/nix/package.nix
@@ -22,12 +22,19 @@
   spirv-headers,
   nodejs,
   importNpmLock,
+  syclToolkit ? null,
+  level-zero,
+  ocl-icd,
+  intel-graphics-compiler,
+  intel-compute-runtime,
+  makeBinaryWrapper,
   useBlas ?
     builtins.all (x: !x) [
       useCuda
       useMetalKit
       useRocm
       useVulkan
+      useSycl
     ]
     && blas.meta.available,
   useCuda ? config.cudaSupport,
@@ -38,11 +45,28 @@
   rocmGpuTargets ? builtins.concatStringsSep ";" rocmPackages.clr.gpuTargets,
   useVulkan ? false,
   useRpc ? false,
+  useSycl ? false,
+  # Rebuild, not a runtime switch. cmake default is off.
+  syclF16 ? false,
+  # null keeps JIT. AOT makes icpx run ocloc, which DPC++ does not ship.
+  syclDeviceArch ? null,
+  syclMaxParallelLinkJobs ? null,
   llamaVersion ? "0.0.0", # Arbitrary version, substituted by the flake

-  # It's necessary to consistently use backendStdenv when building with CUDA support,
-  # otherwise we get libstdc++ errors downstream.
-  effectiveStdenv ? if useCuda then cudaPackages.backendStdenv else stdenv,
+  # CUDA needs its backend stdenv or libstdc++ mismatches downstream.
+  # SYCL needs icpx which is provided by the oneAPI stdenv
+  effectiveStdenv ?
+    if useCuda && useSycl then
+      throw "llama-cpp: useCuda and useSycl are mutually exclusive"
+    else if useCuda then
+      cudaPackages.backendStdenv
+    else if useSycl then
+      if syclToolkit == null then
+        throw "llama-cpp: useSycl requires syclToolkit, consider setting it to intel-oneapi-toolkit"
+      else
+        syclToolkit.stdenv
+    else
+      stdenv,
   enableStatic ? effectiveStdenv.hostPlatform.isStatic,
   precompileMetalShaders ? false,
   useWebUi ? true,
@@ -65,7 +89,8 @@ let
     ++ lib.optionals useMetalKit [ "MetalKit" ]
     ++ lib.optionals useMpi [ "MPI" ]
     ++ lib.optionals useRocm [ "ROCm" ]
-    ++ lib.optionals useVulkan [ "Vulkan" ];
+    ++ lib.optionals useVulkan [ "Vulkan" ]
+    ++ lib.optionals useSycl [ "SYCL" ];

   pnameSuffix =
     strings.optionalString (suffices != [ ])
@@ -108,6 +133,26 @@ let
     shaderc
     spirv-headers
   ];
+
+  syclAot = useSycl && syclDeviceArch != null && syclDeviceArch != "";
+
+  # source ocloc binary from intel-compute-runtime
+  oclocAot = runCommand "ocloc-aot" { nativeBuildInputs = [ makeBinaryWrapper ]; } ''
+    mkdir -p "$out/bin"
+    shopt -s nullglob
+    exact=(${lib.getBin intel-compute-runtime}/bin/ocloc)
+    vers=(${lib.getBin intel-compute-runtime}/bin/ocloc-*)
+    if [ ''${#exact[@]} -eq 1 ]; then
+      src=''${exact[0]}
+    elif [ ''${#vers[@]} -eq 1 ]; then
+      src=''${vers[0]}
+    else
+      echo "expected one ocloc binary from intel-compute-runtime" >&2
+      exit 1
+    fi
+    makeWrapper "$src" "$out/bin/ocloc" \
+      --prefix LD_LIBRARY_PATH : ${lib.makeLibraryPath [ intel-graphics-compiler ]}
+  '';
 in

 effectiveStdenv.mkDerivation (finalAttrs: {
@@ -168,6 +213,13 @@ effectiveStdenv.mkDerivation (finalAttrs: {
   # see https://github.com/ggml-org/llama.cpp/pull/6118 for discussion
   __noChroot = effectiveStdenv.hostPlatform.isDarwin && useMetalKit && precompileMetalShaders;

+  # Do not fortify under SYCL as it breaks compiled memcpy invocations
+  # _FORTIFY_SOURCE rewrites memcpy -> __memcpy_chk
+  hardeningDisable = optionals useSycl [
+    "fortify"
+    "fortify3"
+  ];
+
   nativeBuildInputs =
     [
       cmake
@@ -181,7 +233,12 @@ effectiveStdenv.mkDerivation (finalAttrs: {
       autoAddDriverRunpath
     ]
     ++ optionals (effectiveStdenv.hostPlatform.isGnu && enableStatic) [ glibc.static ]
-    ++ optionals (effectiveStdenv.hostPlatform.isDarwin && useMetalKit && precompileMetalShaders) [ xcrunHost ];
+    ++ optionals (effectiveStdenv.hostPlatform.isDarwin && useMetalKit && precompileMetalShaders) [ xcrunHost ]
+    ++ optionals useSycl [
+      autoAddDriverRunpath
+      makeBinaryWrapper
+    ]
+    ++ optionals syclAot [ oclocAot ];

   buildInputs =
     optionals effectiveStdenv.hostPlatform.isDarwin darwinBuildInputs
@@ -190,39 +247,53 @@ effectiveStdenv.mkDerivation (finalAttrs: {
     ++ optionals useRocm rocmBuildInputs
     ++ optionals useBlas [ blas ]
     ++ optionals useVulkan vulkanBuildInputs
+    ++ optionals useSycl [
+      level-zero
+      ocl-icd
+      intel-graphics-compiler
+    ]
     ++ [ openssl ];

-  cmakeFlags =
-    [
-      (cmakeBool "LLAMA_BUILD_SERVER" true)
-      (cmakeBool "LLAMA_BUILD_WEBUI" useWebUi)
-      (cmakeBool "BUILD_SHARED_LIBS" (!enableStatic))
-      (cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
-      (cmakeBool "GGML_NATIVE" false)
-      (cmakeBool "GGML_BLAS" useBlas)
-      (cmakeBool "GGML_CUDA" useCuda)
-      (cmakeBool "GGML_HIP" useRocm)
-      (cmakeBool "GGML_METAL" useMetalKit)
-      (cmakeBool "GGML_VULKAN" useVulkan)
-      (cmakeBool "GGML_STATIC" enableStatic)
-      (cmakeBool "GGML_RPC" useRpc)
-    ]
-    ++ optionals useCuda [
-      (
-        with cudaPackages.flags;
-        cmakeFeature "CMAKE_CUDA_ARCHITECTURES" (
-          builtins.concatStringsSep ";" (map dropDot cudaCapabilities)
-        )
+  cmakeFlags = [
+    (cmakeBool "LLAMA_BUILD_SERVER" true)
+    (cmakeBool "LLAMA_BUILD_WEBUI" useWebUi)
+    (cmakeBool "BUILD_SHARED_LIBS" (!enableStatic))
+    (cmakeBool "CMAKE_SKIP_BUILD_RPATH" true)
+    (cmakeBool "GGML_NATIVE" false)
+    (cmakeBool "GGML_BLAS" useBlas)
+    (cmakeBool "GGML_CUDA" useCuda)
+    (cmakeBool "GGML_HIP" useRocm)
+    (cmakeBool "GGML_METAL" useMetalKit)
+    (cmakeBool "GGML_VULKAN" useVulkan)
+    (cmakeBool "GGML_STATIC" enableStatic)
+    (cmakeBool "GGML_RPC" useRpc)
+  ]
+  ++ optionals useCuda [
+    (
+      with cudaPackages.flags;
+      cmakeFeature "CMAKE_CUDA_ARCHITECTURES" (
+        builtins.concatStringsSep ";" (map dropDot cudaCapabilities)
       )
-    ]
-    ++ optionals useRocm [
-      (cmakeFeature "CMAKE_HIP_COMPILER" "${rocmPackages.llvm.clang}/bin/clang")
-      (cmakeFeature "CMAKE_HIP_ARCHITECTURES" rocmGpuTargets)
-    ]
-    ++ optionals useMetalKit [
-      (lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1")
-      (cmakeBool "GGML_METAL_EMBED_LIBRARY" (!precompileMetalShaders))
-    ];
+    )
+  ]
+  ++ optionals useRocm [
+    (cmakeFeature "CMAKE_HIP_COMPILER" "${rocmPackages.llvm.clang}/bin/clang")
+    (cmakeFeature "CMAKE_HIP_ARCHITECTURES" rocmGpuTargets)
+  ]
+  ++ optionals useMetalKit [
+    (lib.cmakeFeature "CMAKE_C_FLAGS" "-D__ARM_FEATURE_DOTPROD=1")
+    (cmakeBool "GGML_METAL_EMBED_LIBRARY" (!precompileMetalShaders))
+  ]
+  ++ optionals useSycl [
+    (cmakeBool "GGML_SYCL" true)
+    (cmakeBool "GGML_SYCL_F16" syclF16)
+  ]
+  ++ optionals syclAot [
+    (cmakeFeature "GGML_SYCL_DEVICE_ARCH" syclDeviceArch)
+  ]
+  ++ optionals (syclAot && syclMaxParallelLinkJobs != null) [
+    (cmakeFeature "GGML_SYCL_MAX_PARALLEL_LINK_JOBS" (toString syclMaxParallelLinkJobs))
+  ];

   # Environment variables needed for ROCm
   env = optionalAttrs useRocm {
@@ -237,11 +308,35 @@ effectiveStdenv.mkDerivation (finalAttrs: {
     cp $src/include/llama.h $out/include/
   '';

+  # Fix SYCL binary calling missing important libraries
+  # Also use Level Zero + sysman INSTEAD of OpenCL
+  # due to OpenCL reporting free memory as total memory,
+  # breaking `-fit` functionality
+  postFixup = lib.optionalString useSycl ''
+    driverLibs=${
+      lib.makeLibraryPath [
+        intel-graphics-compiler
+        ocl-icd
+      ]
+    }
+    for b in "$out"/bin/*; do
+      [ -f "$b" ] && [ -x "$b" ] || continue
+      patchelf --print-interpreter "$b" >/dev/null 2>&1 || continue
+      patchelf --add-needed libOpenCL.so.1 --add-needed libigc.so.2 \
+        --add-rpath "$driverLibs" "$b"
+      wrapProgram "$b" \
+        --set-default ONEAPI_DEVICE_SELECTOR level_zero:gpu \
+        --set-default ZES_ENABLE_SYSMAN 1
+    done
+  '';
+
   meta = {
     # Configurations we don't want even the CI to evaluate. Results in the
     # "unsupported platform" messages. This is mostly a no-op, because
     # cudaPackages would've refused to evaluate anyway.
-    badPlatforms = optionals useCuda lib.platforms.darwin;
+    badPlatforms =
+      optionals useCuda lib.platforms.darwin
+      ++ optionals useSycl (lib.subtractLists [ "x86_64-linux" ] lib.platforms.all);

     # Configurations that are known to result in build failures. Can be
     # overridden by importing Nixpkgs with `allowBroken = true`.
diff --git a/flake.nix b/flake.nix
index 6373d3b0b..8b9fefec7 100644
--- a/flake.nix
+++ b/flake.nix
@@ -164,6 +164,11 @@
               }
               // lib.optionalAttrs (system == "x86_64-linux") {
                 rocm = config.legacyPackages.llamaPackagesRocm.llama-cpp;
+
+                sycl = config.packages.default.override {
+                  useSycl = true;
+                  syclToolkit = pkgs.intel-oneapi-toolkit;
+                };
               };

             # Packages exposed in `.#checks` will be built by the CI and by