https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128739

            Bug ID: 128739
           Summary: [17 Regression] vectorizer: peeled early-break loop
                    drops final vector iteration's contribution to
                    reductions
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ted.d.rodgers at gmail dot com
  Target Milestone: ---

Created attachment 66172
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66172&action=edit
longer test case

Real-world impact: miscompiles read_variable_length in liblz4 (inlined into
LZ4_decompress_generic), silently truncating decompressed output 


Test case: 
    Fails with: gcc -O2 -mavx2, gcc -O2 -msse4.1 (returns 17347 instead of
25507 —      exactly one vector of 255s missing).

    Works with: baseline -O2/-O3 (loop not vectorized),
-fno-tree-loop-vectorize, or gcc-16


static unsigned char buf[256];

__attribute__ ((noinline)) static unsigned
rd (const unsigned char **pp, const unsigned char *lim)
{
  const unsigned char *p = *pp;
  unsigned int s = *p++;
  unsigned int len = s;

  while (s == 255)
    {
      if (p >= lim)
        return 0;
      s = *p++;
      len += s;
    }

  *pp = p;
  return len;
}

int
main (void)
{
  for (int i = 0; i < 100; i++)
    buf[31 + i] = 255;

  buf[131] = 7;

  const unsigned char *p = buf + 31;
  if (rd (&p, buf + 132) != 255 * 100 + 7)
    __builtin_abort();
}



Bisection:
r17-5740-g1b7a552c8dbb71 — gcc/tree-vect-loop-manip.cc, around line 1469
(vect_set_loop_condition, the LOOP_VINFO_EARLY_BREAKS_VECT_PEELED path that
creates latch_exit_edge).


A longer standalone test case reduced from lz4 is attached (prints len, showing
the 255×32-short fingerprint)

Reply via email to