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 9cfac608a1..5edafc5037 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 44a6026f2b..097caaad19 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 };