From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153844; cv=none; d=zohomail.com; s=zohoarc; b=G3+zTTx2XILfIYTKd8oRkUDI5T1RFCHOM6RFEeJQMf13Zpa9SpSXZyRZLG2M+kG1DN6LmXuhuA66dHeRbMCyHMzfOtoA4CdlCKecEJqcqoEa24I/5RJzfp5uS3xBi0A28dpnXCLS46TBHi4nNFDLqMPhl9ZqUP6+cgC8GiW6OEQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153844; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=92nCTdq11lbmBeZ/kw31uZpfZQz1CwbJLefAdeyXWco=; b=aJ8+LL5M65QsyT7BBw7SyzM+1buO4iny9aDSiHH8VBzqCjvfKcRpRyj3mWl+3/KwY7whUvtAZijwt7Y78fKyUgWbpKOSZyz2heX9m8eGP8AuqkyMF3dg85g6BaAfhByjTnRyzrX/fppvE444uSf40BGTdUMHB7sKxLlX7X/V8lc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153844517828.9036532627227; Wed, 20 Nov 2024 17:50:44 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHh-0001Wa-7P; Wed, 20 Nov 2024 20:47:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHf-0001Vf-AC for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:03 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHd-0004WG-FT for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=92nCTdq11lbmBeZ/kw31uZpfZQz1CwbJLefAdeyXWco=; b=gIde7BioDXeDDff 7ENqxxKNMKavjEuuh1qdClGTzOBL3iDN4USMtm7mwj47KEEDhQ6FpLezAJhjMZr+JoviVSta9e7vv BKszrb81A0vpM2As20cqJsNeLtefTMP3iyDJu9WE/foNCh3Ycx+cRwzUGi4Ibz94HLfhZ++/Ldypw 2E=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 01/43] Add option to enable/disable helper-to-tcg Date: Thu, 21 Nov 2024 02:49:05 +0100 Message-ID: <20241121014947.18666-2-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153846667116600 Content-Type: text/plain; charset="utf-8" Adds a meson option for enabling/disabling helper-to-tcg along with a CONFIG_* definition. CONFIG_* will in future commits be used to conditionally include the helper-to-tcg subproject, and to remove unneeded code/memory when helper-to-tcg is not in use. Current meson option is limited to Hexagon, as helper-to-tcg will be included as a subproject from target/hexagon. This will change in the future if multiple frontends adopt helper-to-tcg. Signed-off-by: Anton Johansson Acked-by: Richard Henderson --- meson.build | 7 +++++++ meson_options.txt | 2 ++ scripts/meson-buildoptions.sh | 5 +++++ 3 files changed, 14 insertions(+) diff --git a/meson.build b/meson.build index e0b880e4e1..657ebe43f6 100644 --- a/meson.build +++ b/meson.build @@ -230,6 +230,7 @@ have_ga =3D get_option('guest_agent') \ error_message: 'unsupported OS for QEMU guest agent') \ .allowed() have_block =3D have_system or have_tools +helper_to_tcg_enabled =3D get_option('hexagon_helper_to_tcg') =20 enable_modules =3D get_option('modules') \ .require(host_os !=3D 'windows', @@ -3245,6 +3246,11 @@ foreach target : target_dirs 'CONFIG_QEMU_RTSIG_MAP': get_option('rtsig_map'), } endif + if helper_to_tcg_enabled + config_target +=3D { + 'CONFIG_HELPER_TO_TCG': 'y', + } + endif =20 target_kconfig =3D [] foreach sym: accelerators @@ -4122,6 +4128,7 @@ foreach target : target_dirs if host_os =3D=3D 'linux' target_inc +=3D include_directories('linux-headers', is_system: true) endif + if target.endswith('-softmmu') target_type=3D'system' t =3D target_system_arch[target_base_arch].apply(config_target, strict= : false) diff --git a/meson_options.txt b/meson_options.txt index 5eeaf3eee5..0730378305 100644 --- a/meson_options.txt +++ b/meson_options.txt @@ -374,6 +374,8 @@ option('qemu_ga_version', type: 'string', value: '', =20 option('hexagon_idef_parser', type : 'boolean', value : true, description: 'use idef-parser to automatically generate TCG code fo= r the Hexagon frontend') +option('hexagon_helper_to_tcg', type : 'boolean', value : true, + description: 'use the helper-to-tcg translator to automatically gen= erate TCG code for the Hexagon frontend') =20 option('x86_version', type : 'combo', choices : ['0', '1', '2', '3', '4'],= value: '1', description: 'tweak required x86_64 architecture version beyond com= piler default') diff --git a/scripts/meson-buildoptions.sh b/scripts/meson-buildoptions.sh index a8066aab03..19c891a39b 100644 --- a/scripts/meson-buildoptions.sh +++ b/scripts/meson-buildoptions.sh @@ -13,6 +13,9 @@ meson_options_help() { printf "%s\n" ' --datadir=3DVALUE Data file directory [share]' printf "%s\n" ' --disable-coroutine-pool coroutine freelist (better per= formance)' printf "%s\n" ' --disable-debug-info Enable debug symbols and other= information' + printf "%s\n" ' --disable-hexagon-helper-to-tcg' + printf "%s\n" ' use the helper-to-tcg translat= or to automatically' + printf "%s\n" ' generate TCG code for the Hexa= gon frontend' printf "%s\n" ' --disable-hexagon-idef-parser' printf "%s\n" ' use idef-parser to automatical= ly generate TCG' printf "%s\n" ' code for the Hexagon frontend' @@ -341,6 +344,8 @@ _meson_option_parse() { --disable-guest-agent) printf "%s" -Dguest_agent=3Ddisabled ;; --enable-guest-agent-msi) printf "%s" -Dguest_agent_msi=3Denabled ;; --disable-guest-agent-msi) printf "%s" -Dguest_agent_msi=3Ddisabled ;; + --enable-hexagon-helper-to-tcg) printf "%s" -Dhexagon_helper_to_tcg=3D= true ;; + --disable-hexagon-helper-to-tcg) printf "%s" -Dhexagon_helper_to_tcg= =3Dfalse ;; --enable-hexagon-idef-parser) printf "%s" -Dhexagon_idef_parser=3Dtrue= ;; --disable-hexagon-idef-parser) printf "%s" -Dhexagon_idef_parser=3Dfal= se ;; --enable-hv-balloon) printf "%s" -Dhv_balloon=3Denabled ;; --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153740; cv=none; d=zohomail.com; s=zohoarc; b=MfI+rav8iz7zc4j8ix9Nb6OGOHl7uqIqdEA37p6oW/qXIeNMB5K7Sryn8h/jPQjnI49jVJfXOL7c0bxi7erylE+SrHkg78L5ZeZrfEO+ObqnUEl4m4HQ/tOYRAApkBR4VLHbyuM7c5IB4tVdZeYKntyJWrymiG6v1kzKTPJG4OY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153740; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=II26gCWf23z5pgLLS6uEpV6LzQJG3dH6fmuJ7BDdgmg=; b=mUeNLd7OrWsfDA8X3eEuyn03OIw1hXzoElp83QEefuFx+BpH8WN3XPNTkOmMz1/lxhZRfgnSGruvP2VPsVALPRgPORzJsY8Do9EhyDfH/ElsdXfv8sEPov2FGbPWYU5zuCu7SD5hHQ8pqHIrueQRV/3EuNZu0GxyeZEtfQAhqoM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153740374806.4966450716669; Wed, 20 Nov 2024 17:49:00 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHg-0001WE-J5; Wed, 20 Nov 2024 20:47:04 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHf-0001Ve-1n for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:03 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHd-0004WN-4d for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:02 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=II26gCWf23z5pgLLS6uEpV6LzQJG3dH6fmuJ7BDdgmg=; b=BzuvtGtp0TClplo mfMKPTAL6TLmv1o53RXWJIt2c5QuHc0LC6PZnXC+QlyaVAh8BVzoCupiKo33iSZFIA1Cb08wFfFfW T65MsQM6JZcPDNnXUP+XyC0N8aTCI1wpwwZde+5wFfyebiGdDUOc2ZzWRqK5fmjZfc0YeI0A3bX3A Gk=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 02/43] accel/tcg: Add bitreverse and funnel-shift runtime helper functions Date: Thu, 21 Nov 2024 02:49:06 +0100 Message-ID: <20241121014947.18666-3-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153742245116600 Content-Type: text/plain; charset="utf-8" Adds necessary helper functions for mapping LLVM IR onto TCG. Specifically, helpers corresponding to the bitreverse and funnel-shift intrinsics in LLVM. Note: these may be converted to more efficient implementations in the future, but for the time being it allows helper-to-tcg to support a wider subset of LLVM IR. Signed-off-by: Anton Johansson --- accel/tcg/tcg-runtime.c | 29 +++++++++++++++++++++++++++++ accel/tcg/tcg-runtime.h | 5 +++++ 2 files changed, 34 insertions(+) diff --git a/accel/tcg/tcg-runtime.c b/accel/tcg/tcg-runtime.c index 9fa539ad3d..6372fa3f6f 100644 --- a/accel/tcg/tcg-runtime.c +++ b/accel/tcg/tcg-runtime.c @@ -23,6 +23,8 @@ */ #include "qemu/osdep.h" #include "qemu/host-utils.h" +#include "qemu/int128.h" +#include "qemu/bitops.h" #include "cpu.h" #include "exec/helper-proto-common.h" #include "exec/cpu_ldst.h" @@ -57,6 +59,21 @@ uint32_t HELPER(remu_i32)(uint32_t arg1, uint32_t arg2) return arg1 % arg2; } =20 +uint32_t HELPER(bitreverse8_i32)(uint32_t x) +{ + return revbit8((uint8_t) x); +} + +uint32_t HELPER(bitreverse16_i32)(uint32_t x) +{ + return revbit16((uint16_t) x); +} + +uint32_t HELPER(bitreverse32_i32)(uint32_t x) +{ + return revbit32(x); +} + /* 64-bit helpers */ =20 uint64_t HELPER(shl_i64)(uint64_t arg1, uint64_t arg2) @@ -74,6 +91,13 @@ int64_t HELPER(sar_i64)(int64_t arg1, int64_t arg2) return arg1 >> arg2; } =20 +uint64_t HELPER(fshl_i64)(uint64_t a, uint64_t b, uint64_t c) +{ + Int128 d =3D int128_make128(b, a); + Int128 shift =3D int128_lshift(d, c); + return int128_gethi(shift); +} + int64_t HELPER(div_i64)(int64_t arg1, int64_t arg2) { return arg1 / arg2; @@ -94,6 +118,11 @@ uint64_t HELPER(remu_i64)(uint64_t arg1, uint64_t arg2) return arg1 % arg2; } =20 +uint64_t HELPER(bitreverse64_i64)(uint64_t x) +{ + return revbit64(x); +} + uint64_t HELPER(muluh_i64)(uint64_t arg1, uint64_t arg2) { uint64_t l, h; diff --git a/accel/tcg/tcg-runtime.h b/accel/tcg/tcg-runtime.h index c23b5e66c4..0a4d31eb48 100644 --- a/accel/tcg/tcg-runtime.h +++ b/accel/tcg/tcg-runtime.h @@ -2,15 +2,20 @@ DEF_HELPER_FLAGS_2(div_i32, TCG_CALL_NO_RWG_SE, s32, s32,= s32) DEF_HELPER_FLAGS_2(rem_i32, TCG_CALL_NO_RWG_SE, s32, s32, s32) DEF_HELPER_FLAGS_2(divu_i32, TCG_CALL_NO_RWG_SE, i32, i32, i32) DEF_HELPER_FLAGS_2(remu_i32, TCG_CALL_NO_RWG_SE, i32, i32, i32) +DEF_HELPER_FLAGS_1(bitreverse8_i32, TCG_CALL_NO_RWG_SE, i32, i32) +DEF_HELPER_FLAGS_1(bitreverse16_i32, TCG_CALL_NO_RWG_SE, i32, i32) +DEF_HELPER_FLAGS_1(bitreverse32_i32, TCG_CALL_NO_RWG_SE, i32, i32) =20 DEF_HELPER_FLAGS_2(div_i64, TCG_CALL_NO_RWG_SE, s64, s64, s64) DEF_HELPER_FLAGS_2(rem_i64, TCG_CALL_NO_RWG_SE, s64, s64, s64) DEF_HELPER_FLAGS_2(divu_i64, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(remu_i64, TCG_CALL_NO_RWG_SE, i64, i64, i64) +DEF_HELPER_FLAGS_1(bitreverse64_i64, TCG_CALL_NO_RWG_SE, i64, i64) =20 DEF_HELPER_FLAGS_2(shl_i64, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(shr_i64, TCG_CALL_NO_RWG_SE, i64, i64, i64) DEF_HELPER_FLAGS_2(sar_i64, TCG_CALL_NO_RWG_SE, s64, s64, s64) +DEF_HELPER_FLAGS_3(fshl_i64, TCG_CALL_NO_RWG_SE, i64, i64, i64, i64) =20 DEF_HELPER_FLAGS_2(mulsh_i64, TCG_CALL_NO_RWG_SE, s64, s64, s64) DEF_HELPER_FLAGS_2(muluh_i64, TCG_CALL_NO_RWG_SE, i64, i64, i64) --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153958; cv=none; d=zohomail.com; s=zohoarc; b=BxjTC/g2eJU2qpCgR2JhMendnQIWinnvIAvIllHaCNSPguYOaObnkTEHEZEVv3Q9H/TskLmBfEgGZ0mOi9g5kkp59Ul4yCRuxLoSntM4RVr6aQnzNTuxXbpeY7wdJ1suKC2AJRUPzO+sf9ALUfggNlTsK4vtubQMu34GuFmjTMw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153958; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=MkoZS5Eq2jzUTLA5ZLNru01SQe4ERiiU1sa7s9RAVVk=; b=lBWKuYKZg8mNFcaShbKZnA+nMe8rNGoPccQ/AjAF9F7+kxoErzBNyNyIR+8l9ULe2YiAfKiJCqd6KgDybDqLbUZakUGJdQXtt7DPjQRSRMdIKRzbZFlc6ibUD50tMtwKP2WwvlszXkpt4aj/SFt/lnh0SCeDDR57ugcJTgRFl1w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153958129664.7847548343003; Wed, 20 Nov 2024 17:52:38 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHi-0001Xt-Sn; Wed, 20 Nov 2024 20:47:06 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHg-0001WD-EJ for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:04 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHd-0004WP-DS for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=MkoZS5Eq2jzUTLA5ZLNru01SQe4ERiiU1sa7s9RAVVk=; b=B9e2n/WGT0vK5Ay QsuyY8C0qOM4af/q78rMJfsu9999M6PD1KQUcIX7+jphjNtW/zySV3MuYnwgZGC84ZO1jtTxclLEI xQ/stN/6hIu2glfeBF/jLv8jTTLMu4Q11llmjAbt0tDfg61x3mJ2XWOvrdcvNvmzaq3mVdDZZ+aV+ JE=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 03/43] accel/tcg: Add gvec size changing operations Date: Thu, 21 Nov 2024 02:49:07 +0100 Message-ID: <20241121014947.18666-4-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153959005116600 Content-Type: text/plain; charset="utf-8" Adds new functions to the gvec API for truncating, sign- or zero extending vector elements. Currently implemented as helper functions, these may be mapped onto host vector instructions in the future. For the time being, allows translation of more complicated vector instructions by helper-to-tcg. Signed-off-by: Anton Johansson --- accel/tcg/tcg-runtime-gvec.c | 41 +++++++++++++++++ accel/tcg/tcg-runtime.h | 22 +++++++++ include/tcg/tcg-op-gvec-common.h | 18 ++++++++ tcg/tcg-op-gvec.c | 78 ++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+) diff --git a/accel/tcg/tcg-runtime-gvec.c b/accel/tcg/tcg-runtime-gvec.c index afca89baa1..685c991e6a 100644 --- a/accel/tcg/tcg-runtime-gvec.c +++ b/accel/tcg/tcg-runtime-gvec.c @@ -1569,3 +1569,44 @@ void HELPER(gvec_bitsel)(void *d, void *a, void *b, = void *c, uint32_t desc) } clear_high(d, oprsz, desc); } + +#define DO_SZ_OP1(NAME, DSTTY, SRCTY) = \ +void HELPER(NAME)(void *d, void *a, uint32_t desc) = \ +{ = \ + intptr_t oprsz =3D simd_oprsz(desc); = \ + intptr_t elsz =3D oprsz/sizeof(DSTTY); = \ + intptr_t i; = \ + = \ + for (i =3D 0; i < elsz; ++i) { = \ + SRCTY aa =3D *((SRCTY *) a + i); = \ + *((DSTTY *) d + i) =3D aa; = \ + } = \ + clear_high(d, oprsz, desc); = \ +} + +#define DO_SZ_OP2(NAME, INTTY, DSTSZ, SRCSZ) \ + DO_SZ_OP1(NAME##SRCSZ##_##DSTSZ, INTTY##DSTSZ##_t, INTTY##SRCSZ##_t) + +DO_SZ_OP2(gvec_trunc, uint, 32, 64) +DO_SZ_OP2(gvec_trunc, uint, 16, 64) +DO_SZ_OP2(gvec_trunc, uint, 8, 64) +DO_SZ_OP2(gvec_trunc, uint, 16, 32) +DO_SZ_OP2(gvec_trunc, uint, 8, 32) +DO_SZ_OP2(gvec_trunc, uint, 8, 16) + +DO_SZ_OP2(gvec_zext, uint, 64, 32) +DO_SZ_OP2(gvec_zext, uint, 64, 16) +DO_SZ_OP2(gvec_zext, uint, 64, 8) +DO_SZ_OP2(gvec_zext, uint, 32, 16) +DO_SZ_OP2(gvec_zext, uint, 32, 8) +DO_SZ_OP2(gvec_zext, uint, 16, 8) + +DO_SZ_OP2(gvec_sext, int, 64, 32) +DO_SZ_OP2(gvec_sext, int, 64, 16) +DO_SZ_OP2(gvec_sext, int, 64, 8) +DO_SZ_OP2(gvec_sext, int, 32, 16) +DO_SZ_OP2(gvec_sext, int, 32, 8) +DO_SZ_OP2(gvec_sext, int, 16, 8) + +#undef DO_SZ_OP1 +#undef DO_SZ_OP2 diff --git a/accel/tcg/tcg-runtime.h b/accel/tcg/tcg-runtime.h index 0a4d31eb48..5045655bf8 100644 --- a/accel/tcg/tcg-runtime.h +++ b/accel/tcg/tcg-runtime.h @@ -1,3 +1,4 @@ +#include "tcg/tcg.h" DEF_HELPER_FLAGS_2(div_i32, TCG_CALL_NO_RWG_SE, s32, s32, s32) DEF_HELPER_FLAGS_2(rem_i32, TCG_CALL_NO_RWG_SE, s32, s32, s32) DEF_HELPER_FLAGS_2(divu_i32, TCG_CALL_NO_RWG_SE, i32, i32, i32) @@ -328,3 +329,24 @@ DEF_HELPER_FLAGS_4(gvec_leus32, TCG_CALL_NO_RWG, void,= ptr, ptr, i64, i32) DEF_HELPER_FLAGS_4(gvec_leus64, TCG_CALL_NO_RWG, void, ptr, ptr, i64, i32) =20 DEF_HELPER_FLAGS_5(gvec_bitsel, TCG_CALL_NO_RWG, void, ptr, ptr, ptr, ptr,= i32) + +DEF_HELPER_FLAGS_3(gvec_trunc64_32, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_trunc64_16, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_trunc64_8, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_trunc32_16, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_trunc32_8, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_trunc16_8, TCG_CALL_NO_RWG, void, ptr, ptr, i32) + +DEF_HELPER_FLAGS_3(gvec_zext32_64, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_zext16_64, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_zext8_64, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_zext16_32, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_zext8_32, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_zext8_16, TCG_CALL_NO_RWG, void, ptr, ptr, i32) + +DEF_HELPER_FLAGS_3(gvec_sext32_64, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_sext16_64, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_sext8_64, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_sext16_32, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_sext8_32, TCG_CALL_NO_RWG, void, ptr, ptr, i32) +DEF_HELPER_FLAGS_3(gvec_sext8_16, TCG_CALL_NO_RWG, void, ptr, ptr, i32) diff --git a/include/tcg/tcg-op-gvec-common.h b/include/tcg/tcg-op-gvec-com= mon.h index 65553f5f97..39b0c2f64e 100644 --- a/include/tcg/tcg-op-gvec-common.h +++ b/include/tcg/tcg-op-gvec-common.h @@ -390,6 +390,24 @@ void tcg_gen_gvec_bitsel(unsigned vece, uint32_t dofs,= uint32_t aofs, uint32_t bofs, uint32_t cofs, uint32_t oprsz, uint32_t maxsz); =20 +/* + * Perform vector element truncation/extension operations + */ + +void tcg_gen_gvec_trunc(unsigned vecde, unsigned vecse, + uint32_t dofs, uint32_t aofs, + uint32_t doprsz, uint32_t aoprsz, + uint32_t maxsz); + +void tcg_gen_gvec_zext(unsigned vecde, unsigned vecse, + uint32_t dofs, uint32_t aofs, + uint32_t doprsz, uint32_t aoprsz, + uint32_t maxsz); + +void tcg_gen_gvec_sext(unsigned vecde, unsigned vecse, + uint32_t dofs, uint32_t aofs, + uint32_t doprsz, uint32_t aoprsz, + uint32_t maxsz); /* * 64-bit vector operations. Use these when the register has been allocat= ed * with tcg_global_mem_new_i64, and so we cannot also address it via point= er. diff --git a/tcg/tcg-op-gvec.c b/tcg/tcg-op-gvec.c index 97e4df221a..80649dc0d2 100644 --- a/tcg/tcg-op-gvec.c +++ b/tcg/tcg-op-gvec.c @@ -4008,3 +4008,81 @@ void tcg_gen_gvec_bitsel(unsigned vece, uint32_t dof= s, uint32_t aofs, =20 tcg_gen_gvec_4(dofs, aofs, bofs, cofs, oprsz, maxsz, &g); } + +void tcg_gen_gvec_trunc(unsigned vecde, unsigned vecse, + uint32_t dofs, uint32_t aofs, + uint32_t doprsz, uint32_t aoprsz, + uint32_t maxsz) +{ + gen_helper_gvec_2 * const fns[4][4] =3D { + [MO_64] =3D { + [MO_32] =3D gen_helper_gvec_trunc64_32, + [MO_16] =3D gen_helper_gvec_trunc64_16, + [MO_8] =3D gen_helper_gvec_trunc64_8, + }, + [MO_32] =3D { + [MO_16] =3D gen_helper_gvec_trunc32_16, + [MO_8] =3D gen_helper_gvec_trunc32_8, + }, + [MO_16] =3D { + [MO_8] =3D gen_helper_gvec_trunc16_8, + }, + }; + + gen_helper_gvec_2 *fn =3D fns[vecse][vecde]; + tcg_debug_assert(fn !=3D 0 && vecse > vecde); + + tcg_gen_gvec_2_ool(dofs, aofs, doprsz, maxsz, 0, fn); +} + +void tcg_gen_gvec_zext(unsigned vecde, unsigned vecse, + uint32_t dofs, uint32_t aofs, + uint32_t doprsz, uint32_t aoprsz, + uint32_t maxsz) +{ + gen_helper_gvec_2 * const fns[4][4] =3D { + [MO_8] =3D { + [MO_16] =3D gen_helper_gvec_zext8_16, + [MO_32] =3D gen_helper_gvec_zext8_32, + [MO_64] =3D gen_helper_gvec_zext8_64, + }, + [MO_16] =3D { + [MO_32] =3D gen_helper_gvec_zext16_32, + [MO_64] =3D gen_helper_gvec_zext16_64, + }, + [MO_32] =3D { + [MO_64] =3D gen_helper_gvec_zext32_64, + }, + }; + + gen_helper_gvec_2 *fn =3D fns[vecse][vecde]; + tcg_debug_assert(fn !=3D 0 && vecse < vecde); + + tcg_gen_gvec_2_ool(dofs, aofs, doprsz, maxsz, 0, fn); +} + +void tcg_gen_gvec_sext(unsigned vecde, unsigned vecse, + uint32_t dofs, uint32_t aofs, + uint32_t doprsz, uint32_t aoprsz, + uint32_t maxsz) +{ + gen_helper_gvec_2 * const fns[4][4] =3D { + [MO_8] =3D { + [MO_16] =3D gen_helper_gvec_sext8_16, + [MO_32] =3D gen_helper_gvec_sext8_32, + [MO_64] =3D gen_helper_gvec_sext8_64, + }, + [MO_16] =3D { + [MO_32] =3D gen_helper_gvec_sext16_32, + [MO_64] =3D gen_helper_gvec_sext16_64, + }, + [MO_32] =3D { + [MO_64] =3D gen_helper_gvec_sext32_64, + }, + }; + + gen_helper_gvec_2 *fn =3D fns[vecse][vecde]; + tcg_debug_assert(fn !=3D 0 && vecse < vecde); + + tcg_gen_gvec_2_ool(dofs, aofs, doprsz, maxsz, 0, fn); +} --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153928; cv=none; d=zohomail.com; s=zohoarc; b=nPO2MZ5ioMrGWCsLCgHzypikyAReVo4kY7F+anJSUuXfuIQo6jzQ/IHf4HBKJ34iHKVehl9Hsc9QW39mCdQSwaiIYOppL+XH1TTpiEm8frFh8Z+CAjyCLJaX2EVrlmqI3j+7n3SLZH5HONpkV1JHppyhAgzZJw+G/V2XQVG3GMA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153928; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=8jA/Gzvjxlok8TB/HYwf/iXFNHMSXCKjyKHLZ6z242c=; b=eDZZHWLzAkp0kXxlRc826c50QV2Q8RZcyeDaQh3O6Dz+UyYlxjFZVBsXLcJb1uj/7hWFHI9GrbtgIYnRAJtb3za9eVlnBF9dm5QUW+jBX7wZ2uaPnHb5VGMQGzJM7e+JCDazIimYlYrukvQed8n/BJVCuq/+oqAY9EhCSTehe1c= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153928808222.7284267743304; Wed, 20 Nov 2024 17:52:08 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHh-0001WZ-An; Wed, 20 Nov 2024 20:47:05 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHg-0001W1-9J for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:04 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHe-0004Wa-HL for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=8jA/Gzvjxlok8TB/HYwf/iXFNHMSXCKjyKHLZ6z242c=; b=ADrb7mSEOSMIKwW ynHHvZl4lpTy3OtLNngQ4RzIs09wmv40/g6bvEKkX5XsUvl0jWCXYEo8YKNTX+1K55bn78uG6j52U +BlpzhX7HXBt979imJwuR4pplJg5AzkacymN8PF/k3DVn7vmfkvAkaMol+ij0ycCbhta65QHMpS33 mA=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 04/43] tcg: Add gvec functions for creating consant vectors Date: Thu, 21 Nov 2024 02:49:08 +0100 Message-ID: <20241121014947.18666-5-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153930914116600 Content-Type: text/plain; charset="utf-8" This commit adds a gvec function for copying data from constant array given in C to a gvec intptr_t. For each element, a host store of each constant is performed, this is not ideal and will inflate TBs for large vectors. Moreover, data will be copied during each run of the generated code impacting performance. A more suitable solution might store constant vectors separately, this can be handled either on the QEMU or helper-to-tcg side. Signed-off-by: Anton Johansson --- include/tcg/tcg-op-gvec-common.h | 2 ++ tcg/tcg-op-gvec.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/include/tcg/tcg-op-gvec-common.h b/include/tcg/tcg-op-gvec-com= mon.h index 39b0c2f64e..409a56c633 100644 --- a/include/tcg/tcg-op-gvec-common.h +++ b/include/tcg/tcg-op-gvec-common.h @@ -331,6 +331,8 @@ void tcg_gen_gvec_dup_mem(unsigned vece, uint32_t dofs,= uint32_t aofs, uint32_t s, uint32_t m); void tcg_gen_gvec_dup_imm(unsigned vece, uint32_t dofs, uint32_t s, uint32_t m, uint64_t imm); +void tcg_gen_gvec_constant(unsigned vece, TCGv_env env, uint32_t dofs, + void *arr, uint32_t maxsz); void tcg_gen_gvec_dup_i32(unsigned vece, uint32_t dofs, uint32_t s, uint32_t m, TCGv_i32); void tcg_gen_gvec_dup_i64(unsigned vece, uint32_t dofs, uint32_t s, diff --git a/tcg/tcg-op-gvec.c b/tcg/tcg-op-gvec.c index 80649dc0d2..71b6875129 100644 --- a/tcg/tcg-op-gvec.c +++ b/tcg/tcg-op-gvec.c @@ -1835,6 +1835,36 @@ void tcg_gen_gvec_dup_imm(unsigned vece, uint32_t do= fs, uint32_t oprsz, do_dup(vece, dofs, oprsz, maxsz, NULL, NULL, x); } =20 + +void tcg_gen_gvec_constant(unsigned vece, TCGv_env env, uint32_t dofs, + void *arr, uint32_t maxsz) +{ + uint32_t elsz =3D memop_size(vece); + for (uint32_t i =3D 0; i < maxsz/elsz; ++i) + { + uint32_t off =3D i*elsz; + uint8_t *elptr =3D (uint8_t *)arr + off; + switch (vece) { + case MO_8: + tcg_gen_st8_i32(tcg_constant_i32(*elptr), + env, dofs + off); + break; + case MO_16: + tcg_gen_st16_i32(tcg_constant_i32(*(uint16_t *) elptr), + env, dofs + off); + break; + case MO_32: + tcg_gen_st_i32(tcg_constant_i32(*(uint32_t *) elptr), + env, dofs + off); + break; + case MO_64: + tcg_gen_st_i64(tcg_constant_i64(*(uint64_t *) elptr), + env, dofs + off); + break; + } + } +} + void tcg_gen_gvec_not(unsigned vece, uint32_t dofs, uint32_t aofs, uint32_t oprsz, uint32_t maxsz) { --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153763; cv=none; d=zohomail.com; s=zohoarc; b=adu1OLSK6wXcZvlSvvg0Ku2Z+qPDZdTZTAspYtqZ7fW8zcqNjmnU1NnxgebxltF5kQl99JSZ2NMb9hN27jVjmmsXANI40zeS9C5DMcFAbNxlhwUVJavd0AFjQQqdi72yvEXLSqFMa4Xof/nyUraLLmEAM3w9lstvlxanfOd8ypc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153763; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=6XkMx9+DoHnFbLoWD6EoNo07s+PU1JscoSTTclMp8dU=; b=TeMFKZWZ5fUfUR89Y0A8R/I4XW6uMWgPEVVJYMi44f2iQEs4x8ehlPuKw7Px3J2YLjevIhh1U7WrCQz3uYFteiFHA5k+dhpECuGSQalxFSuSL9moRWSkgZXdbLDQ8LMZF7YjZM2jnmcDaD0XBMozuT2Q4MRWOySeHJd9GX04cpo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153763320648.5841869052036; Wed, 20 Nov 2024 17:49:23 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHk-0001Z6-4q; Wed, 20 Nov 2024 20:47:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHg-0001WK-Hk for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:04 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHf-0004Wh-3t for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:04 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=6XkMx9+DoHnFbLoWD6EoNo07s+PU1JscoSTTclMp8dU=; b=nLAB8cvvpufIY0H hMM3lQjbM/DZ5UBaRHI4u8hsk6kW+CbzeDg897UWlu25YJ4frprfiusYojTfY2iK38N2Njepa42Ae wjBtpRH1QYzf4mLAivD0wuEOJCJVdPHrPgdBffB2YZB23eGAcRvp71oOkhui1aJ68bTtlQ7D2JrUq Wk=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 05/43] tcg: Add helper function dispatcher and hook tcg_gen_callN Date: Thu, 21 Nov 2024 02:49:09 +0100 Message-ID: <20241121014947.18666-6-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153764279116600 Content-Type: text/plain; charset="utf-8" Adds a function pointer to the TCGContext which may be set by targets via the TARGET_HELPER_DISPATCHER macro. The dispatcher is function (void *func, TCGTemp *ret, int nargs, TCGTemp **args) -> bool which allows targets to hook the generation of helper calls in TCG and take over translation. Specifically, this will be used by helper-to-tcg to replace helper function translation, without having to modify frontends. Signed-off-by: Anton Johansson --- accel/tcg/translate-all.c | 4 ++++ include/tcg/tcg.h | 4 ++++ tcg/tcg.c | 5 +++++ 3 files changed, 13 insertions(+) diff --git a/accel/tcg/translate-all.c b/accel/tcg/translate-all.c index fdf6d8ac19..814aae93ae 100644 --- a/accel/tcg/translate-all.c +++ b/accel/tcg/translate-all.c @@ -352,6 +352,10 @@ TranslationBlock *tb_gen_code(CPUState *cpu, tcg_ctx->guest_mo =3D TCG_MO_ALL; #endif =20 +#if defined(CONFIG_HELPER_TO_TCG) && defined(TARGET_HELPER_DISPATCHER) + tcg_ctx->helper_dispatcher =3D TARGET_HELPER_DISPATCHER; +#endif + restart_translate: trace_translate_block(tb, pc, tb->tc.ptr); =20 diff --git a/include/tcg/tcg.h b/include/tcg/tcg.h index a77ed12b9d..d3e820568f 100644 --- a/include/tcg/tcg.h +++ b/include/tcg/tcg.h @@ -549,6 +549,10 @@ struct TCGContext { =20 /* Exit to translator on overflow. */ sigjmp_buf jmp_trans; + + + bool (*helper_dispatcher)(void *func, TCGTemp *ret_temp, + int nargs, TCGTemp **args); }; =20 static inline bool temp_readonly(TCGTemp *ts) diff --git a/tcg/tcg.c b/tcg/tcg.c index 0babae1b88..5f03bef688 100644 --- a/tcg/tcg.c +++ b/tcg/tcg.c @@ -2252,6 +2252,11 @@ static void tcg_gen_callN(void *func, TCGHelperInfo = *info, } =20 total_args =3D info->nr_out + info->nr_in + 2; + if (unlikely(tcg_ctx->helper_dispatcher) && + tcg_ctx->helper_dispatcher(info->func, ret, total_args, args)) { + return; + } + op =3D tcg_op_alloc(INDEX_op_call, total_args); =20 #ifdef CONFIG_PLUGIN --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153787; cv=none; d=zohomail.com; s=zohoarc; b=b59tKa619m5UyP9jzDXpjm8wgacZBTWtB77onyzj/yGVQAxf5ZFTKBNaIuwF+WFkfbsQjcENN+YYa+nz0rXPPRWohppoSp2Y9rN9jBiVNnWZ8QAH/a1b2hpRyu3wuMUQx8v5ZmcbcVY5pqj+oQvJDx99NimngTTkRdGm34DfR8E= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153787; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=PAxZ/jvpY1aZ/6uKKvW0Uz9c2GSX+wr6XnbXY09Ah1E=; b=gmp/UrCdzA49hIBBVgpY3lbHbAjcYKVKmxNsscSa3uHcYpXT9lSuzO2p1g/2VQr3CrhxjCn3AcTlEZW6vwdvq1auTYDl154y10ldkEPZ3QYM7P5B8sCXnd+vNVM//L4KC3XfOsRtGmX/684CpVyDbGAlcqNKRO2zp+5I0XW7fOY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17321537879195.93519031012147; Wed, 20 Nov 2024 17:49:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHk-0001Z8-8s; Wed, 20 Nov 2024 20:47:08 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHh-0001XK-PV for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:05 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHf-0004Wn-Ju for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=PAxZ/jvpY1aZ/6uKKvW0Uz9c2GSX+wr6XnbXY09Ah1E=; b=t0n1GVwIYZpDVln e9FWxl8VQh0WHw9/DCc3Y9r1tWVfwh59tVKMzhzAsBIdj0aUOg4RmgF4rmXjigI+X8qa+cehtBH+b 0ZS+yzC5UfSQHmzNUMr7VoBqckYEZYIXfwlDXN6zKPOo7/l91vVnxEAev8y1tjCPCeK8OG9STLt2P DY=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 06/43] tcg: Introduce tcg-global-mappings Date: Thu, 21 Nov 2024 02:49:10 +0100 Message-ID: <20241121014947.18666-7-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153788463116600 Content-Type: text/plain; charset="utf-8" Adds a cpu_mapping struct to describe, in a declarative fashion, the mapping between fields in a struct, and a corresponding TCG global. As such, tcg_global_mem_new() can be automatically called given an array of cpu_mappings. This change is not limited to helper-to-tcg, but will be required in future commits to map between offsets into CPUArchState and TCGv globals in a target-agnostic way. Signed-off-by: Anton Johansson --- include/tcg/tcg-global-mappings.h | 111 ++++++++++++++++++++++++++++++ tcg/meson.build | 1 + tcg/tcg-global-mappings.c | 61 ++++++++++++++++ 3 files changed, 173 insertions(+) create mode 100644 include/tcg/tcg-global-mappings.h create mode 100644 tcg/tcg-global-mappings.c diff --git a/include/tcg/tcg-global-mappings.h b/include/tcg/tcg-global-map= pings.h new file mode 100644 index 0000000000..736380fb20 --- /dev/null +++ b/include/tcg/tcg-global-mappings.h @@ -0,0 +1,111 @@ +/* + * Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef TCG_GLOBAL_MAP_H +#define TCG_GLOBAL_MAP_H + +#include "qemu/osdep.h" + +/** + * cpu_tcg_mapping: Declarative mapping of offsets into a struct to global + * TCGvs. Parseable by LLVM-based tools. + * @tcg_var_name: String name of the TCGv to use as destination of the map= ping. + * @tcg_var_base_address: Address of the above TCGv. + * @cpu_var_names: Array of printable names of TCGvs, used when calling + * tcg_global_mem_new from init_cpu_tcg_mappings. + * @cpu_var_base_offset: Base offset of field in the source struct. + * @cpu_var_size: Size of field in the source struct, if the field is an a= rray, + * this holds the size of the element type. + * @cpu_var_stride: Stride between array elements in the source struct. T= his + * can be greater than the element size when mapping a fi= eld + * in an array of structs. + * @number_of_elements: Number of elements of array in the source struct. + */ +typedef struct cpu_tcg_mapping { + const char *tcg_var_name; + void *tcg_var_base_address; + + const char *const *cpu_var_names; + size_t cpu_var_base_offset; + size_t cpu_var_size; + size_t cpu_var_stride; + + size_t number_of_elements; +} cpu_tcg_mapping; + +#define STRUCT_SIZEOF_FIELD(S, member) sizeof(((S *)0)->member) + +#define STRUCT_ARRAY_SIZE(S, array) = \ + (STRUCT_SIZEOF_FIELD(S, array) / STRUCT_SIZEOF_FIELD(S, array[0])) + +/* + * Following are a few macros that aid in constructing + * `cpu_tcg_mapping`s for a few common cases. + */ + +/* Map between single CPU register and to TCG global */ +#define CPU_TCG_MAP(struct_type, tcg_var, cpu_var, name_str) = \ + (cpu_tcg_mapping) = \ + { = \ + .tcg_var_name =3D stringify(tcg_var), .tcg_var_base_address =3D &t= cg_var, \ + .cpu_var_names =3D (const char *[]){name_str}, = \ + .cpu_var_base_offset =3D offsetof(struct_type, cpu_var), = \ + .cpu_var_size =3D STRUCT_SIZEOF_FIELD(struct_type, cpu_var), = \ + .cpu_var_stride =3D 0, .number_of_elements =3D 1, = \ + } + +/* Map between array of CPU registers and array of TCG globals. */ +#define CPU_TCG_MAP_ARRAY(struct_type, tcg_var, cpu_var, names) = \ + (cpu_tcg_mapping) = \ + { = \ + .tcg_var_name =3D #tcg_var, .tcg_var_base_address =3D tcg_var, = \ + .cpu_var_names =3D names, = \ + .cpu_var_base_offset =3D offsetof(struct_type, cpu_var), = \ + .cpu_var_size =3D STRUCT_SIZEOF_FIELD(struct_type, cpu_var[0]), = \ + .cpu_var_stride =3D STRUCT_SIZEOF_FIELD(struct_type, cpu_var[0]), = \ + .number_of_elements =3D STRUCT_ARRAY_SIZE(struct_type, cpu_var), = \ + } + +/* + * Map between single member in an array of structs to an array + * of TCG globals, e.g. maps + * + * cpu_state.array_of_structs[i].member + * + * to + * + * tcg_global_member[i] + */ +#define CPU_TCG_MAP_ARRAY_OF_STRUCTS(struct_type, tcg_var, cpu_struct, = \ + cpu_var, names) = \ + (cpu_tcg_mapping) = \ + { = \ + .tcg_var_name =3D #tcg_var, .tcg_var_base_address =3D tcg_var, = \ + .cpu_var_names =3D names, = \ + .cpu_var_base_offset =3D offsetof(struct_type, cpu_struct[0].cpu_v= ar), \ + .cpu_var_size =3D = \ + STRUCT_SIZEOF_FIELD(struct_type, cpu_struct[0].cpu_var), = \ + .cpu_var_stride =3D STRUCT_SIZEOF_FIELD(struct_type, cpu_struct[0]= ), \ + .number_of_elements =3D STRUCT_ARRAY_SIZE(struct_type, cpu_struct)= , \ + } + +extern cpu_tcg_mapping tcg_global_mappings[]; +extern size_t tcg_global_mapping_count; + +void init_cpu_tcg_mappings(cpu_tcg_mapping *mappings, size_t size); + +#endif /* TCG_GLOBAL_MAP_H */ diff --git a/tcg/meson.build b/tcg/meson.build index 69ebb4908a..a0d6b09d85 100644 --- a/tcg/meson.build +++ b/tcg/meson.build @@ -13,6 +13,7 @@ tcg_ss.add(files( 'tcg-op-ldst.c', 'tcg-op-gvec.c', 'tcg-op-vec.c', + 'tcg-global-mappings.c', )) =20 if get_option('tcg_interpreter') diff --git a/tcg/tcg-global-mappings.c b/tcg/tcg-global-mappings.c new file mode 100644 index 0000000000..cc1f07fae4 --- /dev/null +++ b/tcg/tcg-global-mappings.c @@ -0,0 +1,61 @@ +/* + * Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#include "tcg/tcg-global-mappings.h" +#include "tcg/tcg-op-common.h" +#include "tcg/tcg.h" + +void init_cpu_tcg_mappings(cpu_tcg_mapping *mappings, size_t size) +{ + uintptr_t tcg_addr; + size_t cpu_offset; + const char *name; + cpu_tcg_mapping m; + + /* + * Paranoid assertion, this should always hold since + * they're typedef'd to pointers. But you never know! + */ + g_assert(sizeof(TCGv_i32) =3D=3D sizeof(TCGv_i64)); + + /* + * Loop over entries in tcg_global_mappings and + * create the `mapped to` TCGv's. + */ + for (int i =3D 0; i < size; ++i) { + m =3D mappings[i]; + + for (int j =3D 0; j < m.number_of_elements; ++j) { + /* + * Here we are using the fact that + * sizeof(TCGv_i32) =3D=3D sizeof(TCGv_i64) =3D=3D sizeof(TCGv) + */ + assert(sizeof(TCGv_i32) =3D=3D sizeof(TCGv_i64)); + tcg_addr =3D (uintptr_t)m.tcg_var_base_address + j * sizeof(TC= Gv_i32); + cpu_offset =3D m.cpu_var_base_offset + j * m.cpu_var_stride; + name =3D m.cpu_var_names[j]; + + if (m.cpu_var_size < 8) { + *(TCGv_i32 *)tcg_addr =3D + tcg_global_mem_new_i32(tcg_env, cpu_offset, name); + } else { + *(TCGv_i64 *)tcg_addr =3D + tcg_global_mem_new_i64(tcg_env, cpu_offset, name); + } + } + } +} --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153689; cv=none; d=zohomail.com; s=zohoarc; b=mpk9ELSybX+c0p+9k1hS2WY2uq1zDB7AFCoo5LPspXPjU4J3/WbHzm8VfPjbO4SYsdrJcje8X64693hwR5s4sXtEL7KjVH/K09UxQAVfutnE4KN2QtKyHli8XwPtWoRUGTjqaROs9TpN2io1XCuNT/jS6AHEXJb/0W89gxZHFN4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153689; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=23UMfXZbF/6Igxr41KQ6yE+eWu+hARLGdgl4G57ZkEA=; b=KQkJX/zlp+EvVSbSUV8MWmPrW16/QX7yv4EFDtZdBtHrrWcoSpEEcZHX4NHS6HQCN40s/DfaYjCcPK0KF3iCNHyQW2JZ3jkpb14GarBTFn5UnfV+aNde2VcTLfgn3y7GrsTTF+3tgScKATYihzgn/TAc9W3X9yA0R7VRvL6yPk4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 173215368883810.808384730212993; Wed, 20 Nov 2024 17:48:08 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHj-0001YT-HS; Wed, 20 Nov 2024 20:47:07 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHh-0001Wj-DX for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:05 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHg-0004Wt-4z for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=23UMfXZbF/6Igxr41KQ6yE+eWu+hARLGdgl4G57ZkEA=; b=dn5Vc9z7EagOZg7 jpSg5S7ifsdtyL/qDAmIbcgFfaqSJeGw9st6ai6BWao83NaolRFO7p9aZMbWm7nEQx2/KHNc5RwnZ OE92j+cX7+i9Xsn4mXBVioeZNwuxZLct7SVw7hoDQDwaUGn3NoqSPF+crDHorbvY65IxYFDp/V+bg rY=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 07/43] tcg: Increase maximum TB size and maximum temporaries Date: Thu, 21 Nov 2024 02:49:11 +0100 Message-ID: <20241121014947.18666-8-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153690457116600 Content-Type: text/plain; charset="utf-8" Doubles amount of space allocated for translation blocks. This is needed, particularly for Hexagon, where a single instruction packet may consist of up to four vector instructions. If each vector instruction then gets expanded into gvec operations that utilize a small host vector size the TB blows up quite quickly. Signed-off-by: Anton Johansson --- include/tcg/tcg.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/tcg/tcg.h b/include/tcg/tcg.h index d3e820568f..bd8cb9ff50 100644 --- a/include/tcg/tcg.h +++ b/include/tcg/tcg.h @@ -39,7 +39,7 @@ /* XXX: make safe guess about sizes */ #define MAX_OP_PER_INSTR 266 =20 -#define CPU_TEMP_BUF_NLONGS 128 +#define CPU_TEMP_BUF_NLONGS 256 #define TCG_STATIC_FRAME_SIZE (CPU_TEMP_BUF_NLONGS * sizeof(long)) =20 #if TCG_TARGET_REG_BITS =3D=3D 32 @@ -231,7 +231,7 @@ typedef struct TCGPool { =20 #define TCG_POOL_CHUNK_SIZE 32768 =20 -#define TCG_MAX_TEMPS 512 +#define TCG_MAX_TEMPS 1024 #define TCG_MAX_INSNS 512 =20 /* when the size of the arguments of a called function is smaller than --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153698; cv=none; d=zohomail.com; s=zohoarc; b=bLrZIWdcr4BS4hl6erKjegv6zowNuPRSfFSSguHhe7QEjLaWagqWlST2/800OGykcM2xF4p2zEWhn26O+JYMcFQ3Cm4f7wTQyUzipuJY/lmtk9cMZ+8wUf0KDbXJyHj/wruOMdBKPDqFHqwTposwnFAScYkkVG1AYk3BH9ZrSWI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153698; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=gzkPiBJ/YsivON7QkdxAWJG2Cn2Aehqf0eG3z2ULstk=; b=cqUtg2aC4x4hQNMh5t8e60pT7pdit0OpPAdi3H6BScNdeQJbbUD6SiEUgElnEGstqyGFU7SbFcHHCW7bWJbp2q/xXjdI3eFSUKyx7axI7x4B58Yhxgu6sxfUf1VcGd981CfGwtctWZjPoBMHsppGSCW3+ggQXAU7NOGQbE48e+4= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153698356122.53263055246316; Wed, 20 Nov 2024 17:48:18 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHl-0001ZQ-O1; Wed, 20 Nov 2024 20:47:09 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHi-0001XT-7a for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:06 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHg-0004Wv-NE for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:05 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=gzkPiBJ/YsivON7QkdxAWJG2Cn2Aehqf0eG3z2ULstk=; b=K7mJ3OoNP33LQz3 wC9DZ+2ldcUjU/kBe3x0YpUzJ1CyZjKM86jjKgkfPZifnpbDeBLWz0UKrw6jiHXLOUcjqSgPhqbDz PcEqZsPKH8Ij5yr3YED4VD7PYeCi/aQcQtQMDFcf6iyNntMfX7ZAXuRV1uPLF2xuoDz095DmVtcPl BE=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 08/43] include/helper-to-tcg: Introduce annotate.h Date: Thu, 21 Nov 2024 02:49:12 +0100 Message-ID: <20241121014947.18666-9-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153700049116600 Content-Type: text/plain; charset="utf-8" Wrap __attribute__((annotate(str))) in a macro for convenient function annotations. Will be used in future commits to tag functions for translation by helper-to-tcg, and to specify which helper function arguments correspond to immediate or vector values. Signed-off-by: Anton Johansson --- include/helper-to-tcg/annotate.h | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 include/helper-to-tcg/annotate.h diff --git a/include/helper-to-tcg/annotate.h b/include/helper-to-tcg/annot= ate.h new file mode 100644 index 0000000000..80ecf23217 --- /dev/null +++ b/include/helper-to-tcg/annotate.h @@ -0,0 +1,28 @@ +/* + * Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef ANNOTATE_H +#define ANNOTATE_H + +/* HELPER_TO_TCG can be defined when generating LLVM IR. */ +#ifdef HELPER_TO_TCG +#define LLVM_ANNOTATE(str) __attribute__((annotate (str))) +#else +#define LLVM_ANNOTATE(str) /* str */ +#endif + +#endif /* ANNOTATE_H */ --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153956; cv=none; d=zohomail.com; s=zohoarc; b=GKGPhjwRUBQyCsCTR5Xqjw6ej7GwXzmE8j2JXOKKa1CALYvuoRnO1Yzfi8T2cm+YNlhGguhkpEYMWKK+dcGadP5IWHpCSajghYJUH+DAyXzpxvbVFQS16sr21w4mvf/tA4YR8zNZhO3BRdDHxGtxowNqQxPOdIoh6yNrJRC7ij0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153956; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=IhruTh9AaaXdYdQ72lpoc2vrYxq8f49r2KLX3qmIYJQ=; b=QzYKUgdupnGceGqnVzjnjrF5fv/LxSfnkRVowPvvAjuA+TkukP/XoXx3xlqBo77mwFbYluCQyMGbLP8BRQ52QBK90ZcOyM/5YThB1QZibHdAMsqYKKMe65/1niYTG0X+qtba3Hf4Y6T8811QfGnc3zyonjIol+Dk7takhy0wQh0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153956636789.142174408456; Wed, 20 Nov 2024 17:52:36 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHn-0001ZS-Jk; Wed, 20 Nov 2024 20:47:11 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHj-0001YO-Cf for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:07 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHh-0004X4-Hz for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:07 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=IhruTh9AaaXdYdQ72lpoc2vrYxq8f49r2KLX3qmIYJQ=; b=WnWdMnp+F6RqqVt gTSr4vNQ4PKS693vPGgInq9YdBo1GtoEqAKup4qGC0LugIyszVAhe0vGjzdRe/OAwPZEP16uZxpze mL8h3BAXpFgRcSAvW7MJpJ8EWasOb5VgSupYD/ZQKkHx0grHMHQgq0FDqLl8mKzCZWf5rvUB1RqqY yI=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 09/43] helper-to-tcg: Introduce get-llvm-ir.py Date: Thu, 21 Nov 2024 02:49:13 +0100 Message-ID: <20241121014947.18666-10-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153957188116600 Content-Type: text/plain; charset="utf-8" Introduces a new python helper script to convert a set of QEMU .c files to LLVM IR .ll using clang. Compile flags are found by looking at compile_commands.json, and llvm-link is used to link together all LLVM modules into a single module. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/get-llvm-ir.py | 143 +++++++++++++++++++++++ 1 file changed, 143 insertions(+) create mode 100755 subprojects/helper-to-tcg/get-llvm-ir.py diff --git a/subprojects/helper-to-tcg/get-llvm-ir.py b/subprojects/helper-= to-tcg/get-llvm-ir.py new file mode 100755 index 0000000000..9ee5d0e136 --- /dev/null +++ b/subprojects/helper-to-tcg/get-llvm-ir.py @@ -0,0 +1,143 @@ +#!/usr/bin/env python3 + +## +## Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see . +## + +import argparse +import json +import os +import shlex +import sys +import subprocess + + +def log(msg): + print(msg, file=3Dsys.stderr) + + +def run_command(command): + proc =3D subprocess.Popen(command, stdout=3Dsubprocess.PIPE, stderr=3D= subprocess.STDOUT) + out =3D proc.communicate() + if proc.wait() !=3D 0: + log(f"Command: {' '.join(command)} exited with {proc.returncode}\n= ") + log(f"output:\n{out}\n") + + +def find_compile_commands(compile_commands_path, clang_path, input_path, t= arget): + with open(compile_commands_path, "r") as f: + compile_commands =3D json.load(f) + for compile_command in compile_commands: + path =3D compile_command["file"] + if os.path.basename(path) !=3D os.path.basename(input_path): + continue + + os.chdir(compile_command["directory"]) + command =3D compile_command["command"] + + # If building multiple targets there's a chance + # input files share the same path and name. + # This could cause us to find the wrong compile + # command, we use the target path to distinguish + # between these. + if not target in command: + continue + + argv =3D shlex.split(command) + argv[0] =3D clang_path + + return argv + + raise ValueError(f"Unable to find compile command for {input_path}") + + +def generate_llvm_ir( + compile_commands_path, clang_path, output_path, input_path, target +): + command =3D find_compile_commands( + compile_commands_path, clang_path, input_path, target + ) + + flags_to_remove =3D { + "-ftrivial-auto-var-init=3Dzero", + "-fzero-call-used-regs=3Dused-gpr", + "-Wimplicit-fallthrough=3D2", + "-Wold-style-declaration", + "-Wno-psabi", + "-Wshadow=3Dlocal", + } + + # Remove + # - output of makefile rules (-MQ,-MF target); + # - output of object files (-o target); + # - excessive zero-initialization of block-scope variables + # (-ftrivial-auto-var-init=3Dzero); + # - and any optimization flags (-O). + for i, arg in reversed(list(enumerate(command))): + if arg in {"-MQ", "-o", "-MF"}: + del command[i : i + 2] + elif arg.startswith("-O") or arg in flags_to_remove: + del command[i] + + # Define a HELPER_TO_TCG macro for translation units wanting to + # conditionally include or exclude code during translation to TCG. + # Disable optimization (-O0) and make sure clang doesn't emit optnone + # attributes (-disable-O0-optnone) which inhibit further optimization. + # Optimization will be performed at a later stage in the helper-to-tcg + # pipeline. + command +=3D [ + "-S", + "-emit-llvm", + "-DHELPER_TO_TCG", + "-O0", + "-Xclang", + "-disable-O0-optnone", + ] + if output_path: + command +=3D ["-o", output_path] + + run_command(command) + + +def main(): + parser =3D argparse.ArgumentParser( + description=3D"Produce the LLVM IR of a given .c file." + ) + parser.add_argument( + "--compile-commands", required=3DTrue, help=3D"Path to compile_com= mands.json" + ) + parser.add_argument("--clang", default=3D"clang", help=3D"Path to clan= g.") + parser.add_argument("--llvm-link", default=3D"llvm-link", help=3D"Path= to llvm-link.") + parser.add_argument("-o", "--output", required=3DTrue, help=3D"Output = .ll file path") + parser.add_argument( + "--target-path", help=3D"Path to QEMU target dir. (e.q. target/i38= 6)" + ) + parser.add_argument("inputs", nargs=3D"+", help=3D".c file inputs") + args =3D parser.parse_args() + + outputs =3D [] + for input in args.inputs: + output =3D os.path.basename(input) + ".ll" + generate_llvm_ir( + args.compile_commands, args.clang, output, input, args.target_= path + ) + outputs.append(output) + + run_command([args.llvm_link] + outputs + ["-S", "-o", args.output]) + + +if __name__ =3D=3D "__main__": + sys.exit(main()) --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153688; cv=none; d=zohomail.com; s=zohoarc; b=jriT9K+sV3xBrPGg8CetFKLvLHqajfXn5zSLRtr+1oLjt6MA3SaOctmuZZ7SVsi05c9NPLN0fMcE538iJhzQhW6QVv2BoL/eQA2qMTNhZAb3VZz9YISYpkUe1VvxS6z4anZfsEp4+pNCavpJHoqzQtgofPirCaccw5zjWgKwLdw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153688; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=5YVDpLsCIhM0Je0KSsPGzZGsGqGUsmMzfsGlF+C2ZE0=; b=hsgBsLj4Pufa+8SUO1AT69LRZ7oSju+jTV1K1XY6E44xNVvHpNblbTBh9gE22b8YOANS/IXCzk6TjuIglIp/y0b/JbsSCCh0NSWDIvEEOUodPlk0OaUXHTLkxlcOavdDIdlTsUiHoqYbgL3Gylja1eoWPkatwL+3tWBxSTEVwTU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153688778950.4536574907193; Wed, 20 Nov 2024 17:48:08 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHp-0001aJ-Iq; Wed, 20 Nov 2024 20:47:13 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHn-0001ZT-IU for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:11 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHl-0004X7-Tm for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:11 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=5YVDpLsCIhM0Je0KSsPGzZGsGqGUsmMzfsGlF+C2ZE0=; b=rK/9p6PrysTn5bv g0i1MoWEYfzdeZDxhNyWJcWd+h0KUjtKXBJb9hNA2X38Sb4jF2FpufHNRdYkvcEnDt5IhGK0qKWmG 7VmyaRCZlY+nkLPTWefj/0E4stKWCUrE8hpXBZN+yQJYvlYNSnOrysrzCdcV9FbE1YdigBNRPI67f 1o=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 10/43] helper-to-tcg: Add meson.build Date: Thu, 21 Nov 2024 02:49:14 +0100 Message-ID: <20241121014947.18666-11-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153690555116600 Content-Type: text/plain; charset="utf-8" Sets up a barebones meson.build that handles: 1. Exposing command for converting .c files to LLVM IR by looking at compile_commads.json; 2. Finding LLVM and verifying the LLVM version manually by running llvm-config, needed for dealing with multiple LLVM versions in a sane way; 3. Building of helper-to-tcg. A meson option is added to specify the path to llvm-config. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/meson.build | 70 +++++++++++++++++++++ subprojects/helper-to-tcg/meson_options.txt | 2 + 2 files changed, 72 insertions(+) create mode 100644 subprojects/helper-to-tcg/meson.build create mode 100644 subprojects/helper-to-tcg/meson_options.txt diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build new file mode 100644 index 0000000000..af593ccdfe --- /dev/null +++ b/subprojects/helper-to-tcg/meson.build @@ -0,0 +1,70 @@ +## +## Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +## +## This program is free software; you can redistribute it and/or modify +## it under the terms of the GNU General Public License as published by +## the Free Software Foundation; either version 2 of the License, or +## (at your option) any later version. +## +## This program is distributed in the hope that it will be useful, +## but WITHOUT ANY WARRANTY; without even the implied warranty of +## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +## GNU General Public License for more details. +## +## You should have received a copy of the GNU General Public License +## along with this program; if not, see . +## + +project('helper-to-tcg', ['cpp'], + meson_version: '>=3D0.63.0', + version: '0.7', + default_options: ['cpp_std=3Dnone', 'optimization=3D2']) + +python =3D import('python').find_installation() + +# Find LLVM using llvm-config manually. Needed as meson struggles when mu= ltiple +# versions of LLVM are installed on the same system (always returns the mo= st +# recent). +llvm_config =3D get_option('llvm_config_path') +cpp_args =3D [run_command(llvm_config, '--cxxflags').stdout().strip().spli= t()] +bindir =3D run_command(llvm_config, '--bindir').stdout().strip() +ldflags =3D run_command(llvm_config, '--ldflags').stdout().strip().split() +libs =3D run_command(llvm_config, '--libs').stdout().strip().split() +syslibs =3D run_command(llvm_config, '--system-libs').stdout().strip().spl= it() +incdir =3D run_command(llvm_config, '--includedir').stdout().strip().split= () +version =3D run_command(llvm_config, '--version').stdout().strip() +version_major =3D version.split('.')[0].to_int() + +# Check LLVM version manually +if version_major < 10 or version_major > 14 + error('LLVM version', version, 'not supported.') +endif + +sources =3D [ +] + +clang =3D bindir / 'clang' +llvm_link =3D bindir / 'llvm-link' + +get_llvm_ir_cmd =3D [python, meson.current_source_dir() / 'get-llvm-ir.py', + '--compile-commands', 'compile_commands.json', + '--clang', clang, + '--llvm-link', llvm_link] + +# NOTE: Add -Wno-template-id-cdtor for GCC versions >=3D 14. This warning= is +# related to a change in the C++ standard in C++20, that also applies to C= ++14 +# for some reason. See defect report DR2237 and commit +# https://gcc.gnu.org/git/gitweb.cgi?p=3Dgcc.git;h=3D4b38d56dbac6742b038= 551a36ec80200313123a1 +# (temporary) +compiler_info =3D meson.get_compiler('cpp') +compiler =3D compiler_info.get_id() +compiler_version =3D compiler_info.version().split('-').get(0) +compiler_version_major =3D compiler_version.split('.').get(0) +if compiler =3D=3D 'gcc' and compiler_version_major.to_int() >=3D 14 + cpp_args +=3D ['-Wno-template-id-cdtor', '-Wno-missing-template-keywor= d'] +endif + +pipeline =3D executable('helper-to-tcg', sources, + include_directories: ['passes', './', 'include'] + [= incdir], + link_args: [ldflags] + [libs] + [syslibs], + cpp_args: cpp_args) diff --git a/subprojects/helper-to-tcg/meson_options.txt b/subprojects/help= er-to-tcg/meson_options.txt new file mode 100644 index 0000000000..8a4b28a585 --- /dev/null +++ b/subprojects/helper-to-tcg/meson_options.txt @@ -0,0 +1,2 @@ +option('llvm_config_path', type : 'string', value : 'llvm-config', + description: 'override default llvm-config used for finding LLVM') --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153925; cv=none; d=zohomail.com; s=zohoarc; b=iD5nPOtlr6yuwLgDBERPQv6eqvLnEc51vohfS1vgteVOcoSrVaEt+JlqDoZwmv74gxuGiSlPVJhjAUiHXzX8VX/ct2FjQ+wzNe0lgCk30ERkVRWgvELg/JZ37ZjXhLYdRQr7o6agBRYLnYd/Gq+KqEvmkGfi31ZlRfHNQH0PeDg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153925; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=Hg/o1H2sN/+u03XPSgfnuR4t0tZbOD4bmwcvMFlXc2g=; b=c1OqdTQO89HPHG7QxdI1LPj573ZJxPynagpZV9IDUB/liAv+UfF1hPlzJUsuJWWE7eRxRgsrVFSVyVZ7+zmeaeeR1O/7Eq+pIV+0YEOmp32FasD3s+D4RXdnZxfkdoSzanrvWsZBI+SdLwKzHnbZ8s344djzovbZI8iESONkIYo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 173215392583567.30055619195775; Wed, 20 Nov 2024 17:52:05 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHq-0001aa-4C; Wed, 20 Nov 2024 20:47:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHo-0001Zq-Nk for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:12 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHm-0004XD-FS for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=Hg/o1H2sN/+u03XPSgfnuR4t0tZbOD4bmwcvMFlXc2g=; b=O2pEpEP7TloW/Vc KHuOaMbiBsqhllQhBjX3xe1E8KRu9ZxDWvsLNsEz1lDtwkOriuE18s6Gp/tQXTz93nI576OcsA/5E yAaD8nVHBGwq+Q6FYNYFBF/2EHEboTBfPLLNpMrqWytrbXnwqSMYFrbS+Glt4eXVVVAGU9eI2aqgx og=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 11/43] helper-to-tcg: Introduce llvm-compat Date: Thu, 21 Nov 2024 02:49:15 +0100 Message-ID: <20241121014947.18666-12-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153927170116600 Content-Type: text/plain; charset="utf-8" Adds a translation unit with the sole purpose of handling inter-LLVM code changes. Instead of littering the code with #ifdefs, most of them will be limited to llvm-compat.[cpp|h] and a saner compat::*() function is exposed in its place. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/meson.build | 1 + .../helper-to-tcg/passes/llvm-compat.cpp | 162 ++++++++++++++++++ .../helper-to-tcg/passes/llvm-compat.h | 143 ++++++++++++++++ 3 files changed, 306 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/llvm-compat.cpp create mode 100644 subprojects/helper-to-tcg/passes/llvm-compat.h diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index af593ccdfe..7bb93ce005 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -41,6 +41,7 @@ if version_major < 10 or version_major > 14 endif =20 sources =3D [ + 'passes/llvm-compat.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/llvm-compat.cpp b/subprojects= /helper-to-tcg/passes/llvm-compat.cpp new file mode 100644 index 0000000000..c5d9d28078 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/llvm-compat.cpp @@ -0,0 +1,162 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include "llvm-compat.h" + +#if LLVM_VERSION_MAJOR > 10 +#include +#else +#include +#endif + +#include + +// Static variables required by LLVM +// +// Defining RegisterCodeGenFlags with static duration registers extra +// codegen commandline flags for specifying the target arch. +#if LLVM_VERSION_MAJOR > 10 +static llvm::codegen::RegisterCodeGenFlags CGF; +#endif +static llvm::ExitOnError ExitOnErr; + +namespace compat +{ + +using namespace llvm; + +#if LLVM_VERSION_MAJOR > 10 +llvm::TargetMachine *getTargetMachine(llvm::Triple &TheTriple) +{ + const TargetOptions Options{}; + std::string Error; + const Target *TheTarget =3D llvm::TargetRegistry::lookupTarget( + llvm::codegen::getMArch(), TheTriple, Error); + // Some modules don't specify a triple, and this is okay. + if (!TheTarget) { + return nullptr; + } + + return TheTarget->createTargetMachine( + TheTriple.getTriple(), llvm::codegen::getCPUStr(), + llvm::codegen::getFeaturesStr(), Options, + llvm::codegen::getExplicitRelocModel(), + llvm::codegen::getExplicitCodeModel(), llvm::CodeGenOpt::Aggressiv= e); +} +#else +llvm::TargetMachine *getTargetMachine(llvm::Triple &TheTriple) +{ + const TargetOptions Options{}; + std::string Error; + const Target *TheTarget =3D + llvm::TargetRegistry::lookupTarget(MArch, TheTriple, Error); + // Some modules don't specify a triple, and this is okay. + if (!TheTarget) { + return nullptr; + } + + return TheTarget->createTargetMachine( + TheTriple.getTriple(), getCPUStr(), getFeaturesStr(), Options, + getRelocModel(), getCodeModel(), llvm::CodeGenOpt::Aggressive); +} +#endif + +// +// LLVM 11 and below does not define the UnifyFunctionExitNodes pass +// for the new pass manager. Copy over the definition from LLVM and use it +// for 11 and below. +// +#if LLVM_VERSION_MAJOR <=3D 11 +static bool unifyReturnBlocks(Function &F) +{ + std::vector ReturningBlocks; + + for (BasicBlock &I : F) + if (isa(I.getTerminator())) + ReturningBlocks.push_back(&I); + + if (ReturningBlocks.size() <=3D 1) + return false; + + // Insert a new basic block into the function, add PHI nodes (if the + // function returns values), and convert all of the return instruction= s into + // unconditional branches. + BasicBlock *NewRetBlock =3D + BasicBlock::Create(F.getContext(), "UnifiedReturnBlock", &F); + + PHINode *PN =3D nullptr; + if (F.getReturnType()->isVoidTy()) { + ReturnInst::Create(F.getContext(), nullptr, NewRetBlock); + } else { + // If the function doesn't return void... add a PHI node to the bl= ock... + PN =3D PHINode::Create(F.getReturnType(), ReturningBlocks.size(), + "UnifiedRetVal"); + NewRetBlock->getInstList().push_back(PN); + ReturnInst::Create(F.getContext(), PN, NewRetBlock); + } + + // Loop over all of the blocks, replacing the return instruction with = an + // unconditional branch. + for (BasicBlock *BB : ReturningBlocks) { + // Add an incoming element to the PHI node for every return instru= ction + // that is merging into this new block... + if (PN) + PN->addIncoming(BB->getTerminator()->getOperand(0), BB); + + BB->getInstList().pop_back(); // Remove the return insn + BranchInst::Create(NewRetBlock, BB); + } + + return true; +} + +static bool unifyUnreachableBlocks(Function &F) +{ + std::vector UnreachableBlocks; + + for (BasicBlock &I : F) + if (isa(I.getTerminator())) + UnreachableBlocks.push_back(&I); + + if (UnreachableBlocks.size() <=3D 1) + return false; + + BasicBlock *UnreachableBlock =3D + BasicBlock::Create(F.getContext(), "UnifiedUnreachableBlock", &F); + new UnreachableInst(F.getContext(), UnreachableBlock); + + for (BasicBlock *BB : UnreachableBlocks) { + BB->getInstList().pop_back(); // Remove the unreachable inst. + BranchInst::Create(UnreachableBlock, BB); + } + + return true; +} + +llvm::PreservedAnalyses +UnifyFunctionExitNodesPass::run(llvm::Function &F, + llvm::FunctionAnalysisManager &AM) +{ + bool Changed =3D false; + Changed |=3D unifyUnreachableBlocks(F); + Changed |=3D unifyReturnBlocks(F); + return Changed ? PreservedAnalyses() : llvm::PreservedAnalyses::all(); +} + +#endif + +} // namespace compat diff --git a/subprojects/helper-to-tcg/passes/llvm-compat.h b/subprojects/h= elper-to-tcg/passes/llvm-compat.h new file mode 100644 index 0000000000..e983ad660e --- /dev/null +++ b/subprojects/helper-to-tcg/passes/llvm-compat.h @@ -0,0 +1,143 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +// +// The purpose of this file is to both collect and hide most api-specific +// changes of LLVM [10,14]. Hopefully making it easier to keep track of the +// changes necessary to support our targeted versions. +// +// Note some #ifdefs still remain throughout the codebase for larger codeb= locks +// that are specific enough such that pulling them here would be more cumb= ersome +// than it's worth. +// + +#include +#include + +#if LLVM_VERSION_MAJOR > 11 +#include +#endif + +#if LLVM_VERSION_MAJOR >=3D 14 +#include +#else +#include +#endif + +#include +#include +#include +#include +#include + +#include + +namespace compat +{ + +#if LLVM_VERSION_MAJOR =3D=3D 14 || LLVM_VERSION_MAJOR =3D=3D 13 +constexpr auto OpenFlags =3D llvm::sys::fs::OF_TextWithCRLF; +#else +constexpr auto OpenFlags =3D llvm::sys::fs::OF_Text; +#endif + +#if LLVM_VERSION_MAJOR =3D=3D 14 +using OptimizationLevel =3D llvm::OptimizationLevel; +#else +using OptimizationLevel =3D llvm::PassBuilder::OptimizationLevel; +#endif + +#if LLVM_VERSION_MAJOR > 11 +constexpr auto LTOPhase =3D llvm::ThinOrFullLTOPhase::None; +#else +constexpr auto LTOPhase =3D llvm::PassBuilder::ThinLTOPhase::None; +#endif + +inline llvm::PassBuilder createPassBuilder(llvm::TargetMachine *TM, + llvm::PipelineTuningOptions &PT= O) +{ +#if LLVM_VERSION_MAJOR =3D=3D 14 || LLVM_VERSION_MAJOR =3D=3D 13 + return llvm::PassBuilder(TM, PTO, llvm::None); +#elif LLVM_VERSION_MAJOR =3D=3D 12 + return llvm::PassBuilder(TM, nullptr, PTO); +#else + return llvm::PassBuilder(TM, PTO); +#endif +} + +// Wrapper to convert Function- to Module analysis manager +template +inline const typename T::Result * +getModuleAnalysisManagerProxyResult(llvm::FunctionAnalysisManager &FAM, + llvm::Function &F) +{ +#if LLVM_VERSION_MAJOR > 10 + auto &MAMProxy =3D FAM.getResult(F); + return MAMProxy.getCachedResult(*F.getParent()); +#else + auto &MAMProxy =3D + FAM.getResult(F).getMana= ger(); + return MAMProxy.getCachedResult(*F.getParent()); +#endif +} + +llvm::TargetMachine *getTargetMachine(llvm::Triple &TheTriple); + +// +// LLVM 11 and below does not define the UnifyFunctionExitNodes pass +// for the new pass manager. Copy over the definition and use it for +// 11 and below. +// +#if LLVM_VERSION_MAJOR > 11 +using llvm::UnifyFunctionExitNodesPass; +#else +class UnifyFunctionExitNodesPass + : public llvm::PassInfoMixin +{ + public: + llvm::PreservedAnalyses run(llvm::Function &F, + llvm::FunctionAnalysisManager &AM); +}; +#endif + +inline uint32_t getVectorElementCount(llvm::VectorType *VecTy) +{ + auto ElementCount =3D VecTy->getElementCount(); +#if LLVM_VERSION_MAJOR > 11 + return ElementCount.getFixedValue(); +#else + return ElementCount.Min; +#endif +} + +// +// PatternMatch +// + +#if LLVM_VERSION_MAJOR > 10 +#define compat_m_InsertElt llvm::PatternMatch::m_InsertElt +#define compat_m_Shuffle llvm::PatternMatch::m_Shuffle +#define compat_m_ZeroMask llvm::PatternMatch::m_ZeroMask +#else +#define compat_m_InsertElt llvm::PatternMatch::m_InsertElement +#define compat_m_Shuffle llvm::PatternMatch::m_ShuffleVector +#define compat_m_ZeroMask llvm::PatternMatch::m_Zero +#endif + +} // namespace compat --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153696; cv=none; d=zohomail.com; s=zohoarc; b=A9p3OWDTtB3dYsrFpVDJfn1upjsxBfN/Qgu2t+1Zyn0CEcATGH1/g3WQCVE1b8FkxzSN6XvIgSDvIHAM0T4dqCgbJwX6nsiHu3KNRoZEOQqnF3vwGAXGQO4SbmXOeB+bMt3WHTkUXWxBQe5OBnE8rMlLy8IUZ0bj6EGM3tHJvRw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153696; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=NXow3yI/n6VGiB50Xsb9COmLd+0cj1NMmA6+H2pmjgc=; b=k8wcN8aY2FMsVJ8gAq8/D+A+wkHuVabswAHOYIUTKrQ1K4s0NetnVOkpFpbginp+cCngAN9vxxgwMVR6QLznHhRvvTx46/l1cA3DmBRclxCSKzCf7kvqdKzEulu1y7+gyREjerg3hm0YY/mZm4ANEa4CU/+s4jsq0mh586Lgmuo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153696672838.7307554022175; Wed, 20 Nov 2024 17:48:16 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHq-0001az-TB; Wed, 20 Nov 2024 20:47:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHp-0001aR-NA for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:13 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHn-0004XK-1d for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=NXow3yI/n6VGiB50Xsb9COmLd+0cj1NMmA6+H2pmjgc=; b=WKYcobFPlzejlh4 I+kietp95+5ETSoDAeKdBuDIiOrQLmbbbGtOZefz0nnYjiVT0mKgpTUSk0NRUAn1jsOqOUlROjM3w 8eaGIJ7sAPauDE4BakwCTHap6xqSC1aQmcPS8ZSP/dZcZYmesO0tzlI9q1ulUjW60jQHHGKCAOydj k0=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 12/43] helper-to-tcg: Introduce custom LLVM pipeline Date: Thu, 21 Nov 2024 02:49:16 +0100 Message-ID: <20241121014947.18666-13-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153698408116600 Content-Type: text/plain; charset="utf-8" Adds a custom pipeline, similar to LLVM opt, with the goal of taking an input LLVM IR module to an equivalent output .c file implementing functions in TCG. Initial LLVM boilerplate is added until the creation of a ModulePassManager. A custom target derived from x64 is added, to ensure consistent behaviour across different hosts. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/CmdLineOptions.h | 23 +++ subprojects/helper-to-tcg/meson.build | 1 + .../helper-to-tcg/pipeline/Pipeline.cpp | 159 ++++++++++++++++++ 3 files changed, 183 insertions(+) create mode 100644 subprojects/helper-to-tcg/include/CmdLineOptions.h create mode 100644 subprojects/helper-to-tcg/pipeline/Pipeline.cpp diff --git a/subprojects/helper-to-tcg/include/CmdLineOptions.h b/subprojec= ts/helper-to-tcg/include/CmdLineOptions.h new file mode 100644 index 0000000000..5774ab07b1 --- /dev/null +++ b/subprojects/helper-to-tcg/include/CmdLineOptions.h @@ -0,0 +1,23 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include + +// Options for pipeline +extern llvm::cl::list InputFiles; diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 7bb93ce005..63c6ed17fb 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -41,6 +41,7 @@ if version_major < 10 or version_major > 14 endif =20 sources =3D [ + 'pipeline/Pipeline.cpp', 'passes/llvm-compat.cpp', ] =20 diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp new file mode 100644 index 0000000000..9c0e777893 --- /dev/null +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -0,0 +1,159 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "llvm-compat.h" + +using namespace llvm; + +cl::OptionCategory Cat("helper-to-tcg Options"); + +// Options for pipeline +cl::opt InputFile(cl::Positional, cl::desc("[input LLVM modul= e]"), + cl::cat(Cat)); + +// Define a TargetTransformInfo (TTI) subclass, this allows for overriding +// common per-llvm-target information expected by other LLVM passes, such +// as the width of the largest scalar/vector registers. Needed for consis= tent +// behaviour across different hosts. +class TcgTTI : public BasicTTIImplBase +{ + friend class BasicTTIImplBase; + + // We need to provide ST, TLI, getST(), getTLI() + const TargetSubtargetInfo *ST; + const TargetLoweringBase *TLI; + + const TargetSubtargetInfo *getST() const { return ST; } + const TargetLoweringBase *getTLI() const { return TLI; } + + public: + // Initialize ST and TLI from the target machine, e.g. if we're + // targeting x86 we'll get the Subtarget and TargetLowering to + // match that architechture. + TcgTTI(TargetMachine *TM, Function const &F) + : BasicTTIImplBase(TM, F.getParent()->getDataLayout()), + ST(TM->getSubtargetImpl(F)), TLI(ST->getTargetLowering()) + { + } + +#if LLVM_VERSION_MAJOR >=3D 13 + TypeSize getRegisterBitWidth(TargetTransformInfo::RegisterKind K) const + { + switch (K) { + case TargetTransformInfo::RGK_Scalar: + // We pretend we always support 64-bit registers + return TypeSize::getFixed(64); + case TargetTransformInfo::RGK_FixedWidthVector: + // We pretend we always support 2048-bit vector registers + return TypeSize::getFixed(2048); + case TargetTransformInfo::RGK_ScalableVector: + return TypeSize::getScalable(0); + default: + abort(); + } + } +#else + unsigned getRegisterBitWidth(bool Vector) const + { + if (Vector) { + return 2048; + } else { + return 64; + } + } +#endif +}; + +int main(int argc, char **argv) +{ + InitLLVM X(argc, argv); + cl::HideUnrelatedOptions(Cat); + + InitializeAllTargets(); + InitializeAllTargetMCs(); + PassRegistry &Registry =3D *PassRegistry::getPassRegistry(); + initializeCore(Registry); + initializeScalarOpts(Registry); + initializeVectorization(Registry); + initializeAnalysis(Registry); + initializeTransformUtils(Registry); + initializeInstCombine(Registry); + initializeTarget(Registry); + + cl::ParseCommandLineOptions(argc, argv); + + LLVMContext Context; + + SMDiagnostic Err; + std::unique_ptr M =3D parseIRFile(InputFile, Err, Context); + + // Create a new TargetMachine to represent a TCG target, + // we use x86_64 as a base and derive from that using a + // TargetTransformInfo to provide allowed scalar and vector + // register sizes. + Triple ModuleTriple("x86_64-pc-unknown"); + assert(ModuleTriple.getArch()); + TargetMachine *TM =3D compat::getTargetMachine(ModuleTriple); + + PipelineTuningOptions PTO; + PassBuilder PB =3D compat::createPassBuilder(TM, PTO); + LoopAnalysisManager LAM; + FunctionAnalysisManager FAM; + CGSCCAnalysisManager CGAM; + ModuleAnalysisManager MAM; + + // Register our TargetIrAnalysis pass using our own TTI + FAM.registerPass([&] { + return TargetIRAnalysis( + [&](const Function &F) { return TcgTTI(TM, F); }); + }); + FAM.registerPass([&] { return LoopAnalysis(); }); + LAM.registerPass([&] { return LoopAccessAnalysis(); }); + // We need to specifically add the aliasing pipeline for LLVM <=3D 13 + FAM.registerPass([&] { return PB.buildDefaultAAPipeline(); }); + + // Register other default LLVM Analyses + PB.registerFunctionAnalyses(FAM); + PB.registerModuleAnalyses(MAM); + PB.registerLoopAnalyses(LAM); + PB.registerCGSCCAnalyses(CGAM); + PB.crossRegisterProxies(LAM, FAM, CGAM, MAM); + + ModulePassManager MPM; + + return 0; +} --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153916; cv=none; d=zohomail.com; s=zohoarc; b=Vscl2H66qR/Q5DFOrv4Wy+hlKVfd+85wEPslkdUCiOcfY3MCE2escPl92EAG+5Fu7YR80xpYnrEpRAT3vp1tMzlmh3DiTTgZbgLeHOaRyWYDTEWMaB/Srs8onbJnbwNBjFVu1aH+zO0wk2J4Gpz1Is4JSyd/McLO/8FjlPPoTcM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153916; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=i2OqdZuO+kr2Tl9S5r8bbQR09WB2cR2wCDZ7uUecgdY=; b=mtL6BtzzII5MIflggb2Ovft/yxF9XX+h7cYWa8PupzxGAitfNX590ICf5XOlP8VnBG+TpKdAbuM1cjRlEKzwGGvQMVYthEIcOD8WdzZRzi/NmSa3S+EYU7d+jMvp1aZcpE0jV3TeqCU8mkaVe/PzXbLLFX1iL7dJfN7dvgj2Q2I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153916920940.3562215881038; Wed, 20 Nov 2024 17:51:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHq-0001ap-De; Wed, 20 Nov 2024 20:47:14 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHp-0001aI-Ah for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:13 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHn-0004XW-Qk for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:13 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=i2OqdZuO+kr2Tl9S5r8bbQR09WB2cR2wCDZ7uUecgdY=; b=hh1zIFXVopAyKTW aMAA1frZj1gI6RIYpQyLTpFJ7MOKhnhkWKEgITYHn/304thqjHTUCxtnoCT3z7NWy4g2gchxFUVB9 opc8FxVPQdrtaUOfisyq82C2i6HcEI/izvzJq/ZmS/6PZ9u+4y76lK7niV2b5r3PEWyT6bnXg7UAq hA=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 13/43] helper-to-tcg: Introduce Error.h Date: Thu, 21 Nov 2024 02:49:17 +0100 Message-ID: <20241121014947.18666-14-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153918909116600 Content-Type: text/plain; charset="utf-8" Simple function for creating Expected<> with nice error messages. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/include/Error.h | 40 +++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 subprojects/helper-to-tcg/include/Error.h diff --git a/subprojects/helper-to-tcg/include/Error.h b/subprojects/helper= -to-tcg/include/Error.h new file mode 100644 index 0000000000..10205e29a6 --- /dev/null +++ b/subprojects/helper-to-tcg/include/Error.h @@ -0,0 +1,40 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include +#include +#include + +inline llvm::Error mkError(const llvm::StringRef Msg) +{ + return llvm::createStringError(llvm::inconvertibleErrorCode(), Msg); +} + +// TODO: Usage of mkError and dbgs() for serializing Values is __really__ = slow, +// and should only occur for error reporting. Wrap these in a class with a +// ModuleSlotTracker. +inline llvm::Error mkError(const llvm::StringRef Msg, const llvm::Value *V) +{ + std::string Str; + llvm::raw_string_ostream Stream(Str); + Stream << Msg; + Stream << *V; + Stream.flush(); + return llvm::createStringError(llvm::inconvertibleErrorCode(), Str); +} --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153927; cv=none; d=zohomail.com; s=zohoarc; b=JRAXSK4f3SFnJF3UCZcSoDCrsR+6T7DixGN+7i8XPIbGFbZKLprzr9O5xKotL+Y4c4WaAmmCGimSHmrkyTSn79F6yzvt9Xsnq80HfSdCHOOZwog1PD/a95AgIzzO7wLv/EEYUVIw6qVoRCA4iuPD6ofLgSdqXOF+LL/XXGWw3dk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153927; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=79pr1drhMqVTakNZzCI6+VG2OY2chgT2RrjIC+e91vQ=; b=kWnGSZKQXjcileSQQzZNrKuyG2HTz8dcrKbT3Ztyu5piAjw/GrTZrLKozF2QouGsOYSdNkSJpBVHbLQIYjFS4lrIDb2IAvT5fxoZEU1JkWAKCBAMAomfISVoIIz4eyOXq5tNWTeH/7py1rWiepgmpk7XRgUopq9rLK3dH7S1sU8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17321539276908.80672429081335; Wed, 20 Nov 2024 17:52:07 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHr-0001bA-CK; Wed, 20 Nov 2024 20:47:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHq-0001aq-El for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:14 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHo-0004XZ-D4 for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=79pr1drhMqVTakNZzCI6+VG2OY2chgT2RrjIC+e91vQ=; b=gvB4mbFQ9zlG5lO u1L/TOkOFKnZcHgVDSsTzZ7wwzSy3rpDes9tkzZz8t0WGAXx7GSwWyS3cCaWNUFo1YnSBC/dLSNUR oeNIDO4ubvM7KYes46csSSvCJzGBDK1oNSTBl4T3pPGH+OugC3pm7wIIaKtx62pwGk8IJW3ucrFXR xA=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 14/43] helper-to-tcg: Introduce PrepareForOptPass Date: Thu, 21 Nov 2024 02:49:18 +0100 Message-ID: <20241121014947.18666-15-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153929075116600 Content-Type: text/plain; charset="utf-8" Adds a new LLVM pass that runs early in the pipeline with the goal of preparing the input module for optimization by doing some early culling of functions and information gathering. This commits sets up a new LLVM pass over the IR module and runs it from the pipeline. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/CmdLineOptions.h | 2 ++ .../helper-to-tcg/include/PrepareForOptPass.h | 34 +++++++++++++++++++ subprojects/helper-to-tcg/meson.build | 2 ++ .../PrepareForOptPass/PrepareForOptPass.cpp | 25 ++++++++++++++ .../helper-to-tcg/pipeline/Pipeline.cpp | 27 +++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 subprojects/helper-to-tcg/include/PrepareForOptPass.h create mode 100644 subprojects/helper-to-tcg/passes/PrepareForOptPass/Prep= areForOptPass.cpp diff --git a/subprojects/helper-to-tcg/include/CmdLineOptions.h b/subprojec= ts/helper-to-tcg/include/CmdLineOptions.h index 5774ab07b1..ed60c45f9a 100644 --- a/subprojects/helper-to-tcg/include/CmdLineOptions.h +++ b/subprojects/helper-to-tcg/include/CmdLineOptions.h @@ -21,3 +21,5 @@ =20 // Options for pipeline extern llvm::cl::list InputFiles; +// Options for PrepareForOptPass +extern llvm::cl::opt TranslateAllHelpers; diff --git a/subprojects/helper-to-tcg/include/PrepareForOptPass.h b/subpro= jects/helper-to-tcg/include/PrepareForOptPass.h new file mode 100644 index 0000000000..d74618613f --- /dev/null +++ b/subprojects/helper-to-tcg/include/PrepareForOptPass.h @@ -0,0 +1,34 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include + +// +// PrepareForOptPass +// +// Pass that performs either early information collection or basic culling= of +// the input module. simplify the module, or to allow for further optimiza= tion. +// + +class PrepareForOptPass : public llvm::PassInfoMixin { +public: + PrepareForOptPass() {} + llvm::PreservedAnalyses run(llvm::Module &M, + llvm::ModuleAnalysisManager &MAM); +}; diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 63c6ed17fb..fd3fd6f0ae 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -43,6 +43,8 @@ endif sources =3D [ 'pipeline/Pipeline.cpp', 'passes/llvm-compat.cpp', + 'pipeline/Pipeline.cpp', + 'passes/PrepareForOptPass/PrepareForOptPass.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp new file mode 100644 index 0000000000..0a018494fe --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp @@ -0,0 +1,25 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include + +using namespace llvm; + +PreservedAnalyses PrepareForOptPass::run(Module &M, ModuleAnalysisManager = &MAM) +{ + return PreservedAnalyses::none(); +} diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index 9c0e777893..fad335f4a9 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -34,7 +34,9 @@ #include #include #include +#include =20 +#include #include "llvm-compat.h" =20 using namespace llvm; @@ -155,5 +157,30 @@ int main(int argc, char **argv) =20 ModulePassManager MPM; =20 + // + // Start by Filtering out functions we don't want to translate, + // following by a pass that removes `noinline`s that are inserted + // by clang on -O0. We finally run a UnifyExitNodesPass to make sure + // the helpers we parse only has a single exit. + // + + { + FunctionPassManager FPM; +#if LLVM_VERSION_MAJOR < 14 + FPM.addPass(SROA()); +#else + FPM.addPass(SROAPass()); +#endif + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + } + + MPM.addPass(PrepareForOptPass()); + + { + FunctionPassManager FPM; + FPM.addPass(compat::UnifyFunctionExitNodesPass()); + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + } + return 0; } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153987; cv=none; d=zohomail.com; s=zohoarc; b=JZuqkRG07vIlPSg5CegTE4NsDPue24I0kGa+qDehApIvAYi0pFCsKfS+1cCfqt1ZQ2PnYz1DCLrJRKAZSVQ9ifb/X6CERsL1Sys0LfmzIwd1ofu8SC8HgE5NibREOLw+VKpZhyhr3RZmJi5yDbsn6AhfSLpzS+Z5xkWJtLGbNWQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153987; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=lmWGLOze3SuRS/fAHZcf3ZMhqPnKfajIC31FbL7DQy4=; b=Px3rGLdJjQhuVl8g0e6MJT/wxmRLYFr24L3DcBIHdeJmu7cc0crODC1oA+cScMG9/tgjxwxdxvHhc7CBn6TFsvJMIQoCkRHFWm5gYb6eNAWO7aEfoWj0ZugaHRJOmvQ83t9sMIGJZJCoFmVPKp1npGUyOwQs6HJMdrDu3R0pwKY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153987139817.7591636343519; Wed, 20 Nov 2024 17:53:07 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHt-0001cf-4W; Wed, 20 Nov 2024 20:47:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHr-0001b0-2o for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:15 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHo-0004Xd-Um for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:14 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=lmWGLOze3SuRS/fAHZcf3ZMhqPnKfajIC31FbL7DQy4=; b=NtKZojsW4OM2ES9 t+zx3ibbMJSZEnIAu6650ysyhwmS0QWy0JKdaK4i2rn3DNUvG5ocwzMFlMkJMyaVtcuGu0MmDJA9l VItuhIQ5DVPVNXjcsYjW7kSQuLTXnEzHm7rkWzQTi7u2FXbWfHQoTVEgl+JmVvEDuY6ZILsSuv/+Q 3s=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 15/43] helper-to-tcg: PrepareForOptPass, map annotations Date: Thu, 21 Nov 2024 02:49:19 +0100 Message-ID: <20241121014947.18666-16-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153989315116600 Content-Type: text/plain; charset="utf-8" In the LLVM IR module function annotations are stored in one big global array of strings. Traverse this array and parse the data into a format more useful for future passes. A map between Functions * and a list of annotations is exposed. Signed-off-by: Anton Johansson --- .../include/FunctionAnnotation.h | 54 ++++++++++++ .../helper-to-tcg/include/PrepareForOptPass.h | 7 +- .../PrepareForOptPass/PrepareForOptPass.cpp | 87 +++++++++++++++++++ .../helper-to-tcg/pipeline/Pipeline.cpp | 3 +- 4 files changed, 149 insertions(+), 2 deletions(-) create mode 100644 subprojects/helper-to-tcg/include/FunctionAnnotation.h diff --git a/subprojects/helper-to-tcg/include/FunctionAnnotation.h b/subpr= ojects/helper-to-tcg/include/FunctionAnnotation.h new file mode 100644 index 0000000000..b562f7c892 --- /dev/null +++ b/subprojects/helper-to-tcg/include/FunctionAnnotation.h @@ -0,0 +1,54 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include +#include +#include + +namespace llvm +{ +class Function; +} + +// Different kind of function annotations which control the behaviour +// of helper-to-tcg. +enum class AnnotationKind : uint8_t { + // Function should be translated + HelperToTcg, + // Declares a list of arguments as immediates + Immediate, + // Declares a list of arguments as vectors, represented by offsets into + // the CPU state + PtrToOffset, +}; + +// Annotation data which may be attached to a function +struct Annotation { + // Indices of function arguments the annotation applies to, only + // used for AnnotationKind::[Immediate|PtrToOffset]. + llvm::SmallVector ArgIndices; + AnnotationKind Kind; +}; + +// Map from Function * to a list of struct Annotation. std::map is used h= ere +// which allocates for each mapped pair due to the value being large +// (at least 48*3 bits). If ArgIndices were to be stored out-of-band this= could +// be reduced, and DenseMap would be more appropriate. +using AnnotationVectorTy =3D llvm::SmallVector; +using AnnotationMapTy =3D llvm::DenseMap; diff --git a/subprojects/helper-to-tcg/include/PrepareForOptPass.h b/subpro= jects/helper-to-tcg/include/PrepareForOptPass.h index d74618613f..5f9c059b97 100644 --- a/subprojects/helper-to-tcg/include/PrepareForOptPass.h +++ b/subprojects/helper-to-tcg/include/PrepareForOptPass.h @@ -17,6 +17,7 @@ =20 #pragma once =20 +#include "FunctionAnnotation.h" #include =20 // @@ -27,8 +28,12 @@ // =20 class PrepareForOptPass : public llvm::PassInfoMixin { + AnnotationMapTy &ResultAnnotations; public: - PrepareForOptPass() {} + PrepareForOptPass(AnnotationMapTy &ResultAnnotations) + : ResultAnnotations(ResultAnnotations) + { + } llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM); }; diff --git a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp index 0a018494fe..9f1d4df102 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp @@ -16,10 +16,97 @@ // =20 #include +#include + +#include +#include +#include +#include =20 using namespace llvm; =20 +static Expected parseAnnotationStr(StringRef Str, + uint32_t num_function_args) +{ + Annotation Ann; + + Str =3D Str.trim(); + + if (Str.consume_front("helper-to-tcg")) { + Ann.Kind =3D AnnotationKind::HelperToTcg; + // Early return, no additional info to parse from annotation string + return Ann; + } else if (Str.consume_front("immediate")) { + Ann.Kind =3D AnnotationKind::Immediate; + } else if (Str.consume_front("ptr-to-offset")) { + Ann.Kind =3D AnnotationKind::PtrToOffset; + } else { + return mkError("Unknown annotation"); + } + + // Parse comma separated list of argument indices + + if (!Str.consume_front(":")) { + return mkError("Expected \":\""); + } + + Str =3D Str.ltrim(' '); + do { + Str =3D Str.ltrim(' '); + uint32_t i =3D 0; + Str.consumeInteger(10, i); + if (i >=3D num_function_args) { + return mkError("Annotation has out of bounds argument index"); + } + Ann.ArgIndices.push_back(i); + } while (Str.consume_front(",")); + + return Ann; +} + +static void collectAnnotations(Module &M, AnnotationMapTy &ResultAnnotatio= ns) +{ + // cast over dyn_cast is being used here to + // assert that the structure of + // + // llvm.global.annotation + // + // is what we expect. + + GlobalVariable *GA =3D M.getGlobalVariable("llvm.global.annotations"); + if (!GA) { + return; + } + + // Get the metadata which is stored in the first op + auto *CA =3D cast(GA->getOperand(0)); + // Loop over metadata + for (Value *CAOp : CA->operands()) { + auto *Struct =3D cast(CAOp); + assert(Struct->getNumOperands() >=3D 2); + Constant *UseOfF =3D Struct->getOperand(0); + if (isa(UseOfF)) { + continue; + } + auto *F =3D cast(UseOfF->getOperand(0)); + auto *AnnVar =3D + cast(Struct->getOperand(1)->getOperand(0)); + auto *AnnData =3D cast(AnnVar->getOperand(0)); + + StringRef AnnStr =3D AnnData->getAsString(); + AnnStr =3D AnnStr.substr(0, AnnStr.size() - 1); + Expected Ann =3D parseAnnotationStr(AnnStr, F->arg_siz= e()); + if (!Ann) { + dbgs() << "Failed to parse annotation: \"" << Ann.takeError() + << "\" for function " << F->getName() << "\n"; + continue; + } + ResultAnnotations[F].push_back(*Ann); + } +} + PreservedAnalyses PrepareForOptPass::run(Module &M, ModuleAnalysisManager = &MAM) { + collectAnnotations(M, ResultAnnotations); return PreservedAnalyses::none(); } diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index fad335f4a9..3b9493bc73 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -174,7 +174,8 @@ int main(int argc, char **argv) MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); } =20 - MPM.addPass(PrepareForOptPass()); + AnnotationMapTy Annotations; + MPM.addPass(PrepareForOptPass(Annotations)); =20 { FunctionPassManager FPM; --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153703; cv=none; d=zohomail.com; s=zohoarc; b=aiZojJhd3pYzLOMjktnfxofgRhkhKbLw0nh/2IaF+nihJ34u36R0kkaNQZ1/3LddDTZCkhTR0Q+gDcNIPoDmkvdY3Oe4DQop1u/PoZ+RBVzR6ab7N+ClhHRAbUAmYiPdgJqxpanzyFOM/fUQJ96oh9w9rzRpfOPz4voFn/U/jTI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153703; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=OIlScO3BuH63uZxdmqbUwS+sjbYNsWyshmkeVqgGtWQ=; b=SfGwBv1NoYD1+5Gxex+2W4OXRL61SVrvK0LbjamEQ03cvR5f9DzO/zHE1tEZc6R8n5ueQfzwmrwxKJx0/XsbIXe6sktwKvr8NtGJRK0J61cwRX4zwzF4bQzZKamXMub5rKCeGroOx5KlxB/hj64VYNJVUK2GFMKa0ecZzyVFmfs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153703103340.9596052068604; Wed, 20 Nov 2024 17:48:23 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHs-0001cX-Tw; Wed, 20 Nov 2024 20:47:16 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHr-0001b4-8i for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:15 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHp-0004Xj-Ie for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=OIlScO3BuH63uZxdmqbUwS+sjbYNsWyshmkeVqgGtWQ=; b=mrO8wMR8oaQDdcZ JelEhQefGPshBw7N41+t9ly5Fx6J1+DhujzxT0HxjnTXZoVDY/DOO8wZK3lle/79GWxu43zp0qmFA 1y/hKzVMBJ/GmKZ78F4PWjgQUBsCQtTBKcCbPCZOL6IO6NaIg6F0K+GdZrUZ5UISl2tMeIuU6L458 xc=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 16/43] helper-to-tcg: PrepareForOptPass, Cull unused functions Date: Thu, 21 Nov 2024 02:49:20 +0100 Message-ID: <20241121014947.18666-17-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153704504116600 Content-Type: text/plain; charset="utf-8" Make an early pass over all functions in the input module and filter out functions with: 1. Invalid return type; 2. No helper-to-tcg annotation. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/PrepareForOptPass.h | 7 +- .../PrepareForOptPass/PrepareForOptPass.cpp | 93 +++++++++++++++++++ .../helper-to-tcg/pipeline/Pipeline.cpp | 7 +- 3 files changed, 104 insertions(+), 3 deletions(-) diff --git a/subprojects/helper-to-tcg/include/PrepareForOptPass.h b/subpro= jects/helper-to-tcg/include/PrepareForOptPass.h index 5f9c059b97..8615625f09 100644 --- a/subprojects/helper-to-tcg/include/PrepareForOptPass.h +++ b/subprojects/helper-to-tcg/include/PrepareForOptPass.h @@ -29,9 +29,12 @@ =20 class PrepareForOptPass : public llvm::PassInfoMixin { AnnotationMapTy &ResultAnnotations; + bool TranslateAllHelpers; public: - PrepareForOptPass(AnnotationMapTy &ResultAnnotations) - : ResultAnnotations(ResultAnnotations) + PrepareForOptPass(AnnotationMapTy &ResultAnnotations, + bool TranslateAllHelpers) + : ResultAnnotations(ResultAnnotations), + TranslateAllHelpers(TranslateAllHelpers) { } llvm::PreservedAnalyses run(llvm::Module &M, diff --git a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp index 9f1d4df102..22509008c8 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp @@ -17,12 +17,17 @@ =20 #include #include +#include =20 #include #include #include +#include #include =20 +#include +#include + using namespace llvm; =20 static Expected parseAnnotationStr(StringRef Str, @@ -105,8 +110,96 @@ static void collectAnnotations(Module &M, AnnotationMa= pTy &ResultAnnotations) } } =20 +inline bool hasValidReturnTy(const Module &M, const Function *F) +{ + Type *RetTy =3D F->getReturnType(); + return RetTy =3D=3D Type::getVoidTy(F->getContext()) || + RetTy =3D=3D Type::getInt8Ty(M.getContext()) || + RetTy =3D=3D Type::getInt16Ty(M.getContext()) || + RetTy =3D=3D Type::getInt32Ty(M.getContext()) || + RetTy =3D=3D Type::getInt64Ty(M.getContext()); +} + +// Functions that should be removed: +// - No helper-to-tcg annotation (if TranslateAllHelpers =3D=3D false); +// - Invalid (non-integer/void) return type +static bool shouldRemoveFunction(const Module &M, const Function &F, + const AnnotationMapTy &AnnotationMap, + bool TranslateAllHelpers) +{ + if (F.isDeclaration()) { + return false; + } + + if (!hasValidReturnTy(M, &F)) { + return true; + } + + auto hasCorrectAnnotation =3D [](const Annotation &Ann) { + return Ann.Kind =3D=3D AnnotationKind::HelperToTcg; + }; + + std::queue Worklist; + std::set Visited; + Worklist.push(&F); + while (!Worklist.empty()) { + const Function *F =3D Worklist.front(); + Worklist.pop(); + if (F->isDeclaration() or Visited.find(F) !=3D Visited.end()) { + continue; + } + Visited.insert(F); + + // Check for llvm-to-tcg annotation + if (TranslateAllHelpers and F->getName().startswith("helper_")) { + return false; + } else { + auto It =3D AnnotationMap.find(F); + if (It !=3D AnnotationMap.end()) { + const auto &AnnotationVec =3D It->second; + auto Res =3D find_if(AnnotationVec, hasCorrectAnnotation); + if (Res !=3D AnnotationVec.end()) { + return false; + } + } + } + + // Push functions that call F to the worklist, this way we retain + // functions that are being called by functions with the llvm-to-t= cg + // annotation. + for (const User *U : F->users()) { + auto Call =3D dyn_cast(U); + if (!Call) { + continue; + } + const Function *ParentF =3D Call->getParent()->getParent(); + Worklist.push(ParentF); + } + } + + return true; +} + +static void cullUnusedFunctions(Module &M, AnnotationMapTy &Annotations, + bool TranslateAllHelpers) +{ + SmallVector FunctionsToRemove; + for (auto &F : M) { + if (shouldRemoveFunction(M, F, Annotations, TranslateAllHelpers)) { + FunctionsToRemove.push_back(&F); + } + } + + for (Function *F : FunctionsToRemove) { + Annotations.erase(F); + F->setComdat(nullptr); + F->deleteBody(); + } +} + PreservedAnalyses PrepareForOptPass::run(Module &M, ModuleAnalysisManager = &MAM) { collectAnnotations(M, ResultAnnotations); + cullUnusedFunctions(M, ResultAnnotations, TranslateAllHelpers); return PreservedAnalyses::none(); } diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index 3b9493bc73..dde3641ab3 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -47,6 +47,11 @@ cl::OptionCategory Cat("helper-to-tcg Options"); cl::opt InputFile(cl::Positional, cl::desc("[input LLVM modul= e]"), cl::cat(Cat)); =20 +// Options for PrepareForOptPass +cl::opt TranslateAllHelpers( + "translate-all-helpers", cl::init(false), + cl::desc("Translate all functions starting with helper_*"), cl::cat(Ca= t)); + // Define a TargetTransformInfo (TTI) subclass, this allows for overriding // common per-llvm-target information expected by other LLVM passes, such // as the width of the largest scalar/vector registers. Needed for consis= tent @@ -175,7 +180,7 @@ int main(int argc, char **argv) } =20 AnnotationMapTy Annotations; - MPM.addPass(PrepareForOptPass(Annotations)); + MPM.addPass(PrepareForOptPass(Annotations, TranslateAllHelpers)); =20 { FunctionPassManager FPM; --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153942; cv=none; d=zohomail.com; s=zohoarc; b=etKFAIb2EyHiZatfE7qwlif4XyU6Pvnowu4tyGmWSgvQar6xFx+Lve4TBTQrsoX77Wg4OlPmECyqZhtqErZXazdgIzsHgigaaA337ONXyFibyo47okNJjtnE1NCZqAlu2oRhdAdOfcSYgVeqwJzHU4j+0TUzROGIK7gYGPVjmKE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153942; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=NewhKMIelYRuDNznnvD5yTlXbeDt4XshQYjlz/pQPQs=; b=cueEJQhHs2Pth5aSfJheZ4R3QOscOkUgkHQiqo4dGjIUMSEnxjaMnwv6Rq8ouvePuNJ+M/mUWFViJde/tOzbXzR1BMivTaCkKQQH52daZXD2IiljqHN4KCJY2Q/oRXlJRMZ4XDOOfPIU6isSwkNz5DCGS4nPd+zuoZOSmm7qM1I= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153942067920.3809771410397; Wed, 20 Nov 2024 17:52:22 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHt-0001cx-LX; Wed, 20 Nov 2024 20:47:17 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHr-0001bW-Nb for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:15 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHq-0004Xz-4e for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:15 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=NewhKMIelYRuDNznnvD5yTlXbeDt4XshQYjlz/pQPQs=; b=hKczrorhim53phv kA62HkSwy+a2H7Y7YBBwMRmuAn2jCl4sxhMrrTtMHVT3OCqMJoYbvv44OY9xkEtjafP1P94/5i7Eq Wx6dKEw8kGEVwheFDZm38N1KiZhyNpR3CkSIo5NYDZ/WPApzDdNcwPqIBMmAkxlIxoj7g7tRi+JSV IM=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 17/43] helper-to-tcg: PrepareForOptPass, undef llvm.returnaddress Date: Thu, 21 Nov 2024 02:49:21 +0100 Message-ID: <20241121014947.18666-18-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153943142116600 Content-Type: text/plain; charset="utf-8" Convert llvm.returnaddress arguments to cpu_[ld|st]*() to undef, causing the LLVM optmizer to discard the intrinsics. Needed as llvm.returnadress is not representable in TCG, and usually results from usage of GETPC() in helper functions. Signed-off-by: Anton Johansson --- .../PrepareForOptPass/PrepareForOptPass.cpp | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp index 22509008c8..b357debb5d 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp @@ -23,6 +23,7 @@ #include #include #include +#include #include =20 #include @@ -197,9 +198,56 @@ static void cullUnusedFunctions(Module &M, AnnotationM= apTy &Annotations, } } =20 +struct RetAddrReplaceInfo { + User *Parent; + unsigned OpIndex; + Type *Ty; +}; + +static void replaceRetaddrWithUndef(Module &M) +{ + // Replace uses of llvm.returnaddress arguments to cpu_ld* w. undef, + // and let optimizations remove it. Needed as llvm.returnaddress is + // not reprensentable in TCG. + SmallVector UsesToReplace; + Function *Retaddr =3D Intrinsic::getDeclaration(&M, Intrinsic::returna= ddress); + // Loop over all calls to llvm.returnaddress + for (auto *CallUser : Retaddr->users()) { + auto *Call =3D dyn_cast(CallUser); + if (!Call) { + continue; + } + for (auto *PtrToIntUser : Call->users()) { + auto *Cast =3D dyn_cast(PtrToIntUser); + if (!Cast) { + continue; + } + for (Use &U : Cast->uses()) { + auto *Call =3D dyn_cast(U.getUser()); + Function *F =3D Call->getCalledFunction(); + StringRef Name =3D F->getName(); + if (Name.startswith("cpu_ld") or Name.startswith("cpu_st")= ) { + UsesToReplace.push_back({ + .Parent =3D U.getUser(), + .OpIndex =3D U.getOperandNo(), + .Ty =3D U->getType(), + }); + } + } + } + } + + // Defer replacement to not invalidate iterators + for (RetAddrReplaceInfo &RI : UsesToReplace) { + auto *Undef =3D UndefValue::get(RI.Ty); + RI.Parent->setOperand(RI.OpIndex, Undef); + } +} + PreservedAnalyses PrepareForOptPass::run(Module &M, ModuleAnalysisManager = &MAM) { collectAnnotations(M, ResultAnnotations); cullUnusedFunctions(M, ResultAnnotations, TranslateAllHelpers); + replaceRetaddrWithUndef(M); return PreservedAnalyses::none(); } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153735; cv=none; d=zohomail.com; s=zohoarc; b=kevMbVSxgFnfZXLuJHeZW7yHqibgpEtFz6OlyiT7QNubTr7WstKX2lZQwqN5uviHxPaxKZNDYGY+PrrHIFXp75quIUpPux6cH9IIvLondkWGyS0ffUDD7LBnDo0sboJP6hexQPAwN474ltlRJ2NuK72cvMeRnB3ZIXslefv80wY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153735; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=50Y7YhhV3UHHQ/N3ezFgY4yjynTIuC8usuDdVwJv/S8=; b=bi2BV6ywLcu2x9TysQOfBdM2WZpiMYxyVDgaRZu5oFnWMfJ+JLZlyMfaGUv8IL2c9zlzIhIUwejMSoDlQghJmkdmAb3LxEy4l8iDVrDl3P6+B1mXbxdbXN8WMIxdnPnPG81cKXcTwxqH0tZtShv5g9VKQBCyOqdAME4qSiG6/6A= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153735886142.15459480868117; Wed, 20 Nov 2024 17:48:55 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHv-0001e5-M7; Wed, 20 Nov 2024 20:47:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHs-0001c9-8q for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:16 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHq-0004YO-Vg for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=50Y7YhhV3UHHQ/N3ezFgY4yjynTIuC8usuDdVwJv/S8=; b=aKHl3k4bJ+dPc9c rHzADXC2HUzUNT4am92KCWNcEKuEwE8IVMCOx2QZQZJCwRU0nWqbuMY3y0BC7xxBjFl2sZuRSFREj 14EhXW8AH9L7MDfUKSTPgBI1t9Eh+8HB7yTS9+ZoNtmE5vle6syyamsI4yy4y6Qki8D0EFggyW+3T Iw=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 18/43] helper-to-tcg: PrepareForOptPass, Remove noinline attribute Date: Thu, 21 Nov 2024 02:49:22 +0100 Message-ID: <20241121014947.18666-19-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153736142116600 Content-Type: text/plain; charset="utf-8" When producing LLVM IR using clang -O0, a noinline attribute is added. Remove this attribute to not inhibit future optimization. Signed-off-by: Anton Johansson --- .../passes/PrepareForOptPass/PrepareForOptPass.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForO= ptPass.cpp index b357debb5d..cfd1c23c24 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForOptPass/PrepareForOptPass.= cpp @@ -25,6 +25,7 @@ #include #include #include +#include =20 #include #include @@ -249,5 +250,11 @@ PreservedAnalyses PrepareForOptPass::run(Module &M, Mo= duleAnalysisManager &MAM) collectAnnotations(M, ResultAnnotations); cullUnusedFunctions(M, ResultAnnotations, TranslateAllHelpers); replaceRetaddrWithUndef(M); + // Remove noinline function attributes automatically added by -O0 + for (Function &F : M) { + if (F.hasFnAttribute(Attribute::AttrKind::NoInline)) { + F.removeFnAttr(Attribute::AttrKind::NoInline); + } + } return PreservedAnalyses::none(); } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153836; cv=none; d=zohomail.com; s=zohoarc; b=KUrTeHqezZnrbmPyEUrefj2GfE5Y2PnMpW9pjlq+g6oM/176UqcQjM50Ie876pd9XuLPv4DsI30e8Fr7ktGw/p1LAENQk8b5II9OWo7Y7kUUvzkhnHCU4lhK4eHYJEZHyP3E6jpjo8DeSIiN4eiWPkPf8hnG4aAyHmCmLUQ4feY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153836; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=unJIN5P7Zmya2tPh5Ef9TFV6DCQ9eVjum3FXEj+KWf4=; b=BUzwSFUIwt63LDjIo1FKsg2IwUaMn11DMN5iM0GbW4eVLx2CvdrCnI5OOPuFxa+Qly+N5Chi1QG+7gyMnzpeR26bsCK82lEaoYPGAu4oP+lAsASNU3MmlYJlPWxTim6c5n4YpKIB1+B/180RvRzrzm6d2uzubTwVuobKKopdWhI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153836488871.2269359740361; Wed, 20 Nov 2024 17:50:36 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHu-0001d3-4F; Wed, 20 Nov 2024 20:47:18 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHs-0001cW-Qt for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:16 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHr-0004Yh-FL for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:16 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=unJIN5P7Zmya2tPh5Ef9TFV6DCQ9eVjum3FXEj+KWf4=; b=hg44Pm/3H03AIcO u9ZsqHrLVbm6+bCAOwH8miu34gY1d85fnOnSXpbEIDBKiXKoN8vCLFWlUZBwxEhzNSfCRPB5uyX2e sE4HGk8FPlzkXhCAijHa0OqoZqHmICruu7PjdXPdNyuCXRWFTZOaUvkY3YHOc3NAFHDlbJ5nBUY/s TA=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 19/43] helper-to-tcg: Pipeline, run optimization pass Date: Thu, 21 Nov 2024 02:49:23 +0100 Message-ID: <20241121014947.18666-20-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153838548116600 Content-Type: text/plain; charset="utf-8" Run a standard LLVM -Os optimization pass, which makes up the bulk of optimizations in helper-to-tcg. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/pipeline/Pipeline.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index dde3641ab3..a26b7a7350 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -188,5 +188,17 @@ int main(int argc, char **argv) MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); } =20 + // + // Run a -Os optimization pass. In general -Os will prefer loop + // vectorization over unrolling, as compared to -O3. In TCG, this + // translates to more utilization of gvec and possibly smaller TBs. + // + + // Optimization passes + MPM.addPass(PB.buildModuleSimplificationPipeline( + compat::OptimizationLevel::Os, compat::LTOPhase)); + MPM.addPass( + PB.buildModuleOptimizationPipeline(compat::OptimizationLevel::Os)); + return 0; } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153757; cv=none; d=zohomail.com; s=zohoarc; b=jy4CwZ76WjxaZHQMOATpxJ+5zRYYLRAHelkAdt4UuAYTpbIdTcbzmSYsuP4FPeYqTl0/JH/kBEYhaWuoOg2DVM7hEgGtEXi+Vt2UnS0+APB9YDqErpIyVEgBVkcwmgvDfh/DWcZVd+HGyh6FPZhq3qCmX4yPLmpiq5WMCknEJ4Y= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153757; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=C3wX1QdcsW7MQWsSRDypo/NZdV81zatwSZ7ea+i2V6E=; b=nmUFOhZgh+zoytfI+i/s3PWcuApz4BxGCN/LRzet9PkBd0AuPON60zT6tMpSdJfMm4Lx6VKGTRnaKz7kKW4Sin49ViqpmYOrk5uKDji/gRMsX07gGvQhF86UewvioP1hFS+LnVzzz7QinHAdGHsVttr9Zqkyh8dGU2OzGbPB5WA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153757787472.00781011068227; Wed, 20 Nov 2024 17:49:17 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHv-0001dz-EL; Wed, 20 Nov 2024 20:47:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHu-0001dT-Nw for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:18 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHs-0004Yv-5U for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=C3wX1QdcsW7MQWsSRDypo/NZdV81zatwSZ7ea+i2V6E=; b=wVGW+6OM+KJB33R Z/AyImJ4n28xyU75TZh5c+d7I3k1U1cJus9nWVQbJnlskEzLxbRnsX35iJoopMCBuvt++8juVvBFM XzP86+ze8FC0jU1AAUQ3ER3cedUMp8meVJHj7TjN4ZgQRKUwg4MgbKLl9K1mB7EGqFbMVV8c6hbYM hk=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 20/43] helper-to-tcg: Introduce pseudo instructions Date: Thu, 21 Nov 2024 02:49:24 +0100 Message-ID: <20241121014947.18666-21-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153758430116600 Content-Type: text/plain; charset="utf-8" "pseudo" instructions makes it easy to add custom instructions to LLVM IR in the form of calls to undefined functions. These will be used in future commits to express functionality present in TCG that is missing from LLVM IR (certain vector ops.), or to simplify the backend by collecting similar instruction mappings into a single opcode (idendity mapping). Mapping from a call instructions in LLVM IR to an enum representing the pseudo instruction is also handled, this avoids string comparisons in the backend, and is easy to switch over. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/meson.build | 1 + .../helper-to-tcg/passes/PseudoInst.cpp | 142 ++++++++++++++++++ subprojects/helper-to-tcg/passes/PseudoInst.h | 63 ++++++++ .../helper-to-tcg/passes/PseudoInst.inc | 76 ++++++++++ 4 files changed, 282 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/PseudoInst.cpp create mode 100644 subprojects/helper-to-tcg/passes/PseudoInst.h create mode 100644 subprojects/helper-to-tcg/passes/PseudoInst.inc diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index fd3fd6f0ae..6aba71d5ca 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -45,6 +45,7 @@ sources =3D [ 'passes/llvm-compat.cpp', 'pipeline/Pipeline.cpp', 'passes/PrepareForOptPass/PrepareForOptPass.cpp', + 'passes/PseudoInst.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/PseudoInst.cpp b/subprojects/= helper-to-tcg/passes/PseudoInst.cpp new file mode 100644 index 0000000000..d7efa11499 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PseudoInst.cpp @@ -0,0 +1,142 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include "PseudoInst.h" +#include "llvm-compat.h" + +#include +#include +#include +#include +#include + +using namespace llvm; + +#define PSEUDO_INST_DEF(name, ret, args) #name +static const char *PseudoInstName[] =3D { +#include "PseudoInst.inc" +}; +#undef PSEUDO_INST_DEF + +#define PSEUDO_INST_ARGVEC(...) = \ + (sizeof((PseudoInstArg[]){__VA_ARGS__}) / sizeof(PseudoInstArg)) + +#define PSEUDO_INST_DEF(name, ret, args) args +static uint8_t PseudoInstArgCount[] =3D { +#include "PseudoInst.inc" +}; +#undef PSEUDO_INST_DEF + +// In order to map from a Function * to a PseudoInst, we keep a map +// of all Functions created, this simplifies mapping of callee's to +// a PseudoInst that can be switched over. +static DenseMap MapFuncToInst; + +// Converts llvm `Type`s to a string representation +// that can be embedded in function names for basic overloading. +// +// Ex. +// +// *i32 -> "pi32" +// [8 x i8] -> "a8xi8" +// <128 x i8> -> "v128xi8" +// +// LLVM has an implementation of a similar function used by intrinsics, +// called getMangledTypeStr, but it's not exposed. +inline std::string getMangledTypeStr(Type *Ty) +{ + std::string TypeStr =3D ""; + llvm::raw_string_ostream TypeStream(TypeStr); + switch (Ty->getTypeID()) { + case Type::ArrayTyID: { + auto *ArrayTy =3D cast(Ty); + std::string ElementStr =3D getMangledTypeStr(ArrayTy->getElementTy= pe()); + TypeStream << "a" << ArrayTy->getNumElements() << "x" << ElementSt= r; + } break; +#if LLVM_VERSION_MAJOR >=3D 11 + case Type::FixedVectorTyID: { +#else + case Type::VectorTyID: { +#endif + auto *VecTy =3D cast(Ty); + uint32_t ElementCount =3D compat::getVectorElementCount(VecTy); + std::string ElementStr =3D getMangledTypeStr(VecTy->getElementType= ()); + TypeStream << "v" << ElementCount << "x" << ElementStr; + } break; + case Type::StructTyID: { + auto *StructTy =3D cast(Ty); + TypeStream << StructTy->getName(); + } break; + case Type::IntegerTyID: { + auto *IntTy =3D cast(Ty); + TypeStream << "i" << IntTy->getBitWidth(); + } break; + case Type::PointerTyID: { + auto *PtrTy =3D cast(Ty); + std::string ElementStr =3D + getMangledTypeStr(PtrTy->getPointerElementType()); + TypeStream << "p" << ElementStr; + } break; + default: + abort(); + } + + return TypeStream.str(); +} + +const char *pseudoInstName(PseudoInst Inst) { return PseudoInstName[Inst];= } + +uint8_t pseudoInstArgCount(PseudoInst Inst) { return PseudoInstArgCount[In= st]; } + +llvm::FunctionCallee pseudoInstFunction(llvm::Module &M, PseudoInst Inst, + llvm::Type *RetType, + llvm::ArrayRef ArgTy= pes) +{ + auto *FT =3D llvm::FunctionType::get(RetType, ArgTypes, false); + + std::string FnName{PseudoInstName[Inst]}; + if (!RetType->isVoidTy()) { + FnName +=3D "."; + FnName +=3D getMangledTypeStr(RetType); + } + for (llvm::Type *Ty : ArgTypes) { + if (Ty->isLabelTy()) { + continue; + } + FnName +=3D "."; + FnName +=3D getMangledTypeStr(Ty); + } + + llvm::FunctionCallee Fn =3D M.getOrInsertFunction(FnName, FT); + auto *F =3D cast(Fn.getCallee()); + MapFuncToInst.insert({F, Inst}); + + return Fn; +} + +// Takes value as convenience +PseudoInst getPseudoInstFromCall(const CallInst *Call) +{ + Function *F =3D Call->getCalledFunction(); + + auto It =3D MapFuncToInst.find(F); + if (It =3D=3D MapFuncToInst.end()) { + return InvalidPseudoInst; + } + + return It->second; +} diff --git a/subprojects/helper-to-tcg/passes/PseudoInst.h b/subprojects/he= lper-to-tcg/passes/PseudoInst.h new file mode 100644 index 0000000000..6bf841d85c --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PseudoInst.h @@ -0,0 +1,63 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include + +#include "llvm/ADT/ArrayRef.h" +#include "llvm/ADT/Optional.h" +#include "llvm/IR/Module.h" +#include "llvm/IR/Value.h" + +// Pseudo instructions refers to extra LLVM instructions implemented as +// calls to undefined functions. They are useful for amending LLVM IR to +// simplify mapping to TCG in the backend, e.g. +// +// %2 =3D call i32 @IdentityMap.i32.i16(i16 %1) +// +// is a pseudo opcode used to communicate that %1 and %2 should be mapped +// to the same value in TCG. + +enum PseudoInstArg { + ArgInt, + ArgVec, + ArgPtr, + ArgLabel, + ArgVoid, +}; + +#define PSEUDO_INST_DEF(name, ret, args) name +enum PseudoInst : uint8_t { +#include "PseudoInst.inc" +}; +#undef PSEUDO_INST_DEF + +// Retrieve string representation and argument counts for a given +// pseudo instruction. +const char *pseudoInstName(PseudoInst Inst); +uint8_t pseudoInstArgCount(PseudoInst Inst); + +// Maps PseudoInst + return/argument types to a FunctionCallee that can be +// called. +llvm::FunctionCallee pseudoInstFunction(llvm::Module &M, PseudoInst Inst, + llvm::Type *RetType, + llvm::ArrayRef ArgTy= pes); + +// Reverse mapping of above, takes a call instruction and attempts to map = the +// callee to a PseudoInst. +PseudoInst getPseudoInstFromCall(const llvm::CallInst *Call); diff --git a/subprojects/helper-to-tcg/passes/PseudoInst.inc b/subprojects/= helper-to-tcg/passes/PseudoInst.inc new file mode 100644 index 0000000000..9856afbe74 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PseudoInst.inc @@ -0,0 +1,76 @@ +PSEUDO_INST_DEF(InvalidPseudoInst, ArgVoid, PSEUDO_INST_ARGVEC(ArgVoid)), +// Identity mapping +PSEUDO_INST_DEF(IdentityMap, ArgInt, PSEUDO_INST_ARGVEC(ArgInt)), +// Pointer arithmetic +PSEUDO_INST_DEF(PtrAdd, ArgPtr, PSEUDO_INST_ARGVEC(ArgPtr, Arg= Int)), +// Global accesses +PSEUDO_INST_DEF(AccessGlobalArray, ArgInt, PSEUDO_INST_ARGVEC(ArgInt)), +PSEUDO_INST_DEF(AccessGlobalValue, ArgInt, PSEUDO_INST_ARGVEC(ArgInt)), +// Conditional branch +PSEUDO_INST_DEF(Brcond, ArgVoid, PSEUDO_INST_ARGVEC(ArgInt, Ar= gInt, ArgInt, ArgLabel, ArgLabel)), +// Conditional move +PSEUDO_INST_DEF(Movcond, ArgInt, PSEUDO_INST_ARGVEC(ArgInt, Arg= Int, ArgInt, ArgInt, ArgInt)), +// Vector creation ops +PSEUDO_INST_DEF(VecSplat, ArgVec, PSEUDO_INST_ARGVEC(ArgInt)), +// Vector unary ops +PSEUDO_INST_DEF(VecNot, ArgVec, PSEUDO_INST_ARGVEC(ArgVec)), +// Vector scalar binary ops +PSEUDO_INST_DEF(VecAddScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +PSEUDO_INST_DEF(VecSubScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +PSEUDO_INST_DEF(VecMulScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +PSEUDO_INST_DEF(VecXorScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +PSEUDO_INST_DEF(VecOrScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +PSEUDO_INST_DEF(VecAndScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +PSEUDO_INST_DEF(VecShlScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +PSEUDO_INST_DEF(VecLShrScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +PSEUDO_INST_DEF(VecAShrScalar, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, Arg= Int)), +// Vector unary ops that stores to pointer +PSEUDO_INST_DEF(VecNotStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec)), +// Vector binary ops that stores to pointer +PSEUDO_INST_DEF(VecAddStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecSubStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecMulStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecXorStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecOrStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecAndStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecShlStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecLShrStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecAShrStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgVec)), +PSEUDO_INST_DEF(VecAddScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +PSEUDO_INST_DEF(VecSubScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +PSEUDO_INST_DEF(VecMulScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +PSEUDO_INST_DEF(VecXorScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +PSEUDO_INST_DEF(VecOrScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +PSEUDO_INST_DEF(VecAndScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +PSEUDO_INST_DEF(VecShlScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +PSEUDO_INST_DEF(VecLShrScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +PSEUDO_INST_DEF(VecAShrScalarStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, Ar= gVec, ArgInt)), +// Host memory operations +// vaddr, value si= gn size endian +PSEUDO_INST_DEF(GuestLoad, ArgInt, PSEUDO_INST_ARGVEC(ArgInt, Ar= gInt, ArgInt, ArgInt)), +PSEUDO_INST_DEF(GuestStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgInt, ArgInt, = ArgInt, ArgInt)), +// ... +PSEUDO_INST_DEF(VecTruncStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec)), +PSEUDO_INST_DEF(VecZExtStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec)), +PSEUDO_INST_DEF(VecSExtStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec)), +PSEUDO_INST_DEF(VecSignedSatAddStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecSignedSatSubStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecSelectStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec, ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecFunnelShrStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec, ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecAbsStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec)), +PSEUDO_INST_DEF(VecSignedMaxStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecUnsignedMaxStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecSignedMinStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecUnsignedMinStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecCtlzStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec)), +PSEUDO_INST_DEF(VecCttzStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec)), +PSEUDO_INST_DEF(VecCtpopStore, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgVec)), +PSEUDO_INST_DEF(VecWideCondBitsel, ArgVec, PSEUDO_INST_ARGVEC(ArgVec, A= rgVec, ArgVec)), +PSEUDO_INST_DEF(VecWideCondBitselStore, ArgVoid, PSEUDO_INST_ARGVEC(Arg= Ptr, ArgVec, ArgVec, ArgVec)), +PSEUDO_INST_DEF(VecCompare, ArgVec, PSEUDO_INST_ARGVEC(ArgInt, A= rgVec, ArgVec)), +PSEUDO_INST_DEF(VecSelect, ArgVec, PSEUDO_INST_ARGVEC(ArgInt, A= rgVec, ArgVec)), + +PSEUDO_INST_DEF(SignExtract, ArgInt, PSEUDO_INST_ARGVEC(ArgInt, A= rgInt, ArgInt)), +PSEUDO_INST_DEF(Extract, ArgInt, PSEUDO_INST_ARGVEC(ArgInt, A= rgInt, ArgInt)), + +PSEUDO_INST_DEF(Exception, ArgVoid, PSEUDO_INST_ARGVEC(ArgPtr, = ArgInt)), --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153727; cv=none; d=zohomail.com; s=zohoarc; b=jfwp1HvyQFsQ99OKlg/2uJ3F0QqxeR/prKpI/WBlSodp/N6vRG9ipUV2M3S1Qo1Vjo8y6EuN93hCBsar7j1jCFp07/QzlXsbATZJDrmusRMsq2VvKOjk79fTH/kLrdjVvoP5NohCeER+Fnj0+mQU6KXV0tRtmCONgUlyE8WZb0U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153727; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=3xa4cSQWq1UFcy/VC4ccaIM0E/2QjTKLoAtI/4PVoBM=; b=mtqT0fS/OWzkNfuDLSG5G+cAf5Tpq7oKv1eGRv1+Nr+lbE86LOjivBD5M9ChxbjyKA1U7V+BKPUIce83T4J1tOf7fMrqagO20fjsEjoHyHjNnviEbnA+kS+tVG7InJ6qqnYLbStS3364Kn+f2LdkzRcuUvZGojeP8mZb/5MhOT0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153727398104.61158515423199; Wed, 20 Nov 2024 17:48:47 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHv-0001e4-MG; Wed, 20 Nov 2024 20:47:19 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHu-0001dC-Gu for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:18 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHs-0004Z9-Gg for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=3xa4cSQWq1UFcy/VC4ccaIM0E/2QjTKLoAtI/4PVoBM=; b=M35JFciezjmnJAh Wji961ULdWWiTcGHd+BVNZ1XDhAoov5IUfXyk0Gnimg4vZ4+zIztxFXs97WkpOI3dt8E+QvWvLHj5 +vSlMxvNCGOD6IxvmnEzeayDvSm7YKXf8Qf5ibwJjr1K3892vLzmetG9vHA4DSxViVXC43Vra4eus 9A=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 21/43] helper-to-tcg: Introduce PrepareForTcgPass Date: Thu, 21 Nov 2024 02:49:25 +0100 Message-ID: <20241121014947.18666-22-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153728305116600 Content-Type: text/plain; charset="utf-8" Adds a new pass over the LLVM module which runs post-optimization with the end-goal of: * culling functions which aren't worth translating; * canonicalizing the IR to something closer to TCG, and; * extracting information which may be useful in the backend pass. This commits sets up a new LLVM pass over the IR module and runs it from the pipeline. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/CmdLineOptions.h | 2 ++ .../helper-to-tcg/include/PrepareForTcgPass.h | 27 +++++++++++++++++++ subprojects/helper-to-tcg/meson.build | 1 + .../PrepareForTcgPass/PrepareForTcgPass.cpp | 25 +++++++++++++++++ .../helper-to-tcg/pipeline/Pipeline.cpp | 26 +++++++++++++++++- 5 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 subprojects/helper-to-tcg/include/PrepareForTcgPass.h create mode 100644 subprojects/helper-to-tcg/passes/PrepareForTcgPass/Prep= areForTcgPass.cpp diff --git a/subprojects/helper-to-tcg/include/CmdLineOptions.h b/subprojec= ts/helper-to-tcg/include/CmdLineOptions.h index ed60c45f9a..9553e26407 100644 --- a/subprojects/helper-to-tcg/include/CmdLineOptions.h +++ b/subprojects/helper-to-tcg/include/CmdLineOptions.h @@ -23,3 +23,5 @@ extern llvm::cl::list InputFiles; // Options for PrepareForOptPass extern llvm::cl::opt TranslateAllHelpers; +// Options for PrepareForTcgPass +extern llvm::cl::opt TcgGlobalMappingsName; diff --git a/subprojects/helper-to-tcg/include/PrepareForTcgPass.h b/subpro= jects/helper-to-tcg/include/PrepareForTcgPass.h new file mode 100644 index 0000000000..a41edb4c2e --- /dev/null +++ b/subprojects/helper-to-tcg/include/PrepareForTcgPass.h @@ -0,0 +1,27 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include + +class PrepareForTcgPass : public llvm::PassInfoMixin { +public: + PrepareForTcgPass() {} + llvm::PreservedAnalyses run(llvm::Module &M, + llvm::ModuleAnalysisManager &MAM); +}; diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 6aba71d5ca..6db1a019ce 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -46,6 +46,7 @@ sources =3D [ 'pipeline/Pipeline.cpp', 'passes/PrepareForOptPass/PrepareForOptPass.cpp', 'passes/PseudoInst.cpp', + 'passes/PrepareForTcgPass/PrepareForTcgPass.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp new file mode 100644 index 0000000000..f0ef1abd17 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp @@ -0,0 +1,25 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include + +using namespace llvm; + +PreservedAnalyses PrepareForTcgPass::run(Module &M, ModuleAnalysisManager = &MAM) +{ + return PreservedAnalyses::none(); +} diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index a26b7a7350..7d03389439 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include #include @@ -34,10 +35,12 @@ #include #include #include +#include #include =20 #include -#include "llvm-compat.h" +#include +#include =20 using namespace llvm; =20 @@ -52,6 +55,13 @@ cl::opt TranslateAllHelpers( "translate-all-helpers", cl::init(false), cl::desc("Translate all functions starting with helper_*"), cl::cat(Ca= t)); =20 +// Options for PrepareForTcgPass +cl::opt TcgGlobalMappingsName( + "tcg-global-mappings", + cl::desc(""), + cl::Required, cl::cat(Cat)); + // Define a TargetTransformInfo (TTI) subclass, this allows for overriding // common per-llvm-target information expected by other LLVM passes, such // as the width of the largest scalar/vector registers. Needed for consis= tent @@ -200,5 +210,19 @@ int main(int argc, char **argv) MPM.addPass( PB.buildModuleOptimizationPipeline(compat::OptimizationLevel::Os)); =20 + // + // Next, we run our final transformations, including removing phis and= our + // own instruction combining that prioritizes instructions that map mo= re + // easily to TCG. + // + + MPM.addPass(PrepareForTcgPass()); + MPM.addPass(VerifierPass()); + { + FunctionPassManager FPM; + FPM.addPass(DCEPass()); + MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); + } + return 0; } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153825; cv=none; d=zohomail.com; s=zohoarc; b=RuzrB4lyH51LJ8EPb8kqnbW0PWvYnEyByPsk/OgrpO5w3dYEAO8OPfaCwdynP7IkiVw1YT6f1j3q2Y7hNV7kjv+cz2m/Ei7jxHxSUEpJgyuKWBM08wpqSO+IBuN/cELO1HG1UAkUmFKAFWJrFAZuU6rGqYE42tU4DOR8sW5KPyw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153825; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=gOqbb409IemoDA9cXRKMqCB6ZUSy0Qk58RmK/jyokAM=; b=J6wifnqvbWUBSmsGt96s0XqoZI2zt6yIg9i22HGWA0qD1n6PWupyOis80eaSKHJbQO/Erjl5g17PY9FzC6L+bgSVyBpemlv7BpKpS2xogxA85HBZq8RJGQDgJVTk8DvDNqqnxXzZoPFpueBdFaxCM4MqAx/l+Z6FeOj7Kao1wMk= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153824977156.45141354552425; Wed, 20 Nov 2024 17:50:24 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHx-0001fT-P9; Wed, 20 Nov 2024 20:47:21 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHu-0001dO-MO for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:18 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHt-0004ZP-7S for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:18 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=gOqbb409IemoDA9cXRKMqCB6ZUSy0Qk58RmK/jyokAM=; b=LQ28/UUF0fF3/ns UMioWfeToFvaoy2MSNVq5FSH6NXASrhNUrNy3f2kiAOfLXOB6pTSB0kroWBFiG9knggN1/iuBQwQ2 S2EGXvo+uYlj6Rac5jrZj9VWFRavn1To83P/d9SFeBmLmeQsZsDwhRL87lWnVDifCI1ygWJ7oXeWj dI=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 22/43] helper-to-tcg: PrepareForTcgPass, remove functions w. cycles Date: Thu, 21 Nov 2024 02:49:26 +0100 Message-ID: <20241121014947.18666-23-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153826513116600 Content-Type: text/plain; charset="utf-8" Functions with cycles are removed for two primary reasons: * as a simplifying assumption for register allocation which occurs down the line, and; * if a function contains cycles post-optimization neither unrolling or loop vectorization were beneficial, and the function _might_ be better suited as a helper anyway. Cycles are detected by iterating over Strongly Connected Components (SCCs) which imply the existence of cycles if: - a SCC contains more than one node, or; - it has a self-edge. Signed-off-by: Anton Johansson --- .../PrepareForTcgPass/PrepareForTcgPass.cpp | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp index f0ef1abd17..ccbe3820a0 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp @@ -16,10 +16,42 @@ // =20 #include +#include +#include +#include =20 using namespace llvm; =20 +static void removeFunctionsWithLoops(Module &M, ModuleAnalysisManager &MAM) +{ + // Iterate over all Strongly Connected Components (SCCs), a SCC implies + // the existence of loops if: + // - it has more than one node, or; + // - it has a self-edge. + SmallVector FunctionsToRemove; + for (Function &F : M) { + if (F.isDeclaration()) { + continue; + } + for (auto It =3D scc_begin(&F); !It.isAtEnd(); ++It) { +#if LLVM_VERSION_MAJOR > 10 + if (It.hasCycle()) { +#else + if (It.hasLoop()) { +#endif + FunctionsToRemove.push_back(&F); + break; + } + } + } + + for (auto *F : FunctionsToRemove) { + F->deleteBody(); + } +} + PreservedAnalyses PrepareForTcgPass::run(Module &M, ModuleAnalysisManager = &MAM) { + removeFunctionsWithLoops(M, MAM); return PreservedAnalyses::none(); } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153834; cv=none; d=zohomail.com; s=zohoarc; b=ThPxlLeFh7YygNDimkc+wFmzyjNUxDjdLdH9kPRjQfGvXgoTAHPYZ57C5K2tMKnrOBox0v97pPmiMfkvnqVBpRY6wd0pF4CBxrH1K2XFCFM3RkN96ZR/aSrn+AecWLGGaMc8psTgOwKRP+Wc3Cb/lqAjMPX4YLaUw5yuKraluWo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153834; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=YnFxbrEajHIM+AsXZK4CS7tNWie2c17m2x9nwklbxBg=; b=c41ehHdjbOEbzE3co5+eeV7o2U6einGGuJOi/mlkswwbuboPIwIvTeIylZ8RJQBe/ZSdvZm1qXl7xf7noK3kHn092GSIqjoSA9omy/iaA2hQJhMnfCsVuCL4yBQ0JyWyXS3Qai+mwe0DWWSz0Jy1D1ec+8yQ6tCC7M1XE7qC3ZI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153834288985.5943614667784; Wed, 20 Nov 2024 17:50:34 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHx-0001fJ-6M; Wed, 20 Nov 2024 20:47:21 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHv-0001e9-Md for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:19 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHu-0004Zd-5I for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=YnFxbrEajHIM+AsXZK4CS7tNWie2c17m2x9nwklbxBg=; b=KPOQ705RT54r1Mm QaEruU5ym0GPWedmCLmh/f/g39ZnJZXf4483fDL+3OqgK+cTtrCYPXdREJNpGf5PWiq15WcGlir8w ALVdfNrA+Yq8Ng77u6q6VAnrrCFRhdciNoI4Suvhu4t3MwCyXbTyiachEE0ukEA3QHohgbXk4IjaE RA=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 23/43] helper-to-tcg: PrepareForTcgPass, demote phi nodes Date: Thu, 21 Nov 2024 02:49:27 +0100 Message-ID: <20241121014947.18666-24-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153834534116600 Content-Type: text/plain; charset="utf-8" PHI nodes have no clear analogue in TCG, this commits converts them to stack accesses using a built-in LLVM transformation. Signed-off-by: Anton Johansson --- .../PrepareForTcgPass/PrepareForTcgPass.cpp | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp index ccbe3820a0..a2808eafed 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp @@ -18,7 +18,10 @@ #include #include #include +#include +#include #include +#include =20 using namespace llvm; =20 @@ -50,8 +53,29 @@ static void removeFunctionsWithLoops(Module &M, ModuleAn= alysisManager &MAM) } } =20 +inline void demotePhis(Function &F) +{ + if (F.isDeclaration()) { + return; + } + + SmallVector Phis; + for (auto &I : instructions(F)) { + if (auto *Phi =3D dyn_cast(&I)) { + Phis.push_back(Phi); + } + } + + for (auto *Phi : Phis) { + DemotePHIToStack(Phi); + } +} + PreservedAnalyses PrepareForTcgPass::run(Module &M, ModuleAnalysisManager = &MAM) { removeFunctionsWithLoops(M, MAM); + for (Function &F : M) { + demotePhis(F); + } return PreservedAnalyses::none(); } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153988; cv=none; d=zohomail.com; s=zohoarc; b=FdWPpQX+C7Y/zLS88/dGa1qszG2jU5xF9Njnco22K+B7WVQ/xu5skjv/2Y2BKsOYK2VzSnDhyehU1igWSjbYJ8AoC9OqYfc1VIlBKiXkWtmNteBeJRuYtEAvZrn/A0H62OvYlyZOTSsQJv2kFQmaHLBqQCVTrrdUKYoAmI13zJw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153988; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=hmTEpHQ0w/eNIbQgvG5tMvX+8YZQnrFZvWmxaOfG/ek=; b=jRqHH9zlRvvbl3TT+4d2qeUzobSMpENJ1l6P0TdEGc2AaMUNiLuF2hJbHHkjgz8ZmlfOZH7Alep5VdOEzQEw52/8UGsECU0EKzGKw45UONbzV5JHvIB3Xb7SH26FsqitysuDQMeCQhdAmW67oDEsTJU8hjle/5R4B8P+KnORrxg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153988358204.75315515008128; Wed, 20 Nov 2024 17:53:08 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHy-0001fi-8T; Wed, 20 Nov 2024 20:47:22 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHw-0001ew-EE for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:20 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHu-0004Zh-Hk for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:20 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=hmTEpHQ0w/eNIbQgvG5tMvX+8YZQnrFZvWmxaOfG/ek=; b=ecK28upZ/txRPoV +Ga3BDq7rsdIKfNmW/G3+py6rd1LS0HY6gIjPwCUyqbjjFU0hksgoEATQ7EXCO70oXjeXP6DcOLzi sfb24opEcntdzF3nvgLO0oBks3r5lrE23XmbE1X3tnTD13xa9JJEnQ2ID9U/uaVDLerRwT5fYHGJn gs=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 24/43] helper-to-tcg: PrepareForTcgPass, map TCG globals Date: Thu, 21 Nov 2024 02:49:28 +0100 Message-ID: <20241121014947.18666-25-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153989289116600 Content-Type: text/plain; charset="utf-8" The input LLVM module may define an array of cpu_mapping structs, describing the mapping between fields in a specified struct (usually CPUArchState) and TCG globals. Create a map between offsets into the specified struct and TCG globals (name, size, number of elements, stride) by iterating over the global cpu_mapping array. The name of this array is configurable via the --tcg-global-mappings flag. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/PrepareForTcgPass.h | 7 ++- .../helper-to-tcg/include/TcgGlobalMap.h | 31 +++++++++++++ .../PrepareForTcgPass/PrepareForTcgPass.cpp | 43 +++++++++++++++++++ .../helper-to-tcg/pipeline/Pipeline.cpp | 9 ++-- 4 files changed, 85 insertions(+), 5 deletions(-) create mode 100644 subprojects/helper-to-tcg/include/TcgGlobalMap.h diff --git a/subprojects/helper-to-tcg/include/PrepareForTcgPass.h b/subpro= jects/helper-to-tcg/include/PrepareForTcgPass.h index a41edb4c2e..a731c70b4b 100644 --- a/subprojects/helper-to-tcg/include/PrepareForTcgPass.h +++ b/subprojects/helper-to-tcg/include/PrepareForTcgPass.h @@ -17,11 +17,16 @@ =20 #pragma once =20 +#include "TcgGlobalMap.h" #include =20 class PrepareForTcgPass : public llvm::PassInfoMixin { + TcgGlobalMap &ResultTcgGlobalMap; public: - PrepareForTcgPass() {} + PrepareForTcgPass(TcgGlobalMap &ResultTcgGlobalMap) + : ResultTcgGlobalMap(ResultTcgGlobalMap) + { + } llvm::PreservedAnalyses run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM); }; diff --git a/subprojects/helper-to-tcg/include/TcgGlobalMap.h b/subprojects= /helper-to-tcg/include/TcgGlobalMap.h new file mode 100644 index 0000000000..7186d805ba --- /dev/null +++ b/subprojects/helper-to-tcg/include/TcgGlobalMap.h @@ -0,0 +1,31 @@ +#pragma once + +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include +#include +#include + +struct TcgGlobal { + llvm::StringRef Code; + uint64_t Size; + uint64_t NumElements; + uint64_t Stride; +}; + +using TcgGlobalMap =3D llvm::DenseMap; diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp index a2808eafed..a453aa8558 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp @@ -15,6 +15,7 @@ // along with this program; if not, see . // =20 +#include #include #include #include @@ -71,11 +72,53 @@ inline void demotePhis(Function &F) } } =20 +static void collectTcgGlobals(Module &M, TcgGlobalMap &ResultTcgGlobalMap) +{ + auto *Map =3D M.getGlobalVariable(TcgGlobalMappingsName); + if (!Map) { + return; + } + + // In case the `tcg_global_mappings` array is empty, + // casting to `ConstantArray` will fail, even though it's a + // `[0 x %struct.cpu_tcg_mapping]`. + auto *MapElems =3D dyn_cast(Map->getOperand(0)); + if (!MapElems) { + return; + } + + for (auto Row : MapElems->operand_values()) { + auto *ConstRow =3D cast(Row); + + // Get code string + auto *CodePtr =3D ConstRow->getOperand(0); + auto CodeStr =3D + cast( + cast(CodePtr->getOperand(0))->getOperand(0)) + ->getAsString(); + CodeStr =3D CodeStr.rtrim('\0'); + + // Get offset in cpu env + auto *Offset =3D cast(ConstRow->getOperand(3)); + // Get size of variable in cpu env + auto *SizeInBytes =3D cast(ConstRow->getOperand(4)); + unsigned SizeInBits =3D 8 * SizeInBytes->getLimitedValue(); + + auto *Stride =3D cast(ConstRow->getOperand(5)); + auto *NumElements =3D cast(ConstRow->getOperand(6)); + + ResultTcgGlobalMap[Offset->getLimitedValue()] =3D { + CodeStr, SizeInBits, NumElements->getLimitedValue(), + Stride->getLimitedValue()}; + } +} + PreservedAnalyses PrepareForTcgPass::run(Module &M, ModuleAnalysisManager = &MAM) { removeFunctionsWithLoops(M, MAM); for (Function &F : M) { demotePhis(F); } + collectTcgGlobals(M, ResultTcgGlobalMap); return PreservedAnalyses::none(); } diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index 7d03389439..a8df592af3 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -58,9 +58,9 @@ cl::opt TranslateAllHelpers( // Options for PrepareForTcgPass cl::opt TcgGlobalMappingsName( "tcg-global-mappings", - cl::desc(""), - cl::Required, cl::cat(Cat)); + cl::desc("Name of global cpu_mappings[] used for mapping accesses" + "into a struct to TCG globals"), + cl::init("mappings"), cl::cat(Cat)); =20 // Define a TargetTransformInfo (TTI) subclass, this allows for overriding // common per-llvm-target information expected by other LLVM passes, such @@ -216,7 +216,8 @@ int main(int argc, char **argv) // easily to TCG. // =20 - MPM.addPass(PrepareForTcgPass()); + TcgGlobalMap TcgGlobals; + MPM.addPass(PrepareForTcgPass(TcgGlobals)); MPM.addPass(VerifierPass()); { FunctionPassManager FPM; --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153782; cv=none; d=zohomail.com; s=zohoarc; b=YlWp/J6gPs/KwT3upChpAMPKZ0PE5OW9phTY+gIVhJkBb+C9nf+Loz87gG/QVlrEoSrszqNAm88yHnahZT+fvAa3JIl3E+GHCcu/DdbDaiRKArnyc9yfN/7+I7Dw3UVsgddubfzkprWwVn6pMd8BouRlm7hiWAXFQx1ktj9OfmU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153782; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=AzEcD40MnOYRYmwxWBYqXftTCENUS2p4MXNkow+7IsY=; b=G08LdmtggM4wi7m+usXfzhIAbwY2seAr3gTJTaw51oK5sur7bZiX17zrX5GkPs8teHHIEVUzUxg1KvTWgP907SLtIwRAAjZaaPd4Wa7UXNAmII2JgfniRjzQz7cutPlbEVlWlM10KQQkLnKg4Vr/yEf2SdPwtrxH4ARg2kO0N1Y= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153782284747.3617034853763; Wed, 20 Nov 2024 17:49:42 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwHz-0001g4-Fj; Wed, 20 Nov 2024 20:47:23 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHx-0001fL-Ck for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:21 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHu-0004Zk-RR for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=AzEcD40MnOYRYmwxWBYqXftTCENUS2p4MXNkow+7IsY=; b=VxT7J7rymkkaSqq OBAjPFuLHUihykpcnDSvyDhOwPiP8lboRmcJz+18dZoLQyoQGhUnWRwFU/OuiyX6viSSVP5pM+3h4 wVUVrfmAhdq1pTtODpq2YFGSJRnFawSVHV4tLAvhi2gEMSeOPvX1YsbesIkE9HZqW/1jB352Ae66r CU=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 25/43] helper-to-tcg: PrepareForTcgPass, transform GEPs Date: Thu, 21 Nov 2024 02:49:29 +0100 Message-ID: <20241121014947.18666-26-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153784821116600 Content-Type: text/plain; charset="utf-8" getelementpointer (GEP) instructions in LLVM IR represent general pointer arithmetic (struct field access, array indexing, ...). From the perspective of TCG, three distinct cases are important and are transformed into pseudo instructions respectively: * struct accesses whose offset into the struct map to a TCG global are transformed into "call @AccessGlobalValue(offset)"; * struct accesses whose offset into the struct map to an array of TCG globals are transformed into "call @AccessGlobalArray(offset, index)"; * otherwise converted to general pointer arithmetic in LLVM IR using "call @PtrAdd(...)". These three cases are treated differently in the backend and all other GEPs are considered an error. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/meson.build | 1 + .../PrepareForTcgPass/PrepareForTcgPass.cpp | 4 + .../PrepareForTcgPass/TransformGEPs.cpp | 286 ++++++++++++++++++ .../passes/PrepareForTcgPass/TransformGEPs.h | 37 +++ 4 files changed, 328 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/PrepareForTcgPass/Tran= sformGEPs.cpp create mode 100644 subprojects/helper-to-tcg/passes/PrepareForTcgPass/Tran= sformGEPs.h diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 6db1a019ce..6b18734bad 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -47,6 +47,7 @@ sources =3D [ 'passes/PrepareForOptPass/PrepareForOptPass.cpp', 'passes/PseudoInst.cpp', 'passes/PrepareForTcgPass/PrepareForTcgPass.cpp', + 'passes/PrepareForTcgPass/TransformGEPs.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp index a453aa8558..b1e2932750 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp @@ -17,6 +17,7 @@ =20 #include #include +#include "TransformGEPs.h" #include #include #include @@ -120,5 +121,8 @@ PreservedAnalyses PrepareForTcgPass::run(Module &M, Mod= uleAnalysisManager &MAM) demotePhis(F); } collectTcgGlobals(M, ResultTcgGlobalMap); + for (Function &F : M) { + transformGEPs(M, F, ResultTcgGlobalMap); + } return PreservedAnalyses::none(); } diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/TransformGE= Ps.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/TransformGEPs.c= pp new file mode 100644 index 0000000000..db395533d1 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/TransformGEPs.cpp @@ -0,0 +1,286 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include "TransformGEPs.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace llvm; + +// collectIndices will, given a getelementptr (GEP) instruction, construct= an +// array of GepIndex structs keeping track of the total offset into the st= ruct +// along with some access information. For instance, +// +// struct SubS { +// uint8_t a; +// uint8_t b; +// uint8_t c; +// }; +// +// struct S { +// uint64_t i; +// struct SubS sub[3]; +// }; +// +// void f(struct S *s, int idx) { +// S->sub[idx].a =3D ... +// S->sub[idx].b =3D ... +// S->sub[idx].c =3D ... +// } +// +// would correspond to the following GEPs +// +// getelementptr %struct.S, %struct.S* %s, i64 0, i32 1, %idx, i32 0 +// getelementptr %struct.S, %struct.S* %s, i64 0, i32 1, %idx, i32 1 +// getelementptr %struct.S, %struct.S* %s, i64 0, i32 1, %idx, i32 2 +// +// or the following GepIndex's +// +// GepIndex{Size=3D0,false}, GepIndex{Size=3D8,false}, GepIndex{Size=3D4= ,true}, +// GepIndex{Size=3D0,false} GepIndex{Size=3D0,false}, GepIndex{Size=3D8,= false}, +// GepIndex{Size=3D4,true}, GepIndex{Size=3D1,false} GepIndex{Size=3D0,f= alse}, +// GepIndex{Size=3D8,false}, GepIndex{Size=3D4,true}, GepIndex{Size=3D2,= false} +// + +struct GepIndex { + Value *V; + uint64_t Size; + bool IsArrayAccess =3D false; +}; + +using GepIndices =3D SmallVector; + +static Expected collectIndices(const DataLayout &DL, + GEPOperator *Gep) +{ + Type *PtrOpTy =3D Gep->getPointerOperandType(); + if (!PtrOpTy->isPointerTy()) { + return mkError("GEPs on vectors are not handled!"); + } + Type *InternalTy =3D Type::getIntNTy(Gep->getContext(), 64); + auto *One =3D ConstantInt::get(InternalTy, 1u); + + GepIndices Result; + + // NOTE: LLVM <=3D 11 doesn't have Gep->indices() + Type *CurrentTy =3D PtrOpTy; + for (auto &Arg : make_range(Gep->idx_begin(), Gep->idx_end())) { + switch (CurrentTy->getTypeID()) { + case Type::PointerTyID: { + CurrentTy =3D cast(CurrentTy)->getPointerElementT= ype(); + uint64_t FixedSize =3D DL.getTypeAllocSize(CurrentTy).getFixed= Size(); + Result.push_back(GepIndex{Arg.get(), FixedSize}); + } break; + case Type::ArrayTyID: { + CurrentTy =3D cast(CurrentTy)->getElementType(); + uint64_t FixedSize =3D DL.getTypeAllocSize(CurrentTy).getFixed= Size(); + Result.push_back( + GepIndex{Arg.get(), FixedSize, /* IsArrayAccess=3D */ true= }); + } break; + case Type::StructTyID: { + auto *StructTy =3D cast(CurrentTy); + auto *Constant =3D dyn_cast(Arg.get()); + if (Constant->getBitWidth() > DL.getPointerSizeInBits()) { + return mkError( + "GEP to struct with unsupported index bit width!"); + } + uint64_t ConstantValue =3D Constant->getZExtValue(); + uint64_t ElementOffset =3D + DL.getStructLayout(StructTy)->getElementOffset(ConstantVal= ue); + CurrentTy =3D StructTy->getTypeAtIndex(ConstantValue); + Result.push_back(GepIndex{One, ElementOffset}); + } break; + default: + return mkError("GEP unsupported index type: "); + } + } + + return Result; +} + +// Takes indices associated with a getelementpointer instruction and expan= ds +// it into pointer math. +static void replaceGEPWithPointerMath(Module &M, Instruction *ParentInst, + GEPOperator *Gep, + const GepIndices &Indices) +{ + assert(Indices.size() > 0); + IRBuilder<> Builder(ParentInst); + Value *PtrOp =3D Gep->getPointerOperand(); + + // Sum indices to get the total offset from the base pointer + Value *PrevV =3D nullptr; + for (auto &Index : Indices) { + Value *Mul =3D Builder.CreateMul( + Index.V, ConstantInt::get(Index.V->getType(), Index.Size)); + if (PrevV) { + uint32_t BitWidthLeft =3D + cast(PrevV->getType())->getIntegerBitWidth(); + uint32_t BitWidthRight =3D + cast(Mul->getType())->getIntegerBitWidth(); + if (BitWidthLeft < BitWidthRight) { + PrevV =3D Builder.CreateZExt(PrevV, Mul->getType()); + } else if (BitWidthLeft > BitWidthRight) { + Mul =3D Builder.CreateZExt(Mul, PrevV->getType()); + } + PrevV =3D Builder.CreateAdd(PrevV, Mul); + } else { + PrevV =3D Mul; + } + } + + FunctionCallee Fn =3D pseudoInstFunction( + M, PtrAdd, Gep->getType(), {PtrOp->getType(), PrevV->getType()}); + CallInst *Call =3D Builder.CreateCall(Fn, {PtrOp, PrevV}); + Gep->replaceAllUsesWith(Call); +} + +// Takes indices associated with a getelementpointer instruction and expan= ds +// it into pointer math. +static void replaceGEPWithGlobalAccess(Module &M, Instruction *ParentInst, + GEPOperator *Gep, uint64_t BaseOffs= et, + Value *ArrayIndex) +{ + IRBuilder<> Builder(ParentInst); + Type *IndexTy =3D Type::getIntNTy(M.getContext(), 64); + auto *ConstBaseOffset =3D ConstantInt::get(IndexTy, BaseOffset); + if (ArrayIndex) { + Type *ArrayAccessTy =3D ArrayIndex->getType(); + FunctionCallee Fn =3D pseudoInstFunction( + M, AccessGlobalArray, Gep->getType(), {IndexTy, ArrayAccessTy}= ); + CallInst *Call =3D Builder.CreateCall(Fn, {ConstBaseOffset, ArrayI= ndex}); + Gep->replaceAllUsesWith(Call); + } else { + FunctionCallee Fn =3D + pseudoInstFunction(M, AccessGlobalValue, Gep->getType(), {Inde= xTy}); + CallInst *Call =3D Builder.CreateCall(Fn, {ConstBaseOffset}); + Gep->replaceAllUsesWith(Call); + } +} + +static bool transformGEP(Module &M, const TcgGlobalMap &TcgGlobals, + const GepIndices &Indices, Instruction *ParentIns= t, + GEPOperator *Gep) +{ + Value *PtrOp =3D Gep->getPointerOperand(); + + bool PtrOpIsEnv =3D false; + { + auto *PtrTy =3D cast(PtrOp->getType()); + auto *StructTy =3D dyn_cast(PtrTy->getPointerElementTy= pe()); + // NOTE: We are identifying the CPU state via matching the typenam= e to + // CPUArchState. This is fragile to QEMU name changes, and does not + // play nicely with non-env structs. + PtrOpIsEnv =3D StructTy and StructTy->getName() =3D=3D "struct.CPU= ArchState"; + } + + uint64_t BaseOffset =3D 0; + uint32_t NumArrayAccesses =3D 0; + Value *LastArrayAccess =3D nullptr; + for (const GepIndex &Index : Indices) { + if (Index.IsArrayAccess) { + LastArrayAccess =3D Index.V; + ++NumArrayAccesses; + } else { + auto *Const =3D dyn_cast(Index.V); + if (Const) { + BaseOffset +=3D Const->getZExtValue() * Index.Size; + } + } + } + + if (PtrOpIsEnv) { + auto It =3D TcgGlobals.find(BaseOffset); + if (It !=3D TcgGlobals.end()) { + if (LastArrayAccess && NumArrayAccesses > 1) { + return false; + } + replaceGEPWithGlobalAccess(M, ParentInst, Gep, BaseOffset, + LastArrayAccess); + return !isa(Gep); + } + } + + replaceGEPWithPointerMath(M, ParentInst, Gep, Indices); + return !isa(Gep); +} + +static GEPOperator *getGEPOperator(Instruction *I) +{ + // If the instructions is directly a GEP, simply return it. + auto *GEP =3D dyn_cast(I); + if (GEP) { + return GEP; + } + + // Hard-code handling of GEPs that appear as an inline operand to loads + // and stores. + if (isa(I)) { + auto *Load =3D cast(I); + auto *ConstExpr =3D dyn_cast(Load->getPointerOperand= ()); + if (ConstExpr) { + return dyn_cast(ConstExpr); + } + } else if (isa(I)) { + auto *Store =3D dyn_cast(I); + auto *ConstExpr =3D dyn_cast(Store->getPointerOperan= d()); + if (ConstExpr) { + return dyn_cast(ConstExpr); + } + } + + return nullptr; +} + +void transformGEPs(Module &M, Function &F, const TcgGlobalMap &TcgGlobals) +{ + SmallSet InstToErase; + + for (auto &I : instructions(F)) { + GEPOperator *GEP =3D getGEPOperator(&I); + if (!GEP) { + continue; + } + + Expected Indices =3D collectIndices(M.getDataLayout(),= GEP); + if (!Indices) { + dbgs() << "Failed collecting GEP indices for:\n\t" << I << "\n= "; + dbgs() << "Reason: " << Indices.takeError(); + abort(); + } + + bool ShouldErase =3D transformGEP(M, TcgGlobals, Indices.get(), &I= , GEP); + if (ShouldErase) { + InstToErase.insert(&I); + } + } + + for (auto *I : InstToErase) { + I->eraseFromParent(); + } +} diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/TransformGE= Ps.h b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/TransformGEPs.h new file mode 100644 index 0000000000..11ac9c7e9b --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/TransformGEPs.h @@ -0,0 +1,37 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include "TcgGlobalMap.h" +#include +#include + +// +// Transform of module that converts getelementptr (GEP) operators to +// pseudo instructions: +// - call @AccessGlobalArray(OffsetInEnv, Index) +// if OffsetInEnv is mapped to a global TCGv array. +// +// - call @AccessGlobalValue(OffsetInEnv) +// if OffsetInEnv is mapped to a global TCGv value. +// +// - pointer math, if above fails. +// + +void transformGEPs(llvm::Module &M, llvm::Function &F, + const TcgGlobalMap &TcgGlobals); --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153856; cv=none; d=zohomail.com; s=zohoarc; b=DdCkg55ZNRuCrbAFmL7X46KTMrVdRic9tbnCBhzEK0rV/59ysGlRB1QEgDqTSAjPU/dfptkc6lbEQzQT0ijnKwXLEPnTk9PlISuFZ3g8FH4qYKWYqYmGrTVCRk4nLAW0Kyg6o9++HmiJnJc/M0vk7TINqhLqjHrO+8VnvyulxyA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153856; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=GcVPUJyptch/NiR/8R+sSM2oGaLEowkzsVXIpJeEORM=; b=Jo4udP5+zlYWOr4BonmUKpDGndFzBk5oiSQEGgwbxX0+Q9o9J0kvocmk7NQAWm6EF8mAVMteBINGoCkCaPdI8UDSfj8MuHOMmPsDy65p28u8hmo0WtPRa0Lqb72+1Hz/zLdvK4FtIiaIJWAQnoiffq1xM8tQYX9sFmnpwvQinYc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153856979794.8937051093002; Wed, 20 Nov 2024 17:50:56 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwI1-0001gn-9H; Wed, 20 Nov 2024 20:47:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHz-0001g2-AT for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:23 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHv-0004Zt-HT for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=GcVPUJyptch/NiR/8R+sSM2oGaLEowkzsVXIpJeEORM=; b=Pu1eS6mafjujCyI wgR/meO9r7ViGmRYbXdDJtGFhHt50GFhGETszsi/5jR3pYhqCGYLTNFmKKXhDHsDTgFwL1DsSYsAw GmC0zCSfI0VM9tiRJ9PygYw43krzvwtqPMzKgQ5yt5LrCVUZqzhRZBMXAxu+IEgTCjUrO3EL66++o uw=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 26/43] helper-to-tcg: PrepareForTcgPass, canonicalize IR Date: Thu, 21 Nov 2024 02:49:30 +0100 Message-ID: <20241121014947.18666-27-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153859076116600 Content-Type: text/plain; charset="utf-8" Iterates over the IR with the goal of converting it to a form closer to TCG, taking care of IR disparencies between LLVM and TCG. This also simplifies the backend by containing the bulk of custom IR transformations, meaning the backend can be as dumb as possible. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/meson.build | 1 + .../PrepareForTcgPass/CanonicalizeIR.cpp | 1000 +++++++++++++++++ .../passes/PrepareForTcgPass/CanonicalizeIR.h | 25 + .../PrepareForTcgPass/PrepareForTcgPass.cpp | 2 + 4 files changed, 1028 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/PrepareForTcgPass/Cano= nicalizeIR.cpp create mode 100644 subprojects/helper-to-tcg/passes/PrepareForTcgPass/Cano= nicalizeIR.h diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 6b18734bad..50bb926f49 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -48,6 +48,7 @@ sources =3D [ 'passes/PseudoInst.cpp', 'passes/PrepareForTcgPass/PrepareForTcgPass.cpp', 'passes/PrepareForTcgPass/TransformGEPs.cpp', + 'passes/PrepareForTcgPass/CanonicalizeIR.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/Canonicaliz= eIR.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/CanonicalizeIR= .cpp new file mode 100644 index 0000000000..d53b7b8580 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/CanonicalizeIR.cpp @@ -0,0 +1,1000 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include "CanonicalizeIR.h" +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace llvm; +using namespace PatternMatch; + +// Needed to track and remove instructions not handled by a subsequent dea= d code +// elimination, this applies to calls to pseudo instructions in particular. +// +// TODO: Can we instead make pseudo instructions side effect free via +// attributes? +using EraseInstVec =3D SmallVector; +using UsageCountMap =3D DenseMap; + +// Helper function to remove an instruction only if all uses have been rem= oved. +// This way we can keep track instruction uses without having to modify th= e IR, +// or without having to iterate over all uses everytime we wish to remove = an +// instruction. +static void addToEraseVectorIfUnused(EraseInstVec &InstToErase, + UsageCountMap &UsageMap, Value *V) +{ + auto *I =3D dyn_cast(V); + if (!I) { + return; + } + + // Add V to map if not there + if (UsageMap.count(V) =3D=3D 0) { + UsageMap[V] =3D V->getNumUses(); + } + + // Erase if count reaches zero + if (--UsageMap[V] =3D=3D 0) { + InstToErase.push_back(I); + UsageMap.erase(V); + } +} + +// Forward declarations of IR transformations used in canonicalizing the IR +static void upcastAshr(Instruction *I); +static void convertInsertShuffleToSplat(Module &M, Instruction *I); + +static void simplifyVecBinOpWithSplat(EraseInstVec &InstToErase, + UsageCountMap &UsageMap, Module &M, + BinaryOperator *BinOp); + +static void convertSelectICmp(Module &M, SelectInst *Select, ICmpInst *ICm= p); + +static void convertQemuLoadStoreToPseudoInst(Module &M, CallInst *Call); +static void convertExceptionCallsToPseudoInst(Module &M, CallInst *Call); +static void convertVecStoreBitcastToPseudoInst(EraseInstVec &InstToErase, + Module &M, StoreInst *Store= ); +static void convertICmpBrToPseudInst(LLVMContext &Context, + EraseInstVec &InstToErase, Module &M, + Instruction *I, BasicBlock *NextBb); + +void canonicalizeIR(Module &M) +{ + for (Function &F : M) { + if (F.isDeclaration()) { + continue; + } + + EraseInstVec InstToErase; + UsageCountMap UsageMap; + LLVMContext &Context =3D F.getContext(); + + // Perform a first pass over all instructions in the function and = apply + // IR transformations sequentially. NOTE: order matters here. + for (Instruction &I : instructions(F)) { + if (I.isArithmeticShift()) { + upcastAshr(&I); + } + + convertInsertShuffleToSplat(M, &I); + + // Depends on convertInsertShuffleToSplat for @VecSplat instru= ctions + if (auto *BinOp =3D dyn_cast(&I)) { + simplifyVecBinOpWithSplat(InstToErase, UsageMap, M, BinOp); + } + + // Independent of above + if (auto *ICmp =3D dyn_cast(&I)) { + for (auto *U : ICmp->users()) { + auto *Select =3D dyn_cast(U); + if (Select and Select->getCondition() =3D=3D ICmp) { + convertSelectICmp(M, Select, ICmp); + } + } + } + + // Independent of above, can run at any point + if (auto *Call =3D dyn_cast(&I)) { + convertQemuLoadStoreToPseudoInst(M, Call); + convertExceptionCallsToPseudoInst(M, Call); + } + + // Depends on other vector conversions performed above, needs = to + // run last + if (auto *Store =3D dyn_cast(&I)) { + convertVecStoreBitcastToPseudoInst(InstToErase, M, Store); + } + } + + // Perform a second pass over the instructions. Can be combined wi= th the + // above by using a worklist and making sure we have access to the + // BasicBlock. + // + // Depends on icmp,select -> @movcond + ReversePostOrderTraversal RPOT(&F); + for (auto BbIt =3D RPOT.begin(); BbIt !=3D RPOT.end(); ++BbIt) { + BasicBlock &BB =3D **BbIt; + + auto NextIt =3D BbIt; + BasicBlock *NextBb =3D &**(++NextIt); + + for (Instruction &I : BB) { + convertICmpBrToPseudInst(Context, InstToErase, M, &I, Next= Bb); + } + } + + // Finally clean up instructions we need to remove manually + for (Instruction *I : InstToErase) { + I->eraseFromParent(); + } + } +} + +static Value *upcastInt(IRBuilder<> &Builder, IntegerType *FinalIntTy, Val= ue *V) +{ + if (auto *ConstInt =3D dyn_cast(V)) { + return ConstantInt::get(FinalIntTy, ConstInt->getZExtValue()); + } else { + return Builder.CreateSExt(V, FinalIntTy); + } +} + +// Convert +// +// %2 =3D ashr i[8|16] %1, %0 +// +// to +// +// %2 =3D zext i[8|16] %1 to i32 +// %3 =3D zext i[8|16] %2 to i32 +// %2 =3D ashr i32 %2, %3 +// +static void upcastAshr(Instruction *I) +{ + // Only care about scalar shifts < on less than 32-bit integers + auto *IntTy =3D dyn_cast(I->getType()); + if (!IntTy or IntTy->getBitWidth() >=3D 32) { + return; + } + + IRBuilder<> Builder(I); + + Value *Op1 =3D I->getOperand(0); + Value *Op2 =3D I->getOperand(1); + auto *UpcastIntTy =3D Builder.getInt32Ty(); + Op1 =3D upcastInt(Builder, UpcastIntTy, Op1); + Op2 =3D upcastInt(Builder, UpcastIntTy, Op2); + + auto *AShr =3D Builder.CreateAShr(Op1, Op2); + auto *Trunc =3D Builder.CreateTrunc(AShr, I->getType()); + I->replaceAllUsesWith(Trunc); +} + +// Convert vector intrinsics +// +// %0 =3D insertelement ... +// %1 =3D shuffle ... +// +// to +// +// %0 =3D call @VecSplat.* +// +static void convertInsertShuffleToSplat(Module &M, Instruction *I) +{ + Value *SplatV; + if (match(I, compat_m_Shuffle(compat_m_InsertElt(m_Value(), m_Value(Sp= latV), + m_ZeroInt()), + m_Value(), compat_m_ZeroMask()))) { + + auto *VecTy =3D cast(I->getType()); + + IRBuilder<> Builder(I); + FunctionCallee Fn =3D + pseudoInstFunction(M, VecSplat, VecTy, {SplatV->getType()}); + CallInst *Call =3D Builder.CreateCall(Fn, {SplatV}); + I->replaceAllUsesWith(Call); + } +} + +// Convert +// +// %1 =3D @VecSplat(%0) +// %2 =3D ... op %1 +// +// to +// +// %2 =3D call @Vec[op]Scalar(..., %0) +// +// which more closely matches TCG gvec operations. +static void simplifyVecBinOpWithSplat(EraseInstVec &InstToErase, + UsageCountMap &UsageMap, Module &M, + BinaryOperator *BinOp) +{ + Value *Lhs =3D BinOp->getOperand(0); + Value *Rhs =3D BinOp->getOperand(1); + if (!Lhs->getType()->isVectorTy() or !Rhs->getType()->isVectorTy()) { + return; + } + + // Get splat value from constant or @VecSplat call + Value *SplatValue =3D nullptr; + if (auto *Const =3D dyn_cast(Rhs)) { + SplatValue =3D Const->getSplatValue(); + } else if (auto *Call =3D dyn_cast(Rhs)) { + if (getPseudoInstFromCall(Call) =3D=3D VecSplat) { + SplatValue =3D Call->getOperand(0); + } + } + + if (SplatValue =3D=3D nullptr) { + return; + } + + auto *VecTy =3D cast(Lhs->getType()); + auto *ConstInt =3D dyn_cast(SplatValue); + bool ConstIsNegOne =3D ConstInt and ConstInt->getSExtValue() =3D=3D -1; + bool IsNot =3D BinOp->getOpcode() =3D=3D Instruction::Xor and ConstIsN= egOne; + if (IsNot) { + FunctionCallee Fn =3D pseudoInstFunction(M, VecNot, VecTy, {VecTy}= ); + IRBuilder<> Builder(BinOp); + CallInst *Call =3D Builder.CreateCall(Fn, {Lhs}); + BinOp->replaceAllUsesWith(Call); + } else { + PseudoInst Inst; + switch (BinOp->getOpcode()) { + case Instruction::Add: + Inst =3D VecAddScalar; + break; + case Instruction::Sub: + Inst =3D VecSubScalar; + break; + case Instruction::Mul: + Inst =3D VecMulScalar; + break; + case Instruction::Xor: + Inst =3D VecXorScalar; + break; + case Instruction::Or: + Inst =3D VecOrScalar; + break; + case Instruction::And: + Inst =3D VecAndScalar; + break; + case Instruction::Shl: + Inst =3D VecShlScalar; + break; + case Instruction::LShr: + Inst =3D VecLShrScalar; + break; + case Instruction::AShr: + Inst =3D VecAShrScalar; + break; + default: + abort(); + } + + IRBuilder<> Builder(BinOp); + // Scalar gvec shift operations uses 32-bit scalars, whereas arith= metic + // operations uses 64-bit scalars. + uint32_t SplatSize =3D SplatValue->getType()->getIntegerBitWidth(); + if (BinOp->isShift()) { + if (SplatSize > 32) { + SplatValue =3D + Builder.CreateTrunc(SplatValue, Builder.getInt32Ty()); + } + } else { + if (SplatSize < 64) { + SplatValue =3D + Builder.CreateZExt(SplatValue, Builder.getInt64Ty()); + } + } + FunctionCallee Fn =3D + pseudoInstFunction(M, Inst, VecTy, {VecTy, SplatValue->getType= ()}); + CallInst *Call =3D Builder.CreateCall(Fn, {Lhs, SplatValue}); + BinOp->replaceAllUsesWith(Call); + } + + InstToErase.push_back(BinOp); + addToEraseVectorIfUnused(InstToErase, UsageMap, Rhs); +} + +// Convert +// +// %2 =3D icmp [sgt|ugt|slt|ult] %0, %1 +// %5 =3D select %2, %3, %4 +// +// to +// +// %5 =3D [s|u][max|min] %0, %1 +// +// if possible. Results in cleaner IR, particularly useful for vector +// instructions. +static bool convertSelectICmpToMinMax(Module &M, SelectInst *Select, + ICmpInst *ICmp, ICmpInst::Predicate = &Pred, + Value *ICmpOp0, Value *ICmpOp1, + Value *SelectOp0, Value *SelectOp1) +{ +#if LLVM_VERSION_MAJOR > 11 + if (ICmpOp0 !=3D SelectOp0 or ICmpOp1 !=3D SelectOp1) { + return false; + } + + Intrinsic::ID Intrin; + switch (Pred) { + case ICmpInst::ICMP_SGT: + Intrin =3D Intrinsic::smax; + break; + case ICmpInst::ICMP_UGT: + Intrin =3D Intrinsic::umax; + break; + case ICmpInst::ICMP_SLT: + Intrin =3D Intrinsic::smin; + break; + case ICmpInst::ICMP_ULT: + Intrin =3D Intrinsic::umin; + break; + default: + return false; + } + + auto Ty =3D Select->getType(); + auto MaxMinF =3D Intrinsic::getDeclaration(&M, Intrin, {Ty}); + + IRBuilder<> Builder(Select); + auto Call =3D Builder.CreateCall(MaxMinF, {ICmpOp0, ICmpOp1}); + Select->replaceAllUsesWith(Call); + + return true; +#else + return false; +#endif +} + +// In LLVM, icmp on vectors returns a vector on i1s whereas TCGs gvec_cmp +// returns a vector of the element type of its operands. This can result = in +// some subtle bugs. Convert +// +// icmp -> call @VecCompare +// select -> call @VecWideCondBitsel +// +static bool convertSelectICmpToVecBitsel(Module &M, SelectInst *Select, + ICmpInst *ICmp, + ICmpInst::Predicate &Pred, + Value *ICmpOp0, Value *ICmpOp1, + Value *SelectOp0, Value *SelectOp= 1) +{ + auto *ICmpVecTy =3D dyn_cast(ICmpOp0->getType()); + auto *SelectVecTy =3D dyn_cast(Select->getType()); + if (!ICmpVecTy or !SelectVecTy) { + return false; + } + + Instruction *Cmp =3D ICmp; + { + IRBuilder<> Builder(Cmp); + FunctionCallee Fn =3D + pseudoInstFunction(M, VecCompare, ICmpVecTy, + {Builder.getInt32Ty(), ICmpVecTy, ICmpVecTy= }); + ICmpInst::Predicate Pred =3D ICmp->getPredicate(); + CallInst *Call =3D Builder.CreateCall( + Fn, + {ConstantInt::get(Builder.getInt32Ty(), Pred), ICmpOp0, ICmpOp= 1}); + Cmp =3D Call; + } + + unsigned SrcWidth =3D ICmpVecTy->getElementType()->getIntegerBitWidth(= ); + unsigned DstWidth =3D SelectVecTy->getElementType()->getIntegerBitWidt= h(); + + if (SrcWidth < DstWidth) { + IRBuilder<> Builder(Select); + Value *ZExt =3D Builder.CreateSExt(Cmp, SelectVecTy); + FunctionCallee Fn =3D + pseudoInstFunction(M, VecWideCondBitsel, SelectVecTy, + {SelectVecTy, SelectVecTy, SelectVecTy}); + CallInst *Call =3D Builder.CreateCall(Fn, {ZExt, SelectOp0, Select= Op1}); + Select->replaceAllUsesWith(Call); + } else if (SrcWidth > DstWidth) { + IRBuilder<> Builder(Select); + Value *ZExt =3D Builder.CreateTrunc(Cmp, SelectVecTy); + FunctionCallee Fn =3D + pseudoInstFunction(M, VecWideCondBitsel, SelectVecTy, + {SelectVecTy, SelectVecTy, SelectVecTy}); + CallInst *Call =3D Builder.CreateCall(Fn, {ZExt, SelectOp0, Select= Op1}); + Select->replaceAllUsesWith(Call); + } else { + IRBuilder<> Builder(Select); + FunctionCallee Fn =3D + pseudoInstFunction(M, VecWideCondBitsel, SelectVecTy, + {SelectVecTy, SelectVecTy, SelectVecTy}); + CallInst *Call =3D Builder.CreateCall(Fn, {Cmp, SelectOp0, SelectO= p1}); + Select->replaceAllUsesWith(Call); + } + + return true; +} + +// Convert +// +// %2 =3D icmp [sgt|ugt|slt|ult] %0, %1 +// %5 =3D select %2, %3, %4 +// +// to +// +// 5 =3D call @Movcond.[cond].*(%1, %0, %3, %4) +// +// to more closely match TCG semantics. +static bool convertSelectICmpToMovcond(Module &M, SelectInst *Select, + ICmpInst *ICmp, + ICmpInst::Predicate &Pred, + Value *ICmpOp0, Value *ICmpOp1, + Value *SelectOp0, Value *SelectOp1) +{ + // We only handle integers, we have no movcond equivalent in gvec + auto *IntTy =3D dyn_cast(Select->getType()); + if (!IntTy) { + return false; + } + + // If the type of the comparison does not match the return type of the + // select statement, we cannot do anything so skip + if (ICmpOp0->getType() !=3D IntTy) { + return false; + } + + IRBuilder<> Builder(Select); + if (cast(ICmpOp0->getType())->getBitWidth() < + IntTy->getBitWidth()) { + if (ICmp->isSigned(Pred)) { + ICmpOp0 =3D Builder.CreateSExt(ICmpOp0, IntTy); + ICmpOp1 =3D Builder.CreateSExt(ICmpOp1, IntTy); + } else { + ICmpOp0 =3D Builder.CreateZExt(ICmpOp0, IntTy); + ICmpOp1 =3D Builder.CreateZExt(ICmpOp1, IntTy); + } + } + + // Create @Movcond.[slt|...].* function + FunctionCallee Fn =3D pseudoInstFunction(M, Movcond, IntTy, + {IntTy, IntTy, IntTy, IntTy, In= tTy}); + CallInst *Call =3D + Builder.CreateCall(Fn, {ConstantInt::get(IntTy, Pred), ICmpOp0, IC= mpOp1, + SelectOp0, SelectOp1}); + Select->replaceAllUsesWith(Call); + + return true; +} + +// Specialize +// +// %2 =3D icmp [sgt|ugt|slt|ult] %0, %1 +// %5 =3D select %2, %3, %4 +// +// to either maximum/minimum, vector operations matching TCG, or a conditi= onal +// move that also matches TCG in sematics. +static void convertSelectICmp(Module &M, SelectInst *Select, ICmpInst *ICm= p) +{ + // Given + // %2 =3D icmp [sgt|ugt|slt|ult] %0, %1 + // %5 =3D select %2, %3, %4 + assert(Select->getCondition() =3D=3D ICmp); + Value *ICmpOp0 =3D ICmp->getOperand(0); + Value *ICmpOp1 =3D ICmp->getOperand(1); + Value *SelectOp0 =3D Select->getTrueValue(); + Value *SelectOp1 =3D Select->getFalseValue(); + ICmpInst::Predicate Pred =3D ICmp->getPredicate(); + + // First try to convert to min/max + // %5 =3D [s|u][max|min] %0, %1 + if (convertSelectICmpToMinMax(M, Select, ICmp, Pred, ICmpOp0, ICmpOp1, + SelectOp0, SelectOp1)) { + return; + } + + // Secondly try convert icmp -> @VecCompare, select -> @VecWideCondBit= sel + if (convertSelectICmpToVecBitsel(M, Select, ICmp, Pred, ICmpOp0, ICmpO= p1, + SelectOp0, SelectOp1)) { + return; + } + + // If min/max and vector conversion failed we fallback to a movcond + // %5 =3D call @Movcond.[cond].*(%1, %0, %3, %4) + convertSelectICmpToMovcond(M, Select, ICmp, Pred, ICmpOp0, ICmpOp1, + SelectOp0, SelectOp1); +} + +// Convert QEMU guest loads/stores represented by calls such as +// +// call cpu_ldub*(), +// call cpu_stb*(), +// +// and friends, to pseudo instructions +// +// %5 =3D call @GuestLoad.*(%addr, %sign, %size, %endian); +// %5 =3D call @GuestStore.*(%addr, %value, %size, %endian); +// +// Makes the backend agnostic to what instructions or calls are used to +// represent loads and stores. +static void convertQemuLoadStoreToPseudoInst(Module &M, CallInst *Call) +{ + Function *F =3D Call->getCalledFunction(); + StringRef Name =3D F->getName(); + if (Name.consume_front("cpu_")) { + bool IsLoad =3D Name.consume_front("ld"); + bool IsStore =3D !IsLoad and Name.consume_front("st"); + if (IsLoad or IsStore) { + bool Signed =3D !Name.consume_front("u"); + uint8_t Size =3D 0; + switch (Name[0]) { + case 'b': + Size =3D 1; + break; + case 'w': + Size =3D 2; + break; + case 'l': + Size =3D 4; + break; + case 'q': + Size =3D 8; + break; + default: + abort(); + } + + uint8_t Endianness =3D 0; // unknown + if (Size > 1) { + Name =3D Name.drop_front(2); + switch (Name[0]) { + case 'l': + Endianness =3D 1; + break; + case 'b': + Endianness =3D 2; + break; + default: + abort(); + } + } + + IRBuilder<> Builder(Call); + Value *AddrOp =3D Call->getArgOperand(1); + IntegerType *AddrTy =3D cast(AddrOp->getType()); + IntegerType *FlagTy =3D Builder.getInt8Ty(); + Value *SizeOp =3D ConstantInt::get(FlagTy, Size); + Value *EndianOp =3D ConstantInt::get(FlagTy, Endianness); + CallInst *NewCall; + if (IsLoad) { + Value *SignOp =3D ConstantInt::get(FlagTy, Signed); + IntegerType *RetTy =3D cast(Call->getType()); + FunctionCallee Fn =3D pseudoInstFunction( + M, GuestLoad, RetTy, {AddrTy, FlagTy, FlagTy, FlagTy}); + NewCall =3D + Builder.CreateCall(Fn, {AddrOp, SignOp, SizeOp, Endian= Op}); + } else { + Value *ValueOp =3D Call->getArgOperand(2); + IntegerType *ValueTy =3D cast(ValueOp->getTyp= e()); + FunctionCallee Fn =3D + pseudoInstFunction(M, GuestStore, Builder.getVoidTy(), + {AddrTy, ValueTy, FlagTy, FlagTy}); + NewCall =3D + Builder.CreateCall(Fn, {AddrOp, ValueOp, SizeOp, Endia= nOp}); + } + Call->replaceAllUsesWith(NewCall); + } + } +} + +// Convert QEMU exception calls +// +// call raise_exception_ra(...), +// ... +// +// to a pseudo instruction +// +// %5 =3D call @Exception.*(...); +// +// Makes the backend agnostic to what instructions or calls are used to +// represent exceptions, and the list of sources can be expanded here. +static void convertExceptionCallsToPseudoInst(Module &M, CallInst *Call) +{ + Function *F =3D Call->getCalledFunction(); + StringRef Name =3D F->getName(); + // NOTE: expand as needed + if (Name =3D=3D "raise_exception_ra") { + IRBuilder<> Builder(Call); + Value *Op0 =3D Call->getArgOperand(0); + Value *Op1 =3D Call->getArgOperand(1); + FunctionCallee Fn =3D + pseudoInstFunction(M, Exception, Builder.getVoidTy(), + {Op0->getType(), Op1->getType()}); + CallInst *NewCall =3D Builder.CreateCall(Fn, {Op0, Op1}); + Call->replaceAllUsesWith(NewCall); + } +} + +// +// Following functions help with converting between different types of +// instructions to pseudo instructions, particularly ones that write +// to a pointer, aka the Vec*Store pseudo instructions +// + +static PseudoInst instructionToStorePseudoInst(unsigned Opcode) +{ + switch (Opcode) { + case Instruction::Trunc: + return VecTruncStore; + case Instruction::ZExt: + return VecZExtStore; + case Instruction::SExt: + return VecSExtStore; + case Instruction::Select: + return VecSelectStore; + case Instruction::Add: + return VecAddStore; + case Instruction::Sub: + return VecSubStore; + case Instruction::Mul: + return VecMulStore; + case Instruction::Xor: + return VecXorStore; + case Instruction::Or: + return VecOrStore; + case Instruction::And: + return VecAndStore; + case Instruction::Shl: + return VecShlStore; + case Instruction::LShr: + return VecLShrStore; + case Instruction::AShr: + return VecAShrStore; + default: + abort(); + } +} + +static PseudoInst pseudoInstToStorePseudoInst(PseudoInst Inst) +{ + switch (Inst) { + case VecNot: + return VecNotStore; + case VecAddScalar: + return VecAddScalarStore; + case VecSubScalar: + return VecSubScalarStore; + case VecMulScalar: + return VecMulScalarStore; + case VecXorScalar: + return VecXorScalarStore; + case VecOrScalar: + return VecOrScalarStore; + case VecAndScalar: + return VecAndScalarStore; + case VecShlScalar: + return VecShlScalarStore; + case VecLShrScalar: + return VecLShrScalarStore; + case VecAShrScalar: + return VecAShrScalarStore; + case VecWideCondBitsel: + return VecWideCondBitselStore; + default: + abort(); + } +} + +static PseudoInst intrinsicToStorePseudoInst(unsigned IntrinsicID) +{ + switch (IntrinsicID) { + case Intrinsic::sadd_sat: + return VecSignedSatAddStore; + case Intrinsic::ssub_sat: + return VecSignedSatSubStore; + case Intrinsic::fshr: + return VecFunnelShrStore; +#if LLVM_VERSION_MAJOR > 11 + case Intrinsic::abs: + return VecAbsStore; + case Intrinsic::smax: + return VecSignedMaxStore; + case Intrinsic::umax: + return VecUnsignedMaxStore; + case Intrinsic::smin: + return VecSignedMinStore; + case Intrinsic::umin: + return VecUnsignedMinStore; +#endif + case Intrinsic::ctlz: + return VecCtlzStore; + case Intrinsic::cttz: + return VecCttzStore; + case Intrinsic::ctpop: + return VecCtpopStore; + default: + abort(); + } +} + +// For binary/unary ops on vectors where the result is stored to a +// pointer +// +// %3 =3D %1 [op] %2 +// %4 =3D bitcast i8* %0 to * +// store %3, * %4 +// +// to +// +// call @Vec[Op]Store.*(%0, %1, %2) +// +// This deals with the duality of pointers and vectors, and +// simplifies the backend. We previously kept a map on the +// side to propagate "vector"-ness from %3 to %4 via the store, +// no longer! +static void convertVecStoreBitcastToPseudoInst(EraseInstVec &InstToErase, + Module &M, StoreInst *Store) +{ + Value *ValueOp =3D Store->getValueOperand(); + Type *ValueTy =3D ValueOp->getType(); + if (!ValueTy->isVectorTy()) { + return; + } + auto *Bitcast =3D cast(Store->getPointerOperand()); + Type *PtrTy =3D Bitcast->getType(); + // Ensure store and binary op. are in the same basic + // block since the op. is moved to the store. + bool InSameBB =3D + cast(ValueOp)->getParent() =3D=3D Store->getParent(); + if (!InSameBB) { + return; + } + + SmallVector Types; + SmallVector Args; + Value *PtrOp =3D Store->getPointerOperand(); + if (auto *BinOp =3D dyn_cast(ValueOp)) { + Instruction *Inst =3D cast(ValueOp); + PseudoInst NewInst =3D instructionToStorePseudoInst(BinOp->getOpco= de()); + IRBuilder<> Builder(Store); + const unsigned ArgCount =3D pseudoInstArgCount(NewInst); + // Add one to account for extra store pointer + // argument of Vec*Store pseudo instructions. + assert(ArgCount > 0 and ArgCount - 1 <=3D Inst->getNumOperands()); + Types.push_back(PtrTy); + Args.push_back(PtrOp); + for (unsigned I =3D 0; I < ArgCount - 1; ++I) { + Value *Op =3D Inst->getOperand(I); + Types.push_back(Op->getType()); + Args.push_back(Op); + } + FunctionCallee Fn =3D + pseudoInstFunction(M, NewInst, Builder.getVoidTy(), Types); + Builder.CreateCall(Fn, Args); + } else if (auto *Call =3D dyn_cast(ValueOp)) { + Function *F =3D Call->getCalledFunction(); + PseudoInst OldInst =3D getPseudoInstFromCall(Call); + if (OldInst !=3D InvalidPseudoInst) { + // Map scalar vector pseudo instructions to + // store variants + PseudoInst NewInst =3D pseudoInstToStorePseudoInst(OldInst); + IRBuilder<> Builder(Store); + Types.push_back(PtrTy); + Args.push_back(PtrOp); + for (Value *Op : Call->args()) { + Types.push_back(Op->getType()); + Args.push_back(Op); + } + FunctionCallee Fn =3D + pseudoInstFunction(M, NewInst, Builder.getVoidTy(), Types); + Builder.CreateCall(Fn, Args); + } else if (F->isIntrinsic()) { + Instruction *Inst =3D cast(ValueOp); + PseudoInst NewInst =3D + intrinsicToStorePseudoInst(F->getIntrinsicID()); + const unsigned ArgCount =3D pseudoInstArgCount(NewInst); + // Add one to account for extra store pointer + // argument of Vec*Store pseudo instructions. + assert(ArgCount > 0 and ArgCount - 1 <=3D Inst->getNumOperands= ()); + IRBuilder<> Builder(Store); + SmallVector ArgTys; + SmallVector Args; + ArgTys.push_back(PtrTy); + Args.push_back(PtrOp); + for (unsigned I =3D 0; I < ArgCount - 1; ++I) { + Value *Op =3D Inst->getOperand(I); + ArgTys.push_back(Op->getType()); + Args.push_back(Op); + } + FunctionCallee Fn =3D + pseudoInstFunction(M, NewInst, Builder.getVoidTy(), ArgTys= ); + Builder.CreateCall(Fn, Args); + } else { + dbgs() << "Uhandled vector + bitcast + store op. " << *ValueOp + << "\n"; + abort(); + } + } else { + Instruction *Inst =3D cast(ValueOp); + PseudoInst NewInst =3D instructionToStorePseudoInst(Inst->getOpcod= e()); + const unsigned ArgCount =3D pseudoInstArgCount(NewInst); + // Add one to account for extra store pointer + // argument of Vec*Store pseudo instructions. + assert(ArgCount > 0 and ArgCount - 1 <=3D Inst->getNumOperands()); + IRBuilder<> Builder(Store); + SmallVector ArgTys; + SmallVector Args; + ArgTys.push_back(PtrTy); + Args.push_back(PtrOp); + for (unsigned I =3D 0; I < ArgCount - 1; ++I) { + Value *Op =3D Inst->getOperand(I); + ArgTys.push_back(Op->getType()); + Args.push_back(Op); + } + FunctionCallee Fn =3D + pseudoInstFunction(M, NewInst, Builder.getVoidTy(), ArgTys); + Builder.CreateCall(Fn, Args); + } + + // Remove store instruction, this ensures DCE + // can cleanup the rest, we also remove ValueOp + // here since it's a call and won't get cleaned + // by DCE. + InstToErase.push_back(cast(ValueOp)); + InstToErase.push_back(Store); +} + +// +// Convert +// +// %cond =3D icmp [cond] i32 %0, i32 %1 +// br i1 %cond, label %true, label %false +// +// to +// +// call void @brcond.[cond].i32(i32 %0, i32 %1, label %true.exit, +// label %false) br i1 %cond, label %true, label %false !dead-branch +// +// note the old branch still remains as @brcond.* is not an actual +// branch instruction. Removing the old branch would result in broken +// IR. +// +// Additionally if the %false basic block immediatly succeeds the +// current one, we can ignore the false branch and fallthrough, this is +// indicated via !fallthrough metadata on the call. +// +// TODO: Consider using a ConstantInt i1 arguments instead. Metadata is +// fragile and does not survive optimization. We do not run any more +// optimization passes, but this could be a source of future headache. +static void convertICmpBrToPseudInst(LLVMContext &Context, + EraseInstVec &InstToErase, Module &M, + Instruction *I, BasicBlock *NextBb) +{ + auto *ICmp =3D dyn_cast(I); + if (!ICmp) { + return; + } + + // Since we want to remove the icmp instruction we ensure that + // all uses are branch instructions that can be converted into + // @brcond.* calls. + for (User *U : ICmp->users()) { + if (!isa(U)) { + return; + } + } + + Value *Op0 =3D ICmp->getOperand(0); + Value *Op1 =3D ICmp->getOperand(1); + auto *CmpIntTy =3D dyn_cast(Op0->getType()); + if (!CmpIntTy) { + return; + } + for (User *U : ICmp->users()) { + auto *Br =3D cast(U); + + BasicBlock *True =3D Br->getSuccessor(0); + BasicBlock *False =3D Br->getSuccessor(1); + + bool TrueUnreachable =3D + True->getTerminator()->getOpcode() =3D=3D Instruction::Unreach= able and + False->getTerminator()->getOpcode() !=3D Instruction::Unreacha= ble; + + // If the next basic block is either of our true/false + // branches, we can fallthrough instead of branching. + bool Fallthrough =3D (NextBb =3D=3D True or NextBb =3D=3D False); + + // If the succeeding basic block is the true branch we + // invert the condition so we can !fallthrough instead. + ICmpInst::Predicate Predicate; + if (NextBb =3D=3D True or (TrueUnreachable and NextBb =3D=3D False= )) { + std::swap(True, False); + Predicate =3D ICmp->getInversePredicate(); + } else { + Predicate =3D ICmp->getPredicate(); + } + + IRBuilder<> Builder(Br); + FunctionCallee Fn =3D pseudoInstFunction( + M, Brcond, Builder.getVoidTy(), + {CmpIntTy, CmpIntTy, CmpIntTy, True->getType(), False->getType= ()}); + CallInst *Call =3D Builder.CreateCall( + Fn, {ConstantInt::get(CmpIntTy, Predicate), Op0, Op1, True, Fa= lse}); + + if (Fallthrough) { + MDTuple *N =3D MDNode::get(Context, MDString::get(Context, "")= ); + Call->setMetadata("fallthrough", N); + } + + // + // We need to keep the BB of the true branch alive + // so that we can iterate over the CFG as usual + // using LLVM. Or custom "opcode" @brcond is not an + // actual branch, so LLVM does not understand that + // we can branch to the true branch. + // + // For this reason we emit an extra dead branch + // to the true branch, and tag it as dead using + // metadata. The backend can later check that if + // this metadata is present and ignore the branch. + // + // Another idea: + // What we could do instead is to + // linearize the CFG before this point, i.e. + // establish the order we want to emit all BBs + // in, in say an array. We can then iterate + // over this array instead, note this can only + // happen in the later stages of the pipeline + // where we don't rely on LLVM for any extra work. + // + // Keeping our own linear array would also allow + // us to optimize brconds for fallthroughs, e.g. + // check if any of the basic blocks we branch to + // is the next basic block, and if so we can adjust + // the condition accordingly. + // (We do this currently, but this assumes the + // iteration order here is the same as in the + // backend.) + // + // Note also: LLVM expectects the BB to end in a single + // branch. + // + BranchInst *DeadBranch =3D + Builder.CreateCondBr(ConstantInt::getFalse(Context), True, Fal= se); + { + MDTuple *N =3D MDNode::get(Context, MDString::get(Context, "")= ); + DeadBranch->setMetadata("dead-branch", N); + } + + InstToErase.push_back(Br); + } + InstToErase.push_back(ICmp); +} diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/Canonicaliz= eIR.h b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/CanonicalizeIR.h new file mode 100644 index 0000000000..441200606d --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/CanonicalizeIR.h @@ -0,0 +1,25 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +namespace llvm +{ +class Module; +} + +void canonicalizeIR(llvm::Module &M); diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp index b1e2932750..7fdbc2a0c9 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp @@ -15,6 +15,7 @@ // along with this program; if not, see . // =20 +#include "CanonicalizeIR.h" #include #include #include "TransformGEPs.h" @@ -124,5 +125,6 @@ PreservedAnalyses PrepareForTcgPass::run(Module &M, Mod= uleAnalysisManager &MAM) for (Function &F : M) { transformGEPs(M, F, ResultTcgGlobalMap); } + canonicalizeIR(M); return PreservedAnalyses::none(); } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153763; cv=none; d=zohomail.com; s=zohoarc; b=Bz5ghrWGi2utOKGVTUiV54adPRFBnJaAh4S6PDs5r4aOiJ50DzCmsyb5gE53E+HuBElrVhoXcAJ13iUzmNmdidlnmILzHs91U6IIAZOFXJrFwnlZ3ImI/MNVvZWaIHOP7+SlYBPikjyiwuC/snq6ZvLOl0YNdfrkF43WWVp8XM0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153763; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=MxJZlENkdmUchmgfGBkk9kv4bmuX6oYA3cdwhm+q3rc=; b=Bs78IrDVW4DSpk5nuLT+QhQfxYwwQGTjTu9yvAwPFLPQoPnd5WFGYReyxs0o+5Va9HRrPn8Zkro6hCKGgRi1DLxoR+pZOIMu6bGX27QLWt0A0T/00w6D/1MD9ZZ3D7ZPrGN0OlDEDszsFj3+wlHoNjEFK/gD3qGGJ76VWYA6VRE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153763852256.515423778631; Wed, 20 Nov 2024 17:49:23 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwI0-0001gi-QQ; Wed, 20 Nov 2024 20:47:24 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHy-0001fj-9O for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:22 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHw-0004Zz-BC for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=MxJZlENkdmUchmgfGBkk9kv4bmuX6oYA3cdwhm+q3rc=; b=hq2y0jcrCKtVUJ6 jmSO+GqWFQJR8gyvoJMP6GPMgxeJTNZDSLuRxR7IS4Uz+6VVuiN6dc5uJ3RjsA8BL1LQvKyleaOtR WocUTOxAPXSFW9MaBiobApnG1GxuZHXaQheOOyLaoWVchROCGQbOmzJsJPbDrj3H2lrn7s41G/il3 YQ=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 27/43] helper-to-tcg: PrepareForTcgPass, identity map trivial expressions Date: Thu, 21 Nov 2024 02:49:31 +0100 Message-ID: <20241121014947.18666-28-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153764461116600 Content-Type: text/plain; charset="utf-8" Transformation of the IR, identity mapping trivial expressions which would amount to nothing more than a move when emitted as TCG, but is required in LLVM IR to not break the IR. Trivial expressions are mapped to a @IdentityMap pseudo instruction allowing them to be dealt with in a uniform manner down the line. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/meson.build | 1 + .../passes/PrepareForTcgPass/IdentityMap.cpp | 80 +++++++++++++++++++ .../passes/PrepareForTcgPass/IdentityMap.h | 39 +++++++++ .../PrepareForTcgPass/PrepareForTcgPass.cpp | 4 + 4 files changed, 124 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/PrepareForTcgPass/Iden= tityMap.cpp create mode 100644 subprojects/helper-to-tcg/passes/PrepareForTcgPass/Iden= tityMap.h diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 50bb926f49..09caa74c63 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -49,6 +49,7 @@ sources =3D [ 'passes/PrepareForTcgPass/PrepareForTcgPass.cpp', 'passes/PrepareForTcgPass/TransformGEPs.cpp', 'passes/PrepareForTcgPass/CanonicalizeIR.cpp', + 'passes/PrepareForTcgPass/IdentityMap.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/IdentityMap= .cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/IdentityMap.cpp new file mode 100644 index 0000000000..b173aeba9c --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/IdentityMap.cpp @@ -0,0 +1,80 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include "IdentityMap.h" +#include +#include "backend/TcgType.h" +#include +#include +#include +#include +#include + +using namespace llvm; + +void identityMap(Module &M, Function &F) +{ + SmallVector InstToErase; + + for (auto &I : instructions(F)) { + auto *ZExt =3D dyn_cast(&I); + if (ZExt) { + auto *IntTy0 =3D + dyn_cast(ZExt->getOperand(0)->getType()); + auto *IntTy1 =3D dyn_cast(ZExt->getType()); + if (IntTy0 and IntTy1) { + uint32_t LlvmSize0 =3D IntTy0->getBitWidth(); + uint32_t LlvmSize1 =3D IntTy1->getBitWidth(); + + if (LlvmSize0 =3D=3D 1) { + auto *ICmp =3D dyn_cast(ZExt->getOperand(0)); + if (ICmp) { + auto *ICmpOp =3D ICmp->getOperand(0); + LlvmSize0 =3D + cast(ICmpOp->getType())->getBitWi= dth(); + } + } + + uint32_t TcgSize0 =3D llvmToTcgSize(LlvmSize0); + uint32_t TcgSize1 =3D llvmToTcgSize(LlvmSize1); + + if (TcgSize0 =3D=3D TcgSize1) { + FunctionCallee Fn =3D + pseudoInstFunction(M, IdentityMap, IntTy1, {IntTy0= }); + IRBuilder<> Builder(&I); + CallInst *Call =3D + Builder.CreateCall(Fn, {ZExt->getOperand(0)}); + ZExt->replaceAllUsesWith(Call); + InstToErase.push_back(&I); + } + } + } else if (isa(&I)) { + auto *IntTy0 =3D dyn_cast(I.getOperand(0)->getTyp= e()); + auto *IntTy1 =3D dyn_cast(I.getType()); + FunctionCallee Fn =3D + pseudoInstFunction(M, IdentityMap, IntTy1, {IntTy0}); + IRBuilder<> Builder(&I); + CallInst *Call =3D Builder.CreateCall(Fn, {I.getOperand(0)}); + I.replaceAllUsesWith(Call); + InstToErase.push_back(&I); + } + } + + for (auto *I : InstToErase) { + I->eraseFromParent(); + } +} diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/IdentityMap= .h b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/IdentityMap.h new file mode 100644 index 0000000000..b0c938c25d --- /dev/null +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/IdentityMap.h @@ -0,0 +1,39 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include +#include + +// +// Transformation of the IR, taking what would become trivial unary operat= ions +// and maps them to a single @IdentityMap pseudo instruction. +// +// To motivate further, in order to produce nice IR on the other end, gene= rally +// the operands of these trivial expressions needs to be forwarded and tre= ated +// as the destination value (identity mapped). However, directly removing= these +// instructions will result in broken LLVM IR (consider zext i8, i32 where= both +// the source and destination would map to TCGv_i32). +// +// Moreover, handling these identity mapped values in an adhoc way quickly +// becomes cumbersome and spreads throughout the codebase. Therefore, +// introducing @IdentityMap allows code further down the pipeline to ignor= e the +// source of the identity map. +// + +void identityMap(llvm::Module &M, llvm::Function &F); diff --git a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForT= cgPass.cpp index 7fdbc2a0c9..3e4713d837 100644 --- a/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp +++ b/subprojects/helper-to-tcg/passes/PrepareForTcgPass/PrepareForTcgPass.= cpp @@ -17,6 +17,7 @@ =20 #include "CanonicalizeIR.h" #include +#include "IdentityMap.h" #include #include "TransformGEPs.h" #include @@ -126,5 +127,8 @@ PreservedAnalyses PrepareForTcgPass::run(Module &M, Mod= uleAnalysisManager &MAM) transformGEPs(M, F, ResultTcgGlobalMap); } canonicalizeIR(M); + for (Function &F : M) { + identityMap(M, F); + } return PreservedAnalyses::none(); } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153716; cv=none; d=zohomail.com; s=zohoarc; b=gWNXrIsk1KCtVkVRrIK7weiUxGtJV2rpy6jdGILxg7WIAceYLMi8X4Ac+lZHbRX1Y44Vr+sCGlNzjkvy1w4Wm4z7Wsvr8JkXGAXChl0/jZ9Gq3BdUrITKhFQPp6iXs0DdEpFn0b6WpIn9eBrRtPO+3Grfh7a7J46EUWWk1XxPdY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153716; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=7ZVMsM/ZVxwvDxZo9mEMb4JOOHxBreG1gQEY2DhLD5k=; b=Yow2zQtR9ZLSfF8FWUGPmBJovftEJ6M1Wir760Lc8I4Bj+AarNcqM+zlMLQCQPZWhcCDCcLZNRJfYUak5Hj8QLZ73CmkxdrrZu7O0JHQfk+iFLIZ30MDgnEyoyF7F0Fws7GtS6kNbYOuD67NMLnMl+EaBjHuX24bPx0x1uE7Nuw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153716750505.4075842379664; Wed, 20 Nov 2024 17:48:36 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwI2-0001hW-RZ; Wed, 20 Nov 2024 20:47:26 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwI1-0001hN-TM for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:25 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHx-0004a4-0V for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:25 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=7ZVMsM/ZVxwvDxZo9mEMb4JOOHxBreG1gQEY2DhLD5k=; b=mi4LEcTf4Dsrcx6 ggzJOR2I1xRbZtZOs2+355YFKE5JF4CoFu5q0yWnrkIzDSSxV8PZGJIoWShfXu+koccD91N+/1bEy +LTGiRrG5Gl/J2R4OF+MfqvZRFJ5EAmRs9Ef3FpDjGK/oAVHSy/mkViSKKlpMy8f3S2Xp7xWem0/T Fk=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 28/43] helper-to-tcg: Introduce TcgType.h Date: Thu, 21 Nov 2024 02:49:32 +0100 Message-ID: <20241121014947.18666-29-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153718342116600 Content-Type: text/plain; charset="utf-8" Adds a struct representing everything a LLVM value might map to in TCG, this includes: * TCGv (IrValue); * TCGv_ptr (IrPtr); * TCGv_env (IrEnv); * TCGLabel (IrLabel); * tcg_constant_*() (IrConst); * 123123ull (IrImmediate); * intptr_t gvec_vector (IrPtrToOffset). NOTE: Patch is subject to change due to rework of the TcgV type system. There is quite significant overlap in handling IrConst/IrImmediate and any other type with the ConstantExpression bool set. Space required for each TcgV can also be reduced by moving to a union. Signed-off-by: Anton Johansson --- .../helper-to-tcg/passes/backend/TcgType.h | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/backend/TcgType.h diff --git a/subprojects/helper-to-tcg/passes/backend/TcgType.h b/subprojec= ts/helper-to-tcg/passes/backend/TcgType.h new file mode 100644 index 0000000000..36ebdbe5cb --- /dev/null +++ b/subprojects/helper-to-tcg/passes/backend/TcgType.h @@ -0,0 +1,133 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include +#include + +#include +#include +#include + +enum TcgVKind : uint8_t { + IrValue, + IrConst, + IrEnv, + IrImmediate, + IrPtr, + IrPtrToOffset, + IrLabel, +}; + +// Counter incremented for every TcgV created, also used in the creation of +// unique names (e.g. varr_10 for an array). +extern uint32_t VarIndex; + +struct TcgV { + uint16_t Id; + std::string Name; + + uint32_t TcgSize; + uint8_t LlvmSize; + uint8_t VectorElementCount; + + TcgVKind Kind; + + bool ConstantExpression =3D false; + + static TcgV makeVector(uint32_t VectorWidthBits, uint32_t ElementWidth= Bits, + uint32_t ElementCount) + { + return TcgV("", VectorWidthBits, ElementWidthBits, ElementCount, + IrPtrToOffset); + } + + static TcgV makeImmediate(llvm::StringRef Name, uint32_t TcgWidth, + uint32_t LlvmWidth) + { + return TcgV(Name.str(), TcgWidth, LlvmWidth, 1, IrImmediate); + } + + static TcgV makeTemp(uint32_t TcgWidth, uint32_t LlvmWidth, TcgVKind K= ind) + { + return TcgV("", TcgWidth, LlvmWidth, 1, Kind); + } + + static TcgV makeConstantExpression(llvm::StringRef Expression, + uint32_t TcgWidth, uint32_t LlvmWid= th, + TcgVKind Kind) + { + TcgV Tcg(Expression.str(), TcgWidth, LlvmWidth, 1, Kind); + Tcg.ConstantExpression =3D true; + return Tcg; + } + + static TcgV makeLabel() { return TcgV("", 32, 32, 1, IrLabel); } + + TcgV(std::string Name, uint32_t TcgSize, uint32_t LlvmSize, + uint32_t VectorElementCount, TcgVKind Kind) + : Id(VarIndex++), Name(Name), TcgSize(TcgSize), LlvmSize(LlvmSize), + VectorElementCount(VectorElementCount), Kind(Kind) + { + assert(verifySize()); + } + + // We make the following assumptions about TcgSize and LLvmSize: + // - TcgSize either 32- or 64-bit; + // - LlvmSize either 1-,8-,16-,32-,64-,or 128-bit. + // We also assume that there are only these valid combinations of + // (TcgSize, LlvmSize): + // - (64, 64) uint64_t + // - (64, 1) bool + // - (32, 32) uint32_t + // - (32, 16) uint16_t + // - (32, 8) uint8_t + // - (32, 1) bool + // So we try to fit the variables in the smallest possible TcgSize, + // with the exception of booleans which need to able to be 64-bit + // when dealing with conditions. + bool verifySize() + { + return (LlvmSize =3D=3D 1 || LlvmSize =3D=3D 8 || LlvmSize =3D=3D = 16 || + LlvmSize =3D=3D 32 || LlvmSize =3D=3D 64) && + (LlvmSize <=3D TcgSize); + } + + bool operator=3D=3D(const TcgV &Other) const { return Other.Id =3D=3D = Id; } + bool operator!=3D(const TcgV &Other) const { return !operator=3D=3D(Ot= her); } +}; + +inline uint64_t llvmToTcgSize(uint64_t LlvmSize) +{ + return (LlvmSize <=3D 32) ? 32 : 64; +} + +inline uint32_t vectorSizeInBytes(const TcgV &Vec) +{ + assert(Vec.Kind =3D=3D IrPtrToOffset); + return Vec.LlvmSize * Vec.VectorElementCount / 8; +} + +struct TcgBinOp { + std::string Code; +}; + +struct TcgVecBinOp { + std::string Code; + llvm::Optional RequiredOp2Size; +}; --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153793; cv=none; d=zohomail.com; s=zohoarc; b=BRKQUmNVKEXEVPbjQ4JZdWttggUNTJqo+Z9rSdH2jWGuG2XX/U+zIN9pyNbAok9olHzyvAoi2BD+z2ko8UIqcUTxOo6WsF2rwS9ktKUACiful0HH94jDb4PBpYhrtBzqQWBCIMUlL6nHl80zNXVXxIecnYKIgVaEzzNw8XSGzOQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153793; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=l7AFrdw6Z5v8pKLgQV4E0S1CKxSIr+1atnVYTuO0AmQ=; b=IjLPulbF3t+K0kgps+1hd6HDNFvvVJjlBvroLJc8Od0lLLJu/rHOnJyIcqmpqxcOLyGdEMEXtmVEQellQ505h5Six+1uAnuP084sr8EVfFCaE6Wa05j8zib/Vyas0BYjJXc3OLdrX9UPPtDaTztElH4JwEc5Slf7zWwhXLzYAM8= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 173215379338382.77535558137515; Wed, 20 Nov 2024 17:49:53 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwI2-0001hV-E3; Wed, 20 Nov 2024 20:47:26 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwI1-0001gk-6O for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:25 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwHx-0004aC-OZ for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:47:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=l7AFrdw6Z5v8pKLgQV4E0S1CKxSIr+1atnVYTuO0AmQ=; b=Uk1rkfoGtu0uxGu fUeAp9b6F9c6NIFrJ2WNUjf48zF9mk55+/KEUbGqSu2XbcLpWKB217uwsbRYhVO+dXCE9rkOn+Pq2 z1CgPIeLjCjeSMs3MmSlGdQXe1vF4xnLJawCTQswQtaKIV8UtnV6TvXVcB4OX2pcbyf0mMTCribMC ok=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 29/43] helper-to-tcg: Introduce TCG register allocation Date: Thu, 21 Nov 2024 02:49:33 +0100 Message-ID: <20241121014947.18666-30-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153794557116600 Content-Type: text/plain; charset="utf-8" Based on the assumption of a cycle free IR, this commit adds a simple register allocator for emitted values in TCG. The goal of this pass is to reduce the number of temporaries required in the output code, which is especially important when dealing with gvec vectors as to not require very large amounts of temporary storage in CPUArchState. For each LLVM value in the IR, the allocator will assign a struct TcgV reprensenting a variable in the output TCG. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/CmdLineOptions.h | 2 + subprojects/helper-to-tcg/meson.build | 1 + .../passes/backend/TcgTempAllocationPass.cpp | 594 ++++++++++++++++++ .../passes/backend/TcgTempAllocationPass.h | 79 +++ .../helper-to-tcg/pipeline/Pipeline.cpp | 6 + 5 files changed, 682 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/backend/TcgTempAllocat= ionPass.cpp create mode 100644 subprojects/helper-to-tcg/passes/backend/TcgTempAllocat= ionPass.h diff --git a/subprojects/helper-to-tcg/include/CmdLineOptions.h b/subprojec= ts/helper-to-tcg/include/CmdLineOptions.h index 9553e26407..a5e467f8be 100644 --- a/subprojects/helper-to-tcg/include/CmdLineOptions.h +++ b/subprojects/helper-to-tcg/include/CmdLineOptions.h @@ -25,3 +25,5 @@ extern llvm::cl::list InputFiles; extern llvm::cl::opt TranslateAllHelpers; // Options for PrepareForTcgPass extern llvm::cl::opt TcgGlobalMappingsName; +// Options for TcgTempAllocation +extern llvm::cl::opt GuestPtrSize; diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 09caa74c63..ad3c307b6b 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -50,6 +50,7 @@ sources =3D [ 'passes/PrepareForTcgPass/TransformGEPs.cpp', 'passes/PrepareForTcgPass/CanonicalizeIR.cpp', 'passes/PrepareForTcgPass/IdentityMap.cpp', + 'passes/backend/TcgTempAllocationPass.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/backend/TcgTempAllocationPass= .cpp b/subprojects/helper-to-tcg/passes/backend/TcgTempAllocationPass.cpp new file mode 100644 index 0000000000..3ee679ec02 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/backend/TcgTempAllocationPass.cpp @@ -0,0 +1,594 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include "TcgTempAllocationPass.h" +#include "PseudoInst.h" +#include "backend/TcgEmit.h" +#include "llvm-compat.h" +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace llvm; + +// Type to represent a list of free TcgV's that can be reused when +// we need a new temporary. Exists for the duration of a function, +// and is expected to be small <=3D 8 free TcgV's at any time. +// +// This justifies the type being an array, since iteration times to +// find a free element will be small. +using FreeListVector =3D SmallVector; + +// Finds the first TcgV in FreeList with a matching TcgSize and Kind +// Iterates over the FreeList to find a TcgV with matching TcgSize and Kind +static Optional findFreeTcgV(FreeListVector &FreeList, uint32_t TcgS= ize, + TcgVKind Kind) +{ + for (size_t i =3D 0; i < FreeList.size(); ++i) { + if (FreeList[i].TcgSize =3D=3D TcgSize and FreeList[i].Kind =3D=3D= Kind) { + TcgV Tcg =3D FreeList[i]; + // Swap-remove + FreeList[i] =3D FreeList.back(); + FreeList.pop_back(); + return Tcg; + } + } + return None; +} + +// +// Functions for mapping an LLVM Value to a TcgV +// + +// Provides a C string representation of a ConstantInt +static std::string constantIntToStr(const ConstantInt *C) +{ + SmallString<20> ResultStr; + auto *Int =3D cast(C); + const APInt Value =3D Int->getUniqueInteger(); + const char *SuffixStr =3D ""; + const bool Negative =3D Int->isNegative(); + if (Value.ugt(UINT32_MAX) or C->getBitWidth() =3D=3D 64) { + SuffixStr =3D Int->isNegative() ? "ll" : "ull"; + } + if (Int->getBitWidth() =3D=3D 1) { + ResultStr =3D (Value.getBoolValue()) ? "true" : "false"; + } else { + bool IsMax =3D (Negative) ? Value.isMaxSignedValue() : Value.isMax= Value(); + bool IsMin =3D Negative and Value.isMinSignedValue(); + unsigned Bitwidth =3D Value.getBitWidth(); + if (IsMax) { + return Twine("INT").concat(Twine(Bitwidth)).concat("_MAX").str= (); + } else if (IsMin) { + return Twine("INT").concat(Twine(Bitwidth)).concat("_MIN").str= (); + } else { + Value.toString(ResultStr, 10, Value.isNegative(), true); + } + } + return Twine(ResultStr).concat(SuffixStr).str(); +} + +// Given an integer LLVM value assign it to a TcgV, either by creating a n= ew +// one or finding a suitable one on the FreeList +static Expected mapInteger(TempAllocationData &TAD, + FreeListVector &FreeList, const Value *V) +{ + auto *Ty =3D cast(V->getType()); + + uint32_t LlvmSize =3D Ty->getBitWidth(); + if (LlvmSize > 64) { + return mkError("Bit widths > 64 are not supported: ", V); + } + + if (isa(V)) { + // Constant integer + auto Tcg =3D TcgV::makeImmediate(constantIntToStr(cast(V)), + llvmToTcgSize(LlvmSize), LlvmSize); + return TAD.map(V, Tcg); + } else if (isa(V)) { + // Argument + uint32_t TcgSize =3D llvmToTcgSize(LlvmSize); + auto ArgInfoIt =3D TAD.Args.ArgInfoMap.find(V); + if (ArgInfoIt !=3D TAD.Args.ArgInfoMap.end() and + ArgInfoIt->second =3D=3D ArgImmediate) { + auto Tcg =3D TcgV::makeImmediate(tcg::mkName("i"), TcgSize, Ll= vmSize); + return TAD.map(V, Tcg); + } else { + auto Tcg =3D TcgV::makeTemp(TcgSize, LlvmSize, IrValue); + return TAD.map(V, Tcg); + } + } else { + // Non-constant integer + uint32_t TcgSize =3D 0; + auto *ICmp =3D dyn_cast(V); + if (ICmp) { + // icmp's return i1's and are used as either 32-bit or 64-bit = TCGv + // in QEMU. Assume the TcgSize from operands. + assert(LlvmSize =3D=3D 1); + auto *IntTy0 =3D + dyn_cast(ICmp->getOperand(0)->getType()); + if (!IntTy0) { + return mkError("Icmp on non-integer type"); + } + TcgSize =3D llvmToTcgSize(IntTy0->getBitWidth()); + } else { + // Normal integer, get the TcgSize from the LlvmSize as for + // constants + TcgSize =3D llvmToTcgSize(LlvmSize); + } + + Optional Tcg =3D findFreeTcgV(FreeList, TcgSize, IrValue); + if (Tcg) { + // Found a TcgV of the corresponding TcgSize, update LlvmSize + Tcg->LlvmSize =3D LlvmSize; + return TAD.map(V, *Tcg); + } else { + // Otherwise, create a new value + const auto Tcg =3D TcgV::makeTemp(TcgSize, LlvmSize, IrValue); + return TAD.map(V, Tcg); + } + } +} + +// Given an vector LLVM value assign it to a TcgV, either by creating a new +// one or finding a suitable one on the FreeList. Special care is taken to +// map individual elements of constant vectors. +static Expected mapVector(TempAllocationData &TAD, + FreeListVector &FreeList, const Value *V, + VectorType *VecTy) +{ + auto *IntTy =3D dyn_cast(VecTy->getElementType()); + if (!IntTy) { + return mkError("Vectors of non-integer element type not supported!= \n"); + } + uint32_t ElementCount =3D compat::getVectorElementCount(VecTy); + uint32_t ElementWidth =3D IntTy->getBitWidth(); + + if (ElementWidth =3D=3D 1) { + return mkError("Invalid vector width"); + } + + auto *ICmp =3D dyn_cast(V); + if (ICmp) { + auto *VecTy =3D cast(ICmp->getOperand(0)->getType()); + auto *IntTy =3D cast(VecTy->getElementType()); + ElementWidth =3D IntTy->getBitWidth(); + } + + uint32_t VectorWidth =3D ElementCount * ElementWidth; + + // Create or find a TcgV + Optional Tcg =3D findFreeTcgV(FreeList, VectorWidth, IrPtrToOffs= et); + if (Tcg) { + Tcg->LlvmSize =3D ElementWidth; + Tcg->VectorElementCount =3D ElementCount; + } else { + Tcg =3D TcgV::makeVector(VectorWidth, ElementWidth, ElementCount); + } + + // For constant vectors, make sure all individual elements are mapped + auto *Const =3D dyn_cast(V); + if (Const) { + // Make sure all arguments being splatted are mapped + Constant *Splat =3D Const->getSplatValue(); + if (Splat) { + // Map single splatted value + // <32 x i32> + // or, + // <32 x i32> + assert(mapInteger(TAD, FreeList, Splat)); + } else { + // Map constant elements of vector where elements differ + // <32 x i32> + for (unsigned I =3D 0; I < Tcg->VectorElementCount; ++I) { + Value *V =3D Const->getAggregateElement(I); + assert(mapInteger(TAD, FreeList, V)); + } + } + } + + return TAD.map(V, *Tcg); +} + +// Given an pointer LLVM value assign it to a TcgV, either by creating a n= ew +// one or finding a suitable one on the FreeList. NOTE: Pointers may be m= apped +// to env via comparison with TempAllocationData::EnvPtr. +static Expected mapPointer(TempAllocationData &TAD, + FreeListVector &FreeList, const Value *V) +{ + auto *Ty =3D cast(V->getType()); + auto *ElTy =3D Ty->getPointerElementType(); + if (isa(V)) { + auto ArgInfoIt =3D TAD.Args.ArgInfoMap.find(V); + if (ArgInfoIt !=3D TAD.Args.ArgInfoMap.end() and + ArgInfoIt->second =3D=3D ArgPtrToOffset) { + const auto Tcg =3D TcgV::makeVector(GuestPtrSize, GuestPtrSize= , 1); + return TAD.map(V, Tcg); + } else { + auto IsEnv =3D (TAD.Args.EnvPtr.hasValue() && *TAD.Args.EnvPtr= =3D=3D V); + const auto Tcg =3D TcgV::makeTemp(GuestPtrSize, GuestPtrSize, + IsEnv ? IrEnv : IrPtr); + return TAD.map(V, Tcg); + } + } else if (isa(V)) { + // `alloca`s represent stack variables in LLVM IR and return + // pointers, we can simply map them to `IrValue`s + auto *IntTy =3D dyn_cast(ElTy); + if (!IntTy) { + return mkError("alloca with unsupported type: ", V); + } + + const uint32_t LlvmBitWidth =3D IntTy->getBitWidth(); + if (LlvmBitWidth > 64) { + return mkError("alloca with unsupported size: ", V); + } + const uint32_t TcgBitWidth =3D llvmToTcgSize(LlvmBitWidth); + + // find or create a new IrValue + Optional Tcg =3D findFreeTcgV(FreeList, TcgBitWidth, IrValue= ); + if (Tcg) { + return TAD.map(V, *Tcg); + } else { + const auto Tcg =3D TcgV::makeTemp(TcgBitWidth, LlvmBitWidth, I= rValue); + return TAD.map(V, Tcg); + } + } else if (isa(ElTy)) { + return mapVector(TAD, FreeList, V, cast(ElTy)); + } else { + // Otherwise, find or create a new IrPtr of the target pointer size + Optional Tcg =3D findFreeTcgV(FreeList, GuestPtrSize, IrPtr); + if (Tcg) { + return TAD.map(V, *Tcg); + } else { + auto Tcg =3D TcgV::makeTemp(GuestPtrSize, GuestPtrSize, IrPtr); + return TAD.map(V, Tcg); + } + } + + return mkError("Unable to map constant ", V); +} + +// Given a LLVM value, assigns a TcgV by type (integer, pointer, vector). = If +// the given value has already been mapped to a TcgV, return it. +static Expected mapValue(TempAllocationData &Data, + FreeListVector &FreeList, const Value *V) +{ + // Return previously mapped value + auto It =3D Data.Map.find(V); + if (It !=3D Data.Map.end()) { + return It->second; + } + + Type *Ty =3D V->getType(); + if (isa(Ty)) { + return mapInteger(Data, FreeList, V); + } else if (isa(Ty)) { + return mapPointer(Data, FreeList, V); + } else if (isa(Ty)) { + return mapVector(Data, FreeList, V, cast(Ty)); + } + + return mkError("Unable to map value ", V); +} + +static bool shouldSkipInstruction(const Instruction *const I, + bool SkipReturnMov) +{ + // Skip returns if we're skipping return mov's + if (isa(I) and SkipReturnMov) { + return true; + } + // Skip assertions + auto Call =3D dyn_cast(I); + if (!Call) { + return false; + } + Function *F =3D Call->getCalledFunction(); + if (!F) { + return false; + } + StringRef Name =3D F->getName(); + return (Name =3D=3D "__assert_fail" or Name =3D=3D "g_assertion_messag= e_expr" or + isa(I) or isa(I)); +} + +static bool shouldSkipValue(const Value *const V) +{ + return (isa(V) or isa(V) or isa= (V)); +} + +// Wrapper function to extract operands from GEP, call, +// and other instruction +static const iterator_range +getOperands(const Instruction *const I) +{ + switch (I->getOpcode()) { + case Instruction::GetElementPtr: + return cast(I)->operands(); + case Instruction::Call: + return cast(I)->args(); + default: + return I->operands(); + } +} + +// A mapping of the return TCG variable to the value RetV is valid +// if no use of an argument is found between the use of value (where +// IBegin/BbBegin starts) and it's definition +// use of an argument is found between the old mapping +// and the new. +static bool isRetMapValid(Arguments &Args, + po_iterator BbBegin, + po_iterator BbEnd, + BasicBlock::const_reverse_iterator IBegin, + BasicBlock::const_reverse_iterator IEnd, + const Value *RetV) +{ + auto BbIt =3D BbBegin; + auto IIt =3D IBegin; + + do { + do { + if (cast(&*IIt) =3D=3D RetV) { + return true; + } + + for (auto &V : getOperands(&*IIt)) { + if (isa(V) and Args.ArgInfoMap[V] !=3D ArgImmedi= ate) { + return false; + } + } + } while (++IIt !=3D IEnd); + + ++BbIt; + IEnd =3D (*BbIt)->rend(); + IIt =3D (*BbIt)->rbegin(); + } while (BbIt !=3D BbEnd); + + return false; +} + +Expected +allocateTemporaries(const Function &F, const AnnotationMapTy &Annotations) +{ + TempAllocationData Data; + FreeListVector FreeList; + + assert(!F.isDeclaration()); + + // Use function annotation data to force type of arguments + auto It =3D Annotations.find(&F); + if (It !=3D Annotations.end()) { + for (const Annotation &Ann : It->second) { + ArgumentKind Kind; + switch (Ann.Kind) { + case AnnotationKind::HelperToTcg: + continue; + case AnnotationKind::Immediate: + Kind =3D ArgImmediate; + break; + case AnnotationKind::PtrToOffset: + Kind =3D ArgPtrToOffset; + break; + default: + abort(); + } + + for (uint32_t i : Ann.ArgIndices) { + assert(i < F.arg_size()); + Data.Args.ArgInfoMap[F.getArg(i)] =3D Kind; + } + } + } + + for (const Argument &Arg : F.args()) { + // Check if argument corresponds to Env, if so set the special + // EnvPtr field. + auto Ptr =3D dyn_cast(Arg.getType()); + if (Ptr) { + auto *Struct =3D dyn_cast(Ptr->getPointerElementTy= pe()); + // TODO: Identifying Env in this way is a bit fragile to name + // changes in QEMU, and assumes any non-QEMU code will still a= dopt + // the CPUArchState naming convention. Better is to handle all + // pointer-to-struct args as env. + if (Struct and Struct->getName() =3D=3D "struct.CPUArchState")= { + assert(!Data.Args.EnvPtr.hasValue()); + Data.Args.EnvPtr =3D &Arg; + } + } + + // If we didn't force an argument kind via annotations, assume Arg= Temp + if (Data.Args.ArgInfoMap.find(&Arg) =3D=3D Data.Args.ArgInfoMap.en= d()) { + Data.Args.ArgInfoMap[&Arg] =3D ArgTemp; + } + + Data.Args.Args.insert(&Arg); + } + + // The PrepareForOptPass removes all functions with non-int/void return + // types, assert this assumption. + Type *RetTy =3D F.getReturnType(); + assert(isa(RetTy) or RetTy->isVoidTy()); + // Map integer return values + if (auto IntTy =3D dyn_cast(RetTy)) { + Data.ReturnValue =3D TcgV::makeTemp(llvmToTcgSize(IntTy->getBitWid= th()), + IntTy->getBitWidth(), IrValue); + } + + // Begin/end iterators over basic blocks in the function. Used for che= cking + // that the initial return value map is valid and later used for itera= ting + // over basic blocks. + auto ItBbBegin =3D po_begin(&F); + auto ItBbEnd =3D po_end(&F); + + // Skip mov's to return value if possible, results of previous + // instructions might have been assigned the return value. + // + // This is possible if: + // 1. The return value is not an argument. + // 2. The return value is not a constant. + // 3. No use of an argument has occured after the definition of the + // value being returned. + { + const Instruction &I =3D *ItBbBegin->rbegin(); + + auto Ret =3D dyn_cast(&I); + if (Ret and Ret->getNumOperands() =3D=3D 1) { + Value *RetV =3D Ret->getReturnValue(); + bool ValidRetV =3D !isa(RetV) and !isa(= RetV); + if (ValidRetV and isRetMapValid(Data.Args, ItBbBegin, ItBbEnd, + (*ItBbBegin)->rbegin(), + (*ItBbBegin)->rend(), RetV)) { + Data.Map.try_emplace(RetV, *Data.ReturnValue); + Data.SkipReturnMov =3D true; + } + } + } + + // Iterate over instructions in reverse and try to allocate TCG variab= les. + // + // The algorithm is very straight forward, we keep a FreeList of TCG + // variables we can reuse. Variables are allocated on first use and + // "freed" on definition. + // + // We allow reuse of the return TCG variable in order to save one vari= able + // and skip the return mov if possible. Since source and return varia= bles + // can overlap, when take the conservative route and only allow reuse = of + // the return variable if no arguments have been used. + + bool SeenArgUse =3D false; + + for (auto ItBb =3D ItBbBegin; ItBb !=3D ItBbEnd; ++ItBb) { + const BasicBlock *BB =3D *ItBb; + // Loop over instructions in the basic block in reverse + for (auto IIt =3D BB->rbegin(), IEnd =3D BB->rend(); IIt !=3D IEnd= ; ++IIt) { + const Instruction &I =3D *IIt; + if (shouldSkipInstruction(&I, Data.SkipReturnMov)) { + continue; + } + + // For calls to the identity mapping pseudo instruction + // we simply want to propagate the type allocated for the resu= lt of + // the call to the operand. + if (isa(&I)) { + auto *Call =3D cast(&I); + PseudoInst Inst =3D getPseudoInstFromCall(Call); + if (Inst =3D=3D IdentityMap) { + Value *Arg =3D Call->getArgOperand(0); + Expected Tcg =3D mapValue(Data, FreeList, Arg); + assert(Tcg); + + auto It =3D Data.Map.find(cast(&I)); + assert(It !=3D Data.Map.end()); + uint8_t LlvmSize =3D It->second.LlvmSize; + It->second =3D Tcg.get(); + It->second.LlvmSize =3D LlvmSize; + continue; + } + } + + // Check if we've encountered any non-immediate argument yet + for (const Use &U : getOperands(&I)) { + if (isa(U) and + Data.Args.ArgInfoMap[U] !=3D ArgImmediate) { + SeenArgUse =3D true; + } + } + + // Free up variables as they are defined, iteration is in post= order + // meaning uses of vars always occur before definitions. + bool IsArg =3D Data.Args.ArgInfoMap.find(cast(&I)) !=3D + Data.Args.ArgInfoMap.end(); + auto It =3D Data.Map.find(cast(&I)); + if (!IsArg and It !=3D Data.Map.end() and + !cast(&I)->getType()->isVoidTy()) { + TcgV &Tcg =3D It->second; + switch (Tcg.Kind) { + case IrValue: + case IrPtr: + case IrPtrToOffset: + FreeList.push_back(Tcg); + break; + case IrConst: + case IrEnv: + case IrImmediate: + break; + default: + abort(); + } + } + + // Loop over operands and assign TcgV's. On first encounter of= a + // given operand we assign a new TcgV from the FreeList. + for (const Use &V : getOperands(&I)) { + auto It =3D Data.Map.find(V); + if (It !=3D Data.Map.end() or shouldSkipValue(V)) { + continue; + } + + Expected Tcg =3D mapValue(Data, FreeList, V); + if (!Tcg) { + return Tcg.takeError(); + } + + // If our value V got mapped to the return value, + // make sure the mapping is valid + // + // A mapping to the return value is valid as long as + // an argument has not been used. This is to prevent clob= bering + // in the case that arguments and the return value overlap. + if (Data.ReturnValue.hasValue() and *Tcg =3D=3D *Data.Retu= rnValue) { + bool Valid =3D + isRetMapValid(Data.Args, ItBb, ItBbEnd, IIt, IEnd,= V); + if (!SeenArgUse and Valid) { + continue; + } + + // The mapping was not valid, erase it and assign a ne= w one + Data.Map.erase(V); + Expected Tcg =3D mapValue(Data, FreeList, V); + if (!Tcg) { + return Tcg.takeError(); + } + } + } + } + } + + // The above only maps arguments that are actually used, make a final = pass + // over the arguments to map unused and immediate arguments. + for (auto V : Data.Args.Args) { + Expected Arg =3D mapValue(Data, FreeList, V); + if (!Arg) { + return Arg.takeError(); + } + } + + return Data; +} diff --git a/subprojects/helper-to-tcg/passes/backend/TcgTempAllocationPass= .h b/subprojects/helper-to-tcg/passes/backend/TcgTempAllocationPass.h new file mode 100644 index 0000000000..fff60d1ff6 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/backend/TcgTempAllocationPass.h @@ -0,0 +1,79 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include "FunctionAnnotation.h" +#include "backend/TcgType.h" + +#include +#include +#include +#include + +// +// TcgTempAllocationPass +// +// Analysis over the IR that performs basic register allocation to assign +// identifiers representing TCGv's to all values in a given function. +// +// Note: Input code is assumed to be loop free, which drastically simplifi= es +// the register allocation. This assumption is reasonable as we expect code +// with loops to be either unrolled or vectorized, and we currently don't = emit +// for loops in C. +// +// This pass also contains the logic for mapping various LLVM values to a = TcgV +// struct, which is necessary in order to figure out what type we need for= in +// TCG. +// + +namespace llvm +{ +class Function; +} + +enum ArgumentKind { + ArgTemp, + ArgImmediate, + ArgPtrToOffset, +}; + +struct Arguments { + llvm::Optional EnvPtr; + llvm::DenseMap ArgInfoMap; + llvm::SmallSet Args; +}; + +struct TempAllocationData { + // Mapping of LLVM Values to the corresponding TcgV + llvm::DenseMap Map; + + // Whether or not the final mov in an instruction can safely + // be ignored or not. + bool SkipReturnMov =3D false; + llvm::Optional ReturnValue; + Arguments Args; + + inline TcgV map(const llvm::Value *V, const TcgV &T) + { + return Map.try_emplace(V, T).first->second; + } +}; + +llvm::Expected +allocateTemporaries(const llvm::Function &F, + const AnnotationMapTy &Annotations); diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index a8df592af3..b933a7bb1a 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -62,6 +62,12 @@ cl::opt TcgGlobalMappingsName( "into a struct to TCG globals"), cl::init("mappings"), cl::cat(Cat)); =20 +// Options for TcgTempAllocation +cl::opt + GuestPtrSize("guest-ptr-size", + cl::desc("Pointer size of the guest architecture"), + cl::init(32), cl::cat(Cat)); + // Define a TargetTransformInfo (TTI) subclass, this allows for overriding // common per-llvm-target information expected by other LLVM passes, such // as the width of the largest scalar/vector registers. Needed for consis= tent --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153863; cv=none; d=zohomail.com; s=zohoarc; b=VXDb2SFZ8D9kt0BlMQORGy2ei2tJvp6F6eyVTiehVvHQrkXftXZ+vibMF2p5I4cTDxOFmkrzRWBypd7EmJecGzsvFbIdGFDwpI1PRi4VokeAbM5le24fm64BdCrIErNb9FsmlKGX310oPbYfj1atySj1APCh2nKITBjAjfBAAIo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153863; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=uh839ebPB0g/yC4m1c+7zP4XxQLEQWTUp71oIl5bLVM=; b=cxL4U2IMbZxuEUXcrZFYshXLF3n1JXPnIkeL3NiQa8RvIVshq0lwIBYMu6B2K+4eyjpKhP490b+qFENQlh9gNSCaGr+TB8WyiYHYjnwL8DDdvVq+HYYu3n8Py5Zonv0Nfxg621uK+VbXmNNgRX61f0Ciy5fsAAGxI17ptXQ6ZkM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153863201484.7377241025248; Wed, 20 Nov 2024 17:51:03 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJy-0006yc-TR; Wed, 20 Nov 2024 20:49:26 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJr-0006fh-9r for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:19 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJm-0004re-Pq for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=uh839ebPB0g/yC4m1c+7zP4XxQLEQWTUp71oIl5bLVM=; b=CZLvMx2KenhHvkQ ozw0L2U86jJwRaiUbXevs6mFTMuwhX3btgPKPtJNY55EhT5TXiopCmwMfCRfum9etCE7gBk4R6ddS XyF9fB2xta5AmWQzCzTAbLLJ7RkK5DsrPAbQL7BppeDNmF/Ai4z/8rPZatnqCj5QHoYaPLEdBoMsA ag=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 30/43] helper-to-tcg: TcgGenPass, introduce TcgEmit.[cpp|h] Date: Thu, 21 Nov 2024 02:49:34 +0100 Message-ID: <20241121014947.18666-31-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153864885116600 Content-Type: text/plain; charset="utf-8" A new translation unit is added with the purpose of containing all code which emits strings of TCG code. The idea is that maintainence of the backend will be simpler if all "tcg_*(*)" strings are contained and wrapped in functions. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/CmdLineOptions.h | 3 + subprojects/helper-to-tcg/meson.build | 1 + .../helper-to-tcg/passes/backend/TcgEmit.cpp | 1074 +++++++++++++++++ .../helper-to-tcg/passes/backend/TcgEmit.h | 290 +++++ .../helper-to-tcg/pipeline/Pipeline.cpp | 13 + 5 files changed, 1381 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/backend/TcgEmit.cpp create mode 100644 subprojects/helper-to-tcg/passes/backend/TcgEmit.h diff --git a/subprojects/helper-to-tcg/include/CmdLineOptions.h b/subprojec= ts/helper-to-tcg/include/CmdLineOptions.h index a5e467f8be..f59b700914 100644 --- a/subprojects/helper-to-tcg/include/CmdLineOptions.h +++ b/subprojects/helper-to-tcg/include/CmdLineOptions.h @@ -27,3 +27,6 @@ extern llvm::cl::opt TranslateAllHelpers; extern llvm::cl::opt TcgGlobalMappingsName; // Options for TcgTempAllocation extern llvm::cl::opt GuestPtrSize; +// Options for TcgEmit +extern llvm::cl::opt MmuIndexFunction; +extern llvm::cl::opt TempVectorBlock; diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index ad3c307b6b..55a177bd94 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -51,6 +51,7 @@ sources =3D [ 'passes/PrepareForTcgPass/CanonicalizeIR.cpp', 'passes/PrepareForTcgPass/IdentityMap.cpp', 'passes/backend/TcgTempAllocationPass.cpp', + 'passes/backend/TcgEmit.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/backend/TcgEmit.cpp b/subproj= ects/helper-to-tcg/passes/backend/TcgEmit.cpp new file mode 100644 index 0000000000..6e3b6bdbd0 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/backend/TcgEmit.cpp @@ -0,0 +1,1074 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include "TcgEmit.h" +#include "CmdLineOptions.h" +#include "backend/TcgType.h" + +using namespace llvm; + +// Counters used for prettier numbers in names +uint32_t VarIndex =3D 0; +bool EmittedVectorMem =3D false; + +namespace tcg +{ + +// Constant used to represent the MMU INDEX for all memory operations. +// get_tb_mmu_index is a function assumed to be defined by the target. +static const TcgV MmuIndex =3D + TcgV::makeImmediate(MmuIndexFunction + "(tcg_ctx->gen_tb->flags)", 32,= 32); + +void resetNameIndices() +{ + VarIndex =3D 0; + EmittedVectorMem =3D false; +} + +// TODO: do we still have manual makename calls? +const std::string mkName(const std::string Suffix) +{ + return Twine("v") + .concat(Suffix) + .concat("_") + .concat(Twine(VarIndex++)) + .str(); +} + +const std::string getType(const TcgV &Value) +{ + switch (Value.Kind) { + case IrValue: + case IrConst: + return Twine("TCGv_i").concat(Twine(Value.TcgSize)).str(); + case IrEnv: + return "TCGv_env"; + case IrImmediate: + if (Value.LlvmSize =3D=3D 1) { + return "bool"; + } else { + return Twine("int") + .concat(Twine((int)Value.LlvmSize)) + .concat("_t") + .str(); + } + case IrPtr: + return "TCGv_ptr"; + case IrPtrToOffset: + return "intptr_t"; + case IrLabel: + return "TCGLabel *"; + default: + abort(); + } +} + +inline StringRef mapPredicate(const CmpInst::Predicate &Pred) +{ + switch (Pred) { + case CmpInst::ICMP_EQ: + return "TCG_COND_EQ"; + case CmpInst::ICMP_NE: + return "TCG_COND_NE"; + case CmpInst::ICMP_UGT: + return "TCG_COND_GTU"; + case CmpInst::ICMP_UGE: + return "TCG_COND_GEU"; + case CmpInst::ICMP_ULT: + return "TCG_COND_LTU"; + case CmpInst::ICMP_ULE: + return "TCG_COND_LEU"; + case CmpInst::ICMP_SGT: + return "TCG_COND_GT"; + case CmpInst::ICMP_SGE: + return "TCG_COND_GE"; + case CmpInst::ICMP_SLT: + return "TCG_COND_LT"; + case CmpInst::ICMP_SLE: + return "TCG_COND_LE"; + default: + abort(); + } +} + +static std::string mapBinOp(const Instruction::BinaryOps &Opcode, + const TcgV &Src0, const TcgV &Src1) +{ + const bool IsImmediate =3D + (Src0.Kind =3D=3D IrImmediate or Src1.Kind =3D=3D IrImmediate); + const bool IsPtr =3D (Opcode =3D=3D Instruction::Add and + (Src0.Kind =3D=3D IrPtr or Src1.Kind =3D=3D IrPtr)= ); + assert(IsImmediate or Src0.TcgSize =3D=3D Src1.TcgSize); + std::string Expr =3D ""; + llvm::raw_string_ostream ExprStream(Expr); + + // Check for valid boolean operations if operating on a boolean + if (Src0.LlvmSize =3D=3D 1) { + assert(Src1.LlvmSize =3D=3D 1); + assert(Src0.TcgSize =3D=3D 32 or Src0.TcgSize =3D=3D 64); + assert(Src1.TcgSize =3D=3D 32 or Src1.TcgSize =3D=3D 64); + switch (Opcode) { + case Instruction::And: + case Instruction::Or: + case Instruction::Xor: + break; + default: + abort(); + } + } + + bool NeedSafe =3D false; + switch (Opcode) { + case Instruction::Add: + ExprStream << "tcg_gen_add"; + break; + case Instruction::Sub: + ExprStream << "tcg_gen_sub"; + break; + case Instruction::And: + ExprStream << "tcg_gen_and"; + break; + case Instruction::Or: + ExprStream << "tcg_gen_or"; + break; + case Instruction::Xor: + ExprStream << "tcg_gen_xor"; + break; + case Instruction::Mul: + ExprStream << "tcg_gen_mul"; + break; + case Instruction::UDiv: + ExprStream << "tcg_gen_divu"; + break; + case Instruction::SDiv: + ExprStream << "tcg_gen_div"; + break; + case Instruction::AShr: + ExprStream << "tcg_gen_sar"; + NeedSafe =3D true; + break; + case Instruction::LShr: + ExprStream << "tcg_gen_shr"; + NeedSafe =3D true; + break; + case Instruction::Shl: + ExprStream << "tcg_gen_shl"; + NeedSafe =3D true; + break; + default: + abort(); + } + NeedSafe =3D false; + + if (IsImmediate) { + ExprStream << "i"; + } + + if (IsPtr) { + ExprStream << "_ptr"; + } else { + ExprStream << "_i" << (int)Src0.TcgSize; + } + + if (IsImmediate and NeedSafe) { + ExprStream << "_safe" << (int)Src0.TcgSize; + } + + ExprStream.flush(); + + return Expr; +} + +static std::string mapVecBinOp(const Instruction::BinaryOps &Opcode, + const TcgV &Src0, const TcgV &Src1) +{ + const bool IsShift =3D Opcode =3D=3D Instruction::Shl or + Opcode =3D=3D Instruction::LShr or + Opcode =3D=3D Instruction::AShr; + + std::string Suffix; + switch (Src1.Kind) { + case IrPtrToOffset: + Suffix =3D (IsShift) ? "v" : ""; + break; + case IrConst: + case IrValue: + Suffix =3D "s"; + break; + case IrImmediate: + Suffix =3D "i"; + break; + default: + abort(); + } + + switch (Opcode) { + case Instruction::Add: + return "add" + Suffix; + break; + case Instruction::Sub: + return "sub" + Suffix; + break; + case Instruction::Mul: + return "mul" + Suffix; + break; + case Instruction::And: + return "and" + Suffix; + break; + case Instruction::Or: + return "or" + Suffix; + break; + case Instruction::Xor: + return "xor" + Suffix; + break; + case Instruction::Shl: + return "shl" + Suffix; + break; + case Instruction::LShr: + return "shr" + Suffix; + break; + case Instruction::AShr: + return "sar" + Suffix; + break; + default: + abort(); + } +} + +void tempNew(raw_ostream &Out, const TcgV &Value) +{ + if (Value.Kind =3D=3D IrValue) { + Out << "tcg_temp_new_i" << (int)Value.TcgSize << "();\n"; + } +} + +void tempNewPtr(raw_ostream &Out) { Out << "tcg_temp_new_ptr();\n"; } + +void tempNewVec(raw_ostream &Out, uint32_t Size) +{ + Out << "temp_new_gvec(&mem, " << Size << ");\n"; +} + +void genNewLabel(raw_ostream &Out) { Out << "gen_new_label();\n"; } + +void genSetLabel(raw_ostream &Out, const TcgV &L) +{ + assert(L.Kind =3D=3D IrLabel); + Out << "gen_set_label(" << L << ");\n"; +} + +void defineNewTemp(raw_ostream &Out, const TcgV &Tcg) +{ + assert(!Tcg.ConstantExpression); + if (Tcg.Kind =3D=3D IrPtrToOffset and !EmittedVectorMem) { + EmittedVectorMem =3D true; + c::emitVectorMemVar(Out); + } + Out << tcg::getType(Tcg) << " " << Tcg << " =3D "; + switch (Tcg.Kind) { + case IrValue: + tcg::tempNew(Out, Tcg); + break; + case IrPtr: + tcg::tempNewPtr(Out); + break; + case IrPtrToOffset: + tcg::tempNewVec(Out, vectorSizeInBytes(Tcg)); + break; + case IrLabel: + tcg::genNewLabel(Out); + break; + default: + abort(); + } +} + +void genBr(raw_ostream &Out, const TcgV &L) +{ + assert(L.Kind =3D=3D IrLabel); + Out << "tcg_gen_br(" << L << ");\n"; +} + +void genTempInit(raw_ostream &Out, const TcgV &Arg1, const StringRef Str) +{ + Out << getType(Arg1) << ' ' << Arg1 << " =3D " << "tcg_const_i" + << (int)Arg1.TcgSize << "(" << Str << ");\n"; +} + +void genTempInit(raw_ostream &Out, const TcgV &Arg1, uint64_t Value) +{ + Out << getType(Arg1) << ' ' << Arg1 << " =3D " << "tcg_const_i" + << (int)Arg1.TcgSize << "((uint64_t)" << Value << "ULL);\n"; +} + +void genTempInit(raw_ostream &Out, const TcgV &Arg1, const TcgV &Arg2) +{ + assert(Arg2.Kind =3D=3D IrImmediate); + Out << getType(Arg1) << ' ' << Arg1 << " =3D " << "tcg_const_i" + << (int)Arg1.TcgSize << "(" << Arg2 << ");\n"; +} + +void genAssignConst(raw_ostream &Out, const TcgV &Arg1, const StringRef St= r) +{ + Out << getType(Arg1) << ' ' << Arg1 << " =3D " << "tcg_constant_i" + << (int)Arg1.TcgSize << "(" << Str << ");\n"; +} + +void genAssignConst(raw_ostream &Out, const TcgV &Arg1, uint64_t Value) +{ + Out << getType(Arg1) << ' ' << Arg1 << " =3D " << "tcg_constant_i" + << (int)Arg1.TcgSize << "((uint64_t)" << Value << "ULL);\n"; +} + +void genAssignConst(raw_ostream &Out, const TcgV &Arg1, const TcgV &Arg2) +{ + assert(Arg2.Kind =3D=3D IrImmediate); + Out << getType(Arg1) << ' ' << Arg1 << " =3D " << "tcg_constant_i" + << (int)Arg1.TcgSize << "(" << Arg2 << ");\n"; +} + +void genExtI32I64(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.TcgSize =3D=3D 64); + assert(Src.TcgSize =3D=3D 32); + emitCallTcg(Out, "tcg_gen_ext_i32_i64", {Dst, Src}); +} + +void genExtrlI64I32(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.TcgSize =3D=3D 32); + assert(Src.TcgSize =3D=3D 64); + emitCallTcg(Out, "tcg_gen_extrl_i64_i32", {Dst, Src}); +} + +void genExtuI32I64(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.TcgSize =3D=3D 64); + assert(Src.TcgSize =3D=3D 32); + emitCallTcg(Out, "tcg_gen_extu_i32_i64", {Dst, Src}); +} + +void genExtrhI64I32(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.TcgSize =3D=3D 32); + assert(Src.TcgSize =3D=3D 64); + emitCallTcg(Out, "tcg_gen_extrh_i64_i32", {Dst, Src}); +} + +void genExtract(raw_ostream &Out, bool Sign, const TcgV &Dst, const TcgV &= Src, + const TcgV &Offset, const TcgV &Length) +{ + assert(Dst.TcgSize =3D=3D Src.TcgSize); + const char *SignStr =3D (Sign) ? "s" : ""; + const TcgV &MSrc =3D materialize(Src); + Out << "tcg_gen_" << SignStr << "extract_i" << (int)Dst.TcgSize << "("; + emitArgListTcg(Out, {Dst, MSrc, Offset, Length}); + Out << ");\n"; +} + +void genDeposit(raw_ostream &Out, const TcgV &Dst, const TcgV &Into, + const TcgV &From, const TcgV &Offset, const TcgV &Length) +{ + assert(Dst.TcgSize =3D=3D Into.TcgSize); + assert(Dst.TcgSize =3D=3D From.TcgSize or From.Kind =3D=3D IrImmediate= ); + Out << "tcg_gen_deposit_i" << (int)Dst.TcgSize << "("; + const TcgV MInto =3D materialize(Into); + const TcgV MLength =3D materialize(Length); + emitArgListTcg(Out, {Dst, MInto, MLength, From, Offset}); + Out << ");\n"; +} + +void genTruncPtr(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + auto FuncStr =3D Twine("tcg_gen_trunc_i") + .concat(std::to_string(Src.TcgSize)) + .concat("_ptr") + .str(); + emitCallTcg(Out, FuncStr, {Dst, Src}); +} + +void genConcat(raw_ostream &Out, const TcgV &Dst, const TcgV &Src1, + const TcgV &Src2) +{ + assert(Dst.TcgSize =3D=3D 64); + assert(Src1.TcgSize =3D=3D 32); + assert(Src2.TcgSize =3D=3D 32); + emitCallTcg(Out, "tcg_gen_concat_i32_i64", {Dst, Src1, Src2}); +} + +void genMov(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.TcgSize =3D=3D Src.TcgSize); + Out << "tcg_gen_mov_i" << (int)Dst.TcgSize << "(" << Dst << ", " << Src + << ");\n"; +} + +void genMovPtr(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.TcgSize =3D=3D Src.TcgSize); + assert(Dst.Kind =3D=3D IrPtr); + assert(Src.Kind =3D=3D IrPtr); + Out << "tcg_gen_mov_ptr(" << Dst << ", " << Src << ");\n"; +} + +void genAddPtr(raw_ostream &Out, const TcgV &Dst, const TcgV &Ptr, + const TcgV &Offset) +{ + assert(Ptr.Kind =3D=3D IrPtr or Ptr.Kind =3D=3D IrEnv); + switch (Offset.Kind) { + case IrConst: + case IrImmediate: { + emitCallTcg(Out, "tcg_gen_addi_ptr", {Dst, Ptr, Offset}); + } break; + case IrValue: { + uint32_t TcgTargetPtrSize =3D 64; + auto OffsetPtr =3D + TcgV::makeTemp(TcgTargetPtrSize, TcgTargetPtrSize, IrPtr); + tcg::defineNewTemp(Out, OffsetPtr); + tcg::genTruncPtr(Out, OffsetPtr, Offset); + + emitCallTcg(Out, "tcg_gen_add_ptr", {Dst, Ptr, OffsetPtr}); + } break; + default: + abort(); + } +} + +void genBinOp(raw_ostream &Out, const TcgV &Dst, + const Instruction::BinaryOps Opcode, const TcgV &Src0, + const TcgV &Src1) +{ + auto OpStr =3D mapBinOp(Opcode, Src0, Src1); + emitCallTcg(Out, OpStr, {Dst, Src0, Src1}); +} + +void genMovI(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Src.Kind =3D=3D IrImmediate); + Out << "tcg_gen_movi_i" << (int)Dst.TcgSize << "(" << Dst << ", " << S= rc + << ");\n"; +} + +void genMovcond(raw_ostream &Out, const CmpInst::Predicate &Pred, + const TcgV &Ret, const TcgV &C1, const TcgV &C2, const Tcg= V &V1, + const TcgV &V2) +{ + assert(Ret.TcgSize =3D=3D C1.TcgSize); + assert(Ret.TcgSize =3D=3D C2.TcgSize); + assert(Ret.TcgSize =3D=3D V1.TcgSize); + assert(Ret.TcgSize =3D=3D V2.TcgSize); + const TcgV mC1 =3D materialize(C1); + const TcgV mC2 =3D materialize(C2); + const TcgV mV1 =3D materialize(V1); + const TcgV mV2 =3D materialize(V2); + Out << "tcg_gen_movcond_i" << (int)Ret.TcgSize << '(' << mapPredicate(= Pred) + << ", "; + emitArgListTcg(Out, {Ret, mC1, mC2, mV1, mV2}); + Out << ");\n"; +} + +void genSetcond(raw_ostream &Out, const CmpInst::Predicate &Pred, + const TcgV &Dst, const TcgV &Op1, const TcgV &Op2) +{ + assert(Op1.TcgSize =3D=3D Op2.TcgSize); + assert(Op1.TcgSize =3D=3D Dst.TcgSize); + assert(Op1.TcgSize =3D=3D 32 or Op1.TcgSize =3D=3D 64); + Out << "tcg_gen_setcond_i" << (int)Dst.TcgSize << "(" << mapPredicate(= Pred) + << ", " << Dst << ", " << Op1 << ", " << Op2 << ");\n"; +} + +void genSetcondI(raw_ostream &Out, const CmpInst::Predicate &Pred, + const TcgV &Dst, const TcgV &Op1, const TcgV &Op2) +{ + assert(Op1.TcgSize =3D=3D Dst.TcgSize); + assert(Op1.TcgSize =3D=3D 32 or Op1.TcgSize =3D=3D 64); + assert(Dst.Kind !=3D IrImmediate && Op1.Kind !=3D IrImmediate && + Op2.Kind =3D=3D IrImmediate); + Out << "tcg_gen_setcondi_i" << (int)Dst.TcgSize << "(" << mapPredicate= (Pred) + << ", " << Dst << ", " << Op1 << ", " << Op2 << ");\n"; +} + +void genBrcond(raw_ostream &Out, const CmpInst::Predicate &Pred, + const TcgV &Arg1, const TcgV &Arg2, const TcgV &Label) +{ + assert(Arg1.TcgSize =3D=3D Arg2.TcgSize); + assert(Arg1.TcgSize =3D=3D 32 || Arg1.TcgSize =3D=3D 64); + assert(Label.Kind =3D=3D IrLabel); + if (Arg2.Kind =3D=3D IrImmediate) { + Out << "tcg_gen_brcondi_i" << (int)Arg1.TcgSize; + } else { + Out << "tcg_gen_brcond_i" << (int)Arg1.TcgSize; + } + Out << "(" << mapPredicate(Pred) << ", " << materialize(Arg1) << ", " + << Arg2 << ", " << Label << ");\n"; +} + +void genQemuLoad(raw_ostream &Out, const TcgV &Dst, const TcgV &Ptr, + const char *MemOpStr) +{ + assert(Dst.Kind =3D=3D IrValue); + assert(Ptr.Kind !=3D IrImmediate); + const auto MPtr =3D materialize(Ptr); + Out << "tcg_gen_qemu_ld_i" << (int)Dst.TcgSize << "("; + emitArgListTcg(Out, {Dst, MPtr, MmuIndex}); + Out << ", " << MemOpStr << ");\n"; +} + +void genQemuStore(raw_ostream &Out, const TcgV &Ptr, const TcgV &Src, + const char *MemOpStr) +{ + assert(Src.Kind =3D=3D IrValue); + assert(Ptr.Kind !=3D IrImmediate); + const auto MPtr =3D materialize(Ptr); + Out << "tcg_gen_qemu_st_i" << (int)Src.TcgSize << "("; + emitArgListTcg(Out, {Src, MPtr, MmuIndex}); + Out << ", " << MemOpStr << ");\n"; +} + +void genLd(raw_ostream &Out, const TcgV &Dst, const TcgV &Ptr, uint64_t Of= fset) +{ + assert(Ptr.Kind =3D=3D IrPtr); + // First output the correct tcg function for the widths of Dst + if (Dst.LlvmSize < Dst.TcgSize) { + Out << "tcg_gen_ld" << (int)Dst.LlvmSize << "u_i" << (int)Dst.TcgS= ize; + } else { + Out << "tcg_gen_ld_i" << (int)Dst.TcgSize; + } + // Then emit params + Out << "(" << Dst << ", " << Ptr << ", " << Offset << ");\n"; +} + +void genSt(raw_ostream &Out, const TcgV &Ptr, const TcgV &Src, uint64_t Of= fset) +{ + assert(Ptr.Kind =3D=3D IrPtr); + // First output the correct tcg function for the widths of Dst + if (Src.LlvmSize < Src.TcgSize) { + Out << "tcg_gen_st" << (int)Src.LlvmSize << "_i" << (int)Src.TcgSi= ze; + } else { + Out << "tcg_gen_st_i" << (int)Src.TcgSize; + } + // Then emit params + Out << "(" << Src << ", " << Ptr << ", " << Offset << ");\n"; +} +void genFunnelShl(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1, const TcgV &Shift) +{ + assert(Src0.TcgSize =3D=3D Dst.TcgSize); + assert(Src1.TcgSize =3D=3D Dst.TcgSize); + assert(Shift.TcgSize =3D=3D Dst.TcgSize); + + if (Dst.TcgSize =3D=3D 32) { + auto Temp =3D TcgV::makeTemp(64, 64, IrValue); + defineNewTemp(Out, Temp); + genConcat(Out, Temp, Src1, Src0); + + if (Shift.Kind =3D=3D IrImmediate) { + genBinOp(Out, Temp, Instruction::Shl, Temp, Shift); + } else { + auto Ext =3D TcgV::makeTemp(64, 64, IrValue); + defineNewTemp(Out, Ext); + genExtuI32I64(Out, Ext, Shift); + genBinOp(Out, Temp, Instruction::Shl, Temp, Ext); + } + + tcg::genExtrhI64I32(Out, Dst, Temp); + } else if (Dst.TcgSize =3D=3D 64) { + const TcgV ASrc0 =3D materialize(Src0); + const TcgV ASrc1 =3D materialize(Src1); + const TcgV AShift =3D materialize(Shift); + genCallHelper(Out, "helper_fshl_i64", {Dst, ASrc0, ASrc1, AShift}); + } +} + +void genBitreverse(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + auto FuncName =3D Twine("helper_bitreverse") + .concat(Twine((int)Dst.LlvmSize)) + .concat("_i") + .concat(Twine((int)Src.TcgSize)) + .str(); + genCallHelper(Out, FuncName, {Dst, Src}); +} + +void genCountLeadingZeros(raw_ostream &Out, const TcgV &Dst, const TcgV &S= rc) +{ + assert(Dst.TcgSize =3D=3D Src.TcgSize); + Out << "tcg_gen_clzi_i" << (int)Dst.TcgSize << "(" << Dst << ", " << S= rc + << ", " << (int)Src.TcgSize << ");\n"; +} + +void genCountTrailingZeros(raw_ostream &Out, const TcgV &Dst, const TcgV &= Src) +{ + assert(Dst.TcgSize =3D=3D Src.TcgSize); + Out << "tcg_gen_ctzi_i" << (int)Dst.TcgSize << "(" << Dst << ", " << S= rc + << ", " << (int)Src.TcgSize << ");\n"; +} + +void genCountOnes(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.TcgSize =3D=3D Src.TcgSize); + Out << "tcg_gen_ctpop_i" << (int)Dst.TcgSize << "(" << Dst << ", " << = Src + << ");\n"; +} + +void genByteswap(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.TcgSize =3D=3D Src.TcgSize); + Out << "tcg_gen_bswap" << (int)Dst.TcgSize << "_i" << (int)Src.TcgSize + << "(" << Dst << ", " << Src << ");\n"; +} + +static void genVecBinOpStr(raw_ostream &Out, StringRef Op, const TcgV &Dst, + const TcgV &Src0, const TcgV &Src1) +{ + const uint32_t VectorSizeInBytes =3D vectorSizeInBytes(Dst); + Out << "tcg_gen_gvec_"; + Out << Op; + Out << "(MO_" << (int)Dst.LlvmSize << ", " << Dst << ", " << Src0 << "= , " + << Src1 << ", " << VectorSizeInBytes << ", " << VectorSizeInBytes + << ");\n"; +} + +void genVecBinOp(raw_ostream &Out, const Instruction::BinaryOps Opcode, + const TcgV &Dst, const TcgV &Src0, const TcgV &Src1) +{ + genVecBinOpStr(Out, mapVecBinOp(Opcode, Src0, Src1), Dst, Src0, Src1); +} + +void genVecSignedSatAdd(raw_ostream &Out, const TcgV &Dst, const TcgV &Src= 0, + const TcgV &Src1) +{ + assert(Dst.Kind =3D=3D IrPtrToOffset); + genVecBinOpStr(Out, "ssadd", Dst, Src0, Src1); +} + +void genVecSignedSatSub(raw_ostream &Out, const TcgV &Dst, const TcgV &Src= 0, + const TcgV &Src1) +{ + assert(Dst.Kind =3D=3D IrPtrToOffset); + genVecBinOpStr(Out, "sssub", Dst, Src0, Src1); +} + +void genVecSignedMax(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1) +{ + switch (Dst.Kind) { + case IrValue: { + const TcgV MSrc0 =3D materialize(Src0); + const TcgV MSrc1 =3D materialize(Src1); + Out << "tcg_gen_smax_i" << (int)Dst.TcgSize << "(" << Dst << ", " + << MSrc0 << ", " << MSrc1 << ");\n"; + } break; + case IrPtrToOffset: { + genVecBinOpStr(Out, "smax", Dst, Src0, Src1); + } break; + default: + abort(); + } +} + +void genVecUnsignedMax(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1) +{ + switch (Dst.Kind) { + case IrValue: { + const TcgV MSrc0 =3D materialize(Src0); + const TcgV MSrc1 =3D materialize(Src1); + Out << "tcg_gen_umax_i" << (int)Dst.TcgSize << "(" << Dst << ", " + << MSrc0 << ", " << MSrc1 << ");\n"; + } break; + case IrPtrToOffset: { + genVecBinOpStr(Out, "umax", Dst, Src0, Src1); + } break; + default: + abort(); + } +} + +void genVecSignedMin(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1) +{ + switch (Dst.Kind) { + case IrValue: { + const TcgV MSrc0 =3D materialize(Src0); + const TcgV MSrc1 =3D materialize(Src1); + Out << "tcg_gen_smin_i" << (int)Dst.TcgSize << "(" << Dst << ", " + << MSrc0 << ", " << MSrc1 << ");\n"; + } break; + case IrPtrToOffset: { + genVecBinOpStr(Out, "smin", Dst, Src0, Src1); + } break; + default: + abort(); + } +} + +void genVecUnsignedMin(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1) +{ + switch (Dst.Kind) { + case IrValue: { + const TcgV MSrc0 =3D materialize(Src0); + const TcgV MSrc1 =3D materialize(Src1); + Out << "tcg_gen_umin_i" << (int)Dst.TcgSize << "(" << Dst << ", " + << MSrc0 << ", " << MSrc1 << ");\n"; + } break; + case IrPtrToOffset: { + genVecBinOpStr(Out, "umin", Dst, Src0, Src1); + } break; + default: + assert(false); + } +} + +void genVecMemcpy(raw_ostream &Out, const TcgV &Dst, const TcgV &Src, + const TcgV &Size) +{ + Out << "tcg_gen_gvec_mov(MO_8" << ", " << Dst << ", " << Src << ", " <= < Size + << ", " << Size << ");\n"; +} + +void genVecMemset(raw_ostream &Out, const TcgV &Dst, const TcgV &Src, + const TcgV &Size) +{ + switch (Src.Kind) { + case IrValue: + case IrConst: + Out << "tcg_gen_gvec_dup_i" << (int)Src.TcgSize << "(MO_" + << (int)Src.LlvmSize << ", " << Dst << ", " << Size << ", " <<= Size + << ", " << Src << ");\n"; + break; + case IrImmediate: + Out << "tcg_gen_gvec_dup_imm" << "(MO_" << (int)Src.LlvmSize << ",= " + << Dst << ", " << Size << ", " << Size << ", " << Src << ");\n= "; + break; + default: + abort(); + } +} + +void genVecSplat(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + const uint32_t VectorSizeInBytes =3D vectorSizeInBytes(Dst); + const auto Size =3D + TcgV::makeImmediate(Twine(VectorSizeInBytes).str(), 64, 64); + genVecMemset(Out, Dst, Src, Size); +} + +void genVecArrSplat(raw_ostream &Out, const TcgV &Dst, + SmallVector &Arr) +{ + const uint32_t VectorSizeInBytes =3D vectorSizeInBytes(Dst); + const std::string TmpName =3D mkName("varr"); + Out << "uint" << (int)Dst.LlvmSize << "_t " << TmpName << "[] =3D {"; + emitArgListTcg(Out, Arr.begin(), Arr.end()); + Out << "};\n"; + // TODO: We are using global tcg_env here as not all functions that mi= ght + // emit constants take env. + Out << "tcg_gen_gvec_constant(MO_" << (int)Dst.LlvmSize << ", tcg_env" + << ", " << Dst << ", " << TmpName << ", " << VectorSizeInBytes + << ");\n"; +} + +void genVecBitsel(raw_ostream &Out, const TcgV &Dst, const TcgV &Cond, + const TcgV &Src0, const TcgV &Src1) +{ + const uint32_t VectorSizeInBytes =3D vectorSizeInBytes(Dst); + Out << "tcg_gen_gvec_bitsel(" << "MO_" << (int)Dst.LlvmSize << ", " <<= Dst + << ", " << Cond << ", " << Src0 << ", " << Src1 << ", " + << VectorSizeInBytes << ", " << VectorSizeInBytes << ");\n"; +} + +void genVecCmp(raw_ostream &Out, const TcgV &Dst, + const CmpInst::Predicate &Pred, const TcgV &Src0, + const TcgV &Src1) +{ + // TODO: Return type of llvm vector compare is actually 128 x i1, curr= ently + // we keep the same element size. Requires trunc. + const uint32_t VectorSizeInBytes =3D vectorSizeInBytes(Dst); + Out << "tcg_gen_gvec_cmp(" << mapPredicate(Pred) << ", " << "MO_" + << (int)Dst.LlvmSize << ", " << Dst << ", " << Src0 << ", " << Src1 + << ", " << VectorSizeInBytes << ", " << VectorSizeInBytes << ");\n= "; +} + +void genAbs(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + assert(Dst.Kind =3D=3D Src.Kind); + assert(Dst.TcgSize =3D=3D Src.TcgSize); + switch (Dst.Kind) { + case IrValue: { + const auto FuncStr =3D + Twine("tcg_gen_abs_i").concat(Twine(Src.TcgSize)).str(); + emitCallTcg(Out, FuncStr, {Dst, Src}); + } break; + case IrPtrToOffset: { + auto VectorSize =3D Dst.LlvmSize * Dst.VectorElementCount / 8; + Out << "tcg_gen_gvec_abs(" << "MO_" << (int)Dst.LlvmSize << ", " <= < Dst + << ", " << Src << ", " << VectorSize << ", " << VectorSize + << ");\n"; + } break; + default: + abort(); + } +} + +void genVecNot(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + const uint32_t VectorSize =3D Dst.LlvmSize * Dst.VectorElementCount / = 8; + Out << "tcg_gen_gvec_not(MO_" << (int)Src.LlvmSize << ", " << Dst << "= , " + << Src << ", " << VectorSize << ", " << VectorSize << ");\n"; +} + +static void genVecSizeChange(raw_ostream &Out, StringRef Name, const TcgV = &Dst, + const TcgV &Src) +{ + auto DstSz =3D vectorSizeInBytes(Dst); + auto SrcSz =3D vectorSizeInBytes(Src); + Out << "tcg_gen_gvec_" << Name << "(MO_" << (int)Dst.LlvmSize << ", MO= _" + << (int)Src.LlvmSize << ", " << Dst << ", " << Src << ", " << DstSz + << ", " << SrcSz << ", " << DstSz << ");\n"; +} + +void genVecTrunc(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + genVecSizeChange(Out, "trunc", Dst, Src); +} + +void genVecSext(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + genVecSizeChange(Out, "sext", Dst, Src); +} + +void genVecZext(raw_ostream &Out, const TcgV &Dst, const TcgV &Src) +{ + genVecSizeChange(Out, "zext", Dst, Src); +} + +} // namespace tcg + +namespace c +{ + +inline StringRef mapCPredicate(const CmpInst::Predicate &Pred) +{ + switch (Pred) { + case CmpInst::ICMP_EQ: + return "=3D=3D"; + case CmpInst::ICMP_NE: + return "!=3D"; + case CmpInst::ICMP_UGT: + return ">"; + case CmpInst::ICMP_UGE: + return ">=3D"; + case CmpInst::ICMP_ULT: + return "<"; + case CmpInst::ICMP_ULE: + return "<=3D"; + case CmpInst::ICMP_SGT: + return ">"; + case CmpInst::ICMP_SGE: + return ">=3D"; + case CmpInst::ICMP_SLT: + return "<"; + case CmpInst::ICMP_SLE: + return "<=3D"; + default: + abort(); + } +} + +enum BinOpSrcCast { + CastNone, + CastSigned, + CastUnsigned, +}; + +static std::string mapBinOp(const Instruction::BinaryOps &Opcode, + const TcgV &Src0, const TcgV &Src1) +{ + assert(Src0.Kind =3D=3D IrImmediate and Src1.Kind =3D=3D IrImmediate); + std::string Op; + BinOpSrcCast CastSrc0 =3D CastNone; + BinOpSrcCast CastSrc1 =3D CastNone; + switch (Opcode) { + case Instruction::Add: + Op =3D "+"; + break; + case Instruction::And: + Op =3D "&"; + break; + case Instruction::AShr: + CastSrc0 =3D CastUnsigned; + Op =3D ">>"; + break; + case Instruction::LShr: + CastSrc0 =3D CastSigned; + Op =3D ">>"; + break; + case Instruction::Shl: + Op =3D "<<"; + break; + case Instruction::Mul: + Op =3D "*"; + break; + case Instruction::UDiv: + CastSrc0 =3D CastUnsigned; + CastSrc1 =3D CastUnsigned; + Op =3D "/"; + break; + case Instruction::SDiv: + CastSrc0 =3D CastSigned; + CastSrc1 =3D CastSigned; + Op =3D "/"; + break; + case Instruction::Or: + Op =3D "|"; + break; + case Instruction::Sub: + Op =3D "-"; + break; + case Instruction::Xor: + Op =3D "^"; + break; + default: + abort(); + } + + std::string Expr =3D ""; + llvm::raw_string_ostream ExprStream(Expr); + ExprStream << "("; + if (CastSrc0 !=3D CastNone) { + auto IntPrefix =3D (CastSrc0 =3D=3D CastSigned) ? "int" : "uint"; + ExprStream << "(" << IntPrefix << (int)Src0.LlvmSize << "_t) "; + } + ExprStream << Src0 << " " << Op << " "; + if (CastSrc1 !=3D CastNone) { + auto IntPrefix =3D (CastSrc1 =3D=3D CastSigned) ? "int" : "uint"; + ExprStream << "(" << IntPrefix << (int)Src1.LlvmSize << "_t) "; + } + ExprStream << Src1 << ")"; + ExprStream.flush(); + + return Expr; +} + +TcgV ptrAdd(const TcgV &Ptr, const TcgV &Offset) +{ + assert(Offset.Kind =3D=3D IrConst or Offset.Kind =3D=3D IrImmediate); + switch (Ptr.Kind) { + case IrConst: + case IrImmediate: { + std::string Expr =3D ""; + llvm::raw_string_ostream ExprStream(Expr); + ExprStream << "(uint" << (int)Ptr.TcgSize << "_t *) ((uintptr_t) " + << Ptr << " + " << Offset << ")"; + ExprStream.flush(); + return TcgV::makeImmediate(Expr, Ptr.TcgSize, Ptr.LlvmSize); + } break; + case IrPtrToOffset: { + std::string Expr =3D ""; + llvm::raw_string_ostream ExprStream(Expr); + ExprStream << "(" << Ptr << " + " << Offset << ")"; + ExprStream.flush(); + TcgV Tcg(Expr, Ptr.TcgSize, Ptr.LlvmSize, Ptr.VectorElementCount, + IrPtrToOffset); + Tcg.ConstantExpression =3D true; + return Tcg; + } break; + default: + abort(); + } +} + +TcgV ternary(const TcgV &Cond, const TcgV &True, const TcgV &False) +{ + assert(Cond.Kind =3D=3D IrImmediate); + std::string Expr =3D ""; + llvm::raw_string_ostream ExprStream(Expr); + ExprStream << "(" << Cond << " ? " << True << " : " << False << ")"; + ExprStream.flush(); + return TcgV::makeImmediate(Expr, True.TcgSize, True.LlvmSize); +} + +TcgV deref(const TcgV &Ptr, uint32_t LlvmSize, uint32_t TcgSize) +{ + assert(Ptr.Kind =3D=3D IrImmediate); + std::string Expr =3D Twine("*").concat(tcg::getName(Ptr)).str(); + return TcgV::makeImmediate(Expr, TcgSize, LlvmSize); +} + +TcgV compare(const CmpInst::Predicate &Pred, const TcgV &Src0, const TcgV = &Src1) +{ + assert(Src0.Kind =3D=3D IrImmediate and Src1.Kind =3D=3D IrImmediate); + std::string Expr =3D ""; + llvm::raw_string_ostream ExprStream(Expr); + ExprStream << "(" << Src0 << " " << mapCPredicate(Pred) << " " << Src1 + << ")"; + ExprStream.flush(); + return TcgV::makeImmediate(Expr, Src0.TcgSize, 1); +} + +TcgV zext(const TcgV &V, uint32_t LlvmSize, uint32_t TcgSize) +{ + assert(V.Kind =3D=3D IrImmediate); + std::string Expr =3D ""; + llvm::raw_string_ostream ExprStream(Expr); + ExprStream << "((uint" << (int)LlvmSize << "_t) (uint" << (int)V.TcgSi= ze + << "_t) " << V << ")"; + ExprStream.flush(); + return TcgV::makeImmediate(Expr, TcgSize, LlvmSize); +} + +TcgV sext(const TcgV &V, uint32_t LlvmSize, uint32_t TcgSize) +{ + assert(V.Kind =3D=3D IrImmediate); + std::string Expr =3D ""; + llvm::raw_string_ostream ExprStream(Expr); + ExprStream << "((int" << (int)LlvmSize << "_t) (int" << (int)V.TcgSize + << "_t) " << V << ")"; + ExprStream.flush(); + return TcgV::makeImmediate(Expr, TcgSize, LlvmSize); +} + +TcgV binop(Instruction::BinaryOps Opcode, const TcgV &Src0, const TcgV &Sr= c1) +{ + std::string Op =3D mapBinOp(Opcode, Src0, Src1); + uint32_t LargestLlvmSize =3D std::max(Src0.LlvmSize, Src1.LlvmSize); + uint32_t LargestTcgSize =3D llvmToTcgSize(LargestLlvmSize); + return TcgV::makeImmediate(Op, LargestTcgSize, LargestLlvmSize); +} + +void emitVectorPreamble(raw_ostream &Out) +{ + Out << "typedef struct VectorMem {\n"; + Out << " uint32_t allocated;\n"; + Out << "} VectorMem;\n\n"; + + Out << "static intptr_t temp_new_gvec(VectorMem *mem, uint32_t size)\n= "; + Out << "{\n"; + Out << " uint32_t off =3D ROUND_UP(mem->allocated, size);\n"; + Out << " g_assert(off + size <=3D STRUCT_ARRAY_SIZE(CPUArchState, " + "tmp_vmem));\n"; + Out << " mem->allocated =3D off + size;\n"; + Out << " return offsetof(CPUArchState, " << TempVectorBlock + << ") + off;\n"; + Out << "}\n"; +} + +void emitVectorMemVar(raw_ostream &Out) { Out << "VectorMem mem =3D {0};\n= "; } + +} // namespace c diff --git a/subprojects/helper-to-tcg/passes/backend/TcgEmit.h b/subprojec= ts/helper-to-tcg/passes/backend/TcgEmit.h new file mode 100644 index 0000000000..5e35efbaa1 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/backend/TcgEmit.h @@ -0,0 +1,290 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include "TcgType.h" + +#include +#include +#include // for CmpInst::Predicate +#include +#include +#include + +#include + +using llvm::CmpInst; +using llvm::Instruction; +using llvm::raw_ostream; +using llvm::SmallVector; +using llvm::StringRef; +using llvm::Twine; +using llvm::Value; + +namespace tcg +{ +inline std::string getName(const TcgV &V) +{ + if (V.ConstantExpression or V.Kind =3D=3D IrImmediate or V.Kind =3D=3D= IrConst) { + return V.Name; + } else { + switch (V.Kind) { + case IrValue: + return Twine("temp").concat(Twine(V.Id)).str(); + case IrEnv: + return "env"; + case IrPtr: + return Twine("ptr").concat(Twine(V.Id)).str(); + case IrPtrToOffset: + return Twine("vec").concat(Twine(V.Id)).str(); + case IrLabel: + return Twine("label").concat(Twine(V.Id)).str(); + default: + abort(); + }; + } +} +} // namespace tcg + +inline raw_ostream &operator<<(raw_ostream &Out, const TcgV &V) +{ + Out << tcg::getName(V); + return Out; +} + +namespace tcg +{ + +// TODO: The names we give temporaries depend on the function we're in, +// maybe we can put this name/index stuff somewhere more relevant? +void resetNameIndices(); +const std::string mkName(const std::string Suffix); + +// String representation of types +const std::string getType(const TcgV &Value); + +inline const TcgV materialize(const TcgV &Value) +{ + if (Value.Kind !=3D IrImmediate) { + return Value; + } + TcgV M =3D Value; + M.Name =3D Twine("tcg_constant_i") + .concat(Twine((int)Value.TcgSize)) + .concat("(") + .concat(tcg::getName(Value)) + .concat(")") + .str(); + M.Kind =3D IrConst; + return M; +} + +template +void emitArgListTcg(raw_ostream &Out, const I Beg, const I End) +{ + auto It =3D Beg; + if (It !=3D End) { + Out << *It; + ++It; + } + while (It !=3D End) { + Out << ", " << *It; + ++It; + } +} + +template +void emitCall(raw_ostream &Out, const StringRef &S, const I Beg, const I E= nd) +{ + Out << S << '('; + auto It =3D Beg; + if (It !=3D End) { + Out << *It; + ++It; + } + while (It !=3D End) { + Out << ", " << *It; + ++It; + } + Out << ");\n"; +} + +template +void emitCallTcg(raw_ostream &Out, const StringRef S, Iterator Begin, + Iterator End) +{ + assert(Begin !=3D End); + Out << S << '('; + Out << *Begin; + ++Begin; + while (Begin !=3D End) { + Out << ", " << *Begin; + ++Begin; + } + Out << ");\n"; +} + +inline void emitArgListTcg(raw_ostream &Out, + const std::initializer_list Args) +{ + emitArgListTcg(Out, Args.begin(), Args.end()); +} + +inline void emitCall(raw_ostream &Out, const StringRef &S, + const std::initializer_list Args) +{ + emitCall(Out, S, Args.begin(), Args.end()); +} + +inline void emitCallTcg(raw_ostream &Out, const StringRef &S, + std::initializer_list Args) +{ + emitCallTcg(Out, S, Args.begin(), Args.end()); +} + +inline void genCallHelper(raw_ostream &Out, const StringRef &Helper, + const std::initializer_list Args) +{ + auto Func =3D Twine("gen_").concat(Helper).str(); + emitCallTcg(Out, Func, Args); +} + +template +void genCallHelper(raw_ostream &Out, const StringRef &Helper, I Beg, I End) +{ + auto Func =3D Twine("gen_").concat(Helper).str(); + emitCallTcg(Out, Func, Beg, End); +} + +void tempNew(raw_ostream &Out, const TcgV &Value); +void tempNewPtr(raw_ostream &Out); +void tempNewVec(raw_ostream &Out); + +void genNewLabel(raw_ostream &Out); +void genSetLabel(raw_ostream &Out, const TcgV &L); + +void defineNewTemp(raw_ostream &Out, const TcgV &Tcg); + +void genBr(raw_ostream &Out, const TcgV &L); + +void genTempInit(raw_ostream &Out, const TcgV &Arg1, const StringRef Str); +void genTempInit(raw_ostream &Out, const TcgV &Arg1, uint64_t Value); +void genTempInit(raw_ostream &Out, const TcgV &Arg1, const TcgV &Arg2); +void genAssignConst(raw_ostream &Out, const TcgV &Arg1, const StringRef St= r); +void genAssignConst(raw_ostream &Out, const TcgV &Arg1, uint64_t Value); +void genAssignConst(raw_ostream &Out, const TcgV &Arg1, const TcgV &Arg2); + +void genExtI32I64(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genExtrlI64I32(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genExtuI32I64(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genExtrhI64I32(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genExtract(raw_ostream &Out, bool Sign, const TcgV &Dst, const TcgV &= Src, + const TcgV &Offset, const TcgV &Length); +void genDeposit(raw_ostream &Out, const TcgV &Dst, const TcgV &Into, + const TcgV &From, const TcgV &Offset, const TcgV &Length); + +void genTruncPtr(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); + +void genConcat(raw_ostream &Out, const TcgV &Dst, const TcgV &Src1, + const TcgV &Src2); +void genMov(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genMovPtr(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genAddPtr(raw_ostream &Out, const TcgV &Dst, const TcgV &Ptr, + const TcgV &Offset); +void genBinOp(raw_ostream &Out, const TcgV &Dst, + const Instruction::BinaryOps Opcode, const TcgV &Src0, + const TcgV &Src1); +void genMovI(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); + +void genMovcond(raw_ostream &Out, const CmpInst::Predicate &Pred, + const TcgV &Ret, const TcgV &C1, const TcgV &C2, const Tcg= V &V1, + const TcgV &V2); +void genSetcond(raw_ostream &Out, const CmpInst::Predicate &Pred, + const TcgV &Dst, const TcgV &Op1, const TcgV &Op2); +void genSetcondI(raw_ostream &Out, const CmpInst::Predicate &Pred, + const TcgV &Dst, const TcgV &Op1, const TcgV &Op2); +void genBrcond(raw_ostream &Out, const CmpInst::Predicate &Pred, + const TcgV &Arg1, const TcgV &Arg2, const TcgV &Label); + +void genQemuLoad(raw_ostream &Out, const TcgV &Dst, const TcgV &Ptr, + const char *MemOpStr); +void genQemuStore(raw_ostream &Out, const TcgV &Ptr, const TcgV &Src, + const char *MemOpStr); + +void genLd(raw_ostream &Out, const TcgV &Dst, const TcgV &Ptr, uint64_t Of= fset); +void genSt(raw_ostream &Out, const TcgV &Ptr, const TcgV &Src, uint64_t Of= fset); + +void genFunnelShl(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1, const TcgV &Shift); +void genBitreverse(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genAbs(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genCountLeadingZeros(raw_ostream &Out, const TcgV &Dst, const TcgV &S= rc); +void genCountTrailingZeros(raw_ostream &Out, const TcgV &Dst, const TcgV &= Src); +void genCountOnes(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genByteswap(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); + +// Vector ops. +void genVecBinOp(raw_ostream &Out, const Instruction::BinaryOps Opcode, + const TcgV &Dst, const TcgV &Src0, const TcgV &Src1); +void genVecSignedSatAdd(raw_ostream &Out, const TcgV &Dst, const TcgV &Src= 0, + const TcgV &Src1); +void genVecSignedSatSub(raw_ostream &Out, const TcgV &Dst, const TcgV &Src= 0, + const TcgV &Src1); +void genVecSignedMax(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1); +void genVecUnsignedMax(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1); +void genVecSignedMin(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1); +void genVecUnsignedMin(raw_ostream &Out, const TcgV &Dst, const TcgV &Src0, + const TcgV &Src1); +void genVecMemcpy(raw_ostream &Out, const TcgV &Dst, const TcgV &Src, + const TcgV &Size); +void genVecMemset(raw_ostream &Out, const TcgV &Dst, const TcgV &Src, + const TcgV &Size); +void genVecSplat(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genVecArrSplat(raw_ostream &Out, const TcgV &Dst, + SmallVector &Arr); +void genVecBitsel(raw_ostream &Out, const TcgV &Dst, const TcgV &Cond, + const TcgV &Src0, const TcgV &Src1); +void genVecCmp(raw_ostream &Out, const TcgV &Dst, + const CmpInst::Predicate &Pred, const TcgV &Src0, + const TcgV &Src1); +void genVecNot(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genVecTrunc(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genVecSext(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); +void genVecZext(raw_ostream &Out, const TcgV &Dst, const TcgV &Src); + +} // namespace tcg + +namespace c +{ + +TcgV ptrAdd(const TcgV &Ptr, const TcgV &Offset); +TcgV ternary(const TcgV &Cond, const TcgV &True, const TcgV &False); +TcgV deref(const TcgV &Ptr, uint32_t LlvmSize, uint32_t TcgSize); +TcgV compare(const CmpInst::Predicate &Pred, const TcgV &Src0, + const TcgV &Src1); +TcgV zext(const TcgV &V, uint32_t LlvmSize, uint32_t TcgSize); +TcgV sext(const TcgV &V, uint32_t LlvmSize, uint32_t TcgSize); +TcgV binop(Instruction::BinaryOps Opcode, const TcgV &Src0, const TcgV &Sr= c1); + +void emitVectorPreamble(raw_ostream &Out); +void emitVectorMemVar(raw_ostream &Out); + +} // namespace c diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index b933a7bb1a..004c16550a 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -68,6 +68,19 @@ cl::opt cl::desc("Pointer size of the guest architecture"), cl::init(32), cl::cat(Cat)); =20 +// Options for TcgEmit +cl::opt MmuIndexFunction( + "mmu-index-function", + cl::desc("Name of a (uint32_t tb_flag) -> int function returning the " + "mmu index from the tb_flags of the current translation block= "), + cl::init("get_tb_mmu_index"), cl::cat(Cat)); + +cl::opt + TempVectorBlock("temp-vector-block", + cl::desc("Name of uint8_t[...] field in CPUArchState u= sed " + "for allocating temporary gvec variables"), + cl::init("tmp_vmem"), cl::cat(Cat)); + // Define a TargetTransformInfo (TTI) subclass, this allows for overriding // common per-llvm-target information expected by other LLVM passes, such // as the width of the largest scalar/vector registers. Needed for consis= tent --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153853; cv=none; d=zohomail.com; s=zohoarc; b=DBx7Mx7IfTs+2LtdBcg8mkoh8kl/D1HQ3XC//lBSmPSK0k+oLUSYj4icGXXpZayBlAH6cLYjyByFIUg7AD/9MxNwkmUHxKnMicGT1co3njdTHG/5KhvidD21O1ir7DeLxmZT1C0H7ceGyuB6wPnr47nvR134eo2ZU0JIT/kWAiw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153853; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=CNnUELic1Nay7/rcgzfoEkgqXL52+S53mtLCKeakdMg=; b=BZE9MuWVJd+pjKLjAqCm9p4l+IA8Xq+Ug+S5iLw3N7OiKzv/PQnGitlzDy7iBmRtj3F3oeUoPFbrexIfDFu1fcHuQQD2SKFvOThdVBG/9MQHGQVgoGYjd0zaecetUJIJr4wfUGN9CauD6LULkKVlTFQSnMxVC6Iz7emXwNRiKeg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 17321538539701006.1968031003778; Wed, 20 Nov 2024 17:50:53 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJx-0006lJ-N1; Wed, 20 Nov 2024 20:49:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJk-0006Pr-Ki for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:13 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJf-0004qR-Il for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:12 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=CNnUELic1Nay7/rcgzfoEkgqXL52+S53mtLCKeakdMg=; b=tAvx0ko0bfoqDEF E/dNsUFouD+MiVBEgIJk2SaM8q1C3NI5zZEPPGyI0XgMwcCv7Fn/40YPKNo/JukrGBLIJz1idm2m1 GgT7TweXFlZrkWjS5VTnUC8xcgKt+KTxrw8aO0n4RM3QsgSCYAoylt4S5dYFtQ9T3fAoS6Aa4/tNn OE=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 31/43] helper-to-tcg: Introduce TcgGenPass Date: Thu, 21 Nov 2024 02:49:35 +0100 Message-ID: <20241121014947.18666-32-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153854920116600 Content-Type: text/plain; charset="utf-8" Adds a backend pass, taking previously optimized and canonicalized LLVM IR and for each function: 1. Runs the TcgV register allocator; 2. Iterates over instructions and calls appropriate functions in TcgEmit.h to emit TCG code. Signed-off-by: Anton Johansson --- .../helper-to-tcg/include/CmdLineOptions.h | 6 + subprojects/helper-to-tcg/meson.build | 1 + .../passes/backend/TcgGenPass.cpp | 1812 +++++++++++++++++ .../helper-to-tcg/passes/backend/TcgGenPass.h | 57 + .../helper-to-tcg/pipeline/Pipeline.cpp | 49 + 5 files changed, 1925 insertions(+) create mode 100644 subprojects/helper-to-tcg/passes/backend/TcgGenPass.cpp create mode 100644 subprojects/helper-to-tcg/passes/backend/TcgGenPass.h diff --git a/subprojects/helper-to-tcg/include/CmdLineOptions.h b/subprojec= ts/helper-to-tcg/include/CmdLineOptions.h index f59b700914..3787fbbaec 100644 --- a/subprojects/helper-to-tcg/include/CmdLineOptions.h +++ b/subprojects/helper-to-tcg/include/CmdLineOptions.h @@ -30,3 +30,9 @@ extern llvm::cl::opt GuestPtrSize; // Options for TcgEmit extern llvm::cl::opt MmuIndexFunction; extern llvm::cl::opt TempVectorBlock; +// Options for TcgGenPass +extern llvm::cl::opt OutputSourceFile; +extern llvm::cl::opt OutputHeaderFile; +extern llvm::cl::opt OutputEnabledFile; +extern llvm::cl::opt OutputLogFile; +extern llvm::cl::opt ErrorOnTranslationFailure; diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 55a177bd94..4f045eb1da 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -52,6 +52,7 @@ sources =3D [ 'passes/PrepareForTcgPass/IdentityMap.cpp', 'passes/backend/TcgTempAllocationPass.cpp', 'passes/backend/TcgEmit.cpp', + 'passes/backend/TcgGenPass.cpp', ] =20 clang =3D bindir / 'clang' diff --git a/subprojects/helper-to-tcg/passes/backend/TcgGenPass.cpp b/subp= rojects/helper-to-tcg/passes/backend/TcgGenPass.cpp new file mode 100644 index 0000000000..81adb42a5d --- /dev/null +++ b/subprojects/helper-to-tcg/passes/backend/TcgGenPass.cpp @@ -0,0 +1,1812 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#include "TcgGenPass.h" +#include "CmdLineOptions.h" +#include "Error.h" +#include "FunctionAnnotation.h" +#include "PseudoInst.h" +#include "TcgEmit.h" +#include "TcgTempAllocationPass.h" +#include "TcgType.h" +#include "llvm-compat.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// For std::swap +#include + +using namespace llvm; + +// Wrapper class around a TcgV to cast it to/from 32-/64-bit +class TcgSizeAdapter +{ + raw_ostream &Out; + const TcgV Orig; + Optional Adapted; + + public: + TcgSizeAdapter(raw_ostream &Out, const TcgV Orig) : Out(Out), Orig(Ori= g) {} + + const TcgV get(uint32_t Size) + { + if (Orig.Kind =3D=3D IrImmediate or (Orig.TcgSize =3D=3D Size)) { + return Orig; + } else if (!Adapted.hasValue()) { + initAdapted(Size); + } + return *Adapted; + } + + private: + void initAdapted(uint32_t Size) + { + assert(!Adapted.hasValue()); + assert((Size =3D=3D 32 and Orig.TcgSize =3D=3D 64) or + (Size =3D=3D 64 and Orig.TcgSize =3D=3D 32)); + + Adapted =3D TcgV::makeTemp(Size, Orig.LlvmSize, Orig.Kind); + tcg::defineNewTemp(Out, *Adapted); + if (Size =3D=3D 32) { + tcg::genExtrlI64I32(Out, *Adapted, Orig); + } else { + tcg::genExtuI32I64(Out, *Adapted, Orig); + } + } +}; + +class Mapper +{ + raw_ostream &Out; + llvm::DenseMap Map; + llvm::DenseMap Labels; + + // Keep track of whether a TcgV has been defined already, or not + SmallBitVector HasBeenDefined; + + const TempAllocationData &TAD; + + public: + Mapper(raw_ostream &Out, const TcgGlobalMap &TcgGlobals, const Module = &M, + const TempAllocationData &TAD) + : Out(Out), TAD(TAD) + { + // Default to size of previously mapped TcgVs + HasBeenDefined.resize(TAD.Map.size()); + } + + Expected getMapped(const Value *V) + { + auto It =3D Map.find(V); + if (It !=3D Map.end()) { + return It->second; + } + return mkError("Value not mapped"); + } + + TcgV mapBbAndEmit(BasicBlock *BB) + { + auto Find =3D Labels.find(BB); + if (Find =3D=3D Labels.end()) { + TcgV Label =3D TcgV::makeLabel(); + tcg::defineNewTemp(Out, Label); + return Labels.try_emplace(BB, Label).first->second; + } + return Find->second; + } + + void mapExplicitly(Value *Val, const TcgV &TcgVal) + { + assert(Map.find(Val) =3D=3D Map.end()); + Map.try_emplace(Val, TcgVal); + } + + void mapClear(Value *Val) + { + auto It =3D Map.find(Val); + assert(It !=3D Map.end()); + Map.erase(It); + } + + Expected mapAndEmit(const Value *V) + { + auto Mapped =3D getMapped(V); + if (Mapped) { + return Mapped.get(); + } + + auto It =3D TAD.Map.find(V); + if (It =3D=3D TAD.Map.end()) { + return mkError("Unable to map value: ", V); + } + + const TcgV Tcg =3D It->second; + + bool IsArg =3D TAD.Args.ArgInfoMap.find(V) !=3D TAD.Args.ArgInfoMa= p.end(); + + if (Tcg.Id >=3D HasBeenDefined.size()) { + HasBeenDefined.resize(Tcg.Id + 1); + } + + if (!IsArg and !HasBeenDefined[Tcg.Id] and + (!TAD.ReturnValue.hasValue() or Tcg !=3D *TAD.ReturnValue) and + Tcg.Kind !=3D IrImmediate and Tcg.Kind !=3D IrConst) { + HasBeenDefined.set(Tcg.Id); + tcg::defineNewTemp(Out, Tcg); + } + + // Logic for emitted TCG corresponding to constant LLVM vectors, t= wo + // cases are handled, splatted values + // + // + // + // and vectors where elements differ + // + // + // + // For the latter case, attemt to emit it as a constant splatted + // vector with a larger size by combining adjacent elements. This + // is an optimization as initialzing a constant vector with differ= ent + // elements is expensive compared to splatting. + auto ConstV =3D dyn_cast(V); + if (ConstV and V->getType()->isVectorTy()) { + Constant *Splat =3D ConstV->getSplatValue(); + if (Splat) { + // Constant splatted vector + auto It =3D TAD.Map.find(Splat); + assert(It !=3D TAD.Map.end()); + auto Size =3D TcgV::makeImmediate( + Twine(vectorSizeInBytes(Tcg)).str(), 64, 64); + tcg::genVecMemset(Out, Tcg, It->second, Size); + } else { + // Constant non-splatted vector, attempt to combine elemen= ts + // to make it splattable. + SmallVector Ints; + + // Copy over elements to a vector + for (unsigned I =3D 0; I < Tcg.VectorElementCount; ++I) { + Constant *Element =3D ConstV->getAggregateElement(I); + uint64_t Value =3D Element->getUniqueInteger().getZExt= Value(); + Ints.push_back(Value); + } + + // When combining adjacent elements, the maximum size supp= orted + // by TCG is 64-bit. MaxNumElements is the maximum amount= of + // elements to attempt to merge + size_t PatternLen =3D 0; + unsigned MaxNumElements =3D 8 * sizeof(uint64_t) / Tcg.Llv= mSize; + for (unsigned N =3D MaxNumElements; N > 1; N /=3D 2) { + // Attempt to combine N elements by checking if the fi= rst + // N elements tile the vector. + bool Match =3D true; + for (unsigned J =3D 0; J < Tcg.VectorElementCount; ++J= ) { + if (Ints[J % N] !=3D Ints[J]) { + Match =3D false; + break; + } + } + // If tiling succeeded, break out + if (Match) { + PatternLen =3D N; + break; + } + } + + if (PatternLen > 0) { + // Managed to tile vector with splattable element, com= pute + // final splattable value + uint64_t Value =3D 0; + for (unsigned I =3D 0; I < PatternLen; ++I) { + Value |=3D Ints[I] << I * Tcg.LlvmSize; + } + auto Splat =3D + TcgV::makeImmediate(Twine(Value).str(), 64, 64); + auto Size =3D TcgV::makeImmediate( + Twine(vectorSizeInBytes(Tcg)).str(), 64, 64); + tcg::genVecMemset(Out, Tcg, Splat, Size); + } else { + // Tiling failed, fall back to emitting an array copy = from + // C to a gvec vector. + SmallVector Arr; + for (unsigned I =3D 0; I < Tcg.VectorElementCount; ++I= ) { + Constant *Element =3D ConstV->getAggregateElement(= I); + auto It =3D TAD.Map.find(Element); + assert(It !=3D TAD.Map.end()); + Arr.push_back(It->second); + } + tcg::genVecArrSplat(Out, Tcg, Arr); + } + } + } + + return Map.try_emplace(V, It->second).first->second; + } + + Expected mapCondAndEmit(Value *V, uint32_t TcgSize, uint32_t Llv= mSize) + { + auto Mapped =3D getMapped(V); + if (Mapped) { + assert(Mapped.get().LlvmSize =3D=3D 1); + return Mapped.get(); + } + + auto It =3D TAD.Map.find(const_cast(V)); + if (It =3D=3D TAD.Map.end()) { + return mkError("Unable to map cond: ", V); + } + + const TcgV Tcg =3D It->second; + if (Tcg.Id >=3D HasBeenDefined.size()) { + HasBeenDefined.resize(Tcg.Id + 1); + } + if (!HasBeenDefined[Tcg.Id] and + (!TAD.ReturnValue.hasValue() or Tcg !=3D *TAD.ReturnValue)) { + HasBeenDefined.set(Tcg.Id); + tcg::defineNewTemp(Out, Tcg); + } + return Map.try_emplace(V, It->second).first->second; + } +}; + +struct TranslatedFunction { + std::string Name; + std::string Decl; + std::string Code; + std::string DispatchCode; + bool IsHelper; +}; + +static void ensureSignBitIsSet(raw_ostream &Out, const TcgV &V) +{ + if (V.LlvmSize =3D=3D V.TcgSize or V.Kind !=3D IrValue) { + return; + } + tcg::genExtract(Out, true, V, V, + TcgV::makeImmediate("0", V.TcgSize, V.LlvmSize), + TcgV::makeImmediate(Twine((int)V.LlvmSize).str(), V.Tc= gSize, + V.LlvmSize)); +} + +static Expected mapCallReturnValue(Mapper &Mapper, CallInst *Call) +{ + // Only map return value if it has > 0 uses. Destination values of ca= ll + // instructions are the only ones which LLVM will not remove if unused. + if (Call->getType()->isVoidTy() or Call->getNumUses() =3D=3D 0) { + return mkError("Invalid return type", Call); + } + return Mapper.mapAndEmit(Call); +} + +static Instruction::BinaryOps mapPseudoInstToOpcode(PseudoInst Inst) +{ + switch (Inst) { + case VecAddScalar: + case VecAddStore: + case VecAddScalarStore: + return Instruction::Add; + case VecSubScalar: + case VecSubStore: + case VecSubScalarStore: + return Instruction::Sub; + case VecMulScalar: + case VecMulStore: + case VecMulScalarStore: + return Instruction::Mul; + case VecXorScalar: + case VecXorStore: + case VecXorScalarStore: + return Instruction::Xor; + case VecOrScalar: + case VecOrStore: + case VecOrScalarStore: + return Instruction::Or; + case VecAndScalar: + case VecAndStore: + case VecAndScalarStore: + return Instruction::And; + case VecShlScalar: + case VecShlStore: + case VecShlScalarStore: + return Instruction::Shl; + case VecLShrScalar: + case VecLShrStore: + case VecLShrScalarStore: + return Instruction::LShr; + case VecAShrScalar: + case VecAShrStore: + case VecAShrScalarStore: + return Instruction::AShr; + default: + abort(); + } +} + +static bool translatePseudoInstCall(raw_ostream &Out, CallInst *Call, + PseudoInst PInst, + const SmallVector &Args, + Mapper &Mapper, + const TcgGlobalMap &TcgGlobals) +{ + switch (PInst) { + case IdentityMap: { + Mapper.mapExplicitly(Call, Args[0]); + } break; + case PtrAdd: { + if (Args[0].Kind =3D=3D IrPtr or Args[0].Kind =3D=3D IrEnv) { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genAddPtr(Out, *MaybeRes, Args[0], Args[1]); + } else if ((Args[0].Kind =3D=3D IrImmediate or Args[0].Kind =3D=3D= IrConst) and + (Args[1].Kind =3D=3D IrConst or Args[1].Kind =3D=3D IrI= mmediate)) { + Mapper.mapExplicitly(Call, c::ptrAdd(Args[0], Args[1])); + } else if (Args[0].Kind =3D=3D IrPtrToOffset and + (Args[1].Kind =3D=3D IrConst or Args[1].Kind =3D=3D IrI= mmediate)) { + Mapper.mapExplicitly(Call, c::ptrAdd(Args[0], Args[1])); + } else { + // ptradd on vector types requires immediate offset + return false; + } + } break; + case AccessGlobalArray: { + auto Offset =3D cast(Call->getArgOperand(0))->getZExt= Value(); + auto It =3D TcgGlobals.find(Offset); + assert(It !=3D TcgGlobals.end()); + TcgGlobal Global =3D It->second; + uint32_t LlvmSize =3D Global.Size; + uint32_t TcgSize =3D llvmToTcgSize(LlvmSize); + if (Args[1].Kind !=3D IrImmediate) { + // globalArray access with non-immediate index + return false; + } + auto Code =3D Global.Code.str() + "[" + tcg::getName(Args[1]) + "]= "; + auto Tcg =3D + TcgV::makeConstantExpression(Code, TcgSize, LlvmSize, IrValue); + Mapper.mapExplicitly(Call, Tcg); + } break; + case AccessGlobalValue: { + auto Offset =3D cast(Call->getArgOperand(0))->getZExt= Value(); + auto It =3D TcgGlobals.find(Offset); + assert(It !=3D TcgGlobals.end()); + TcgGlobal Global =3D It->second; + auto LlvmSize =3D Global.Size; + auto TcgSize =3D llvmToTcgSize(LlvmSize); + auto Tcg =3D TcgV::makeConstantExpression(Global.Code.str(), TcgSi= ze, + LlvmSize, IrValue); + Mapper.mapExplicitly(Call, Tcg); + } break; + case Brcond: { + auto LlvmPred =3D static_cast( + cast(Call->getOperand(0))->getZExtValue()); + tcg::genBrcond(Out, LlvmPred, Args[1], Args[2], Args[3]); + if (!Call->hasMetadata("fallthrough")) { + tcg::genBr(Out, Args[4]); + } + } break; + case Movcond: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + auto LlvmPred =3D static_cast( + cast(Call->getOperand(0))->getZExtValue()); + if (CmpInst::isSigned(LlvmPred)) { + ensureSignBitIsSet(Out, Args[1]); + ensureSignBitIsSet(Out, Args[2]); + } + tcg::genMovcond(Out, LlvmPred, *MaybeRes, Args[1], Args[2], Args[3= ], + Args[4]); + } break; + case VecSplat: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecSplat(Out, *MaybeRes, Args[0]); + } break; + case VecNot: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecNot(Out, *MaybeRes, Args[0]); + } break; + case VecNotStore: { + tcg::genVecNot(Out, Args[0], Args[1]); + } break; + case VecAddScalar: + case VecSubScalar: + case VecMulScalar: + case VecXorScalar: + case VecOrScalar: + case VecAndScalar: + case VecShlScalar: + case VecLShrScalar: + case VecAShrScalar: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + auto Opcode =3D mapPseudoInstToOpcode(PInst); + tcg::genVecBinOp(Out, Opcode, *MaybeRes, Args[0], Args[1]); + } break; + case VecAddStore: + case VecSubStore: + case VecMulStore: + case VecXorStore: + case VecOrStore: + case VecAndStore: + case VecShlStore: + case VecLShrStore: + case VecAShrStore: + case VecAddScalarStore: + case VecSubScalarStore: + case VecMulScalarStore: + case VecXorScalarStore: + case VecOrScalarStore: + case VecAndScalarStore: + case VecShlScalarStore: + case VecLShrScalarStore: + case VecAShrScalarStore: { + auto Opcode =3D mapPseudoInstToOpcode(PInst); + tcg::genVecBinOp(Out, Opcode, Args[0], Args[1], Args[2]); + } break; + case VecSignedSatAddStore: { + tcg::genVecSignedSatAdd(Out, Args[0], Args[1], Args[2]); + } break; + case VecSignedSatSubStore: { + tcg::genVecSignedSatSub(Out, Args[0], Args[1], Args[2]); + } break; + case VecSelectStore: { + tcg::genVecBitsel(Out, Args[0], Args[1], Args[2], Args[3]); + } break; + case VecAbsStore: { + tcg::genAbs(Out, Args[0], Args[1]); + } break; + case VecSignedMaxStore: { + tcg::genVecSignedMax(Out, Args[0], Args[1], Args[2]); + } break; + case VecUnsignedMaxStore: { + tcg::genVecUnsignedMax(Out, Args[0], Args[1], Args[2]); + } break; + case VecSignedMinStore: { + tcg::genVecSignedMin(Out, Args[0], Args[1], Args[2]); + } break; + case VecUnsignedMinStore: { + tcg::genVecUnsignedMin(Out, Args[0], Args[1], Args[2]); + } break; + case VecTruncStore: { + tcg::genVecTrunc(Out, Args[0], Args[1]); + } break; + case VecCompare: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + auto LlvmPred =3D static_cast( + cast(Call->getOperand(0))->getZExtValue()); + tcg::genVecCmp(Out, MaybeRes.get(), LlvmPred, Args[1], Args[2]); + } break; + case VecWideCondBitsel: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecBitsel(Out, MaybeRes.get(), Args[0], Args[1], Args[2]); + break; + } break; + case VecWideCondBitselStore: { + tcg::genVecBitsel(Out, Args[0], Args[1], Args[2], Args[3]); + break; + } break; + case GuestLoad: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + uint8_t Sign =3D cast(Call->getOperand(1))->getZExtVa= lue(); + uint8_t Size =3D cast(Call->getOperand(2))->getZExtVa= lue(); + uint8_t Endianness =3D + cast(Call->getOperand(3))->getZExtValue(); + std::string MemOpStr =3D "MO_"; + raw_string_ostream MemOpStream(MemOpStr); + switch (Endianness) { + case 0: + break; // do nothing + case 1: + MemOpStream << "LE"; + break; + case 2: + MemOpStream << "BE"; + break; + default: + abort(); + } + switch (Sign) { + case 0: + MemOpStream << "U"; + break; + case 1: + MemOpStream << "S"; + break; + default: + abort(); + } + switch (Size) { + case 1: + MemOpStream << "B"; + break; + case 2: + MemOpStream << "W"; + break; + case 4: + MemOpStream << "L"; + break; + case 8: + MemOpStream << "Q"; + break; + default: + abort(); + } + tcg::genQemuLoad(Out, *MaybeRes, Args[0], MemOpStream.str().c_str(= )); + } break; + case GuestStore: { + uint8_t Size =3D cast(Call->getOperand(2))->getZExtVa= lue(); + uint8_t Endianness =3D + cast(Call->getOperand(3))->getZExtValue(); + std::string MemOpStr =3D "MO_"; + raw_string_ostream MemOpStream(MemOpStr); + switch (Endianness) { + case 0: + break; // do nothing + case 1: + MemOpStream << "LE"; + break; + case 2: + MemOpStream << "BE"; + break; + default: + abort(); + } + // Always unsigned for stores + MemOpStream << "U"; + switch (Size) { + case 1: + MemOpStream << "B"; + break; + case 2: + MemOpStream << "W"; + break; + case 4: + MemOpStream << "L"; + break; + case 8: + MemOpStream << "Q"; + break; + default: + abort(); + } + tcg::genQemuStore(Out, Args[0], Args[1], MemOpStream.str().c_str()= ); + } break; + case Exception: { + // Map and adapt arguments to the call + SmallVector IArgs; + for (auto Arg : Args) { + IArgs.push_back(tcg::materialize(Arg)); + } + tcg::genCallHelper(Out, "helper_raise_exception", IArgs.begin(), + IArgs.end()); + } break; + default: + // unmapped pseudo inst + return false; + } + return true; +} + +static bool translateIntrinsicCall(raw_ostream &Out, CallInst *Call, + Function *F, + const SmallVector &Args, + Mapper &Mapper) +{ + switch (F->getIntrinsicID()) { +#if LLVM_VERSION_MAJOR > 11 + case Intrinsic::abs: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genAbs(Out, *MaybeRes, Args[0]); + } break; + case Intrinsic::smax: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecSignedMax(Out, *MaybeRes, Args[0], Args[1]); + } break; + case Intrinsic::smin: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecSignedMin(Out, *MaybeRes, Args[0], Args[1]); + } break; + case Intrinsic::umax: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecUnsignedMax(Out, *MaybeRes, Args[0], Args[1]); + } break; + case Intrinsic::umin: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecUnsignedMin(Out, *MaybeRes, Args[0], Args[1]); + } break; +#endif + case Intrinsic::sadd_sat: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecSignedSatAdd(Out, *MaybeRes, Args[0], Args[1]); + } break; + case Intrinsic::ssub_sat: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genVecSignedSatSub(Out, *MaybeRes, Args[0], Args[1]); + } break; + case Intrinsic::ctlz: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + if (Args[0].Kind =3D=3D IrPtrToOffset) { + // no gvec equivalent to clzi + return false; + } + tcg::genCountLeadingZeros(Out, *MaybeRes, Args[0]); + } break; + case Intrinsic::cttz: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + if (Args[0].Kind =3D=3D IrPtrToOffset) { + // no gvec equivalent to ctti + return false; + } + tcg::genCountTrailingZeros(Out, *MaybeRes, Args[0]); + } break; + case Intrinsic::ctpop: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + if (Args[0].Kind =3D=3D IrPtrToOffset) { + // no gvec equivalent to ctpop + return false; + } + tcg::genCountOnes(Out, *MaybeRes, Args[0]); + } break; + case Intrinsic::bswap: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genByteswap(Out, *MaybeRes, Args[0]); + } break; + case Intrinsic::fshl: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genFunnelShl(Out, *MaybeRes, Args[0], Args[1], Args[2]); + } break; + case Intrinsic::bitreverse: { + Expected MaybeRes =3D mapCallReturnValue(Mapper, Call); + if (!MaybeRes) { + return false; + } + tcg::genBitreverse(Out, *MaybeRes, Args[0]); + } break; + case Intrinsic::memcpy: { + tcg::genVecMemcpy(Out, Args[0], Args[1], Args[2]); + } break; + case Intrinsic::memset: { + tcg::genVecMemset(Out, Args[0], Args[1], Args[2]); + } break; + default: + // Unhandled LLVM intrinsic + return false; + } + return true; +} + +static Expected +translateFunction(const Function *F, const TcgGlobalMap &TcgGlobals, + const AnnotationMapTy &Annotations, + const SmallPtrSet HasTranslatedFunction) +{ + TranslatedFunction TF =3D { + .Name =3D F->getName().str(), + }; + + // Run TcgV register allocation + Expected MaybeTAD =3D + allocateTemporaries(*F, Annotations); + if (!MaybeTAD) { + return MaybeTAD.takeError(); + } + const TempAllocationData TAD =3D MaybeTAD.get(); + + { + StringRef NameRef(TF.Name); + std::string DemangledFuncName =3D demangle(TF.Name); + if (TF.Name !=3D DemangledFuncName) { + // If the function name changed when trying to demangle the na= me, + // the name was mangled. The resulting demangled name might l= ook + // something like + // + // namespace::subnamespace::function(...) + // + // extract the function name, this assumes 0 name collisions in + // the output. + size_t Index =3D 0; + NameRef =3D DemangledFuncName; + // Remove namespaces + Index =3D NameRef.find_last_of(':'); + if (Index !=3D StringRef::npos) { + NameRef =3D NameRef.substr(Index + 1); + } + // Remove arguments + Index =3D NameRef.find_first_of('('); + if (Index !=3D StringRef::npos) { + NameRef =3D NameRef.substr(0, Index); + } + } + + // Remove prefix for helper functions to get cleaner emitted names + TF.IsHelper =3D NameRef.consume_front("helper_"); + TF.Name =3D NameRef.str(); + } + + raw_string_ostream Out(TF.Code); + raw_string_ostream HeaderWriter(TF.Decl); + + raw_string_ostream DispatchWriter(TF.DispatchCode); + std::string DispatchCall; + raw_string_ostream DispatchCallWriter(DispatchCall); + int dispatch_arg_count =3D 0; + bool IsVectorInst =3D false; + + // Functions that should be ignored are convereted + // to declarations, see FilterFunctionsPass. + if (F->isDeclaration()) { + return mkError("Function is not translated"); + } + + Mapper Mapper(Out, TcgGlobals, *F->getParent(), TAD); + Optional RetVal =3D None; + Out << "// " << *F->getReturnType() << ' ' << F->getName() << '\n'; + HeaderWriter << "void " << "emit_" << TF.Name << '('; + SmallVector CArgs; + + if (!F->getReturnType()->isVoidTy()) { + assert(TAD.ReturnValue.hasValue()); + IsVectorInst =3D (*TAD.ReturnValue).Kind =3D=3D IrPtrToOffset; + CArgs.push_back(*TAD.ReturnValue); + } + + for (const Value *Arg : TAD.Args.Args) { + Expected MaybeMapped =3D Mapper.mapAndEmit(Arg); + if (!MaybeMapped) { + return mkError("failed mapping arg"); + } + IsVectorInst |=3D (MaybeMapped.get().Kind =3D=3D IrPtrToOffset); + CArgs.push_back(MaybeMapped.get()); + } + + auto CArgIt =3D CArgs.begin(); + if (CArgIt !=3D CArgs.end()) { + HeaderWriter << tcg::getType(*CArgIt) << ' ' << tcg::getName(*CArg= It); + ++CArgIt; + } + while (CArgIt !=3D CArgs.end()) { + HeaderWriter << ", " << tcg::getType(*CArgIt) << ' ' + << tcg::getName(*CArgIt); + ++CArgIt; + } + + if (!IsVectorInst) { + DispatchCallWriter << "emit_" << TF.Name << "("; + auto CArgIt =3D CArgs.begin(); + if (CArgIt !=3D CArgs.end()) { + DispatchWriter << tcg::getType(*CArgIt) << ' ' + << tcg::getName(*CArgIt) << " =3D "; + if (TAD.ReturnValue and CArgIt->Id =3D=3D (*TAD.ReturnValue).I= d) { + assert(CArgIt->Kind =3D=3D IrValue); + DispatchWriter << "temp_tcgv_i" << CArgIt->TcgSize + << "(ret_temp);\n"; + } else { + switch (CArgIt->Kind) { + case IrPtr: + case IrEnv: + DispatchWriter << "temp_tcgv_ptr(args[" + << dispatch_arg_count++ << "]);\n"; + break; + case IrValue: + DispatchWriter << "temp_tcgv_i" << CArgIt->TcgSize + << "(args[" << dispatch_arg_count++ + << "]);\n"; + break; + case IrImmediate: + DispatchWriter << "args[" << dispatch_arg_count++ + << "]->val;\n"; + break; + case IrPtrToOffset: + DispatchWriter << "args[" << dispatch_arg_count++ + << "]->val;\n"; + break; + default: + abort(); + }; + } + DispatchCallWriter << tcg::getName(*CArgIt); + ++CArgIt; + } + while (CArgIt !=3D CArgs.end()) { + DispatchWriter << tcg::getType(*CArgIt) << ' ' + << tcg::getName(*CArgIt) << " =3D "; + switch (CArgIt->Kind) { + case IrPtr: + case IrEnv: + DispatchWriter << "temp_tcgv_ptr(args[" << dispatch_arg_co= unt++ + << "]);\n"; + break; + case IrValue: + DispatchWriter << "temp_tcgv_i" << CArgIt->TcgSize << "(ar= gs[" + << dispatch_arg_count++ << "]);\n"; + break; + case IrImmediate: + DispatchWriter << "args[" << dispatch_arg_count++ + << "]->val;\n"; + break; + case IrPtrToOffset: + DispatchWriter << "args[" << dispatch_arg_count++ + << "]->val;\n"; + break; + default: + abort(); + }; + DispatchCallWriter << ", " << tcg::getName(*CArgIt); + ++CArgIt; + } + DispatchCallWriter << ");\n"; + DispatchWriter << DispatchCallWriter.str(); + } + + // Copy over function declaration from header to source file + HeaderWriter << ')'; + Out << HeaderWriter.str(); + Out << " {\n"; + HeaderWriter << ';'; + + ReversePostOrderTraversal RPOT((Function *)F); + for (auto BBI =3D RPOT.begin(); BBI !=3D RPOT.end(); ++BBI) { + BasicBlock &BB =3D **BBI; + + // Set label if not first BB + if (&BB !=3D &F->getEntryBlock()) { + TcgV Label =3D Mapper.mapBbAndEmit(&BB); + tcg::genSetLabel(Out, Label); + } + + // Emit TCG generators for the current BB + for (Instruction &I : BB) { + switch (I.getOpcode()) { + case Instruction::Alloca: { + auto Alloca =3D cast(&I); + Expected Res =3D Mapper.mapAndEmit(Alloca); + if (!Res) { + return Res.takeError(); + } + } break; + case Instruction::Br: { + // We need to keep the BB of the true branch alive + // so that we can iterate over the CFG as usual + // using LLVM. Or custom "opcode" @brcond is not an + // actual branch, so LLVM does not understand that + // we can branch to the true branch. + // + // For this reason we emit an extra dead branch + // to the true branch, and tag it as dead using + // metadata. The backend can later check that if + // this metadata is present and ignore the branch. + if (I.hasMetadata("dead-branch")) { + break; + } + + auto Branch =3D cast(&I); + if (Branch->isConditional()) { + assert(Branch->getNumSuccessors() =3D=3D 2); + Expected Condition =3D + Mapper.mapCondAndEmit(Branch->getCondition(), 32, = 1); + if (!Condition) + return mkError("couldn't map brcond condition ", + Branch->getCondition()); + const TcgV CCondition =3D tcg::materialize(Condition.g= et()); + const TcgV True =3D + Mapper.mapBbAndEmit(Branch->getSuccessor(0)); + const TcgV False =3D + Mapper.mapBbAndEmit(Branch->getSuccessor(1)); + + // Jump if condition is !=3D 0 + auto Zero =3D TcgV::makeImmediate("0", CCondition.TcgS= ize, 1); + tcg::genBrcond(Out, CmpInst::Predicate::ICMP_NE, CCond= ition, + Zero, True); + tcg::genBr(Out, False); + } else { + const TcgV Label =3D + Mapper.mapBbAndEmit(Branch->getSuccessor(0)); + tcg::genBr(Out, Label); + } + } break; + case Instruction::SExt: { + auto SExt =3D cast(&I); + + Expected SrcVal =3D Mapper.mapAndEmit(SExt->getOpera= nd(0)); + if (!SrcVal) { + return mkError("Couldn't map value ", SExt->getOperand= (0)); + } + if (SrcVal.get().Kind =3D=3D IrImmediate) { + auto ResLlvmSize =3D SExt->getDestTy()->getIntegerBitW= idth(); + Mapper.mapExplicitly(&I, + c::sext(SrcVal.get(), ResLlvmSize, + llvmToTcgSize(ResLlvmSize= ))); + } else if (SrcVal.get().Kind =3D=3D IrPtrToOffset) { + Expected Res =3D Mapper.mapAndEmit(&I); + if (!Res) { + return Res.takeError(); + } + tcg::genVecSext(Out, Res.get(), SrcVal.get()); + } else { + Expected Res =3D Mapper.mapAndEmit(&I); + if (!Res) { + return Res.takeError(); + } + if (Res.get().LlvmSize < 32) { + return mkError("sext to unsupported size: ", &I); + } + if (SrcVal.get().Kind =3D=3D IrPtrToOffset) { + return mkError("sext on vector type not supported:= ", + &I); + } + if (SrcVal.get().LlvmSize > 1 and + SrcVal.get().LlvmSize < 32) { + // TODO: Here we are using the fact that we + // support (16,64), (8,64). Also, move to TcgEmit + auto FuncStr =3D + Twine("tcg_gen_ext") + .concat(std::to_string(SrcVal.get().LlvmSi= ze)) + .concat("s_i") + .concat(std::to_string(Res.get().TcgSize)) + .str(); + auto ASrcVal =3D TcgSizeAdapter(Out, SrcVal.get()); + tcg::emitCallTcg( + Out, FuncStr, + {Res.get(), ASrcVal.get(Res.get().TcgSize)}); + } else if (SrcVal.get().LlvmSize =3D=3D 1 and + Res.get().TcgSize =3D=3D 32) { + tcg::genMov(Out, Res.get(), SrcVal.get()); + } else { + tcg::genExtI32I64(Out, Res.get(), SrcVal.get()); + } + } + } break; + case Instruction::ZExt: { + auto ZExt =3D cast(&I); + + Expected SrcVal =3D Mapper.mapAndEmit(ZExt->getOpera= nd(0)); + if (!SrcVal) + return mkError("Couldn't map value ", ZExt->getOperand= (0)); + + if (SrcVal.get().Kind =3D=3D IrImmediate) { + auto ResLlvmSize =3D ZExt->getDestTy()->getIntegerBitW= idth(); + if (ResLlvmSize > 64) { + return mkError("128-bit integers not supported: ",= &I); + } + Mapper.mapExplicitly(&I, + c::zext(SrcVal.get(), ResLlvmSize, + llvmToTcgSize(ResLlvmSize= ))); + break; + } + + auto *DestTy =3D ZExt->getDestTy(); + if (DestTy->isIntegerTy()) { + const uint32_t ResLlvmSize =3D + cast(DestTy)->getIntegerBitWidth(); + const uint32_t ResTcgSize =3D llvmToTcgSize(ResLlvmSiz= e); + if (ResLlvmSize > 64) { + return mkError("Invalid size: ", &I); + } + const uint32_t SrcLlvmSize =3D SrcVal.get().LlvmSize; + const uint32_t SrcTcgSize =3D SrcVal.get().TcgSize; + + Expected Res =3D Mapper.mapAndEmit(&I); + if (!Res) { + return Res.takeError(); + } + if (SrcTcgSize =3D=3D ResTcgSize) { + tcg::genMov(Out, Res.get(), SrcVal.get()); + } else if (SrcTcgSize > Res.get().TcgSize and + SrcLlvmSize =3D=3D 1) { + // Paradoxically we may need to emit an extract + // instruction for when a zero extension is reques= ted. + // This is to account for the fact that "booleans"= in + // tcg can be both 64- and 32-bit. So for instance= zext + // i1 -> i32, here i1 may actually be 64-bit. + tcg::genExtrlI64I32(Out, Res.get(), SrcVal.get()); + } else { + tcg::genExtuI32I64(Out, Res.get(), SrcVal.get()); + } + } else if (DestTy->isVectorTy()) { + Expected Res =3D Mapper.mapAndEmit(&I); + if (!Res) { + return Res.takeError(); + } + tcg::genVecZext(Out, Res.get(), SrcVal.get()); + } else { + return mkError("Invalid TcgSize!"); + } + } break; + case Instruction::Trunc: { + auto Trunc =3D cast(&I); + + Expected SrcVal =3D Mapper.mapAndEmit(Trunc->getOper= and(0)); + if (!SrcVal) { + return mkError("Couldn't map value ", Trunc->getOperan= d(0)); + } + if (SrcVal.get().Kind =3D=3D IrImmediate) { + Mapper.mapExplicitly(&I, SrcVal.get()); + break; + } + + Expected Res =3D Mapper.mapAndEmit(&I); + if (!Res) { + return Res.takeError(); + } + if (Res.get().Kind =3D=3D IrValue) { + if (SrcVal.get().TcgSize =3D=3D 64) { + if (Res.get().LlvmSize =3D=3D 32) { + // 64 -> 32 + tcg::genExtrlI64I32(Out, Res.get(), SrcVal.get= ()); + } else { + // 64 -> 16,8,1 + TcgV MRes =3D Res.get(); + TcgV MSrc =3D SrcVal.get(); + auto Offset =3D TcgV::makeImmediate("0", MRes.= TcgSize, + MRes.LlvmSiz= e); + auto Size =3D TcgV::makeImmediate( + Twine((int)MRes.LlvmSize).str(), MRes.TcgS= ize, + MRes.LlvmSize); + auto Temp =3D TcgV::makeTemp(64, 64, IrValue); + tcg::defineNewTemp(Out, Temp); + tcg::genExtract(Out, false, Temp, MSrc, Offset, + Size); + tcg::genExtrlI64I32(Out, MRes, Temp); + } + } else if (SrcVal.get().TcgSize =3D=3D 32) { + // 32 -> 16,8,1 + // 16 -> 8,1 + // 8 -> 1 + TcgV MRes =3D Res.get(); + TcgV MSrc =3D SrcVal.get(); + auto Offset =3D TcgV::makeImmediate("0", MRes.TcgS= ize, + MRes.LlvmSize); + auto Size =3D + TcgV::makeImmediate(Twine((int)MRes.LlvmSize).= str(), + MRes.TcgSize, MRes.LlvmSiz= e); + tcg::genExtract(Out, false, MRes, MSrc, Offset, Si= ze); + } else { + return mkError("Invalid TcgSize!"); + } + } else if (Res.get().Kind =3D=3D IrPtrToOffset) { + tcg::genVecTrunc(Out, Res.get(), SrcVal.get()); + } else { + return mkError("Invalid TcgSize!"); + } + } break; + case Instruction::Add: + case Instruction::And: + case Instruction::AShr: + case Instruction::LShr: + case Instruction::Mul: + case Instruction::UDiv: + case Instruction::SDiv: + case Instruction::Or: + case Instruction::Shl: + case Instruction::Sub: + case Instruction::Xor: { + auto Bin =3D cast(&I); + // Check we are working on integers + Expected MaybeOp1 =3D Mapper.mapAndEmit(Bin->getOper= and(0)); + if (!MaybeOp1) { + return MaybeOp1.takeError(); + } + Expected MaybeOp2 =3D Mapper.mapAndEmit(Bin->getOper= and(1)); + if (!MaybeOp2) { + return MaybeOp2.takeError(); + } + TcgV Op1 =3D MaybeOp1.get(); + TcgV Op2 =3D MaybeOp2.get(); + + // Swap operands if the first op. is an immediate + // and the operator is commutative + if (Op1.Kind =3D=3D IrImmediate and Op2.Kind !=3D IrImmedi= ate and + Bin->isCommutative()) { + std::swap(Op1, Op2); + } + + if (isa(Bin->getType())) { + if (Op1.Kind =3D=3D IrImmediate and Op2.Kind =3D=3D Ir= Immediate) { + Mapper.mapExplicitly( + Bin, c::binop(Bin->getOpcode(), Op1, Op2)); + } else { + Expected Res =3D Mapper.mapAndEmit(Bin); + if (!Res) { + return mkError("couldn't map binary op res", &= I); + } + + // Adapt sizes to account for boolean values, with + // LlvmSize =3D=3D 1 and TcgSize =3D=3D 32 or 64. = Materialize + // first op. to deal with non-commutative ops. + TcgSizeAdapter AOp1(Out, tcg::materialize(Op1)); + TcgSizeAdapter AOp2(Out, Op2); + + const uint32_t ResSize =3D Res.get().TcgSize; + tcg::genBinOp(Out, Res.get(), Bin->getOpcode(), + AOp1.get(ResSize), AOp2.get(ResSize)= ); + } + } else if (isa(Bin->getType())) { + Expected Res =3D Mapper.mapAndEmit(Bin); + if (!Res) { + return Res.takeError(); + } + assert(Res.get().Kind =3D=3D IrPtrToOffset); + tcg::genVecBinOp(Out, Bin->getOpcode(), Res.get(), Op1, + Op2); + } + } break; + case Instruction::Call: { + auto Call =3D cast(&I); + Function *F =3D Call->getCalledFunction(); + if (!F) { + return mkError("Indirect function calls not handled: "= , &I); + } + assert(F->hasName()); + StringRef Name =3D F->getName(); + + // These are the calls we currently no-op/ignore + if (Name =3D=3D "__assert_fail" or + Name =3D=3D "g_assertion_message_expr" or + isa(I) or isa(I)) { + break; + } + + SmallVector Args; + for (uint32_t i =3D 0; i < Call->arg_size(); ++i) { + if (auto Bb =3D + dyn_cast(Call->getArgOperand(i))) { + Args.push_back(Mapper.mapBbAndEmit(Bb)); + } else { + Expected Mapped =3D + Mapper.mapAndEmit(Call->getArgOperand(i)); + if (!Mapped) { + return Mapped.takeError(); + } + Args.push_back(Mapped.get()); + } + } + + // Function names sometimes contain embedded type informat= ion to + // handle polymorphic arguments, for instance + // + // llvm.memcpy.p0i8.p0i8.i64 + // + // specifying the source and desination pointer types as i= 8* and + // the size argument as an i64. + // + // Find the index for the first '.' before the types are + // specified + // + // llvm.memcpy.p0i8.p0i8.i64 + // ^- index of this '.' + size_t IndexBeforeTypes =3D StringRef::npos; + for (size_t i =3D Name.size() - 1; i > 0; --i) { + const char c =3D Name[i]; + bool ValidType =3D (c >=3D '0' and c <=3D '9') or c = =3D=3D 'i' or + c =3D=3D 'p' or c =3D=3D 'a' or c =3D= =3D 'v' or + c =3D=3D 'x'; + if (c =3D=3D '.') { + IndexBeforeTypes =3D i; + } else if (!ValidType) { + break; + } + } + StringRef StrippedName =3D Name.substr(0, IndexBeforeTypes= ); + + PseudoInst PInst =3D getPseudoInstFromCall(Call); + + if (F->isIntrinsic()) { + if (!translateIntrinsicCall(Out, Call, F, Args, Mapper= )) { + return mkError("Unable to map intrinsic: ", Call); + } + } else if (PInst !=3D InvalidPseudoInst) { + if (!translatePseudoInstCall(Out, Call, PInst, Args, M= apper, + TcgGlobals)) { + return mkError("Unable to map pseudo inst: ", Call= ); + } + } else if (StrippedName =3D=3D "extract32") { + Expected MaybeRes =3D mapCallReturnValue(Mapper,= Call); + if (!MaybeRes) { + return MaybeRes.takeError(); + } + tcg::genExtract(Out, false, *MaybeRes, Args[0], Args[1= ], + Args[2]); + } else if (StrippedName =3D=3D "extract64") { + Expected MaybeRes =3D mapCallReturnValue(Mapper,= Call); + if (!MaybeRes) { + return MaybeRes.takeError(); + } + tcg::genExtract(Out, false, *MaybeRes, Args[0], Args[1= ], + Args[2]); + } else if (StrippedName =3D=3D "sextract32") { + Expected MaybeRes =3D mapCallReturnValue(Mapper,= Call); + if (!MaybeRes) { + return MaybeRes.takeError(); + } + tcg::genExtract(Out, true, *MaybeRes, Args[0], Args[1], + Args[2]); + } else if (StrippedName =3D=3D "sextract64") { + Expected MaybeRes =3D mapCallReturnValue(Mapper,= Call); + if (!MaybeRes) { + return MaybeRes.takeError(); + } + tcg::genExtract(Out, true, *MaybeRes, Args[0], Args[1], + Args[2]); + } else if (StrippedName =3D=3D "deposit32") { + Expected MaybeRes =3D mapCallReturnValue(Mapper,= Call); + if (!MaybeRes) { + return MaybeRes.takeError(); + } + tcg::genDeposit(Out, *MaybeRes, Args[0], Args[1], Args= [2], + Args[3]); + } else if (StrippedName =3D=3D "deposit64") { + Expected MaybeRes =3D mapCallReturnValue(Mapper,= Call); + if (!MaybeRes) { + return MaybeRes.takeError(); + } + tcg::genDeposit(Out, *MaybeRes, Args[0], Args[1], Args= [2], + Args[3]); + } else if (Name.startswith("helper")) { + // Map and adapt arguments to the call + SmallVector IArgs; + for (auto Arg : Args) { + IArgs.push_back(tcg::materialize(Arg)); + } + tcg::genCallHelper(Out, Name, IArgs.begin(), IArgs.end= ()); + } else { + if (F->isDeclaration()) { + return mkError("call to declaration: ", Call); + } + if (HasTranslatedFunction.find(F) =3D=3D + HasTranslatedFunction.end()) { + return mkError( + "call to function which failed to translate: ", + Call); + } + + // Map and adapt arguments to the call + + Expected MaybeRes =3D mapCallReturnValue(Mapper,= Call); + + StringRef Name =3D F->getName(); + Name.consume_front("helper_"); + Out << "emit_" << Name << "("; + + if (MaybeRes) { + Out << tcg::getName(MaybeRes.get()); + if (!Args.empty()) { + Out << ", "; + } + } + + for (unsigned i =3D 0; i < Args.size(); ++i) { + Out << tcg::getName(tcg::materialize(Args[i])); + if (i < Args.size() - 1) { + Out << ", "; + } + } + Out << ");\n"; + } + + } break; + case Instruction::ICmp: { + auto *ICmp =3D cast(&I); + Expected Op1 =3D Mapper.mapAndEmit(I.getOperand(0)); + if (!Op1) { + return mkError("Couldn't map first op: ", ICmp); + } + Expected Op2 =3D Mapper.mapAndEmit(I.getOperand(1)); + if (!Op2) { + return mkError("Couldn't map first op: ", ICmp); + } + // If both operands are immediates (constant expressions, = we can + // perform the operation as a constant expression. + if (Op1.get().Kind =3D=3D IrImmediate and + Op2.get().Kind =3D=3D IrImmediate) { + Mapper.mapExplicitly( + ICmp, + c::compare(ICmp->getPredicate(), Op1.get(), Op2.ge= t())); + break; + } + + ICmpInst::Predicate LlvmPred =3D ICmp->getPredicate(); + + if (Op1.get().Kind =3D=3D IrPtrToOffset) { + Expected Res =3D Mapper.mapCondAndEmit( + &I, Op1.get().TcgSize, Op1.get().LlvmSize); + if (!Res) { + return mkError("couldn't map icmp result", &I); + } + tcg::genVecCmp(Out, Res.get(), LlvmPred, Op1.get(), + Op2.get()); + } else { + Expected Res =3D + Mapper.mapCondAndEmit(&I, Op1.get().TcgSize, 1); + if (!Res) { + return mkError("couldn't map icmp result", &I); + } + auto IOp1 =3D tcg::materialize(Op1.get()); + if (ICmp->isSigned()) { + ensureSignBitIsSet(Out, IOp1); + ensureSignBitIsSet(Out, Op2.get()); + } + if (Op2.get().Kind =3D=3D IrImmediate) { + tcg::genSetcondI(Out, LlvmPred, Res.get(), IOp1, + Op2.get()); + } else { + tcg::genSetcond(Out, LlvmPred, Res.get(), IOp1, + Op2.get()); + } + } + + } break; + case Instruction::Select: { + auto Select =3D cast(&I); + Expected Res =3D Mapper.mapAndEmit(&I); + if (!Res) { + return mkError("Couldn't map select result", &I); + } + if (Res.get().Kind =3D=3D IrPtr) { + return mkError( + "Select statements for pointer types not supported= : ", + Select); + } + Expected Cond =3D Mapper.mapAndEmit(Select->getCondi= tion()); + if (!Cond) { + return mkError("Error mapping select cond"); + } + Expected True =3D Mapper.mapAndEmit(Select->getTrueV= alue()); + if (!True) { + return mkError("Couldn't map True for select instructi= on: ", + Select); + } + Expected False =3D + Mapper.mapAndEmit(Select->getFalseValue()); + if (!False) { + return mkError( + "Couldn't map False for select instruction: ", Sel= ect); + } + + if (Res.get().Kind =3D=3D IrPtrToOffset) { + tcg::genVecBitsel(Out, Res.get(), Cond.get(), True.get= (), + False.get()); + } else if (Cond.get().Kind =3D=3D IrImmediate) { + assert(Res.get().Kind !=3D IrImmediate); + const TcgV MTrue =3D tcg::materialize(True.get()); + const TcgV MFalse =3D tcg::materialize(False.get()); + tcg::genMov(Out, Res.get(), + c::ternary(Cond.get(), MTrue, MFalse)); + } else { + TcgV Zero =3D TcgV::makeImmediate("0", Res.get().TcgSi= ze, 1); + TcgSizeAdapter ACond(Out, Cond.get()); + TcgSizeAdapter ATrue(Out, True.get()); + TcgSizeAdapter AFalse(Out, False.get()); + if (True.get().Kind =3D=3D IrImmediate or + False.get().Kind =3D=3D IrImmediate) { + auto CTrue =3D + tcg::materialize(ATrue.get(Res.get().TcgSize)); + auto CFalse =3D + tcg::materialize(AFalse.get(Res.get().TcgSize)= ); + + tcg::genMovcond(Out, CmpInst::Predicate::ICMP_NE, + Res.get(), ACond.get(CTrue.TcgSize= ), + Zero, CTrue, CFalse); + } else { + tcg::genMovcond(Out, CmpInst::Predicate::ICMP_NE, + Res.get(), + ACond.get(True.get().TcgSize), Zer= o, + ATrue.get(Res.get().TcgSize), + AFalse.get(Res.get().TcgSize)); + } + } + } break; + case Instruction::Ret: { + auto Ret =3D cast(&I); + if (Ret->getNumOperands() =3D=3D 0) + break; + + assert(TAD.ReturnValue.hasValue()); + Expected Tcg =3D Mapper.mapAndEmit(Ret->getReturnVal= ue()); + if (!Tcg) { + return Tcg.takeError(); + } + if (Tcg.get().Kind =3D=3D IrImmediate) { + tcg::genMovI(Out, *TAD.ReturnValue, Tcg.get()); + } else if (!TAD.SkipReturnMov) { + tcg::genMov(Out, *TAD.ReturnValue, Tcg.get()); + } + } break; + case Instruction::BitCast: { + // We currently identity-map `BitCast`s + // + // If the bitcast has a larger lifetime than the source + // variable, we need to allocate a new variable so we + // don't accidentally free too soon. + auto Bitcast =3D cast(&I); + Expected SrcVal =3D + Mapper.mapAndEmit(Bitcast->getOperand(0)); + if (!SrcVal) { + return SrcVal.takeError(); + } + auto *DstTy =3D Bitcast->getType(); + if (SrcVal.get().Kind =3D=3D IrPtrToOffset) { + auto *PtrTy =3D cast(DstTy); + auto *VecTy =3D + dyn_cast(PtrTy->getPointerElementType(= )); + if (!VecTy) { + return mkError("bitcast to unsuppored type: ", Bit= cast); + } + auto *IntTy =3D cast(VecTy->getElementTyp= e()); + uint32_t LlvmSize =3D IntTy->getBitWidth(); + uint32_t VectorElements =3D + compat::getVectorElementCount(VecTy); + uint32_t VectorSize =3D LlvmSize * VectorElements; + TcgV Tcg =3D SrcVal.get(); + uint32_t TcgVectorSize =3D llvmToTcgSize(VectorSize); + Tcg.TcgSize =3D TcgVectorSize; + Tcg.LlvmSize =3D LlvmSize; + Tcg.VectorElementCount =3D VectorElements; + Tcg.Kind =3D IrPtrToOffset; + Mapper.mapExplicitly(Bitcast, Tcg); + } else if (DstTy->isPointerTy()) { + auto *ElmTy =3D DstTy->getPointerElementType(); + if (ElmTy->isIntegerTy()) { + auto *IntTy =3D cast(ElmTy); + const uint32_t TcgSize =3D + llvmToTcgSize(IntTy->getBitWidth()); + if (TcgSize =3D=3D SrcVal.get().TcgSize) { + Mapper.mapExplicitly(Bitcast, SrcVal.get()); + } else { + return mkError("Invalid bitcast changes tcg si= ze: ", + &I); + } + } else if (ElmTy->isArrayTy()) { + return mkError("Bitcast to unsupported type: ", &I= ); + } else { + Mapper.mapExplicitly(Bitcast, SrcVal.get()); + } + } else if (DstTy->isVectorTy()) { + auto *VecTy =3D cast(DstTy); + auto *IntTy =3D cast(VecTy->getElementTyp= e()); + uint32_t LlvmSize =3D IntTy->getBitWidth(); + uint32_t VectorElements =3D + compat::getVectorElementCount(VecTy); + uint32_t VectorSize =3D LlvmSize * VectorElements; + uint32_t TcgVectorSize =3D llvmToTcgSize(VectorSize); + TcgV Tcg =3D SrcVal.get(); + Tcg.TcgSize =3D TcgVectorSize; + Tcg.LlvmSize =3D LlvmSize; + Tcg.VectorElementCount =3D VectorElements; + Tcg.Kind =3D IrPtrToOffset; + Mapper.mapExplicitly(Bitcast, Tcg); + } else { + return mkError("Unhandled bitcast type: ", Bitcast); + } + } break; + case Instruction::Load: { + auto *Load =3D cast(&I); + auto *LlvmPtr =3D Load->getPointerOperand(); + + Expected Mapped =3D Mapper.mapAndEmit(LlvmPtr); + if (!Mapped) { + return Mapped.takeError(); + } + switch (Mapped.get().Kind) { + case IrPtr: { + Expected Res =3D Mapper.mapAndEmit(Load); + if (!Res) { + return Res.takeError(); + } + tcg::genLd(Out, Res.get(), Mapped.get(), 0); + } break; + case IrImmediate: { + Expected Res =3D Mapper.mapAndEmit(Load); + if (!Res) { + return Res.takeError(); + } + // Add pointer dereference to immediate address + tcg::genMovI(Out, Res.get(), + c::deref(Mapped.get(), Res.get().LlvmSize, + Res.get().TcgSize)); + } break; + case IrValue: { + Expected Res =3D Mapper.mapAndEmit(Load); + if (!Res) { + return Res.takeError(); + } + tcg::genMov(Out, Res.get(), Mapped.get()); + } break; + case IrPtrToOffset: { + // Loads from IrPtrToOffset are identity mapped, they = are an + // artifact of IrPtrToOffset arguments being pointers. + // Stores to results are instead taken care of by what= ever + // instruction generated the result. + if (isa(Load->getType())) { + Mapper.mapExplicitly(Load, Mapped.get()); + } + } break; + default: + return mkError("Load from unsupported TcgV type"); + }; + + } break; + case Instruction::Store: { + auto *Store =3D cast(&I); + Expected Val =3D + Mapper.mapAndEmit(Store->getValueOperand()); + if (!Val) { + return Val.takeError(); + } + auto *LlvmPtr =3D Store->getPointerOperand(); + Expected Mapped =3D Mapper.mapAndEmit(LlvmPtr); + if (!Mapped) { + return Mapped.takeError(); + } + if (Mapped.get().Kind =3D=3D IrValue) { + switch (Val.get().Kind) { + case IrImmediate: { + tcg::genMovI(Out, Mapped.get(), Val.get()); + } break; + case IrValue: { + tcg::genMov(Out, Mapped.get(), Val.get()); + } break; + default: + return mkError("Store from unsupported TcgV type"); + }; + } else if (Mapped.get().Kind =3D=3D IrPtr) { + tcg::genSt(Out, Mapped.get(), tcg::materialize(Val.get= ()), + 0); + } else if (Mapped.get().Kind =3D=3D IrPtrToOffset) { + // Stores to IrPtrToOffset are ignored, they are an ar= tifact + // of IrPtrToOffset arguments being pointers. Stores to + // results are instead taken care of by whatever instr= uction + // generated the result. + } else { + return mkError("Store to unsupported TcgV kind: ", Sto= re); + } + } break; + case Instruction::Unreachable: { + Out << "/* unreachable */\n"; + } break; + case Instruction::Switch: { + auto Switch =3D cast(&I); + // Operands to switch instructions alternate between + // case values and the corresponding label: + // Operands: { Cond, DefaultLabel, Case0, Label0, Case1, + // Label1, ... } + Expected Val =3D Mapper.mapAndEmit(Switch->getOperan= d(0)); + if (!Val) { + return Val.takeError(); + } + const TcgV DefaultLabel =3D Mapper.mapBbAndEmit( + cast(Switch->getOperand(1))); + for (uint32_t i =3D 2; i < Switch->getNumOperands(); i += =3D 2) { + Expected BranchVal =3D + Mapper.mapAndEmit(Switch->getOperand(i)); + if (!BranchVal) { + return BranchVal.takeError(); + } + const TcgV BranchLabel =3D Mapper.mapBbAndEmit( + cast(Switch->getOperand(i + 1))); + tcg::genBrcond(Out, CmpInst::Predicate::ICMP_EQ, Val.g= et(), + BranchVal.get(), BranchLabel); + } + tcg::genBr(Out, DefaultLabel); + } break; + case Instruction::Freeze: { + } break; + default: { + return mkError("Instruction not yet implemented", &I); + } + } + } + } + + Out << "}\n"; + + Out.flush(); + HeaderWriter.flush(); + DispatchWriter.flush(); + DispatchCallWriter.flush(); + + return TF; +} + +PreservedAnalyses TcgGenPass::run(Module &M, ModuleAnalysisManager &MAM) +{ + auto &CG =3D MAM.getResult(M); + + // Vector of translation results + SmallVector TranslatedFunctions; + // Two sets used for quickly looking up whether or not a function has + // already been translated, or the translation failed. + SmallPtrSet FailedToTranslateFunction; + SmallPtrSet HasTranslatedFunction; + for (Function &F : M) { + if (F.isDeclaration()) { + continue; + } + + // Depth first traversal of call graph. Needed to ensure called + // functions are translated before the current function. + CallGraphNode *Node =3D CG[&F]; + for (auto *N : make_range(po_begin(Node), po_end(Node))) { + Function *F =3D N->getFunction(); + + // If F in the call graph has already been translated and fail= ed, + // abort translation of the current function. (NOTE: use of .f= ind() + // over .contains() is to appease LLVM 10.) + bool FailedTranslation =3D FailedToTranslateFunction.find(F) != =3D + FailedToTranslateFunction.end(); + if (FailedTranslation) { + break; + } + + // Skip translation of invalid functions or functions that have + // already been translated. (NOTE: use of .find() over .contai= ns() + // is to appease LLVM 10.) + bool AlreadyTranslated =3D + HasTranslatedFunction.find(F) !=3D HasTranslatedFunction.e= nd(); + if (!F or F->isDeclaration() or AlreadyTranslated) { + continue; + } + + tcg::resetNameIndices(); + + auto Translated =3D translateFunction(F, TcgGlobals, Annotatio= ns, + HasTranslatedFunction); + if (!Translated) { + FailedToTranslateFunction.insert(F); + OutLog << F->getName() << ": " << Translated.takeError() + << "\n"; + if (ErrorOnTranslationFailure) { + return PreservedAnalyses::all(); + } else { + break; + } + } + + TranslatedFunctions.push_back(*Translated); + HasTranslatedFunction.insert(F); + OutLog << F->getName() << ": OK\n"; + } + } + + // Preamble + OutSource << "#include \"qemu/osdep.h\"\n"; + OutSource << "#include \"qemu/log.h\"\n"; + OutSource << "#include \"cpu.h\"\n"; + OutSource << "#include \"tcg/tcg-op.h\"\n"; + OutSource << "#include \"tcg/tcg-op-gvec.h\"\n"; + OutSource << "#include \"tcg/tcg.h\"\n"; + OutSource << "#include \"tcg/tcg-global-mappings.h\"\n"; + OutSource << "#include \"exec/exec-all.h\"\n"; + OutSource << "#include \"exec/helper-gen.h\"\n"; + OutSource << '\n'; + + OutSource << "#include \"" + << HeaderPath.substr(HeaderPath.find_last_of('/') + 1) << "\= "\n"; + OutSource << '\n'; + + // Emit extern definitions for all global TCGv_* that are mapped + // to the CPUState. + for (auto &P : TcgGlobals) { + const TcgGlobal &Global =3D P.second; + const uint32_t Size =3D llvmToTcgSize(Global.Size); + OutSource << "extern " << "TCGv_i" << Size << " " << Global.Code; + if (Global.NumElements > 1) { + OutSource << "[" << Global.NumElements << "]"; + } + OutSource << ";\n"; + } + + c::emitVectorPreamble(OutSource); + + // Emit translated functions + for (auto &TF : TranslatedFunctions) { + OutSource << TF.Code << '\n'; + OutHeader << TF.Decl << '\n'; + OutEnabled << TF.Name << '\n'; + } + + // Emit a dispatched to go from helper function address to our + // emitted code, if we succeeded. + OutHeader << "int helper_to_tcg_dispatcher(void *func, TCGTemp *ret_te= mp, " + "int nargs, TCGTemp **args);\n"; + + OutSource << "\n"; + OutSource << "#include \"exec/helper-proto.h\"\n"; + OutSource << "int helper_to_tcg_dispatcher(void *func, TCGTemp *ret_te= mp, " + "int nargs, TCGTemp **args) {\n"; + for (auto &TF : TranslatedFunctions) { + if (!TF.IsHelper or TF.DispatchCode.empty()) { + continue; + } + OutSource << " if ((uintptr_t) func =3D=3D (uintptr_t) helper_" + << TF.Name << ") {\n"; + OutSource << TF.DispatchCode; + OutSource << " return 1;\n"; + OutSource << " }\n"; + } + OutSource << " return 0;\n"; + OutSource << "}\n"; + + return PreservedAnalyses::all(); +} diff --git a/subprojects/helper-to-tcg/passes/backend/TcgGenPass.h b/subpro= jects/helper-to-tcg/passes/backend/TcgGenPass.h new file mode 100644 index 0000000000..0bbd4782e2 --- /dev/null +++ b/subprojects/helper-to-tcg/passes/backend/TcgGenPass.h @@ -0,0 +1,57 @@ +// +// Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. +// +// This program is free software; you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation; either version 2 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, see . +// + +#pragma once + +#include "FunctionAnnotation.h" +#include "TcgGlobalMap.h" +#include + +// +// TcgGenPass +// +// Backend pass responsible for emitting the final TCG code. Ideally this= pass +// should be as simple as possible simply mapping one expression LLVM IR +// directly to another in TCG. +// +// However, we currently still rely on this pass to perform the mapping of +// constants. (mapping of values is handled by the TcgTempAllocationPass.) +// + +class TcgGenPass : public llvm::PassInfoMixin { + llvm::raw_ostream &OutSource; + llvm::raw_ostream &OutHeader; + llvm::raw_ostream &OutEnabled; + llvm::raw_ostream &OutLog; + llvm::StringRef HeaderPath; + const AnnotationMapTy &Annotations; + const TcgGlobalMap &TcgGlobals; + +public: + TcgGenPass(llvm::raw_ostream &OutSource, llvm::raw_ostream &OutHeader, + llvm::raw_ostream &OutEnabled, llvm::raw_ostream &OutLog, + llvm::StringRef HeaderPath, const AnnotationMapTy &Annotati= ons, + const TcgGlobalMap &TcgGlobals) + : OutSource(OutSource), OutHeader(OutHeader), OutEnabled(OutEnable= d), + OutLog(OutLog), HeaderPath(HeaderPath), Annotations(Annotations), + TcgGlobals(TcgGlobals) + { + } + + llvm::PreservedAnalyses run(llvm::Module &M, + llvm::ModuleAnalysisManager &MAM); +}; diff --git a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp b/subprojects/= helper-to-tcg/pipeline/Pipeline.cpp index 004c16550a..3664603451 100644 --- a/subprojects/helper-to-tcg/pipeline/Pipeline.cpp +++ b/subprojects/helper-to-tcg/pipeline/Pipeline.cpp @@ -34,12 +34,14 @@ #include #include #include +#include #include #include #include =20 #include #include +#include #include =20 using namespace llvm; @@ -81,6 +83,30 @@ cl::opt "for allocating temporary gvec variables"), cl::init("tmp_vmem"), cl::cat(Cat)); =20 +// Options for TcgGenPass +cl::opt OutputSourceFile("output-source", + cl::desc("output .c file"), + cl::init("helper-to-tcg-emitted.c"), + cl::cat(Cat)); + +cl::opt OutputHeaderFile("output-header", + cl::desc("output .h file"), + cl::init("helper-to-tcg-emitted.h"), + cl::cat(Cat)); + +cl::opt + OutputEnabledFile("output-enabled", + cl::desc("output list of parsed functions"), + cl::init("helper-to-tcg-enabled"), cl::cat(Cat)); + +cl::opt OutputLogFile("output-log", cl::desc("output log file= "), + cl::init("helper-to-tcg-log"), cl::cat(= Cat)); + +cl::opt + ErrorOnTranslationFailure("error-on-translation-failure", + cl::desc("Abort translation on first failure= "), + cl::init(false), cl::cat(Cat)); + // Define a TargetTransformInfo (TTI) subclass, this allows for overriding // common per-llvm-target information expected by other LLVM passes, such // as the width of the largest scalar/vector registers. Needed for consis= tent @@ -244,5 +270,28 @@ int main(int argc, char **argv) MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM))); } =20 + // + // Finally we run a backend pass that converts from LLVM IR to TCG, + // and emits the final code. + // + + std::error_code EC; + ToolOutputFile OutSource(OutputSourceFile, EC, compat::OpenFlags); + ToolOutputFile OutHeader(OutputHeaderFile, EC, compat::OpenFlags); + ToolOutputFile OutEnabled(OutputEnabledFile, EC, compat::OpenFlags); + ToolOutputFile OutLog(OutputLogFile, EC, compat::OpenFlags); + assert(!EC); + + MPM.addPass(TcgGenPass(OutSource.os(), OutHeader.os(), OutEnabled.os(), + OutLog.os(), OutputHeaderFile, Annotations, + TcgGlobals)); + + MPM.run(*M, MAM); + + OutSource.keep(); + OutHeader.keep(); + OutEnabled.keep(); + OutLog.keep(); + return 0; } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153925; cv=none; d=zohomail.com; s=zohoarc; b=nSFP5eWIzLYjIWp2qPkTs2YOmlnBUgCxQ+yZuFzeywY65sPwi19EMYtfQ4PWN09Gyfb1lwVeyUzSbjspi03k+KVUmdUvJI5+Igh71Ns6fcydLHXdKAoRIZ/eRoZYv1eodvl4Yh1YekTDx5LhCv0F6QjN27QjDQ64p6QUKRvcRgs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153925; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=o0Qxb4icP5QHxebSgta6L/OLUkbwEJuI+/53RhwccoA=; b=JqOqeG3bI76gzEtnNJkbWapRCDGpCg0pakHmG49koM4o0QSctMZy4MQOif9dKL/WkP4CxjIEUchTX3bPPJ9joSsi3ilHBcdgUBkL1GtiU8yljP7cAAY1o36ZpI4Ltx1dM/NzoXpQI+R5M34V6hVFW7FYKIhHXjV+Jb3qS1iMgLc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153925356903.3968348221349; Wed, 20 Nov 2024 17:52:05 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJn-0006UC-Ix; Wed, 20 Nov 2024 20:49:15 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJd-0006EC-MG for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:05 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJY-0004pJ-NN for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=o0Qxb4icP5QHxebSgta6L/OLUkbwEJuI+/53RhwccoA=; b=ulBmn1bzcH+0Ntg ZrTqWVHoPdEAAzzrT8Lb5SpW/rFfVf+ZAgBVj05Ex4s3X+4Ilb8BgVA5RRhHyKp3+bHEWhuRjy7vu 16mSOLDpllGQRt68NRMiEC/jHoUtV/cH8uz/yoSign7cPCmlDpexjkjxYhONQhOLNdwwXQ8nAf3oB Xc=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 32/43] helper-to-tcg: Add README Date: Thu, 21 Nov 2024 02:49:36 +0100 Message-ID: <20241121014947.18666-33-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153927141116600 Content-Type: text/plain; charset="utf-8" Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/README.md | 265 ++++++++++++++++++++++++++++ 1 file changed, 265 insertions(+) create mode 100644 subprojects/helper-to-tcg/README.md diff --git a/subprojects/helper-to-tcg/README.md b/subprojects/helper-to-tc= g/README.md new file mode 100644 index 0000000000..8d1304ef4f --- /dev/null +++ b/subprojects/helper-to-tcg/README.md @@ -0,0 +1,265 @@ +# helper-to-tcg + +`helper-to-tcg` is a standalone LLVM IR to TCG translator, with the goal o= f simplifying the implementation of complicated instructions in TCG. Instru= ction semantics can be specified either directly in LLVM IR or any language= that can be compiled to it (C, C++, ...). However, the tool is tailored to= wards QEMU helper functions written in C. + +Internally, `helper-to-tcg` consists of a mix of custom and built-in trans= formation and analysis passes that are applied to the input LLVM IR sequent= ially. The pipeline of passes is laid out as follows +``` + +---------------+ +-----+ +---------------+ +---------= ---+ +LLVM IR -> | PrepareForOpt | -> | -Os | -> | PrepareForTcg | -> | TcgGenPa= ss | -> TCG + +---------------+ +-----+ +---------------+ +---------= ---+ +``` +where the custom passes performs: +* `PrepareForOpt` - Early culling of unneeded functions, mapping of functi= on annotations, removal of `noinline` added by `-O0` +* `PrepareForTcg` - Post-optimization pass that tries to get the IR as clo= se to Tinycode as possible, goal is to take complexity away from the backen= d; +* `TcgGenPass` - Backend pass that allocates TCG variables to LLVM values,= and emits final TCG C code. + +As for LLVM optimization, `-Os` strikes a good balance between unrolling a= nd vectorization, from testing. More aggressive optimization levels would o= ften unroll loops over compacting it with loop vectorization. + +## Project Structure + +* `get-llvm-ir.py` - Helper script to convert a QEMU .c file to LLVM IR by= getting compile flags from `compile_commands.json`. +* `pipeline` - Implementation of pipeline orchestrating LLVM passes and ha= ndling input. +* `passes` - Implementation of custom LLVM passes (`PrepareForOpt`,`Prepar= eForTcg`,`TcgGenPass`). +* `include` - Shared headers between `passes/pipeline`. +* `tests` - Simple end-to-end tests of C functions we expect to be able to= translate, tests fail if any function fails to translate, output is not ve= rified. + +## Example Translations + +`helper-to-tcg` is able to deal with a wide variety of helper functions, t= he following code snippet contains two examples from the Hexagon architectu= re implementing the semantics of a predicated and instruction (`A2_pandt`) = and a vectorized signed saturated 2-element scalar product (`V6_vdmpyhvsat`= ). + +```c +int32_t HELPER(A2_pandt)(CPUHexagonState *env, int32_t RdV, + int32_t PuV, int32_t RsV, int32_t RtV) +{ + if(fLSBOLD(PuV)) { + RdV=3DRsV&RtV; + } else { + CANCEL; + } + return RdV; +} + +void HELPER(V6_vdmpyhvsat)(CPUHexagonState *env, + void * restrict VdV_void, + void * restrict VuV_void, + void * restrict VvV_void) +{ + fVFOREACH(32, i) { + size8s_t accum =3D fMPY16SS(fGETHALF(0,VuV.w[i]),fGETHALF(0, VvV.w= [i])); + accum +=3D fMPY16SS(fGETHALF(1,VuV.w[i]),fGETHALF(1, VvV.w[i])); + VdV.w[i] =3D fVSATW(accum); + } +} +``` +For the above snippet, `helper-to-tcg` produces the following TCG +```c +void emit_A2_pandt(TCGv_i32 temp0, TCGv_env env, TCGv_i32 temp4, + TCGv_i32 temp8, TCGv_i32 temp7, TCGv_i32 temp6) { + TCGv_i32 temp2 =3D tcg_temp_new_i32(); + tcg_gen_andi_i32(temp2, temp8, 1); + TCGv_i32 temp5 =3D tcg_temp_new_i32(); + tcg_gen_and_i32(temp5, temp6, temp7); + tcg_gen_movcond_i32(TCG_COND_EQ, temp0, temp2, tcg_constant_i32(0), te= mp4, temp5); +} + +void emit_V6_vdmpyhvsat(TCGv_env env, intptr_t vec3, + intptr_t vec7, intptr_t vec6) { + VectorMem mem =3D {0}; + intptr_t vec0 =3D temp_new_gvec(&mem, 128); + tcg_gen_gvec_shli(MO_32, vec0, vec7, 16, 128, 128); + intptr_t vec5 =3D temp_new_gvec(&mem, 128); + tcg_gen_gvec_sari(MO_32, vec5, vec0, 16, 128, 128); + intptr_t vec1 =3D temp_new_gvec(&mem, 128); + tcg_gen_gvec_shli(MO_32, vec1, vec6, 16, 128, 128); + tcg_gen_gvec_sari(MO_32, vec1, vec1, 16, 128, 128); + tcg_gen_gvec_mul(MO_32, vec1, vec1, vec5, 128, 128); + intptr_t vec2 =3D temp_new_gvec(&mem, 128); + tcg_gen_gvec_sari(MO_32, vec2, vec7, 16, 128, 128); + tcg_gen_gvec_sari(MO_32, vec0, vec6, 16, 128, 128); + tcg_gen_gvec_mul(MO_32, vec2, vec0, vec2, 128, 128); + tcg_gen_gvec_ssadd(MO_32, vec3, vec1, vec2, 128, 128); +} +``` + +In the first case, the predicated and instruction was made branchless by u= sing a conditional move, and in the latter case the inner loop of the vecto= rized scalar product could be converted to a few vectorized shifts and mult= iplications, folllowed by a vectorized signed saturated addition. + +## Usage + +Building `helper-to-tcg` produces a binary implementing the pipeline outli= ned above, going from LLVM IR to TCG. + +### Specifying Functions to Translate + +Unless `--translate-all-helpers` is specified, the default behaviour of `h= elper-to-tcg` is to only translate functions annotated via a special `"help= er-to-tcg"` annotation. Functions called by annotated functions will also b= e translated, see the following example: + +```c +// Function will be translated, annotation provided +__attribute__((annotate ("helper-to-tcg"))) +int f(int a, int b) { + return 2 * g(a, b); +} + +// Function will be translated, called by annotated `f()` function +int g(int a, int b) { + ... +} + +// Function will not be translated +int h(int a, int b) { + ... +} +``` + +### Immediate and Vector Arguments + +Function annotations are in some cases used to provide extra information t= o `helper-to-tcg` not otherwise present in the IR. For example, whether an = integer argument should actually be treated as an immediate rather than a r= egister, or if a pointer argument should be treated as a `gvec` vector (off= set into `CPUArchState`). For instance: +```c +__attribute__((annotate ("helper-to-tcg"))) +__attribute__((annotate ("immediate: 1"))) +int f(int a, int i) { + ... +} + +__attribute__((annotate ("helper-to-tcg"))) +__attribute__((annotate ("ptr-to-offset: 0, 1"))) +void g(void * restrict a, void * restrict b) { + ... +} +``` +where `"immediate: 1"` tells `helper-to-tcg` that the argument with index = `1` should be treated as an immediate (multiple arguments are specified thr= ough a comma separated list). Similarly `"ptr-to-offset: 0, 1"` indicates t= hat arguments width index 0 and 1 should be treated as offsets from `CPUArc= hState` (given as `intptr_t`), rather than actual pointer arguments. For th= e above code, `helper-to-tcg` emits +```c +void emit_f(TCGv_i32 res, TCGv_i32 a, int i) { + ... +} + +void emit_g(intptr_t a, intptr_t b) { + ... +} +``` + +### Loads and Stores + +Translating loads and stores is slightly trickier, as some QEMU specific a= ssumptions are made. Loads and stores in the input are assumed to go throug= h the `cpu_[st|ld]*()` functions defined in `exec/cpu_ldst.h` that a helper= function would use.=20 + +If using standalone input functions (not QEMU helper functions), loads and= stores are still represented by `cpu_[st|ld]*()` which needs to be declare= d, consider: +```c +/* Opaque CPU state type, will be mapped to tcg_env */ +struct CPUArchState; +typedef struct CPUArchState CPUArchState; + +/* Prototype of QEMU helper guest load/store functions, see exec/cpu_ldst.= h */ +uint32_t cpu_ldub_data(CPUArchState *, uint32_t ptr); +void cpu_stb_data(CPUArchState *, uint32_t ptr, uint32_t data); + +uint32_t helper_ld8(CPUArchState *env, uint32_t addr) { + return cpu_ldub_data(env, addr); +} + +void helper_st8(CPUArchState *env, uint32_t addr, uint32_t data) { + return cpu_stb_data(env, addr, data); +} +``` +implementing an 8-bit load and store instruction, these will be translated= to the following TCG. +```c +void emit_ld8(TCGv_i32 temp0, TCGv_env env, TCGv_i32 temp1) { + tcg_gen_qemu_ld_i32(temp0, temp1, tb_mmu_index(tcg_ctx->gen_tb->flags)= , MO_UB); +} + +void emit_st8(TCGv_env env, TCGv_i32 temp0, TCGv_i32 temp1) { + tcg_gen_qemu_st_i32(temp1, temp0, tb_mmu_index(tcg_ctx->gen_tb->flags)= , MO_UB); +} +``` +Note, the emitted code assumes the definition of a `tb_mmu_index()` functi= on to retrieve the current CPU MMU index, the name of this function can be = configured via the `--mmu-index-function` flag. + +### Mapping CPU State + +In QEMU, commonly accessed fields in the `CPUArchState` are often mapped t= o global `TCGv*` variables representing that piece of CPU state in TCG. Whe= n translating helper functions (or other C functions), a method of specifyi= ng which fields in the CPU state should be mapped to which globals is neede= d. To this end, a declarative approach is taken, where mappings between CPU= state and globals can be consumed by both `helper-to-tcg` and runtime QEMU= for instantiating the `TCGv` globals themselves. + +Users must define this mapping via a global `cpu_tcg_mapping []` array, as= can be seen in the following example where `mapped_field` of `CPUArchState= ` is mapped to the global `tcg_field`. For more complicated examples see th= e tests in `tests/cpustate.c`. +```c +#include +#include "tcg/tcg-global-mappings.h" + +/* Define a CPU state with some different fields */ + +typedef struct CPUArchState { + uint32_t mapped_field; + uint32_t unmapped_field; +} CPUArchState; + +/* Dummy struct, in QEMU this would correspond to TCGv_i32 in tcg.h */ +typedef struct TCGv_i32 {} TCGv_i32; + +/* Global TCGv representing CPU state */ +TCGv_i32 tcg_field; + +/* + * Finally provide a mapping of CPUArchState to TCG globals we care about,= here + * we map mapped_field to tcg_field + */ +cpu_tcg_mapping mappings[] =3D { + CPU_TCG_MAP(CPUArchState, tcg_field, mapped_field, NULL), +}; + +uint32_t helper_mapped(CPUArchState *env) { + return env->mapped_field; +} + +uint32_t helper_unmapped(CPUArchState *env) { + return env->unmapped_field; +} +``` +Note, the name of the `cpu_tcg_mapping[]` is provided via the `--tcg-globa= l-mappings` flag. For the above example, `helper-to-tcg` emits +```c +extern TCGv_i32 tcg_field; + +void emit_mapped(TCGv_i32 temp0, TCGv_env env) { + tcg_gen_mov_i32(temp0, tcg_field); +} + +void emit_unmapped(TCGv_i32 temp0, TCGv_env env) { + TCGv_ptr ptr1 =3D tcg_temp_new_ptr(); + tcg_gen_addi_ptr(ptr1, env, 128ull); + tcg_gen_ld_i32(temp0, ptr1, 0); +} +``` +where accesses in the input C code are correctly mapped to the correspondi= ng TCG globals. The unmapped `CPUArchState` access turns into pointer math = and a load, whereas the mapped access turns into a `mov` from a global. + +### Automatic Calling of Generated Code + +Finally, calling the generated code is as simple as including the output o= f `helper-to-tcg` into the project and manually calling `emit_*(...)`. Howe= ver, when dealing with an existing frontend that has a lot of helper functi= ons already in use, we simplify this process somewhat for non-vector instru= ctions. `helper-to-tcg` can emit a dispatcher, which for the above CPU stat= e mapping example looks like +```c +int helper_to_tcg_dispatcher(void *func, TCGTemp *ret_temp, int nargs, TCG= Temp **args) { + if ((uintptr_t) func =3D=3D (uintptr_t) helper_mapped) { + TCGv_i32 temp0 =3D temp_tcgv_i32(ret_temp); + TCGv_env env =3D temp_tcgv_ptr(args[0]); + emit_mapped(temp0, env); + return 1; + } + if ((uintptr_t) func =3D=3D (uintptr_t) helper_unmapped) { + TCGv_i32 temp0 =3D temp_tcgv_i32(ret_temp); + TCGv_env env =3D temp_tcgv_ptr(args[0]); + emit_unmapped(temp0, env); + return 1; + } + return 0; +} +``` +Here `emit_mapped()` and `emit_unmapped()` are automatically called if the= current helper function call being translated `void *func` corresponds to = either of the input helper functions. If the fronend then defines +```c +#ifdef CONFIG_HELPER_TO_TCG +#define TARGET_HELPER_DISPATCHER helper_to_tcg_dispatcher +#endif +``` +in `cpu-param.h`, then calls to `gen_helper_mapped()` for instance, will e= nd up in `emit_mapped()` with no change to frontends. Additionally, dispatc= hing from helper calls allows for easy toggling of `helper-to-tcg`, which i= s increadibly useful for testing purposes. + +### Simple Command Usage + +Assume a `helpers.c` file with functions to translate, then to obtain LLVM= IR +```bash +$ clang helpers.c -O0 -Xclang -disable-O0-optnone -S -emit-llvm +``` +which produces `helpers.ll` to be fed into `helper-to-tcg` +```bash +$ ./helper-to-tcg helpers.ll --translate-all-helpers +``` +where `--translate-all-helpers` means "translate all functions starting wi= th helper_*". Finally, the above command produces `helper-to-tcg-emitted.[c= |h]` with emitted TCG code. --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153814; cv=none; d=zohomail.com; s=zohoarc; b=mXQZx7pfojhttHuF2RBL++CgdWhuST2aHbSpJElehaJWqFaRsxMODH+yu75JHZKujuQSPgKDJsH+Op16PiYwGh7c7vtNJ5pZ1Q0+PUflBZWUv2wTKdKiTfW/kDp/vC3x338hvUYio2cw/T/7/wJ/0Q4DNpXlR3jCD1bZ7XTz5XM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153814; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=jgCd+M5r15AZHXI9ALG7AycpEm51VFk6or8yUWOvEIc=; b=CtrbFnhQCc3j7+U5FEwkmioH95LmK1JDVmqHmkLj07M21bUHmb+UmhJD5S2Z+2irTrjSzC8k2Woa6ISjDVJfCuTq+J2YDRtHp8+auL2lfYNwLNAbM8JdNTuKz+aeuiNonYtF8ZDfGGXinM0seew8Eghek6VM+HSfK6UVkECrjAY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153814664280.4058805736147; Wed, 20 Nov 2024 17:50:14 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJX-0005tm-Rt; Wed, 20 Nov 2024 20:48:59 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJV-0005dx-4x for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:58 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJS-0004oR-Sr for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:56 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=jgCd+M5r15AZHXI9ALG7AycpEm51VFk6or8yUWOvEIc=; b=OS6n2Kfj/8mms83 8sAytOmvykelhX5+HLyIFNH4QF5m1uJLiUlinc/SttYg6XBQvKj0oL61SdzOXjrS6bmauM7llaBiB GVGTt61F6cmi+yblF0NeVbGlSjDYIDUD2lcafP7+khepcoiCynaplMNyA0+9DwJeqosBrH9Vbp1S/ Yo=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 33/43] helper-to-tcg: Add end-to-end tests Date: Thu, 21 Nov 2024 02:49:37 +0100 Message-ID: <20241121014947.18666-34-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153816764116600 Content-Type: text/plain; charset="utf-8" Introduces simple end-to-end tests of helper-to-tcg of functions the translator is expected to handle, any translation failure will result in a test failure. More test cases to come. Signed-off-by: Anton Johansson --- subprojects/helper-to-tcg/meson.build | 2 + subprojects/helper-to-tcg/tests/cpustate.c | 45 +++++++ subprojects/helper-to-tcg/tests/ldst.c | 17 +++ subprojects/helper-to-tcg/tests/meson.build | 24 ++++ subprojects/helper-to-tcg/tests/scalar.c | 15 +++ .../helper-to-tcg/tests/tcg-global-mappings.h | 115 ++++++++++++++++++ subprojects/helper-to-tcg/tests/vector.c | 26 ++++ 7 files changed, 244 insertions(+) create mode 100644 subprojects/helper-to-tcg/tests/cpustate.c create mode 100644 subprojects/helper-to-tcg/tests/ldst.c create mode 100644 subprojects/helper-to-tcg/tests/meson.build create mode 100644 subprojects/helper-to-tcg/tests/scalar.c create mode 100644 subprojects/helper-to-tcg/tests/tcg-global-mappings.h create mode 100644 subprojects/helper-to-tcg/tests/vector.c diff --git a/subprojects/helper-to-tcg/meson.build b/subprojects/helper-to-= tcg/meson.build index 4f045eb1da..e09f121e18 100644 --- a/subprojects/helper-to-tcg/meson.build +++ b/subprojects/helper-to-tcg/meson.build @@ -80,3 +80,5 @@ pipeline =3D executable('helper-to-tcg', sources, include_directories: ['passes', './', 'include'] + [= incdir], link_args: [ldflags] + [libs] + [syslibs], cpp_args: cpp_args) + +subdir('tests') diff --git a/subprojects/helper-to-tcg/tests/cpustate.c b/subprojects/helpe= r-to-tcg/tests/cpustate.c new file mode 100644 index 0000000000..79205da75e --- /dev/null +++ b/subprojects/helper-to-tcg/tests/cpustate.c @@ -0,0 +1,45 @@ +#include +#include "tcg-global-mappings.h" + +typedef struct SpecialData { + uint32_t a; + uint32_t unmapped_field; +} SpecialData; + +typedef struct CPUArchState { + uint32_t regs[32]; + uint32_t unmapped_field; + SpecialData data[8]; + uint32_t mapped_field; +} CPUArchState; + +/* Dummy struct, in QEMU this would correspond to TCGv_i32 in tcg.h */ +typedef struct TCGv_i32 {} TCGv_i32; +/* Global TCGv's representing CPU state */ +TCGv_i32 tcg_regs[32]; +TCGv_i32 tcg_a[8]; +TCGv_i32 tcg_field; + +cpu_tcg_mapping mappings[] =3D { + CPU_TCG_MAP_ARRAY(CPUArchState, tcg_regs, regs, NULL), + CPU_TCG_MAP_ARRAY_OF_STRUCTS(CPUArchState, tcg_a, data, a, NULL), + CPU_TCG_MAP(CPUArchState, tcg_field, mapped_field, NULL), +}; + +__attribute__((annotate ("immediate: 1"))) +uint32_t helper_reg(CPUArchState *env, uint32_t i) { + return env->regs[i]; +} + +__attribute__((annotate ("immediate: 1"))) +uint32_t helper_data_a(CPUArchState *env, uint32_t i) { + return env->data[i].a; +} + +uint32_t helper_single_mapped(CPUArchState *env) { + return env->mapped_field; +} + +uint32_t helper_unmapped(CPUArchState *env) { + return env->unmapped_field; +} diff --git a/subprojects/helper-to-tcg/tests/ldst.c b/subprojects/helper-to= -tcg/tests/ldst.c new file mode 100644 index 0000000000..44d32d0875 --- /dev/null +++ b/subprojects/helper-to-tcg/tests/ldst.c @@ -0,0 +1,17 @@ +#include + +/* Opaque CPU state type, will be mapped to tcg_env */ +struct CPUArchState; +typedef struct CPUArchState CPUArchState; + +/* Prototype of QEMU helper guest load/store functions, see exec/cpu_ldst.= h */ +uint32_t cpu_ldub_data(CPUArchState *, uint32_t ptr); +void cpu_stb_data(CPUArchState *, uint32_t ptr, uint32_t data); + +uint32_t helper_ld8(CPUArchState *env, uint32_t addr) { + return cpu_ldub_data(env, addr); +} + +void helper_st8(CPUArchState *env, uint32_t addr, uint32_t data) { + return cpu_stb_data(env, addr, data); +} diff --git a/subprojects/helper-to-tcg/tests/meson.build b/subprojects/help= er-to-tcg/tests/meson.build new file mode 100644 index 0000000000..e7b9329c82 --- /dev/null +++ b/subprojects/helper-to-tcg/tests/meson.build @@ -0,0 +1,24 @@ +sources =3D [ + 'scalar.c', + 'vector.c', + 'ldst.c', + 'cpustate.c', +] + + +foreach s : sources + name =3D s.split('.')[0] + name_ll =3D name + '.ll' + ll =3D custom_target(name_ll, + input: s, + output: name_ll, + command: [clang, '-O0', '-Xclang', '-disable-O0-optnone', + '-S', '-emit-llvm', '-o', '@OUTPUT@', '@INPUT@'] + ) + test(name, pipeline, + args: [ll, + '--mmu-index-function=3Dtb_mmu_index', + '--tcg-global-mappings=3Dmappings', + '--translate-all-helpers'], + suite: 'end-to-end') +endforeach diff --git a/subprojects/helper-to-tcg/tests/scalar.c b/subprojects/helper-= to-tcg/tests/scalar.c new file mode 100644 index 0000000000..09af72371d --- /dev/null +++ b/subprojects/helper-to-tcg/tests/scalar.c @@ -0,0 +1,15 @@ +#include + +/* Simple arithmetic */ +uint32_t helper_add(uint32_t a, uint32_t b) { + return a + b; +} + +/* Control flow reducable to conditinal move */ +uint32_t helper_cmov(uint32_t c0, uint32_t c1, uint32_t a, uint32_t b) { + if (c0 < c1) { + return a; + } else { + return b; + } +} diff --git a/subprojects/helper-to-tcg/tests/tcg-global-mappings.h b/subpro= jects/helper-to-tcg/tests/tcg-global-mappings.h new file mode 100644 index 0000000000..fed3577bcf --- /dev/null +++ b/subprojects/helper-to-tcg/tests/tcg-global-mappings.h @@ -0,0 +1,115 @@ +/* + * Copyright(c) 2024 rev.ng Labs Srl. All Rights Reserved. + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, see . + */ + +#ifndef TCG_GLOBAL_MAP_H +#define TCG_GLOBAL_MAP_H + +#include +#include + +#define _stringify(STR) #STR +#define stringify(STR) _stringify(TR) + +/** + * cpu_tcg_mapping: Declarative mapping of offsets into a struct to global + * TCGvs. Parseable by LLVM-based tools. + * @tcg_var_name: String name of the TCGv to use as destination of the map= ping. + * @tcg_var_base_address: Address of the above TCGv. + * @cpu_var_names: Array of printable names of TCGvs, used when calling + * tcg_global_mem_new from init_cpu_tcg_mappings. + * @cpu_var_base_offset: Base offset of field in the source struct. + * @cpu_var_size: Size of field in the source struct, if the field is an a= rray, + * this holds the size of the element type. + * @cpu_var_stride: Stride between array elements in the source struct. T= his + * can be greater than the element size when mapping a fi= eld + * in an array of structs. + * @number_of_elements: Number of elements of array in the source struct. + */ +typedef struct cpu_tcg_mapping { + const char *tcg_var_name; + void *tcg_var_base_address; + + const char *const *cpu_var_names; + size_t cpu_var_base_offset; + size_t cpu_var_size; + size_t cpu_var_stride; + + size_t number_of_elements; +} cpu_tcg_mapping; + +#define STRUCT_SIZEOF_FIELD(S, member) sizeof(((S *)0)->member) + +#define STRUCT_ARRAY_SIZE(S, array) = \ + (STRUCT_SIZEOF_FIELD(S, array) / STRUCT_SIZEOF_FIELD(S, array[0])) + +/* + * Following are a few macros that aid in constructing + * `cpu_tcg_mapping`s for a few common cases. + */ + +/* Map between single CPU register and to TCG global */ +#define CPU_TCG_MAP(struct_type, tcg_var, cpu_var, name_str) = \ + (cpu_tcg_mapping) = \ + { = \ + .tcg_var_name =3D stringify(tcg_var), .tcg_var_base_address =3D &t= cg_var, \ + .cpu_var_names =3D (const char *[]){name_str}, = \ + .cpu_var_base_offset =3D offsetof(struct_type, cpu_var), = \ + .cpu_var_size =3D STRUCT_SIZEOF_FIELD(struct_type, cpu_var), = \ + .cpu_var_stride =3D 0, .number_of_elements =3D 1, = \ + } + +/* Map between array of CPU registers and array of TCG globals. */ +#define CPU_TCG_MAP_ARRAY(struct_type, tcg_var, cpu_var, names) = \ + (cpu_tcg_mapping) = \ + { = \ + .tcg_var_name =3D #tcg_var, .tcg_var_base_address =3D tcg_var, = \ + .cpu_var_names =3D names, = \ + .cpu_var_base_offset =3D offsetof(struct_type, cpu_var), = \ + .cpu_var_size =3D STRUCT_SIZEOF_FIELD(struct_type, cpu_var[0]), = \ + .cpu_var_stride =3D STRUCT_SIZEOF_FIELD(struct_type, cpu_var[0]), = \ + .number_of_elements =3D STRUCT_ARRAY_SIZE(struct_type, cpu_var), = \ + } + +/* + * Map between single member in an array of structs to an array + * of TCG globals, e.g. maps + * + * cpu_state.array_of_structs[i].member + * + * to + * + * tcg_global_member[i] + */ +#define CPU_TCG_MAP_ARRAY_OF_STRUCTS(struct_type, tcg_var, cpu_struct, = \ + cpu_var, names) = \ + (cpu_tcg_mapping) = \ + { = \ + .tcg_var_name =3D #tcg_var, .tcg_var_base_address =3D tcg_var, = \ + .cpu_var_names =3D names, = \ + .cpu_var_base_offset =3D offsetof(struct_type, cpu_struct[0].cpu_v= ar), \ + .cpu_var_size =3D = \ + STRUCT_SIZEOF_FIELD(struct_type, cpu_struct[0].cpu_var), = \ + .cpu_var_stride =3D STRUCT_SIZEOF_FIELD(struct_type, cpu_struct[0]= ), \ + .number_of_elements =3D STRUCT_ARRAY_SIZE(struct_type, cpu_struct)= , \ + } + +extern cpu_tcg_mapping tcg_global_mappings[]; +extern size_t tcg_global_mapping_count; + +void init_cpu_tcg_mappings(cpu_tcg_mapping *mappings, size_t size); + +#endif /* TCG_GLOBAL_MAP_H */ diff --git a/subprojects/helper-to-tcg/tests/vector.c b/subprojects/helper-= to-tcg/tests/vector.c new file mode 100644 index 0000000000..c40f63b60d --- /dev/null +++ b/subprojects/helper-to-tcg/tests/vector.c @@ -0,0 +1,26 @@ +#include + +__attribute__((annotate("ptr-to-offset: 0"))) void +helper_vec_splat_reg(void *restrict d, uint8_t imm) +{ + for (int i =3D 0; i < 32; ++i) { + ((uint8_t *)d)[i] =3D imm; + } +} + +__attribute__((annotate("immediate: 1"))) +__attribute__((annotate("ptr-to-offset: 0"))) void +helper_vec_splat_imm(void *restrict d, uint8_t imm) +{ + for (int i =3D 0; i < 32; ++i) { + ((uint8_t *)d)[i] =3D imm; + } +} + +__attribute__((annotate("ptr-to-offset: 0, 1, 2"))) void +helper_vec_add(void *restrict d, void *restrict a, void *restrict b) +{ + for (int i =3D 0; i < 32; ++i) { + ((uint8_t *)d)[i] =3D ((uint8_t *)a)[i] + ((uint8_t *)b)[i]; + } +} --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153798; cv=none; d=zohomail.com; s=zohoarc; b=iDE8q0SdR6NMlyh8Rs/n1UoYSdAxjoNTF20nlZrU1ZTiCq+GoQdRnrEkYYpN/fJAPHpRSzoXu4N7nri/KYh6t0jZyJHc2ZtpLhDXn5DZfLoeXUURJeFXKtuDycmlsWCYs2BEZq1MeZDMPQeqcWZF0i4rnelyTAFmO9vwplKVClc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153798; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=XdRCsx0NU7YzRpnmN8Uf2wu+MGo6jaBAg0CJXu+Wbdo=; b=RDYT1WjC5jTicD7C86Gc5oVFZjdlcNZCQdcx3U8VChZwc6/YWmOYc3+KrY582b9UQIEiwzS/Smp2VBJDw8kTDaD8SoKEV15cYVizHkwHnvH3JvdCs3JOHvz9sFiwV7lQHLKVHe7+g6gjvbkjsP/gERSu/cUNqmNIdcH8T0zW7mQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153798130776.6818773749878; Wed, 20 Nov 2024 17:49:58 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJz-0007CX-ST; Wed, 20 Nov 2024 20:49:27 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJu-0006lK-26 for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:23 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJs-0004sG-LI for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:21 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=XdRCsx0NU7YzRpnmN8Uf2wu+MGo6jaBAg0CJXu+Wbdo=; b=BFLBdRLKOCSs7bd CMYhd3gYoIYnhuMqUd9qpJv0VYo/xlwpugtYWT1S1puC3NH5Ncaw4SvG89WBfiqaVQfrlbuGB0rag a+t259i7owrd72qo92c4D2U1cfmwbRMVsayOSMYgMMXGRxJotFzSXJVnvxWUMljktC/UHesQoIUmI a8=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 34/43] target/hexagon: Add get_tb_mmu_index() Date: Thu, 21 Nov 2024 02:49:38 +0100 Message-ID: <20241121014947.18666-35-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153798655116600 Content-Type: text/plain; charset="utf-8" Adds a functions to return the current mmu index given tb_flags of the current translation block. Required by helper-to-tcg in order to retrieve the mmu index for memory operations without changing the signature of helper functions. Signed-off-by: Anton Johansson --- target/hexagon/cpu.h | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/target/hexagon/cpu.h b/target/hexagon/cpu.h index 764f3c38cc..7be4b5769e 100644 --- a/target/hexagon/cpu.h +++ b/target/hexagon/cpu.h @@ -153,6 +153,18 @@ static inline void cpu_get_tb_cpu_state(CPUHexagonStat= e *env, vaddr *pc, } } =20 +// Returns the current mmu index given tb_flags of the current translation +// block. Required by helper-to-tcg in order to retrieve the mmu index for +// memory operations without changing the signature of helper functions. +static inline int get_tb_mmu_index(uint32_t flags) +{ +#ifdef CONFIG_USER_ONLY + return MMU_USER_IDX; +#else +#error System mode not supported on Hexagon yet +#endif +} + typedef HexagonCPU ArchCPU; =20 void hexagon_translate_init(void); --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153857; cv=none; d=zohomail.com; s=zohoarc; b=jyc9Mw2G610/HBDZOvsvP2GvKb28vIk0nQsawx3y/tcG/QD+agzzp+1dJGXlc9O4eGuVpC6Z1Hq9NYkUzepCEjeHBpcqhjVn2+ZtqFQEQksZtpcqdYdtt/1rRvM6PtxbPK19Pa4lp9WG32x3S9u8pWlFA5T5TWhGgAelURHmL18= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153857; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=bZuxt5qAqK6NkZvo8x4OT4L1OuOUMSkYtpG4jSe71JU=; b=hc83NVQhVspPkRZ2yL3Z8MhecBCkpIVRrHGGeqzfj1gBlAgqdxtC6PitWN7FLZOtrG/ftRdET4YUwGwkfaWYc8wDFWSBG2OviiSbuoxIaOOC2Gu4esDD/enpyMm93CpnFSPPF6kzYNGY9VTgj9ZTW45gTmmn6PhV8iwq2WfhRDY= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153857336742.8318660828928; Wed, 20 Nov 2024 17:50:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJT-0005Sq-1d; Wed, 20 Nov 2024 20:48:55 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJP-0005LF-BS for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:52 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJN-0004nq-0E for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:51 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=bZuxt5qAqK6NkZvo8x4OT4L1OuOUMSkYtpG4jSe71JU=; b=o5/46J14uW1iY+9 PNfgqltgr0FOgC7Tk/87WFrSUwwBBV74UWHBnRm6GtWkm7ICn5ABC//JDcTz6azBX5YayojzdoZRe lhAVHcO0nBcpjMbRL7cAESII3mmZ2lsWYHbEtkxB4Qlcc4P4o7YzQvM1wuAWWGV+76ARMEfiEg7Qt SQ=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 35/43] target/hexagon: Use argparse in all python scripts Date: Thu, 21 Nov 2024 02:49:39 +0100 Message-ID: <20241121014947.18666-36-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153859124116600 Content-Type: text/plain; charset="utf-8" QOL commit, all the various gen_* python scripts take a large set arguments where order is implicit. Using argparse we also get decent error messages if a field is missing or too many are added. Signed-off-by: Anton Johansson --- target/hexagon/gen_analyze_funcs.py | 6 +++-- target/hexagon/gen_decodetree.py | 19 +++++++++++---- target/hexagon/gen_helper_funcs.py | 7 +++--- target/hexagon/gen_helper_protos.py | 7 +++--- target/hexagon/gen_idef_parser_funcs.py | 11 +++++++-- target/hexagon/gen_op_attribs.py | 11 +++++++-- target/hexagon/gen_opcodes_def.py | 11 +++++++-- target/hexagon/gen_printinsn.py | 11 +++++++-- target/hexagon/gen_tcg_func_table.py | 11 +++++++-- target/hexagon/gen_tcg_funcs.py | 9 +++---- target/hexagon/gen_trans_funcs.py | 17 ++++++++++--- target/hexagon/hex_common.py | 32 ++++++++++++------------- target/hexagon/meson.build | 2 +- 13 files changed, 107 insertions(+), 47 deletions(-) diff --git a/target/hexagon/gen_analyze_funcs.py b/target/hexagon/gen_analy= ze_funcs.py index 54bac19724..3ac7cc2cfe 100755 --- a/target/hexagon/gen_analyze_funcs.py +++ b/target/hexagon/gen_analyze_funcs.py @@ -78,11 +78,13 @@ def gen_analyze_func(f, tag, regs, imms): =20 =20 def main(): - hex_common.read_common_files() + args =3D hex_common.parse_common_args( + "Emit functions analyzing register accesses" + ) tagregs =3D hex_common.get_tagregs() tagimms =3D hex_common.get_tagimms() =20 - with open(sys.argv[-1], "w") as f: + with open(args.out, "w") as f: f.write("#ifndef HEXAGON_ANALYZE_FUNCS_C_INC\n") f.write("#define HEXAGON_ANALYZE_FUNCS_C_INC\n\n") =20 diff --git a/target/hexagon/gen_decodetree.py b/target/hexagon/gen_decodetr= ee.py index a4fcd622c5..ce703af41d 100755 --- a/target/hexagon/gen_decodetree.py +++ b/target/hexagon/gen_decodetree.py @@ -24,6 +24,7 @@ import textwrap import iset import hex_common +import argparse =20 encs =3D { tag: "".join(reversed(iset.iset[tag]["enc"].replace(" ", ""))) @@ -191,8 +192,18 @@ def gen_decodetree_file(f, class_to_decode): f.write(f"{tag}\t{enc_str} @{tag}\n") =20 =20 +def main(): + parser =3D argparse.ArgumentParser( + description=3D"Emit opaque macro calls with instruction semantics" + ) + parser.add_argument("semantics", help=3D"semantics file") + parser.add_argument("class_to_decode", help=3D"instruction class to de= code") + parser.add_argument("out", help=3D"output file") + args =3D parser.parse_args() + + hex_common.read_semantics_file(args.semantics) + with open(args.out, "w") as f: + gen_decodetree_file(f, args.class_to_decode) + if __name__ =3D=3D "__main__": - hex_common.read_semantics_file(sys.argv[1]) - class_to_decode =3D sys.argv[2] - with open(sys.argv[3], "w") as f: - gen_decodetree_file(f, class_to_decode) + main() diff --git a/target/hexagon/gen_helper_funcs.py b/target/hexagon/gen_helper= _funcs.py index e9685bff2f..c1f806ac4b 100755 --- a/target/hexagon/gen_helper_funcs.py +++ b/target/hexagon/gen_helper_funcs.py @@ -102,12 +102,13 @@ def gen_helper_function(f, tag, tagregs, tagimms): =20 =20 def main(): - hex_common.read_common_files() + args =3D hex_common.parse_common_args( + "Emit helper function definitions for each instruction" + ) tagregs =3D hex_common.get_tagregs() tagimms =3D hex_common.get_tagimms() =20 - output_file =3D sys.argv[-1] - with open(output_file, "w") as f: + with open(args.out, "w") as f: for tag in hex_common.tags: ## Skip the priv instructions if "A_PRIV" in hex_common.attribdict[tag]: diff --git a/target/hexagon/gen_helper_protos.py b/target/hexagon/gen_helpe= r_protos.py index fd2bfd0f36..77f8e0a6a3 100755 --- a/target/hexagon/gen_helper_protos.py +++ b/target/hexagon/gen_helper_protos.py @@ -52,12 +52,13 @@ def gen_helper_prototype(f, tag, tagregs, tagimms): =20 =20 def main(): - hex_common.read_common_files() + args =3D hex_common.parse_common_args( + "Emit helper function prototypes for each instruction" + ) tagregs =3D hex_common.get_tagregs() tagimms =3D hex_common.get_tagimms() =20 - output_file =3D sys.argv[-1] - with open(output_file, "w") as f: + with open(args.out, "w") as f: for tag in hex_common.tags: ## Skip the priv instructions if "A_PRIV" in hex_common.attribdict[tag]: diff --git a/target/hexagon/gen_idef_parser_funcs.py b/target/hexagon/gen_i= def_parser_funcs.py index 72f11c68ca..2f6e826f76 100644 --- a/target/hexagon/gen_idef_parser_funcs.py +++ b/target/hexagon/gen_idef_parser_funcs.py @@ -20,6 +20,7 @@ import sys import re import string +import argparse from io import StringIO =20 import hex_common @@ -43,13 +44,19 @@ ## them are inputs ("in" prefix), while some others are outputs. ## def main(): - hex_common.read_semantics_file(sys.argv[1]) + parser =3D argparse.ArgumentParser( + "Emit instruction implementations that can be fed to idef-parser" + ) + parser.add_argument("semantics", help=3D"semantics file") + parser.add_argument("out", help=3D"output file") + args =3D parser.parse_args() + hex_common.read_semantics_file(args.semantics) hex_common.calculate_attribs() hex_common.init_registers() tagregs =3D hex_common.get_tagregs() tagimms =3D hex_common.get_tagimms() =20 - with open(sys.argv[-1], "w") as f: + with open(args.out, "w") as f: f.write('#include "macros.h.inc"\n\n') =20 for tag in hex_common.tags: diff --git a/target/hexagon/gen_op_attribs.py b/target/hexagon/gen_op_attri= bs.py index 99448220da..bbbb02df3a 100755 --- a/target/hexagon/gen_op_attribs.py +++ b/target/hexagon/gen_op_attribs.py @@ -21,16 +21,23 @@ import re import string import hex_common +import argparse =20 =20 def main(): - hex_common.read_semantics_file(sys.argv[1]) + parser =3D argparse.ArgumentParser( + "Emit opaque macro calls containing instruction attributes" + ) + parser.add_argument("semantics", help=3D"semantics file") + parser.add_argument("out", help=3D"output file") + args =3D parser.parse_args() + hex_common.read_semantics_file(args.semantics) hex_common.calculate_attribs() =20 ## ## Generate all the attributes associated with each instruction ## - with open(sys.argv[-1], "w") as f: + with open(args.out, "w") as f: for tag in hex_common.tags: f.write( f"OP_ATTRIB({tag},ATTRIBS(" diff --git a/target/hexagon/gen_opcodes_def.py b/target/hexagon/gen_opcodes= _def.py index 536f0eb68a..94a19ff412 100755 --- a/target/hexagon/gen_opcodes_def.py +++ b/target/hexagon/gen_opcodes_def.py @@ -21,15 +21,22 @@ import re import string import hex_common +import argparse =20 =20 def main(): - hex_common.read_semantics_file(sys.argv[1]) + parser =3D argparse.ArgumentParser( + description=3D"Emit opaque macro calls with instruction names" + ) + parser.add_argument("semantics", help=3D"semantics file") + parser.add_argument("out", help=3D"output file") + args =3D parser.parse_args() + hex_common.read_semantics_file(args.semantics) =20 ## ## Generate a list of all the opcodes ## - with open(sys.argv[-1], "w") as f: + with open(args.out, "w") as f: for tag in hex_common.tags: f.write(f"OPCODE({tag}),\n") =20 diff --git a/target/hexagon/gen_printinsn.py b/target/hexagon/gen_printinsn= .py index 8bf4d0985c..d5f969960a 100755 --- a/target/hexagon/gen_printinsn.py +++ b/target/hexagon/gen_printinsn.py @@ -21,6 +21,7 @@ import re import string import hex_common +import argparse =20 =20 ## @@ -96,11 +97,17 @@ def spacify(s): =20 =20 def main(): - hex_common.read_semantics_file(sys.argv[1]) + parser =3D argparse.ArgumentParser( + "Emit opaque macro calls with information for printing string repr= esentations of instrucions" + ) + parser.add_argument("semantics", help=3D"semantics file") + parser.add_argument("out", help=3D"output file") + args =3D parser.parse_args() + hex_common.read_semantics_file(args.semantics) =20 immext_casere =3D re.compile(r"IMMEXT\(([A-Za-z])") =20 - with open(sys.argv[-1], "w") as f: + with open(args.out, "w") as f: for tag in hex_common.tags: if not hex_common.behdict[tag]: continue diff --git a/target/hexagon/gen_tcg_func_table.py b/target/hexagon/gen_tcg_= func_table.py index 978ac1819b..299a39b1aa 100755 --- a/target/hexagon/gen_tcg_func_table.py +++ b/target/hexagon/gen_tcg_func_table.py @@ -21,15 +21,22 @@ import re import string import hex_common +import argparse =20 =20 def main(): - hex_common.read_semantics_file(sys.argv[1]) + parser =3D argparse.ArgumentParser( + "Emit opaque macro calls with instruction semantics" + ) + parser.add_argument("semantics", help=3D"semantics file") + parser.add_argument("out", help=3D"output file") + args =3D parser.parse_args() + hex_common.read_semantics_file(args.semantics) hex_common.calculate_attribs() tagregs =3D hex_common.get_tagregs() tagimms =3D hex_common.get_tagimms() =20 - with open(sys.argv[-1], "w") as f: + with open(args.out, "w") as f: f.write("#ifndef HEXAGON_FUNC_TABLE_H\n") f.write("#define HEXAGON_FUNC_TABLE_H\n\n") =20 diff --git a/target/hexagon/gen_tcg_funcs.py b/target/hexagon/gen_tcg_funcs= .py index 05aa0a7855..c2ba91ddc0 100755 --- a/target/hexagon/gen_tcg_funcs.py +++ b/target/hexagon/gen_tcg_funcs.py @@ -108,15 +108,16 @@ def gen_def_tcg_func(f, tag, tagregs, tagimms): =20 =20 def main(): - is_idef_parser_enabled =3D hex_common.read_common_files() + args =3D hex_common.parse_common_args( + "Emit functions calling generated code implementing instruction se= mantics (helpers, idef-parser)" + ) tagregs =3D hex_common.get_tagregs() tagimms =3D hex_common.get_tagimms() =20 - output_file =3D sys.argv[-1] - with open(output_file, "w") as f: + with open(args.out, "w") as f: f.write("#ifndef HEXAGON_TCG_FUNCS_H\n") f.write("#define HEXAGON_TCG_FUNCS_H\n\n") - if is_idef_parser_enabled: + if args.idef_parser: f.write('#include "idef-generated-emitter.h.inc"\n\n') =20 for tag in hex_common.tags: diff --git a/target/hexagon/gen_trans_funcs.py b/target/hexagon/gen_trans_f= uncs.py index 30f0c73e0c..aea1c36f7d 100755 --- a/target/hexagon/gen_trans_funcs.py +++ b/target/hexagon/gen_trans_funcs.py @@ -24,6 +24,7 @@ import textwrap import iset import hex_common +import argparse =20 encs =3D { tag: "".join(reversed(iset.iset[tag]["enc"].replace(" ", ""))) @@ -136,8 +137,18 @@ def gen_trans_funcs(f): """)) =20 =20 -if __name__ =3D=3D "__main__": - hex_common.read_semantics_file(sys.argv[1]) +def main(): + parser =3D argparse.ArgumentParser( + description=3D"Emit trans_*() functions to be called by instructio= n decoder" + ) + parser.add_argument("semantics", help=3D"semantics file") + parser.add_argument("out", help=3D"output file") + args =3D parser.parse_args() + hex_common.read_semantics_file(args.semantics) hex_common.init_registers() - with open(sys.argv[2], "w") as f: + with open(args.out, "w") as f: gen_trans_funcs(f) + + +if __name__ =3D=3D "__main__": + main() diff --git a/target/hexagon/hex_common.py b/target/hexagon/hex_common.py index 15ed4980e4..bb20711a2e 100755 --- a/target/hexagon/hex_common.py +++ b/target/hexagon/hex_common.py @@ -21,6 +21,7 @@ import re import string import textwrap +import argparse =20 behdict =3D {} # tag ->behavior semdict =3D {} # tag -> semantics @@ -1181,22 +1182,19 @@ def helper_args(tag, regs, imms): return args =20 =20 -def read_common_files(): - read_semantics_file(sys.argv[1]) - read_overrides_file(sys.argv[2]) - read_overrides_file(sys.argv[3]) - ## Whether or not idef-parser is enabled is - ## determined by the number of arguments to - ## this script: - ## - ## 4 args. -> not enabled, - ## 5 args. -> idef-parser enabled. - ## - ## The 5:th arg. then holds a list of the successfully - ## parsed instructions. - is_idef_parser_enabled =3D len(sys.argv) > 5 - if is_idef_parser_enabled: - read_idef_parser_enabled_file(sys.argv[4]) +def parse_common_args(desc): + parser =3D argparse.ArgumentParser(desc) + parser.add_argument("semantics", help=3D"semantics file") + parser.add_argument("overrides", help=3D"overrides file") + parser.add_argument("overrides_vec", help=3D"vector overrides file") + parser.add_argument("out", help=3D"output file") + parser.add_argument("--idef-parser", help=3D"file of instructions tran= slated by idef-parser") + args =3D parser.parse_args() + read_semantics_file(args.semantics) + read_overrides_file(args.overrides) + read_overrides_file(args.overrides_vec) + if args.idef_parser: + read_idef_parser_enabled_file(args.idef_parser) calculate_attribs() init_registers() - return is_idef_parser_enabled + return args diff --git a/target/hexagon/meson.build b/target/hexagon/meson.build index f1723778a6..bb4ebaae81 100644 --- a/target/hexagon/meson.build +++ b/target/hexagon/meson.build @@ -346,7 +346,7 @@ if idef_parser_enabled and 'hexagon-linux-user' in targ= et_dirs # Setup input and dependencies for the next step, this depends on whet= her or # not idef-parser is enabled helper_dep =3D [semantics_generated, idef_generated_tcg_c, idef_genera= ted_tcg] - helper_in =3D [semantics_generated, gen_tcg_h, gen_tcg_hvx_h, idef_gen= erated_list] + helper_in =3D [semantics_generated, gen_tcg_h, gen_tcg_hvx_h, '--idef-= parser', idef_generated_list] else # Setup input and dependencies for the next step, this depends on whet= her or # not idef-parser is enabled --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153785; cv=none; d=zohomail.com; s=zohoarc; b=h9Y/3QrvMYOY9VB+U1vf9s9Cv1nlg7Bk6Y3+vVYDEJdsysad6kKsg0wDfCdJwe8RfBZj/0+PKFmaGN13XqN7jQg4lqKarERBvk39LFDr6rhgFOXvqzSEcEDLqvtJ6UFavVNGrqr5oEpRKzxC2wJeQ50uo/g8mxQfr8fPbS2xkCY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153785; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=XQcHvwKeA4tB1CDjW/j9CD6mQBUj91ZRHILJBcsMegA=; b=F2Ftpr9NA4u+Ih/WJYvtvfDN2+zQqBijw12xJcHI29jOVtQknckeXR2U8+d7SgIIF9/SfrH6iWivo6BHL64uf4q3K0LS99jiEY66f8fLJISof5m6/Hv9C4VPCNb7pQcYC490KFz0bGfH8CWmqZGOhAgzie85sqpahjmRp+mCFpM= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153785836670.001957638967; Wed, 20 Nov 2024 17:49:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJM-0005EW-7P; Wed, 20 Nov 2024 20:48:50 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJH-000583-Rq for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:43 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJG-0004mp-GV for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:43 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=XQcHvwKeA4tB1CDjW/j9CD6mQBUj91ZRHILJBcsMegA=; b=OPxBex1NZfeN/EC wRovIjE4FSRL9AjG+jVw4fn4xV184i4Cfu7lNXifikh0zauwP29m0NKkh3qUVCldYUKwr91du7Noq NDSP7vHnOEGBYQpewS/rnHSj+Rp4oqNJoa7zQgchfKp3z6yetuN3K86J37x8hSNr9OCwuM4UXgy12 fM=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 36/43] target/hexagon: Add temporary vector storage Date: Thu, 21 Nov 2024 02:49:40 +0100 Message-ID: <20241121014947.18666-37-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153786317116600 Content-Type: text/plain; charset="utf-8" Temporary vectors in helper-to-tcg generated code are allocated from an array of bytes in CPUArchState, specified with --temp-vector-block. This commits adds such a block of memory to CPUArchState, if CONFIG_HELPER_TO_TCG is set. Signed-off-by: Anton Johansson --- target/hexagon/cpu.h | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/target/hexagon/cpu.h b/target/hexagon/cpu.h index 7be4b5769e..fa6ac83e01 100644 --- a/target/hexagon/cpu.h +++ b/target/hexagon/cpu.h @@ -97,6 +97,10 @@ typedef struct CPUArchState { MMVector future_VRegs[VECTOR_TEMPS_MAX] QEMU_ALIGNED(16); MMVector tmp_VRegs[VECTOR_TEMPS_MAX] QEMU_ALIGNED(16); =20 +#ifdef CONFIG_HELPER_TO_TCG + uint8_t tmp_vmem[4096] QEMU_ALIGNED(16); +#endif + MMQReg QRegs[NUM_QREGS] QEMU_ALIGNED(16); MMQReg future_QRegs[NUM_QREGS] QEMU_ALIGNED(16); =20 --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153917; cv=none; d=zohomail.com; s=zohoarc; b=dtVwwjxIrDzjTBytEx2KUCG7GQppC+rvVNNgXFyJm4A4CJo0vsnAkSHVHXrNFjkOj5SrYr6w4nbSzH8Hd5jXaZAB8neLdHcwJP5GiPrgkfRsC5Vbz92MF1l5paZbfR2SOywrHnZ7nrTP5YmeN5qGWDLjFPsjitqPGD8WJ9DpsTw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153917; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=v6yNgUtqmAbkKUCCRshTh81ZOJJ3PLSMzGXN2kDgk8Q=; b=XdJ0ZYMPQ1ZGP/KZAowVkJeiWsxJs6R5Tf/cHjay7TCo0oai2WQzR8gXlfZdvVEPDOje4zIjqk6+fdzgfGUTTgiL02PdESv+neLfwI385s9p6UBk7GMBT2+xd6FlYpo+H7oUwHxair+kcQqo4smJC+BqD113DYB+nZeg2ZwuPEc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153917038920.1360341026091; Wed, 20 Nov 2024 17:51:57 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwK1-0007Wp-Ls; Wed, 20 Nov 2024 20:49:29 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwK0-0007G3-1z for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:28 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJy-0004sv-I8 for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=v6yNgUtqmAbkKUCCRshTh81ZOJJ3PLSMzGXN2kDgk8Q=; b=xS8Fw53VsdnzzE6 YRiEaY0hJrpW902X1vcnNeE6nQ9lUswJNRvHrwUzEme39lYv4HTz7WgVY2KimuNl3sSQijDIcqr2U FlvJyLuajDjmsqmXGWUX5MBp/HBmxAP6DVRMslk2RBTp6zFA42URNpIG8F+BZD7TQr+okFwzgYi4X ik=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 37/43] target/hexagon: Make HVX vector args. restrict * Date: Thu, 21 Nov 2024 02:49:41 +0100 Message-ID: <20241121014947.18666-38-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153918858116600 Content-Type: text/plain; charset="utf-8" If pointer arguments to HVX helper functions are not marked restrict *, then LLVM will assume that input vectors may alias and emit runtime checks. Signed-off-by: Anton Johansson --- target/hexagon/mmvec/macros.h | 36 +++++++++++++++++------------------ 1 file changed, 18 insertions(+), 18 deletions(-) diff --git a/target/hexagon/mmvec/macros.h b/target/hexagon/mmvec/macros.h index 1ceb9453ee..dfaefc6b26 100644 --- a/target/hexagon/mmvec/macros.h +++ b/target/hexagon/mmvec/macros.h @@ -23,26 +23,26 @@ #include "mmvec/system_ext_mmvec.h" =20 #ifndef QEMU_GENERATE -#define VdV (*(MMVector *)(VdV_void)) -#define VsV (*(MMVector *)(VsV_void)) -#define VuV (*(MMVector *)(VuV_void)) -#define VvV (*(MMVector *)(VvV_void)) -#define VwV (*(MMVector *)(VwV_void)) -#define VxV (*(MMVector *)(VxV_void)) -#define VyV (*(MMVector *)(VyV_void)) +#define VdV (*(MMVector * restrict)(VdV_void)) +#define VsV (*(MMVector * restrict)(VsV_void)) +#define VuV (*(MMVector * restrict)(VuV_void)) +#define VvV (*(MMVector * restrict)(VvV_void)) +#define VwV (*(MMVector * restrict)(VwV_void)) +#define VxV (*(MMVector * restrict)(VxV_void)) +#define VyV (*(MMVector * restrict)(VyV_void)) =20 -#define VddV (*(MMVectorPair *)(VddV_void)) -#define VuuV (*(MMVectorPair *)(VuuV_void)) -#define VvvV (*(MMVectorPair *)(VvvV_void)) -#define VxxV (*(MMVectorPair *)(VxxV_void)) +#define VddV (*(MMVectorPair * restrict)(VddV_void)) +#define VuuV (*(MMVectorPair * restrict)(VuuV_void)) +#define VvvV (*(MMVectorPair * restrict)(VvvV_void)) +#define VxxV (*(MMVectorPair * restrict)(VxxV_void)) =20 -#define QeV (*(MMQReg *)(QeV_void)) -#define QdV (*(MMQReg *)(QdV_void)) -#define QsV (*(MMQReg *)(QsV_void)) -#define QtV (*(MMQReg *)(QtV_void)) -#define QuV (*(MMQReg *)(QuV_void)) -#define QvV (*(MMQReg *)(QvV_void)) -#define QxV (*(MMQReg *)(QxV_void)) +#define QeV (*(MMQReg * restrict)(QeV_void)) +#define QdV (*(MMQReg * restrict)(QdV_void)) +#define QsV (*(MMQReg * restrict)(QsV_void)) +#define QtV (*(MMQReg * restrict)(QtV_void)) +#define QuV (*(MMQReg * restrict)(QuV_void)) +#define QvV (*(MMQReg * restrict)(QvV_void)) +#define QxV (*(MMQReg * restrict)(QxV_void)) #endif =20 #define LOG_VTCM_BYTE(VA, MASK, VAL, IDX) \ --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153965; cv=none; d=zohomail.com; s=zohoarc; b=FDwRqKvb1XeTWQMj4EXM+nmr/b1qvPzpLQsEf+oZ0r9+ubrvsRJfBgaB35toqm0et9X+IUt1hWKqd+HZT/WAEmQMwg8EIYHTzOTmotsPo4hhqGrbEtqWn2W34IwFp+hyXXeMKxjPpNeSs5s+Tr3KV5ljcRhPuey2u7tCzYSo9z4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153965; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=MKLR6c3mNCNege2rKF5LffLNelQnuoKTKn4NaM4VX+E=; b=NFpLOhCDfMtkXQAo06myV2hlfOeZAOaKk2mDeNib4qWSYWksJx32InnhX9RcrRZQ8mDwQeUEgXgEIaqXySqtKdANMlRSKSe/IMM8WMh9oIR7Qu640RacIH04qIlyJIdBBabC2J6ByCFYjztEHIjCgeFWpis328k1ZRBxrzNseuo= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153965984888.4260447116901; Wed, 20 Nov 2024 17:52:45 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJE-00052S-P6; Wed, 20 Nov 2024 20:48:40 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJC-0004z9-N8 for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:38 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJA-0004mD-RK for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=MKLR6c3mNCNege2rKF5LffLNelQnuoKTKn4NaM4VX+E=; b=KnWdSmEcPfqMBnd KKEfRhMlUV0H2DBKi67/EnqT6Pzv7cmSnFxYCLTnqCrLztIn8OdA/vMFqYHsWnN4pE5WSYWosyAsy A20K9WhPM3lpQgj0cl72aUKGxK1Fid3EcmYK0vCTsUSTBHWJ4GakLN3C5WCmoVZJk4v1M1Q4ALA/s RE=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 38/43] target/hexagon: Use cpu_mapping to map env -> TCG Date: Thu, 21 Nov 2024 02:49:42 +0100 Message-ID: <20241121014947.18666-39-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153967027116600 Content-Type: text/plain; charset="utf-8" Replaces previous calls to tcg_global_mem_new*() with a declarative global array of cpu_mapping structs. This array can be used to initialize all TCG globals with one function call from the target, and may additionally be used from LLVM based tools to map between offsets into a struct and a mapped TCGv global. Signed-off-by: Anton Johansson --- target/hexagon/translate.c | 116 +++++++++++++++++++++---------------- 1 file changed, 65 insertions(+), 51 deletions(-) diff --git a/target/hexagon/translate.c b/target/hexagon/translate.c index 4b1bee3c6d..f9a9de35fe 100644 --- a/target/hexagon/translate.c +++ b/target/hexagon/translate.c @@ -15,6 +15,7 @@ * along with this program; if not, see . */ =20 +#include "qemu/typedefs.h" #define QEMU_GENERATE #include "qemu/osdep.h" #include "cpu.h" @@ -32,6 +33,7 @@ #include "translate.h" #include "genptr.h" #include "printinsn.h" +#include "tcg/tcg-global-mappings.h" =20 #define HELPER_H "helper.h" #include "exec/helper-info.c.inc" @@ -1093,7 +1095,6 @@ void gen_intermediate_code(CPUState *cs, TranslationB= lock *tb, int *max_insns, } =20 #define NAME_LEN 64 -static char reg_written_names[TOTAL_PER_THREAD_REGS][NAME_LEN]; static char store_addr_names[STORES_MAX][NAME_LEN]; static char store_width_names[STORES_MAX][NAME_LEN]; static char store_val32_names[STORES_MAX][NAME_LEN]; @@ -1101,63 +1102,73 @@ static char store_val64_names[STORES_MAX][NAME_LEN]; static char vstore_addr_names[VSTORES_MAX][NAME_LEN]; static char vstore_size_names[VSTORES_MAX][NAME_LEN]; static char vstore_pending_names[VSTORES_MAX][NAME_LEN]; +#if HEX_DEBUG +static const char *reg_written_names_ptr[TOTAL_PER_THREAD_REGS]; +#endif +static const char *store_addr_names_ptr[STORES_MAX]; +static const char *store_width_names_ptr[STORES_MAX]; +static const char *store_val32_names_ptr[STORES_MAX]; +static const char *store_val64_names_ptr[STORES_MAX]; + +cpu_tcg_mapping tcg_global_mappings[] =3D { + /* General purpose and predicate registers */ + CPU_TCG_MAP_ARRAY(CPUArchState, hex_gpr, gpr, hexagon_regnames), + CPU_TCG_MAP_ARRAY(CPUArchState, hex_pred, pred, hexagon_prednames), + + /* Misc */ + CPU_TCG_MAP(CPUArchState, hex_new_value_usr, new_value_usr, "new= _value_usr"), + CPU_TCG_MAP(CPUArchState, hex_slot_cancelled, slot_cancelled, "slo= t_cancelled"), + CPU_TCG_MAP(CPUArchState, hex_llsc_addr, llsc_addr, "lls= c_addr"), + CPU_TCG_MAP(CPUArchState, hex_llsc_val, llsc_val, "lls= c_val"), + CPU_TCG_MAP(CPUArchState, hex_llsc_val_i64, llsc_val_i64, "lls= c_val_i64"), =20 -void hexagon_translate_init(void) -{ - int i; - - opcode_init(); - - for (i =3D 0; i < TOTAL_PER_THREAD_REGS; i++) { - hex_gpr[i] =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, gpr[i]), - hexagon_regnames[i]); - - if (HEX_DEBUG) { - snprintf(reg_written_names[i], NAME_LEN, "reg_written_%s", - hexagon_regnames[i]); - hex_reg_written[i] =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, reg_written[i]), - reg_written_names[i]); - } - } - hex_new_value_usr =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, new_value_usr), "new_value_usr"); + /* + * New general purpose and predicate register values, + * and reg_written used in debugging + */ +#if HEX_DEBUG + CPU_TCG_MAP_ARRAY(hex_reg_written, reg_written, reg_written_name= s_ptr), +#endif + + /* Logging stores */ + CPU_TCG_MAP_ARRAY_OF_STRUCTS(CPUArchState, hex_store_addr, mem_log_st= ores, va, store_addr_names_ptr), + CPU_TCG_MAP_ARRAY_OF_STRUCTS(CPUArchState, hex_store_width, mem_log_st= ores, width, store_width_names_ptr), + CPU_TCG_MAP_ARRAY_OF_STRUCTS(CPUArchState, hex_store_val32, mem_log_st= ores, data32, store_val32_names_ptr), + CPU_TCG_MAP_ARRAY_OF_STRUCTS(CPUArchState, hex_store_val64, mem_log_st= ores, data64, store_val64_names_ptr), +}; =20 - for (i =3D 0; i < NUM_PREGS; i++) { - hex_pred[i] =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, pred[i]), - hexagon_prednames[i]); - } - hex_slot_cancelled =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, slot_cancelled), "slot_cancelled"); - hex_llsc_addr =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, llsc_addr), "llsc_addr"); - hex_llsc_val =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, llsc_val), "llsc_val"); - hex_llsc_val_i64 =3D tcg_global_mem_new_i64(tcg_env, - offsetof(CPUHexagonState, llsc_val_i64), "llsc_val_i64"); - for (i =3D 0; i < STORES_MAX; i++) { - snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i); - hex_store_addr[i] =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, mem_log_stores[i].va), - store_addr_names[i]); +size_t tcg_global_mapping_count =3D ARRAY_SIZE(tcg_global_mappings); =20 +static void init_cpu_reg_names(void) { + /* + * Create register names and store them in `*_names`, + * then copy to and array of pointers in `*_names_ptr` + * which is easier to pass around. + */ +#if HEX_DEBUG + for (int i =3D 0; i < TOTAL_PER_THREAD_REGS; ++i) { + snprintf(reg_written_names[i], NAME_LEN, "reg_written_%s", hexagon= _regnames[i]); + reg_written_names_ptr[i] =3D reg_written_names[i]; + } +#endif + for (int i =3D 0; i < STORES_MAX; ++i) { + snprintf(store_addr_names[i], NAME_LEN, "store_addr_%d", i); snprintf(store_width_names[i], NAME_LEN, "store_width_%d", i); - hex_store_width[i] =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, mem_log_stores[i].width), - store_width_names[i]); - snprintf(store_val32_names[i], NAME_LEN, "store_val32_%d", i); - hex_store_val32[i] =3D tcg_global_mem_new(tcg_env, - offsetof(CPUHexagonState, mem_log_stores[i].data32), - store_val32_names[i]); - snprintf(store_val64_names[i], NAME_LEN, "store_val64_%d", i); - hex_store_val64[i] =3D tcg_global_mem_new_i64(tcg_env, - offsetof(CPUHexagonState, mem_log_stores[i].data64), - store_val64_names[i]); + store_addr_names_ptr[i] =3D store_addr_names[i]; + store_width_names_ptr[i] =3D store_width_names[i]; + store_val32_names_ptr[i] =3D store_val32_names[i]; + store_val64_names_ptr[i] =3D store_val64_names[i]; } +} + +void hexagon_translate_init(void) +{ + int i; + + opcode_init(); + for (i =3D 0; i < VSTORES_MAX; i++) { snprintf(vstore_addr_names[i], NAME_LEN, "vstore_addr_%d", i); hex_vstore_addr[i] =3D tcg_global_mem_new(tcg_env, @@ -1174,4 +1185,7 @@ void hexagon_translate_init(void) offsetof(CPUHexagonState, vstore_pending[i]), vstore_pending_names[i]); } + + init_cpu_reg_names(); + init_cpu_tcg_mappings(tcg_global_mappings, tcg_global_mapping_count); } --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153733; cv=none; d=zohomail.com; s=zohoarc; b=fqyZWed9bR9/8OEpaZjhe9+th8N/kj9hajyyPyZJh2T0G6giffn1+Zw2X8Q8TfonG3ptnZdZyedtQ6hF4fZ5QeA/oAzRBTPV7FDHziSqmT0ECe7DbqRQ4+tRmhcJzsjKkbwEntDSI/0wX54dLWu0qfrXLNi/UIWujUfxFUj5qUk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153733; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=iuOj1a1gnKZAf92H8D0zYKZjKUE/7EIVKEMc77kWSCk=; b=DRIsb2CK7tbsGcqvsD6R1PN18WRqDHMAjYXbVxIZzKXovMS69E6SulKumtM3Bcvamtv/Car+DQ6ieOrzLZ7hCPSn0G+OGk2Bga9v3bVAGFnZAFN+oWuG6fuoYA81xTBPu/VFx3bygPEqsiosXjBr4mxbscMu5oN9GoRXxQ8V+vQ= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153733039824.2682378011427; Wed, 20 Nov 2024 17:48:53 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJ9-0004vT-GW; Wed, 20 Nov 2024 20:48:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJ6-0004uZ-18 for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:33 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJ4-0004lT-Pp for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:31 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=iuOj1a1gnKZAf92H8D0zYKZjKUE/7EIVKEMc77kWSCk=; b=a6hf772In8tH27c Bk6u9m1sqobXM9d/HLRAN1vg5TBsGQOz4ebrw0VSbWKr8z4vEzDuZA38MJ7MV+5PKADwFEWN+SQxa MVS52J0VRGKhko4Dep23o9QAG1Naj2vYYWBsLTStyKw1kEdyr9gwyx40oTUlj1WfkJFvheWPwNtDE M4=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 39/43] target/hexagon: Keep gen_slotval/check_noshuf for helper-to-tcg Date: Thu, 21 Nov 2024 02:49:43 +0100 Message-ID: <20241121014947.18666-40-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153734166116600 Content-Type: text/plain; charset="utf-8" Makes sure gen_slotval() and check_noshuf() remains defined when helper-to-tcg and idef-parser are both used. gen_slotval() is needed for creating a TCGv of the slot value fed to helpers (generated helper-to-tcg code), and check_noshuf() is needed for helper definitions used as input to helper-to-tcg. Signed-off-by: Anton Johansson --- target/hexagon/genptr.c | 2 +- target/hexagon/op_helper.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/target/hexagon/genptr.c b/target/hexagon/genptr.c index dbae6c570a..ea3ccf649a 100644 --- a/target/hexagon/genptr.c +++ b/target/hexagon/genptr.c @@ -399,7 +399,7 @@ static inline void gen_store_conditional8(DisasContext = *ctx, tcg_gen_movi_tl(hex_llsc_addr, ~0); } =20 -#ifndef CONFIG_HEXAGON_IDEF_PARSER +#if !defined(CONFIG_HEXAGON_IDEF_PARSER) || defined(CONFIG_HELPER_TO_TCG) static TCGv gen_slotval(DisasContext *ctx) { int slotval =3D (ctx->pkt->pkt_has_store_s1 & 1) | (ctx->insn->slot <<= 1); diff --git a/target/hexagon/op_helper.c b/target/hexagon/op_helper.c index 90e7aaa097..0f9c6ab19f 100644 --- a/target/hexagon/op_helper.c +++ b/target/hexagon/op_helper.c @@ -567,7 +567,7 @@ void HELPER(probe_pkt_scalar_hvx_stores)(CPUHexagonStat= e *env, int mask) } } =20 -#ifndef CONFIG_HEXAGON_IDEF_PARSER +#if !defined(CONFIG_HEXAGON_IDEF_PARSER) || defined(CONFIG_HELPER_TO_TCG) /* * mem_noshuf * Section 5.5 of the Hexagon V67 Programmer's Reference Manual --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153838; cv=none; d=zohomail.com; s=zohoarc; b=VIxooBrV8Al/kmkJue9FPKMn+pimqiC7iSTiwpWj8nqyN58eGsSKadPk+KZ1TczrW0M1Xyh80h24daHZqWi1//qTF8h+TleWciZKpnSzeaB6UrOrUjEvI6k+f48lE5NIBw+0GNiBwhh4vsr1+e3yBYg0ybNezCTyJq7bf+giZJU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153838; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=9+JUQklo1fAygUmg6bP51Hb2Kw75ebMHVooLkGmxJb8=; b=D5wZ2/xP8S3uDCN8fJv+HEqWLH7lwm//OuzYhTPTtJzfQmjMtJdThBr75evKTDd6ofCfWvF1kFOEVum6y72Gnnlf3aHIwa7nIDnXTJkJ5tzNFtcspP/FPM2Ov5ZIbeQtP/RQUZsYum8m7Ke4Y0Paes/3XEv7U+k3crk0tOOeWDU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153838412386.87184216514333; Wed, 20 Nov 2024 17:50:38 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwJ2-0004p2-Pi; Wed, 20 Nov 2024 20:48:28 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwJ1-0004cL-1P for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:27 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwIy-0004k7-OA for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:26 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=9+JUQklo1fAygUmg6bP51Hb2Kw75ebMHVooLkGmxJb8=; b=G2l6wby7s4PHfXm lVpwpOackaLd3ILYRkfYJSLyj0zbLBaucoTDqvWOajwhJkh8RlkTnw6XrZa23wlYtEC5nrjKN0B6Z qDHjJm3GWxE06KD6LgGMftIg3jtLo+fwLNBb6j7JlzNSnAAThbFD3lBof2f9Vzeev1B0Xnt9U71+V xA=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 40/43] target/hexagon: Emit annotations for helpers Date: Thu, 21 Nov 2024 02:49:44 +0100 Message-ID: <20241121014947.18666-41-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153840833116600 Content-Type: text/plain; charset="utf-8" Adds the following LLVM_ANNOTATE attributes to helper functions generated by Hexagon: 1. "helper-to-tcg", to specify that a given helper functions should be translated, and; 2. "immediate: ..." to make sure immediate arguments to helper functions remain immediates in the emitted TCG code (e.g. slot). 3. "ptr-to-offset: ..." to make sure pointer arguments are treated as immediates representing an offset into the CPU state, needed to work with gvec. Two functions are also added to hex_common.py, to firstly parse the generated file containing all successfully translated helper functions, and secondly to expose the indices of immediate and pointer (vector) arguments to helper functions. The latter is needed to generate the input of helper-to-tcg. Signed-off-by: Anton Johansson --- target/hexagon/gen_helper_funcs.py | 17 ++++++++++- target/hexagon/gen_helper_protos.py | 2 +- target/hexagon/gen_tcg_funcs.py | 2 +- target/hexagon/hex_common.py | 45 +++++++++++++++++++++++------ target/hexagon/op_helper.c | 1 + 5 files changed, 55 insertions(+), 12 deletions(-) diff --git a/target/hexagon/gen_helper_funcs.py b/target/hexagon/gen_helper= _funcs.py index c1f806ac4b..7f0844d843 100755 --- a/target/hexagon/gen_helper_funcs.py +++ b/target/hexagon/gen_helper_funcs.py @@ -41,9 +41,23 @@ def gen_helper_function(f, tag, tagregs, tagimms): ret_type =3D hex_common.helper_ret_type(tag, regs).func_arg =20 declared =3D [] - for arg in hex_common.helper_args(tag, regs, imms): + helper_args, imm_inds, hvx_inds =3D hex_common.helper_args(tag, regs, = imms) + for arg in helper_args: declared.append(arg.func_arg) =20 + ## Specify that helpers should be translated by helper-to-tcg + f.write(f'LLVM_ANNOTATE("helper-to-tcg")\n') + ## Specify which arguments to the helper function should be treated as + ## immediate arguments + if len(imm_inds) > 0: + imm_inds_str =3D ','.join(str(i) for i in imm_inds) + f.write(f'LLVM_ANNOTATE("immediate: {imm_inds_str}")\n') + ## Specify which arguments to the helper function should be treated as + ## gvec vectors + if len(hvx_inds) > 0: + hvx_inds_str =3D ','.join(str(i) for i in hvx_inds) + f.write(f'LLVM_ANNOTATE("ptr-to-offset: {hvx_inds_str}")\n') + arguments =3D ", ".join(declared) f.write(f"{ret_type} HELPER({tag})({arguments})\n") f.write("{\n") @@ -51,6 +65,7 @@ def gen_helper_function(f, tag, tagregs, tagimms): f.write(hex_common.code_fmt(f"""\ uint32_t EA; """)) + ## Declare the return variable if not hex_common.is_predicated(tag): for regtype, regid in regs: diff --git a/target/hexagon/gen_helper_protos.py b/target/hexagon/gen_helpe= r_protos.py index 77f8e0a6a3..2082757891 100755 --- a/target/hexagon/gen_helper_protos.py +++ b/target/hexagon/gen_helper_protos.py @@ -36,7 +36,7 @@ def gen_helper_prototype(f, tag, tagregs, tagimms): ret_type =3D hex_common.helper_ret_type(tag, regs).proto_arg declared.append(ret_type) =20 - for arg in hex_common.helper_args(tag, regs, imms): + for arg in hex_common.helper_args(tag, regs, imms)[0]: declared.append(arg.proto_arg) =20 arguments =3D ", ".join(declared) diff --git a/target/hexagon/gen_tcg_funcs.py b/target/hexagon/gen_tcg_funcs= .py index c2ba91ddc0..a06edeb9de 100755 --- a/target/hexagon/gen_tcg_funcs.py +++ b/target/hexagon/gen_tcg_funcs.py @@ -85,7 +85,7 @@ def gen_tcg_func(f, tag, regs, imms): if ret_type !=3D "void": declared.append(ret_type) =20 - for arg in hex_common.helper_args(tag, regs, imms): + for arg in hex_common.helper_args(tag, regs, imms)[0]: declared.append(arg.call_arg) =20 arguments =3D ", ".join(declared) diff --git a/target/hexagon/hex_common.py b/target/hexagon/hex_common.py index bb20711a2e..fc4c9f648e 100755 --- a/target/hexagon/hex_common.py +++ b/target/hexagon/hex_common.py @@ -32,6 +32,7 @@ tags =3D [] # list of all tags overrides =3D {} # tags with helper overrides idef_parser_enabled =3D {} # tags enabled for idef-parser +helper_to_tcg_enabled =3D {} # tags enabled for helper-to-tcg =20 # We should do this as a hash for performance, # but to keep order let's keep it as a list. @@ -262,6 +263,10 @@ def is_idef_parser_enabled(tag): return tag in idef_parser_enabled =20 =20 +def is_helper_to_tcg_enabled(tag): + return tag in helper_to_tcg_enabled + + def is_hvx_insn(tag): return "A_CVI" in attribdict[tag] =20 @@ -304,6 +309,13 @@ def read_idef_parser_enabled_file(name): idef_parser_enabled =3D set(lines) =20 =20 +def read_helper_to_tcg_enabled_file(name): + global helper_to_tcg_enabled + with open(name, "r") as helper_to_tcg_enabled_file: + lines =3D helper_to_tcg_enabled_file.read().strip().split("\n") + helper_to_tcg_enabled =3D set(lines) + + def is_predicated(tag): return "A_CONDEXEC" in attribdict[tag] =20 @@ -383,7 +395,7 @@ def hvx_off(self): def helper_proto_type(self): return "ptr" def helper_arg_type(self): - return "void *" + return "void * restrict" def helper_arg_name(self): return f"{self.reg_tcg()}_void" =20 @@ -719,7 +731,7 @@ def decl_tcg(self, f, tag, regno): const intptr_t {self.hvx_off()} =3D {vreg_offset_func(tag)}(ctx, {self.reg_num}, 1, true); """)) - if not skip_qemu_helper(tag): + if not skip_qemu_helper(tag) and not is_helper_to_tcg_enabled(tag): f.write(code_fmt(f"""\ TCGv_ptr {self.reg_tcg()} =3D tcg_temp_new_ptr(); tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off(= )}); @@ -744,7 +756,7 @@ def decl_tcg(self, f, tag, regno): f.write(code_fmt(f"""\ const intptr_t {self.hvx_off()} =3D vreg_src_off(ctx, {self.re= g_num}); """)) - if not skip_qemu_helper(tag): + if not skip_qemu_helper(tag) and not is_helper_to_tcg_enabled(tag): f.write(code_fmt(f"""\ TCGv_ptr {self.reg_tcg()} =3D tcg_temp_new_ptr(); tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off(= )}); @@ -785,7 +797,7 @@ def decl_tcg(self, f, tag, regno): vreg_src_off(ctx, {self.reg_num}), sizeof(MMVector), sizeof(MMVector)); """)) - if not skip_qemu_helper(tag): + if not skip_qemu_helper(tag) and not is_helper_to_tcg_enabled(tag): f.write(code_fmt(f"""\ TCGv_ptr {self.reg_tcg()} =3D tcg_temp_new_ptr(); tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off(= )}); @@ -814,7 +826,7 @@ def decl_tcg(self, f, tag, regno): f.write(code_fmt(f"""\ const intptr_t {self.hvx_off()} =3D offsetof(CPUHexagonState, = vtmp); """)) - if not skip_qemu_helper(tag): + if not skip_qemu_helper(tag) and not is_helper_to_tcg_enabled(tag): f.write(code_fmt(f"""\ TCGv_ptr {self.reg_tcg()} =3D tcg_temp_new_ptr(); tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off(= )}); @@ -850,7 +862,7 @@ def decl_tcg(self, f, tag, regno): const intptr_t {self.hvx_off()} =3D {vreg_offset_func(tag)}(ctx, {self.reg_num}, 2, true); """)) - if not skip_qemu_helper(tag): + if not skip_qemu_helper(tag) and not is_helper_to_tcg_enabled(tag): f.write(code_fmt(f"""\ TCGv_ptr {self.reg_tcg()} =3D tcg_temp_new_ptr(); tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off(= )}); @@ -882,7 +894,7 @@ def decl_tcg(self, f, tag, regno): vreg_src_off(ctx, {self.reg_num} ^ 1), sizeof(MMVector), sizeof(MMVector)); """)) - if not skip_qemu_helper(tag): + if not skip_qemu_helper(tag) and not is_helper_to_tcg_enabled(tag): f.write(code_fmt(f"""\ TCGv_ptr {self.reg_tcg()} =3D tcg_temp_new_ptr(); tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off(= )}); @@ -909,7 +921,7 @@ def decl_tcg(self, f, tag, regno): vreg_src_off(ctx, {self.reg_num} ^ 1), sizeof(MMVector), sizeof(MMVector)); """)) - if not skip_qemu_helper(tag): + if not skip_qemu_helper(tag) and not is_helper_to_tcg_enabled(tag): f.write(code_fmt(f"""\ TCGv_ptr {self.reg_tcg()} =3D tcg_temp_new_ptr(); tcg_gen_addi_ptr({self.reg_tcg()}, tcg_env, {self.hvx_off(= )}); @@ -1092,8 +1104,13 @@ def helper_ret_type(tag, regs): raise Exception("numscalarresults > 1") return return_type =20 + def helper_args(tag, regs, imms): args =3D [] + # Used to ensure immediates are passed translated as immediates by + # helper-to-tcg. + imm_indices =3D [] + hvx_indices =3D [] =20 ## First argument is the CPU state if need_env(tag): @@ -1114,16 +1131,20 @@ def helper_args(tag, regs, imms): for regtype, regid in regs: reg =3D get_register(tag, regtype, regid) if reg.is_written() and reg.is_hvx_reg(): + hvx_indices.append(len(args)) args.append(reg.helper_arg()) =20 ## Pass the source registers for regtype, regid in regs: reg =3D get_register(tag, regtype, regid) if reg.is_read() and not (reg.is_hvx_reg() and reg.is_readwrite()): + if reg.is_hvx_reg(): + hvx_indices.append(len(args)) args.append(reg.helper_arg()) =20 ## Pass the immediates for immlett, bits, immshift in imms: + imm_indices.append(len(args)) args.append(HelperArg( "s32", f"tcg_constant_tl({imm_name(immlett)})", @@ -1132,24 +1153,28 @@ def helper_args(tag, regs, imms): =20 ## Other stuff the helper might need if need_pkt_has_multi_cof(tag): + imm_indices.append(len(args)) args.append(HelperArg( "i32", "tcg_constant_tl(ctx->pkt->pkt_has_multi_cof)", "uint32_t pkt_has_multi_cof" )) if need_pkt_need_commit(tag): + imm_indices.append(len(args)) args.append(HelperArg( "i32", "tcg_constant_tl(ctx->need_commit)", "uint32_t pkt_need_commit" )) if need_PC(tag): + imm_indices.append(len(args)) args.append(HelperArg( "i32", "tcg_constant_tl(ctx->pkt->pc)", "target_ulong PC" )) if need_next_PC(tag): + imm_indices.append(len(args)) args.append(HelperArg( "i32", "tcg_constant_tl(ctx->next_PC)", @@ -1168,18 +1193,20 @@ def helper_args(tag, regs, imms): "uint32_t SP" )) if need_slot(tag): + imm_indices.append(len(args)) args.append(HelperArg( "i32", "gen_slotval(ctx)", "uint32_t slotval" )) if need_part1(tag): + imm_indices.append(len(args)) args.append(HelperArg( "i32", "tcg_constant_tl(insn->part1)" "uint32_t part1" )) - return args + return args, imm_indices, hvx_indices =20 =20 def parse_common_args(desc): diff --git a/target/hexagon/op_helper.c b/target/hexagon/op_helper.c index 0f9c6ab19f..182e8fdeab 100644 --- a/target/hexagon/op_helper.c +++ b/target/hexagon/op_helper.c @@ -31,6 +31,7 @@ #include "mmvec/macros.h" #include "op_helper.h" #include "translate.h" +#include "helper-to-tcg/annotate.h" =20 #define SF_BIAS 127 #define SF_MANTBITS 23 --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153988; cv=none; d=zohomail.com; s=zohoarc; b=AXIA9mR1zJULIAbJS/Ap1M4qjUkOjone4FRrmnB7eLtDo9rQVFYXGjYhTiuoxEFEqiCKJm6+liwQPCZFoCrhvl2hg8l2Dv4V6ow04qPYAQZkkbbD4fW8aCQ6NtF0w2aG3oLjlZGMYgO0EIRwk4osN4QVcPf3/a4nnfn+E9HJAj0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153988; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=e9Q0spTYTQ6FChSJsHReGLb3mMStqUbY3UIv4xcEKXQ=; b=e+0bV3QxlzUW2yZdfy8fR1CQA4Z5t4NGCR3cFGuQk5HtiRbq2JAQu6/Fv6hBLjgAnbiGTQzut6376EThKzctcnvWehUt/6rLDCHLM0kyNkQmdEJNfcjulMf5VFl5sdIBvErAL2OIi8N2z5k3qA/B2xy8RS2mdL2e4yvB1TKDVQc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153988386647.0835177818143; Wed, 20 Nov 2024 17:53:08 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwK7-0008Hu-DP; Wed, 20 Nov 2024 20:49:35 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwK6-00087S-5l for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:34 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwK4-0004tX-DY for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=e9Q0spTYTQ6FChSJsHReGLb3mMStqUbY3UIv4xcEKXQ=; b=nxVu8FWkMR2wWBb +KWQA0MvgmQGgKINhTshe7ddIXWZ3zfZVUarFHvaclMfbdyVpBC/kXy5tu3y8txBKepvasBfuYwwL FGNqeqyr1XcESac+m6Ly+SYt+EoX5PsdRFTqhdyfWceRN7/tueVkmWTt/D5AbohHDnXBfg6qcSy+q ZQ=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 41/43] target/hexagon: Manually call generated HVX instructions Date: Thu, 21 Nov 2024 02:49:45 +0100 Message-ID: <20241121014947.18666-42-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153989213116600 Content-Type: text/plain; charset="utf-8" For HVX instructions that were successfully translated by helper-to-tcg, emit calls to emit_*() "manually" from generate_*(). Recall that scalar instructions translated by helper-to-tcg are automatically called by a hook in tcg_gen_callN. Signed-off-by: Anton Johansson --- target/hexagon/gen_tcg_funcs.py | 13 ++++++++ target/hexagon/hex_common.py | 57 +++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/target/hexagon/gen_tcg_funcs.py b/target/hexagon/gen_tcg_funcs= .py index a06edeb9de..fd8dbf3724 100755 --- a/target/hexagon/gen_tcg_funcs.py +++ b/target/hexagon/gen_tcg_funcs.py @@ -75,7 +75,18 @@ def gen_tcg_func(f, tag, regs, imms): =20 arguments =3D ", ".join(["ctx", "ctx->insn", "ctx->pkt"] + declare= d) f.write(f" emit_{tag}({arguments});\n") + elif hex_common.is_helper_to_tcg_enabled(tag) and tag.startswith("V6_"= ): + ## For vector functions translated by helper-to-tcg we need to + ## manually call the emitted code. All other instructions transla= ted + ## are automatically called by the helper-functions dispatcher in + ## tcg_gen_callN. + declared =3D [] + ## Handle registers + for arg in hex_common.helper_to_tcg_hvx_call_args(tag, regs, imms): + declared.append(arg) =20 + arguments =3D ", ".join(declared) + f.write(f" emit_{tag}({arguments});\n") elif hex_common.skip_qemu_helper(tag): f.write(f" fGEN_TCG_{tag}({hex_common.semdict[tag]});\n") else: @@ -119,6 +130,8 @@ def main(): f.write("#define HEXAGON_TCG_FUNCS_H\n\n") if args.idef_parser: f.write('#include "idef-generated-emitter.h.inc"\n\n') + if args.helper_to_tcg: + f.write('#include "helper-to-tcg-emitted.h"\n\n') =20 for tag in hex_common.tags: ## Skip the priv instructions diff --git a/target/hexagon/hex_common.py b/target/hexagon/hex_common.py index fc4c9f648e..8391084982 100755 --- a/target/hexagon/hex_common.py +++ b/target/hexagon/hex_common.py @@ -1105,6 +1105,60 @@ def helper_ret_type(tag, regs): return return_type =20 =20 +def helper_to_tcg_hvx_call_args(tag, regs, imms): + args =3D [] + # Used to ensure immediates are passed translated as immediates by + # helper-to-tcg. + imm_indices =3D [] + + ## First argument is the CPU state + if need_env(tag): + args.append("tcg_env") + + ## For predicated instructions, we pass in the destination register + if is_predicated(tag): + for regtype, regid in regs: + reg =3D get_register(tag, regtype, regid) + if reg.is_writeonly() and not reg.is_hvx_reg(): + args.append(reg.helper_arg().call_arg) + + ## Pass the HVX destination registers + for regtype, regid in regs: + reg =3D get_register(tag, regtype, regid) + if reg.is_written() and reg.is_hvx_reg(): + args.append(reg.hvx_off()) + + ## Pass the source registers + for regtype, regid in regs: + reg =3D get_register(tag, regtype, regid) + if reg.is_read() and not (reg.is_hvx_reg() and reg.is_readwrite()): + if reg.is_hvx_reg(): + args.append(reg.hvx_off()) + else: + args.append(reg.helper_arg().call_arg) + + ## Pass the immediates + for immlett, bits, immshift in imms: + imm_indices.append(len(args)) + args.append(f"{imm_name(immlett)}") + + ## Other stuff the helper might need + if need_pkt_has_multi_cof(tag): + args.append("ctx->pkt->pkt_has_multi_cof") + if need_pkt_need_commit(tag): + args.append("ctx->need_commit") + if need_PC(tag): + args.append("ctx->pkt->pc") + if need_next_PC(tag): + args.append("ctx->next_PC") + if need_slot(tag): + args.append("gen_slotval(ctx)") + if need_part1(tag): + args.append("insn->part1") + + return args + + def helper_args(tag, regs, imms): args =3D [] # Used to ensure immediates are passed translated as immediates by @@ -1216,12 +1270,15 @@ def parse_common_args(desc): parser.add_argument("overrides_vec", help=3D"vector overrides file") parser.add_argument("out", help=3D"output file") parser.add_argument("--idef-parser", help=3D"file of instructions tran= slated by idef-parser") + parser.add_argument("--helper-to-tcg", help=3D"file of instructions tr= anslated by helper-to-tcg") args =3D parser.parse_args() read_semantics_file(args.semantics) read_overrides_file(args.overrides) read_overrides_file(args.overrides_vec) if args.idef_parser: read_idef_parser_enabled_file(args.idef_parser) + if args.helper_to_tcg: + read_helper_to_tcg_enabled_file(args.helper_to_tcg) calculate_attribs() init_registers() return args --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153970; cv=none; d=zohomail.com; s=zohoarc; b=i0TsaHQG+ABkQA/0EBjMsOwHH3DI4NkXSJJScm6xt9tAhZ+VJamZ3fZjCElsWNAMHICia6GYc8ELEhtj1Dqoyt+xvXg2WS6I+GQfnr+/zTo5gsthx6ldT8itZ1dYvBr2Ed8khjyLefn+DTEqaoGpVzifJH7zbxQJ35r/t+qpNH8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153970; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=dmXpWhtvpyPXyh2A6q90RECDvEn6x4P02umFqSdoGtk=; b=BzRJdrM0NVdovAOc8tdCFb+rUrG3kMKLumDg4Z1NRL3uq80u+1F+AduXrCgRZ8mt+Hu0QP7S2N/y20F4+zMhRf8JhmJDL06T/bzR8fD0/e4LM9ERu+0ev70lFtlXeK3/GyTt9n8xp+VOIVd9Fzh27sTlzv+Z2ANPRL+6MAjPgQc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153970294660.9417954038357; Wed, 20 Nov 2024 17:52:50 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwKE-0000uk-Co; Wed, 20 Nov 2024 20:49:42 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwKC-0000ei-OG for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:40 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwKB-0004uW-AA for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:49:40 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=dmXpWhtvpyPXyh2A6q90RECDvEn6x4P02umFqSdoGtk=; b=vU6S1umXgQtilM/ Pkd0N6voSAC2bHfG5+dDtbRuWN1CxVF8Ja+TLYYBxpRkS4gplxar23AVLUMX/+tUmHk26LJBEZ/Mj KZ1Rz+mnd0k6RYLiU0tL/OeisqoJ5xBp1iGs9Kc02+j6PA+voZglQ3FlfFe4gWfuCBP0WK1eig+8/ 8k=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 42/43] target/hexagon: Only translate w. idef-parser if helper-to-tcg failed Date: Thu, 21 Nov 2024 02:49:46 +0100 Message-ID: <20241121014947.18666-43-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153971087116600 Content-Type: text/plain; charset="utf-8" Only generate input functions to idef-parser for instructions which failed to be translated by helper-to-tcg. Signed-off-by: Anton Johansson --- target/hexagon/gen_idef_parser_funcs.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/target/hexagon/gen_idef_parser_funcs.py b/target/hexagon/gen_i= def_parser_funcs.py index 2f6e826f76..08ae94a646 100644 --- a/target/hexagon/gen_idef_parser_funcs.py +++ b/target/hexagon/gen_idef_parser_funcs.py @@ -49,10 +49,13 @@ def main(): ) parser.add_argument("semantics", help=3D"semantics file") parser.add_argument("out", help=3D"output file") + parser.add_argument("--helper-to-tcg", help=3D"file of instructions tr= anslated by helper-to-tcg") args =3D parser.parse_args() hex_common.read_semantics_file(args.semantics) hex_common.calculate_attribs() hex_common.init_registers() + if args.helper_to_tcg: + hex_common.read_helper_to_tcg_enabled_file(args.helper_to_tcg) tagregs =3D hex_common.get_tagregs() tagimms =3D hex_common.get_tagimms() =20 @@ -60,6 +63,9 @@ def main(): f.write('#include "macros.h.inc"\n\n') =20 for tag in hex_common.tags: + ## Skip instructions translated by helper-to-tcg + if hex_common.is_helper_to_tcg_enabled(tag): + continue ## Skip the priv instructions if "A_PRIV" in hex_common.attribdict[tag]: continue --=20 2.45.2 From nobody Sat Nov 23 15:18:35 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1732153809; cv=none; d=zohomail.com; s=zohoarc; b=fo2cMXY0Rs9RnibbUAO5rr4K0iuojqtc3zKpLxOik/tcAdxXEWs5R+R7QDiY0zefavaysFUTzJ8fuiA/kNHIZmZZ/uB5LCr1t58wkmUgNCJhRXs7VXEMrXN+aGOvBoXme8qDsAGSParmni4pt5wn3VH2Y2F1xSDkVVOo2T/dQQw= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1732153809; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=ktv0i4T9quoFh1XqbHeead8oN9HJCwFMqj8THqsqa04=; b=gWx+Rig7AJukWrxUyvb0KXhOX2Q0rnzHDrJ95Olz2v5Lup9C81Z4Z+amgqgolaAu7nppNhI5TnQ6CKAzJugmNj4C/PrC5p3Sjw/H7XXDfKrr5JpLpG3mHRrUzR5S4LQawW0WWPCRNJ3HV3YSg/Fr3qDK+rdYBGhdRYLsV/D5WmU= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1732153809836207.19426300041653; Wed, 20 Nov 2024 17:50:09 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1tDwIz-0004Hj-5w; Wed, 20 Nov 2024 20:48:25 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwIx-00042K-8j for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:23 -0500 Received: from rev.ng ([94.130.142.21]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1tDwIu-0004hy-0I for qemu-devel@nongnu.org; Wed, 20 Nov 2024 20:48:22 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=rev.ng; s=dkim; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Subscribe: List-Post:List-Owner:List-Archive:List-Unsubscribe:List-Unsubscribe-Post: List-Help; bh=ktv0i4T9quoFh1XqbHeead8oN9HJCwFMqj8THqsqa04=; b=PVqGTMib49hP9Z2 OZtlZ8SOe1PAbISs/5MJTmf9evN7cIWHaSJSJ/lZL1fmtwAMY11472wgiKZCJ/fCbuZPbTjdwKFZy W8g2fniFXqHRl7I3DMg66/NnVz6W8QmTDcReD+STcii0uXoXvh6rXLRu8CCCQ2SFY+NLdDyBo5DYM ns=; To: qemu-devel@nongnu.org Cc: ale@rev.ng, ltaylorsimpson@gmail.com, bcain@quicinc.com, richard.henderson@linaro.org, philmd@linaro.org, alex.bennee@linaro.org Subject: [RFC PATCH v1 43/43] target/hexagon: Use helper-to-tcg Date: Thu, 21 Nov 2024 02:49:47 +0100 Message-ID: <20241121014947.18666-44-anjo@rev.ng> In-Reply-To: <20241121014947.18666-1-anjo@rev.ng> References: <20241121014947.18666-1-anjo@rev.ng> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=94.130.142.21; envelope-from=anjo@rev.ng; helo=rev.ng X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, RCVD_IN_VALIDITY_SAFE_BLOCKED=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Anton Johansson From: Anton Johansson via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1732153810627116600 Content-Type: text/plain; charset="utf-8" Modifies meson.build to use helper-to-tcg for automatic translation of helper functions. Any helper functions with the "helper-to-tcg" attribute will be automatically translated to TCG. Order of code generation is changed, and helper functions are always generated first, for all instructions. Helper functions are needed as input helper-to-tcg. Next, input to idef-parser is generated for all instructions that were not successfully translated by helper-to-tcg. As such, a majority of instructions will be translated by helper-to-tcg, and the remaining instructions fed through idef-parser can be reduced moving forward. Signed-off-by: Anton Johansson --- target/hexagon/meson.build | 151 +++++++++++++++++++++++++++---------- 1 file changed, 111 insertions(+), 40 deletions(-) diff --git a/target/hexagon/meson.build b/target/hexagon/meson.build index bb4ebaae81..563d60e976 100644 --- a/target/hexagon/meson.build +++ b/target/hexagon/meson.build @@ -262,22 +262,127 @@ hexagon_ss.add(files( 'mmvec/system_ext_mmvec.c', )) =20 +helper_dep =3D [semantics_generated] +helper_in =3D [semantics_generated, gen_tcg_h, gen_tcg_hvx_h] + +# +# Step 5 +# We use Python scripts to generate the following files +# helper_protos_generated.h.inc +# helper_funcs_generated.c.inc +# analyze_funcs_generated.c.inc +# +helper_protos_generated =3D custom_target( + 'helper_protos_generated.h.inc', + output: 'helper_protos_generated.h.inc', + depends: helper_dep, + depend_files: [hex_common_py, gen_tcg_h, gen_tcg_hvx_h], + command: [python, files('gen_helper_protos.py'), helper_in, '@OUTPUT@'= ], +) +hexagon_ss.add(helper_protos_generated) + +helper_funcs_generated =3D custom_target( + 'helper_funcs_generated.c.inc', + output: 'helper_funcs_generated.c.inc', + depends: helper_dep, + depend_files: [hex_common_py, gen_tcg_h, gen_tcg_hvx_h], + command: [python, files('gen_helper_funcs.py'), helper_in, '@OUTPUT@'], +) +hexagon_ss.add(helper_funcs_generated) + +analyze_funcs_generated =3D custom_target( + 'analyze_funcs_generated.c.inc', + output: 'analyze_funcs_generated.c.inc', + depends: helper_dep, + depend_files: [hex_common_py, gen_tcg_h, gen_tcg_hvx_h], + command: [python, files('gen_analyze_funcs.py'), helper_in, '@OUTPUT@'= ], +) +hexagon_ss.add(analyze_funcs_generated) + +# +# Step 6 +# If enabled, run helper-to-tcg to attempt to translate any remaining +# helper functions, producing: +# helper-to-tcg-emitted.c +# helper-to-tcg-emitted.h +# helper-to-tcg-enabled +# helper-to-tcg-log # -# Step 4.5 + +if helper_to_tcg_enabled + helper_to_tcg =3D subproject('helper-to-tcg', required: true) + helper_to_tcg_get_llvm_ir_cmd =3D helper_to_tcg.get_variable('get_llvm= _ir_cmd') + helper_to_tcg_pipeline =3D helper_to_tcg.get_variable('pipeline') +endif + +idef_command_extra =3D [] +idef_dep_extra =3D [] +if helper_to_tcg_enabled + helper_to_tcg_input_files =3D [ + meson.current_source_dir() / 'op_helper.c', + meson.current_source_dir() / 'translate.c', + meson.current_source_dir() / 'reg_fields.c', + meson.current_source_dir() / 'arch.c', + ] + + ll =3D custom_target('to-ll', + input: helper_to_tcg_input_files, + output:'helper-to-tcg-input.ll', + depends: [helper_funcs_generated, helper_protos_generated], + command: helper_to_tcg_get_llvm_ir_cmd + ['-o', '@OUTPUT@', '@INPU= T@', '--target-path', 'target/hexagon'] + ) + + helper_to_tcg_target =3D custom_target('helper-to-tcg-hexagon', + output: ['helper-to-tcg-emitted.c', + 'helper-to-tcg-emitted.h', + 'helper-to-tcg-enabled', + 'helper-to-tcg-log'], + input: [ll], + depends: [helper_to_tcg_pipeline, analyze_funcs_generated, helper_= funcs_generated, helper_protos_generated], + command: [helper_to_tcg_pipeline, + '--temp-vector-block=3Dtmp_vmem', + '--mmu-index-function=3Dget_tb_mmu_index', + '--tcg-global-mappings=3Dtcg_global_mappings', + '--output-source=3D@OUTPUT0@', + '--output-header=3D@OUTPUT1@', + '--output-enabled=3D@OUTPUT2@', + '--output-log=3D@OUTPUT3@', + '@INPUT@'] + ) + + hexagon_ss.add(helper_to_tcg_target) + + # List of instructions for which TCG generation was successful + generated_tcg_list =3D helper_to_tcg_target[2].full_path() + + # Setup dependencies for idef-parser + idef_dep_extra +=3D helper_to_tcg_target + idef_command_extra +=3D ['--helper-to-tcg', generated_tcg_list] + + # Setup input and dependencies for the final step, this depends on whe= ther + helper_dep +=3D [helper_to_tcg_target] + helper_in +=3D ['--helper-to-tcg', generated_tcg_list] +endif + + + +# +# Step 6 # We use flex/bison based idef-parser to generate TCG code for a lot # of instructions. idef-parser outputs # idef-generated-emitter.c # idef-generated-emitter.h.inc # idef-generated-enabled-instructions # + idef_parser_enabled =3D get_option('hexagon_idef_parser') if idef_parser_enabled and 'hexagon-linux-user' in target_dirs idef_parser_input_generated =3D custom_target( 'idef_parser_input.h.inc', output: 'idef_parser_input.h.inc', - depends: [semantics_generated], + depends: [semantics_generated, ] + idef_dep_extra, depend_files: [hex_common_py], - command: [python, files('gen_idef_parser_funcs.py'), semantics_gen= erated, '@OUTPUT@'], + command: [python, files('gen_idef_parser_funcs.py'), semantics_gen= erated, '@OUTPUT@'] + idef_command_extra ) =20 preprocessed_idef_parser_input_generated =3D custom_target( @@ -345,39 +450,14 @@ if idef_parser_enabled and 'hexagon-linux-user' in ta= rget_dirs =20 # Setup input and dependencies for the next step, this depends on whet= her or # not idef-parser is enabled - helper_dep =3D [semantics_generated, idef_generated_tcg_c, idef_genera= ted_tcg] - helper_in =3D [semantics_generated, gen_tcg_h, gen_tcg_hvx_h, '--idef-= parser', idef_generated_list] -else - # Setup input and dependencies for the next step, this depends on whet= her or - # not idef-parser is enabled - helper_dep =3D [semantics_generated] - helper_in =3D [semantics_generated, gen_tcg_h, gen_tcg_hvx_h] + helper_dep +=3D [idef_generated_tcg_c, idef_generated_tcg] + helper_in +=3D ['--idef-parser', idef_generated_list] endif =20 # -# Step 5 +# Step 7 # We use Python scripts to generate the following files -# helper_protos_generated.h.inc -# helper_funcs_generated.c.inc # tcg_funcs_generated.c.inc -# -helper_protos_generated =3D custom_target( - 'helper_protos_generated.h.inc', - output: 'helper_protos_generated.h.inc', - depends: helper_dep, - depend_files: [hex_common_py, gen_tcg_h, gen_tcg_hvx_h], - command: [python, files('gen_helper_protos.py'), helper_in, '@OUTPUT@'= ], -) -hexagon_ss.add(helper_protos_generated) - -helper_funcs_generated =3D custom_target( - 'helper_funcs_generated.c.inc', - output: 'helper_funcs_generated.c.inc', - depends: helper_dep, - depend_files: [hex_common_py, gen_tcg_h, gen_tcg_hvx_h], - command: [python, files('gen_helper_funcs.py'), helper_in, '@OUTPUT@'], -) -hexagon_ss.add(helper_funcs_generated) =20 tcg_funcs_generated =3D custom_target( 'tcg_funcs_generated.c.inc', @@ -388,13 +468,4 @@ tcg_funcs_generated =3D custom_target( ) hexagon_ss.add(tcg_funcs_generated) =20 -analyze_funcs_generated =3D custom_target( - 'analyze_funcs_generated.c.inc', - output: 'analyze_funcs_generated.c.inc', - depends: helper_dep, - depend_files: [hex_common_py, gen_tcg_h, gen_tcg_hvx_h], - command: [python, files('gen_analyze_funcs.py'), helper_in, '@OUTPUT@'= ], -) -hexagon_ss.add(analyze_funcs_generated) - target_arch +=3D {'hexagon': hexagon_ss} --=20 2.45.2