aboutsummaryrefslogtreecommitdiff
path: root/src/codegen
diff options
context:
space:
mode:
authorAndrew Kelley <andrew@ziglang.org>2022-03-11 13:48:28 -0500
committerGitHub <noreply@github.com>2022-03-11 13:48:28 -0500
commit4c1cc4d8d91af459e5ac1e20b744c7dc9b5b8da3 (patch)
tree8ee7c3b7f2b4c66e9ed8599b9c5164b78d1cc43c /src/codegen
parent5fbae9cd6fe7a53d85ad6dd20cab75f7b835f0ad (diff)
parent03b8206f27485f871fc489f884ffbc276d61877c (diff)
downloadzig-4c1cc4d8d91af459e5ac1e20b744c7dc9b5b8da3.tar.gz
zig-4c1cc4d8d91af459e5ac1e20b744c7dc9b5b8da3.zip
Merge pull request #11120 from Vexu/stage2
Stage2: make std.rand tests pass
Diffstat (limited to 'src/codegen')
-rw-r--r--src/codegen/c.zig16
-rw-r--r--src/codegen/llvm.zig38
2 files changed, 54 insertions, 0 deletions
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;