authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2024-03-18 04:27:39+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-03-18 04:27:39+02:00
log64173dadcacdcad9b7f5898fee2b00f2cf177ead
treed848615ca50e00493863ad724d1555011aef3b1d
parent54f6e74cda07a1153fd205afc8973665397f6cc7
parentaa03ec8001f98ac36e1705c92191246be53cf31b
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #19334 from antlilja/llvm-fast-math

Fix setFloatMode in LLVM backend

3 files changed, 43 insertions(+), 10 deletions(-)

src/codegen/llvm/Builder.zig+10-10
......@@ -6839,7 +6839,7 @@ pub const FastMath = packed struct(u8) {
68396839 .arcp = true,
68406840 .contract = true,
68416841 .afn = true,
6842 .realloc = true,
6842 .reassoc = true,
68436843 };
68446844};
68456845
......@@ -14721,13 +14721,13 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1472114721 try function_block.writeAbbrevAdapted(FunctionBlock.CallFast{
1472214722 .attributes = extra.data.attributes,
1472314723 .call_type = switch (kind) {
14724 .call => .{ .call_conv = call_conv },
14725 .@"tail call" => .{ .tail = true, .call_conv = call_conv },
14726 .@"musttail call" => .{ .must_tail = true, .call_conv = call_conv },
14727 .@"notail call" => .{ .no_tail = true, .call_conv = call_conv },
14724 .@"call fast" => .{ .call_conv = call_conv },
14725 .@"tail call fast" => .{ .tail = true, .call_conv = call_conv },
14726 .@"musttail call fast" => .{ .must_tail = true, .call_conv = call_conv },
14727 .@"notail call fast" => .{ .no_tail = true, .call_conv = call_conv },
1472814728 else => unreachable,
1472914729 },
14730 .fast_math = .{},
14730 .fast_math = FastMath.fast,
1473114731 .type_id = extra.data.ty,
1473214732 .callee = extra.data.callee,
1473314733 .args = args,
......@@ -14786,7 +14786,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1478614786 .opcode = kind.toBinaryOpcode(),
1478714787 .lhs = adapter.getOffsetValueIndex(extra.lhs),
1478814788 .rhs = adapter.getOffsetValueIndex(extra.rhs),
14789 .fast_math = .{},
14789 .fast_math = FastMath.fast,
1479014790 });
1479114791 },
1479214792 .alloca,
......@@ -14884,7 +14884,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1488414884 .lhs = adapter.getOffsetValueIndex(extra.lhs),
1488514885 .rhs = adapter.getOffsetValueIndex(extra.rhs),
1488614886 .pred = kind.toCmpPredicate(),
14887 .fast_math = .{},
14887 .fast_math = FastMath.fast,
1488814888 });
1488914889 },
1489014890 .fneg => try function_block.writeAbbrev(FunctionBlock.FNeg{
......@@ -14892,7 +14892,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1489214892 }),
1489314893 .@"fneg fast" => try function_block.writeAbbrev(FunctionBlock.FNegFast{
1489414894 .val = adapter.getOffsetValueIndex(@enumFromInt(datas[instr_index])),
14895 .fast_math = .{},
14895 .fast_math = FastMath.fast,
1489614896 }),
1489714897 .extractvalue => {
1489814898 var extra = func.extraDataTrail(Function.Instruction.ExtractValue, datas[instr_index]);
......@@ -14940,7 +14940,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
1494014940 .lhs = adapter.getOffsetValueIndex(extra.lhs),
1494114941 .rhs = adapter.getOffsetValueIndex(extra.rhs),
1494214942 .cond = adapter.getOffsetValueIndex(extra.cond),
14943 .fast_math = .{},
14943 .fast_math = FastMath.fast,
1494414944 });
1494514945 },
1494614946 .shufflevector => {
test/behavior/floatop.zig+21
......@@ -1636,3 +1636,24 @@ test "runtime isNan(inf * 0)" {
16361636 const zero_times_inf = 0 * std.math.inf(f64);
16371637 try std.testing.expect(std.math.isNan(zero_times_inf));
16381638}
1639
1640test "optimized float mode" {
1641 if (builtin.mode == .Debug) return error.SkipZigTest;
1642
1643 const big = 0x1p40;
1644 const small = 0.001;
1645 const tiny = 0x1p-10;
1646
1647 const S = struct {
1648 fn strict(x: f64) f64 {
1649 @setFloatMode(.strict);
1650 return x + big - big;
1651 }
1652 fn optimized(x: f64) f64 {
1653 @setFloatMode(.optimized);
1654 return x + big - big;
1655 }
1656 };
1657 try expect(S.optimized(small) == small);
1658 try expect(S.strict(small) == tiny);
1659}
test/cases/float_mode_optimized_reduce.zig created+12
......@@ -0,0 +1,12 @@
1pub fn main() void {
2 var a: @Vector(2, f32) = @splat(5.0);
3 _ = &a;
4
5 @setFloatMode(.optimized);
6 var b = @reduce(.Add, a);
7 _ = &b;
8}
9
10// run
11// backend=llvm
12//