> -----Original Message-----
> From: Richard Biener <[email protected]>
> Sent: 02 November 2026 12:53
> To: Tamar Christina <[email protected]>
> Cc: [email protected]; nd <[email protected]>; [email protected]
> Subject: Re: [patch v2 14/14][vect]: allow degenerate SLP permutations for
> early loads
> 
> On Thu, 22 Oct 2026, Tamar Christina wrote:
> 
> > This patch relaxes one speculative-load check used by early-break
> vectorization.
> > The vectorizer correctly rejects SLP load permutations for speculative loads
> > because an arbitrary permutation can invalidate the load layout
> assumptions.
> > However, an all-zero permutation is fine when the group has no gaps,
> including
> > permutations such as { 0, 0, 0, 0 } for multi-element groups. It only 
> > replicates
> > the first element of each scalar iteration's group and does not change the
> > alignment or footprint of the input loads. The existing checks still require
> > alignment for the whole group and check the speculative read amount
> against the
> > page size.
> >
> > A simplified C example is:
> >
> >   void **find_ptr (void **first, void **last, void *needle)
> >   {
> >     while (first != last && *first != needle)
> >       ++first;
> >     return first;
> >   }
> >
> > Before this patch the vectorizer could reject the speculative load because 
> > it
> > saw an SLP load permutation and treated it like a real permute:
> >
> >   missed: loads with load permutations not supported for speculative loads
> >
> > After this patch, all-zero load permutations are accepted for groups without
> > gaps, and the loop can continue down the early-break vectorization path.
> >
> > Bootstrapped Regtested on aarch64-none-linux-gnu,
> > arm-none-linux-gnueabihf, x86_64-pc-linux-gnu
> > -m32, -m64 and no issues.
> >
> > -O3, profiled, profiled -O3 and -O3 -LTO bootstrap tested and no issues.
> >
> >  Also SPECCPU 2017 and SPECCPU 2026 tested.
> >
> > Any feedback?
> 
> Would that, by extension, not be the same for any uniform load
> permutation?  So even for { 1, 1, 1 ... } for a multi-element group?
> The reasoning is that the first iteration is executed, so any further
> iteration does not change whether that element load trapped, right?
> 

If the group access isn't change indeed. I was conservative here because
I wasn't sure if this scenario could happen. Consider

[0 1 . .][0 1 . .]

I was concerned that instead we could optimize this by changing base to
be base + 1, and loading [1 0 . 1] instead.  I suppose we'd block this later
but [0 0 0 ..] was easier to reason about.  But Maybe I'm too conservative.

> I'll note that this isn't universally true but only when all
> scalar elements of one vector iterations load the same element
> which depends on a) VF, b) the group size, so I think you are
> lacking some multiple-of checks here.
> 

Hmm will check. Don't immediately follow this one.

Thanks for the reviews!,
Tamar

> Richard.
> 
> > Thanks,
> > Tamar
> >
> > gcc/ChangeLog:
> >
> >     * tree-vect-stmts.cc (get_load_store_type): Allow all-zero SLP load
> >     permutations for speculative loads from groups without gaps,
> including
> >     multi-element groups.  Update the diagnostic.
> >
> > gcc/testsuite/ChangeLog:
> >
> >     * g++.dg/vect/vect-early-break-pr-spec-find.cc: New test.
> >
> > ---
> > diff --git a/gcc/testsuite/g++.dg/vect/vect-early-break-pr-spec-find.cc
> b/gcc/testsuite/g++.dg/vect/vect-early-break-pr-spec-find.cc
> > new file mode 100644
> > index
> 0000000000000000000000000000000000000000..05e82345f0214d70da
> 355a07053e10a9a069ab73
> > --- /dev/null
> > +++ b/gcc/testsuite/g++.dg/vect/vect-early-break-pr-spec-find.cc
> > @@ -0,0 +1,96 @@
> > +/* { dg-do compile } */
> > +/* { dg-add-options vect_early_break } */
> > +/* { dg-require-effective-target vect_early_break } */
> > +/* { dg-require-effective-target vect_long_long } */
> > +/* { dg-additional-options "-O3 -fno-tree-loop-distribute-patterns" } */
> > +
> > +struct X;
> > +
> > +template<typename Value>
> > +struct iter_equals_val
> > +{
> > +  Value& value;
> > +
> > +  explicit
> > +  iter_equals_val (Value& val)
> > +    : value (val)
> > +  {}
> > +
> > +  template<typename Iter>
> > +  bool
> > +  operator() (Iter it)
> > +  { return *it == value; }
> > +};
> > +
> > +template<typename Iter, typename Pred>
> > +inline Iter
> > +find_if (Iter first, Iter last, Pred pred)
> > +{
> > +#pragma GCC unroll 4
> > +  while (first != last && !pred (first))
> > +    ++first;
> > +  return first;
> > +}
> > +
> > +__attribute__((noipa, noinline))
> > +X **
> > +find_ptr (X **first, X **last, X *needle)
> > +{
> > +  iter_equals_val<X *> pred (needle);
> > +  return find_if (first, last, pred);
> > +}
> > +
> > +struct object_pool
> > +{
> > +  X **first;
> > +  X **last;
> > +
> > +  __attribute__((noipa, noinline))
> > +  bool
> > +  return_object (X *needle, bool reset)
> > +  {
> > +    iter_equals_val<X *> pred (needle);
> > +    X **pos = find_if (first, last, pred);
> > +    if (pos == last)
> > +      return false;
> > +
> > +    if (!reset)
> > +      {
> > +   for (X **it = pos; it + 1 != last; ++it)
> > +     *it = *(it + 1);
> > +   --last;
> > +      }
> > +
> > +    return true;
> > +  }
> > +};
> > +
> > +__attribute__((noipa, noinline))
> > +bool
> > +return_to_pool (object_pool *pool, X *needle, bool reset)
> > +{
> > +  return pool->return_object (needle, reset);
> > +}
> > +
> > +__attribute__((noipa, noinline))
> > +bool
> > +dom_string_equals (const unsigned short *lhs, unsigned long lhs_length,
> > +              const unsigned short *rhs, unsigned long rhs_length)
> > +{
> > +  if (lhs_length != rhs_length)
> > +    return false;
> > +  if (lhs_length == 0)
> > +    return true;
> > +
> > +  const unsigned short *end = lhs + lhs_length;
> > +  while (*lhs == *rhs)
> > +    {
> > +      ++lhs;
> > +      if (lhs == end)
> > +   return true;
> > +      ++rhs;
> > +    }
> > +  return false;
> > +}
> > +
> > +/* { dg-final { scan-tree-dump "LOOP VECTORIZED" "vect" } } */
> > diff --git a/gcc/tree-vect-stmts.cc b/gcc/tree-vect-stmts.cc
> > index
> 3bb10d4b9a9eee94cc6bce565ba78afc5fb243ab..72a44b278d298019d483
> 24aa739aa58544f89645 100644
> > --- a/gcc/tree-vect-stmts.cc
> > +++ b/gcc/tree-vect-stmts.cc
> > @@ -2644,13 +2644,25 @@ get_load_store_type (vec_info  *vinfo,
> stmt_vec_info stmt_info,
> >      the blend.  This is what the x86_64 backend does for instance.  As
> >      such only the first load in the group is aligned, the rest are not.
> >      Because of this the permutes may break the alignment requirements
> that
> > -    have been set, and as such we should for now, reject them.  */
> > -      if (SLP_TREE_LOAD_PERMUTATION (slp_node).exists ())
> > +    have been set, and as such we should for now, reject them.  However,
> > +    an all-zero permutation of a group without gaps only replicates the
> > +    first element of each scalar iteration's group.  It does not change
> > +    the alignment or the footprint of the input loads, which the checks
> > +    above account for using the whole group size.  */
> > +      load_permutation_t perm = SLP_TREE_LOAD_PERMUTATION
> (slp_node);
> > +      bool all_zero_p = true;
> > +      for (unsigned int index : perm)
> > +   if (index != 0)
> > +     {
> > +       all_zero_p = false;
> > +       break;
> > +     }
> > +      if (perm.exists () && (gap != 0 || !all_zero_p))
> >     {
> >       if (dump_enabled_p ())
> >         dump_printf_loc (MSG_MISSED_OPTIMIZATION, vect_location,
> >                          "loads with load permutations not supported for "
> > -                        "speculative early break loads for %G",
> > +                        "speculative loads for %G",
> >                          STMT_VINFO_STMT (stmt_info));
> >       return false;
> >     }
> >
> >
> >
> 
> --
> Richard Biener <[email protected]>
> SUSE Software Solutions Germany GmbH,
> Frankenstrasse 146, 90461 Nuernberg, Germany;
> GF: Stefan Gaiser, Jochen Jaser, Abhinav Puri; (HRB 36809, AG Nuernberg)

Reply via email to