-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Missing optimization with optional pointers #20254
Comments
Here's an even simpler repro: const std = @import("std");
const A = struct { data: usize = 0 };
const B = struct { data: *usize };
export fn getA(a: *A) usize {
const maybe_data: ?*usize = &a.data;
return if (maybe_data) |data| data.* else 0xFFFF;
}
export fn getB(b: *B) usize {
const maybe_data: ?*usize = b.data;
return if (maybe_data) |data| data.* else 0xFFFF;
}
pub fn main() void {
var a = A{};
var b = B{ .data = &a.data };
std.mem.doNotOptimizeAway(&a);
std.mem.doNotOptimizeAway(&b);
@breakpoint();
std.mem.doNotOptimizeAway(@call(.never_inline, getA, .{&a}));
std.mem.doNotOptimizeAway(@call(.never_inline, getB, .{&b}));
} The generated code for 00C310A0: 48 8B 01 mov rax, qword ptr [rcx]
00C310A3: C3 ret The generated code for 00C310B0: 48 8B 01 mov rax, qword ptr [rcx]
00C310B3: 48 85 C0 test rax, rax
00C310B6: 74 04 je 0xc310bc ; <+12> at main.zig:13
00C310B8: 48 8B 00 mov rax, qword ptr [rax]
00C310BB: C3 ret
00C310BC: B8 FF FF 00 00 mov eax, 0xffff
00C310C1: C3 ret Why is there a comparison against (Also I had to use the |
The issue seems to have wide-reaching implications in preventing efficient implementation of |
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
Compile the following code in ReleaseFast mode
Check the disassembly for
getA
. On x64 it looks likeCheck the disassembly for
getB
: On x64 it looks likeApparently there is a second unnecessary check for null in the code generated for
getB
.Expected Behavior
In the code generation for
getB
the compiler is expected to infer that the second check is unnecessary, since the pointer retrieved fromb.data
is never null, same as in compilation ofgetA
it inferred that the pointer obtained from&a.data
is never null.The text was updated successfully, but these errors were encountered: