From patchwork Thu Jul 6 16:15:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Xi Ruoyao X-Patchwork-Id: 1804428 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org 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=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=yqgLBi2z; dkim-atps=neutral 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 (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QxhX64l7Pz20Nq for ; Fri, 7 Jul 2023 02:17:58 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6973B3857718 for ; Thu, 6 Jul 2023 16:17:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 6973B3857718 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688660276; bh=PfCNFeRXumxuIBa2aEaPS3TXmMQGT4kTDcdtEaOkHFM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=yqgLBi2zbkXwSu/+d57wU5IgmlxlSO/2GUZgQoRqY5M0D4rBHO/ZjVE9y3MGCweKV OLgBTFyhqFlR1dTc6HHTrUUsLSnjzIowaKYQrRSytNJmjCuAM+TXcoEl2ZBHBL7OA1 6L21GcTOPReCNza01XQw9mOhOrKNzK7/Pzqz3Xj8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from xry111.site (xry111.site [89.208.246.23]) by sourceware.org (Postfix) with ESMTPS id C26F93858C2D for ; Thu, 6 Jul 2023 16:17:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C26F93858C2D Received: from stargazer.. (unknown [IPv6:240e:358:110b:4200:dc73:854d:832e:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (Client did not present a certificate) (Authenticated sender: xry111@xry111.site) by xry111.site (Postfix) with ESMTPSA id 8E8F4663BE; Thu, 6 Jul 2023 12:17:30 -0400 (EDT) To: gcc-patches@gcc.gnu.org Cc: Andre Vieira , Richard Biener , Jakub Jelinek , Hongtao Liu , Xi Ruoyao Subject: [PATCH] vect: Fix vectorized BIT_FIELD_REF for signed bit-fields [PR110557] Date: Fri, 7 Jul 2023 00:15:22 +0800 Message-ID: <20230706161521.6094-2-xry111@xry111.site> X-Mailer: git-send-email 2.41.0 MIME-Version: 1.0 X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, LIKELY_SPAM_FROM, SPF_HELO_PASS, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Xi Ruoyao via Gcc-patches From: Xi Ruoyao Reply-To: Xi Ruoyao Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" If a bit-field is signed and it's wider than the output type, we must ensure the extracted result sign-extended. But this was not handled correctly. For example: int x : 8; long y : 55; bool z : 1; The vectorized extraction of y was: vect__ifc__49.29_110 = MEM [(struct Item *)vectp_a.27_108]; vect_patt_38.30_112 = vect__ifc__49.29_110 & { 9223372036854775552, 9223372036854775552 }; vect_patt_39.31_113 = vect_patt_38.30_112 >> 8; vect_patt_40.32_114 = VIEW_CONVERT_EXPR(vect_patt_39.31_113); This is obviously incorrect. This pach has implemented it as: vect__ifc__25.16_62 = MEM [(struct Item *)vectp_a.14_60]; vect_patt_31.17_63 = VIEW_CONVERT_EXPR(vect__ifc__25.16_62); vect_patt_32.18_64 = vect_patt_31.17_63 << 1; vect_patt_33.19_65 = vect_patt_32.18_64 >> 9; gcc/ChangeLog: PR tree-optimization/110557 * tree-vect-patterns.cc (vect_recog_bitfield_ref_pattern): Ensure the output sign-extended if necessary. gcc/testsuite/ChangeLog: PR tree-optimization/110557 * g++.dg/vect/pr110557.cc: New test. --- Bootstrapped and regtested on x86_64-linux-gnu. Ok for trunk and gcc-13 branch? gcc/testsuite/g++.dg/vect/pr110557.cc | 37 +++++++++++++++++ gcc/tree-vect-patterns.cc | 58 ++++++++++++++++++++------- 2 files changed, 81 insertions(+), 14 deletions(-) create mode 100644 gcc/testsuite/g++.dg/vect/pr110557.cc diff --git a/gcc/testsuite/g++.dg/vect/pr110557.cc b/gcc/testsuite/g++.dg/vect/pr110557.cc new file mode 100644 index 00000000000..e1fbe1caac4 --- /dev/null +++ b/gcc/testsuite/g++.dg/vect/pr110557.cc @@ -0,0 +1,37 @@ +// { dg-additional-options "-mavx" { target { avx_runtime } } } + +static inline long +min (long a, long b) +{ + return a < b ? a : b; +} + +struct Item +{ + int x : 8; + long y : 55; + bool z : 1; +}; + +__attribute__ ((noipa)) long +test (Item *a, int cnt) +{ + long size = 0; + for (int i = 0; i < cnt; i++) + size = min ((long)a[i].y, size); + return size; +} + +int +main () +{ + struct Item items[] = { + { 1, -1 }, + { 2, -2 }, + { 3, -3 }, + { 4, -4 }, + }; + + if (test (items, 4) != -4) + __builtin_trap (); +} diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc index 1bc36b043a0..20412c27ead 100644 --- a/gcc/tree-vect-patterns.cc +++ b/gcc/tree-vect-patterns.cc @@ -2566,7 +2566,7 @@ vect_recog_widen_sum_pattern (vec_info *vinfo, Widening with mask first, shift later: container = (type_out) container; masked = container & (((1 << bitsize) - 1) << bitpos); - result = patt2 >> masked; + result = masked >> bitpos; Widening with shift first, mask last: container = (type_out) container; @@ -2578,6 +2578,15 @@ vect_recog_widen_sum_pattern (vec_info *vinfo, result = masked >> bitpos; result = (type_out) result; + If the bitfield is signed and it's wider than type_out, we need to + keep the result sign-extended: + container = (type) container; + masked = container << (prec - bitsize - bitpos); + result = (type_out) (masked >> (prec - bitsize)); + + Here type is the signed variant of the wider of type_out and the type + of container. + The shifting is always optional depending on whether bitpos != 0. */ @@ -2636,14 +2645,22 @@ vect_recog_bitfield_ref_pattern (vec_info *vinfo, stmt_vec_info stmt_info, if (BYTES_BIG_ENDIAN) shift_n = prec - shift_n - mask_width; + bool sign_ext = (!TYPE_UNSIGNED (TREE_TYPE (bf_ref)) && + TYPE_PRECISION (ret_type) > mask_width); + bool widening = ((TYPE_PRECISION (TREE_TYPE (container)) < + TYPE_PRECISION (ret_type)) + && !useless_type_conversion_p (TREE_TYPE (container), + ret_type)); + /* We move the conversion earlier if the loaded type is smaller than the return type to enable the use of widening loads. */ - if (TYPE_PRECISION (TREE_TYPE (container)) < TYPE_PRECISION (ret_type) - && !useless_type_conversion_p (TREE_TYPE (container), ret_type)) + if (sign_ext || widening) { - pattern_stmt - = gimple_build_assign (vect_recog_temp_ssa_var (ret_type), - NOP_EXPR, container); + tree type = widening ? ret_type : container_type; + if (sign_ext) + type = gimple_signed_type (type); + pattern_stmt = gimple_build_assign (vect_recog_temp_ssa_var (type), + NOP_EXPR, container); container = gimple_get_lhs (pattern_stmt); container_type = TREE_TYPE (container); prec = tree_to_uhwi (TYPE_SIZE (container_type)); @@ -2671,7 +2688,7 @@ vect_recog_bitfield_ref_pattern (vec_info *vinfo, stmt_vec_info stmt_info, shift_first = true; tree result; - if (shift_first) + if (shift_first && !sign_ext) { tree shifted = container; if (shift_n) @@ -2694,14 +2711,27 @@ vect_recog_bitfield_ref_pattern (vec_info *vinfo, stmt_vec_info stmt_info, } else { - tree mask = wide_int_to_tree (container_type, - wi::shifted_mask (shift_n, mask_width, - false, prec)); - pattern_stmt - = gimple_build_assign (vect_recog_temp_ssa_var (container_type), - BIT_AND_EXPR, container, mask); - tree masked = gimple_assign_lhs (pattern_stmt); + tree temp = vect_recog_temp_ssa_var (container_type); + if (!sign_ext) + { + tree mask = wide_int_to_tree (container_type, + wi::shifted_mask (shift_n, + mask_width, + false, prec)); + pattern_stmt = gimple_build_assign (temp, BIT_AND_EXPR, + container, mask); + } + else + { + HOST_WIDE_INT shl = prec - shift_n - mask_width; + shift_n += shl; + pattern_stmt = gimple_build_assign (temp, LSHIFT_EXPR, + container, + build_int_cst (sizetype, + shl)); + } + tree masked = gimple_assign_lhs (pattern_stmt); append_pattern_def_seq (vinfo, stmt_info, pattern_stmt, vectype); pattern_stmt = gimple_build_assign (vect_recog_temp_ssa_var (container_type),