Skip to content

@Vector -> @Type(.Vector) #5196

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 5 additions & 16 deletions doc/langref.html.in
Original file line number Diff line number Diff line change
Expand Up @@ -1850,8 +1850,8 @@ test "null terminated array" {
{#header_open|Vectors#}
<p>
A vector is a group of {#link|Integers#}, {#link|Floats#}, or {#link|Pointers#} which are operated on
in parallel using a single instruction ({#link|SIMD#}). Vector types are created with the builtin
function {#link|@Vector#}.
in parallel using a single instruction ({#link|SIMD#}). Vector types are created with the builtin function {#link|@Type#},
or using the shorthand as {#syntax#}std.meta.Vector{#endsyntax#}.
</p>
<p>
TODO talk about C ABI interop
Expand Down Expand Up @@ -7934,7 +7934,7 @@ test "@setRuntimeSafety" {
{#header_close#}

{#header_open|@shuffle#}
<pre>{#syntax#}@shuffle(comptime E: type, a: @Vector(a_len, E), b: @Vector(b_len, E), comptime mask: @Vector(mask_len, i32)) @Vector(mask_len, E){#endsyntax#}</pre>
<pre>{#syntax#}@shuffle(comptime E: type, a: std.meta.Vector(a_len, E), b: std.meta.Vector(b_len, E), comptime mask: std.meta.Vector(mask_len, i32)) std.meta.Vector(mask_len, E){#endsyntax#}</pre>
<p>
Constructs a new {#link|vector|Vectors#} by selecting elements from {#syntax#}a{#endsyntax#} and
{#syntax#}b{#endsyntax#} based on {#syntax#}mask{#endsyntax#}.
Expand Down Expand Up @@ -7990,7 +7990,7 @@ test "@setRuntimeSafety" {
{#header_close#}

{#header_open|@splat#}
<pre>{#syntax#}@splat(comptime len: u32, scalar: var) @Vector(len, @TypeOf(scalar)){#endsyntax#}</pre>
<pre>{#syntax#}@splat(comptime len: u32, scalar: var) std.meta.Vector(len, @TypeOf(scalar)){#endsyntax#}</pre>
<p>
Produces a vector of length {#syntax#}len{#endsyntax#} where each element is the value
{#syntax#}scalar{#endsyntax#}:
Expand All @@ -8002,7 +8002,7 @@ const assert = std.debug.assert;
test "vector @splat" {
const scalar: u32 = 5;
const result = @splat(4, scalar);
comptime assert(@TypeOf(result) == @Vector(4, u32));
comptime assert(@TypeOf(result) == std.meta.Vector(4, u32));
assert(std.mem.eql(u32, &@as([4]u32, result), &[_]u32{ 5, 5, 5, 5 }));
}
{#code_end#}
Expand Down Expand Up @@ -8360,17 +8360,6 @@ fn foo(comptime T: type, ptr: *T) T {
{#syntax#}@unionInit{#endsyntax#} forwards its {#link|result location|Result Location Semantics#} to {#syntax#}init_expr{#endsyntax#}.
</p>
{#header_close#}

{#header_open|@Vector#}
<pre>{#syntax#}@Vector(comptime len: u32, comptime ElemType: type) type{#endsyntax#}</pre>
<p>
This function returns a vector type for {#link|SIMD#}.
</p>
<p>
{#syntax#}ElemType{#endsyntax#} must be an {#link|integer|Integers#}, a {#link|float|Floats#}, or a
{#link|pointer|Pointers#}.
</p>
{#header_close#}
{#header_close#}

{#header_open|Build Mode#}
Expand Down
6 changes: 3 additions & 3 deletions lib/std/fmt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1670,9 +1670,9 @@ test "vector" {
return error.SkipZigTest;
}

const vbool: @Vector(4, bool) = [_]bool{ true, false, true, false };
const vi64: @Vector(4, i64) = [_]i64{ -2, -1, 0, 1 };
const vu64: @Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 };
const vbool: std.meta.Vector(4, bool) = [_]bool{ true, false, true, false };
const vi64: std.meta.Vector(4, i64) = [_]i64{ -2, -1, 0, 1 };
const vu64: std.meta.Vector(4, u64) = [_]u64{ 1000, 2000, 3000, 4000 };

try testFmt("{ true, false, true, false }", "{}", .{vbool});
try testFmt("{ -2, -1, 0, 1 }", "{}", .{vi64});
Expand Down
8 changes: 4 additions & 4 deletions lib/std/hash/auto_hash.zig
Original file line number Diff line number Diff line change
Expand Up @@ -359,13 +359,13 @@ test "testHash vector" {
// Disabled because of #3317
if (@import("builtin").arch == .mipsel or @import("builtin").arch == .mips) return error.SkipZigTest;

const a: @Vector(4, u32) = [_]u32{ 1, 2, 3, 4 };
const b: @Vector(4, u32) = [_]u32{ 1, 2, 3, 5 };
const a: meta.Vector(4, u32) = [_]u32{ 1, 2, 3, 4 };
const b: meta.Vector(4, u32) = [_]u32{ 1, 2, 3, 5 };
testing.expect(testHash(a) == testHash(a));
testing.expect(testHash(a) != testHash(b));

const c: @Vector(4, u31) = [_]u31{ 1, 2, 3, 4 };
const d: @Vector(4, u31) = [_]u31{ 1, 2, 3, 5 };
const c: meta.Vector(4, u31) = [_]u31{ 1, 2, 3, 4 };
const d: meta.Vector(4, u31) = [_]u31{ 1, 2, 3, 5 };
testing.expect(testHash(c) == testHash(c));
testing.expect(testHash(c) != testHash(d));
}
Expand Down
9 changes: 9 additions & 0 deletions lib/std/meta.zig
Original file line number Diff line number Diff line change
Expand Up @@ -650,3 +650,12 @@ pub fn IntType(comptime is_signed: bool, comptime bit_count: u16) type {
},
});
}

pub fn Vector(comptime len: u32, comptime child: type) type {
return @Type(TypeInfo{
.Vector = .{
.len = len,
.child = child,
},
});
}
2 changes: 1 addition & 1 deletion lib/std/special/compiler_rt.zig
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ comptime {
@export(@import("compiler_rt/umodti3.zig").__umodti3, .{ .name = "__umodti3", .linkage = linkage });
},
.x86_64 => {
// The "ti" functions must use @Vector(2, u64) parameter types to adhere to the ABI
// The "ti" functions must use Vector(2, u64) parameter types to adhere to the ABI
// that LLVM expects compiler-rt to have.
@export(@import("compiler_rt/divti3.zig").__divti3_windows_x86_64, .{ .name = "__divti3", .linkage = linkage });
@export(@import("compiler_rt/modti3.zig").__modti3_windows_x86_64, .{ .name = "__modti3", .linkage = linkage });
Expand Down
2 changes: 1 addition & 1 deletion lib/std/special/compiler_rt/divti3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub fn __divti3(a: i128, b: i128) callconv(.C) i128 {
return (@bitCast(i128, r) ^ s) -% s;
}

const v128 = @Vector(2, u64);
const v128 = @import("std").meta.Vector(2, u64);
pub fn __divti3_windows_x86_64(a: v128, b: v128) callconv(.C) v128 {
return @bitCast(v128, @call(.{ .modifier = .always_inline }, __divti3, .{
@bitCast(i128, a),
Expand Down
2 changes: 1 addition & 1 deletion lib/std/special/compiler_rt/modti3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn __modti3(a: i128, b: i128) callconv(.C) i128 {
return (@bitCast(i128, r) ^ s_a) -% s_a; // negate if s == -1
}

const v128 = @Vector(2, u64);
const v128 = @import("std").meta.Vector(2, u64);
pub fn __modti3_windows_x86_64(a: v128, b: v128) callconv(.C) v128 {
return @bitCast(v128, @call(.{ .modifier = .always_inline }, __modti3, .{
@bitCast(i128, a),
Expand Down
2 changes: 1 addition & 1 deletion lib/std/special/compiler_rt/multi3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn __multi3(a: i128, b: i128) callconv(.C) i128 {
return r.all;
}

const v128 = @Vector(2, u64);
const v128 = @import("std").meta.Vector(2, u64);
pub fn __multi3_windows_x86_64(a: v128, b: v128) callconv(.C) v128 {
return @bitCast(v128, @call(.{ .modifier = .always_inline }, __multi3, .{
@bitCast(i128, a),
Expand Down
2 changes: 1 addition & 1 deletion lib/std/special/compiler_rt/udivmodti4.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub fn __udivmodti4(a: u128, b: u128, maybe_rem: ?*u128) callconv(.C) u128 {
return udivmod(u128, a, b, maybe_rem);
}

const v128 = @Vector(2, u64);
const v128 = @import("std").meta.Vector(2, u64);
pub fn __udivmodti4_windows_x86_64(a: v128, b: v128, maybe_rem: ?*u128) callconv(.C) v128 {
@setRuntimeSafety(builtin.is_test);
return @bitCast(v128, udivmod(u128, @bitCast(u128, a), @bitCast(u128, b), maybe_rem));
Expand Down
2 changes: 1 addition & 1 deletion lib/std/special/compiler_rt/udivti3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ pub fn __udivti3(a: u128, b: u128) callconv(.C) u128 {
return udivmodti4.__udivmodti4(a, b, null);
}

const v128 = @Vector(2, u64);
const v128 = @import("std").meta.Vector(2, u64);
pub fn __udivti3_windows_x86_64(a: v128, b: v128) callconv(.C) v128 {
@setRuntimeSafety(builtin.is_test);
return udivmodti4.__udivmodti4_windows_x86_64(a, b, null);
Expand Down
2 changes: 1 addition & 1 deletion lib/std/special/compiler_rt/umodti3.zig
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub fn __umodti3(a: u128, b: u128) callconv(.C) u128 {
return r;
}

const v128 = @Vector(2, u64);
const v128 = @import("std").meta.Vector(2, u64);
pub fn __umodti3_windows_x86_64(a: v128, b: v128) callconv(.C) v128 {
return @bitCast(v128, @call(.{ .modifier = .always_inline }, __umodti3, .{
@bitCast(u128, a),
Expand Down
8 changes: 4 additions & 4 deletions lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -488,8 +488,8 @@ pub const Target = struct {

/// Adds the specified feature set but not its dependencies.
pub fn addFeatureSet(set: *Set, other_set: Set) void {
set.ints = @as(@Vector(usize_count, usize), set.ints) |
@as(@Vector(usize_count, usize), other_set.ints);
set.ints = @as(std.meta.Vector(usize_count, usize), set.ints) |
@as(std.meta.Vector(usize_count, usize), other_set.ints);
}

/// Removes the specified feature but not its dependents.
Expand All @@ -501,8 +501,8 @@ pub const Target = struct {

/// Removes the specified feature but not its dependents.
pub fn removeFeatureSet(set: *Set, other_set: Set) void {
set.ints = @as(@Vector(usize_count, usize), set.ints) &
~@as(@Vector(usize_count, usize), other_set.ints);
set.ints = @as(std.meta.Vector(usize_count, usize), set.ints) &
~@as(std.meta.Vector(usize_count, usize), other_set.ints);
}

pub fn populateDependencies(set: *Set, all_features_list: []const Cpu.Feature) void {
Expand Down
4 changes: 4 additions & 0 deletions src/ir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25426,6 +25426,10 @@ static ZigType *type_info_to_type(IrAnalyze *ira, IrInst *source_instr, ZigTypeI
assert(payload->type == ir_type_info_get_type(ira, "Vector", nullptr));
BigInt *len = get_const_field_lit_int(ira, source_instr->source_node, payload, "len", 0);
ZigType *child_type = get_const_field_meta_type(ira, source_instr->source_node, payload, "child", 1);
Error err;
if ((err = ir_validate_vector_elem_type(ira, source_instr->source_node, child_type))) {
return ira->codegen->invalid_inst_gen->value->type;
}
return get_vector_type(ira->codegen, bigint_as_u32(len), child_type);
}
case ZigTypeIdAnyFrame: {
Expand Down
20 changes: 11 additions & 9 deletions test/compile_errors.zig
Original file line number Diff line number Diff line change
Expand Up @@ -984,7 +984,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {

cases.add("store vector pointer with unknown runtime index",
\\export fn entry() void {
\\ var v: @Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
\\ var v: @import("std").meta.Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
\\
\\ var i: u32 = 0;
\\ storev(&v[i], 42);
Expand All @@ -999,7 +999,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {

cases.add("load vector pointer with unknown runtime index",
\\export fn entry() void {
\\ var v: @Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
\\ var v: @import("std").meta.Vector(4, i32) = [_]i32{ 1, 5, 3, undefined };
\\
\\ var i: u32 = 0;
\\ var x = loadv(&v[i]);
Expand Down Expand Up @@ -1885,8 +1885,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {

cases.addTest("comptime vector overflow shows the index",
\\comptime {
\\ var a: @Vector(4, u8) = [_]u8{ 1, 2, 255, 4 };
\\ var b: @Vector(4, u8) = [_]u8{ 5, 6, 1, 8 };
\\ var a: @import("std").meta.Vector(4, u8) = [_]u8{ 1, 2, 255, 4 };
\\ var b: @import("std").meta.Vector(4, u8) = [_]u8{ 5, 6, 1, 8 };
\\ var x = a + b;
\\}
, &[_][]const u8{
Expand Down Expand Up @@ -6846,8 +6846,8 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {

cases.addTest("@shuffle with selected index past first vector length",
\\export fn entry() void {
\\ const v: @Vector(4, u32) = [4]u32{ 10, 11, 12, 13 };
\\ const x: @Vector(4, u32) = [4]u32{ 14, 15, 16, 17 };
\\ const v: @import("std").meta.Vector(4, u32) = [4]u32{ 10, 11, 12, 13 };
\\ const x: @import("std").meta.Vector(4, u32) = [4]u32{ 14, 15, 16, 17 };
\\ var z = @shuffle(u32, v, x, [8]i32{ 0, 1, 2, 3, 7, 6, 5, 4 });
\\}
, &[_][]const u8{
Expand All @@ -6858,11 +6858,13 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {

cases.addTest("nested vectors",
\\export fn entry() void {
\\ const V = @Vector(4, @Vector(4, u8));
\\ var v: V = undefined;
\\ const V1 = @import("std").meta.Vector(4, u8);
\\ const V2 = @Type(@import("builtin").TypeInfo{ .Vector = .{ .len = 4, .child = V1 } });
\\ var v: V2 = undefined;
\\}
, &[_][]const u8{
"tmp.zig:2:26: error: vector element type must be integer, float, bool, or pointer; '@Vector(4, u8)' is invalid",
"tmp.zig:3:49: error: vector element type must be integer, float, bool, or pointer; '@Vector(4, u8)' is invalid",
"tmp.zig:3:16: note: referenced here",
});

cases.addTest("bad @splat type",
Expand Down
40 changes: 20 additions & 20 deletions test/runtime_safety.zig
Original file line number Diff line number Diff line change
Expand Up @@ -472,11 +472,11 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ @import("std").os.exit(126);
\\}
\\pub fn main() void {
\\ var a: @Vector(4, i32) = [_]i32{ 1, 2, 2147483643, 4 };
\\ var b: @Vector(4, i32) = [_]i32{ 5, 6, 7, 8 };
\\ var a: @import("std").meta.Vector(4, i32) = [_]i32{ 1, 2, 2147483643, 4 };
\\ var b: @import("std").meta.Vector(4, i32) = [_]i32{ 5, 6, 7, 8 };
\\ const x = add(a, b);
\\}
\\fn add(a: @Vector(4, i32), b: @Vector(4, i32)) @Vector(4, i32) {
\\fn add(a: @import("std").meta.Vector(4, i32), b: @import("std").meta.Vector(4, i32)) @import("std").meta.Vector(4, i32) {
\\ return a + b;
\\}
);
Expand All @@ -486,11 +486,11 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ @import("std").os.exit(126);
\\}
\\pub fn main() void {
\\ var a: @Vector(4, u32) = [_]u32{ 1, 2, 8, 4 };
\\ var b: @Vector(4, u32) = [_]u32{ 5, 6, 7, 8 };
\\ var a: @import("std").meta.Vector(4, u32) = [_]u32{ 1, 2, 8, 4 };
\\ var b: @import("std").meta.Vector(4, u32) = [_]u32{ 5, 6, 7, 8 };
\\ const x = sub(b, a);
\\}
\\fn sub(a: @Vector(4, u32), b: @Vector(4, u32)) @Vector(4, u32) {
\\fn sub(a: @import("std").meta.Vector(4, u32), b: @import("std").meta.Vector(4, u32)) @import("std").meta.Vector(4, u32) {
\\ return a - b;
\\}
);
Expand All @@ -500,11 +500,11 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ @import("std").os.exit(126);
\\}
\\pub fn main() void {
\\ var a: @Vector(4, u8) = [_]u8{ 1, 2, 200, 4 };
\\ var b: @Vector(4, u8) = [_]u8{ 5, 6, 2, 8 };
\\ var a: @import("std").meta.Vector(4, u8) = [_]u8{ 1, 2, 200, 4 };
\\ var b: @import("std").meta.Vector(4, u8) = [_]u8{ 5, 6, 2, 8 };
\\ const x = mul(b, a);
\\}
\\fn mul(a: @Vector(4, u8), b: @Vector(4, u8)) @Vector(4, u8) {
\\fn mul(a: @import("std").meta.Vector(4, u8), b: @import("std").meta.Vector(4, u8)) @import("std").meta.Vector(4, u8) {
\\ return a * b;
\\}
);
Expand All @@ -514,10 +514,10 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ @import("std").os.exit(126);
\\}
\\pub fn main() void {
\\ var a: @Vector(4, i16) = [_]i16{ 1, -32768, 200, 4 };
\\ var a: @import("std").meta.Vector(4, i16) = [_]i16{ 1, -32768, 200, 4 };
\\ const x = neg(a);
\\}
\\fn neg(a: @Vector(4, i16)) @Vector(4, i16) {
\\fn neg(a: @import("std").meta.Vector(4, i16)) @import("std").meta.Vector(4, i16) {
\\ return -a;
\\}
);
Expand Down Expand Up @@ -579,12 +579,12 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ @import("std").os.exit(126);
\\}
\\pub fn main() !void {
\\ var a: @Vector(4, i16) = [_]i16{ 1, 2, -32768, 4 };
\\ var b: @Vector(4, i16) = [_]i16{ 1, 2, -1, 4 };
\\ var a: @import("std").meta.Vector(4, i16) = [_]i16{ 1, 2, -32768, 4 };
\\ var b: @import("std").meta.Vector(4, i16) = [_]i16{ 1, 2, -1, 4 };
\\ const x = div(a, b);
\\ if (x[2] == 32767) return error.Whatever;
\\}
\\fn div(a: @Vector(4, i16), b: @Vector(4, i16)) @Vector(4, i16) {
\\fn div(a: @import("std").meta.Vector(4, i16), b: @import("std").meta.Vector(4, i16)) @import("std").meta.Vector(4, i16) {
\\ return @divTrunc(a, b);
\\}
);
Expand Down Expand Up @@ -658,11 +658,11 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ @import("std").os.exit(126);
\\}
\\pub fn main() void {
\\ var a: @Vector(4, i32) = [4]i32{111, 222, 333, 444};
\\ var b: @Vector(4, i32) = [4]i32{111, 0, 333, 444};
\\ var a: @import("std").meta.Vector(4, i32) = [4]i32{111, 222, 333, 444};
\\ var b: @import("std").meta.Vector(4, i32) = [4]i32{111, 0, 333, 444};
\\ const x = div0(a, b);
\\}
\\fn div0(a: @Vector(4, i32), b: @Vector(4, i32)) @Vector(4, i32) {
\\fn div0(a: @import("std").meta.Vector(4, i32), b: @import("std").meta.Vector(4, i32)) @import("std").meta.Vector(4, i32) {
\\ return @divTrunc(a, b);
\\}
);
Expand All @@ -685,11 +685,11 @@ pub fn addCases(cases: *tests.CompareOutputContext) void {
\\ @import("std").os.exit(126);
\\}
\\pub fn main() !void {
\\ var a: @Vector(4, i32) = [4]i32{111, 222, 333, 444};
\\ var b: @Vector(4, i32) = [4]i32{111, 222, 333, 441};
\\ var a: @import("std").meta.Vector(4, i32) = [4]i32{111, 222, 333, 444};
\\ var b: @import("std").meta.Vector(4, i32) = [4]i32{111, 222, 333, 441};
\\ const x = divExact(a, b);
\\}
\\fn divExact(a: @Vector(4, i32), b: @Vector(4, i32)) @Vector(4, i32) {
\\fn divExact(a: @import("std").meta.Vector(4, i32), b: @import("std").meta.Vector(4, i32)) @import("std").meta.Vector(4, i32) {
\\ return @divExact(a, b);
\\}
);
Expand Down
4 changes: 2 additions & 2 deletions test/stage1/behavior/byteswap.zig
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ test "@byteSwap vectors" {
fn t(
comptime I: type,
comptime n: comptime_int,
input: @Vector(n, I),
expected_vector: @Vector(n, I),
input: std.meta.Vector(n, I),
expected_vector: std.meta.Vector(n, I),
) void {
const actual_output: [n]I = @byteSwap(I, input);
const expected_output: [n]I = expected_vector;
Expand Down
Loading