Skip to content

Commit

Permalink
builtins: overflow arithmetic perf optimization
Browse files Browse the repository at this point in the history
change signature of arithmetic operations @addwithOverflow,
@subWithOverflow, @mulWithOverflow, shlWithOverflow from
  @operation(comptime T: type, a: T, b: T, result: *T) bool
to
  @operation(comptime T: type, a: T, b: T) anytype
with anytype being a tuple
  struct { res: T, ov: bool }

This removes the pointer store and load for efficiency of codegen.
Comptime operation is accordingly kept in sync.

closes ziglang#10248
  • Loading branch information
matu3ba committed Feb 10, 2022
1 parent 57357c4 commit 5a100e7
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 33 deletions.
9 changes: 5 additions & 4 deletions src/Air.zig
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,11 @@ pub const Inst = struct {
/// is the same as both operands.
/// Uses the `bin_op` field.
min,
/// DEBUG
/// Integer addition with overflow. Both operands are guaranteed to be the same type,
/// and the result is bool. The wrapped value is written to the pointer given by the in
/// operand of the `pl_op` field. Payload is `Bin` with `lhs` and `rhs` the relevant types
/// of the operation.
/// and the result is a tuple with .{res, ov}. The wrapped value is written to res
/// and if an overflow happens, ov if true. Otherwise ov is false.
/// Payload is `Bin` with `lhs` and `rhs` the relevant types of the operation.
/// Uses the `pl_op` field with payload `Bin`.
add_with_overflow,
/// Integer subtraction with overflow. Both operands are guaranteed to be the same type,
Expand Down Expand Up @@ -934,7 +935,7 @@ pub fn typeOfIndex(air: Air, inst: Air.Inst.Index) Type {
.sub_with_overflow,
.mul_with_overflow,
.shl_with_overflow,
=> return Type.initTag(.bool),
=> return Type.initTag(.Struct),
}
}

Expand Down
22 changes: 0 additions & 22 deletions src/AstGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7186,23 +7186,12 @@ fn builtinCall(
.shl_with_overflow => {
const int_type = try typeExpr(gz, scope, params[0]);
const log2_int_type = try gz.addUnNode(.log2_int_type, int_type, params[0]);
const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
.ptr_type_simple = .{
.is_allowzero = false,
.is_mutable = true,
.is_volatile = false,
.size = .One,
.elem_type = int_type,
},
} });
const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);
const rhs = try expr(gz, scope, .{ .ty = log2_int_type }, params[2]);
const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[3]);
const result = try gz.addExtendedPayload(.shl_with_overflow, Zir.Inst.OverflowArithmetic{
.node = gz.nodeIndexToRelative(node),
.lhs = lhs,
.rhs = rhs,
.ptr = ptr,
});
return rvalue(gz, rl, result, node);
},
Expand Down Expand Up @@ -7596,23 +7585,12 @@ fn overflowArithmetic(
tag: Zir.Inst.Extended,
) InnerError!Zir.Inst.Ref {
const int_type = try typeExpr(gz, scope, params[0]);
const ptr_type = try gz.add(.{ .tag = .ptr_type_simple, .data = .{
.ptr_type_simple = .{
.is_allowzero = false,
.is_mutable = true,
.is_volatile = false,
.size = .One,
.elem_type = int_type,
},
} });
const lhs = try expr(gz, scope, .{ .ty = int_type }, params[1]);
const rhs = try expr(gz, scope, .{ .ty = int_type }, params[2]);
const ptr = try expr(gz, scope, .{ .ty = ptr_type }, params[3]);
const result = try gz.addExtendedPayload(tag, Zir.Inst.OverflowArithmetic{
.node = gz.nodeIndexToRelative(node),
.lhs = lhs,
.rhs = rhs,
.ptr = ptr,
});
return rvalue(gz, rl, result, node);
}
Expand Down
14 changes: 10 additions & 4 deletions src/BuiltinFn.zig
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,13 @@ pub const list = list: {
@setEvalBranchQuota(3000);
break :list std.ComptimeStringMap(@This(), .{
.{
// changed from @addWithOverflow(comptime T: type, a: T, b: T, result: *T) bool //DEBUG
// to 1. RetType(ST) = struct {res: T, flag: u8}; //DEBUG
// 2. @addWithOverflow(comptime T: type, a: T, b: T) RetType //DEBUG
"@addWithOverflow",
.{
.tag = .add_with_overflow,
.param_count = 4,
.param_count = 3,
},
},
.{
Expand Down Expand Up @@ -596,10 +599,11 @@ pub const list = list: {
},
},
.{
// see @addWithOverflow // DEBUG
"@mulWithOverflow",
.{
.tag = .mul_with_overflow,
.param_count = 4,
.param_count = 3,
},
},
.{
Expand Down Expand Up @@ -701,10 +705,11 @@ pub const list = list: {
},
},
.{
// see @addWithOverflow // DEBUG
"@shlWithOverflow",
.{
.tag = .shl_with_overflow,
.param_count = 4,
.param_count = 3,
},
},
.{
Expand Down Expand Up @@ -842,10 +847,11 @@ pub const list = list: {
},
},
.{
// see @addWithOverflow // DEBUG
"@subWithOverflow",
.{
.tag = .sub_with_overflow,
.param_count = 4,
.param_count = 3,
},
},
.{
Expand Down
1 change: 0 additions & 1 deletion src/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2843,7 +2843,6 @@ pub const Inst = struct {
node: i32,
lhs: Ref,
rhs: Ref,
ptr: Ref,
};

pub const Cmpxchg = struct {
Expand Down
2 changes: 0 additions & 2 deletions src/print_zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1076,8 +1076,6 @@ const Writer = struct {
try self.writeInstRef(stream, extra.lhs);
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.rhs);
try stream.writeAll(", ");
try self.writeInstRef(stream, extra.ptr);
try stream.writeAll(")) ");
try self.writeSrc(stream, src);
}
Expand Down

0 comments on commit 5a100e7

Please sign in to comment.