From patchwork Fri Jul 19 11:10:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Pan2" X-Patchwork-Id: 1962450 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=ncXIr2hg; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WQRmt4bswz1ySl for ; Fri, 19 Jul 2024 21:10:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 602DD386075A for ; Fri, 19 Jul 2024 11:10:50 +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.12]) by sourceware.org (Postfix) with ESMTPS id 2E8353857349 for ; Fri, 19 Jul 2024 11:10:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2E8353857349 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 2E8353857349 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=198.175.65.12 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721387427; cv=none; b=fpfeKcp0vc/2wsqMeP+TfGLgJdkv8r8PYy3T8WQa+TAAwvtDHEkfiGZdi+8AD9pcRBcIhtFR9V3BPOsJDYwHisBwlS5tr8aY4T6zDU18SAY6EhR+jz9u/2AWuDQVecgdOVLnsxG6Y9LEdfEJFA/UFakbSPYaTaJx7cB7dmYHxzA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1721387427; c=relaxed/simple; bh=c9nI+obErR8iVP0kn0rf0LmRi6NsQiEqx0DxsQz4Bh4=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=QMLy3wNuXMAZUs5dnRAVBvGcN1dJ2ZyPEYwXOzC8WBIYG+L9+UdawW5YJvK/viUVAMQyx/L1FpTaUPNxUFhfRvH7xsIsVOJDKAhTr/cQXwpFfjVpO0Z+WvyK3M9VcpS4XXnXOQyMZdcINQqCOWLTWN3nSbs9kk4mngIXzf5cfP4= 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=1721387425; x=1752923425; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=c9nI+obErR8iVP0kn0rf0LmRi6NsQiEqx0DxsQz4Bh4=; b=ncXIr2hg+g00hJsJkHCjS5glwYkuAlFaD5CLQz8sJNiG39Ul49xXNnwE t9smIheVoMqDrTCWuiKD5mTF7Cl0Kv3EmrGj0bju0Oy4NqALIJlPbf0Gr NSsYip6IQY9LzcmmAr2ppypKNlAcy86fdJD9mxF/Nzm23BCyy0WZKRwp4 rTs8JeR9AT8YXu/ys4n7ixukmAAycX4gmcgQyr39P7pL+tp1j9wwAr7zC Ag/fg+7jNP0hHbCskNpw/oMxZMwR++EQS+/vZHioJI2/h3fFT715D+E/f g66nnUT+DWVRgVK+uqq3QPCs8mWJXb7btbCQnR6DQxX4y2O4a35sbCoQ6 g==; X-CSE-ConnectionGUID: v1Ot++MWQN2laFYUa3gAZQ== X-CSE-MsgGUID: yUQXoaiETv62CECZoaJi7w== X-IronPort-AV: E=McAfee;i="6700,10204,11137"; a="30377402" X-IronPort-AV: E=Sophos;i="6.09,220,1716274800"; d="scan'208";a="30377402" Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jul 2024 04:10:24 -0700 X-CSE-ConnectionGUID: 3a1OqiZBTfKfr+W17YN4Ag== X-CSE-MsgGUID: ffzHpLcVTeOo8EyC2sYgDg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.09,220,1716274800"; d="scan'208";a="50819708" Received: from shvmail02.sh.intel.com ([10.239.244.9]) by fmviesa007.fm.intel.com with ESMTP; 19 Jul 2024 04:10:10 -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 A206A1005672; Fri, 19 Jul 2024 19:10:09 +0800 (CST) From: pan2.li@intel.com To: gcc-patches@gcc.gnu.org Cc: juzhe.zhong@rivai.ai, kito.cheng@gmail.com, richard.guenther@gmail.com, tamar.christina@arm.com, jeffreyalaw@gmail.com, rdapp.gcc@gmail.com, richard.sandiford@arm.com, Pan Li Subject: [PATCH v2] Internal-fn: Only allow type matches mode for internal fn[PR115961] Date: Fri, 19 Jul 2024 19:10:08 +0800 Message-Id: <20240719111008.2566858-1-pan2.li@intel.com> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 X-Spam-Status: No, score=-11.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_NONE, TXREP 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 The direct_internal_fn_supported_p has no restrictions for the type modes. For example the bitfield like below will be recog as .SAT_TRUNC. struct e { unsigned pre : 12; unsigned a : 4; }; __attribute__((noipa)) void bug (e * v, unsigned def, unsigned use) { e & defE = *v; defE.a = min_u (use + 1, 0xf); } This patch would like to check strictly for the direct_internal_fn_supported_p, and only allows the type matches mode for ifn type tree pair. The below test suites are passed for this patch: 1. The rv64gcv fully regression tests. 2. The x86 bootstrap tests. 3. The x86 fully regression tests. PR target/115961 gcc/ChangeLog: * internal-fn.cc (type_strictly_matches_mode_p): Add new func impl to check type strictly matches mode or not. (type_pair_strictly_matches_mode_p): Ditto but for tree type pair. (direct_internal_fn_supported_p): Add above check for the tree type pair. gcc/testsuite/ChangeLog: * g++.target/i386/pr115961-run-1.C: New test. * g++.target/riscv/rvv/base/pr115961-run-1.C: New test. Signed-off-by: Pan Li --- gcc/internal-fn.cc | 32 +++++++++++++++++ .../g++.target/i386/pr115961-run-1.C | 34 +++++++++++++++++++ .../riscv/rvv/base/pr115961-run-1.C | 34 +++++++++++++++++++ 3 files changed, 100 insertions(+) create mode 100644 gcc/testsuite/g++.target/i386/pr115961-run-1.C create mode 100644 gcc/testsuite/g++.target/riscv/rvv/base/pr115961-run-1.C diff --git a/gcc/internal-fn.cc b/gcc/internal-fn.cc index 95946bfd683..5c21249318e 100644 --- a/gcc/internal-fn.cc +++ b/gcc/internal-fn.cc @@ -4164,6 +4164,35 @@ direct_internal_fn_optab (internal_fn fn) gcc_unreachable (); } +/* Return true if TYPE's mode has the same format as TYPE, and if there is + a 1:1 correspondence between the values that the mode can store and the + values that the type can store. */ + +static bool +type_strictly_matches_mode_p (const_tree type) +{ + if (VECTOR_TYPE_P (type)) + return VECTOR_MODE_P (TYPE_MODE (type)); + + if (INTEGRAL_TYPE_P (type)) + return type_has_mode_precision_p (type); + + if (SCALAR_FLOAT_TYPE_P (type) || COMPLEX_FLOAT_TYPE_P (type)) + return true; + + return false; +} + +/* Return true if both the first and the second type of tree pair are + strictly matches their modes, or return false. */ + +static bool +type_pair_strictly_matches_mode_p (tree_pair type_pair) +{ + return type_strictly_matches_mode_p (type_pair.first) + && type_strictly_matches_mode_p (type_pair.second); +} + /* Return true if FN is supported for the types in TYPES when the optimization type is OPT_TYPE. The types are those associated with the "type0" and "type1" fields of FN's direct_internal_fn_info @@ -4173,6 +4202,9 @@ bool direct_internal_fn_supported_p (internal_fn fn, tree_pair types, optimization_type opt_type) { + if (!type_pair_strictly_matches_mode_p (types)) + return false; + switch (fn) { #define DEF_INTERNAL_FN(CODE, FLAGS, FNSPEC) \ diff --git a/gcc/testsuite/g++.target/i386/pr115961-run-1.C b/gcc/testsuite/g++.target/i386/pr115961-run-1.C new file mode 100644 index 00000000000..b8c8aef3b17 --- /dev/null +++ b/gcc/testsuite/g++.target/i386/pr115961-run-1.C @@ -0,0 +1,34 @@ +/* PR target/115961 */ +/* { dg-do run } */ +/* { dg-options "-O3 -fdump-rtl-expand-details" } */ + +struct e +{ + unsigned pre : 12; + unsigned a : 4; +}; + +static unsigned min_u (unsigned a, unsigned b) +{ + return (b < a) ? b : a; +} + +__attribute__((noipa)) +void bug (e * v, unsigned def, unsigned use) { + e & defE = *v; + defE.a = min_u (use + 1, 0xf); +} + +__attribute__((noipa, optimize(0))) +int main(void) +{ + e v = { 0xded, 3 }; + + bug(&v, 32, 33); + + if (v.a != 0xf) + __builtin_abort (); + + return 0; +} +/* { dg-final { scan-rtl-dump-not ".SAT_TRUNC " "expand" } } */ diff --git a/gcc/testsuite/g++.target/riscv/rvv/base/pr115961-run-1.C b/gcc/testsuite/g++.target/riscv/rvv/base/pr115961-run-1.C new file mode 100644 index 00000000000..b8c8aef3b17 --- /dev/null +++ b/gcc/testsuite/g++.target/riscv/rvv/base/pr115961-run-1.C @@ -0,0 +1,34 @@ +/* PR target/115961 */ +/* { dg-do run } */ +/* { dg-options "-O3 -fdump-rtl-expand-details" } */ + +struct e +{ + unsigned pre : 12; + unsigned a : 4; +}; + +static unsigned min_u (unsigned a, unsigned b) +{ + return (b < a) ? b : a; +} + +__attribute__((noipa)) +void bug (e * v, unsigned def, unsigned use) { + e & defE = *v; + defE.a = min_u (use + 1, 0xf); +} + +__attribute__((noipa, optimize(0))) +int main(void) +{ + e v = { 0xded, 3 }; + + bug(&v, 32, 33); + + if (v.a != 0xf) + __builtin_abort (); + + return 0; +} +/* { dg-final { scan-rtl-dump-not ".SAT_TRUNC " "expand" } } */