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 {...@@ -372,7 +372,8 @@ pub const DeclGen = struct {
372 // As of yet, there is no vector support in the self-hosted compiler.372 // As of yet, there is no vector support in the self-hosted compiler.
373 .Vector => self.todo("implement arithmeticTypeInfo for Vector", .{}),373 .Vector => self.todo("implement arithmeticTypeInfo for Vector", .{}),
374 // TODO: For which types is this the case?374 // 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,
376 };377 };
377 }378 }
378379
...@@ -1712,7 +1713,7 @@ pub const DeclGen = struct {...@@ -1712,7 +1713,7 @@ pub const DeclGen = struct {
1712 .shl => try self.airShift(inst, .OpShiftLeftLogical),1713 .shl => try self.airShift(inst, .OpShiftLeftLogical),
17131714
1714 .bitcast => try self.airBitcast(inst),1715 .bitcast => try self.airBitcast(inst),
1715 .intcast, .trunc => try self.airIntcast(inst),1716 .intcast, .trunc => try self.airIntCast(inst),
1716 .ptrtoint => try self.airPtrToInt(inst),1717 .ptrtoint => try self.airPtrToInt(inst),
1717 .int_to_float => try self.airIntToFloat(inst),1718 .int_to_float => try self.airIntToFloat(inst),
1718 .float_to_int => try self.airFloatToInt(inst),1719 .float_to_int => try self.airFloatToInt(inst),
...@@ -2162,15 +2163,19 @@ pub const DeclGen = struct {...@@ -2162,15 +2163,19 @@ pub const DeclGen = struct {
2162 return try self.bitcast(result_type_id, operand_id);2163 return try self.bitcast(result_type_id, operand_id);
2163 }2164 }
21642165
2165 fn airIntcast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {2166 fn airIntCast(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2166 if (self.liveness.isUnused(inst)) return null;2167 if (self.liveness.isUnused(inst)) return null;
21672168
2168 const ty_op = self.air.instructions.items(.data)[inst].ty_op;2169 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2169 const operand_id = try self.resolve(ty_op.operand);2170 const operand_id = try self.resolve(ty_op.operand);
2170 const dest_ty = self.air.typeOfIndex(inst);2171 const dest_ty = self.air.typeOfIndex(inst);
2171 const dest_info = try self.arithmeticTypeInfo(dest_ty);
2172 const dest_ty_id = try self.resolveTypeId(dest_ty);2172 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
2174 const result_id = self.spv.allocId();2179 const result_id = self.spv.allocId();
2175 switch (dest_info.signedness) {2180 switch (dest_info.signedness) {
2176 .signed => try self.func.body.emit(self.spv.gpa, .OpSConvert, .{2181 .signed => try self.func.body.emit(self.spv.gpa, .OpSConvert, .{
test/behavior/enum.zig-2
...@@ -20,8 +20,6 @@ test "enum to int" {...@@ -20,8 +20,6 @@ test "enum to int" {
20}20}
2121
22fn testIntToEnumEval(x: i32) !void {22fn testIntToEnumEval(x: i32) !void {
23 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
24
25 try expect(@intToEnum(IntToEnumNumber, x) == IntToEnumNumber.Three);23 try expect(@intToEnum(IntToEnumNumber, x) == IntToEnumNumber.Three);
26}24}
27const IntToEnumNumber = enum { Zero, One, Two, Three, Four };25const IntToEnumNumber = enum { Zero, One, Two, Three, Four };