From cba68090a60c3de8eadbf8eb53e37620a1d66683 Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Fri, 11 Mar 2022 13:12:32 +0200 Subject: stage2: implement `@shuffle` at runtime --- src/codegen/c.zig | 16 ++++++++++++++++ src/codegen/llvm.zig | 38 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) (limited to 'src/codegen') diff --git a/src/codegen/c.zig b/src/codegen/c.zig index 4a30bf023b..c179b65046 100644 --- a/src/codegen/c.zig +++ b/src/codegen/c.zig @@ -1716,6 +1716,7 @@ fn genBody(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail, OutO .tag_name => try airTagName(f, inst), .error_name => try airErrorName(f, inst), .splat => try airSplat(f, inst), + .shuffle => try airShuffle(f, inst), .aggregate_init => try airAggregateInit(f, inst), .union_init => try airUnionInit(f, inst), .prefetch => try airPrefetch(f, inst), @@ -3557,6 +3558,21 @@ fn airSplat(f: *Function, inst: Air.Inst.Index) !CValue { return f.fail("TODO: C backend: implement airSplat", .{}); } +fn airShuffle(f: *Function, inst: Air.Inst.Index) !CValue { + if (f.liveness.isUnused(inst)) return CValue.none; + + const inst_ty = f.air.typeOfIndex(inst); + const ty_op = f.air.instructions.items(.data)[inst].ty_op; + const operand = try f.resolveInst(ty_op.operand); + const writer = f.object.writer(); + const local = try f.allocLocal(inst_ty, .Const); + try writer.writeAll(" = "); + + _ = operand; + _ = local; + return f.fail("TODO: C backend: implement airShuffle", .{}); +} + fn airAggregateInit(f: *Function, inst: Air.Inst.Index) !CValue { if (f.liveness.isUnused(inst)) return CValue.none; diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index d7df161b00..190fcd0ee6 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -3204,6 +3204,7 @@ pub const FuncGen = struct { .tag_name => try self.airTagName(inst), .error_name => try self.airErrorName(inst), .splat => try self.airSplat(inst), + .shuffle => try self.airShuffle(inst), .aggregate_init => try self.airAggregateInit(inst), .union_init => try self.airUnionInit(inst), .prefetch => try self.airPrefetch(inst), @@ -5850,6 +5851,43 @@ pub const FuncGen = struct { return self.builder.buildShuffleVector(op_vector, undef_vector, mask_llvm_ty.constNull(), ""); } + fn airShuffle(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value { + if (self.liveness.isUnused(inst)) return null; + + const ty_pl = self.air.instructions.items(.data)[inst].ty_pl; + const extra = self.air.extraData(Air.Shuffle, ty_pl.payload).data; + const a = try self.resolveInst(extra.a); + const b = try self.resolveInst(extra.b); + const mask = self.air.values[extra.mask]; + const mask_len = extra.mask_len; + const a_len = self.air.typeOf(extra.a).vectorLen(); + + // LLVM uses integers larger than the length of the first array to + // index into the second array. This was deemed unnecessarily fragile + // when changing code, so Zig uses negative numbers to index the + // second vector. These start at -1 and go down, and are easiest to use + // with the ~ operator. Here we convert between the two formats. + const values = try self.gpa.alloc(*const llvm.Value, mask_len); + defer self.gpa.free(values); + + const llvm_i32 = self.context.intType(32); + + for (values) |*val, i| { + var buf: Value.ElemValueBuffer = undefined; + const elem = mask.elemValueBuffer(i, &buf); + if (elem.isUndef()) { + val.* = llvm_i32.getUndef(); + } else { + const int = elem.toSignedInt(); + const unsigned = if (int >= 0) @intCast(u32, int) else @intCast(u32, ~int + a_len); + val.* = llvm_i32.constInt(unsigned, .False); + } + } + + const llvm_mask_value = llvm.constVector(values.ptr, mask_len); + return self.builder.buildShuffleVector(a, b, llvm_mask_value, ""); + } + fn airAggregateInit(self: *FuncGen, inst: Air.Inst.Index) !?*const llvm.Value { if (self.liveness.isUnused(inst)) return null; -- cgit v1.2.3