Gentoo Archives: gentoo-dev

From: Yiyang Wu <xgreenlandforwyy@×××××.com>
To: gentoo-dev@l.g.o
Cc: Benda Xu <heroxbd@g.o>, "Michał Górny" <mgorny@g.o>
Subject: [gentoo-dev] [PATCH v6 1/2] rocm.eclass: new eclass
Date: Wed, 31 Aug 2022 16:41:01
Message-Id: 80098f72fcb37017063c50f44c18a9ac1e88c9b8.1661963640.git.xgreenlandforwyy@gmail.com
In Reply to: [gentoo-dev] [PATCH v6 0/2] rocm.eclass: new eclass 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 | 284 ++++++++++++++++++++++++++++++++++++
14 profiles/base/make.defaults | 2 +-
15 2 files changed, 285 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..1866d6b7cc94
21 --- /dev/null
22 +++ b/eclass/rocm.eclass
23 @@ -0,0 +1,284 @@
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-{configure,test} in src_configure and src_test.
45 +#
46 +# @EXAMPLE:
47 +# @CODE
48 +# # Example ebuild for ROCm library in https://github.com/ROCmSoftwarePlatform
49 +# # whcih depends on rocBLAS
50 +# inherit cmake rocm
51 +# # ROCm libraries SRC_URI is usually in form of:
52 +# SRC_URI="https://github.com/ROCmSoftwarePlatform/${PN}/archive/rocm-${PV}.tar.gz -> ${P}.tar.gz"
53 +# S=${WORKDIR}/${PN}-rocm-${PV}
54 +# SLOT="0/$(ver_cut 1-2)"
55 +# IUSE="test"
56 +# REQUIRED_USE="${ROCM_REQUIRED_USE}"
57 +# RESTRICT="!test? ( test )"
58 +#
59 +# RDEPEND="
60 +# dev-util/hip
61 +# sci-libs/rocBLAS:${SLOT}[${ROCM_USEDEP}]
62 +# "
63 +#
64 +# src_configure() {
65 +# local mycmakeargs=(
66 +# -DBUILD_CLIENTS_TESTS=$(usex test ON OFF)
67 +# )
68 +# rocm-configure
69 +# }
70 +#
71 +# src_test() {
72 +# rocm-test
73 +# }
74 +# @CODE
75 +#
76 +# # Example for packages depend on ROCm libraries -- a package depend on
77 +# # rocBLAS, and use comma seperated ${HCC_AMDGPU_TARGET} to determine GPU
78 +# # architecture to compile. Requires ROCm version >5.
79 +# @CODE
80 +# ROCM_VERSION=5.1
81 +# inherit rocm
82 +# IUSE="rocm"
83 +# REQUIRED_USE="rocm? ( ${ROCM_REQUIRED_USE} )"
84 +# DEPEND="rocm? ( >=dev-util/hip-${ROCM_VERSION}
85 +# >=sci-libs/rocBLAS-${ROCM_VERSION}[${ROCM_USEDEP}] )"
86 +# ....
87 +# src_configure() {
88 +# if use rocm; then
89 +# local AMDGPU_FLAGS=$(get_amdgpu_flags)
90 +# export HCC_AMDGPU_TARGET=${AMDGPU_FLAGS//;/,}
91 +# fi
92 +# default
93 +# }
94 +# @CODE
95 +
96 +if [[ ! ${_ROCM_ECLASS} ]]; then
97 +
98 +case ${EAPI} in
99 + 7|8) ;;
100 + *) die "${ECLASS}: EAPI ${EAPI:-0} not supported" ;;
101 +esac
102 +
103 +inherit edo
104 +
105 +# @ECLASS_VARIABLE: ROCM_VERSION
106 +# @DEFAULT_UNSET
107 +# @PRE_INHERIT
108 +# @DESCRIPTION:
109 +# The ROCm version of current package. Default is ${PV}, but for other packages
110 +# that depend on ROCm libraries, this can be set to match the version of
111 +# required ROCm libraries.
112 +
113 +# @ECLASS_VARIABLE: ALL_AMDGPU_TARGETS
114 +# @INTERNAL
115 +# @DESCRIPTION:
116 +# The list of USE flags corresponding to all AMDGPU targets in this ROCm
117 +# version. The value depends on ${PV}. Architectures and devices map:
118 +# https://www.coelacanth-dream.com/posts/2019/12/30/did-rid-product-matome-p2
119 +
120 +# @ECLASS_VARIABLE: OFFICIAL_AMDGPU_TARGETS
121 +# @INTERNAL
122 +# @DESCRIPTION:
123 +# The list of USE flags corresponding to all officially supported AMDGPU
124 +# targets in this ROCm version, documented at
125 +# https://docs.amd.com/bundle/ROCm-Installation-Guide-v${PV}/page/Prerequisite_Actions.html.
126 +# USE flag of these architectures will be default on. Depends on ${PV}.
127 +
128 +# @ECLASS_VARIABLE: ROCM_REQUIRED_USE
129 +# @OUTPUT_VARIABLE
130 +# @DESCRIPTION:
131 +# Requires at least one AMDGPU target to be compiled.
132 +# Example use for ROCm libraries:
133 +# @CODE
134 +# REQUIRED_USE="${ROCM_REQUIRED_USE}"
135 +# @CODE
136 +# Example use for packages that depend on ROCm libraries
137 +# @CODE
138 +# IUSE="rocm"
139 +# REQUIRED_USE="rocm? ( ${ROCM_REQUIRED_USE} )"
140 +# @CODE
141 +
142 +# @ECLASS_VARIABLE: ROCM_USEDEP
143 +# @OUTPUT_VARIABLE
144 +# @DESCRIPTION:
145 +# This is an eclass-generated USE-dependency string which can be used to
146 +# depend on another ROCm package being built for the same AMDGPU architecture.
147 +#
148 +# The generated USE-flag list is compatible with packages using rocm.eclass.
149 +#
150 +# Example use:
151 +# @CODE
152 +# DEPEND="sci-libs/rocBLAS[${ROCM_USEDEP}]"
153 +# @CODE
154 +
155 +# @FUNCTION: _rocm_set_globals
156 +# @DESCRIPTION:
157 +# Set global variables used by the eclass: ALL_AMDGPU_TARGETS,
158 +# OFFICIAL_AMDGPU_TARGETS, ROCM_REQUIRED_USE, and ROCM_USEDEP
159 +_rocm_set_globals() {
160 + case ${ROCM_VERSION:-${PV}} in
161 + 4.*)
162 + ALL_AMDGPU_TARGETS=(
163 + gfx803 gfx900 gfx906 gfx908
164 + gfx1010 gfx1011 gfx1012 gfx1030
165 + )
166 + OFFICIAL_AMDGPU_TARGETS=(
167 + gfx906 gfx908
168 + )
169 + ;;
170 + 5.*)
171 + ALL_AMDGPU_TARGETS=(
172 + gfx803 gfx900 gfx906 gfx908 gfx90a
173 + gfx1010 gfx1011 gfx1012 gfx1030 gfx1031
174 + )
175 + OFFICIAL_AMDGPU_TARGETS=(
176 + gfx906 gfx908 gfx90a gfx1030
177 + )
178 + ;;
179 + *)
180 + die "Unknown ROCm major version! Please update rocm.eclass before bumping to new ebuilds"
181 + ;;
182 + esac
183 +
184 + ROCM_REQUIRED_USE+=" || ("
185 + for gpu_target in "${ALL_AMDGPU_TARGETS[@]}"; do
186 + if has "${gpu_target}" "${OFFICIAL_AMDGPU_TARGETS[@]}"; then
187 + IUSE+=" ${gpu_target/#/+amdgpu_targets_}"
188 + else
189 + IUSE+=" ${gpu_target/#/amdgpu_targets_}"
190 + fi
191 + ROCM_REQUIRED_USE+=" ${gpu_target/#/amdgpu_targets_}"
192 + done
193 + ROCM_REQUIRED_USE+=" ) "
194 +
195 + local flags=( "${ALL_AMDGPU_TARGETS[@]/#/amdgpu_targets_}" )
196 + local optflags=${flags[@]/%/(-)?}
197 + ROCM_USEDEP=${optflags// /,}
198 +}
199 +_rocm_set_globals
200 +unset -f _rocm_set_globals
201 +
202 +
203 +# @FUNCTION: get_amdgpu_flags
204 +# @USAGE: get_amdgpu_flags
205 +# @DESCRIPTION:
206 +# Convert specified use flag of amdgpu_targets to compilation flags.
207 +# Append default target feature to GPU arch. See
208 +# https://llvm.org/docs/AMDGPUUsage.html#target-features
209 +get_amdgpu_flags() {
210 + local AMDGPU_TARGET_FLAGS
211 + for gpu_target in "${ALL_AMDGPU_TARGETS[@]}"; do
212 + local target_feature=
213 + if use amdgpu_targets_${gpu_target}; then
214 + case ${gpu_target} in
215 + gfx906|gfx908)
216 + target_feature=:xnack-
217 + ;;
218 + gfx90a)
219 + target_feature=:xnack+
220 + ;;
221 + *)
222 + ;;
223 + esac
224 + AMDGPU_TARGET_FLAGS+="${gpu_target}${target_feature};"
225 + fi
226 + done
227 + echo ${AMDGPU_TARGET_FLAGS}
228 +}
229 +
230 +# @FUNCTION: check_rw_permission
231 +# @USAGE: check_rw_permission <file>
232 +# @DESCRIPTION:
233 +# check read and write permissions on a specific file, die if no permission.
234 +# @EXAMPLE:
235 +# @CODE
236 +# check_rw_permission /dev/kfd
237 +# CODE
238 +check_rw_permission() {
239 + if [[ ! -r $1 ]] || [[ ! -w $1 ]]; then
240 + eerror "Portage do not have read or write permissions on $1!"
241 + eerror "Make sure both are in render group and check the permissions."
242 + die "No permissions on $1"
243 + fi
244 +}
245 +
246 +# == phase functions ==
247 +
248 +# @FUNCTION: rocm-configure
249 +# @DESCRIPTION:
250 +# configure rocm packages, and setting common cmake arguments. Only for ROCm
251 +# libraries in https://github.com/ROCmSoftwarePlatform using cmake.
252 +rocm-configure() {
253 + # avoid sandbox violation
254 + addpredict /dev/kfd
255 + addpredict /dev/dri/
256 +
257 + mycmakeargs+=(
258 + -DAMDGPU_TARGETS="$(get_amdgpu_flags)"
259 + -DCMAKE_SKIP_RPATH=TRUE
260 + )
261 +
262 + CXX="hipcc" cmake_src_configure
263 +}
264 +
265 +# @FUNCTION: rocm-test
266 +# @DESCRIPTION:
267 +# Test whether valid GPU device is present. If so, execute test.
268 +# @EXAMPLE:
269 +# ROCm packages can have two test scenarioes:
270 +# 1. cmake_src_test. MAKEOPTS="-j1" ensures only one test on GPU at a time;
271 +# @CODE
272 +# LD_LIBRARY_PATH=<path-to-lib> rocm-test --cmake
273 +# @CODE
274 +# 2. one gtest binary called "${PN,,}"-test in ${BUILD_DIR}/clients/staging;
275 +# @CODE
276 +# cd "${BUILD_DIR}"/clients/staging || die
277 +# LD_LIBRARY_PATH=<path-to-lib> rocm-test "${PN,,}"-test
278 +# @CODE
279 +# Some packages like rocFFT have two test binaries like rocfft-selftest;
280 +# packages like dev-libs/rccl have test binary with custom names.
281 +# @CODE
282 +# cd "${BUILD_DIR}"/clients/staging || die
283 +# export LD_LIBRARY_PATH=<path-to-lib>
284 +# cd <test-bin-location> || die
285 +# rocm-test <test-bin-1>
286 +# rocm-test <test-bin-2>
287 +# @CODE
288 +rocm-test() {
289 + # grant and check permissions on /dev/kfd and /dev/dri/render*
290 + for device in /dev/kfd /dev/dri/render*; do
291 + addwrite "${device}"
292 + check_rw_permission "${device}"
293 + done
294 +
295 + case ${1} in
296 + --cmake)
297 + # Avoid multi jobs running that may cause GPU error or CPU overload
298 + MAKEOPTS="-j1" cmake_src_test
299 + ;;
300 + *)
301 + edob ./${1}
302 + ;;
303 + esac
304 +}
305 +
306 +_ROCM_ECLASS=1
307 +fi
308 diff --git a/profiles/base/make.defaults b/profiles/base/make.defaults
309 index 326cb28de537..2c288d12d103 100644
310 --- a/profiles/base/make.defaults
311 +++ b/profiles/base/make.defaults
312 @@ -13,7 +13,7 @@ USE_EXPAND_VALUES_USERLAND="BSD GNU"
313
314 # Env vars to expand into USE vars. Modifying this requires prior
315 # discussion on gentoo-dev@l.g.o.
316 -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"
317 +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"
318
319 # USE_EXPAND variables whose contents are not shown in package manager
320 # output. Changes need discussion on gentoo-dev.
321 --
322 2.34.1