-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Using @sin, @cos on x86_64-macos fails with missing symbol _sincosf when building optimized #10318
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
Comments
What about building non-optimized such as Debug? |
Works fine. Only release modes fail |
After quick investigation, similar thing happens when targeting Debug
ReleaseSafe
|
I think I was at this point once already: #7267 |
Oh, I see now. We don't provide this function at all when compiling for macOS, and it works in Debug since LLVM calls separate functions |
Here's a proposed fix: diff --git a/lib/std/special/c_stage1.zig b/lib/std/special/c_stage1.zig
index 3ae93c2bd..a7c730028 100644
--- a/lib/std/special/c_stage1.zig
+++ b/lib/std/special/c_stage1.zig
@@ -687,7 +687,7 @@ export fn sincos(a: f64, r_sin: *f64, r_cos: *f64) void {
r_cos.* = math.cos(a);
}
-export fn sincosf(a: f32, r_sin: *f32, r_cos: *f32) void {
+pub export fn sincosf(a: f32, r_sin: *f32, r_cos: *f32) void {
r_sin.* = math.sin(a);
r_cos.* = math.cos(a);
}
diff --git a/lib/std/special/compiler_rt.zig b/lib/std/special/compiler_rt.zig
index 95edfee86..6348f870c 100644
--- a/lib/std/special/compiler_rt.zig
+++ b/lib/std/special/compiler_rt.zig
@@ -45,6 +45,10 @@ comptime {
const __getf2 = @import("compiler_rt/compareXf2.zig").__getf2;
@export(__getf2, .{ .name = "__getf2", .linkage = linkage });
+ if (builtin.os.tag.isDarwin()) {
+ _ = @import("c_stage1.zig").sincosf;
+ }
+
if (!is_test) {
@export(__lesf2, .{ .name = "__cmpsf2", .linkage = linkage });
@export(__ledf2, .{ .name = "__cmpdf2", .linkage = linkage });
Lemme know what every thinks! EDIT: @MasterQ32 I haven't run the resulting binary on an actual macOS but I don't see why it would fail at runtime. Anyhow, if we agree this is the right approach, I'll make the necessary adjustments and test everything out. Also, FWIW I'm working on a standalone test harness for the self-hosted linker and this will naturally get a test case of its own. |
Actually, it occurred to me just now that we should really re-export it as a weak symbol - this way, if Apple decides to expose the symbol under the name of |
Instead of fixing the immediate problem it's better to ask why it's not working.
According to LLVM source code this should not happen at all, unless Zig is somehow telling LLVM the abi is Aw shit, if you check the emitted And... that's exactly what I reported in #9089? I'll also say it loud again, none of this libc/libm shit has a place in |
Wait, so I'm looking at the LLVM source, and it should produce an undefined symbol for
Also, this is the first time I'm looking into this, and I really don't think any hostile behaviour or a sarcastic outburst like this is warranted. |
FWIW this has been the behavior on mac os with clang for a long time in my experience, I've profiled non-zig applications that use sincosf in release builds. It's a neat function, it puts (x, x + pi/2) into a simd vector and then does a simd sin calculation to get both answers at once. |
Missing symbols, is that what you mean? |
No, I mean clang generating calls to sincosf in release modes when sin and cos are used near each other. |
Mhm, gotcha. Do you know what the symbol resolves to by any chance? |
I don't, all I know is that instruments was able to dump the assembly of _sincosf when profiling. |
Let's take this small piece of code lifted from LLVM's unit tests: define float @test1(float %x) nounwind {
%call = tail call float @sinf(float %x) nounwind readnone
%call1 = tail call float @cosf(float %x) nounwind readnone
%add = fadd float %call, %call1
ret float %add
}
declare float @sinf(float) readonly
declare float @cosf(float) readonly Now check and compare the output of:
|
So for the first one, I do see a call to extern symbol
Interestingly, for this old version of macOS, I see two separate calls to
And for the last one, I see a bizarre call to |
Ohh, wait, the last example is for unspecified OS it seems. Does that mean it assumes native? I did try the first example as |
The feature detection stuff is quite delicate, somehow the ABI part has lower priority than the OS part (eg. |
Yeah, this is exactly what it is. I just did a quick test on the original test case filed in this issue, I swapped out the triple from the generated LLVM IR from
This seems to suggest we should assume the ABI as unspecified (by default) when targeting Darwin. Would you agree with this conclusion? |
Yep, IMO every other target beside linux (and possibly other known exceptions) should default to Specifying the macos version may or may not be needed, I don't know if we currently specify that in some other way. |
You have asserted this a few times now, and I have no doubt that you have solid reasoning behind it, but you have not shared the reasoning with me yet. There are no arguments against #7265 listed on that issue and my comment here is sad and lonely, with no response to it: #10163 (comment) Consider the following case: A user builds zig code on freestanding into an ELF file. No C code is involved in this process whatsoever. The LLVM backend is used and we emit a call to the LLVM intrinsic You're claiming that the linker line should have: I'm not convinced; I will definitely need to hear reasoning to swing my opinion around to a different one. |
For another datapoint: I'm also hitting the same build error when compiling on x86_64-macos for a game which utilizes the Raylib library. Follow-up edit: Upon 2nd pass actually reading this whole thread, this has nothing to with Raylib and everything to do with LLVM's optimization of @sin/@cos. This blows up with both ReleaseSafe/ReleaseFast and is fine in Debug mode. |
Zig Version
0.9.0-dev.1945+efdb94486
Steps to Reproduce
Build the following file:
Expected Behavior
The code builds successfully, like when building to
x86_64-linux
Actual Behavior
The text was updated successfully, but these errors were encountered: