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) {...@@ -6839,7 +6839,7 @@ pub const FastMath = packed struct(u8) {
6839 .arcp = true,6839 .arcp = true,
6840 .contract = true,6840 .contract = true,
6841 .afn = true,6841 .afn = true,
6842 .realloc = true,6842 .reassoc = true,
6843 };6843 };
6844};6844};
68456845
...@@ -14721,13 +14721,13 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14721,13 +14721,13 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14721 try function_block.writeAbbrevAdapted(FunctionBlock.CallFast{14721 try function_block.writeAbbrevAdapted(FunctionBlock.CallFast{
14722 .attributes = extra.data.attributes,14722 .attributes = extra.data.attributes,
14723 .call_type = switch (kind) {14723 .call_type = switch (kind) {
14724 .call => .{ .call_conv = call_conv },14724 .@"call fast" => .{ .call_conv = call_conv },
14725 .@"tail call" => .{ .tail = true, .call_conv = call_conv },14725 .@"tail call fast" => .{ .tail = true, .call_conv = call_conv },
14726 .@"musttail call" => .{ .must_tail = true, .call_conv = call_conv },14726 .@"musttail call fast" => .{ .must_tail = true, .call_conv = call_conv },
14727 .@"notail call" => .{ .no_tail = true, .call_conv = call_conv },14727 .@"notail call fast" => .{ .no_tail = true, .call_conv = call_conv },
14728 else => unreachable,14728 else => unreachable,
14729 },14729 },
14730 .fast_math = .{},14730 .fast_math = FastMath.fast,
14731 .type_id = extra.data.ty,14731 .type_id = extra.data.ty,
14732 .callee = extra.data.callee,14732 .callee = extra.data.callee,
14733 .args = args,14733 .args = args,
...@@ -14786,7 +14786,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14786,7 +14786,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14786 .opcode = kind.toBinaryOpcode(),14786 .opcode = kind.toBinaryOpcode(),
14787 .lhs = adapter.getOffsetValueIndex(extra.lhs),14787 .lhs = adapter.getOffsetValueIndex(extra.lhs),
14788 .rhs = adapter.getOffsetValueIndex(extra.rhs),14788 .rhs = adapter.getOffsetValueIndex(extra.rhs),
14789 .fast_math = .{},14789 .fast_math = FastMath.fast,
14790 });14790 });
14791 },14791 },
14792 .alloca,14792 .alloca,
...@@ -14884,7 +14884,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14884,7 +14884,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14884 .lhs = adapter.getOffsetValueIndex(extra.lhs),14884 .lhs = adapter.getOffsetValueIndex(extra.lhs),
14885 .rhs = adapter.getOffsetValueIndex(extra.rhs),14885 .rhs = adapter.getOffsetValueIndex(extra.rhs),
14886 .pred = kind.toCmpPredicate(),14886 .pred = kind.toCmpPredicate(),
14887 .fast_math = .{},14887 .fast_math = FastMath.fast,
14888 });14888 });
14889 },14889 },
14890 .fneg => try function_block.writeAbbrev(FunctionBlock.FNeg{14890 .fneg => try function_block.writeAbbrev(FunctionBlock.FNeg{
...@@ -14892,7 +14892,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co...@@ -14892,7 +14892,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14892 }),14892 }),
14893 .@"fneg fast" => try function_block.writeAbbrev(FunctionBlock.FNegFast{14893 .@"fneg fast" => try function_block.writeAbbrev(FunctionBlock.FNegFast{
14894 .val = adapter.getOffsetValueIndex(@enumFromInt(datas[instr_index])),14894 .val = adapter.getOffsetValueIndex(@enumFromInt(datas[instr_index])),
14895 .fast_math = .{},14895 .fast_math = FastMath.fast,
14896 }),14896 }),
14897 .extractvalue => {14897 .extractvalue => {
14898 var extra = func.extraDataTrail(Function.Instruction.ExtractValue, datas[instr_index]);14898 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...@@ -14940,7 +14940,7 @@ pub fn toBitcode(self: *Builder, allocator: Allocator) bitcode_writer.Error![]co
14940 .lhs = adapter.getOffsetValueIndex(extra.lhs),14940 .lhs = adapter.getOffsetValueIndex(extra.lhs),
14941 .rhs = adapter.getOffsetValueIndex(extra.rhs),14941 .rhs = adapter.getOffsetValueIndex(extra.rhs),
14942 .cond = adapter.getOffsetValueIndex(extra.cond),14942 .cond = adapter.getOffsetValueIndex(extra.cond),
14943 .fast_math = .{},14943 .fast_math = FastMath.fast,
14944 });14944 });
14945 },14945 },
14946 .shufflevector => {14946 .shufflevector => {
test/behavior/floatop.zig+21
...@@ -1636,3 +1636,24 @@ test "runtime isNan(inf * 0)" {...@@ -1636,3 +1636,24 @@ test "runtime isNan(inf * 0)" {
1636 const zero_times_inf = 0 * std.math.inf(f64);1636 const zero_times_inf = 0 * std.math.inf(f64);
1637 try std.testing.expect(std.math.isNan(zero_times_inf));1637 try std.testing.expect(std.math.isNan(zero_times_inf));
1638}1638}
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//