authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-09-17 20:57:00+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-23 12:36:56-07:00
log66b1f6c163e5fa3fddd3361a1ff4b64b3cbfc6bf
tree5b5bd86e6788df781f68f2a18115c9e5b67d79b8
parent749307dbb260886433f366b3fdf9baf8f5b2ae30

spirv: air sub_with_overflow


2 files changed, 26 insertions(+), 10 deletions(-)

src/codegen/spirv.zig+26-9
......@@ -1658,7 +1658,8 @@ pub const DeclGen = struct {
16581658 .rem_optimized,
16591659 => try self.airArithOp(inst, .OpFRem, .OpSRem, .OpSRem, false),
16601660
1661 .add_with_overflow => try self.airOverflowArithOp(inst),
1661 .add_with_overflow => try self.airAddSubOverflow(inst, .OpIAdd, .OpULessThan, .OpSLessThan),
1662 .sub_with_overflow => try self.airAddSubOverflow(inst, .OpISub, .OpUGreaterThan, .OpSGreaterThan),
16621663
16631664 .shuffle => try self.airShuffle(inst),
16641665
......@@ -1878,7 +1879,13 @@ pub const DeclGen = struct {
18781879 return result_id;
18791880 }
18801881
1881 fn airOverflowArithOp(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
1882 fn airAddSubOverflow(
1883 self: *DeclGen,
1884 inst: Air.Inst.Index,
1885 comptime add: Opcode,
1886 comptime ucmp: Opcode,
1887 comptime scmp: Opcode,
1888 ) !?IdRef {
18821889 if (self.liveness.isUnused(inst)) return null;
18831890
18841891 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
......@@ -1910,7 +1917,7 @@ pub const DeclGen = struct {
19101917
19111918 // TODO: Operations other than addition.
19121919 const value_id = self.spv.allocId();
1913 try self.func.body.emit(self.spv.gpa, .OpIAdd, .{
1920 try self.func.body.emit(self.spv.gpa, add, .{
19141921 .id_result_type = operand_ty_id,
19151922 .id_result = value_id,
19161923 .operand_1 = lhs,
......@@ -1920,8 +1927,9 @@ pub const DeclGen = struct {
19201927 const overflowed_id = switch (info.signedness) {
19211928 .unsigned => blk: {
19221929 // Overflow happened if the result is smaller than either of the operands. It doesn't matter which.
1930 // For subtraction the conditions need to be swapped.
19231931 const overflowed_id = self.spv.allocId();
1924 try self.func.body.emit(self.spv.gpa, .OpULessThan, .{
1932 try self.func.body.emit(self.spv.gpa, ucmp, .{
19251933 .id_result_type = self.typeId(bool_ty_ref),
19261934 .id_result = overflowed_id,
19271935 .operand_1 = value_id,
......@@ -1930,13 +1938,22 @@ pub const DeclGen = struct {
19301938 break :blk overflowed_id;
19311939 },
19321940 .signed => blk: {
1933 // Overflow happened if:
1941 // lhs - rhs
1942 // For addition, overflow happened if:
19341943 // - rhs is negative and value > lhs
19351944 // - rhs is positive and value < lhs
19361945 // This can be shortened to:
1937 // (rhs < 0 && value > lhs) || (rhs >= 0 && value <= lhs)
1946 // (rhs < 0 and value > lhs) or (rhs >= 0 and value <= lhs)
19381947 // = (rhs < 0) == (value > lhs)
1948 // = (rhs < 0) == (lhs < value)
19391949 // Note that signed overflow is also wrapping in spir-v.
1950 // For subtraction, overflow happened if:
1951 // - rhs is negative and value < lhs
1952 // - rhs is positive and value > lhs
1953 // This can be shortened to:
1954 // (rhs < 0 and value < lhs) or (rhs >= 0 and value >= lhs)
1955 // = (rhs < 0) == (value < lhs)
1956 // = (rhs < 0) == (lhs > value)
19401957
19411958 const rhs_lt_zero_id = self.spv.allocId();
19421959 const zero_id = try self.constInt(operand_ty_ref, 0);
......@@ -1948,11 +1965,11 @@ pub const DeclGen = struct {
19481965 });
19491966
19501967 const value_gt_lhs_id = self.spv.allocId();
1951 try self.func.body.emit(self.spv.gpa, .OpSGreaterThan, .{
1968 try self.func.body.emit(self.spv.gpa, scmp, .{
19521969 .id_result_type = self.typeId(bool_ty_ref),
19531970 .id_result = value_gt_lhs_id,
1954 .operand_1 = value_id,
1955 .operand_2 = lhs,
1971 .operand_1 = lhs,
1972 .operand_2 = value_id,
19561973 });
19571974
19581975 const overflowed_id = self.spv.allocId();
test/behavior/math.zig-1
......@@ -1020,7 +1020,6 @@ test "@subWithOverflow" {
10201020 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
10211021 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10221022 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1023 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
10241023
10251024 {
10261025 var a: u8 = 1;