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

            Bug ID: 128973
           Summary: [8/9/10/11/12/13/14/15/16/17 Regression] wrong code
                    with -mavx512f / -march=x86-64-v3: __builtin_alloca(0)
                    returns a pointer that is not aligned to
                    __BIGGEST_ALIGNMENT__, while CCP folds its alignment
           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
/* Godbolt: x86-64 gcc (trunk), flags: -O1 -mavx512f   (also -O2/-O3,
-march=x86-64-v3; any GCC >= 8)
   __builtin_alloca(0) is documented to return a pointer aligned to
__BIGGEST_ALIGNMENT__
   (32 with AVX, 64 with AVX-512).  The middle end folds (uintptr_t)p %
__BIGGEST_ALIGNMENT__
   to 0, but for a constant size 0 the RTL expansion returns the unaligned
dynamic stack
   address, so the same pointer has two different values of p % 64.
   Expected output "0" (exit 0); miscompiled: the number of disagreeing calls
(> 0, exit 1).  */
int printf(const char *, ...);
typedef __UINTPTR_TYPE__ uptr;
__attribute__((noipa)) uptr id(void *p) { return (uptr)p; }
__attribute__((noipa)) int probe(int depth) {
  if (depth) {
    volatile char pad[16];
    pad[0] = 0;
    return probe(depth - 1) + pad[0];
  }
  void *p = __builtin_alloca(0);
  uptr folded = (uptr)p % __BIGGEST_ALIGNMENT__;   /* folded to 0 at -O1 and
above */
  uptr actual = id(p) % __BIGGEST_ALIGNMENT__;     /* the pointer actually
returned */
  return folded != actual;
}
int main(void) {
  int bad = 0;
  for (int d = 0; d < 8; d++)
    bad += probe(d);
  printf("%d\n", bad);
  return bad != 0;
}
```

Resulted in this output:
```
$ gcc -O1 -mavx512f repro.c && ./a.out        (also -O2/-O3, and
-march=x86-64-v3)
4
(exit 1; -O0 prints 0: there the size is not a constant at expand time and the
pointer is realigned)
```

Trunk: https://godbolt.org/z/68K6Yo9z9 (x86-64 gcc trunk, -O1 -mavx512f,
executed; also 8.3, 9.3, 11.4, 14.2; 7.5 OK)

Reply via email to