[Clang] Crash when using [[clang::preserve_none]]
struct S {
unsigned long long arr[3];
};
[[clang::preserve_none]] void call(S);
[[clang::preserve_none]] void func() {
S s{};
call(s);
}
Causes Clang to crash at -O1: https://godbolt.org/z/E77TdTn9P
Look like this started in clang-22
Happened to run into this when doing some experiment. Looked into it a bit, as far as I can see this is X86 backend specific:
When using -O(>=1), the TRE pass runs which stage tail calls, and together with preserve_none with, IR looks like:
tail call preserve_nonecc void @_Z14func(ptr noundef nonnull byval(%struct.S) align 8 %1)
Now we have tailcall on a func with preserve_none cc, and a byval in the parameter.
When lowering to DAG, we will consider if this is a tailcall/sibling call. It slips through the isEligibleForSiblingCallOpt check and thinks this is a sibling call, because byval for preserve_none is passed as a register, not memory.
As a result, the lowering logic thinks this is a sibling call and does not reserve stack space for it. But preserve_none with byval would try copy outgoing args in the stack, corrupting the frame. This leads to a runtime crash.
The propose fix is to check for non-standard CC like preserve_none and disqualify it for sibling call. Will put up a PR.