spirv: ptr_elem_val

Implements the ptr_elem_val air tag. Implementation is unified
with ptr_elem_ptr.
This commit is contained in:
Robin Voetter 2023-05-19 14:17:58 +02:00
parent 77b8bf2b82
commit 65157d30ab
No known key found for this signature in database
GPG Key ID: E755662F227CB468
21 changed files with 36 additions and 41 deletions

View File

@ -1765,6 +1765,7 @@ pub const DeclGen = struct {
.slice_elem_ptr => try self.airSliceElemPtr(inst),
.slice_elem_val => try self.airSliceElemVal(inst),
.ptr_elem_ptr => try self.airPtrElemPtr(inst),
.ptr_elem_val => try self.airPtrElemVal(inst),
.struct_field_val => try self.airStructFieldVal(inst),
@ -2482,29 +2483,52 @@ pub const DeclGen = struct {
return try self.load(slice_ty, elem_ptr);
}
fn ptrElemPtr(self: *DeclGen, ptr_ty: Type, ptr_id: IdRef, index_id: IdRef) !IdRef {
// Construct new pointer type for the resulting pointer
const elem_ty = ptr_ty.elemType2(); // use elemType() so that we get T for *[N]T.
const elem_ty_ref = try self.resolveType(elem_ty, .direct);
const elem_ptr_ty_ref = try self.spv.ptrType(elem_ty_ref, spvStorageClass(ptr_ty.ptrAddressSpace()), 0);
if (ptr_ty.isSinglePointer()) {
// Pointer-to-array. In this case, the resulting pointer is not of the same type
// as the ptr_ty (we want a *T, not a *[N]T), and hence we need to use accessChain.
return try self.accessChain(elem_ptr_ty_ref, ptr_id, &.{index_id});
} else {
// Resulting pointer type is the same as the ptr_ty, so use ptrAccessChain
return try self.ptrAccessChain(elem_ptr_ty_ref, ptr_id, index_id, &.{});
}
}
fn airPtrElemPtr(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
if (self.liveness.isUnused(inst)) return null;
const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
const ptr_ty = self.air.typeOf(bin_op.lhs);
const result_ty = self.air.typeOfIndex(inst);
const elem_ty = ptr_ty.childType();
// TODO: Make this return a null ptr or something
if (!elem_ty.hasRuntimeBitsIgnoreComptime()) return null;
const result_ty_ref = try self.resolveType(result_ty, .direct);
const base_ptr = try self.resolve(bin_op.lhs);
const rhs = try self.resolve(bin_op.rhs);
const ptr_id = try self.resolve(bin_op.lhs);
const index_id = try self.resolve(bin_op.rhs);
return try self.ptrElemPtr(ptr_ty, ptr_id, index_id);
}
if (ptr_ty.isSinglePointer()) {
// Pointer-to-array. In this case, the resulting pointer is not of the same type
// as the ptr_ty, and hence we need to use accessChain.
return try self.accessChain(result_ty_ref, base_ptr, &.{rhs});
} else {
// Resulting pointer type is the same as the ptr_ty, so use ptrAccessChain
return try self.ptrAccessChain(result_ty_ref, base_ptr, rhs, &.{});
}
fn airPtrElemVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
const bin_op = self.air.instructions.items(.data)[inst].bin_op;
const ptr_ty = self.air.typeOf(bin_op.lhs);
const ptr_id = try self.resolve(bin_op.lhs);
const index_id = try self.resolve(bin_op.rhs);
const elem_ptr_id = try self.ptrElemPtr(ptr_ty, ptr_id, index_id);
// If we have a pointer-to-array, construct an element pointer to use with load()
// If we pass ptr_ty directly, it will attempt to load the entire array rather than
// just an element.
var elem_ptr_info = ptr_ty.ptrInfo();
elem_ptr_info.data.size = .One;
const elem_ptr_ty = Type.initPayload(&elem_ptr_info.base);
return try self.load(elem_ptr_ty, elem_ptr_id);
}
fn airStructFieldVal(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {

View File

@ -215,8 +215,6 @@ test "alignment and size of structs with 128-bit fields" {
}
test "@ptrCast preserves alignment of bigger source" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
var x: u32 align(16) = 1234;
const ptr = @ptrCast(*u8, &x);
try expect(@TypeOf(ptr) == *align(16) u8);

View File

@ -385,8 +385,6 @@ test "generic function with generic function parameter" {
}
test "recursive inline call with comptime known argument" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
inline fn foo(x: i32) i32 {
if (x <= 0) {

View File

@ -322,7 +322,6 @@ test "peer result null and comptime_int" {
test "*const ?[*]const T to [*c]const [*c]const T" {
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 array = [_]u8{ 'o', 'k' };
const opt_array_ptr: ?[*]const u8 = &array;

View File

@ -22,7 +22,6 @@ test "error values" {
}
test "redefinition of error values allowed" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
shouldBeNotEqual(error.AnError, error.SecondError);
}
fn shouldBeNotEqual(a: anyerror, b: anyerror) void {

View File

@ -47,8 +47,6 @@ test "inline variable gets result of const if" {
}
test "static function evaluation" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
try expect(statically_added_number == 3);
}
const statically_added_number = staticAdd(1, 2);

View File

@ -620,7 +620,6 @@ test "@floor" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
comptime try testFloor();
try testFloor();

View File

@ -12,8 +12,6 @@ fn testParamsAdd(a: i32, b: i32) i32 {
}
test "local variables" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
testLocVars(2);
}
fn testLocVars(b: i32) void {

View File

@ -39,8 +39,6 @@ fn testBreakOuter() !void {
}
test "continue outer for loop" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
try testContinueOuter();
comptime try testContinueOuter();
}

View File

@ -19,7 +19,6 @@ fn checkSize(comptime T: type) usize {
test "simple generic fn" {
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 expect(max(i32, 3, -1) == 3);
try expect(max(u8, 1, 100) == 100);

View File

@ -421,7 +421,6 @@ test "optional of noreturn used with orelse" {
}
test "orelse on C pointer" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
// TODO https://github.com/ziglang/zig/issues/6597
const foo: [*c]const u8 = "hey";

View File

@ -93,7 +93,6 @@ test "flags in packed structs" {
test "consistent size of packed structs" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const TxData1 = packed struct { data: u8, _23: u23, full: bool = false };
const TxData2 = packed struct { data: u9, _22: u22, full: bool = false };

View File

@ -19,7 +19,6 @@ fn testDerefPtr() !void {
test "pointer arithmetic" {
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 ptr: [*]const u8 = "abcd";
@ -300,7 +299,6 @@ test "null terminated pointer" {
test "allow any sentinel" {
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;
const S = struct {
fn doTheTest() !void {

View File

@ -154,7 +154,6 @@ test "@TypeOf() has no runtime side effects" {
test "branching logic inside @TypeOf" {
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
var data: i32 = 0;

View File

@ -672,7 +672,6 @@ test "array mult of slice gives ptr to array" {
test "slice bounds in comptime concatenation" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const bs = comptime blk: {
const b = "........1........";

View File

@ -121,8 +121,6 @@ test "struct byval assign" {
}
test "call struct static method" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const result = StructWithNoFields.add(3, 4);
try expect(result == 7);
}

View File

@ -348,8 +348,6 @@ fn returnsFalse() bool {
}
}
test "switch on const enum with var" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
try expect(!returnsFalse());
}

View File

@ -46,7 +46,6 @@ test "reference a global threadlocal variable" {
else => return error.SkipZigTest,
}; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
_ = nrfx_uart_rx(&g_uart0);
}

View File

@ -512,7 +512,6 @@ test "type info for async frames" {
test "Declarations are returned in declaration order" {
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
const S = struct {
const a = 1;

View File

@ -30,7 +30,6 @@ test "send void arg to var args" {
test "pass args directly" {
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
try expect(addSomeStuff(.{ @as(i32, 1), @as(i32, 2), @as(i32, 3), @as(i32, 4) }) == 10);
try expect(addSomeStuff(.{@as(i32, 1234)}) == 1234);

View File

@ -38,8 +38,6 @@ fn staticWhileLoop2() i32 {
}
test "while with continue expression" {
if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
var sum: i32 = 0;
{
var i: i32 = 0;