From 5870ffeb82e93ab2a35f9153668d170eced25cbc Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Mon, 27 Mar 2023 21:32:37 -0400 Subject: [PATCH] compiler_rt: change the abi of f16 on mac to depend on the other type --- lib/compiler_rt/common.zig | 20 ++++++++++++++------ lib/compiler_rt/extendf_test.zig | 4 ++-- lib/compiler_rt/extendhfdf2.zig | 2 +- lib/compiler_rt/extendhfsf2.zig | 4 ++-- lib/compiler_rt/extendhftf2.zig | 2 +- lib/compiler_rt/extendhfxf2.zig | 2 +- lib/compiler_rt/truncdfhf2.zig | 6 +++--- lib/compiler_rt/truncsfhf2.zig | 10 +++++----- lib/compiler_rt/trunctfhf2.zig | 4 ++-- lib/compiler_rt/truncxfhf2.zig | 4 ++-- lib/zig.h | 14 ++++++++++---- 11 files changed, 43 insertions(+), 29 deletions(-) diff --git a/lib/compiler_rt/common.zig b/lib/compiler_rt/common.zig index ca76f0663a00d4ac3d05779df6d963a10912a25f..ec4f545f37879c8c8492cc16c2fb7f3a3c8d8191 100644 --- a/lib/compiler_rt/common.zig +++ b/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) { diff --git a/lib/compiler_rt/extendf_test.zig b/lib/compiler_rt/extendf_test.zig index 9857cf91f9ad478db03f63f69bd1751130363caf..e9192ae5250cbbf7400c95b2830d165a3b5ab961 100644 --- a/lib/compiler_rt/extendf_test.zig +++ b/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); diff --git a/lib/compiler_rt/extendhfdf2.zig b/lib/compiler_rt/extendhfdf2.zig index 97311b104c6daca1c8b48046c500a65cb0933e11..1a95002883fbd484adaa3eb874e079154be2e628 100644 --- a/lib/compiler_rt/extendhfdf2.zig +++ b/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)); } diff --git a/lib/compiler_rt/extendhfsf2.zig b/lib/compiler_rt/extendhfsf2.zig index 41d9282e8ae3e2efef411d04d2950192efd27794..86ef751c3590ddfeb141c6548cc4cd1e4c4e5606 100644 --- a/lib/compiler_rt/extendhfsf2.zig +++ b/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)); } diff --git a/lib/compiler_rt/extendhftf2.zig b/lib/compiler_rt/extendhftf2.zig index 2d6307af350606ad2cbffd155a05a84fc73bda10..6479a0f52f65a25f2c8c275eb8bd966e99a45877 100644 --- a/lib/compiler_rt/extendhftf2.zig +++ b/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)); } diff --git a/lib/compiler_rt/extendhfxf2.zig b/lib/compiler_rt/extendhfxf2.zig index 1f9c43328f200c6e100ee303fdb2b887ed194a09..bd685827665af6aec3b6d2aed04ca96ed5b3f53f 100644 --- a/lib/compiler_rt/extendhfxf2.zig +++ b/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)); } diff --git a/lib/compiler_rt/truncdfhf2.zig b/lib/compiler_rt/truncdfhf2.zig index a8ec9bb0a5750103c784573793f057ef8620def4..e76ad2ce62ef5b1d418a190515237d95c823fc31 100644 --- a/lib/compiler_rt/truncdfhf2.zig +++ b/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)); } diff --git a/lib/compiler_rt/truncsfhf2.zig b/lib/compiler_rt/truncsfhf2.zig index 80b78c2452adbce67d5959d13534daf921688942..77dd0ba642d5f8d8f9698b5c0e0a4c61c133d1ab 100644 --- a/lib/compiler_rt/truncsfhf2.zig +++ b/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)); } diff --git a/lib/compiler_rt/trunctfhf2.zig b/lib/compiler_rt/trunctfhf2.zig index e963753950af2cd993908b1125c4ef2582beb302..e9cc19da188b0393583bd501dfdead170dec21b8 100644 --- a/lib/compiler_rt/trunctfhf2.zig +++ b/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)); } diff --git a/lib/compiler_rt/truncxfhf2.zig b/lib/compiler_rt/truncxfhf2.zig index 80a0dd69febcd44a09c952aa0ac4cbcb62cbdd93..31965d3e2a359bc0b89a74cb266e8a0e7cff3fb0 100644 --- a/lib/compiler_rt/truncxfhf2.zig +++ b/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)); } diff --git a/lib/zig.h b/lib/zig.h index e2e46a041873c14d5d1adaa31d4b571a338ffd4d..519928dcb26d0b15573a9b52303fba0b363d2040 100644 --- a/lib/zig.h +++ b/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) -- 2.54.0