On Sat, 31 Oct 2026 at 00:12, Robin Dapp <[email protected]> wrote:
> This connects can_vec_extract and the new vec_extract_perm_p and
> replaces the vectorizer's vec_extract optab checks with it.
> That way it is mostly in sync with what we can actually expand.
> The patch passes a dummy extraction index because we don't know the
> final index yet.

But...

> A side effect of using can_vec_extract instead of a direct check is that
> we perform type punning a bit earlier now in vectorizable_store.  This
> leads to slightly different codegen in one case where we had
>
>    (set (reg:DI 123) (vec_select:DI (subreg:V2DI (reg:V4SI 121) 0) ...))
>    (set (mem:DI ...) (reg:DI 123))
>
> before but now get
>
>   (set (reg:DI 125) (vec_select:DI (subreg:V2DI (reg:V4SI 109) 0) ...))
>   (set (mem:V2SI ...) (subreg:V2SI (reg:DI 125) 0))
>
> The latter is not recognized as direct DImode store.  This is a
> scalar/vector corner case and could be fixed at the target level as well
> as by an RTL optimization.  In order not to change too much I made
> can_vec_extract's scalar punning explicit by having it return the
> extract mode.
>
> gcc/ChangeLog:
>
>         * optabs-query.cc (can_vec_extract): Query vec_extract_perm_p
>         and return extract mode.
>         * optabs-query.h (can_vec_extract): Change return type.
>         * tree-vect-loop.cc (vect_find_reusable_accumulator): Use
>         can_vec_extract.
>         (vect_create_partial_epilog): Likewise.
>         * tree-vect-stmts.cc (vectorizable_store): Likewise.
> ---
>  gcc/optabs-query.cc    | 30 +++++++++++++++++++-----------
>  gcc/optabs-query.h     |  2 +-
>  gcc/tree-vect-loop.cc  | 17 ++++++++---------
>  gcc/tree-vect-stmts.cc |  8 ++------
>  4 files changed, 30 insertions(+), 27 deletions(-)
>
> diff --git a/gcc/optabs-query.cc b/gcc/optabs-query.cc
> index da0d37cf5df..d1f513697af 100644
> --- a/gcc/optabs-query.cc
> +++ b/gcc/optabs-query.cc
> @@ -30,6 +30,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "recog.h"
>  #include "vec-perm-indices.h"
>  #include "internal-fn.h"
> +#include "expmed.h"
>  #include "memmodel.h"
>  #include "optabs.h"
>
> @@ -793,32 +794,39 @@ supports_vec_scatter_store_p (machine_mode mode)
>  }
>
>  /* Whether we can extract part of the vector mode MODE as
> -   (scalar or vector) mode EXTR_MODE.  */
> +   (scalar or vector) mode EXTR_MODE.  Returns the extraction mode
> +   we would use or no mode.  */
>
> -bool
> +opt_machine_mode
>  can_vec_extract (machine_mode mode, machine_mode extr_mode)
>  {
>    unsigned m;
>    if (!VECTOR_MODE_P (mode)
>        || !constant_multiple_p (GET_MODE_SIZE (mode),
>                                GET_MODE_SIZE (extr_mode), &m))
> -    return false;
> +    return opt_machine_mode ();
>
>    if (convert_optab_handler (vec_extract_optab, mode, extr_mode)
>        != CODE_FOR_nothing)
> -    return true;
> +    return extr_mode;
>
>    /* Besides a direct vec_extract we can also use an element extract from
>       an integer vector mode with elements of the size of the extr_mode.  */
>    scalar_int_mode imode;
>    machine_mode vmode;
> -  if (!int_mode_for_size (GET_MODE_BITSIZE (extr_mode), 0).exists (&imode)
> -      || !related_vector_mode (mode, imode, m).exists (&vmode)
> -      || (convert_optab_handler (vec_extract_optab, vmode, imode)
> -         == CODE_FOR_nothing))
> -    return false;
> -  /* We assume we can pun mode to vmode and imode to extr_mode.  */
> -  return true;
> +  if (int_mode_for_size (GET_MODE_BITSIZE (extr_mode), 0).exists (&imode)
> +      && related_vector_mode (mode, imode, m).exists (&vmode)
> +      && (convert_optab_handler (vec_extract_optab, vmode, imode)
> +         != CODE_FOR_nothing))
> +    return imode;
> +
> +  /* We don't know yet where exactly to extract from, just use
> +     the number of units.  */
> +  if (vec_extract_perm_p (mode, extr_mode, GET_MODE_NUNITS
> +                         (extr_mode)).exists ())

...why GET_MODE_NUNITS rather than 0, given that 0 should be the most
permissive value?  GET_MODE_NUNITS could rule out some valid cases
for VLA vectors, such as extracting an AArch64 AdvSIMD vector from
an SVE vector.

(No other comment on the patch from me.)


Richard

> +    return extr_mode;
> +
> +  return opt_machine_mode ();
>  }
>
>  /* OP is either neg_optab or abs_optab and FMODE is the floating-point inner
> diff --git a/gcc/optabs-query.h b/gcc/optabs-query.h
> index faa688a0609..c230ee19aac 100644
> --- a/gcc/optabs-query.h
> +++ b/gcc/optabs-query.h
> @@ -173,7 +173,7 @@ bool supports_vec_gather_load_p (machine_mode = 
> E_VOIDmode,
>  bool supports_vec_scatter_store_p (machine_mode = E_VOIDmode);
>  opt_machine_mode get_absneg_bit_mode (optab, machine_mode,
>                                       scalar_float_mode, int *);
> -bool can_vec_extract (machine_mode, machine_mode);
> +opt_machine_mode can_vec_extract (machine_mode, machine_mode);
>  bool can_open_code_p (optab, machine_mode);
>  bool can_implement_p (optab, machine_mode);
>
> diff --git a/gcc/tree-vect-loop.cc b/gcc/tree-vect-loop.cc
> index e8cff98997c..89a3671ab08 100644
> --- a/gcc/tree-vect-loop.cc
> +++ b/gcc/tree-vect-loop.cc
> @@ -5226,7 +5226,10 @@ vect_find_reusable_accumulator (loop_vec_info 
> loop_vinfo,
>           || !directly_supported_p (VECT_REDUC_INFO_CODE (reduc_info),
>                                     intermediate_vectype)
>           || !can_vec_extract (TYPE_MODE (prev_vectype),
> -                              TYPE_MODE (intermediate_vectype)))
> +                              TYPE_MODE (intermediate_vectype)).exists ()
> +         /* We could actually extract VLA vectors but bit_field_ref and
> +            friends aren't yet prepared for it.  */
> +         || !TYPE_VECTOR_SUBPARTS (intermediate_vectype).is_constant ())
>         return false;
>        prev_vectype = intermediate_vectype;
>      }
> @@ -5286,10 +5289,8 @@ vect_create_partial_epilog (tree vec_def, tree 
> vectype, code_helper code,
>          an integer mode punning.  */
>        tree dst1, dst2;
>        gimple *epilog_stmt;
> -      if (convert_optab_handler (vec_extract_optab,
> -                                TYPE_MODE (TREE_TYPE (new_temp)),
> -                                TYPE_MODE (vectype1))
> -         != CODE_FOR_nothing)
> +      if (can_vec_extract (TYPE_MODE (TREE_TYPE (new_temp)),
> +                          TYPE_MODE (vectype1)).exists ())
>         {
>           /* Extract sub-vectors directly once vec_extract becomes
>              a conversion optab.  */
> @@ -5314,10 +5315,8 @@ vect_create_partial_epilog (tree vec_def, tree 
> vectype, code_helper code,
>              vector.  */
>           tree eltype = build_nonstandard_integer_type (bitsize, 1);
>           tree etype = build_vector_type (eltype, 2);
> -         gcc_assert (convert_optab_handler (vec_extract_optab,
> -                                            TYPE_MODE (etype),
> -                                            TYPE_MODE (eltype))
> -                     != CODE_FOR_nothing);
> +         gcc_assert (can_vec_extract (TYPE_MODE (etype),
> +                                      TYPE_MODE (eltype)).exists ());
>           tree tem = make_ssa_name (etype);
>           epilog_stmt = gimple_build_assign (tem, VIEW_CONVERT_EXPR,
>                                              build1 (VIEW_CONVERT_EXPR,
> diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
> index b33d3f6b782..7a72a872e67 100644
> --- a/gcc/tree-vect-stmts.cc
> +++ b/gcc/tree-vect-stmts.cc
> @@ -8552,9 +8552,7 @@ vectorizable_store (vec_info *vinfo,
>           if (!VECTOR_MODE_P (TYPE_MODE (vectype))
>               || !related_vector_mode (TYPE_MODE (vectype), elmode,
>                                        n).exists (&vmode)
> -             || (convert_optab_handler (vec_extract_optab,
> -                                        TYPE_MODE (vectype), vmode)
> -                 == CODE_FOR_nothing)
> +             || can_vec_extract (TYPE_MODE (vectype), vmode) != vmode
>               || !(dr_align == dr_aligned
>                    || dr_align == dr_unaligned_supported))
>             {
> @@ -8572,9 +8570,7 @@ vectorizable_store (vec_info *vinfo,
>                   && VECTOR_MODE_P (TYPE_MODE (vectype))
>                   && related_vector_mode (TYPE_MODE (vectype), elmode,
>                                           lnunits).exists (&vmode)
> -                 && (convert_optab_handler (vec_extract_optab,
> -                                            vmode, elmode)
> -                     != CODE_FOR_nothing))
> +                 && can_vec_extract (vmode, elmode).exists ())
>                 {
>                   nstores = lnunits;
>                   lnel = n;
> --
> 2.56.0

Reply via email to