-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Vector element access #3575
Vector element access #3575
Conversation
I'm confused, if |
@dbandstra here's a test case that is expected to pass once #903 is closed: const std = @import("std");
const expect = std.testing.expect;
test "vector of pointers" {
var array0: [10]i32 = undefined;
var array1: [10]i32 = undefined;
var array2: [10]i32 = undefined;
var array3: [10]i32 = undefined;
const vector_of_ptrs: @Vector(4, [*]i32) = [_][*]i32{ &array0, &array1, &array2, &array3 };
vector_of_ptrs[1] = 42;
expect(array0[1] == 42);
expect(array1[1] == 42);
expect(array2[1] == 42);
expect(array3[1] == 42);
} Does that help clarify? |
Thanks I get it now. I had something like I guess |
What code would actually use this form of dereferencing? There is no
hardware instruction for this type of thing either.
El sáb., 2 nov. 2019 0:55, dbandstra <notifications@github.com> escribió:
… Thanks I get it now. I had something like @vector(4, *i32) in my head. I
didn't consider a vector of pointers to arrays.
I guess .* syntax will also go "through" the vector? (Meaning you would
need another builtin function to be able to dereference a pointer to a
vector?)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#3575?email_source=notifications&email_token=AAD4W4QJCQMTMTOYVVW5PWDQRUXAVA5CNFSM4JICL6XKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEC4WHCQ#issuecomment-549020554>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAD4W4RNLQRLYDSCK3LYW7DQRUXAVANCNFSM4JICL6XA>
.
|
Good question. Vectors of integers act like integers, allowing operations that work on the element type. Vectors of pointers act like pointers, allowing operations that work on the element type. Just like
Vectors of pointers makes sense, and ability to dereference vectors of pointers follows naturally. One can do all the operations on a vector of elements that one can do on the elements themselves. LLVM supports this as well. In theory hardware could support for this, although I do not know of any architectures that do this. |
yes, you can do this with vectorgather and vectorscatter, and my patch has that ( |
Corrected test case (note that the index is now a vector): const std = @import("std");
const expect = std.testing.expect;
test "vector of pointers" {
var array0: [10]i32 = undefined;
var array1: [10]i32 = undefined;
var array2: [10]i32 = undefined;
var array3: [10]i32 = undefined;
const vector_of_ptrs: @Vector(4, [*]i32) = [_][*]i32{ &array0, &array1, &array2, &array3 };
const index: @Vector(4, u32) == [_]u32{5, 2, 3, 6};
vector_of_ptrs[index] = [_]i32{11, 22, 33, 44};
expect(array0[5] == 11);
expect(array1[2] == 22);
expect(array2[3] == 33);
expect(array3[6] == 44);
} |
Here is a test case it fails:
There are two problems here: supporting the C ABI here, and doing pointer arithmetic on the passed pointer. (I am not sure if I understand the difference between |
I'm not sure I like this dereference idea. It feels opaque and hard to read when passing through code. We could also just disallow indexing on vectors and expect the user to bitcast or provide builtins (as Andrew first suggested). |
@dimenus accessing elements of vectors this way is extremely common in code, and it is the way C does it. Andrew's dereference idea is ambiguous with my current proposed patch that turns vectors with vector indexes into a @Shuffle, that returns just the indexed elements. In short, it is ambiguous if the indexes refer to elements of the vector, or elements of the array's the vector points to (if they are all pointers and all support pointer arithmetic). Also, such an operation would be limited by the type system, as they would all have to be the same type, and the typical use-case of them all having the same base address is already covered by supporting |
Here is a test case based on @dimenus test cases, and I think it requires ResultLoc to work correctly.
|
<andrewrk> tomorrow, end of day, ziglang#3580 will be closed and ziglang#3575 will be closed, and dimenus's examples will work (except for vector literals which is not happening, see ziglang#208)
This is my re-implementation of the vector element access from #2945.
I went through all this trouble, but then I realized that I had already considered this when I typed up #903 (comment):
Compile error for trying to get a pointer to a vector element? But why? In this PR you can see it works just fine.
Because, my friends, vectors can be vectors of pointers, in which case
x[y]
should produce a new vector with each pointer dereferenced, not access a single element.So this was a waste of time. Element access of vectors can be implemented either as builtin functions
@vectorElemLoad
and@vectorElemStore
, or even in userland, by bitcasting a vector to and from an array. Butx[y]
syntax is reserved for when you have a vector of pointers.cc @shawnl
cc @dimenus