| Issue |
234277
|
| Summary |
[WebAssembly] Support "musttail" when forwarding incoming "byval" parameters
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
yalexey-jcode
|
Summary
PR "#227491" (https://github.com/llvm/llvm-project/pull/227491) prevents WebAssembly tail calls with non-zero "byval" arguments to avoid passing pointers to copies in the caller's stack frame that becomes invalid before the callee reads them.
Could the backend support a narrower case: a "musttail" call that forwards an incoming "byval" parameter through the corresponding outgoing argument slot, without first creating another stack-resident copy?
The goal is to preserve guaranteed tail calls where the incoming argument storage can safely be reused, while retaining the existing protection for cases that cannot be lowered safely.
Reproducer
Tested with:
"clang version 24.0.0git (llvm-project 83ee07799c341b60965f07892f7bb3d175da05be)"
// repro.c
struct S {
unsigned long *a;
unsigned long *b;
};
void f(int n, struct S s) {
if (n == 0)
return;
__attribute__((musttail)) return f(n - 1, s);
}
On wasm32, "struct S" is 8 bytes and is passed using the "byval" convention.
clang --target=wasm32-unknown-unknown -mtail-call -c repro.c
The compilation fails with:
error: WebAssembly does not support tail calling with byval arguments
Relevant IR
The frontend emits a "musttail" call whose pointer operand is the incoming "byval" parameter:
define hidden void @f(
i32 noundef %0,
ptr noundef byval(%struct.S) align 4 %1
) {
...
musttail call void @f(
i32 noundef %9,
ptr noundef byval(%struct.S) align 4 %1
)
ret void
}
The pointer operand is forwarded unchanged, but the call still carries the "byval" attribute. Consequently, this is not yet copy elision: "WebAssemblyTargetLowering::LowerCall" still creates an outgoing copy in the current function's stack frame.
For a "return_call", the epilogue restores "__stack_pointer" before transferring control. A copy placed in that frame cannot safely be accessed by the callee afterward.
This is the class of failure addressed by "#66216" (https://github.com/llvm/llvm-project/issues/66216) and prevented by "#227491" (https://github.com/llvm/llvm-project/pull/227491).
Requested behavior
Please consider supporting the restricted forwarding case when a "musttail" call passes the caller's corresponding incoming "byval" parameter unchanged, with compatible ABI attributes.
The intended result is a real WebAssembly "return_call" without unbounded stack growth, provided the backend can preserve the required "byval" semantics. Calls that require a fresh copy, or for which forwarding cannot be proven safe, should retain the existing rejection or fallback behavior.
Pointer identity alone should not be treated as a complete proof of safety. The implementation must account for the "byval" copy semantics and relevant aliasing constraints.
Related work
- "#227491" (https://github.com/llvm/llvm-project/pull/227491) — the current WebAssembly restriction.
- "#66216" (https://github.com/llvm/llvm-project/issues/66216) — the original incorrect-code-generation report.
- "#199351" (https://github.com/llvm/llvm-project/pull/199351) — an ongoing Clang CodeGen change for forwarding incoming indirect arguments across "musttail". The WebAssembly case appears distinct because the target backend still materializes an outgoing copy for a call carrying "byval".
This issue is specifically about WebAssembly lowering and does not propose removing the protection added by #227491.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs