From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <gentoo-commits+bounces-1669051-garchives=archives.gentoo.org@lists.gentoo.org>
Received: from lists.gentoo.org (pigeon.gentoo.org [208.92.234.80])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (2048 bits))
	(No client certificate requested)
	by finch.gentoo.org (Postfix) with ESMTPS id 5BBAA158083
	for <garchives@archives.gentoo.org>; Mon,  9 Sep 2024 18:20:42 +0000 (UTC)
Received: from pigeon.gentoo.org (localhost [127.0.0.1])
	by pigeon.gentoo.org (Postfix) with SMTP id 7387FE29FD;
	Mon,  9 Sep 2024 18:20:41 +0000 (UTC)
Received: from smtp.gentoo.org (dev.gentoo.org [IPv6:2001:470:ea4a:1:5054:ff:fec7:86e4])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by pigeon.gentoo.org (Postfix) with ESMTPS id 5A191E29FD
	for <gentoo-commits@lists.gentoo.org>; Mon,  9 Sep 2024 18:20:41 +0000 (UTC)
Received: from oystercatcher.gentoo.org (oystercatcher.gentoo.org [148.251.78.52])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256)
	(No client certificate requested)
	by smtp.gentoo.org (Postfix) with ESMTPS id 8F32A33FECD
	for <gentoo-commits@lists.gentoo.org>; Mon,  9 Sep 2024 18:20:40 +0000 (UTC)
Received: from localhost.localdomain (localhost [IPv6:::1])
	by oystercatcher.gentoo.org (Postfix) with ESMTP id EF77B14D0
	for <gentoo-commits@lists.gentoo.org>; Mon,  9 Sep 2024 18:20:38 +0000 (UTC)
From: "Sam James" <sam@gentoo.org>
To: gentoo-commits@lists.gentoo.org
Content-Transfer-Encoding: 8bit
Content-type: text/plain; charset=UTF-8
Reply-To: gentoo-dev@lists.gentoo.org, "Sam James" <sam@gentoo.org>
Message-ID: <1725905710.e2c865d8b5507755d6ad9c63e3b4b8ccbb5d6cd7.sam@gentoo>
Subject: [gentoo-commits] repo/gentoo:master commit in: eclass/
X-VCS-Repository: repo/gentoo
X-VCS-Files: eclass/rocm.eclass
X-VCS-Directories: eclass/
X-VCS-Committer: sam
X-VCS-Committer-Name: Sam James
X-VCS-Revision: e2c865d8b5507755d6ad9c63e3b4b8ccbb5d6cd7
X-VCS-Branch: master
Date: Mon,  9 Sep 2024 18:20:38 +0000 (UTC)
Precedence: bulk
List-Post: <mailto:gentoo-commits@lists.gentoo.org>
List-Help: <mailto:gentoo-commits+help@lists.gentoo.org>
List-Unsubscribe: <mailto:gentoo-commits+unsubscribe@lists.gentoo.org>
List-Subscribe: <mailto:gentoo-commits+subscribe@lists.gentoo.org>
List-Id: Gentoo Linux mail <gentoo-commits.gentoo.org>
X-BeenThere: gentoo-commits@lists.gentoo.org
X-Auto-Response-Suppress: DR, RN, NRN, OOF, AutoReply
X-Archives-Salt: b5e004f4-5da2-4a91-b62a-01540161f623
X-Archives-Hash: 5c94fdfcbe9efec3f67f027bc758d9d7

commit:     e2c865d8b5507755d6ad9c63e3b4b8ccbb5d6cd7
Author:     Sv. Lockal <lockalsash <AT> gmail <DOT> com>
AuthorDate: Tue Jul 30 23:14:55 2024 +0000
Commit:     Sam James <sam <AT> gentoo <DOT> org>
CommitDate: Mon Sep  9 18:15:10 2024 +0000
URL:        https://gitweb.gentoo.org/repo/gentoo.git/commit/?id=e2c865d8

rocm.eclass: add rocm_use_hipcc function and update example accordingly

Bug: https://bugs.gentoo.org/936099
Signed-off-by: Sv. Lockal <lockalsash <AT> gmail.com>
Signed-off-by: Sam James <sam <AT> gentoo.org>

 eclass/rocm.eclass | 40 ++++++++++++++++++++++++++++++++++------
 1 file changed, 34 insertions(+), 6 deletions(-)

diff --git a/eclass/rocm.eclass b/eclass/rocm.eclass
index 7039455dec6b..cf501d5a8861 100644
--- a/eclass/rocm.eclass
+++ b/eclass/rocm.eclass
@@ -15,9 +15,13 @@
 # edit USE flag to control which GPU architecture to compile. Using
 # ${ROCM_USEDEP} can ensure coherence among dependencies. Ebuilds can call the
 # function get_amdgpu_flag to translate activated target to GPU compile flags,
-# passing it to configuration. Function check_amdgpu can help ebuild ensure
+# passing it to configuration. Function rocm_use_hipcc switches active compiler
+# to hipcc and cleans incompatible flags (useful for users with gcc-only flags
+# in /etc/portage/make.conf). Function check_amdgpu can help ebuild ensure
 # read and write permissions to GPU device in src_test phase, throwing friendly
-# error message if unavailable.
+# error message if unavailable. However src_configure in general should not
+# access any AMDGPU devices. If it does, it usually means that CMakeLists.txt
+# ignores AMDGPU_TARGETS in favor of autodetected GPU, which is not desired.
 #
 # @EXAMPLE:
 # Example ebuild for ROCm library in https://github.com/ROCmSoftwarePlatform
@@ -39,14 +43,12 @@
 # "
 #
 # src_configure() {
-#     # avoid sandbox violation
-#     addpredict /dev/kfd
-#     addpredict /dev/dri/
+#     rocm_use_hipcc
 #     local mycmakeargs=(
 #         -DAMDGPU_TARGETS="$(get_amdgpu_flags)"
 #         -DBUILD_CLIENTS_TESTS=$(usex test ON OFF)
 #     )
-#     CXX=hipcc cmake_src_configure
+#     cmake_src_configure
 # }
 #
 # src_test() {
@@ -90,6 +92,8 @@ esac
 if [[ ! ${_ROCM_ECLASS} ]]; then
 _ROCM_ECLASS=1
 
+inherit flag-o-matic
+
 # @ECLASS_VARIABLE: ROCM_VERSION
 # @REQUIRED
 # @PRE_INHERIT
@@ -231,3 +235,27 @@ check_amdgpu() {
 }
 
 fi
+
+# @FUNCTION: rocm_use_hipcc
+# @USAGE: rocm_use_hipcc
+# @DESCRIPTION:
+# switch active C and C++ compilers to hipcc, clean unsupported flags and setup ROCM_TARGET_LST file.
+rocm_use_hipcc() {
+	# During the configuration stage, CMake tests whether the compiler is able to compile a simple program.
+	# Since CMake checker does not specify --offload-arch=, hipcc enumerates devices using four methods
+	# until it finds at least one device. Last way is by accessing them (via rocminfo).
+	# To prevent potential sandbox violations, we set the ROCM_TARGET_LST variable (which is checked first).
+	local target_lst="${T}"/gentoo_rocm_target.lst
+	if [[ "${AMDGPU_TARGETS[@]}" = "" ]]; then
+		# Expected no GPU code; still need to calm down sandbox
+		echo "gfx000" > "${target_lst}" || die
+	else
+		printf "%s\n" ${AMDGPU_TARGETS[@]} > "${target_lst}" || die
+	fi
+	export ROCM_TARGET_LST="${target_lst}"
+
+	# Export updated CC and CXX. Note that CC is needed even if no C code used,
+	# as CMake checks that C compiler can compile a simple test program.
+	export CC=hipcc CXX=hipcc
+	strip-unsupported-flags
+}