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...
This commit is contained in:
Robin Voetter 2023-05-18 19:25:52 +02:00
parent 3c14438a93
commit 64f99f36a6
No known key found for this signature in database
GPG Key ID: E755662F227CB468
2 changed files with 29 additions and 1 deletions

View File

@ -1738,6 +1738,8 @@ pub const DeclGen = struct {
.shuffle => try self.airShuffle(inst),
.ptr_add => try self.airPtrAdd(inst),
.bit_and => try self.airBinOpSimple(inst, .OpBitwiseAnd),
.bit_or => try self.airBinOpSimple(inst, .OpBitwiseOr),
.xor => try self.airBinOpSimple(inst, .OpBitwiseXor),
@ -2120,6 +2122,33 @@ pub const DeclGen = struct {
return result_id;
}
fn airPtrAdd(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_id = try self.resolve(bin_op.lhs);
const offset_id = try self.resolve(bin_op.rhs);
const ptr_ty = self.air.typeOf(bin_op.lhs);
const result_ty = self.air.typeOfIndex(inst);
const result_ty_ref = try self.resolveType(result_ty, .direct);
switch (ptr_ty.ptrSize()) {
.One => {
// Pointer to array
// TODO: Is this correct?
return try self.accessChain(result_ty_ref, ptr_id, &.{offset_id});
},
.C, .Many => {
return try self.ptrAccessChain(result_ty_ref, ptr_id, offset_id, &.{});
},
.Slice => {
// TODO: This is probably incorrect. A slice should be returned here, though this is what llvm does.
const slice_ptr_id = try self.extractField(result_ty, ptr_id, 0);
return try self.ptrAccessChain(result_ty_ref, slice_ptr_id, offset_id, &.{});
},
}
}
fn cmp(
self: *DeclGen,
comptime op: std.math.CompareOperator,

View File

@ -751,7 +751,6 @@ fn maybe(x: bool) anyerror!?u32 {
test "auto created variables have correct alignment" {
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;
const S = struct {
fn foo(str: [*]const u8) u32 {