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 24B7D385842D for ; Wed, 24 May 2023 09:48:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 24B7D385842D 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 C6F511042; Wed, 24 May 2023 02:49:43 -0700 (PDT) Received: from localhost (e121540-lin.manchester.arm.com [10.32.110.72]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPSA id 2CD353F67D; Wed, 24 May 2023 02:48:58 -0700 (PDT) From: Richard Sandiford To: Oluwatamilore Adebayo Mail-Followup-To: Oluwatamilore Adebayo ,, , richard.sandiford@arm.com Cc: , Subject: Re: [PATCH 1/2] Missed opportunity to use [SU]ABD References: <20230523142721.182917-1-oluwatamilore.adebayo@arm.com> <20230523143412.184530-1-oluwatamilore.adebayo@arm.com> Date: Wed, 24 May 2023 10:48:56 +0100 In-Reply-To: <20230523143412.184530-1-oluwatamilore.adebayo@arm.com> (Oluwatamilore Adebayo's message of "Tue, 23 May 2023 15:34:12 +0100") 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=-28.8 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,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 List-Id: Thanks for the update. Mostly LGTM, just some minor things left below. Oluwatamilore Adebayo writes: > diff --git a/gcc/tree-vect-patterns.cc b/gcc/tree-vect-patterns.cc > index a49b09539776c0056e77f99b10365d0a8747fbc5..3a2248263cf67834a1cb41167a1783a3b6400014 100644 > --- a/gcc/tree-vect-patterns.cc > +++ b/gcc/tree-vect-patterns.cc > @@ -770,6 +770,93 @@ vect_split_statement (vec_info *vinfo, stmt_vec_info stmt2_info, tree new_rhs, > } > } > > +/* Look for the following pattern > + X = x[i] > + Y = y[i] > + DIFF = X - Y > + DAD = ABS_EXPR > + > + ABS_STMT should point to a statement of code ABS_EXPR or ABSU_EXPR. > + If REJECT_UNSIGNED is true it aborts if the type of ABS_STMT is unsigned. This line no longer applies. > + HALF_TYPE and UNPROM will be set should the statement be found to > + be a widened operation. > + DIFF_OPRNDS will be set to the two inputs of the MINUS_EXPR preceding > + ABS_STMT, otherwise it will be set the operations found by > + vect_widened_op_tree. > + */ > +static bool > +vect_recog_absolute_difference (vec_info *vinfo, gassign *abs_stmt, > + tree *half_type, > + vect_unpromoted_value unprom[2], > + tree diff_oprnds[2]) > +{ > + if (!abs_stmt) > + return false; > + > + /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi > + inside the loop (in case we are analyzing an outer-loop). */ > + enum tree_code code = gimple_assign_rhs_code (abs_stmt); > + if (code != ABS_EXPR && code != ABSU_EXPR) > + return false; > + > + tree abs_oprnd = gimple_assign_rhs1 (abs_stmt); > + tree abs_type = TREE_TYPE (abs_oprnd); > + if (!abs_oprnd) > + return false; > + if (!ANY_INTEGRAL_TYPE_P (abs_type) > + || TYPE_OVERFLOW_WRAPS (abs_type) > + || TYPE_UNSIGNED (abs_type)) > + return false; > + > + /* Peel off conversions from the ABS input. This can involve sign > + changes (e.g. from an unsigned subtraction to a signed ABS input) Nit: should just be one space after "e.g." > + or signed promotion, but it can't include unsigned promotion. > + (Note that ABS of an unsigned promotion should have been folded > + away before now anyway.) */ > + vect_unpromoted_value unprom_diff; > + abs_oprnd = vect_look_through_possible_promotion (vinfo, abs_oprnd, > + &unprom_diff); > + if (!abs_oprnd) > + return false; > + if (TYPE_PRECISION (unprom_diff.type) != TYPE_PRECISION (abs_type) > + && TYPE_UNSIGNED (unprom_diff.type) > + && TYPE_UNSIGNED (abs_type)) The last line is now redundant, since TYPE_UNSIGNED was checked above. > + return false; > + > + /* We then detect if the operand of abs_expr is defined by a minus_expr. */ > + stmt_vec_info diff_stmt_vinfo = vect_get_internal_def (vinfo, abs_oprnd); > + if (!diff_stmt_vinfo) > + return false; > + > + /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi > + inside the loop (in case we are analyzing an outer-loop). */ > + if (vect_widened_op_tree (vinfo, diff_stmt_vinfo, > + MINUS_EXPR, WIDEN_MINUS_EXPR, > + false, 2, unprom, half_type)) > + { > + if (diff_oprnds) > + { > + diff_oprnds[0] = unprom[0].op; > + diff_oprnds[1] = unprom[1].op; > + } > + return true; > + } > + > + // Failed to find a widen operation so we check for a regular MINUS_EXPR Nit: please use /* ... */ for files that currently use that style. > + gassign *diff = dyn_cast (STMT_VINFO_STMT (diff_stmt_vinfo)); > + > + if (diff_oprnds && diff > + && gimple_assign_rhs_code (diff) == MINUS_EXPR) We also need to check TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (abs_oprnd)), since this case exploits undefined behaviour for signed subtraction. > + { > + diff_oprnds[0] = gimple_assign_rhs1 (diff); > + diff_oprnds[1] = gimple_assign_rhs2 (diff); > + *half_type = NULL_TREE; > + return true; > + } > + > + return false; > +} > + > /* Convert UNPROM to TYPE and return the result, adding new statements > to STMT_INFO's pattern definition statements if no better way is > available. VECTYPE is the vector form of TYPE. > @@ -1308,40 +1395,13 @@ vect_recog_sad_pattern (vec_info *vinfo, > /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi > inside the loop (in case we are analyzing an outer-loop). */ > gassign *abs_stmt = dyn_cast (abs_stmt_vinfo->stmt); > - if (!abs_stmt > - || (gimple_assign_rhs_code (abs_stmt) != ABS_EXPR > - && gimple_assign_rhs_code (abs_stmt) != ABSU_EXPR)) > - return NULL; > - > - tree abs_oprnd = gimple_assign_rhs1 (abs_stmt); > - tree abs_type = TREE_TYPE (abs_oprnd); > - if (TYPE_UNSIGNED (abs_type)) > - return NULL; > - > - /* Peel off conversions from the ABS input. This can involve sign > - changes (e.g. from an unsigned subtraction to a signed ABS input) > - or signed promotion, but it can't include unsigned promotion. > - (Note that ABS of an unsigned promotion should have been folded > - away before now anyway.) */ > - vect_unpromoted_value unprom_diff; > - abs_oprnd = vect_look_through_possible_promotion (vinfo, abs_oprnd, > - &unprom_diff); > - if (!abs_oprnd) > - return NULL; > - if (TYPE_PRECISION (unprom_diff.type) != TYPE_PRECISION (abs_type) > - && TYPE_UNSIGNED (unprom_diff.type)) > - return NULL; > > - /* We then detect if the operand of abs_expr is defined by a minus_expr. */ > - stmt_vec_info diff_stmt_vinfo = vect_get_internal_def (vinfo, abs_oprnd); > - if (!diff_stmt_vinfo) > + vect_unpromoted_value unprom[2]; > + if (!vect_recog_absolute_difference (vinfo, abs_stmt, &half_type, > + unprom, NULL)) > return NULL; > > - /* FORNOW. Can continue analyzing the def-use chain when this stmt in a phi > - inside the loop (in case we are analyzing an outer-loop). */ > - vect_unpromoted_value unprom[2]; > - if (!vect_widened_op_tree (vinfo, diff_stmt_vinfo, MINUS_EXPR, WIDEN_MINUS_EXPR, > - false, 2, unprom, &half_type)) > + if (!half_type) > return NULL; > > vect_pattern_detected ("vect_recog_sad_pattern", last_stmt); > @@ -1363,6 +1423,114 @@ vect_recog_sad_pattern (vec_info *vinfo, > return pattern_stmt; > } > > +/* Function vect_recog_abd_pattern > + > + Try to find the following ABsolute Difference (ABD) pattern: > + > + VTYPE x, y, out; > + type diff; > + loop i in range: > + S1 diff = x[i] - y[i] > + S2 out[i] = ABS_EXPR ; > + > + where 'type' is a integer and 'VTYPE' is a vector of integers > + the same size as 'type' > + > + Input: > + > + * STMT_VINFO: The stmt from which the pattern search begins > + > + Output: > + > + * TYPE_out: The type of the output of this pattern > + > + * Return value: A new stmt that will be used to replace the sequence of > + stmts that constitute the pattern; either SABD or UABD: > + SABD_EXPR > + UABD_EXPR > + > + UABD expressions are used when the input types are > + narrower than the output types or the output type is narrower > + than 32 bits Isn't it only the first (input types narrower than the output types)? I guess the second part is referring to the fact that, in C, an operation on sub-32-bit values will happen in int and then be narrowed back down. But in gimple that's all explicit, and it's the widening to int that allows UABD to be used. (And UABD can then be used regardless of whether the result is narrowed back down.) > + */ > + > +static gimple * > +vect_recog_abd_pattern (vec_info *vinfo, > + stmt_vec_info stmt_vinfo, tree *type_out) > +{ > + /* Look for the following patterns > + X = x[i] > + Y = y[i] > + DIFF = X - Y > + DAD = ABS_EXPR > + out[i] = DAD > + > + In which > + - X, Y, DIFF, DAD all have the same type > + - x, y, out are all vectors of the same type > + */ > + > + gassign *last_stmt = dyn_cast (STMT_VINFO_STMT (stmt_vinfo)); > + if (!last_stmt) > + return NULL; > + > + tree out_type = TREE_TYPE (gimple_assign_lhs (last_stmt)); > + > + vect_unpromoted_value unprom[2]; > + tree diff_oprnds[2]; > + tree half_type; > + if (!vect_recog_absolute_difference (vinfo, last_stmt, &half_type, > + unprom, diff_oprnds)) > + return NULL; > + > +#define SAME_TYPE(A, B) (TYPE_PRECISION (A) == TYPE_PRECISION (B)) > + > + tree vectype; > + tree abd_oprnds[2]; > + if (half_type) > + { > + vectype = get_vectype_for_scalar_type (vinfo, half_type); > + if (!vectype) > + return NULL; > + > + out_type = half_type; > + vect_convert_inputs (vinfo, stmt_vinfo, 2, abd_oprnds, > + half_type, unprom, vectype); > + } > + else > + { > + unprom[0].op = diff_oprnds[0]; > + unprom[1].op = diff_oprnds[1]; > + tree signed_out = signed_type_for (out_type); > + tree signed_out_vectype = get_vectype_for_scalar_type (vinfo, signed_out); > + if (!signed_out_vectype) > + return NULL; > + > + vectype = signed_out_vectype; Might as well drop signed_out_vectype and use vectype directly. > + vect_convert_inputs (vinfo, stmt_vinfo, 2, abd_oprnds, > + signed_out, unprom, signed_out_vectype); > + > + if (!SAME_TYPE (TREE_TYPE (diff_oprnds[0]), TREE_TYPE (abd_oprnds[0]))) > + return NULL; This shouldn't be necesary. The transformation would be correct even if the subtraction is widened before the ABS, since we already checked for signed promotion, and since we're only trying SABD. Thanks, Richard > + } > + > + vect_pattern_detected ("vect_recog_abd_pattern", last_stmt); > + > + if (!vectype > + || !direct_internal_fn_supported_p (IFN_ABD, vectype, > + OPTIMIZE_FOR_SPEED)) > + return NULL; > + > + *type_out = STMT_VINFO_VECTYPE (stmt_vinfo); > + > + tree var = vect_recog_temp_ssa_var (out_type, NULL); > + gcall *abd_stmt = gimple_build_call_internal (IFN_ABD, 2, > + abd_oprnds[0], abd_oprnds[1]); > + gimple_call_set_lhs (abd_stmt, var); > + gimple_set_location (abd_stmt, gimple_location (last_stmt)); > + return abd_stmt; > +} > + > /* Recognize an operation that performs ORIG_CODE on widened inputs, > so that it can be treated as though it had the form: > > @@ -6439,6 +6607,7 @@ struct vect_recog_func > static vect_recog_func vect_vect_recog_func_ptrs[] = { > { vect_recog_bitfield_ref_pattern, "bitfield_ref" }, > { vect_recog_bit_insert_pattern, "bit_insert" }, > + { vect_recog_abd_pattern, "abd" }, > { vect_recog_over_widening_pattern, "over_widening" }, > /* Must come after over_widening, which narrows the shift as much as > possible beforehand. */