From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by sourceware.org (Postfix) with ESMTP id C10713858C5E for ; Thu, 12 Oct 2023 13:53:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C10713858C5E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=arm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=arm.com Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id 22BDB13D5; Thu, 12 Oct 2023 06:54:29 -0700 (PDT) Received: from localhost (unknown [10.32.110.65]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id E1EB43F5A1; Thu, 12 Oct 2023 06:53:47 -0700 (PDT) From: Richard Sandiford To: Robin Dapp via Gcc-patches Mail-Followup-To: Robin Dapp via Gcc-patches ,Robin Dapp , richard.sandiford@arm.com Cc: Robin Dapp Subject: Re: [PATCH] gimple-match: Do not try UNCOND optimization with COND_LEN. References: <4b77e155-0936-67d6-ab2d-ae7ef49bfde0@gmail.com> <4afb967d-96ea-7e74-1a35-c86aa5a5ffa6@gmail.com> Date: Thu, 12 Oct 2023 14:53:46 +0100 In-Reply-To: <4afb967d-96ea-7e74-1a35-c86aa5a5ffa6@gmail.com> (Robin Dapp via Gcc-patches's message of "Mon, 11 Sep 2023 22:35:19 +0200") Message-ID: User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Status: No, score=-24.2 required=5.0 tests=BAYES_00,GIT_PATCH_0,KAM_DMARC_NONE,KAM_DMARC_STATUS,KAM_LAZY_DOMAIN_SECURITY,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 List-Id: Robin Dapp via Gcc-patches writes: > Hi, > > as Juzhe noticed in gcc.dg/pr92301.c there was still something missing in > the last patch. The attached v2 makes sure we always have a COND_LEN operation > before returning true and initializes len and bias even if they are unused. > > Bootstrapped and regtested on aarch64 and x86. Sorry for the slow review. I was hoping Richi would take it, but I see he was hoping the same from me. > Regards > Robin > > Subject: [PATCH v2] gimple-match: Do not try UNCOND optimization with > COND_LEN. > > On riscv we mis-optimize conditional (length) operations into > unconditional operations e.g. in slp-reduc-7.c and > gcc.dg/pr92301.c. > > This patch prevents optimizing e.g. > COND_LEN_ADD ({-1, ... }, a, 0, c, len, bias) > unconditionally into just "a". > > Currently, we assume that COND_LEN operations can be optimized similarly > to COND operations. As the length is part of the mask (and usually not > compile-time constant), we must not perform any optimization that relies > on just the mask being "true". This patch ensures that we still have a > COND_LEN pattern after optimization. > > gcc/ChangeLog: > > PR target/111311 > * gimple-match-exports.cc (maybe_resimplify_conditional_op): > Check for length masking. > (try_conditional_simplification): Check that the result is still > length masked. > --- > gcc/gimple-match-exports.cc | 38 ++++++++++++++++++++++++++++++------- > gcc/gimple-match.h | 3 ++- > 2 files changed, 33 insertions(+), 8 deletions(-) > > diff --git a/gcc/gimple-match-exports.cc b/gcc/gimple-match-exports.cc > index b36027b0bad..d41de98a3d3 100644 > --- a/gcc/gimple-match-exports.cc > +++ b/gcc/gimple-match-exports.cc > @@ -262,7 +262,8 @@ maybe_resimplify_conditional_op (gimple_seq *seq, gimple_match_op *res_op, > if (!res_op->cond.cond) > return false; > > - if (!res_op->cond.else_value > + if (!res_op->cond.len > + && !res_op->cond.else_value > && res_op->code.is_tree_code ()) > { > /* The "else" value doesn't matter. If the "then" value is a Why are the contents of this if statement wrong for COND_LEN? If the "else" value doesn't matter, then the masked form can use the "then" value for all elements. I would have expected the same thing to be true of COND_LEN. > @@ -301,9 +302,12 @@ maybe_resimplify_conditional_op (gimple_seq *seq, gimple_match_op *res_op, > > /* If the "then" value is a gimple value and the "else" value matters, > create a VEC_COND_EXPR between them, then see if it can be further > - simplified. */ > + simplified. > + Don't do this if we have a COND_LEN_ as that would make us lose the > + length masking. */ > gimple_match_op new_op; > - if (res_op->cond.else_value > + if (!res_op->cond.len > + && res_op->cond.else_value > && VECTOR_TYPE_P (res_op->type) > && gimple_simplified_result_is_gimple_val (res_op)) > { The change LGTM, but it would be nice to phrase the comment to avoid the "Do A. Don't do A if B" pattern. Maybe: /* If the condition represents MASK ? THEN : ELSE, where THEN is a gimple value and ELSE matters, create a VEC_COND_EXPR between them, then see if it can be further simplified. */ > @@ -314,7 +318,7 @@ maybe_resimplify_conditional_op (gimple_seq *seq, gimple_match_op *res_op, > return gimple_resimplify3 (seq, res_op, valueize); > } > > - /* Otherwise try rewriting the operation as an IFN_COND_* call. > + /* Otherwise try rewriting the operation as an IFN_COND_(LEN_)* call. > Again, this isn't a simplification in itself, since it's what > RES_OP already described. */ > if (convert_conditional_op (res_op, &new_op)) > @@ -386,9 +390,29 @@ try_conditional_simplification (internal_fn ifn, gimple_match_op *res_op, > default: > gcc_unreachable (); > } > - *res_op = cond_op; > - maybe_resimplify_conditional_op (seq, res_op, valueize); > - return true; > + > + if (len) > + { > + /* If we had a COND_LEN before we need to ensure that it stays that > + way. */ > + gimple_match_op old_op = *res_op; > + *res_op = cond_op; > + maybe_resimplify_conditional_op (seq, res_op, valueize); > + > + auto cfn = combined_fn (res_op->code); > + if (internal_fn_p (cfn) > + && internal_fn_len_index (as_internal_fn (cfn)) != -1) > + return true; Why isn't it enough to check the result of maybe_resimplify_conditional_op? Thanks, Richard > + > + *res_op = old_op; > + return false; > + } > + else > + { > + *res_op = cond_op; > + maybe_resimplify_conditional_op (seq, res_op, valueize); > + return true; > + } > } > > /* Helper for the autogenerated code, valueize OP. */ > diff --git a/gcc/gimple-match.h b/gcc/gimple-match.h > index bec3ff42e3e..d192b7dae3e 100644 > --- a/gcc/gimple-match.h > +++ b/gcc/gimple-match.h > @@ -56,7 +56,8 @@ public: > > inline > gimple_match_cond::gimple_match_cond (tree cond_in, tree else_value_in) > - : cond (cond_in), else_value (else_value_in) > + : cond (cond_in), else_value (else_value_in), len (NULL_TREE), > + bias (NULL_TREE) > { > }