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