-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Return value of inline
functions called from .Naked
functions cause stack allocations
#21193
Comments
These stack allocations are not coming from us:
; Function Attrs: naked noredzone noreturn nounwind uwtable nosanitize_coverage skipprofile
define dso_local void @someFunc() #0 !dbg !6 {
Entry:
%0 = call i32 asm sideeffect "mflr ${0}", "=r"(), !dbg !7
br label %Block, !dbg !7
Block:
%1 = phi i32 [ %0, %Entry ], !dbg !8
call void @llvm.dbg.value(metadata i32 %1, metadata !10, metadata !DIExpression()), !dbg !9
call void asm sideeffect "mtlr ${0}", "r"(i32 %1), !dbg !11
br label %Block1, !dbg !11
Block1:
call void asm sideeffect "rfi", ""(), !dbg !12
call void @llvm.trap(), !dbg !12
unreachable, !dbg !12
} In the general case, there is nothing we can do about this; LLVM is free to introduce stack accesses in backends if it likes. This is why both GCC and Clang consider This is why I've talked about a new language rule requiring naked functions to reduce to only |
Sorry I should have clarified, these stack allocations are coming from the debug information. We've previously had this for inline function parameters as well, but some recent work fixed that? So that would likely be |
Yes, I fixed a case where Zig itself was emitting an I can make that change if we want it, but we're fundamentally treating symptoms here. At the end of the day, an LLVM backend is still allowed to create stack accesses for non- |
After a quick chat we've found that the issue is not in the Removing the block and it's related parts will generate the same IR as the manual inlined version, and no longer do stack allocation. define dso_local void @someFunc() naked noinline {
Entry:
%0 = call i32 asm sideeffect "mflr ${0}", "=r"()
call void asm sideeffect "mtlr ${0}", "r"(i32 %0)
br label %Block1
Block1:
call void @llvm.trap()
unreachable
} someFunc: # @someFunc
mflr 3
mtlr 3
b .LBB0_1
.LBB0_1: # %Block1
trap EDIT: Just to clarify, this also happens on x86 and other targets. I just happen to code on powerpc. |
Zig Version
0.14.0-dev.1298+d9e8671d9
Steps to Reproduce and Observed Behavior
Compiling the following snippet using
-ODebug -target powerpc-freestanding-eabi
Output:
It will emit 2 stack operations to store the return value of
read()
and then load it again from stack for use withwrite()
.It will only do this in
.Debug
mode.Expected Behavior
No stack read and write.
Manually inlining the 2 functions leads to:
The text was updated successfully, but these errors were encountered: