Gentoo Archives: gentoo-dev

From: Yiyang Wu <xgreenlandforwyy@×××××.com>
To: gentoo-dev@l.g.o
Cc: Benda Xu <heroxbd@g.o>, Ulrich Mueller <ulm@g.o>
Subject: [gentoo-dev] [PATCH v3 1/2] rocm.eclass: new eclass
Date: Sun, 21 Aug 2022 07:23:18
Message-Id: f0a039d7d56e90bebeb78a4e8a7ed20ceb787663.1661065019.git.xgreenlandforwyy@gmail.com
In Reply to: [gentoo-dev] [PATCH v3 0/2] rocm.eclass: new eclass for ROCm packages by Yiyang Wu
1 This eclass provides utilities for ROCm libraries in
2 https://github.com/ROCmSoftwarePlatform, e.g. rocBLAS, rocFFT.
3 It contains a USE_EXPAND, amdgpu_targets_*, which handles the GPU
4 architecture to compile, and keep targets coherent among dependencies.
5 Packages that depend on ROCm libraries, like cupy, can also make use of
6 this eclass, mainly specify GPU architecture and it's corresponding
7 dependencies via USE_EXPAND.
8
9 Closes: https://bugs.gentoo.org/810619
10 Bugs: https://bugs.gentoo.org/817440
11 Signed-off-by: Yiyang Wu <xgreenlandforwyy@×××××.com>
12 ---
13 eclass/rocm.eclass | 278 ++++++++++++++++++++++++++++++++++++
14 profiles/base/make.defaults | 2 +-
15 2 files changed, 279 insertions(+), 1 deletion(-)
16 create mode 100644 eclass/rocm.eclass
17
18 diff --git a/eclass/rocm.eclass b/eclass/rocm.eclass
19 new file mode 100644
20 index 000000000000..4b220db0aa81
21 --- /dev/null
22 +++ b/eclass/rocm.eclass
23 @@ -0,0 +1,278 @@
24 +# Copyright 2022 Gentoo Authors
25 +# Distributed under the terms of the GNU General Public License v2
26 +
27 +# @ECLASS: rocm.eclass
28 +# @MAINTAINER:
29 +# Gentoo Science Project <sci@g.o>
30 +# @AUTHOR:
31 +# Yiyang Wu <xgreenlandforwyy@×××××.com>
32 +# @SUPPORTED_EAPIS: 7 8
33 +# @BLURB: Common functions and variables for ROCm packages written in HIP
34 +# @DESCRIPTION:
35 +# ROCm packages such as sci-libs/<roc|hip>* can utilize functions in this
36 +# eclass. Currently, it handles the AMDGPU_TARGETS variable via USE_EXPAND, so
37 +# user can use USE flag to control which GPU architecture to compile, and
38 +# ensure coherence among dependencies. It also specify CXX=hipcc, to let hipcc
39 +# compile. Another important function is src_test, which checks whether a valid
40 +# KFD device exists for testing, and then execute the test program.
41 +#
42 +# Most ROCm packages use cmake as build system, so this eclass does not export
43 +# phase functions which overwrites the phase functions in cmake.eclass. Ebuild
44 +# should explicitly call rocm_src_* in src_configure and src_test.
45 +#
46 +# @EXAMPLE:
47 +# # Example for ROCm packages in https://github.com/ROCmSoftwarePlatform
48 +# @CODE
49 +# inherit cmake rocm
50 +# SRC_URI="https://github.com/ROCmSoftwarePlatform/${PN}/archive/rocm-${PV}.tar.gz -> ${P}.tar.gz"
51 +# SLOT="0/$(ver_cut 1-2)"
52 +# IUSE="test"
53 +# REQUIRED_USE="${ROCM_REQUIRED_USE}"
54 +# RESTRICT="!test? ( test )"
55 +#
56 +# RDEPEND="
57 +# dev-util/hip
58 +# sci-libs/rocBLAS:${SLOT}[${ROCM_USEDEP}]
59 +# "
60 +#
61 +# S=${WORKDIR}/${PN}-rocm-${PV}
62 +#
63 +# src_configure() {
64 +# local mycmakeargs=(
65 +# -DBUILD_CLIENTS_TESTS=$(usex test ON OFF)
66 +# )
67 +# rocm_src_configure
68 +# }
69 +#
70 +# src_test() {
71 +# rocm_src_test
72 +# }
73 +# @CODE
74 +#
75 +# # Example for packages depend on ROCm libraries -- a package depend on
76 +# # rocBLAS, and use comma seperated ${HCC_AMDGPU_TARGET} to determine GPU
77 +# # architecture to compile. Requires ROCm version >5.
78 +# @CODE
79 +# ROCM_VERSION=5
80 +# inherit rocm
81 +# IUSE="rocm"
82 +# REQUIRED_USE="rocm? ( ${ROCM_REQUIRED_USE} )"
83 +# DEPEND="rocm? ( >=dev-util/hip-${ROCM_VERSION}
84 +# >=sci-libs/rocBLAS-${ROCM_VERSION}[${ROCM_USEDEP}] )"
85 +# ....
86 +# src_configure() {
87 +# if use rocm; then
88 +# local AMDGPU_FLAGS=$(get_amdgpu_flags)
89 +# export HCC_AMDGPU_TARGET=${AMDGPU_FLAGS//;/,}
90 +# fi
91 +# default
92 +# }
93 +# @CODE
94 +
95 +if [[ ! ${_ROCM_ECLASS} ]]; then
96 +
97 +case ${EAPI} in
98 + 7|8) ;;
99 + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
100 +esac
101 +
102 +inherit edo
103 +
104 +# @ECLASS_VARIABLE: ROCM_VERSION
105 +# @DEFAULT_UNSET
106 +# @PRE_INHERIT
107 +# @DESCRIPTION:
108 +# The ROCm version of current package. Default is ${PV}, but for other packages
109 +# that depend on ROCm libraries, this can be set to match the version of
110 +# required ROCm libraries.
111 +
112 +# @ECLASS_VARIABLE: ALL_AMDGPU_TARGETS
113 +# @INTERNAL
114 +# @DESCRIPTION:
115 +# The list of USE flags corresponding to all AMDGPU targets in this ROCm
116 +# version. The value depends on ${PV}. Architectures and devices map:
117 +# https://www.coelacanth-dream.com/posts/2019/12/30/did-rid-product-matome-p2
118 +
119 +# @ECLASS_VARIABLE: OFFICIAL_AMDGPU_TARGETS
120 +# @INTERNAL
121 +# @DESCRIPTION:
122 +# The list of USE flags corresponding to all officially supported AMDGPU
123 +# targets in this ROCm version, documented at
124 +# https://docs.amd.com/bundle/ROCm-Installation-Guide-v${PV}/page/Prerequisite_Actions.html.
125 +# USE flag of these architectures will be default on. Depends on ${PV}.
126 +
127 +# @ECLASS_VARIABLE: ROCM_REQUIRED_USE
128 +# @OUTPUT_VARIABLE
129 +# @DESCRIPTION:
130 +# Requires at least one AMDGPU target to be compiled.
131 +# Example use for ROCm libraries:
132 +# @CODE
133 +# REQUIRED_USE="${ROCM_REQUIRED_USE}"
134 +# @CODE
135 +# Example use for packages that depend on ROCm libraries
136 +# @CODE
137 +# IUSE="rocm"
138 +# REQUIRED_USE="rocm? ( ${ROCM_REQUIRED_USE} )"
139 +# @CODE
140 +
141 +# @ECLASS_VARIABLE: ROCM_USEDEP
142 +# @OUTPUT_VARIABLE
143 +# @DESCRIPTION:
144 +# This is an eclass-generated USE-dependency string which can be used to
145 +# depend on another ROCm package being built for the same AMDGPU architecture.
146 +#
147 +# The generated USE-flag list is compatible with packages using rocm.eclass.
148 +#
149 +# Example use:
150 +# @CODE
151 +# DEPEND="sci-libs/rocBLAS[${ROCM_USEDEP}]"
152 +# @CODE
153 +
154 +# @FUNCTION: _rocm_set_globals
155 +# @DESCRIPTION:
156 +# Set global variables used by the eclass: ALL_AMDGPU_TARGETS,
157 +# OFFICIAL_AMDGPU_TARGETS, ROCM_REQUIRED_USE, and ROCM_USEDEP
158 +_rocm_set_globals() {
159 + case ${ROCM_VERSION:-${PV}} in
160 + 4.*)
161 + ALL_AMDGPU_TARGETS=(
162 + gfx803 gfx900 gfx906 gfx908
163 + gfx1010 gfx1011 gfx1012 gfx1030
164 + )
165 + OFFICIAL_AMDGPU_TARGETS=(
166 + gfx906 gfx908
167 + )
168 + ;;
169 + 5.*)
170 + ALL_AMDGPU_TARGETS=(
171 + gfx803 gfx900 gfx906 gfx908 gfx90a
172 + gfx1010 gfx1011 gfx1012 gfx1030 gfx1031
173 + )
174 + OFFICIAL_AMDGPU_TARGETS=(
175 + gfx906 gfx908 gfx90a gfx1030
176 + )
177 + ;;
178 + *)
179 + die "Unknown ROCm major version! Please update rocm.eclass before bumping to new ebuilds"
180 + ;;
181 + esac
182 +
183 + ROCM_REQUIRED_USE+=" || ("
184 + for gpu_target in "${ALL_AMDGPU_TARGETS[@]}"; do
185 + if has ${gpu_target} "${OFFICIAL_AMDGPU_TARGETS[*]}"; then
186 + IUSE+=" ${gpu_target/#/+amdgpu_targets_}"
187 + else
188 + IUSE+=" ${gpu_target/#/amdgpu_targets_}"
189 + fi
190 + ROCM_REQUIRED_USE+=" ${gpu_target/#/amdgpu_targets_}"
191 + done
192 + ROCM_REQUIRED_USE+=" ) "
193 +
194 + local flags=( "${ALL_AMDGPU_TARGETS[@]/#/amdgpu_targets_}" )
195 + local optflags=${flags[@]/%/(-)?}
196 + ROCM_USEDEP=${optflags// /,}
197 +}
198 +_rocm_set_globals
199 +unset -f _rocm_set_globals
200 +
201 +
202 +# @FUNCTION: get_amdgpu_flags
203 +# @USAGE: get_amdgpu_flags
204 +# @DESCRIPTION:
205 +# Convert specified use flag of amdgpu_targets to compilation flags.
206 +# Append default target feature to GPU arch. See
207 +# https://llvm.org/docs/AMDGPUUsage.html#target-features
208 +get_amdgpu_flags() {
209 + local AMDGPU_TARGET_FLAGS
210 + for gpu_target in "${ALL_AMDGPU_TARGETS[@]}"; do
211 + local target_feature=
212 + if use amdgpu_targets_${gpu_target}; then
213 + case ${gpu_target} in
214 + gfx906|gfx908)
215 + target_feature=:xnack-
216 + ;;
217 + gfx90a)
218 + target_feature=:xnack+
219 + ;;
220 + *)
221 + ;;
222 + esac
223 + AMDGPU_TARGET_FLAGS+="${gpu_target}${target_feature};"
224 + fi
225 + done
226 + echo ${AMDGPU_TARGET_FLAGS}
227 +}
228 +
229 +# @FUNCTION: check_rw_permission
230 +# @USAGE: check_rw_permission <file>
231 +# @DESCRIPTION:
232 +# check read and write permissions on specific files.
233 +# allow using wildcard, for example check_rw_permission /dev/dri/render*
234 +check_rw_permission() {
235 + [[ -r $1 ]] && [[ -w $1 ]] || die \
236 + "Portage do not have read or write permissions on $1! \n Make sure both are in render group and check the permissions."
237 +}
238 +
239 +# == phase functions ==
240 +
241 +# @FUNCTION: rocm_src_configure
242 +# @DESCRIPTION:
243 +# configure rocm packages, and setting common cmake arguments
244 +rocm_src_configure() {
245 + # allow acces to hardware
246 + addpredict /dev/kfd
247 + addpredict /dev/dri/
248 +
249 + mycmakeargs+=(
250 + -DCMAKE_INSTALL_PREFIX="${EPREFIX}/usr"
251 + -DAMDGPU_TARGETS="$(get_amdgpu_flags)"
252 + -DCMAKE_SKIP_RPATH=TRUE
253 + )
254 +
255 + CXX="hipcc" cmake_src_configure
256 +}
257 +
258 +# @FUNCTION: rocm_src_test
259 +# @DESCRIPTION:
260 +# Test whether valid GPU device is present. If so, find how to, and execute test.
261 +# ROCm packages can have to test mechanism:
262 +# 1. cmake_src_test. MAKEOPTS="-j1" ensures only one test on GPU at a time;
263 +# 2. one single gtest binary called "${PN,,}"-test;
264 +# 3. Some package like rocFFT have alternative test like rocfft-selftest;
265 +# 4. Custome testing binaries like dev-libs/rccl. Use ${ROCM_TESTS} to specify.
266 +rocm_src_test() {
267 + # grant and check permissions on /dev/kfd and /dev/dri/render*
268 + for device in /dev/kfd /dev/dri/render*; do
269 + addwrite ${device}
270 + check_rw_permission ${device}
271 + done
272 +
273 + : ${LD_LIBRARY_PATH:="${BUILD_DIR}/clients:${BUILD_DIR}/src:${BUILD_DIR}/library:${BUILD_DIR}/library/src:${BUILD_DIR}/library/src/device"}
274 + export LD_LIBRARY_PATH
275 + if grep -q 'build test:' "${BUILD_DIR}"/build.ninja; then
276 + MAKEOPTS="-j1" cmake_src_test
277 + elif [[ -d ${BUILD_DIR}/clients/staging ]]; then
278 + cd "${BUILD_DIR}/clients/staging" || die "Test directory not found!"
279 + for test_program in "${PN,,}-"*test; do
280 + if [[ -x ${test_program} ]]; then
281 + edob ./${test_program}
282 + else
283 + die "The test program ${test_program} does not exist or cannot be excuted!"
284 + fi
285 + done
286 + elif [[ -n ${ROCM_TESTS} ]]; then
287 + for test_program in ${ROCM_TESTS}; do
288 + cd "${BUILD_DIR}" || die
289 + if [[ -x ${test_program} ]]; then
290 + edob ./${test_program}
291 + else
292 + die "The test program ${test_program} does not exist or cannot be excuted!"
293 + fi
294 + done
295 + else
296 + die "There is no cmake tests, no \${ROCM_TESTS} executable provided, nor ${BUILD_DIR}/clients/staging where test program might be located."
297 + fi
298 +}
299 +
300 +_ROCM_ECLASS=1
301 +fi
302 diff --git a/profiles/base/make.defaults b/profiles/base/make.defaults
303 index 326cb28de537..2c288d12d103 100644
304 --- a/profiles/base/make.defaults
305 +++ b/profiles/base/make.defaults
306 @@ -13,7 +13,7 @@ USE_EXPAND_VALUES_USERLAND="BSD GNU"
307
308 # Env vars to expand into USE vars. Modifying this requires prior
309 # discussion on gentoo-dev@l.g.o.
310 -USE_EXPAND="ABI_MIPS ABI_S390 ABI_X86 ADA_TARGET ALSA_CARDS APACHE2_MODULES APACHE2_MPMS CALLIGRA_FEATURES CAMERAS COLLECTD_PLUGINS CPU_FLAGS_ARM CPU_FLAGS_PPC CPU_FLAGS_X86 CURL_SSL ELIBC FFTOOLS GPSD_PROTOCOLS GRUB_PLATFORMS INPUT_DEVICES KERNEL L10N LCD_DEVICES LIBREOFFICE_EXTENSIONS LLVM_TARGETS LUA_SINGLE_TARGET LUA_TARGETS MONKEYD_PLUGINS NGINX_MODULES_HTTP NGINX_MODULES_MAIL NGINX_MODULES_STREAM OFFICE_IMPLEMENTATION OPENMPI_FABRICS OPENMPI_OFED_FEATURES OPENMPI_RM PHP_TARGETS POSTGRES_TARGETS PYTHON_SINGLE_TARGET PYTHON_TARGETS QEMU_SOFTMMU_TARGETS QEMU_USER_TARGETS ROS_MESSAGES RUBY_TARGETS SANE_BACKENDS USERLAND UWSGI_PLUGINS VIDEO_CARDS VOICEMAIL_STORAGE XTABLES_ADDONS"
311 +USE_EXPAND="ABI_MIPS ABI_S390 ABI_X86 ADA_TARGET ALSA_CARDS AMDGPU_TARGETS APACHE2_MODULES APACHE2_MPMS CALLIGRA_FEATURES CAMERAS COLLECTD_PLUGINS CPU_FLAGS_ARM CPU_FLAGS_PPC CPU_FLAGS_X86 CURL_SSL ELIBC FFTOOLS GPSD_PROTOCOLS GRUB_PLATFORMS INPUT_DEVICES KERNEL L10N LCD_DEVICES LIBREOFFICE_EXTENSIONS LLVM_TARGETS LUA_SINGLE_TARGET LUA_TARGETS MONKEYD_PLUGINS NGINX_MODULES_HTTP NGINX_MODULES_MAIL NGINX_MODULES_STREAM OFFICE_IMPLEMENTATION OPENMPI_FABRICS OPENMPI_OFED_FEATURES OPENMPI_RM PHP_TARGETS POSTGRES_TARGETS PYTHON_SINGLE_TARGET PYTHON_TARGETS QEMU_SOFTMMU_TARGETS QEMU_USER_TARGETS ROS_MESSAGES RUBY_TARGETS SANE_BACKENDS USERLAND UWSGI_PLUGINS VIDEO_CARDS VOICEMAIL_STORAGE XTABLES_ADDONS"
312
313 # USE_EXPAND variables whose contents are not shown in package manager
314 # output. Changes need discussion on gentoo-dev.
315 --
316 2.34.1