authorgravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-12-11 15:18:33+01:00
committergravatar for luuk@degram.devLuuk de Gram <luuk@degram.dev> 2022-12-12 17:42:00+01:00
log355b5929b2c49422b92e7aa7374c7e4f0008f400
tree3a191aa6206f967e497e256dd7562c6b5ac985dc
parentc6d654f73bbe80ed3653be6a31ddcaa4772a4fe2
signaturelock-open Commit is signed but in an unrecognized format.

wasm: `splat` for vector elements divisible by 8

This implements `@splat` for vectors where the element type is divisible by 8 and a power of two. This is fairly simple as we can store the values directly within the virtual stack. But for all other sizes, we must first shift and bitwise-or the values before we can store them to fit them like a packed-struct, rather than an array.

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

src/arch/wasm/CodeGen.zig+17-2
......@@ -4433,7 +4433,6 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
44334433 const ty = func.air.typeOfIndex(inst);
44344434 const elem_ty = ty.childType();
44354435
4436 const result = try func.allocLocal(ty);
44374436 if (determineSimdStoreStrategy(ty, func.target) == .direct) blk: {
44384437 switch (operand) {
44394438 // when the operand lives in the linear memory section, we can directly
......@@ -4447,6 +4446,7 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
44474446 64 => std.wasm.simdOpcode(.v128_load64_splat),
44484447 else => break :blk, // Cannot make use of simd-instructions
44494448 };
4449 const result = try func.allocLocal(ty);
44504450 try func.emitWValue(operand);
44514451 // TODO: Add helper functions for simd opcodes
44524452 const extra_index = @intCast(u32, func.mir_extra.items.len);
......@@ -4468,6 +4468,7 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
44684468 64 => if (elem_ty.isInt()) std.wasm.simdOpcode(.i64x2_splat) else std.wasm.simdOpcode(.f64x2_splat),
44694469 else => break :blk, // Cannot make use of simd-instructions
44704470 };
4471 const result = try func.allocLocal(ty);
44714472 try func.emitWValue(operand);
44724473 const extra_index = @intCast(u32, func.mir_extra.items.len);
44734474 try func.mir_extra.append(func.gpa, opcode);
......@@ -4478,8 +4479,22 @@ fn airSplat(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
44784479 else => unreachable,
44794480 }
44804481 }
4482 const elem_size = elem_ty.bitSize(func.target);
4483 const vector_len = @intCast(usize, ty.vectorLen());
4484 if ((!std.math.isPowerOfTwo(elem_size) or elem_size % 8 != 0) and vector_len > 1) {
4485 return func.fail("TODO: WebAssembly `@splat` for arbitrary element bitsize {d}", .{elem_size});
4486 }
4487
4488 const result = try func.allocStack(ty);
4489 const elem_byte_size = @intCast(u32, elem_ty.abiSize(func.target));
4490 var index: usize = 0;
4491 var offset: u32 = 0;
4492 while (index < vector_len) : (index += 1) {
4493 try func.store(result, operand, elem_ty, offset);
4494 offset += elem_byte_size;
4495 }
44814496
4482 return func.fail("TODO: Implement wasm airSplat unrolled", .{});
4497 return func.finishAir(inst, result, &.{ty_op.operand});
44834498}
44844499
44854500fn airSelect(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {