From patchwork Mon Mar 18 06:05:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1913003 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=TXk7+V6Z; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4Tykrd53DHz1yWn for ; Mon, 18 Mar 2024 17:06:39 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 078653858C98 for ; Mon, 18 Mar 2024 06:06:37 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by sourceware.org (Postfix) with ESMTPS id ACC723858D37 for ; Mon, 18 Mar 2024 06:06:01 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ACC723858D37 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=intel.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org ACC723858D37 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.10 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710741973; cv=none; b=OmaA2+nVqDvSNes/CSJsT7cNMA5h99MrAFO9pfQcGAiYax/Zo06gAV6h9Y36FTRd6HlzAN2d6UpwfdhRmuO8FmJQs0JgdugHB7kFokY1hCjaWSkJV78yl0/OEvFXgZ5QEjKhbqGilpFAGIEaEUBaI1EdW03ak9/Ks+YpSgl7j1Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1710741973; c=relaxed/simple; bh=M63anYIlvEcamc11jwGQt0/uLDc3WtnG5wVoNzWBkAY=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=k5seazr0fpt3D0mo0k30b1FeGT+BpLW1Qser55Vvao9D13XurPcY0xlrMkDBleUtbHbB/aC+GxR01mxqN+6Kghj2jiZ0P4rtq96j9L8XX6JhLlN3R77fWGDg299f9QiGZ5qNO05ZjNCYdikk/QWOeyEQZ83wwxGXvmLrRLHx0qw= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1710741962; x=1742277962; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=M63anYIlvEcamc11jwGQt0/uLDc3WtnG5wVoNzWBkAY=; b=TXk7+V6ZmBqvsVCgZ74VCczFekqApHTdjY31sREnF4T6HvvUdgWfmQzP QjPUjJFNIPIZVQ1YuDS1w6sSVSYi8FeAdTl0maVd6GFsPc80YeWae6LEE ySXagbShvmChULOKBmTCgJ15Bf9m6ozivZPjzzCWCYv0JZ+Lg+B3ltDw4 hcC7YLVNfTiuUT7q0Tc0AsEFK6+HKQmL4vtEfrqCAmkRD5qv3PzuLTB/O EYUfdFhmmaoV28JEKCV/wopGsnXGztyKsIUJA+JqyBq8lQQNzAeNfMWiL 9DDcJo4iWhNHOAQV3Kop3GhsH6onM7NWJrG9T1JSXWRHTo4TQNPdNrjKF g==; X-IronPort-AV: E=McAfee;i="6600,9927,11016"; a="22996468" X-IronPort-AV: E=Sophos;i="6.07,134,1708416000"; d="scan'208";a="22996468" Received: from orviesa010.jf.intel.com ([10.64.159.150]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 17 Mar 2024 23:06:01 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,134,1708416000"; d="scan'208";a="13283567" Received: from shvmail02.sh.intel.com ([10.239.244.9]) by orviesa010.jf.intel.com with ESMTP; 17 Mar 2024 23:05:59 -0700 Received: from pli-ubuntu.sh.intel.com (pli-ubuntu.sh.intel.com [10.239.159.47]) by shvmail02.sh.intel.com (Postfix) with ESMTP id B00CD10057F1; Mon, 18 Mar 2024 14:05:57 +0800 (CST) From: pan2.li@intel.com To: gcc-patches@gcc.gnu.org Cc: juzhe.zhong@rivai.ai, kito.cheng@gmail.com, yanzhang.wang@intel.com, Pan Li Subject: [PATCH v1] RISC-V: Bugfix ICE for __attribute__((target("arch=+v")) Date: Mon, 18 Mar 2024 14:05:55 +0800 Message-Id: <20240318060555.2823258-1-pan2.li@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.6 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE, WEIRD_PORT autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org From: Pan Li This patch would like to fix one ICE for __attribute__((target("arch=+v")) and likewise extension(s). Given we have sample code as below: void __attribute__((target("arch=+v"))) test_2 (int *a, int *b, int *out, unsigned count) { unsigned i; for (i = 0; i < count; i++) out[i] = a[i] + b[i]; } It will have ICE when build with -march=rv64gc -O3. test.c: In function ‘test_2’: test.c:4:1: internal compiler error: Floating point exception 4 | { | ^ 0x1a5891b crash_signal .../__RISC-V_BUILD__/../gcc/toplev.cc:319 0x7f0a7884251f ??? ./signal/../sysdeps/unix/sysv/linux/x86_64/libc_sigaction.c:0 0x1f51ba4 riscv_hard_regno_nregs .../__RISC-V_BUILD__/../gcc/config/riscv/riscv.cc:8143 0x1967bb9 init_reg_modes_target() .../__RISC-V_BUILD__/../gcc/reginfo.cc:471 0x13fc029 init_emit_regs() .../__RISC-V_BUILD__/../gcc/emit-rtl.cc:6237 0x1a5b83d target_reinit() .../__RISC-V_BUILD__/../gcc/toplev.cc:1936 0x35e374d save_target_globals() .../__RISC-V_BUILD__/../gcc/target-globals.cc:92 0x35e381f save_target_globals_default_opts() .../__RISC-V_BUILD__/../gcc/target-globals.cc:122 0x1f544cc riscv_save_restore_target_globals(tree_node*) .../__RISC-V_BUILD__/../gcc/config/riscv/riscv.cc:9138 0x1f55c36 riscv_set_current_function ... There are two reasons for this ICE. 1. The implied extension(s) of v are not well handled and the TARGET_MIN_VLEN is 0 which is not reinitialized. Then the size / TARGET_MIN_VLEN will have DivideByZero. 2. The machine modes of the vector types will be vary after the v extension is introduced. This patch passed below testsuite: 1. The riscv fully regression test. PR target/114352 gcc/ChangeLog: * common/config/riscv/riscv-common.cc (riscv_subset_list::parse_single_ext): Add implied, combine and conflict check after parse single extension. * config/riscv/riscv.cc (riscv_set_current_function): Reini the machine mode before when set cur function. gcc/testsuite/ChangeLog: * gcc.target/riscv/rvv/base/pr114352-1.c: New test. * gcc.target/riscv/rvv/base/pr114352-2.c: New test. Signed-off-by: Pan Li Signed-off-by: Pan Li --- gcc/common/config/riscv/riscv-common.cc | 33 ++++++++--- gcc/config/riscv/riscv.cc | 4 ++ .../gcc.target/riscv/rvv/base/pr114352-1.c | 58 +++++++++++++++++++ .../gcc.target/riscv/rvv/base/pr114352-2.c | 27 +++++++++ 4 files changed, 115 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/pr114352-1.c create mode 100644 gcc/testsuite/gcc.target/riscv/rvv/base/pr114352-2.c diff --git a/gcc/common/config/riscv/riscv-common.cc b/gcc/common/config/riscv/riscv-common.cc index 48efef40dfd..d32bf147eca 100644 --- a/gcc/common/config/riscv/riscv-common.cc +++ b/gcc/common/config/riscv/riscv-common.cc @@ -1375,20 +1375,39 @@ riscv_subset_list::parse_single_multiletter_ext (const char *p, const char * riscv_subset_list::parse_single_ext (const char *p, bool exact_single_p) { + const char *end_of_ext; + switch (p[0]) { case 'x': - return parse_single_multiletter_ext (p, "x", "non-standard extension", - exact_single_p); + end_of_ext = parse_single_multiletter_ext (p, "x", + "non-standard extension", + exact_single_p); + break; case 'z': - return parse_single_multiletter_ext (p, "z", "sub-extension", - exact_single_p); + end_of_ext = parse_single_multiletter_ext (p, "z", "sub-extension", + exact_single_p); + break; case 's': - return parse_single_multiletter_ext (p, "s", "supervisor extension", - exact_single_p); + end_of_ext = parse_single_multiletter_ext (p, "s", "supervisor extension", + exact_single_p); + break; default: - return parse_single_std_ext (p, exact_single_p); + end_of_ext = parse_single_std_ext (p, exact_single_p); + break; } + + /* Make sure the implied or combined extension is included after add + a new std extension to subset list. For exmaple as below, + + void __attribute__((target("arch=+v"))) func () with -march=rv64gc. + + The implied zvl128b and zve64d of the std v should be included. */ + handle_implied_ext (p); + handle_combine_ext (); + check_conflict_ext (); + + return end_of_ext; } /* Parsing arch string to subset list, return NULL if parsing failed. */ diff --git a/gcc/config/riscv/riscv.cc b/gcc/config/riscv/riscv.cc index 680c4a728e9..89acb94af10 100644 --- a/gcc/config/riscv/riscv.cc +++ b/gcc/config/riscv/riscv.cc @@ -9474,6 +9474,10 @@ riscv_set_current_function (tree decl) cl_target_option_restore (&global_options, &global_options_set, TREE_TARGET_OPTION (new_tree)); + /* The ISA extension can vary based on the function extension like target. + Thus, make sure that the machine modes are reflected correctly here. */ + init_adjust_machine_modes (); + riscv_save_restore_target_globals (new_tree); } diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/pr114352-1.c b/gcc/testsuite/gcc.target/riscv/rvv/base/pr114352-1.c new file mode 100644 index 00000000000..b3f1f20fb79 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/pr114352-1.c @@ -0,0 +1,58 @@ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc -mabi=lp64 -O3 -fno-schedule-insns -fno-schedule-insns2" } */ +/* { dg-final { check-function-bodies "**" "" } } */ + +/* +** test_1: +** beq\s+a3,\s*zero,\s*\.L[0-9]+ +** ... +** bne\s+[atx][0-9]+,\s*[atx][0-9]+,\s*\.L[0-9]+ +** ... +** ret +*/ +void +test_1 (int *a, int *b, int *out, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; i++) + out[i] = a[i] + b[i]; +} + +/* +** test_2: +** ... +** vsetvli\s+[atx][0-9]+,\s*[atx][0-9]+,\s*e32,\s*m1,\s*ta,\s*ma +** ... +** vadd\.vv\s+v[0-9]+,\s*v[0-9]+,\s*v[0-9]+ +** ... +*/ +void +__attribute__((target("arch=+v"))) +test_2 (int *a, int *b, int *out, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; i++) + out[i] = a[i] + b[i]; +} + +/* +** test_3: +** beq\s+a3,\s*zero,\s*\.L[0-9]+ +** ... +** bne\s+[atx][0-9]+,\s*[atx][0-9]+,\s*\.L[0-9]+ +** ... +** ret +*/ +void +test_3 (int *a, int *b, int *out, unsigned count) +{ + unsigned i; + + for (i = 0; i < count; i++) + out[i] = a[i] + b[i]; +} + +/* { dg-final { scan-assembler ".attribute arch, \"rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_zicsr2p0_zifencei2p0\"" } } */ +/* { dg-final { scan-assembler ".option arch, rv64i2p1_m2p0_a2p1_f2p2_d2p2_c2p0_v1p0_zicsr2p0_zifencei2p0_zve32f1p0_zve32x1p0_zve64d1p0_zve64f1p0_zve64x1p0_zvl128b1p0_zvl32b1p0_zvl64b1p0" } } */ diff --git a/gcc/testsuite/gcc.target/riscv/rvv/base/pr114352-2.c b/gcc/testsuite/gcc.target/riscv/rvv/base/pr114352-2.c new file mode 100644 index 00000000000..3b3d69d2751 --- /dev/null +++ b/gcc/testsuite/gcc.target/riscv/rvv/base/pr114352-2.c @@ -0,0 +1,27 @@ +/* Test that we do not have ice when compile */ +/* { dg-do compile } */ +/* { dg-options "-march=rv64gc -mabi=lp64 -O3" } */ + +#define DEF_ATTR_FUNC(ATTR, ID) \ +void ATTR \ +test_##ID (int *a, int *b, int *out, unsigned count) \ +{ \ + unsigned i; \ + \ + for (i = 0; i < count; i++) \ + out[i] = a[i] + b[i]; \ +} + +DEF_ATTR_FUNC (__attribute__((target("arch=+zve32x"))), 1) +DEF_ATTR_FUNC (__attribute__((target("arch=+zve32f"))), 2) +DEF_ATTR_FUNC (__attribute__((target("arch=+zve64x"))), 3) +DEF_ATTR_FUNC (__attribute__((target("arch=+zve64f"))), 4) +DEF_ATTR_FUNC (__attribute__((target("arch=+zve64d"))), 5) +DEF_ATTR_FUNC (__attribute__((target("arch=+v"))), 6) +DEF_ATTR_FUNC (__attribute__((target("arch=+zvl64b"))), 7) +DEF_ATTR_FUNC (__attribute__((target("arch=+zvl128b"))), 8) +DEF_ATTR_FUNC (__attribute__((target("arch=+zvl256b"))), 9) +DEF_ATTR_FUNC (__attribute__((target("arch=+zvl512b"))), 10) +DEF_ATTR_FUNC (__attribute__((target("arch=+zvl1024b"))), 11) +DEF_ATTR_FUNC (__attribute__((target("arch=+zvl2048b"))), 12) +DEF_ATTR_FUNC (__attribute__((target("arch=+zvl4096b"))), 13)