On Sat, 31 Oct 2026 at 00:12, Robin Dapp <[email protected]> wrote:
> This patch adds a way of extracting vectors from vectors by building a
> consecutive permute pattern for the target to handle.
I think this is ultimately what we should do for all vector-from-vector
extracts, and never use direct optabs. Maybe that's the plan. I realise
it would be a bit of work though.
I agree that this patch can go in on its own though. Some comments below.
>
> gcc/ChangeLog:
>
> * expmed.cc (vec_extract_perm_p): New function.
I think this belongs in optabs-query.cc.
> (extract_vector_from_vector_perm): New function.
> (extract_vector_from_vector): Allow vector extraction via
> permute.
> * expmed.h (vec_extract_perm_p): Declare.
> ---
> gcc/expmed.cc | 105 +++++++++++++++++++++++++++++++++++++++++++++++++-
> gcc/expmed.h | 3 ++
> 2 files changed, 107 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/expmed.cc b/gcc/expmed.cc
> index 367e8af46e5..fcf100df90d 100644
> --- a/gcc/expmed.cc
> +++ b/gcc/expmed.cc
> @@ -747,6 +747,48 @@ store_bit_field_using_insv (const extraction_insn *insv,
> rtx op0,
> return false;
> }
>
> +/* See if we can extract a vector of mode SUBVEC_MODE from a larger vector
> + of mode VEC_MODE at EXTRACT_IDX. EXTRACT_IDX counts in elements
> + of VEC_MOD and SUBVEC_MODE. If we can, return the possibly punned
VEC_MODE
> + input mode. */
> +
> +opt_machine_mode
> +vec_extract_perm_p (machine_mode vec_mode, machine_mode subvec_mode,
> + poly_uint64 extract_idx)
> +{
> + if (!VECTOR_MODE_P (vec_mode) || !VECTOR_MODE_P (subvec_mode))
> + return opt_machine_mode ();
> +
> + if (maybe_ne (GET_MODE_UNIT_SIZE (vec_mode),
> + GET_MODE_UNIT_SIZE (subvec_mode)))
This can just be !=.
> + return opt_machine_mode ();
> +
> + if (maybe_gt (GET_MODE_NUNITS (subvec_mode) + extract_idx,
> + GET_MODE_NUNITS (vec_mode)))
> + return opt_machine_mode ();
> +
> + /* Look for a matching mode for the vector elements.
> + Do not pun the whole vector with a mode of different element size
> + here (e.g. V2DI and V4SI). We expect a target to do that itself. */
> + if (GET_MODE_INNER (vec_mode) != GET_MODE_INNER (subvec_mode)
> + && !related_vector_mode (vec_mode, GET_MODE_INNER (subvec_mode),
> + GET_MODE_NUNITS (vec_mode)).exists
> + (&vec_mode))
> + return opt_machine_mode ();
> +
> + poly_uint64 out_nunits = GET_MODE_NUNITS (subvec_mode);
> + poly_uint64 in_nunits = GET_MODE_NUNITS (vec_mode);
> +
> + vec_perm_builder sel (out_nunits, 1, 3);
> + for (unsigned int i = 0; i < 3; ++i)
> + sel.quick_push (extract_idx + i);
> +
> + vec_perm_indices indices (sel, 2, in_nunits);
Why is this 2-input rather than 1-input?
> + if (can_vec_perm_const_p (subvec_mode, vec_mode, indices))
> + return vec_mode;
> + return opt_machine_mode ();
> +}
> +
> /* A subroutine of store_bit_field, with the same arguments. Return true
> if the operation could be implemented.
>
> @@ -1698,9 +1740,65 @@ extract_bit_field_as_subreg (machine_mode mode, rtx
> op0,
> return NULL_RTX;
> }
>
> +/* Same as extract_vector_from_vector but try with a permutation. */
> +
> +static rtx
> +extract_vector_from_vector_perm (rtx vec, rtx target, machine_mode mode,
> + machine_mode subvec_mode, poly_uint64
> bitsize,
> + poly_uint64 bitnum, rtx *alt_rtl)
> +{
> + machine_mode vec_mode = GET_MODE (vec);
> + poly_uint64 extract_idx;
> + if (!VECTOR_MODE_P (vec_mode)
> + || MEM_P (vec)
> + || !VECTOR_MODE_P (subvec_mode)
> + || maybe_ne (bitsize, GET_MODE_PRECISION (subvec_mode))
> + || known_le (GET_MODE_SIZE (GET_MODE (vec)),
> + GET_MODE_SIZE (subvec_mode))
known_le doesn't seem right. Extracting a 128-bit vector from a
possibly 128-bit, possibly larger vector should be ok.
But this kind of check is redundant wrt what vec_extract_perm_p checks,
so I think we should just drop it rather than change it.
> + || !multiple_p (bitnum, GET_MODE_UNIT_BITSIZE (subvec_mode),
> + &extract_idx))
> + return NULL_RTX;
> +
> + /* We don't want to handle single-element vector modes here. */
Why not? I think the comment should say, rather than leave the reader
to guess :)
> + if (known_le (GET_MODE_NUNITS (subvec_mode), 1)
> + || known_le (GET_MODE_NUNITS (vec_mode), 1))
> + return NULL_RTX;
> +
> + machine_mode punned_vec_mode;
> + if (!vec_extract_perm_p (vec_mode, subvec_mode, extract_idx).exists
> + (&punned_vec_mode))
> + return NULL_RTX;
> +
> + if (punned_vec_mode != vec_mode)
> + vec = gen_lowpart (punned_vec_mode, vec);
> +
> + poly_uint64 out_nunits = GET_MODE_NUNITS (subvec_mode);
> +
> + vec_perm_builder sel (out_nunits, 1, 3);
> + for (unsigned int i = 0; i < 3; ++i)
> + sel.quick_push (extract_idx + i);
> +
> + rtx tmp = subvec_mode == mode ? target : NULL_RTX;
> + rtx res = expand_vec_perm_const (subvec_mode, vec, vec, sel, BLKmode, tmp);
> + if (!res)
> + return NULL_RTX;
> +
> + if (res == target && alt_rtl)
> + *alt_rtl = target;
> +
> + if (subvec_mode != mode)
> + res = gen_lowpart (mode, res);
> +
> + return res;
> +}
> +
> /* Try to extract a subvector of mode SUBVEC_MODE (which has BITSIZE bits)
> from vector VEC, starting at bit BITNUM, then extract the MODE lowpart of
> - the subvector. Return the result on success or NULL_RTX on failure. */
> + the subvector.
> + If the vec_extract-based attempt failed, build a vector permute to extract
> + the part and hand it to the target.
I think the new sentence should either flow into the existing paragraph
or be a new paragraph.
OK with the above changes if changing to a single-input permute is correct.
Thanks,
Richard
> +
> + Return the result on success or NULL_RTX on failure. */
>
> static rtx
> extract_vector_from_vector (rtx vec, rtx target, machine_mode mode,
> @@ -1755,6 +1853,11 @@ extract_vector_from_vector (rtx vec, rtx target,
> machine_mode mode,
> }
> }
>
> + rtx res = extract_vector_from_vector_perm (vec, target, mode, subvec_mode,
> + bitsize, bitnum, alt_rtl);
> + if (res)
> + return res;
> +
> return NULL_RTX;
> }
>
> diff --git a/gcc/expmed.h b/gcc/expmed.h
> index b4b44bba1d7..2e2d9009c84 100644
> --- a/gcc/expmed.h
> +++ b/gcc/expmed.h
> @@ -730,6 +730,9 @@ extern rtx expmed_mult_highpart_optab (scalar_int_mode,
> rtx, rtx, rtx,
> int, int);
> extern rtx expand_rotate_as_vec_perm (machine_mode, rtx, rtx, rtx);
>
> +extern opt_machine_mode vec_extract_perm_p (machine_mode, machine_mode,
> + poly_uint64);
> +
> /* The constant divisor of the last division expanded, reset per function
> by prepare_function_start. */
> extern HOST_WIDE_INT last_div_const;
> --
> 2.56.