Issue 244085
Summary [SystemZ][clang] vecintrin.h 64-bit vec_scatter_element/vec_gather_element access memory as `long long`, so a store into a `uint64_t` array is lost at -O1
Labels clang
Assignees
Reporter 20000419
    On Linux s390x, `uint64_t` is `unsigned long`. The 64-bit overloads of `vec_scatter_element` and `vec_gather_element` in `clang/lib/Headers/vecintrin.h` take an `unsigned long long *` / `signed long long *` and access memory through that type (`*(unsigned long long *)((char *)__ptr + __offset[__index]) = __vec[__index];`). There is no `unsigned long *` overload, and passing a `uint64_t *` directly is an error (`-Wincompatible-pointer-types`), so every caller with a `uint64_t` array has to cast. TBAA then treats the scatter store (`long long`) and the program's own accesses (`long`) as non-aliasing, and the stored value is lost.

```c
#include <vecintrin.h>
#include <stdio.h>
#include <stdint.h>

volatile uint64_t escape = 1;

int main(void) {
  uint64_t ob[16];
  for (int i = 0; i < 16; i++) ob[i] = 0;
  __vector unsigned long long sv = {0x1122334455667788ULL, 0x99aabbccddeeff00ULL};
  __vector unsigned long long off = {0, 8 * (escape & 1)};
  vec_scatter_element(sv, off, (unsigned long long *)(ob + 4), 1);
  uint64_t got = ob[5];
 printf("got=%016llx want=99aabbccddeeff00\n", (unsigned long long)got);
 return got != 0x99aabbccddeeff00ULL;
}
```

`clang --target=s390x-linux-gnu -fzvector -march=z15 -O1 -static t.c` (run under `qemu-s390x`)

Prints `got=0000000000000000` at `-O1`, `-O2`, `-O3` and `-Os`. `-O0`, `-O2 -fno-strict-aliasing`, an `unsigned long long` array, and s390x gcc 14 `-O2` all print `got=99aabbccddeeff00`.

At `-O2`, GVN forwards the zero from the initializing `memset` to the load of `ob[5]` across the scatter store, and `printf` is called with a constant 0. At `-O1` the IR still has the store (`!tbaa` `long long`) followed by the load (`!tbaa` `long`), and the generated code loads `ob[5]` (`lg %r13, 200(%r15)`) before the store (`stg %r12, 192(%r1,%r15)`).

Godbolt (clang trunk, `-O0` and `-O1`): https://godbolt.org/z/WeznbcYqG

`vec_gather_element` has the same problem in the other direction: the gather loads are moved above the program's stores to the array.

```c
#include <vecintrin.h>
#include <stdio.h>
#include <stdint.h>
typedef uint64_t u64;
volatile u64 seed = 1;
int main(void) {
  u64 mem[4];
  for (int i = 0; i < 4; i++) mem[i] = 0;
  __vector unsigned long long g = {7, 7};
  __vector unsigned long long goff = {0, 8 * (seed & 1)};
  mem[1] = 0x1111111111111111ULL;
  __vector unsigned long long g1 =
      vec_gather_element(g, goff, (const unsigned long long *)mem, 1);
  mem[1] = 0x2222222222222222ULL;
  __vector unsigned long long g2 =
      vec_gather_element(g, goff, (const unsigned long long *)mem, 1);
  printf("g1=%016llx g2=%016llx %s\n", (unsigned long long)g1[1],
         (unsigned long long)g2[1],
         g1[1] == 0x1111111111111111ULL && g2[1] == 0x2222222222222222ULL
             ? "OK" : "FAIL");
  return 0;
}
```

`-O0`, `-O2 -fno-strict-aliasing` and gcc 14 `-O2` print `OK`. `-O1` prints zeros for both values. At `-O2`, `-O3` and `-Os`, clang 15 to 23 print uninitialized stack contents and trunk prints zeros.

The header avoids typed accesses elsewhere: `vec_xl`/`vec_xst` use `__builtin_memcpy`. The problem seems to be that the 64-bit gather/scatter overloads access the memory through `unsigned long long *` / `signed long long *`, which does not alias the `long` accesses of a `uint64_t` / `int64_t` array.

Versions (`qemu-s390x`): both programs are wrong at `-O1` to `-Os` with clang 15.0.6, 16.0.4, 17.0.6, 18.1.8, 19, 20, 21, 22, 23 and 24 (`d5a22c0d1c26`). `-O0` is correct with all of them, except that the 17.0.6 `-O0` build of the gather program stops with SIGILL under qemu. Compiler Explorer clang trunk (`c80ecb8de257`) generates the same code for the first program. The header code is unchanged on main (`1559c018809d`).

cc @uweigand

Disclaimer: This test case was found with LLM-assisted targeted testing, and the report was written with LLM assistance.

Assisted-by: Grok

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to