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.
This commit is contained in:
Robin Voetter 2023-05-18 12:27:16 +02:00
parent 6e3770e970
commit 7d519b3383
No known key found for this signature in database
GPG Key ID: E755662F227CB468
2 changed files with 9 additions and 6 deletions

View File

@ -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, .{

View File

@ -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 };