On Thu, Nov 5, 2026 at 2:53 PM Robin Dapp <[email protected]> wrote:
>
> From: Robin Dapp <[email protected]>
>
> This adds vect_early_break_kind_index and adjusts the early-break
> codegen so we emit something like:
>
> index = vfirst (mask)
> branch index < 0 # scalar branch
>
> which is in gimple terms
> index = IFN_COND_LEN_INDEX_FIRST_ACTIVE
> branch
>
> Now, the controversial part is that I'd like
> IFN_COND_LEN_INDEX_FIRST_ACTIVE to return -1 if the mask is empty to
> match our vfirst insn (and SVE's firstp). But that contradicts how we
> define IFN_INDEX_FIRST_ACTIVE and IFN_COND_INDEX_FIRST_ACTIVE which
> leave the result unspecified. There are good reasons for doing so, as
> Tamar explained in the other thread and I'm not doubting these.
>
> I still figured I'd send the patch as-is to discuss whether -1 just for
> the LEN variant is acceptable (I don't think so, we might want to strip
> the cond and len) and which other options there are short of inventing
> IFN_COND_LEN_INDEX_FIRST_ACTIVE2. This also affects the type (signed vs
> unsigned) which I just hard-coded to signed for the index kind for now.
I don't see how it makes sense to specify the value for an empty mask
with _LEN but not with _MASK. Can we instead add an optional value-at-zero
argument (to both) that we'd somehow validate with predicates like we
validate the BIAS for LEN?
> The rest of the changes is pretty straightforward or mechanical -
> luckily there is not much more needed.
>
> I didn't go ahead and remove the existing VCOND_MASK_LEN machinery even
> though I think we won't be needing it anymore. Once the new way is
> accepted, it's easy to just remove it entirely.
>
> gcc/ChangeLog:
>
> * doc/md.texi: Document -1.
> * tree-vect-loop.cc (vectorizable_live_operation): Handle
> index-based approach.
> (vect_update_ivs_after_vectorizer_for_early_breaks): Likewise.
> * tree-vect-stmts.cc (vectorizable_early_exit): Likewise.
> * tree-vectorizer.h (enum vect_early_break_kind): Add
> index-based.
> ---
> gcc/doc/md.texi | 3 +-
> gcc/tree-vect-loop.cc | 84 ++++++++++++++++++++++++--------
> gcc/tree-vect-stmts.cc | 107 ++++++++++++++++++++++++++++++++---------
> gcc/tree-vectorizer.h | 3 +-
> 4 files changed, 153 insertions(+), 44 deletions(-)
>
> diff --git a/gcc/doc/md.texi b/gcc/doc/md.texi
> index 385bb495c5f..1f0f942aa0d 100644
> --- a/gcc/doc/md.texi
> +++ b/gcc/doc/md.texi
> @@ -8757,7 +8757,8 @@ active elements.
> Like @samp{cond_index_first_active_@var{mode}}, but additionally ignore
> elements whose index is greater than or equal to the sum of operands 3
> and 4. Operand 3 has whichever integer mode the target prefers, and
> -operand 4 is a bias.
> +operand 4 is a bias. Unlike @samp{cond_index_first_active_@var{mode}},
> +the result is -1 if no element is selected.
>
> @mdindex cond_take_incl_active_@var{mode}
> @item @samp{cond_take_incl_active_@var{mode}}
> diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
> index fa013719ffb..ab6f8336456 100644
> --- a/gcc/tree-vect-loop.cc
> +++ b/gcc/tree-vect-loop.cc
> @@ -10536,9 +10536,21 @@ vectorizable_live_operation (vec_info *vinfo,
> stmt_vec_info stmt_info,
> && LOOP_VINFO_EARLY_BREAKS (loop_vinfo)
> && !LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo))
> {
> - if (!live_early_break_iv_p
> - && !direct_internal_fn_supported_p (IFN_EXTRACT_FIRST_ACTIVE,
> - vectype, OPTIMIZE_FOR_BOTH))
> + bool cond_len_first_active_p
> + = direct_internal_fn_supported_p (IFN_COND_LEN_INDEX_FIRST_ACTIVE,
> + truth_type_for (vectype),
> + OPTIMIZE_FOR_BOTH);
> +
> + bool can_extract_p
> + = ((cond_len_first_active_p
> + && can_vec_extract_var_idx_p (TYPE_MODE (vectype),
> + TYPE_MODE (TREE_TYPE
> (vectype))))
> + || (!cond_len_first_active_p
> + && direct_internal_fn_supported_p
> (IFN_EXTRACT_FIRST_ACTIVE,
> + vectype,
> + OPTIMIZE_FOR_BOTH)));
> +
> + if (!live_early_break_iv_p && !can_extract_p)
> {
> if (dump_enabled_p ())
> dump_printf_loc (
> @@ -10684,6 +10696,24 @@ vectorizable_live_operation (vec_info *vinfo,
> stmt_vec_info stmt_info,
> if (slot)
> {
> tree cmp_op = *slot;
> + tree cmp_type = TREE_TYPE (cmp_op);
> + gcc_assert ((LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> + == vect_early_break_kind_mask
> + && VECTOR_TYPE_P (cmp_type))
> + || (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> + == vect_early_break_kind_index
> + && INTEGRAL_TYPE_P (cmp_type)));
> +
> + bool index_based_p
> + = (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> + == vect_early_break_kind_index);
> +
> + bool mask_based_p
> + = (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> + == vect_early_break_kind_mask
> + && direct_internal_fn_supported_p
> (IFN_INDEX_FIRST_ACTIVE,
> + cmp_type,
> + OPTIMIZE_FOR_BOTH));
>
> gimple_seq stmts = NULL;
> /* If the live value is a simple vector load, reload the
> @@ -10693,9 +10723,7 @@ vectorizable_live_operation (vec_info *vinfo,
> stmt_vec_info stmt_info,
> data_reference *dr = STMT_VINFO_DATA_REF (stmt_info);
> gassign *load = NULL;
> if (single_pred_p (e->dest)
> - && direct_internal_fn_supported_p
> - (IFN_INDEX_FIRST_ACTIVE, TREE_TYPE (cmp_op),
> - OPTIMIZE_FOR_BOTH)
> + && (index_based_p || mask_based_p)
> && dr
> && TREE_CODE (tmp_vec_lhs) == SSA_NAME
> && (load = dyn_cast<gassign *>
> @@ -10716,10 +10744,13 @@ vectorizable_live_operation (vec_info *vinfo,
> stmt_vec_info stmt_info,
>
> if (reload_vuse)
> {
> - tree first = gimple_build (&stmts,
> - IFN_INDEX_FIRST_ACTIVE,
> - unsigned_intDI_type_node,
> - cmp_op);
> + tree first;
> + if (index_based_p)
> + first = cmp_op;
> + else
> + first = gimple_build
> + (&stmts, IFN_INDEX_FIRST_ACTIVE,
> + unsigned_intDI_type_node, cmp_op);
> tree offset = gimple_convert (&stmts, sizetype,
> first);
> offset
> @@ -10756,9 +10787,14 @@ vectorizable_live_operation (vec_info *vinfo,
> stmt_vec_info stmt_info,
> {
> /* Convert the extracted vector element to the scalar
> type. */
> - tree tmp = gimple_build (&stmts,
> CFN_EXTRACT_FIRST_ACTIVE,
> - TREE_TYPE (vectype),
> - cmp_op, tmp_vec_lhs);
> + tree tmp
> + = (index_based_p
> + ? gimple_build (&stmts, CFN_VEC_EXTRACT,
> + TREE_TYPE (vectype), tmp_vec_lhs,
> + cmp_op)
> + : gimple_build (&stmts, CFN_EXTRACT_FIRST_ACTIVE,
> + TREE_TYPE (vectype), cmp_op,
> + tmp_vec_lhs));
> new_tree = gimple_convert (&stmts, lhs_type, tmp);
> }
>
> @@ -11590,7 +11626,11 @@ vect_update_ivs_after_vectorizer_for_early_breaks
> (loop_vec_info loop_vinfo)
> = LOOP_VINFO_EARLY_BRK_DEPS (loop_vinfo)->get (gimple_bb (gcond));
> gcc_assert (slot && TYPE_P (*slot));
>
> - tree cmp_op = make_temp_ssa_name (*slot, NULL, "vexit");
> + tree cmp_type = *slot;
> + if (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> + == vect_early_break_kind_index)
> + cmp_type = intDI_type_node;
> + tree cmp_op = make_temp_ssa_name (cmp_type, NULL, "vexit");
> gcall *varying = gimple_build_call_internal (IFN_VARYING, 0);
> gimple_call_set_lhs (varying, cmp_op);
>
> @@ -11709,8 +11749,10 @@ vect_update_ivs_after_vectorizer_for_early_breaks
> (loop_vec_info loop_vinfo)
> operation or a partition-and-count sequence. */
> internal_fn active_fn = IFN_INDEX_FIRST_ACTIVE;
> tree active_mask = cmp_op;
> - if (!direct_internal_fn_supported_p (active_fn, mask_type,
> - OPTIMIZE_FOR_BOTH))
> + if (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> + == vect_early_break_kind_mask
> + && !direct_internal_fn_supported_p (active_fn, mask_type,
> + OPTIMIZE_FOR_BOTH))
> {
> active_mask = make_temp_ssa_name (mask_type, NULL, "pred");
> gcall *p_call
> @@ -11793,9 +11835,13 @@ vect_update_ivs_after_vectorizer_for_early_breaks
> (loop_vec_info loop_vinfo)
> (gimple *) phi);
>
> tree res = PHI_RESULT (phi1);
> - tree reduc_iv = gimple_build (&stmts, active_fn,
> - unsigned_intDI_type_node,
> - active_mask);
> + tree reduc_iv
> + = (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> + == vect_early_break_kind_index
> + ? gimple_convert (&stmts, unsigned_intDI_type_node,
> + active_mask)
> + : gimple_build (&stmts, active_fn,
> unsigned_intDI_type_node,
> + active_mask));
>
> tree stype = TREE_TYPE (step_expr);
> /* The offset can overflow even when every scalar IV value is
> diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
> index fd14ec66fa7..0626f6d12f4 100644
> --- a/gcc/tree-vect-stmts.cc
> +++ b/gcc/tree-vect-stmts.cc
> @@ -12933,16 +12933,44 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
> stmt_vec_info stmt_info,
> /* Analyze only. */
> if (cost_vec)
> {
> - if (addhn_supported_p
> - || supports_vector_compare_and_branch (loop_vinfo, mode))
> - LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo) = vect_early_break_kind_mask;
> - else
> + tree mask_vectype = (VECTOR_BOOLEAN_TYPE_P (vectype)
> + ? vectype : truth_type_for (vectype));
> +
> + bool mask_based_p
> + = (addhn_supported_p
> + || supports_vector_compare_and_branch (loop_vinfo, mode));
> + bool index_based_p
> + = (vec_num == 1
> + && direct_internal_fn_supported_p (IFN_COND_LEN_INDEX_FIRST_ACTIVE,
> + mask_vectype,
> OPTIMIZE_FOR_BOTH));
> +
> + gcc_checking_assert (!(mask_based_p && index_based_p));
> +
> + vect_early_break_kind early_break_kind
> + = (mask_based_p ? vect_early_break_kind_mask
> + : index_based_p ? vect_early_break_kind_index
> + : vect_early_break_kind_none);
> +
> + if (early_break_kind == vect_early_break_kind_none)
> {
> if (dump_enabled_p ())
> - dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
> - "can't vectorize early exit because the "
> - "target doesn't support flag setting vector "
> - "comparisons.\n");
> + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
> + "can't vectorize early exit because the "
> + "target supports neither a vector compare and "
> + "branch nor a length-controlled reduction of the
> "
> + "exit mask to a scalar.\n");
> + return false;
> + }
> +
> + if (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> + == vect_early_break_kind_none)
> + LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo) = early_break_kind;
> + else if (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo) != early_break_kind)
> + {
> + if (dump_enabled_p ())
> + dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
> + "can't vectorize early exit because we "
> + "cannot mix early-break types.\n");
> return false;
> }
>
> @@ -12953,7 +12981,12 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
> stmt_vec_info stmt_info,
> return false;
> gcc_assert (comparison_vectype);
>
> - if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
> + if (early_break_kind == vect_early_break_kind_index)
> + {
> + if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
> + vect_record_loop_len (loop_vinfo, lens, vec_num, vectype, 1);
> + }
> + else if (LOOP_VINFO_CAN_USE_PARTIAL_VECTORS_P (loop_vinfo))
> {
> if (direct_internal_fn_supported_p (IFN_VCOND_MASK_LEN, vectype,
> OPTIMIZE_FOR_SPEED))
> @@ -12965,9 +12998,6 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
> stmt_vec_info stmt_info,
> if (!vect_compute_type_for_early_break_scalar_iv (loop_vinfo))
> return false;
>
> - tree mask_vectype = (VECTOR_BOOLEAN_TYPE_P (vectype)
> - ? vectype : truth_type_for (vectype));
> -
> if (!LOOP_VINFO_EARLY_BREAKS_LIVE_IVS (loop_vinfo).is_empty ()
> && !LOOP_VINFO_EARLY_BRK_NEEDS_EPILOG (loop_vinfo))
> {
> @@ -12977,15 +13007,20 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
> stmt_vec_info stmt_info,
> provide a predicate for the lanes before the first active lane
> and
> then count those lanes. This lets the vector loop update a live
> IV
> without replaying the iteration in a scalar epilogue. */
> - if (!direct_internal_fn_supported_p (IFN_INDEX_FIRST_ACTIVE,
> - mask_vectype,
> - OPTIMIZE_FOR_BOTH)
> - && (!direct_internal_fn_supported_p (IFN_TAKE_EXCL_ACTIVE,
> + bool mask_p
> + = (early_break_kind == vect_early_break_kind_mask
> + && (direct_internal_fn_supported_p (IFN_INDEX_FIRST_ACTIVE,
> mask_vectype,
> OPTIMIZE_FOR_BOTH)
> - || !direct_internal_fn_supported_p (IFN_COUNT_ACTIVE,
> - mask_vectype,
> - OPTIMIZE_FOR_BOTH)))
> + || (direct_internal_fn_supported_p (IFN_TAKE_EXCL_ACTIVE,
> + mask_vectype,
> + OPTIMIZE_FOR_BOTH)
> + && direct_internal_fn_supported_p (IFN_COUNT_ACTIVE,
> + mask_vectype,
> +
> OPTIMIZE_FOR_BOTH))));
> + bool index_p = early_break_kind == vect_early_break_kind_index;
> +
> + if (!mask_p && !index_p)
> {
> if (dump_enabled_p ())
> dump_printf_loc (
> @@ -13075,8 +13110,33 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
> stmt_vec_info stmt_info,
> EDGE_SUCC (bb, 1)->flags ^= (EDGE_TRUE_VALUE|EDGE_FALSE_VALUE);
> }
>
> + /* The index-based approach doesn't use vector masks but uses a scalar
> + branch determined by the "mismatch" index. */
> + bool index_based_p
> + = (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo) ==
> vect_early_break_kind_index);
> + if (index_based_p)
> + {
> + gcc_checking_assert (stmts.length () == 1);
> + tree len;
> + if (len_loop_p)
> + len = vect_get_loop_len (loop_vinfo, gsi, lens, 1, vectype, 0, 1,
> + true);
> + else
> + len = size_int (TYPE_VECTOR_SUBPARTS (vectype));
> +
> + tree bias = build_int_cst (intQI_type_node,
> + LOOP_VINFO_PARTIAL_LOAD_STORE_BIAS
> (loop_vinfo));
> + new_temp = make_temp_ssa_name (intDI_type_node, NULL, "vexit_idx");
> + new_stmt
> + = gimple_build_call_internal (IFN_COND_LEN_INDEX_FIRST_ACTIVE, 4,
> + build_all_ones_cst (TREE_TYPE
> (stmts[0])),
> + stmts[0], len, bias);
> + gimple_call_set_lhs (new_stmt, new_temp);
> + vect_finish_stmt_generation (loop_vinfo, stmt_info, new_stmt,
> + &cond_gsi);
> + }
> /* Determine if we need to reduce the final value. */
> - if (stmts.length () > 1)
> + else if (stmts.length () > 1)
> {
> /* We build the reductions in a way to maintain as much parallelism as
> possible. */
> @@ -13168,9 +13228,10 @@ vectorizable_early_exit (loop_vec_info loop_vinfo,
> stmt_vec_info stmt_info,
> }
>
> gcc_checking_assert (LOOP_VINFO_EARLY_BRK_KIND (loop_vinfo)
> - == vect_early_break_kind_mask);
> - tree cst = build_zero_cst (vectype_out);
> - gimple_cond_set_condition (cond_stmt, NE_EXPR, new_temp, cst);
> + != vect_early_break_kind_none);
> + tree cst = build_zero_cst (TREE_TYPE (new_temp));
> + gimple_cond_set_condition (cond_stmt, index_based_p ? GE_EXPR : NE_EXPR,
> + new_temp, cst);
> update_stmt (orig_stmt);
>
> /* ??? */
> diff --git a/gcc/tree-vectorizer.h b/gcc/tree-vectorizer.h
> index 13fc0d1431a..c9c99c6e51c 100644
> --- a/gcc/tree-vectorizer.h
> +++ b/gcc/tree-vectorizer.h
> @@ -516,7 +516,8 @@ enum vect_partial_vector_style {
>
> enum vect_early_break_kind {
> vect_early_break_kind_none,
> - vect_early_break_kind_mask
> + vect_early_break_kind_mask,
> + vect_early_break_kind_index,
> };
>
> /* Key for map that records association between
> --
> 2.56.0
>