-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Pass by reference "optimization" copies the entire struct on the stack when taking its address. (this didn't happen in stage1) #16343
Comments
Definitely agree. Note that for safe build modes a stack copy might help implementing stack leak checking (#2646 and, to a lesser extent, #5725) to be as strict as possible regardless of where the passed original argument comes from. |
I just wanted to point out (maybe this is already known/obvious) that this stack copying seems to occur in all optimization modes and in all cases that const std = @import("std");
pub fn printAddr(v: u64) void {
std.debug.print("{*}\n", .{&v});
std.debug.print("{*}\n", .{&v});
std.debug.print("{*}\n", .{&v});
}
pub fn main() void {
printAddr(12);
}
|
It is crazy. In the example above adding the line |
Zig Version
0.11.0-dev.3910+689f3163a
Steps to Reproduce and Observed Behavior
Here is the test code:
Here it is on godbolt: https://godbolt.org/z/7dTneM1od
As you can see in stage2 every single
_ = &x
creates a copy ofx
on the stack. (bottom left window in godbolt)While in simple cases like
_ = &x
, llvm can optimize it away, the last linevar ptr = &x;
stays in ReleaseFast(bottom right window in godbolt) and copies the entire struct onto the stack.Checking with 0.10 and
-fstage1
(top windows in godbolt) reveals that there is not a single memcpy in stage1Expected Behavior
This optimization is supposed to reduce the number of times we copy something, not increase it.
If the thing is already passed by reference then taking the reference should effectively be a no-op.
The text was updated successfully, but these errors were encountered: