13 llvmMajor = lib.versions.major llvm.version;
14 isROCm = lib.hasPrefix "rocm" llvm.pname;
16 # ROCm, if actively updated will always be at the latest version
18 if llvmMajor == "17" || isROCm then rec {
21 hash = "sha256-Rzm5Py9IPFtS9G7kME+uSwZ/0gPGW6MlL35ZWk4LfHM=";
22 } else if llvmMajor == "16" then rec {
25 hash = "sha256-EUabcYqSjXshbPmcs1DRLvCSL1nd9rEdpqELBrItCW8=";
26 } else if llvmMajor == "15" then rec {
29 hash = "sha256-OsDohXRxovtEXaWiRGp8gJ0dXmoALyO+ZimeSO8aPVI=";
30 } else if llvmMajor == "14" then rec{
33 hash = "sha256-BhNAApgZ/w/92XjpoDY6ZEIhSTwgJ4D3/EfNvPmNM2o=";
34 } else if llvmMajor == "11" then {
35 version = "unstable-2022-05-04";
36 rev = "4ef524240833abfeee1c5b9fff6b1bd53f4806b3"; # 267 commits ahead of v11.0.0
37 hash = "sha256-NoIoa20+2sH41rEnr8lsMhtfesrtdPINiXtUnxYVm8s=";
38 } else throw "Incompatible LLVM version.";
41 pname = "SPIRV-LLVM-Translator";
42 inherit (branch) version;
44 src = fetchFromGitHub {
45 owner = "KhronosGroup";
46 repo = "SPIRV-LLVM-Translator";
47 inherit (branch) rev hash;
50 patches = lib.optionals (llvmMajor == "16")[
51 # Fixes builds that link against external LLVM dynamic library
53 url = "https://github.com/KhronosGroup/SPIRV-LLVM-Translator/commit/f3b9b604d7eda18d0d1029d94a6eebd33aa3a3fe.patch";
54 hash = "sha256-opDjyZcy7O4wcSfm/A51NCIiDyIvbcmbv9ns1njdJbc=";
58 nativeBuildInputs = [ pkg-config cmake ]
59 ++ (if isROCm then [ llvm ] else [ llvm.dev ]);
61 buildInputs = [ spirv-headers spirv-tools ]
62 ++ lib.optionals (!isROCm) [ llvm ];
64 nativeCheckInputs = [ lit ];
67 "-DLLVM_INCLUDE_TESTS=ON"
68 "-DLLVM_DIR=${(if isROCm then llvm else llvm.dev)}"
69 "-DBUILD_SHARED_LIBS=YES"
70 "-DLLVM_SPIRV_BUILD_EXTERNAL=YES"
71 # RPATH of binary /nix/store/.../bin/llvm-spirv contains a forbidden reference to /build/
72 "-DCMAKE_SKIP_BUILD_RPATH=ON"
73 ] ++ lib.optionals (llvmMajor != "11") [ "-DLLVM_EXTERNAL_SPIRV_HEADERS_SOURCE_DIR=${spirv-headers.src}" ];
75 # FIXME: CMake tries to run "/llvm-lit" which of course doesn't exist
78 makeFlags = [ "all" "llvm-spirv" ];
81 install -D tools/llvm-spirv/llvm-spirv $out/bin/llvm-spirv
82 '' + lib.optionalString stdenv.isDarwin ''
83 install_name_tool $out/bin/llvm-spirv \
84 -change @rpath/libLLVMSPIRVLib.dylib $out/lib/libLLVMSPIRVLib.dylib
88 homepage = "https://github.com/KhronosGroup/SPIRV-LLVM-Translator";
89 description = "A tool and a library for bi-directional translation between SPIR-V and LLVM IR";
90 license = licenses.ncsa;
91 platforms = platforms.unix;
92 maintainers = with maintainers; [ gloaming ];