| Issue |
241282
|
| Summary |
[clang][X86-64] Named 256-bit vector in variadic call: clang and back end disagree (wrong code at all -O levels)
|
| Labels |
clang
|
| Assignees |
|
| Reporter |
jeffchen006
|
With `-mavx`, clang counts a named `__m256d`-like argument of a variadic call as one SSE register, so a later `_Complex double` vararg goes to memory. The X86 back end (`CCIfNotVarArg`) passes the named vector on the stack, so `%al` = 6 and the callee's `va_arg` reads xmm6/xmm7 from the register save area, which the caller never set.
**Reproducer** (`repro.c`)
```c
/* f reads its first vararg, the complex number 2+3i, so it returns 2*100 + 3*1000.
Correct output: 3200. With -mavx clang reads it from the xmm6/xmm7 save slots,
but the caller passed it in memory: wrong at every -O level (induced). */
#include <stdarg.h>
#include <stdio.h>
typedef double V4 __attribute__((vector_size(32)));
__attribute__((noinline)) double f(double a, double b, double c, double d, double e, double g, V4 v, ...) {
va_list ap;
va_start(ap, v);
_Complex double z = va_arg(ap, _Complex double);
va_end(ap);
return __real__ z * 100 + __imag__ z * 1000;
}
int main(void) {
printf("%g\n", f(1, 1, 1, 1, 1, 1, (V4){1, 1, 1, 1}, 2.0 + 3.0i));
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 -mavx repro.c -o o0
$ ./o0
100
$ clang -O3 -mavx repro.c -o o3
$ ./o3
5.2371e-320
```
Correct: `3200`. Clang `-O0` prints `100`, `-O3` prints `5.2371e-320` (stale register contents).
**Why the expected output is right**
One variadic call; the vararg is `_Complex double`, read with `va_arg(ap, _Complex double)`. Named args are `double` and a GNU vector. UBSan/ASan/MSan at -O0 report nothing.
**How the -O0/-O3 difference arises**
Both levels are wrong: the callee reads uninitialized register-save slots, so the value depends on stale register contents and differs between -O0 and -O3.
**Potential Root cause**
Clang and the X86 back end disagree. `clang/lib/CodeGen/Targets/X86.cpp`, `X86_64ABIInfo::classify` / `computeInfo`: a named vector of <= 256 bits (with -mavx) is SSE/SSEUP and takes one SSE register, so 6 doubles + 1 = 7 and the `_Complex double` vararg is passed `byval`. `llvm/lib/Target/X86/X86CallingConv.td`, `CC_X86_64_C`: the YMM rule is inside `CCIfNotVarArg`, so for any variadic call the `v4f64` goes to the stack (the FIXME there says this is not ABI-correct). The callee sees 2 free SSE slots and reads xmm6/xmm7.
**Possible Proposed fix**
Option A (clang only, keeps the current clang ABI): in `computeInfo`, for a variadic function, do not count an SSE register for a named vector wider than 128 bits:
```diff
it->info = classifyArgumentType(it->type, FreeIntRegs, NeededInt, NeededSSE, IsNamedArg);
+ if (FI.isVariadic() && IsNamedArg && it->type->isVectorType() &&
+ getContext().getTypeSize(it->type) > 128)
+ NeededSSE = 0;
```
Option B (psABI and GCC compatible): in `CC_X86_64_C` use `CCIfArgFixed` instead of `CCIfNotVarArg` for the YMM/ZMM rules. B changes the ABI of clang-compiled code.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs