Gentoo Archives: gentoo-dev

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

Replies