authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-09-20 23:34:26+02:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-09-23 12:36:56-07:00
logd9a8c779d8d7e251bcb549c4b1627508c012ee8c
tree97dbd460de42664793762646e8d4cd4f52675332
parenta75300c8d8cfe21647db1d50f77a9a1ee8d62a0e

spirv: constant elem ptr


3 files changed, 33 insertions(+), 29 deletions(-)

src/codegen/spirv.zig+33-18
......@@ -694,24 +694,7 @@ pub const DeclGen = struct {
694694 .none => ty,
695695 else => ty.slicePtrFieldType(mod),
696696 };
697 const ptr_id = switch (ptr.addr) {
698 .decl => |decl| try self.constructDeclRef(ptr_ty, decl),
699 .mut_decl => |mut_decl| try self.constructDeclRef(ptr_ty, mut_decl.decl), // TODO
700 .int => |int| blk: {
701 const ptr_id = self.spv.allocId();
702 // TODO: This can probably be an OpSpecConstantOp Bitcast, but
703 // that is not implemented by Mesa yet. Therefore, just generate it
704 // as a runtime operation.
705 try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{
706 .id_result_type = try self.resolveTypeId(ptr_ty),
707 .id_result = ptr_id,
708 .integer_value = try self.constant(Type.usize, int.toValue(), .direct),
709 });
710 break :blk ptr_id;
711 },
712 .comptime_field => unreachable,
713 else => |tag| return self.todo("pointer value of type {s}", .{@tagName(tag)}),
714 };
697 const ptr_id = try self.constantPtr(ptr_ty, val);
715698 if (ptr.len == .none) {
716699 return ptr_id;
717700 }
......@@ -818,6 +801,38 @@ pub const DeclGen = struct {
818801 }
819802 }
820803
804 fn constantPtr(self: *DeclGen, ptr_ty: Type, ptr_val: Value) !IdRef {
805 const result_ty_ref = try self.resolveType(ptr_ty, .direct);
806 const mod = self.module;
807 switch (mod.intern_pool.indexToKey(ptr_val.toIntern()).ptr.addr) {
808 .decl => |decl| return try self.constructDeclRef(ptr_ty, decl),
809 .mut_decl => |decl_mut| return try self.constructDeclRef(ptr_ty, decl_mut.decl),
810 .int => |int| {
811 const ptr_id = self.spv.allocId();
812 // TODO: This can probably be an OpSpecConstantOp Bitcast, but
813 // that is not implemented by Mesa yet. Therefore, just generate it
814 // as a runtime operation.
815 try self.func.body.emit(self.spv.gpa, .OpConvertUToPtr, .{
816 .id_result_type = self.typeId(result_ty_ref),
817 .id_result = ptr_id,
818 .integer_value = try self.constant(Type.usize, int.toValue(), .direct),
819 });
820 return ptr_id;
821 },
822 .eu_payload => unreachable, // TODO
823 .opt_payload => unreachable, // TODO
824 .comptime_field => unreachable,
825 .elem => |elem_ptr| {
826 const elem_ptr_ty = mod.intern_pool.typeOf(elem_ptr.base).toType();
827 const parent_ptr_id = try self.constantPtr(elem_ptr_ty, elem_ptr.base.toValue());
828 const size_ty_ref = try self.sizeType();
829 const index_id = try self.constInt(size_ty_ref, elem_ptr.index);
830 return self.ptrAccessChain(result_ty_ref, parent_ptr_id, index_id, &.{});
831 },
832 .field => unreachable, // TODO
833 }
834 }
835
821836 // Turn a Zig type's name into a cache reference.
822837 fn resolveTypeName(self: *DeclGen, ty: Type) !CacheString {
823838 var name = std.ArrayList(u8).init(self.gpa);
test/behavior/pointers.zig-8
......@@ -141,7 +141,6 @@ test "peer type resolution with C pointers" {
141141}
142142
143143test "peer type resolution with C pointer and const pointer" {
144 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
145144 var ptr_c: [*c]u8 = undefined;
146145 const ptr_const: u8 = undefined;
147146 try expect(@TypeOf(ptr_c, &ptr_const) == [*c]const u8);
......@@ -314,7 +313,6 @@ test "allow any sentinel" {
314313test "pointer sentinel with enums" {
315314 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
316315 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
317 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
318316
319317 const S = struct {
320318 const Number = enum {
......@@ -336,7 +334,6 @@ test "pointer sentinel with optional element" {
336334 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
337335 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
338336 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
339 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
340337
341338 const S = struct {
342339 fn doTheTest() !void {
......@@ -353,7 +350,6 @@ test "pointer sentinel with +inf" {
353350 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
354351 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
355352 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
356 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
357353
358354 const S = struct {
359355 fn doTheTest() !void {
......@@ -374,7 +370,6 @@ test "pointer to array at fixed address" {
374370}
375371
376372test "pointer arithmetic affects the alignment" {
377 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
378373 {
379374 var ptr: [*]align(8) u32 = undefined;
380375 var x: usize = 1;
......@@ -430,7 +425,6 @@ test "indexing array with sentinel returns correct type" {
430425test "element pointer to slice" {
431426 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
432427 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
433 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
434428
435429 const S = struct {
436430 fn doTheTest() !void {
......@@ -453,7 +447,6 @@ test "element pointer to slice" {
453447test "element pointer arithmetic to slice" {
454448 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
455449 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
456 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
457450
458451 const S = struct {
459452 fn doTheTest() !void {
......@@ -478,7 +471,6 @@ test "element pointer arithmetic to slice" {
478471
479472test "array slicing to slice" {
480473 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
481 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
482474
483475 const S = struct {
484476 fn doTheTest() !void {
test/behavior/slice.zig-3
......@@ -121,7 +121,6 @@ test "slice of type" {
121121
122122test "generic malloc free" {
123123 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
124 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
125124
126125 const a = memAlloc(u8, 10) catch unreachable;
127126 memFree(u8, a);
......@@ -302,7 +301,6 @@ test "slice type with custom alignment" {
302301
303302test "obtaining a null terminated slice" {
304303 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
305 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
306304
307305 // here we have a normal array
308306 var buf: [50]u8 = undefined;
......@@ -623,7 +621,6 @@ test "type coercion of pointer to anon struct literal to pointer to slice" {
623621 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
624622 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
625623 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
626 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
627624
628625 const S = struct {
629626 const U = union {