authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-18 19:25:52+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-20 17:30:22+02:00
log64f99f36a6d960a97d241c3235653c464f7ac6b2
tree1901e5e51fd73a6e005a919d4e4d6ffcbb1db87f
parent3c14438a937eb5b470f7f6191d850030aa7a4a06
signaturelock-open Commit is signed but in an unrecognized format.

spirv: ptr_add

Implements the ptr_add air tag for spirv. The implementation for slices is probably wrong, but there seems to be no test for this...

2 files changed, 29 insertions(+), 1 deletions(-)

src/codegen/spirv.zig+29
......@@ -1738,6 +1738,8 @@ pub const DeclGen = struct {
17381738
17391739 .shuffle => try self.airShuffle(inst),
17401740
1741 .ptr_add => try self.airPtrAdd(inst),
1742
17411743 .bit_and => try self.airBinOpSimple(inst, .OpBitwiseAnd),
17421744 .bit_or => try self.airBinOpSimple(inst, .OpBitwiseOr),
17431745 .xor => try self.airBinOpSimple(inst, .OpBitwiseXor),
......@@ -2120,6 +2122,33 @@ pub const DeclGen = struct {
21202122 return result_id;
21212123 }
21222124
2125 fn airPtrAdd(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2126 if (self.liveness.isUnused(inst)) return null;
2127 const ty_pl = self.air.instructions.items(.data)[inst].ty_pl;
2128 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
2129 const ptr_id = try self.resolve(bin_op.lhs);
2130 const offset_id = try self.resolve(bin_op.rhs);
2131 const ptr_ty = self.air.typeOf(bin_op.lhs);
2132 const result_ty = self.air.typeOfIndex(inst);
2133 const result_ty_ref = try self.resolveType(result_ty, .direct);
2134
2135 switch (ptr_ty.ptrSize()) {
2136 .One => {
2137 // Pointer to array
2138 // TODO: Is this correct?
2139 return try self.accessChain(result_ty_ref, ptr_id, &.{offset_id});
2140 },
2141 .C, .Many => {
2142 return try self.ptrAccessChain(result_ty_ref, ptr_id, offset_id, &.{});
2143 },
2144 .Slice => {
2145 // TODO: This is probably incorrect. A slice should be returned here, though this is what llvm does.
2146 const slice_ptr_id = try self.extractField(result_ty, ptr_id, 0);
2147 return try self.ptrAccessChain(result_ty_ref, slice_ptr_id, offset_id, &.{});
2148 },
2149 }
2150 }
2151
21232152 fn cmp(
21242153 self: *DeclGen,
21252154 comptime op: std.math.CompareOperator,
test/behavior/basic.zig-1
......@@ -751,7 +751,6 @@ fn maybe(x: bool) anyerror!?u32 {
751751test "auto created variables have correct alignment" {
752752 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
753753 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
754 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
755754
756755 const S = struct {
757756 fn foo(str: [*]const u8) u32 {