1{
2 lib,
3 stdenv,
4 callPackage,
5 fetchFromGitHub,
6 fetchpatch,
7 rocmUpdateScript,
8 makeWrapper,
9 cmake,
10 perl,
11 hip-common,
12 hipcc,
13 rocm-device-libs,
14 rocm-comgr,
15 rocm-runtime,
16 rocm-core,
17 roctracer,
18 rocminfo,
19 rocm-smi,
20 numactl,
21 libffi,
22 zstd,
23 zlib,
24 libGL,
25 libxml2,
26 libX11,
27 python3Packages,
28 rocm-merged-llvm,
29 khronos-ocl-icd-loader,
30 gcc-unwrapped,
31 writeShellScriptBin,
32 localGpuTargets ? null,
33}:
34
35let
36 inherit (rocm-core) ROCM_LIBPATCH_VERSION;
37 hipClang = rocm-merged-llvm;
38 hipClangPath = "${hipClang}/bin";
39 wrapperArgs = [
40 "--prefix PATH : $out/bin"
41 "--prefix LD_LIBRARY_PATH : ${rocm-runtime}"
42 "--set HIP_PLATFORM amd"
43 "--set HIP_PATH $out"
44 "--set HIP_CLANG_PATH ${hipClangPath}"
45 "--set DEVICE_LIB_PATH ${rocm-device-libs}/amdgcn/bitcode"
46 "--set HSA_PATH ${rocm-runtime}"
47 "--set ROCM_PATH $out"
48 ];
49 amdclang = writeShellScriptBin "amdclang" ''
50 exec clang "$@"
51 '';
52 amdclangxx = writeShellScriptBin "amdclang++" ''
53 exec clang++ "$@"
54 '';
55in
56stdenv.mkDerivation (finalAttrs: {
57 pname = "clr";
58 version = "6.3.3";
59
60 outputs = [
61 "out"
62 "icd"
63 ];
64
65 src = fetchFromGitHub {
66 owner = "ROCm";
67 repo = "clr";
68 rev = "rocm-${finalAttrs.version}";
69 hash = "sha256-4qjfnn0kto2sNaSumXxHRHFrf3a3RZILOdhVSxkEs1I=";
70 };
71
72 nativeBuildInputs = [
73 makeWrapper
74 cmake
75 perl
76 python3Packages.python
77 python3Packages.cppheaderparser
78 amdclang
79 amdclangxx
80 ];
81
82 buildInputs = [
83 numactl
84 libGL
85 libxml2
86 libX11
87 khronos-ocl-icd-loader
88 hipClang
89 libffi
90 zstd
91 zlib
92 ];
93
94 propagatedBuildInputs = [
95 rocm-core
96 rocm-device-libs
97 rocm-comgr
98 rocm-runtime
99 rocminfo
100 ];
101
102 cmakeFlags = [
103 "-DCMAKE_BUILD_TYPE=Release"
104 "-DCMAKE_POLICY_DEFAULT_CMP0072=NEW" # Prefer newer OpenGL libraries
105 "-DCLR_BUILD_HIP=ON"
106 "-DCLR_BUILD_OCL=ON"
107 "-DHIP_COMMON_DIR=${hip-common}"
108 "-DHIPCC_BIN_DIR=${hipcc}/bin"
109 "-DHIP_PLATFORM=amd"
110 "-DPROF_API_HEADER_PATH=${roctracer.src}/inc/ext"
111 "-DROCM_PATH=${rocminfo}"
112 "-DBUILD_ICD=ON"
113 "-DHIP_ENABLE_ROCPROFILER_REGISTER=OFF" # circular dep - may need -minimal and -full builds?
114 "-DAMD_ICD_LIBRARY_DIR=${khronos-ocl-icd-loader}"
115
116 # Temporarily set variables to work around upstream CMakeLists issue
117 # Can be removed once https://github.com/ROCm/rocm-cmake/issues/121 is fixed
118 "-DCMAKE_INSTALL_BINDIR=bin"
119 "-DCMAKE_INSTALL_INCLUDEDIR=include"
120 "-DCMAKE_INSTALL_LIBDIR=lib"
121 ];
122
123 env.LLVM_DIR = "";
124
125 patches = [
126 ./cmake-find-x11-libgl.patch
127
128 (fetchpatch {
129 # Fix handling of old fatbin version https://github.com/ROCm/clr/issues/99
130 sha256 = "sha256-CK/QwgWJQEruiG4DqetF9YM0VEWpSiUMxAf1gGdJkuA=";
131 url = "https://src.fedoraproject.org/rpms/rocclr/raw/rawhide/f/0001-handle-v1-of-compressed-fatbins.patch";
132 })
133 (fetchpatch {
134 # improve rocclr isa compatibility check
135 sha256 = "sha256-wUrhpYN68AbEXeFU5f366C6peqHyq25kujJXY/bBJMs=";
136 url = "https://github.com/GZGavinZhao/clr/commit/22c17a0ac09c6b77866febf366591f669a1ed133.patch";
137 })
138 (fetchpatch {
139 # [PATCH] Improve hipamd compat check
140 sha256 = "sha256-uZQ8rMrWH61CCbxwLqQGggDmXFmYTi6x8OcgYPrZRC8=";
141 url = "https://github.com/GZGavinZhao/clr/commit/63c6ee630966744d4199fdfb854e98d2da9e1122.patch";
142 })
143 (fetchpatch {
144 # [PATCH] SWDEV-504340 - Move cast of cl_mem inside the condition
145 # Fixes crash due to UB in KernelBlitManager::setArgument
146 sha256 = "sha256-nL4CZ7EOXqsTVUtYhuu9DLOMpnMeMRUhkhylEQLTg9I=";
147 url = "https://github.com/ROCm/clr/commit/fa63919a6339ea2a61111981ba2362c97fbdf743.patch";
148 })
149 (fetchpatch {
150 # [PATCH] SWDEV-507104 - Removes alignment requirement for Semaphore class to resolve runtime misaligned memory issues
151 sha256 = "sha256-nStJ22B/CM0fzQTvYjbHDbQt0GlE8DXxVK+UDU9BAx4=";
152 url = "https://github.com/ROCm/clr/commit/21d764518363d74187deaef2e66c1a127bc5aa64.patch";
153 })
154 ];
155
156 postPatch = ''
157 patchShebangs hipamd/*.sh
158 patchShebangs hipamd/src
159
160 # We're not on Windows so these are never installed to hipcc...
161 substituteInPlace hipamd/CMakeLists.txt \
162 --replace-fail "install(PROGRAMS \''${HIPCC_BIN_DIR}/hipcc.bat DESTINATION bin)" "" \
163 --replace-fail "install(PROGRAMS \''${HIPCC_BIN_DIR}/hipconfig.bat DESTINATION bin)" ""
164
165 substituteInPlace hipamd/src/hip_embed_pch.sh \
166 --replace-fail "\''$LLVM_DIR/bin/clang" "${hipClangPath}/clang"
167
168 substituteInPlace opencl/khronos/icd/loader/icd_platform.h \
169 --replace-fail '#define ICD_VENDOR_PATH "/etc/OpenCL/vendors/";' \
170 '#define ICD_VENDOR_PATH "/run/opengl-driver/etc/OpenCL/vendors/";'
171
172 # new unbundler has better error messages, defaulting it on
173 substituteInPlace rocclr/utils/flags.hpp \
174 --replace-fail "HIP_ALWAYS_USE_NEW_COMGR_UNBUNDLING_ACTION, false" "HIP_ALWAYS_USE_NEW_COMGR_UNBUNDLING_ACTION, true"
175 '';
176
177 postInstall = ''
178 chmod +x $out/bin/*
179 patchShebangs $out/bin
180
181 cp ${amdclang}/bin/* $out/bin/
182 cp ${amdclangxx}/bin/* $out/bin/
183
184 for prog in hip{cc,config}{,.pl}; do
185 wrapProgram $out/bin/$prog ${lib.concatStringsSep " " wrapperArgs}
186 done
187
188 mkdir -p $out/nix-support/
189 echo '
190 export HIP_PATH="${placeholder "out"}"
191 export HIP_PLATFORM=amd
192 export HIP_DEVICE_LIB_PATH="${rocm-device-libs}/amdgcn/bitcode"
193 export NIX_CC_USE_RESPONSE_FILE=0
194 export HIP_CLANG_PATH="${hipClangPath}"
195 export ROCM_LIBPATCH_VERSION="${ROCM_LIBPATCH_VERSION}"
196 export HSA_PATH="${rocm-runtime}"' > $out/nix-support/setup-hook
197
198 # Just link rocminfo, it's easier
199 ln -s ${rocminfo}/bin/* $out/bin
200 ln -s ${rocm-core}/include/* $out/include/
201
202 # Replace rocm-opencl-icd functionality
203 mkdir -p $icd/etc/OpenCL/vendors
204 echo "$out/lib/libamdocl64.so" > $icd/etc/OpenCL/vendors/amdocl64.icd
205
206 # add version info to output (downstream rocmPackages look for this)
207 ln -s ${rocm-core}/.info/ $out/.info
208
209 ln -s ${hipClang} $out/llvm
210 '';
211
212 disallowedRequisites = [
213 gcc-unwrapped
214 ];
215
216 passthru = {
217 # All known and valid general GPU targets
218 # We cannot use this for each ROCm library, as each defines their own supported targets
219 # See: https://github.com/ROCm/ROCm/blob/77cbac4abab13046ee93d8b5bf410684caf91145/README.md#library-target-matrix
220 # Generic targets are not yet available in rocm-6.3.1 llvm
221 gpuTargets = lib.forEach [
222 # "9-generic"
223 "900" # MI25, Vega 56/64
224 "906" # MI50/60, Radeon VII
225 "908" # MI100
226 "90a" # MI210 / MI250
227 # "9-4-generic"
228 # 940/1 - never released publicly, maybe HPE cray specific MI3xx?
229 "942" # MI300
230 # "10-1-generic"
231 "1010"
232 "1012"
233 # "10-3-generic"
234 "1030" # W6800, various Radeon cards
235 # "11-generic"
236 "1100"
237 "1101"
238 "1102"
239 "1151" # Strix Halo
240 "1200" # RX 9070
241 "1201" # RX 9070 XT
242 ] (target: "gfx${target}");
243
244 inherit hipClangPath;
245
246 updateScript = rocmUpdateScript {
247 name = finalAttrs.pname;
248 inherit (finalAttrs.src) owner;
249 inherit (finalAttrs.src) repo;
250 page = "tags?per_page=4";
251 };
252
253 impureTests = {
254 rocm-smi = callPackage ./test-rocm-smi.nix {
255 inherit rocm-smi;
256 clr = finalAttrs.finalPackage;
257 };
258 opencl-example = callPackage ./test-opencl-example.nix {
259 clr = finalAttrs.finalPackage;
260 };
261 };
262
263 selectGpuTargets =
264 {
265 supported ? [ ],
266 }:
267 supported;
268 gpuArchSuffix = "";
269 }
270 // lib.optionalAttrs (localGpuTargets != null) {
271 inherit localGpuTargets;
272 gpuArchSuffix = "-" + (builtins.concatStringsSep "-" localGpuTargets);
273 selectGpuTargets =
274 {
275 supported ? [ ],
276 }:
277 if supported == [ ] then localGpuTargets else lib.lists.intersectLists localGpuTargets supported;
278 };
279
280 meta = with lib; {
281 description = "AMD Common Language Runtime for hipamd, opencl, and rocclr";
282 homepage = "https://github.com/ROCm/clr";
283 license = with licenses; [ mit ];
284 maintainers = with maintainers; [ lovesegfault ];
285 teams = [ teams.rocm ];
286 platforms = platforms.linux;
287 };
288})