Merge pull request #15713 from alichraghi/ali-spirv

spirv: get more behavior tests passing
This commit is contained in:
Andrew Kelley 2023-05-16 16:09:22 -07:00 committed by GitHub
commit 958fba0eb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 114 additions and 20 deletions

View File

@ -369,6 +369,17 @@ pub const DeclGen = struct {
.composite_integer,
};
},
.Enum => blk: {
var buffer: Type.Payload.Bits = undefined;
const int_ty = ty.intTagType(&buffer);
const int_info = int_ty.intInfo(target);
break :blk ArithmeticTypeInfo{
.bits = int_info.bits,
.is_vector = false,
.signedness = int_info.signedness,
.class = .integer,
};
},
// 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?
@ -568,6 +579,28 @@ pub const DeclGen = struct {
try self.addBytes(std.mem.asBytes(&int_bits)[0..@intCast(usize, len)]);
}
fn addFloat(self: *@This(), ty: Type, val: Value) !void {
const target = self.dg.getTarget();
const len = ty.abiSize(target);
// TODO: Swap endianess if the compiler is big endian.
switch (ty.floatBits(target)) {
16 => {
const float_bits = val.toFloat(f16);
try self.addBytes(std.mem.asBytes(&float_bits)[0..@intCast(usize, len)]);
},
32 => {
const float_bits = val.toFloat(f32);
try self.addBytes(std.mem.asBytes(&float_bits)[0..@intCast(usize, len)]);
},
64 => {
const float_bits = val.toFloat(f64);
try self.addBytes(std.mem.asBytes(&float_bits)[0..@intCast(usize, len)]);
},
else => unreachable,
}
}
fn addDeclRef(self: *@This(), ty: Type, decl_index: Decl.Index) !void {
const dg = self.dg;
@ -618,6 +651,7 @@ pub const DeclGen = struct {
switch (ty.zigTypeTag()) {
.Int => try self.addInt(ty, val),
.Float => try self.addFloat(ty, val),
.Bool => try self.addConstBool(val.toBool()),
.Array => switch (val.tag()) {
.aggregate => {
@ -1690,6 +1724,9 @@ pub const DeclGen = struct {
.bitcast => try self.airBitcast(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),
.not => try self.airNot(inst),
.slice_ptr => try self.airSliceField(inst, 0),
@ -1750,10 +1787,12 @@ pub const DeclGen = struct {
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.dbg_var_ptr => return,
.dbg_var_val => return,
.dbg_block_begin => return,
.dbg_block_end => return,
.dbg_inline_begin => return,
.dbg_inline_end => return,
.dbg_var_ptr => return,
.dbg_var_val => return,
.dbg_block_begin => return,
.dbg_block_end => return,
// zig fmt: on
else => |tag| return self.todo("implement AIR tag {s}", .{@tagName(tag)}),
@ -2095,6 +2134,73 @@ pub const DeclGen = struct {
return result_id;
}
fn airPtrToInt(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
if (self.liveness.isUnused(inst)) return null;
const un_op = self.air.instructions.items(.data)[inst].un_op;
const operand_id = try self.resolve(un_op);
const result_type_id = try self.resolveTypeId(Type.usize);
const result_id = self.spv.allocId();
try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{
.id_result_type = result_type_id,
.id_result = result_id,
.pointer = operand_id,
});
return result_id;
}
fn airIntToFloat(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_ty = self.air.typeOf(ty_op.operand);
const operand_id = try self.resolve(ty_op.operand);
const operand_info = try self.arithmeticTypeInfo(operand_ty);
const dest_ty = self.air.typeOfIndex(inst);
const dest_ty_id = try self.resolveTypeId(dest_ty);
const result_id = self.spv.allocId();
switch (operand_info.signedness) {
.signed => try self.func.body.emit(self.spv.gpa, .OpConvertSToF, .{
.id_result_type = dest_ty_id,
.id_result = result_id,
.signed_value = operand_id,
}),
.unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertUToF, .{
.id_result_type = dest_ty_id,
.id_result = result_id,
.unsigned_value = operand_id,
}),
}
return result_id;
}
fn airFloatToInt(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 result_id = self.spv.allocId();
switch (dest_info.signedness) {
.signed => try self.func.body.emit(self.spv.gpa, .OpConvertFToS, .{
.id_result_type = dest_ty_id,
.id_result = result_id,
.float_value = operand_id,
}),
.unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertFToU, .{
.id_result_type = dest_ty_id,
.id_result = result_id,
.float_value = operand_id,
}),
}
return result_id;
}
fn airNot(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;

View File

@ -97,7 +97,6 @@ test "@intToFloat" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
fn doTheTest() !void {
@ -155,7 +154,6 @@ test "@floatToInt" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
try testFloatToInts();
comptime try testFloatToInts();
@ -207,16 +205,12 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
}
test "@intCast comptime_int" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const result = @intCast(i32, 1234);
try expect(@TypeOf(result) == i32);
try expect(result == 1234);
}
test "@floatCast comptime_int and comptime_float" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
{
const result = @floatCast(f16, 1234);
try expect(@TypeOf(result) == f16);
@ -1290,14 +1284,10 @@ test "implicit cast *[0]T to E![]const u8" {
var global_array: [4]u8 = undefined;
test "cast from array reference to fn: comptime fn ptr" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const f = @ptrCast(*align(1) const fn () callconv(.C) void, &global_array);
try expect(@ptrToInt(f) == @ptrToInt(&global_array));
}
test "cast from array reference to fn: runtime fn ptr" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
var f = @ptrCast(*align(1) const fn () callconv(.C) void, &global_array);
try expect(@ptrToInt(f) == @ptrToInt(&global_array));
}

View File

@ -27,7 +27,6 @@ const IntToEnumNumber = enum { Zero, One, Two, Three, Four };
test "int to enum" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
try testIntToEnumEval(3);
}
@ -576,8 +575,6 @@ test "enum literal equality" {
}
test "enum literal cast to enum" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const Color = enum { Auto, Off, On };
var color1: Color = .Auto;
@ -874,8 +871,6 @@ test "switch on enum with one member is comptime-known" {
}
test "method call on an enum" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
const E = enum {
one,

View File

@ -916,6 +916,7 @@ test "optional error set return type" {
test "try used in recursive function with inferred error set" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
const Value = union(enum) {
values: []const @This(),

View File

@ -106,6 +106,7 @@ test "@min/max for floats" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
fn doTheTest(comptime T: type) !void {

View File

@ -186,6 +186,8 @@ test "slicing zero length array" {
test "slicing pointer by length" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
const ptr: [*]const u8 = @ptrCast([*]const u8, &array);
const slice = ptr[1..][0..5];

View File

@ -365,7 +365,6 @@ test "simple union(enum(u32))" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
var x = MultipleChoice.C;
try expect(x == MultipleChoice.C);