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

Bad codegen for inline functions, it just doesn't work #19286

Closed
lassade opened this issue Mar 13, 2024 · 2 comments
Closed

Bad codegen for inline functions, it just doesn't work #19286

lassade opened this issue Mar 13, 2024 · 2 comments
Labels
question No questions on the issue tracker, please.

Comments

@lassade
Copy link

lassade commented Mar 13, 2024

Zig Version

0.12.0-dev.3282+da5b16f9e

Steps to Reproduce and Observed Behavior

Please give a the code in action here: https://godbolt.org/z/6T1edKa94

In the link above you will see these 4 functions. Each pair of function should do the exact same thing. First function contains the manually inlined code that I copied and pasted just to make sure it's really inlined and in the other I called the function.

The problem is that the assembly of the versions where the function is used are just crazy, and do way more stuff that it really should.

I'm using -Doptimize=ReleaseFast

export fn inlinePtrTest(item: *Ptr) void {
    @as(*Id, @alignCast(@ptrCast(item.inner + @offsetOf(Block.Inner, "id")))).* = .{ 0 };
    @as(*Pos, @alignCast(@ptrCast(item.inner + @offsetOf(Block.Inner, "pos")))).* = .{ @splat(0.0) };
}

export fn fnPtrTest(item: *Ptr) void {
    item.field(.id).* = .{ 0 };
    item.field(.pos).* = .{ @splat(0.0) };
}

export fn directBlock(block: *Block) void {
    _ = Ptr{ .inner = @alignCast(@ptrCast(&block.inner.pos[0])) };
}

export fn indirectBlock(block: *Block) void {
    _ = block.index(0);
}

Expected Behavior

Both assemblies of each pair should be identical

@lassade lassade added the bug Observed behavior contradicts documented or intended behavior label Mar 13, 2024
@mlugg
Copy link
Member

mlugg commented Mar 13, 2024

-Doptimize=ReleaseFast is an option you pass to zig build when a build script is using std.Built.standardOptimizeOption. When directly building a binary with build-exe/build-obj/build-lib, which is what Godbolt does, you need to use -OReleaseFast instead. The option you're passing isn't doing anything helpful (it's just defining a C macro!).

When you pass the correct optimization option, the codegen is identical for the two functions: https://godbolt.org/z/cKWTdTYWf

@mlugg mlugg closed this as not planned Won't fix, can't repro, duplicate, stale Mar 13, 2024
@mlugg mlugg added question No questions on the issue tracker, please. and removed bug Observed behavior contradicts documented or intended behavior labels Mar 13, 2024
@lassade
Copy link
Author

lassade commented Mar 13, 2024

Sorry I got skilled issued

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question No questions on the issue tracker, please.
Projects
None yet
Development

No branches or pull requests

2 participants