| Issue |
241264
|
| Summary |
[X86] vzeroupper before ret of preserve_all / intel_ocl_bicc functions that keep YMM (miscompile at -O3)
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
jeffchen006
|
For a `preserve_all` function with AVX, the callee must keep the upper halves of all YMM registers. PEI restores them, but X86InsertVZeroUpper then puts `vzeroupper` before `retq`. The caller keeps a vector in YMM over the call and loses lanes 2-3.
**Reproducer** (`repro.c`)
```c
/* f is preserve_all, so it must keep all YMM registers, and main can keep a in
a YMM register over the call. a = {1,2,3,4} + {2,4,6,8}: must print 3 6 9 12.
Clang -O3 puts vzeroupper before the return of f: lanes 2-3 of a become 0. */
int printf(const char *, ...);
typedef unsigned long long v4 __attribute__((vector_size(32)));
volatile v4 g = {1, 2, 3, 4};
__attribute__((preserve_all, noinline)) static void f(void) {
v4 t = g;
g = t + t;
}
int main(void) {
v4 a = g;
f();
a = a + g;
printf("%llu %llu %llu %llu\n", a[0], a[1], a[2], a[3]);
return 0;
}
```
**Commands and output** (clang version 24.0.0git (https://github.com/llvm/llvm-project.git 8207e9143575), Target: x86_64-unknown-linux-gnu):
```console
$ clang -O0 -march=x86-64-v3 repro.c -o o0
$ ./o0
3 6 9 12
$ clang -O3 -march=x86-64-v3 repro.c -o o3
$ ./o3
3 6 6 8
```
Expected (as `-O0`): `3 6 9 12`. `-O3` prints `3 6 6 8`.
**Why the expected output is right**
Unsigned 64-bit lane arithmetic (wraps, defined); a volatile global vector; `preserve_all` is a documented Clang attribute and both sides are in one TU. UBSan/ASan/MSan at -O0 are clean.
**Potential Root cause**
`llvm/lib/Target/X86/X86InsertVZeroUpper.cpp`, `processBasicBlock`: a return of a dirty block gets `vzeroupper`; the only exception is `IsX86INTR && IsReturn`, and `insertVZeroUpper(MF)` sets `IsX86INTR` from `CallingConv::X86_INTR` only. For `preserve_all` + AVX the CSR list is `CSR_64_RT_AllRegs_AVX` (YMM0-15). The same happens for `intel_ocl_bicc` (YMM8-15) and `no_caller_saved_registers`.
**Possible Proposed fix**
Replace the `IsX86INTR` test with "the function must preserve YMM upper halves":
```diff
- bool IsX86INTR = MF.getFunction().getCallingConv() == CallingConv::X86_INTR;
+ const TargetRegisterInfo *TRI = ST.getRegisterInfo();
+ bool PreservesYmm = false;
+ for (const MCPhysReg *R = TRI->getCalleeSavedRegs(&MF); *R; ++R)
+ if (isYmmOrZmmReg(*R)) { PreservesYmm = true; break; }
```
and in `processBasicBlock` use `if (PreservesYmm && IsReturn) continue;`.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs