From patchwork Thu Aug 24 02:08:36 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "juzhe.zhong@rivai.ai" X-Patchwork-Id: 136738 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a7d1:0:b0:3f2:4152:657d with SMTP id p17csp838301vqm; Wed, 23 Aug 2023 19:09:49 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFpp+6ti3giwJjM+0HlE81PegsKPQM1ehZS4N72X1l6P6WxZCY5eUkCI7rRJP0i9jXoDCzt X-Received: by 2002:a17:906:535d:b0:98e:f13:e908 with SMTP id j29-20020a170906535d00b0098e0f13e908mr11975387ejo.60.1692842989014; Wed, 23 Aug 2023 19:09:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1692842989; cv=none; d=google.com; s=arc-20160816; b=wWnL5YCQ3yhPMRBpLUgXBWHHLeGhLJgu+5/eC7e6hXdRUzPsT+WsNgRzadfJBZ+SrH 6hLJc7l1xnRbBrlRDtgZTQApO5GqfuPRbBcVVpRSfhjxNN4InGQTYfRVFcQ4kTTtitj1 oc16vSjNskz529uwuBNn8N1YWVNFO6dxzuNxLRLS49ETJkrYaHdIq2KzMopx3InZjP1q FwF4VnmAiwopspgQyGWc8RTH362WtTHML/AxNNt25Zt50NUfSPfEqu30FrjokaqnYKq4 hdUIM0sqFdACTGmCHvcAbkO+8jlG6OxKVk/PZFEl13Ok65kuKKoK7NPQmcYd4cKsJqd+ ci+g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:feedback-id :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:dmarc-filter:delivered-to; bh=/jNFZE7ewx8IC4/nd23TCQUW0r6wfqGZkYswbsc/Wq4=; fh=bFGt8bvBdss4RDVNwtoG4vql1cbNszblsxOGwrQqCdA=; b=bHcWlrdT4iSwIzZLoz9tQqPeUJUr+nnpbNm3lehaGuFYhhMMNWOveeorMlerdh24Id +BQAEp3EgHPmvDn88RT8McSuexL7c6ip7RN4V1eIBlT7Ow0t5yZyNjQyKvtzQMfsmW62 3bb+lCanZWkdeyAcBaZG93ST0CSxKhGMJiwxg0OgIOnMsRSKYR3MpIeuh+pCvm4WLHP0 0eEXekrSQdmmm2D77T4CzCPr6+BnQTtDA6sUWvSHPxJa7Nrd+z09r4xJRoYub5Pw4RW1 59epyGbcVPCXGcp3IGhwykxYEVkPE56EqQENjFqXt/FpoRgdK23hpFQmMxIYjBOskfDU BCPQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id s25-20020a1709066c9900b0099ca94b4d01si10251673ejr.705.2023.08.23.19.09.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 23 Aug 2023 19:09:49 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5757C385C6ED for ; Thu, 24 Aug 2023 02:09:41 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from smtpbgsg2.qq.com (smtpbgsg2.qq.com [54.254.200.128]) by sourceware.org (Postfix) with ESMTPS id C9E31385773F for ; Thu, 24 Aug 2023 02:09:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C9E31385773F Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=rivai.ai Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=rivai.ai X-QQ-mid: bizesmtp71t1692842919teujcwev Received: from rios-cad122.hadoop.rioslab.org ( [58.60.1.26]) by bizesmtp.qq.com (ESMTP) with id ; Thu, 24 Aug 2023 10:08:38 +0800 (CST) X-QQ-SSF: 01400000000000G0V000000A0000000 X-QQ-FEAT: XBN7tc9DADJ24Np9ChdsRDAYxyAwOQ9eRA6aa2/PSGkNeOAcEKMq9a4Pk/Mdz nsZ/VtYofWjttwSHBsZZfeLN397yiv2VNB3Lxcq/0cGbiIW71wJrdtavymIm44pdZg4zgkv 61iXMaHMHz/8YFsuzBIml2BaDGOgjouA39xKIoDrkNlNKzUsFiTgRuG4e7HIwscJjF9gyUY daaN5RhrfLuRmKHi+CQgan5wxOy+coNPcQn99SvzoZKgRkGlysNvx8NM6w8qBlH9wVQJesp TrQIfgPzlYMvOYW9RxcAT0/XEIQ7H2rlCFUulR7/w6Y7ykJqLcANRCzYnWrjqaVtQnUOyst zQ7Fjq0Ags80noZWnpm6lcE1a/Q4wrGZhQK+a8qi/UINB/GVsc= X-QQ-GoodBg: 2 X-BIZMAIL-ID: 18372486429281306525 From: Juzhe-Zhong To: gcc-patches@gcc.gnu.org Cc: richard.sandiford@arm.com, rguenther@suse.de, Juzhe-Zhong Subject: [PATCH] VECT: Apply LEN_FOLD_EXTRACT_LAST into loop vectorizer Date: Thu, 24 Aug 2023 10:08:36 +0800 Message-Id: <20230824020836.48335-1-juzhe.zhong@rivai.ai> X-Mailer: git-send-email 2.36.3 MIME-Version: 1.0 X-QQ-SENDSIZE: 520 Feedback-ID: bizesmtp:rivai.ai:qybglogicsvrgz:qybglogicsvrgz7a-one-0 X-Spam-Status: No, score=-10.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_PASS, SPF_PASS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1775074529686705747 X-GMAIL-MSGID: 1775074529686705747 Hi. This patch is apply LEN_FOLD_EXTRACT_LAST into loop vectorizer. Consider this following case: #include #define N 32 /* Simple condition reduction. */ int __attribute__ ((noinline, noclone)) condition_reduction (int *a, int min_v) { int last = 66; /* High start value. */ for (int i = 0; i < N; i++) if (a[i] < min_v) last = i; return last; } With this patch, we can generate this following IR: _44 = .SELECT_VL (ivtmp_42, POLY_INT_CST [4, 4]); _34 = vect_vec_iv_.5_33 + { POLY_INT_CST [4, 4], ... }; ivtmp_36 = _44 * 4; vect__4.8_39 = .MASK_LEN_LOAD (vectp_a.6_37, 32B, { -1, ... }, _44, 0); mask__11.9_41 = vect__4.8_39 < vect_cst__40; last_5 = .LEN_FOLD_EXTRACT_LAST (last_14, mask__11.9_41, vect_vec_iv_.5_33, _44, 0); ... gcc/ChangeLog: * tree-vect-loop.cc (vectorizable_reduction): Apply LEN_FOLD_EXTRACT_LAST. * tree-vect-stmts.cc (vectorizable_condition): Ditto. --- gcc/tree-vect-loop.cc | 7 ++++-- gcc/tree-vect-stmts.cc | 52 ++++++++++++++++++++++++++++++++++++------ 2 files changed, 50 insertions(+), 9 deletions(-) diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc index 1cd6c291377..ebee8037e02 100644 --- a/gcc/tree-vect-loop.cc +++ b/gcc/tree-vect-loop.cc @@ -7494,8 +7494,11 @@ vectorizable_reduction (loop_vec_info loop_vinfo, } if (reduc_chain_length == 1 - && direct_internal_fn_supported_p (IFN_FOLD_EXTRACT_LAST, - vectype_in, OPTIMIZE_FOR_SPEED)) + && (direct_internal_fn_supported_p (IFN_FOLD_EXTRACT_LAST, vectype_in, + OPTIMIZE_FOR_SPEED) + || direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST, + vectype_in, + OPTIMIZE_FOR_SPEED))) { if (dump_enabled_p ()) dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location, diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc index 413a88750d6..be9f3a280bd 100644 --- a/gcc/tree-vect-stmts.cc +++ b/gcc/tree-vect-stmts.cc @@ -11740,8 +11740,17 @@ vectorizable_condition (vec_info *vinfo, && LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo)) { if (reduction_type == EXTRACT_LAST_REDUCTION) - vect_record_loop_mask (loop_vinfo, &LOOP_VINFO_MASKS (loop_vinfo), - ncopies * vec_num, vectype, NULL); + { + if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST, + vectype, OPTIMIZE_FOR_SPEED)) + vect_record_loop_len (loop_vinfo, + &LOOP_VINFO_LENS (loop_vinfo), + ncopies * vec_num, vectype, 1); + else + vect_record_loop_mask (loop_vinfo, + &LOOP_VINFO_MASKS (loop_vinfo), + ncopies * vec_num, vectype, NULL); + } /* Extra inactive lanes should be safe for vect_nested_cycle. */ else if (STMT_VINFO_DEF_TYPE (reduc_info) != vect_nested_cycle) { @@ -11772,7 +11781,13 @@ vectorizable_condition (vec_info *vinfo, mask to the condition, or to its inverse. */ vec_loop_masks *masks = NULL; - if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)) + vec_loop_lens *lens = NULL; + if (loop_vinfo && LOOP_VINFO_FULLY_WITH_LENGTH_P (loop_vinfo)) + { + if (reduction_type == EXTRACT_LAST_REDUCTION) + lens = &LOOP_VINFO_LENS (loop_vinfo); + } + else if (loop_vinfo && LOOP_VINFO_FULLY_MASKED_P (loop_vinfo)) { if (reduction_type == EXTRACT_LAST_REDUCTION) masks = &LOOP_VINFO_MASKS (loop_vinfo); @@ -11910,7 +11925,8 @@ vectorizable_condition (vec_info *vinfo, /* Force vec_compare to be an SSA_NAME rather than a comparison, in cases where that's necessary. */ - if (masks || reduction_type == EXTRACT_LAST_REDUCTION) + tree len = NULL_TREE, bias = NULL_TREE; + if (masks || lens || reduction_type == EXTRACT_LAST_REDUCTION) { if (!is_gimple_val (vec_compare)) { @@ -11931,6 +11947,23 @@ vectorizable_condition (vec_info *vinfo, vec_compare = vec_compare_name; } + if (direct_internal_fn_supported_p (IFN_LEN_FOLD_EXTRACT_LAST, + vectype, OPTIMIZE_FOR_SPEED)) + { + if (lens) + { + len = vect_get_loop_len (loop_vinfo, gsi, lens, + vec_num * ncopies, vectype, i, 1); + signed char biasval + = LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS (loop_vinfo); + bias = build_int_cst (intQI_type_node, biasval); + } + else + { + len = size_int (TYPE_VECTOR_SUBPARTS (vectype)); + bias = build_int_cst (intQI_type_node, 0); + } + } if (masks) { tree loop_mask @@ -11950,9 +11983,14 @@ vectorizable_condition (vec_info *vinfo, { gimple *old_stmt = vect_orig_stmt (stmt_info)->stmt; tree lhs = gimple_get_lhs (old_stmt); - new_stmt = gimple_build_call_internal - (IFN_FOLD_EXTRACT_LAST, 3, else_clause, vec_compare, - vec_then_clause); + if (len) + new_stmt = gimple_build_call_internal + (IFN_LEN_FOLD_EXTRACT_LAST, 5, else_clause, vec_compare, + vec_then_clause, len, bias); + else + new_stmt = gimple_build_call_internal + (IFN_FOLD_EXTRACT_LAST, 3, else_clause, vec_compare, + vec_then_clause); gimple_call_set_lhs (new_stmt, lhs); SSA_NAME_DEF_STMT (lhs) = new_stmt; if (old_stmt == gsi_stmt (*gsi))