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

            Bug ID: 129022
           Summary: [fold-const] wrong code at -O3 -march=znver4:
                    VIEW_CONVERT_EXPR folding of a constant AVX-512 mask
                    to a 4-lane mask uses 2-bit elements, turning
                    {-1,-1,0,0,...} into {-1,-1,-1,-1} in a masked
                    epilogue
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 0599jiangyc at gmail dot com
  Target Milestone: ---

The following code:

```c
/* GCC trunk wrong code: constant folding of VIEW_CONVERT_EXPR from an 8-lane
AVX-512
   mask (vector(8) <signed-boolean:1>, QImode) to a 4-lane mask (vector(4)
   <signed-boolean:1>, also QImode) turns {-1,-1,0,0,0,0,0,0} into
{-1,-1,-1,-1}.
   The masked vector epilogue then adds two inactive lanes (i = 58, 59) to the
   reduction.
   Flags: -O3 -march=znver4   (also -march=znver5,
          -O3 -march=x86-64-v4 --param vect-partial-vector-usage=1)
   Run under Intel SDE: sde64 -future -- ./a.out
   Expected: prints 2001, exit 0.  Trunk: prints 2130, exits 1.
   OK: -O0, -fno-tree-vectorize, -O3 -march=x86-64-v4 (no masked epilogue),
   GCC 11.4 -O3 -march=x86-64-v4 --param vect-partial-vector-usage=1, clang. 
*/
#include <stdio.h>
int A[64], C[64];
__attribute__((noipa)) unsigned long long f(void) {
  unsigned long long acc = 0;
  for (int i = 0; i < 58; i++) {
    unsigned short x = A[i] + 1;
    int c = x ? x + 1 : 0;
    C[i] = c;
    acc += (unsigned long long)c * 3 + i;
  }
  return acc;
}
int main(void) {
  unsigned long long r = f();
  printf("%llu\n", r);
  return r != 58 * 6 + 58 * 57 / 2;
}
```

Resulted in this output:
```
$ gcc -O3 -march=znver4 repro.c && ./a.out      (also -march=znver5; -O3
-march=x86-64-v4 --param vect-partial-vector-usage=1)
2130
(exit 1; -O0, -fno-tree-vectorize, -O3 -march=x86-64-v4 (no masked epilogue),
GCC 11.4 and clang print 2001)
```

Trunk: https://godbolt.org/z/eEoKTz69e (x86-64 gcc trunk, -O3 -march=znver4,
executed; reproduces as of 2026-11-12)

Reply via email to