authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-18 12:27:16+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-20 17:30:21+02:00
log7d519b3383431df48a41d6b32520e5c5e3d77612
tree00bee0e877593683b7906ef348e40872db4dd30a
parent6e3770e970dbf460271a0e0cb60c2bf40a7c861e
signaturelock-open Commit is signed but in an unrecognized format.

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.

2 files changed, 9 insertions(+), 6 deletions(-)

src/codegen/spirv.zig+9-4
......@@ -372,7 +372,8 @@ pub const DeclGen = struct {
372372 // As of yet, there is no vector support in the self-hosted compiler.
373373 .Vector => self.todo("implement arithmeticTypeInfo for Vector", .{}),
374374 // TODO: For which types is this the case?
375 else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmt(self.module)}),
375 // else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmt(self.module)}),
376 else => unreachable,
376377 };
377378 }
378379
......@@ -1712,7 +1713,7 @@ pub const DeclGen = struct {
17121713 .shl => try self.airShift(inst, .OpShiftLeftLogical),
17131714
17141715 .bitcast => try self.airBitcast(inst),
1715 .intcast, .trunc => try self.airIntcast(inst),
1716 .intcast, .trunc => try self.airIntCast(inst),
17161717 .ptrtoint => try self.airPtrToInt(inst),
17171718 .int_to_float => try self.airIntToFloat(inst),
17181719 .float_to_int => try self.airFloatToInt(inst),
......@@ -2162,15 +2163,19 @@ pub const DeclGen = struct {
21622163 return try self.bitcast(result_type_id, operand_id);
21632164 }
21642165
2165 fn airIntcast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2166 fn airIntCast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
21662167 if (self.liveness.isUnused(inst)) return null;
21672168
21682169 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
21692170 const operand_id = try self.resolve(ty_op.operand);
21702171 const dest_ty = self.air.typeOfIndex(inst);
2171 const dest_info = try self.arithmeticTypeInfo(dest_ty);
21722172 const dest_ty_id = try self.resolveTypeId(dest_ty);
21732173
2174 const target = self.getTarget();
2175 const dest_info = dest_ty.intInfo(target);
2176
2177 // TODO: Masking?
2178
21742179 const result_id = self.spv.allocId();
21752180 switch (dest_info.signedness) {
21762181 .signed => try self.func.body.emit(self.spv.gpa, .OpSConvert, .{
test/behavior/enum.zig-2
......@@ -20,8 +20,6 @@ test "enum to int" {
2020}
2121
2222fn testIntToEnumEval(x: i32) !void {
23 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
24
2523 try expect(@intToEnum(IntToEnumNumber, x) == IntToEnumNumber.Three);
2624}
2725const IntToEnumNumber = enum { Zero, One, Two, Three, Four };