diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2022-03-11 13:48:28 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-03-11 13:48:28 -0500 |
| commit | 4c1cc4d8d91af459e5ac1e20b744c7dc9b5b8da3 (patch) | |
| tree | 8ee7c3b7f2b4c66e9ed8599b9c5164b78d1cc43c /src/codegen/llvm.zig | |
| parent | 5fbae9cd6fe7a53d85ad6dd20cab75f7b835f0ad (diff) | |
| parent | 03b8206f27485f871fc489f884ffbc276d61877c (diff) | |
| download | zig-4c1cc4d8d91af459e5ac1e20b744c7dc9b5b8da3.tar.gz zig-4c1cc4d8d91af459e5ac1e20b744c7dc9b5b8da3.zip | |
Merge pull request #11120 from Vexu/stage2
Stage2: make std.rand tests pass
Diffstat (limited to 'src/codegen/llvm.zig')
| -rw-r--r-- | src/codegen/llvm.zig | 38 |
1 files changed, 38 insertions, 0 deletions
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; |
