From 7d519b3383431df48a41d6b32520e5c5e3d77612 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 18 May 2023 12:27:16 +0200 Subject: [PATCH] spirv: use intInfo instead of arithmeticTypeInfo in airIntCast This ensures that we can also cast enums and error sets here. In the future this function will need to be changed to support composite and strange integers, but that is fine. --- src/codegen/spirv.zig | 13 +++++++++---- test/behavior/enum.zig | 2 -- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 9cfac608a139da86713ab19063ad4588b51d1fe1..5edafc503768f01a6af214eab1f3ed9e10f8209f 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -372,7 +372,8 @@ pub const DeclGen = struct { // As of yet, there is no vector support in the self-hosted compiler. .Vector => self.todo("implement arithmeticTypeInfo for Vector", .{}), // TODO: For which types is this the case? - else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmt(self.module)}), + // else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmt(self.module)}), + else => unreachable, }; } @@ -1712,7 +1713,7 @@ pub const DeclGen = struct { .shl => try self.airShift(inst, .OpShiftLeftLogical), .bitcast => try self.airBitcast(inst), - .intcast, .trunc => try self.airIntcast(inst), + .intcast, .trunc => try self.airIntCast(inst), .ptrtoint => try self.airPtrToInt(inst), .int_to_float => try self.airIntToFloat(inst), .float_to_int => try self.airFloatToInt(inst), @@ -2162,15 +2163,19 @@ pub const DeclGen = struct { return try self.bitcast(result_type_id, operand_id); } - fn airIntcast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef { + fn airIntCast(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 dest_ty = self.air.typeOfIndex(inst); - const dest_info = try self.arithmeticTypeInfo(dest_ty); const dest_ty_id = try self.resolveTypeId(dest_ty); + const target = self.getTarget(); + const dest_info = dest_ty.intInfo(target); + + // TODO: Masking? + const result_id = self.spv.allocId(); switch (dest_info.signedness) { .signed => try self.func.body.emit(self.spv.gpa, .OpSConvert, .{ diff --git a/test/behavior/enum.zig b/test/behavior/enum.zig index 44a6026f2b6d4e7ca1b3f8537dfee39459db8c2f..097caaad19e820787ad239b7c6cdee513520fa15 100644 --- a/test/behavior/enum.zig +++ b/test/behavior/enum.zig @@ -20,8 +20,6 @@ test "enum to int" { } fn testIntToEnumEval(x: i32) !void { - if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; - try expect(@intToEnum(IntToEnumNumber, x) == IntToEnumNumber.Three); } const IntToEnumNumber = enum { Zero, One, Two, Three, Four }; -- 2.54.0