Skip to content
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

Open
IntegratedQuantum opened this issue Jul 7, 2023 · 3 comments
Labels
optimization regression It worked in a previous version of Zig, but stopped working.
Milestone

Comments

@IntegratedQuantum
Copy link
Contributor

Zig Version

0.11.0-dev.3910+689f3163a

Steps to Reproduce and Observed Behavior

Here is the test code:

const std = @import("std");

const Struct = struct {
    smallArray: [2]u8 = undefined,
    thisStructIsBig: [65536]u8 = undefined,
    
    pub noinline fn testHiddenPassByReference(x: Struct, index: usize) u8 {
        _ = &x;
        _ = &x;
        _ = &x;
        _ = &x;
        _ = &x;
        _ = &x;
        _ = &x;
        _ = &x;
        _ = &x;
        _ = &x;
        var ptr = &x;
        return ptr.smallArray[index];
    }
};

export fn fun(index: usize) u8 {
    var str = Struct{};
    str.smallArray[index] = 0;
    return str.testHiddenPassByReference(index);
}

Here it is on godbolt: https://godbolt.org/z/7dTneM1od

As you can see in stage2 every single _ = &x creates a copy of x on the stack. (bottom left window in godbolt)
While in simple cases like _ = &x, llvm can optimize it away, the last line var 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 stage1

Expected 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.

@IntegratedQuantum IntegratedQuantum added the bug Observed behavior contradicts documented or intended behavior label Jul 7, 2023
@rohlem
Copy link
Contributor

rohlem commented Jul 7, 2023

If the thing is already passed by reference then taking the reference should effectively be a no-op.

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.
But even if that's the way that is implemented, we should be re-using this one stack copy.

@andrewrk andrewrk added optimization regression It worked in a previous version of Zig, but stopped working. and removed bug Observed behavior contradicts documented or intended behavior labels Jul 22, 2023
@andrewrk andrewrk modified the milestones: 0.11.0, 0.12.0 Jul 22, 2023
190n added a commit to 190n/zip8 that referenced this issue Sep 27, 2023
@permutationlock
Copy link
Contributor

permutationlock commented Dec 3, 2023

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 &x is referenced on a function parameter x, regardless of type or size. It isn't calling memcpy for small parameters, but new stack copies are made for each reference. The easiest way to observe the behavior is executing the following and seeing that three different addresses are printed.

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);
}

@slonik-az
Copy link

It is crazy. In the example above adding the line std.debug.print("{}\n", .{&v==&v}) to printAddr function prints false. Apparently, &v is not equal to itself. PRO seems to be broken.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
optimization regression It worked in a previous version of Zig, but stopped working.
Projects
Status: Optimize Machine Code
Development

No branches or pull requests

5 participants