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

Replies

Subject Author
Re: [gentoo-dev] [PATCH 1/2] rocm.eclass: new eclass Ulrich Mueller <ulm@g.o>