authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-19 22:43:59-04:00
committergravatar for kubkon@jakubkonka.comJakub Konka <kubkon@jakubkonka.com> 2023-03-21 08:49:54+01:00
log3f4569bf187bfe296323aee6fbb59ab374041243
tree8a8a92c78f3c248774932daf7351c4a185fcb308
parent6c453dd806d9a0207b1f1e64adfc38eca0c38f16

codegen: fix backend breakage due to optional layout change


3 files changed, 65 insertions(+), 67 deletions(-)

src/arch/aarch64/CodeGen.zig+46-36
......@@ -3011,41 +3011,16 @@ fn optionalPayload(self: *Self, inst: Air.Inst.Index, mcv: MCValue, optional_ty:
30113011 return MCValue{ .register = reg };
30123012 }
30133013
3014 const offset = @intCast(u32, optional_ty.abiSize(self.target.*) - payload_ty.abiSize(self.target.*));
30153014 switch (mcv) {
3016 .register => |source_reg| {
3015 .register => {
30173016 // TODO should we reuse the operand here?
30183017 const raw_reg = try self.register_manager.allocReg(inst, gp);
30193018 const dest_reg = raw_reg.toX();
30203019
3021 const shift = @intCast(u6, offset * 8);
3022 if (shift == 0) {
3023 try self.genSetReg(payload_ty, dest_reg, mcv);
3024 } else {
3025 _ = try self.addInst(.{
3026 .tag = if (payload_ty.isSignedInt())
3027 Mir.Inst.Tag.asr_immediate
3028 else
3029 Mir.Inst.Tag.lsr_immediate,
3030 .data = .{ .rr_shift = .{
3031 .rd = dest_reg,
3032 .rn = source_reg.toX(),
3033 .shift = shift,
3034 } },
3035 });
3036 }
3037
3020 try self.genSetReg(payload_ty, dest_reg, mcv);
30383021 return MCValue{ .register = self.registerAlias(dest_reg, payload_ty) };
30393022 },
3040 .stack_argument_offset => |off| {
3041 return MCValue{ .stack_argument_offset = off + offset };
3042 },
3043 .stack_offset => |off| {
3044 return MCValue{ .stack_offset = off - offset };
3045 },
3046 .memory => |addr| {
3047 return MCValue{ .memory = addr + offset };
3048 },
3023 .stack_argument_offset, .stack_offset, .memory => return mcv,
30493024 else => unreachable, // invalid MCValue for an error union
30503025 }
30513026}
......@@ -3289,12 +3264,11 @@ fn airWrapOptional(self: *Self, inst: Air.Inst.Index) !void {
32893264
32903265 const optional_abi_size = @intCast(u32, optional_ty.abiSize(self.target.*));
32913266 const optional_abi_align = optional_ty.abiAlignment(self.target.*);
3292 const payload_abi_size = @intCast(u32, payload_ty.abiSize(self.target.*));
3293 const offset = optional_abi_size - payload_abi_size;
3267 const offset = @intCast(u32, payload_ty.abiSize(self.target.*));
32943268
32953269 const stack_offset = try self.allocMem(optional_abi_size, optional_abi_align, inst);
3296 try self.genSetStack(Type.bool, stack_offset, .{ .immediate = 1 });
3297 try self.genSetStack(payload_ty, stack_offset - @intCast(u32, offset), operand);
3270 try self.genSetStack(payload_ty, stack_offset, operand);
3271 try self.genSetStack(Type.bool, stack_offset - offset, .{ .immediate = 1 });
32983272
32993273 break :result MCValue{ .stack_offset = stack_offset };
33003274 };
......@@ -4834,13 +4808,49 @@ fn airCondBr(self: *Self, inst: Air.Inst.Index) !void {
48344808}
48354809
48364810fn isNull(self: *Self, operand_bind: ReadArg.Bind, operand_ty: Type) !MCValue {
4837 const sentinel_ty: Type = if (!operand_ty.isPtrLikeOptional()) blk: {
4811 const sentinel: struct { ty: Type, bind: ReadArg.Bind } = if (!operand_ty.isPtrLikeOptional()) blk: {
48384812 var buf: Type.Payload.ElemType = undefined;
48394813 const payload_ty = operand_ty.optionalChild(&buf);
4840 break :blk if (payload_ty.hasRuntimeBitsIgnoreComptime()) Type.bool else operand_ty;
4841 } else operand_ty;
4814 if (!payload_ty.hasRuntimeBitsIgnoreComptime())
4815 break :blk .{ .ty = operand_ty, .bind = operand_bind };
4816
4817 const offset = @intCast(u32, payload_ty.abiSize(self.target.*));
4818 const operand_mcv = try operand_bind.resolveToMcv(self);
4819 const new_mcv: MCValue = switch (operand_mcv) {
4820 .register => |source_reg| new: {
4821 // TODO should we reuse the operand here?
4822 const raw_reg = try self.register_manager.allocReg(null, gp);
4823 const dest_reg = raw_reg.toX();
4824
4825 const shift = @intCast(u6, offset * 8);
4826 if (shift == 0) {
4827 try self.genSetReg(payload_ty, dest_reg, operand_mcv);
4828 } else {
4829 _ = try self.addInst(.{
4830 .tag = if (payload_ty.isSignedInt())
4831 Mir.Inst.Tag.asr_immediate
4832 else
4833 Mir.Inst.Tag.lsr_immediate,
4834 .data = .{ .rr_shift = .{
4835 .rd = dest_reg,
4836 .rn = source_reg.toX(),
4837 .shift = shift,
4838 } },
4839 });
4840 }
4841
4842 break :new .{ .register = self.registerAlias(dest_reg, payload_ty) };
4843 },
4844 .stack_argument_offset => |off| .{ .stack_argument_offset = off + offset },
4845 .stack_offset => |off| .{ .stack_offset = off - offset },
4846 .memory => |addr| .{ .memory = addr + offset },
4847 else => unreachable, // invalid MCValue for an optional
4848 };
4849
4850 break :blk .{ .ty = Type.bool, .bind = .{ .mcv = new_mcv } };
4851 } else .{ .ty = operand_ty, .bind = operand_bind };
48424852 const imm_bind: ReadArg.Bind = .{ .mcv = .{ .immediate = 0 } };
4843 return self.cmp(operand_bind, imm_bind, sentinel_ty, .eq);
4853 return self.cmp(sentinel.bind, imm_bind, sentinel.ty, .eq);
48444854}
48454855
48464856fn isNonNull(self: *Self, operand_bind: ReadArg.Bind, operand_ty: Type) !MCValue {
src/arch/wasm/CodeGen.zig+18-31
......@@ -2715,20 +2715,7 @@ fn lowerParentPtr(func: *CodeGen, ptr_val: Value, ptr_child_ty: Type) InnerError
27152715 },
27162716 .opt_payload_ptr => {
27172717 const payload_ptr = ptr_val.castTag(.opt_payload_ptr).?.data;
2718 const parent_ptr = try func.lowerParentPtr(payload_ptr.container_ptr, payload_ptr.container_ty);
2719 var buf: Type.Payload.ElemType = undefined;
2720 const payload_ty = payload_ptr.container_ty.optionalChild(&buf);
2721 if (!payload_ty.hasRuntimeBitsIgnoreComptime() or payload_ty.optionalReprIsPayload()) {
2722 return parent_ptr;
2723 }
2724
2725 const abi_size = payload_ptr.container_ty.abiSize(func.target);
2726 const offset = abi_size - payload_ty.abiSize(func.target);
2727
2728 return WValue{ .memory_offset = .{
2729 .pointer = parent_ptr.memory,
2730 .offset = @intCast(u32, offset),
2731 } };
2718 return func.lowerParentPtr(payload_ptr.container_ptr, payload_ptr.container_ty);
27322719 },
27332720 else => |tag| return func.fail("TODO: Implement lowerParentPtr for tag: {}", .{tag}),
27342721 }
......@@ -2889,7 +2876,7 @@ fn lowerConstant(func: *CodeGen, arg_val: Value, ty: Type) InnerError!WValue {
28892876 }
28902877 } else {
28912878 const is_pl = val.tag() == .opt_payload;
2892 return WValue{ .imm32 = if (is_pl) @as(u32, 1) else 0 };
2879 return WValue{ .imm32 = @boolToInt(is_pl) };
28932880 },
28942881 .Struct => {
28952882 const struct_obj = ty.castTag(.@"struct").?.data;
......@@ -3882,7 +3869,11 @@ fn isNull(func: *CodeGen, operand: WValue, optional_ty: Type, opcode: wasm.Opcod
38823869 // When payload is zero-bits, we can treat operand as a value, rather than
38833870 // a pointer to the stack value
38843871 if (payload_ty.hasRuntimeBitsIgnoreComptime()) {
3885 try func.addMemArg(.i32_load8_u, .{ .offset = operand.offset(), .alignment = 1 });
3872 const offset = std.math.cast(u32, payload_ty.abiSize(func.target)) orelse {
3873 const module = func.bin_file.base.options.module.?;
3874 return func.fail("Optional type {} too big to fit into stack frame", .{optional_ty.fmt(module)});
3875 };
3876 try func.addMemArg(.i32_load8_u, .{ .offset = operand.offset() + offset, .alignment = 1 });
38863877 }
38873878 } else if (payload_ty.isSlice()) {
38883879 switch (func.arch()) {
......@@ -3911,13 +3902,11 @@ fn airOptionalPayload(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
39113902 const operand = try func.resolveInst(ty_op.operand);
39123903 if (opt_ty.optionalReprIsPayload()) break :result func.reuseOperand(ty_op.operand, operand);
39133904
3914 const offset = opt_ty.abiSize(func.target) - payload_ty.abiSize(func.target);
3915
39163905 if (isByRef(payload_ty, func.target)) {
3917 break :result try func.buildPointerOffset(operand, offset, .new);
3906 break :result try func.buildPointerOffset(operand, 0, .new);
39183907 }
39193908
3920 const payload = try func.load(operand, payload_ty, @intCast(u32, offset));
3909 const payload = try func.load(operand, payload_ty, 0);
39213910 break :result try payload.toLocal(func, payload_ty);
39223911 };
39233912 func.finishAir(inst, result, &.{ty_op.operand});
......@@ -3936,8 +3925,7 @@ fn airOptionalPayloadPtr(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
39363925 break :result func.reuseOperand(ty_op.operand, operand);
39373926 }
39383927
3939 const offset = opt_ty.abiSize(func.target) - payload_ty.abiSize(func.target);
3940 break :result try func.buildPointerOffset(operand, offset, .new);
3928 break :result try func.buildPointerOffset(operand, 0, .new);
39413929 };
39423930 func.finishAir(inst, result, &.{ty_op.operand});
39433931}
......@@ -3956,16 +3944,16 @@ fn airOptionalPayloadPtrSet(func: *CodeGen, inst: Air.Inst.Index) InnerError!voi
39563944 return func.finishAir(inst, operand, &.{ty_op.operand});
39573945 }
39583946
3959 const offset = std.math.cast(u32, opt_ty.abiSize(func.target) - payload_ty.abiSize(func.target)) orelse {
3947 const offset = std.math.cast(u32, payload_ty.abiSize(func.target)) orelse {
39603948 const module = func.bin_file.base.options.module.?;
39613949 return func.fail("Optional type {} too big to fit into stack frame", .{opt_ty.fmt(module)});
39623950 };
39633951
39643952 try func.emitWValue(operand);
39653953 try func.addImm32(1);
3966 try func.addMemArg(.i32_store8, .{ .offset = operand.offset(), .alignment = 1 });
3954 try func.addMemArg(.i32_store8, .{ .offset = operand.offset() + offset, .alignment = 1 });
39673955
3968 const result = try func.buildPointerOffset(operand, offset, .new);
3956 const result = try func.buildPointerOffset(operand, 0, .new);
39693957 return func.finishAir(inst, result, &.{ty_op.operand});
39703958}
39713959
......@@ -3988,7 +3976,7 @@ fn airWrapOptional(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
39883976 if (op_ty.optionalReprIsPayload()) {
39893977 break :result func.reuseOperand(ty_op.operand, operand);
39903978 }
3991 const offset = std.math.cast(u32, op_ty.abiSize(func.target) - payload_ty.abiSize(func.target)) orelse {
3979 const offset = std.math.cast(u32, payload_ty.abiSize(func.target)) orelse {
39923980 const module = func.bin_file.base.options.module.?;
39933981 return func.fail("Optional type {} too big to fit into stack frame", .{op_ty.fmt(module)});
39943982 };
......@@ -3997,9 +3985,9 @@ fn airWrapOptional(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
39973985 const result_ptr = try func.allocStack(op_ty);
39983986 try func.emitWValue(result_ptr);
39993987 try func.addImm32(1);
4000 try func.addMemArg(.i32_store8, .{ .offset = result_ptr.offset(), .alignment = 1 });
3988 try func.addMemArg(.i32_store8, .{ .offset = result_ptr.offset() + offset, .alignment = 1 });
40013989
4002 const payload_ptr = try func.buildPointerOffset(result_ptr, offset, .new);
3990 const payload_ptr = try func.buildPointerOffset(result_ptr, 0, .new);
40033991 try func.store(payload_ptr, operand, payload_ty, 0);
40043992 break :result result_ptr;
40053993 };
......@@ -4719,7 +4707,6 @@ fn cmpOptionals(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op:
47194707 assert(op == .eq or op == .neq);
47204708 var buf: Type.Payload.ElemType = undefined;
47214709 const payload_ty = operand_ty.optionalChild(&buf);
4722 const offset = @intCast(u32, operand_ty.abiSize(func.target) - payload_ty.abiSize(func.target));
47234710
47244711 // We store the final result in here that will be validated
47254712 // if the optional is truly equal.
......@@ -4732,8 +4719,8 @@ fn cmpOptionals(func: *CodeGen, lhs: WValue, rhs: WValue, operand_ty: Type, op:
47324719 try func.addTag(.i32_ne); // inverse so we can exit early
47334720 try func.addLabel(.br_if, 0);
47344721
4735 _ = try func.load(lhs, payload_ty, offset);
4736 _ = try func.load(rhs, payload_ty, offset);
4722 _ = try func.load(lhs, payload_ty, 0);
4723 _ = try func.load(rhs, payload_ty, 0);
47374724 const opcode = buildOpcode(.{ .op = .ne, .valtype1 = typeToValtype(payload_ty, func.target) });
47384725 try func.addTag(Mir.Inst.Tag.fromOpcode(opcode));
47394726 try func.addLabel(.br_if, 0);
test/behavior/error.zig+1
......@@ -874,6 +874,7 @@ test "field access of anyerror results in smaller error set" {
874874}
875875
876876test "optional error union return type" {
877 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
877878 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
878879
879880 const S = struct {