From 6c16465d45527c195302beb1f3a1a6d41cdcdd92 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Tue, 11 Apr 2023 22:29:02 +0200 Subject: [PATCH] spirv: lower air optional_payload Implements lowering the AIR tag optional_payload. Also fixes an issue with lowering constant ints. --- src/codegen/spirv.zig | 29 ++++++++++++++++++++++++----- 1 file changed, 24 insertions(+), 5 deletions(-) diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 0388466c10..ab2bdd64a7 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -957,11 +957,11 @@ pub const DeclGen = struct { switch (ty.zigTypeTag()) { .Int => { - const int_bits = if (ty.isSignedInt()) - @bitCast(u64, val.toSignedInt(target)) - else - val.toUnsignedInt(target); - try self.genConstInt(result_ty_ref, result_id, int_bits); + if (ty.isSignedInt()) { + try self.genConstInt(result_ty_ref, result_id, val.toSignedInt(target)); + } else { + try self.genConstInt(result_ty_ref, result_id, val.toUnsignedInt(target)); + } }, .Bool => { const operands = .{ .id_result_type = result_ty_id, .id_result = result_id }; @@ -1717,6 +1717,8 @@ pub const DeclGen = struct { .is_null => try self.airIsNull(inst, .is_null), .is_non_null => try self.airIsNull(inst, .is_non_null), + .optional_payload => try self.airUnwrapOptional(inst), + .assembly => try self.airAssembly(inst), .call => try self.airCall(inst, .auto), @@ -2694,6 +2696,23 @@ pub const DeclGen = struct { }; } + fn airUnwrapOptional(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { + if (self.liveness.isUnused(inst)) return null; + + const ty_op = self.air.instructions.items(.data)[inst].ty_op; + const operand_id = try self.resolve(ty_op.operand); + const optional_ty = self.air.typeOf(ty_op.operand); + const payload_ty = self.air.typeOfIndex(inst); + + if (!payload_ty.hasRuntimeBitsIgnoreComptime()) return null; + + if (optional_ty.optionalReprIsPayload()) { + return operand_id; + } + + return try self.extractField(payload_ty, operand_id, 0); + } + fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void { const target = self.getTarget(); const pl_op = self.air.instructions.items(.data)[inst].pl_op;