authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-10-15 19:08:08+02:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-10-16 15:54:17+02:00
logff1cab037c1a770fba558b9d888a01a5b71190b8
tree6473822c58adea83afda16b69fc720000385e07f
parent273b8e20ca086b62debc869e1c375cd834043e24
signaturelock-open Commit is signed but in an unrecognized format.

wasm: re-use operands

When we return an operand directly as a result, we must call `reuseOperand`. This commit ensures it's done for all currently- implemented AIR instructions.

1 files changed, 16 insertions(+), 14 deletions(-)

src/arch/wasm/CodeGen.zig+16-14
...@@ -3403,7 +3403,7 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In...@@ -3403,7 +3403,7 @@ fn airUnwrapErrUnionError(self: *Self, inst: Air.Inst.Index, op_is_ptr: bool) In
3403 }3403 }
34043404
3405 if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime()) {3405 if (op_is_ptr or !payload_ty.hasRuntimeBitsIgnoreComptime()) {
3406 break :result operand;3406 break :result self.reuseOperand(ty_op.operand, operand);
3407 }3407 }
34083408
3409 const error_val = try self.load(operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(payload_ty, self.target)));3409 const error_val = try self.load(operand, Type.anyerror, @intCast(u32, errUnionErrorOffset(payload_ty, self.target)));
...@@ -3422,7 +3422,7 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -3422,7 +3422,7 @@ fn airWrapErrUnionPayload(self: *Self, inst: Air.Inst.Index) InnerError!void {
3422 const pl_ty = self.air.typeOf(ty_op.operand);3422 const pl_ty = self.air.typeOf(ty_op.operand);
3423 const result = result: {3423 const result = result: {
3424 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {3424 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
3425 break :result operand;3425 break :result self.reuseOperand(ty_op.operand, operand);
3426 }3426 }
34273427
3428 const err_union = try self.allocStack(err_ty);3428 const err_union = try self.allocStack(err_ty);
...@@ -3449,7 +3449,7 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -3449,7 +3449,7 @@ fn airWrapErrUnionErr(self: *Self, inst: Air.Inst.Index) InnerError!void {
34493449
3450 const result = result: {3450 const result = result: {
3451 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {3451 if (!pl_ty.hasRuntimeBitsIgnoreComptime()) {
3452 break :result operand;3452 break :result self.reuseOperand(ty_op.operand, operand);
3453 }3453 }
34543454
3455 const err_union = try self.allocStack(err_ty);3455 const err_union = try self.allocStack(err_ty);
...@@ -3481,7 +3481,7 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -3481,7 +3481,7 @@ fn airIntcast(self: *Self, inst: Air.Inst.Index) InnerError!void {
3481 }3481 }
34823482
3483 const result = try (try self.intcast(operand, operand_ty, ty)).toLocal(self, ty);3483 const result = try (try self.intcast(operand, operand_ty, ty)).toLocal(self, ty);
3484 self.finishAir(inst, result, &.{ty_op.operand});3484 self.finishAir(inst, result, &.{});
3485}3485}
34863486
3487/// Upcasts or downcasts an integer based on the given and wanted types,3487/// Upcasts or downcasts an integer based on the given and wanted types,
...@@ -3579,7 +3579,7 @@ fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -3579,7 +3579,7 @@ fn airOptionalPayload(self: *Self, inst: Air.Inst.Index) InnerError!void {
35793579
3580 const result = result: {3580 const result = result: {
3581 const operand = try self.resolveInst(ty_op.operand);3581 const operand = try self.resolveInst(ty_op.operand);
3582 if (opt_ty.optionalReprIsPayload()) break :result operand;3582 if (opt_ty.optionalReprIsPayload()) break :result self.reuseOperand(ty_op.operand, operand);
35833583
3584 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);3584 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
35853585
...@@ -3603,7 +3603,7 @@ fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -3603,7 +3603,7 @@ fn airOptionalPayloadPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
3603 var buf: Type.Payload.ElemType = undefined;3603 var buf: Type.Payload.ElemType = undefined;
3604 const payload_ty = opt_ty.optionalChild(&buf);3604 const payload_ty = opt_ty.optionalChild(&buf);
3605 if (!payload_ty.hasRuntimeBitsIgnoreComptime() or opt_ty.optionalReprIsPayload()) {3605 if (!payload_ty.hasRuntimeBitsIgnoreComptime() or opt_ty.optionalReprIsPayload()) {
3606 break :result operand;3606 break :result self.reuseOperand(ty_op.operand, operand);
3607 }3607 }
36083608
3609 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);3609 const offset = opt_ty.abiSize(self.target) - payload_ty.abiSize(self.target);
...@@ -3656,7 +3656,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -3656,7 +3656,7 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) InnerError!void {
3656 const operand = try self.resolveInst(ty_op.operand);3656 const operand = try self.resolveInst(ty_op.operand);
3657 const op_ty = self.air.typeOfIndex(inst);3657 const op_ty = self.air.typeOfIndex(inst);
3658 if (op_ty.optionalReprIsPayload()) {3658 if (op_ty.optionalReprIsPayload()) {
3659 break :result operand;3659 break :result self.reuseOperand(ty_op.operand, operand);
3660 }3660 }
3661 const offset = std.math.cast(u32, op_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) orelse {3661 const offset = std.math.cast(u32, op_ty.abiSize(self.target) - payload_ty.abiSize(self.target)) orelse {
3662 const module = self.bin_file.base.options.module.?;3662 const module = self.bin_file.base.options.module.?;
...@@ -3793,8 +3793,10 @@ fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -3793,8 +3793,10 @@ fn airBoolToInt(self: *Self, inst: Air.Inst.Index) InnerError!void {
3793 const un_op = self.air.instructions.items(.data)[inst].un_op;3793 const un_op = self.air.instructions.items(.data)[inst].un_op;
3794 const result = if (self.liveness.isUnused(inst))3794 const result = if (self.liveness.isUnused(inst))
3795 WValue{ .none = {} }3795 WValue{ .none = {} }
3796 else3796 else result: {
3797 try self.resolveInst(un_op);3797 const operand = try self.resolveInst(un_op);
3798 break :result self.reuseOperand(un_op, operand);
3799 };
37983800
3799 self.finishAir(inst, result, &.{un_op});3801 self.finishAir(inst, result, &.{un_op});
3800}3802}
...@@ -3939,7 +3941,7 @@ fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -3939,7 +3941,7 @@ fn airMemset(self: *Self, inst: Air.Inst.Index) InnerError!void {
3939 const len = try self.resolveInst(bin_op.rhs);3941 const len = try self.resolveInst(bin_op.rhs);
3940 try self.memset(ptr, len, value);3942 try self.memset(ptr, len, value);
39413943
3942 self.finishAir(inst, .none, &.{ bin_op.lhs, bin_op.rhs });3944 self.finishAir(inst, .none, &.{pl_op.operand});
3943}3945}
39443946
3945/// Sets a region of memory at `ptr` to the value of `value`3947/// Sets a region of memory at `ptr` to the value of `value`
...@@ -4489,7 +4491,7 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -4489,7 +4491,7 @@ fn airErrUnionPayloadPtrSet(self: *Self, inst: Air.Inst.Index) InnerError!void {
4489 if (self.liveness.isUnused(inst)) break :result WValue{ .none = {} };4491 if (self.liveness.isUnused(inst)) break :result WValue{ .none = {} };
44904492
4491 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {4493 if (!payload_ty.hasRuntimeBitsIgnoreComptime()) {
4492 break :result operand;4494 break :result self.reuseOperand(ty_op.operand, operand);
4493 }4495 }
44944496
4495 break :result try self.buildPointerOffset(operand, @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)), .new);4497 break :result try self.buildPointerOffset(operand, @intCast(u32, errUnionPayloadOffset(payload_ty, self.target)), .new);
...@@ -4513,7 +4515,7 @@ fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -4513,7 +4515,7 @@ fn airFieldParentPtr(self: *Self, inst: Air.Inst.Index) InnerError!void {
4513 try self.addTag(.i32_sub);4515 try self.addTag(.i32_sub);
4514 try self.addLabel(.local_set, base.local.value);4516 try self.addLabel(.local_set, base.local.value);
4515 break :result base;4517 break :result base;
4516 } else field_ptr;4518 } else self.reuseOperand(extra.field_ptr, field_ptr);
45174519
4518 self.finishAir(inst, result, &.{extra.field_ptr});4520 self.finishAir(inst, result, &.{extra.field_ptr});
4519}4521}
...@@ -4526,7 +4528,7 @@ fn airMemcpy(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -4526,7 +4528,7 @@ fn airMemcpy(self: *Self, inst: Air.Inst.Index) InnerError!void {
4526 const len = try self.resolveInst(bin_op.rhs);4528 const len = try self.resolveInst(bin_op.rhs);
4527 try self.memcpy(dst, src, len);4529 try self.memcpy(dst, src, len);
45284530
4529 self.finishAir(inst, .none, &.{ pl_op.operand, bin_op.lhs, bin_op.rhs });4531 self.finishAir(inst, .none, &.{pl_op.operand});
4530}4532}
45314533
4532fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!void {4534fn airPopcount(self: *Self, inst: Air.Inst.Index) InnerError!void {
...@@ -5220,7 +5222,7 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!void {...@@ -5220,7 +5222,7 @@ fn airByteSwap(self: *Self, inst: Air.Inst.Index) InnerError!void {
52205222
5221 // bytes are no-op5223 // bytes are no-op
5222 if (int_info.bits == 8) {5224 if (int_info.bits == 8) {
5223 return self.finishAir(inst, operand, &.{ty_op.operand});5225 return self.finishAir(inst, self.reuseOperand(ty_op.operand, operand), &.{ty_op.operand});
5224 }5226 }
52255227
5226 const result = result: {5228 const result = result: {