nixpkgs mirror (for testing)
github.com/NixOS/nixpkgs
nix
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-toolchain,
17 rocm-core,
18 roctracer,
19 rocminfo,
20 rocm-smi,
21 symlinkJoin,
22 numactl,
23 libffi,
24 zstd,
25 zlib,
26 libGL,
27 libxml2,
28 libX11,
29 python3Packages,
30 llvm,
31 khronos-ocl-icd-loader,
32 gcc-unwrapped,
33 writeShellScriptBin,
34 localGpuTargets ? null,
35}:
36
37let
38 inherit (rocm-core) ROCM_LIBPATCH_VERSION;
39 # HIP_CLANG_PATH or ROCM_PATH/llvm
40 # Note: relying on ROCM_PATH/llvm is bad for cross
41 hipClang = symlinkJoin {
42 name = "hipClang";
43 paths = [
44 # FIXME: if we don't put this first aotriton build fails with ld.lld: -flavor gnu
45 # Probably wrapper jank
46 llvm.bintools.bintools
47 llvm.rocm-toolchain
48 ];
49 postBuild = ''
50 rm -rf $out/{include,lib,share,etc,nix-support,usr}
51 '';
52 };
53 hipClangPath = "${hipClang}/bin";
54 wrapperArgs = [
55 "--prefix PATH : $out/bin"
56 "--prefix LD_LIBRARY_PATH : ${rocm-runtime}"
57 "--set HIP_PLATFORM amd"
58 "--set HIP_PATH $out"
59 "--set HIP_CLANG_PATH ${hipClangPath}"
60 "--set DEVICE_LIB_PATH ${rocm-device-libs}/amdgcn/bitcode"
61 "--set HSA_PATH ${rocm-runtime}"
62 "--set ROCM_PATH $out"
63 ];
64 amdclang = writeShellScriptBin "amdclang" ''
65 exec ${hipClang}/bin/clang "$@"
66 '';
67 amdclangxx = writeShellScriptBin "amdclang++" ''
68 exec ${hipClang}/bin/clang++ "$@"
69 '';
70in
71stdenv.mkDerivation (finalAttrs: {
72 pname = "clr";
73 version = "7.1.1";
74
75 outputs = [
76 "out"
77 "icd"
78 ];
79
80 __structuredAttrs = true;
81 strictDeps = true;
82
83 src = fetchFromGitHub {
84 owner = "ROCm";
85 repo = "clr";
86 rev = "rocm-${finalAttrs.version}";
87 hash = "sha256-ofsq1uqMixtum5C6cp/UgTDpgGPfj+rAd6PoDx5iLLw=";
88 };
89
90 nativeBuildInputs = [
91 makeWrapper
92 cmake
93 perl
94 python3Packages.python
95 python3Packages.cppheaderparser
96 amdclang
97 amdclangxx
98 ];
99
100 buildInputs = [
101 llvm.llvm
102 numactl
103 libGL
104 libxml2
105 libX11
106 khronos-ocl-icd-loader
107 hipClang
108 libffi
109 zstd
110 zlib
111 ];
112
113 propagatedBuildInputs = [
114 rocm-core
115 rocm-device-libs
116 rocm-comgr
117 rocm-runtime
118 rocminfo
119 hipClangPath
120 ];
121
122 cmakeBuildType = "RelWithDebInfo";
123 separateDebugInfo = true;
124
125 cmakeFlags = [
126 "-DCMAKE_POLICY_DEFAULT_CMP0072=NEW" # Prefer newer OpenGL libraries
127 "-DCLR_BUILD_HIP=ON"
128 "-DCLR_BUILD_OCL=ON"
129 "-DHIP_COMMON_DIR=${hip-common}"
130 "-DHIPCC_BIN_DIR=${hipcc}/bin"
131 "-DHIP_PLATFORM=amd"
132 "-DPROF_API_HEADER_PATH=${roctracer.src}/inc/ext"
133 "-DROCM_PATH=${rocminfo}"
134 "-DBUILD_ICD=ON"
135 "-DHIP_ENABLE_ROCPROFILER_REGISTER=OFF" # circular dep - may need -minimal and -full builds?
136 "-DAMD_ICD_LIBRARY_DIR=${khronos-ocl-icd-loader}"
137
138 # Temporarily set variables to work around upstream CMakeLists issue
139 # Can be removed once https://github.com/ROCm/rocm-cmake/issues/121 is fixed
140 "-DCMAKE_INSTALL_BINDIR=bin"
141 "-DCMAKE_INSTALL_INCLUDEDIR=include"
142 "-DCMAKE_INSTALL_LIBDIR=lib"
143 ];
144
145 env.LLVM_DIR = "";
146
147 patches = [
148 ./cmake-find-x11-libgl.patch
149 (fetchpatch {
150 # [PATCH] rocclr: Extend HIP ISA compatibility checks
151 sha256 = "sha256-InUSIFI1MgkfocBEoZjO2BCgXNyfF10ehh9jkTtAPXs=";
152 url = "https://github.com/GZGavinZhao/rocm-systems/commit/937dcfdd316b589509c061809186fe5451d22431.patch";
153 relative = "projects/clr";
154 })
155 ];
156
157 postPatch = ''
158 patchShebangs hipamd/*.sh
159 patchShebangs hipamd/src
160
161 # We're not on Windows so these are never installed to hipcc...
162 substituteInPlace hipamd/CMakeLists.txt \
163 --replace-fail "install(PROGRAMS \''${HIPCC_BIN_DIR}/hipcc.bat DESTINATION bin)" "" \
164 --replace-fail "install(PROGRAMS \''${HIPCC_BIN_DIR}/hipconfig.bat DESTINATION bin)" ""
165
166 substituteInPlace hipamd/src/hip_embed_pch.sh \
167 --replace-fail "\''$LLVM_DIR/bin/clang" "${hipClangPath}/clang" \
168 --replace-fail "\''$LLVM_DIR/bin/llvm-mc" "${lib.getExe' llvm.bintools.bintools "llvm-mc"}"
169
170 substituteInPlace opencl/khronos/icd/loader/icd_platform.h \
171 --replace-fail '#define ICD_VENDOR_PATH "/etc/OpenCL/vendors/";' \
172 '#define ICD_VENDOR_PATH "/run/opengl-driver/etc/OpenCL/vendors/";'
173 '';
174
175 postInstall = ''
176 chmod +x $out/bin/*
177 patchShebangs $out/bin
178
179 cp ${amdclang}/bin/* $out/bin/
180 cp ${amdclangxx}/bin/* $out/bin/
181
182 for prog in hip{cc,config}; do
183 wrapProgram $out/bin/$prog ${lib.concatStringsSep " " wrapperArgs}
184 done
185
186 mkdir -p $out/nix-support/
187 echo '
188 export HIP_PATH="${placeholder "out"}"
189 export HIP_PLATFORM=amd
190 export HIP_DEVICE_LIB_PATH="${rocm-device-libs}/amdgcn/bitcode"
191 export NIX_CC_USE_RESPONSE_FILE=0
192 export HIP_CLANG_PATH="${hipClangPath}"
193 export ROCM_LIBPATCH_VERSION="${ROCM_LIBPATCH_VERSION}"
194 export HSA_PATH="${rocm-runtime}"' > $out/nix-support/setup-hook
195
196 # Just link rocminfo, it's easier
197 ln -s ${rocminfo}/bin/* $out/bin
198 ln -s ${rocm-core}/include/* $out/include/
199
200 # Replace rocm-opencl-icd functionality
201 mkdir -p $icd/etc/OpenCL/vendors
202 echo "$out/lib/libamdocl64.so" > $icd/etc/OpenCL/vendors/amdocl64.icd
203
204 # add version info to output (downstream rocmPackages look for this)
205 ln -s ${rocm-core}/.info/ $out/.info
206
207 ln -s ${hipClang} $out/llvm
208 '';
209
210 disallowedRequisites = [
211 gcc-unwrapped
212 ];
213
214 passthru = {
215 # All known and valid general GPU targets
216 # We cannot use this for each ROCm library, as each defines their own supported targets
217 # See: https://github.com/ROCm/ROCm/blob/77cbac4abab13046ee93d8b5bf410684caf91145/README.md#library-target-matrix
218 gpuTargets = lib.forEach [
219 # "9-generic" # can handle all Vega variants
220 "900" # MI25, Vega 56/64
221 # "902" # Vega 8
222 # "909" # Renoir Vega APU
223 # "90c" # Renoir Vega APU
224 # Past this point cards need their own kernels for perf despite gfx9-generic compat
225 "906" # MI50/60, Radeon VII - adds dot product & mixed precision FMA ops
226 "908" # MI100 - adds MFMA (matrix fused multiply-add) ops
227 "90a" # MI210/MI250 - additional MFMA variants
228 # "9-4-generic" - since only 942 is valid for 6.4 target it directly
229 # 940/1 - never released publicly, maybe HPE cray specific MI3xx?
230 "942" # MI300A/X, MI325X
231 # "950" # MI350X TODO: Expected in ROCm 7.x
232 # "10-1-generic" # fine for all RDNA1 cards
233 "1010"
234 # "10-3-generic"
235 "1030" # W6800, various Radeon cards
236 # "11-generic" # will handle 7600, hopefully ryzen AI series iGPUs
237 "1100"
238 "1101"
239 "1102"
240 "1150" # Strix Point
241 "1151" # Strix Halo
242 # "12-generic"
243 "1200" # RX 9060
244 "1201" # RX 9070 + XT
245 ] (target: "gfx${target}");
246
247 inherit hipClangPath;
248
249 updateScript = rocmUpdateScript {
250 name = finalAttrs.pname;
251 inherit (finalAttrs.src) owner;
252 inherit (finalAttrs.src) repo;
253 page = "tags?per_page=4";
254 };
255
256 impureTests = {
257 rocm-smi = callPackage ./test-rocm-smi.nix {
258 inherit rocm-smi;
259 clr = finalAttrs.finalPackage;
260 };
261 opencl-example = callPackage ./test-opencl-example.nix {
262 clr = finalAttrs.finalPackage;
263 };
264 generic-arch = callPackage ./test-isa-compat.nix {
265 clr = finalAttrs.finalPackage;
266 name = "generic-arch";
267 offloadArches = [
268 "gfx9-generic"
269 "gfx10-1-generic"
270 "gfx10-3-generic"
271 "gfx11-generic"
272 "gfx12-generic"
273 ];
274 };
275 isa-compat = callPackage ./test-isa-compat.nix {
276 clr = finalAttrs.finalPackage;
277 name = "isa-compat";
278 offloadArches = [
279 "gfx900"
280 "gfx1010"
281 "gfx1030"
282 ];
283 };
284 spirv = callPackage ./test-isa-compat.nix {
285 clr = finalAttrs.finalPackage;
286 name = "spirv";
287 offloadArches = [
288 "amdgcnspirv"
289 ];
290 };
291 };
292
293 selectGpuTargets =
294 {
295 supported ? [ ],
296 }:
297 supported;
298 gpuArchSuffix = "";
299 }
300 // lib.optionalAttrs (localGpuTargets != null) {
301 inherit localGpuTargets;
302 gpuArchSuffix = "-" + (builtins.concatStringsSep "-" localGpuTargets);
303 selectGpuTargets =
304 {
305 supported ? [ ],
306 }:
307 if supported == [ ] then localGpuTargets else lib.lists.intersectLists localGpuTargets supported;
308 };
309
310 meta = {
311 description = "AMD Common Language Runtime for hipamd, opencl, and rocclr";
312 homepage = "https://github.com/ROCm/clr";
313 license = with lib.licenses; [ mit ];
314 maintainers = with lib.maintainers; [ lovesegfault ];
315 teams = [ lib.teams.rocm ];
316 platforms = lib.platforms.linux;
317 };
318})