authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-05-16 16:09:22-07:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2023-05-16 16:09:22-07:00
log958fba0eb715fe9f0b5eddcf53507db03e37ae09
tree2505c7bdfd50cebb986201271a42899fce12c21c
parentb754068fbc7492962953068d31386d4c04e37ae5
parent6fca3f8b72397d62dd7b2d2e07db843c88428ac3
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #15713 from alichraghi/ali-spirv

spirv: get more behavior tests passing

7 files changed, 114 insertions(+), 20 deletions(-)

src/codegen/spirv.zig+110-4
......@@ -369,6 +369,17 @@ pub const DeclGen = struct {
369369 .composite_integer,
370370 };
371371 },
372 .Enum => blk: {
373 var buffer: Type.Payload.Bits = undefined;
374 const int_ty = ty.intTagType(&buffer);
375 const int_info = int_ty.intInfo(target);
376 break :blk ArithmeticTypeInfo{
377 .bits = int_info.bits,
378 .is_vector = false,
379 .signedness = int_info.signedness,
380 .class = .integer,
381 };
382 },
372383 // As of yet, there is no vector support in the self-hosted compiler.
373384 .Vector => self.todo("implement arithmeticTypeInfo for Vector", .{}),
374385 // TODO: For which types is this the case?
......@@ -568,6 +579,28 @@ pub const DeclGen = struct {
568579 try self.addBytes(std.mem.asBytes(&int_bits)[0..@intCast(usize, len)]);
569580 }
570581
582 fn addFloat(self: *@This(), ty: Type, val: Value) !void {
583 const target = self.dg.getTarget();
584 const len = ty.abiSize(target);
585
586 // TODO: Swap endianess if the compiler is big endian.
587 switch (ty.floatBits(target)) {
588 16 => {
589 const float_bits = val.toFloat(f16);
590 try self.addBytes(std.mem.asBytes(&float_bits)[0..@intCast(usize, len)]);
591 },
592 32 => {
593 const float_bits = val.toFloat(f32);
594 try self.addBytes(std.mem.asBytes(&float_bits)[0..@intCast(usize, len)]);
595 },
596 64 => {
597 const float_bits = val.toFloat(f64);
598 try self.addBytes(std.mem.asBytes(&float_bits)[0..@intCast(usize, len)]);
599 },
600 else => unreachable,
601 }
602 }
603
571604 fn addDeclRef(self: *@This(), ty: Type, decl_index: Decl.Index) !void {
572605 const dg = self.dg;
573606
......@@ -618,6 +651,7 @@ pub const DeclGen = struct {
618651
619652 switch (ty.zigTypeTag()) {
620653 .Int => try self.addInt(ty, val),
654 .Float => try self.addFloat(ty, val),
621655 .Bool => try self.addConstBool(val.toBool()),
622656 .Array => switch (val.tag()) {
623657 .aggregate => {
......@@ -1690,6 +1724,9 @@ pub const DeclGen = struct {
16901724
16911725 .bitcast => try self.airBitcast(inst),
16921726 .intcast, .trunc => try self.airIntcast(inst),
1727 .ptrtoint => try self.airPtrToInt(inst),
1728 .int_to_float => try self.airIntToFloat(inst),
1729 .float_to_int => try self.airFloatToInt(inst),
16931730 .not => try self.airNot(inst),
16941731
16951732 .slice_ptr => try self.airSliceField(inst, 0),
......@@ -1750,10 +1787,12 @@ pub const DeclGen = struct {
17501787 .call_never_tail => try self.airCall(inst, .never_tail),
17511788 .call_never_inline => try self.airCall(inst, .never_inline),
17521789
1753 .dbg_var_ptr => return,
1754 .dbg_var_val => return,
1755 .dbg_block_begin => return,
1756 .dbg_block_end => return,
1790 .dbg_inline_begin => return,
1791 .dbg_inline_end => return,
1792 .dbg_var_ptr => return,
1793 .dbg_var_val => return,
1794 .dbg_block_begin => return,
1795 .dbg_block_end => return,
17571796 // zig fmt: on
17581797
17591798 else => |tag| return self.todo("implement AIR tag {s}", .{@tagName(tag)}),
......@@ -2095,6 +2134,73 @@ pub const DeclGen = struct {
20952134 return result_id;
20962135 }
20972136
2137 fn airPtrToInt(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2138 if (self.liveness.isUnused(inst)) return null;
2139
2140 const un_op = self.air.instructions.items(.data)[inst].un_op;
2141 const operand_id = try self.resolve(un_op);
2142 const result_type_id = try self.resolveTypeId(Type.usize);
2143
2144 const result_id = self.spv.allocId();
2145 try self.func.body.emit(self.spv.gpa, .OpConvertPtrToU, .{
2146 .id_result_type = result_type_id,
2147 .id_result = result_id,
2148 .pointer = operand_id,
2149 });
2150 return result_id;
2151 }
2152
2153 fn airIntToFloat(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2154 if (self.liveness.isUnused(inst)) return null;
2155
2156 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2157 const operand_ty = self.air.typeOf(ty_op.operand);
2158 const operand_id = try self.resolve(ty_op.operand);
2159 const operand_info = try self.arithmeticTypeInfo(operand_ty);
2160 const dest_ty = self.air.typeOfIndex(inst);
2161 const dest_ty_id = try self.resolveTypeId(dest_ty);
2162
2163 const result_id = self.spv.allocId();
2164 switch (operand_info.signedness) {
2165 .signed => try self.func.body.emit(self.spv.gpa, .OpConvertSToF, .{
2166 .id_result_type = dest_ty_id,
2167 .id_result = result_id,
2168 .signed_value = operand_id,
2169 }),
2170 .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertUToF, .{
2171 .id_result_type = dest_ty_id,
2172 .id_result = result_id,
2173 .unsigned_value = operand_id,
2174 }),
2175 }
2176 return result_id;
2177 }
2178
2179 fn airFloatToInt(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2180 if (self.liveness.isUnused(inst)) return null;
2181
2182 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
2183 const operand_id = try self.resolve(ty_op.operand);
2184 const dest_ty = self.air.typeOfIndex(inst);
2185 const dest_info = try self.arithmeticTypeInfo(dest_ty);
2186 const dest_ty_id = try self.resolveTypeId(dest_ty);
2187
2188 const result_id = self.spv.allocId();
2189 switch (dest_info.signedness) {
2190 .signed => try self.func.body.emit(self.spv.gpa, .OpConvertFToS, .{
2191 .id_result_type = dest_ty_id,
2192 .id_result = result_id,
2193 .float_value = operand_id,
2194 }),
2195 .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertFToU, .{
2196 .id_result_type = dest_ty_id,
2197 .id_result = result_id,
2198 .float_value = operand_id,
2199 }),
2200 }
2201 return result_id;
2202 }
2203
20982204 fn airNot(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
20992205 if (self.liveness.isUnused(inst)) return null;
21002206 const ty_op = self.air.instructions.items(.data)[inst].ty_op;
test/behavior/cast.zig-10
......@@ -97,7 +97,6 @@ test "@intToFloat" {
9797 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
9898 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
9999 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
100 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
101100
102101 const S = struct {
103102 fn doTheTest() !void {
......@@ -155,7 +154,6 @@ test "@floatToInt" {
155154 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
156155 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
157156 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
158 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
159157
160158 try testFloatToInts();
161159 comptime try testFloatToInts();
......@@ -207,16 +205,12 @@ test "implicitly cast indirect pointer to maybe-indirect pointer" {
207205}
208206
209207test "@intCast comptime_int" {
210 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
211
212208 const result = @intCast(i32, 1234);
213209 try expect(@TypeOf(result) == i32);
214210 try expect(result == 1234);
215211}
216212
217213test "@floatCast comptime_int and comptime_float" {
218 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
219
220214 {
221215 const result = @floatCast(f16, 1234);
222216 try expect(@TypeOf(result) == f16);
......@@ -1290,14 +1284,10 @@ test "implicit cast *[0]T to E![]const u8" {
12901284
12911285var global_array: [4]u8 = undefined;
12921286test "cast from array reference to fn: comptime fn ptr" {
1293 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1294
12951287 const f = @ptrCast(*align(1) const fn () callconv(.C) void, &global_array);
12961288 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
12971289}
12981290test "cast from array reference to fn: runtime fn ptr" {
1299 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1300
13011291 var f = @ptrCast(*align(1) const fn () callconv(.C) void, &global_array);
13021292 try expect(@ptrToInt(f) == @ptrToInt(&global_array));
13031293}
test/behavior/enum.zig-5
......@@ -27,7 +27,6 @@ const IntToEnumNumber = enum { Zero, One, Two, Three, Four };
2727test "int to enum" {
2828 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
2929 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
30 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
3130
3231 try testIntToEnumEval(3);
3332}
......@@ -576,8 +575,6 @@ test "enum literal equality" {
576575}
577576
578577test "enum literal cast to enum" {
579 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
580
581578 const Color = enum { Auto, Off, On };
582579
583580 var color1: Color = .Auto;
......@@ -874,8 +871,6 @@ test "switch on enum with one member is comptime-known" {
874871}
875872
876873test "method call on an enum" {
877 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
878
879874 const S = struct {
880875 const E = enum {
881876 one,
test/behavior/error.zig+1
......@@ -916,6 +916,7 @@ test "optional error set return type" {
916916test "try used in recursive function with inferred error set" {
917917 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
918918 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
919 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; // TODO
919920
920921 const Value = union(enum) {
921922 values: []const @This(),
test/behavior/maximum_minimum.zig+1
......@@ -106,6 +106,7 @@ test "@min/max for floats" {
106106 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
107107 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
108108 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
109 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
109110
110111 const S = struct {
111112 fn doTheTest(comptime T: type) !void {
test/behavior/slice.zig+2
......@@ -186,6 +186,8 @@ test "slicing zero length array" {
186186
187187test "slicing pointer by length" {
188188 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest;
189 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
190
189191 const array = [_]u8{ 1, 2, 3, 4, 5, 6, 7, 8 };
190192 const ptr: [*]const u8 = @ptrCast([*]const u8, &array);
191193 const slice = ptr[1..][0..5];
test/behavior/union.zig-1
......@@ -365,7 +365,6 @@ test "simple union(enum(u32))" {
365365 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
366366 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
367367 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
368 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
369368
370369 var x = MultipleChoice.C;
371370 try expect(x == MultipleChoice.C);