Commit 5870ffeb82

Jacob Young <jacobly0@users.noreply.github.com>
2023-03-28 03:32:37
compiler_rt: change the abi of f16 on mac to depend on the other type
1 parent 2438481
lib/compiler_rt/common.zig
@@ -81,12 +81,20 @@ pub fn panic(msg: []const u8, error_return_trace: ?*std.builtin.StackTrace, _: ?
 /// need for extending them to wider fp types.
 /// TODO remove this; do this type selection in the language rather than
 /// here in compiler-rt.
-pub const F16T = switch (builtin.cpu.arch) {
-    .aarch64, .aarch64_be, .aarch64_32 => f16,
-    .riscv64 => if (builtin.zig_backend == .stage1) u16 else f16,
-    .x86, .x86_64 => if (builtin.target.isDarwin()) u16 else f16,
-    else => u16,
-};
+pub fn F16T(comptime other_type: type) type {
+    return switch (builtin.cpu.arch) {
+        .aarch64, .aarch64_be, .aarch64_32 => f16,
+        .riscv64 => if (builtin.zig_backend == .stage1) u16 else f16,
+        .x86, .x86_64 => if (builtin.target.isDarwin()) switch (other_type) {
+            // Starting with LLVM 16, Darwin uses different abi for f16
+            // depending on the type of the other return/argument..???
+            f32, f64 => u16,
+            f80, f128 => f16,
+            else => unreachable,
+        } else f16,
+        else => u16,
+    };
+}
 
 pub fn wideMultiply(comptime Z: type, a: Z, b: Z, hi: *Z, lo: *Z) void {
     switch (Z) {
lib/compiler_rt/extendf_test.zig
@@ -45,7 +45,7 @@ fn test__extenddftf2(a: f64, expected_hi: u64, expected_lo: u64) !void {
 }
 
 fn test__extendhfsf2(a: u16, expected: u32) !void {
-    const x = __extendhfsf2(@bitCast(F16T, a));
+    const x = __extendhfsf2(@bitCast(F16T(f32), a));
     const rep = @bitCast(u32, x);
 
     if (rep == expected) {
@@ -208,7 +208,7 @@ fn makeInf32() f32 {
 }
 
 fn test__extendhftf2(a: u16, expected_hi: u64, expected_lo: u64) !void {
-    const x = __extendhftf2(@bitCast(F16T, a));
+    const x = __extendhftf2(@bitCast(F16T(f128), a));
 
     const rep = @bitCast(u128, x);
     const hi = @intCast(u64, rep >> 64);
lib/compiler_rt/extendhfdf2.zig
@@ -7,6 +7,6 @@ comptime {
     @export(__extendhfdf2, .{ .name = "__extendhfdf2", .linkage = common.linkage, .visibility = common.visibility });
 }
 
-pub fn __extendhfdf2(a: common.F16T) callconv(.C) f64 {
+pub fn __extendhfdf2(a: common.F16T(f64)) callconv(.C) f64 {
     return extendf(f64, f16, @bitCast(u16, a));
 }
lib/compiler_rt/extendhfsf2.zig
@@ -12,11 +12,11 @@ comptime {
     @export(__extendhfsf2, .{ .name = "__extendhfsf2", .linkage = common.linkage, .visibility = common.visibility });
 }
 
-pub fn __extendhfsf2(a: common.F16T) callconv(.C) f32 {
+pub fn __extendhfsf2(a: common.F16T(f32)) callconv(.C) f32 {
     return extendf(f32, f16, @bitCast(u16, a));
 }
 
-fn __gnu_h2f_ieee(a: common.F16T) callconv(.C) f32 {
+fn __gnu_h2f_ieee(a: common.F16T(f32)) callconv(.C) f32 {
     return extendf(f32, f16, @bitCast(u16, a));
 }
 
lib/compiler_rt/extendhftf2.zig
@@ -7,6 +7,6 @@ comptime {
     @export(__extendhftf2, .{ .name = "__extendhftf2", .linkage = common.linkage, .visibility = common.visibility });
 }
 
-pub fn __extendhftf2(a: common.F16T) callconv(.C) f128 {
+pub fn __extendhftf2(a: common.F16T(f128)) callconv(.C) f128 {
     return extendf(f128, f16, @bitCast(u16, a));
 }
lib/compiler_rt/extendhfxf2.zig
@@ -7,6 +7,6 @@ comptime {
     @export(__extendhfxf2, .{ .name = "__extendhfxf2", .linkage = common.linkage, .visibility = common.visibility });
 }
 
-fn __extendhfxf2(a: common.F16T) callconv(.C) f80 {
+fn __extendhfxf2(a: common.F16T(f80)) callconv(.C) f80 {
     return extend_f80(f16, @bitCast(u16, a));
 }
lib/compiler_rt/truncdfhf2.zig
@@ -11,10 +11,10 @@ comptime {
     }
 }
 
-pub fn __truncdfhf2(a: f64) callconv(.C) common.F16T {
-    return @bitCast(common.F16T, truncf(f16, f64, a));
+pub fn __truncdfhf2(a: f64) callconv(.C) common.F16T(f64) {
+    return @bitCast(common.F16T(f64), truncf(f16, f64, a));
 }
 
 fn __aeabi_d2h(a: f64) callconv(.AAPCS) u16 {
-    return @bitCast(common.F16T, truncf(f16, f64, a));
+    return @bitCast(common.F16T(f64), truncf(f16, f64, a));
 }
lib/compiler_rt/truncsfhf2.zig
@@ -12,14 +12,14 @@ comptime {
     @export(__truncsfhf2, .{ .name = "__truncsfhf2", .linkage = common.linkage, .visibility = common.visibility });
 }
 
-pub fn __truncsfhf2(a: f32) callconv(.C) common.F16T {
-    return @bitCast(common.F16T, truncf(f16, f32, a));
+pub fn __truncsfhf2(a: f32) callconv(.C) common.F16T(f32) {
+    return @bitCast(common.F16T(f32), truncf(f16, f32, a));
 }
 
-fn __gnu_f2h_ieee(a: f32) callconv(.C) common.F16T {
-    return @bitCast(common.F16T, truncf(f16, f32, a));
+fn __gnu_f2h_ieee(a: f32) callconv(.C) common.F16T(f32) {
+    return @bitCast(common.F16T(f32), truncf(f16, f32, a));
 }
 
 fn __aeabi_f2h(a: f32) callconv(.AAPCS) u16 {
-    return @bitCast(common.F16T, truncf(f16, f32, a));
+    return @bitCast(common.F16T(f32), truncf(f16, f32, a));
 }
lib/compiler_rt/trunctfhf2.zig
@@ -7,6 +7,6 @@ comptime {
     @export(__trunctfhf2, .{ .name = "__trunctfhf2", .linkage = common.linkage, .visibility = common.visibility });
 }
 
-pub fn __trunctfhf2(a: f128) callconv(.C) common.F16T {
-    return @bitCast(common.F16T, truncf(f16, f128, a));
+pub fn __trunctfhf2(a: f128) callconv(.C) common.F16T(f128) {
+    return @bitCast(common.F16T(f128), truncf(f16, f128, a));
 }
lib/compiler_rt/truncxfhf2.zig
@@ -7,6 +7,6 @@ comptime {
     @export(__truncxfhf2, .{ .name = "__truncxfhf2", .linkage = common.linkage, .visibility = common.visibility });
 }
 
-fn __truncxfhf2(a: f80) callconv(.C) common.F16T {
-    return @bitCast(common.F16T, trunc_f80(f16, a));
+fn __truncxfhf2(a: f80) callconv(.C) common.F16T(f80) {
+    return @bitCast(common.F16T(f80), trunc_f80(f16, a));
 }
lib/zig.h
@@ -2873,6 +2873,12 @@ typedef zig_repr_f16 zig_f16;
 #undef zig_init_special_f16
 #define zig_init_special_f16(sign, name, arg, repr) repr
 #endif
+#if __APPLE__
+typedef zig_repr_f16 zig_compiler_rt_f16;
+#else
+typedef zig_f16 zig_compiler_rt_f16;
+#endif
+#define zig_compiler_rt_abbrev_zig_compiler_rt_f16 zig_compiler_rt_abbrev_zig_f16
 
 #define zig_has_f32 1
 #define zig_bitSizeOf_f32 32
@@ -3088,15 +3094,15 @@ zig_float_from_repr(f128)
 #define zig_convert_builtin(ResType, operation, ArgType, version) \
     zig_extern ResType zig_expand_concat(zig_expand_concat(zig_expand_concat(__##operation, \
         zig_compiler_rt_abbrev_##ArgType), zig_compiler_rt_abbrev_##ResType), version)(ArgType);
-zig_convert_builtin(zig_f16,  trunc,  zig_f32,  2)
-zig_convert_builtin(zig_f16,  trunc,  zig_f64,  2)
+zig_convert_builtin(zig_compiler_rt_f16,  trunc,  zig_f32,  2)
+zig_convert_builtin(zig_compiler_rt_f16,  trunc,  zig_f64,  2)
 zig_convert_builtin(zig_f16,  trunc,  zig_f80,  2)
 zig_convert_builtin(zig_f16,  trunc,  zig_f128, 2)
-zig_convert_builtin(zig_f32,  extend, zig_f16,  2)
+zig_convert_builtin(zig_f32,  extend, zig_compiler_rt_f16,  2)
 zig_convert_builtin(zig_f32,  trunc,  zig_f64,  2)
 zig_convert_builtin(zig_f32,  trunc,  zig_f80,  2)
 zig_convert_builtin(zig_f32,  trunc,  zig_f128, 2)
-zig_convert_builtin(zig_f64,  extend, zig_f16,  2)
+zig_convert_builtin(zig_f64,  extend, zig_compiler_rt_f16,  2)
 zig_convert_builtin(zig_f64,  extend, zig_f32,  2)
 zig_convert_builtin(zig_f64,  trunc,  zig_f80,  2)
 zig_convert_builtin(zig_f64,  trunc,  zig_f128, 2)