Skip to content
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

WIP: Give NTuple{N, T} a layout #31681

Closed
wants to merge 5 commits into from
Closed

WIP: Give NTuple{N, T} a layout #31681

wants to merge 5 commits into from

Conversation

Keno
Copy link
Member

@Keno Keno commented Apr 11, 2019

In the type system, we generally support tuples of the patter Tuple{..., Vararg{T, N}}.
However, when it comes to actually allocating these, we expand them out to
Tuple{..., T, T, T, T, T, ...}. This makes tuples not particularly suitable
as the representation for various memory buffers, because the cost of doing various
operations on their datatypes is proportional to the number of elements. This attempts
to keep them in the Vararg{T, N} form throughout the system, and similarly adding
a form of the layout specification that works for repeated trailing arguments.

Current status: Various things work, printing a tuple of this sort crashes in interesting
places. Inverse normalization (repeated trailing elements to NTuple) not yet implemented. Putting up early for feedback on the overall direction, since this involves a lot of tedious changes that I'd rather only do once.

@Keno
Copy link
Member Author

Keno commented Apr 11, 2019

Meant to make this a draft PR. Oh well.

In the type system, we generally support tuples of the patter Tuple{..., Vararg{T, N}}.
However, when it comes to actually allocating these, we expand them out to
`Tuple{..., T, T, T, T, T, ...}`. This makes tuples not particularly suitable
as the representation for various memory buffers, because the cost of doing various
operations on their datatypes is proportional to the number of elements. This attempts
to keep them in the Vararg{T, N} form throughout the system, and similarly adding
a form of the layout specification that works for repeated trailing arguments.

Current status: Various things work, printing a tuple of this sort crashes in interesting
places. Inverse normalization (repeated trailing elements to NTuple) not yet implemented.
@Keno
Copy link
Member Author

Keno commented Apr 12, 2019

Note to self. Current issue is the following subtype: Tuple{typeof(Core.Compiler.fill), Vararg{Int64, 2}} <: Tuple{typeof(Core.Compiler.fill), Any, Vararg{Union{Integer, Core.Compiler.AbstractUnitRange{T} where T}, N} where N}

Keno added a commit that referenced this pull request Apr 17, 2019
In preparation for #31681 in particular, but the problem exists on master
as well, we just tend to not encounter non-normalized tuple types that often.
Keno added a commit that referenced this pull request Apr 18, 2019
In preparation for #31681 in particular, but the problem exists on master
as well, we just tend to not encounter non-normalized tuple types that often.
Keno added a commit that referenced this pull request Apr 18, 2019
In preparation for #31681 in particular, but the problem exists on master
as well, we just tend to not encounter non-normalized tuple types that often.
@maleadt
Copy link
Member

maleadt commented May 18, 2019

Would it be possible to extend this distinction to codegen, and be able to irgen both arrays and structs from tuples? This is currently dealt with somewhat automagically:

using LLVM

julia> convert(LLVMType, Tuple{Int32, Int32})
[2 x i32]

julia> convert(LLVMType, Tuple{Int32, Int64})
{ i32, i64 }

# idea: only NTuple is passed as an LLVM array
julia> convert(LLVMType, NTuple{2,Int32})
[2 x i32]

Ran into this with @jiahao trying to llvmcall a 2-register snippet of inline assembly (the same applies to ccalling the LLVM intrinsic) which returns a {i32, i32}.

@colinxs
Copy link

colinxs commented Nov 2, 2019

Just checking in to see what the timeline for something like this might be? I'm in the process of wrapping a C library which has several structs with large fields declared as inline/fixed-size arrays (e.g. int foo[10000]). My understanding is that the only way to get a Julia struct with an equivalent memory layout is to declare that field as NTuple{10000, Int} (or as an SVector), but instantiating such a gargantuan field takes an incredible amount of time. I'm hoping something like this would make working with similar structs easier (or in some cases, even possible).

Keno added a commit that referenced this pull request Jan 23, 2021
The parameters of `sig` are not guaranteed to match the arguments
that were passed on method construction, since the type may have
been normalized. This will become more pronounced after #31681,
but with OpaqueClosure we now have some method whose sig is
Tuple{Vararg{Any}}, so we might as well make this change now.
Of course, we may want to print OpaqueClosure methods differently
in general, but that's a question for another time, once we've
worked out exactly what the sig fields of OpaqueClosure methods
should look like.
Keno added a commit that referenced this pull request Jan 31, 2021
The parameters of `sig` are not guaranteed to match the arguments
that were passed on method construction, since the type may have
been normalized. This will become more pronounced after #31681,
but with OpaqueClosure we now have some method whose sig is
Tuple{Vararg{Any}}, so we might as well make this change now.
Of course, we may want to print OpaqueClosure methods differently
in general, but that's a question for another time, once we've
worked out exactly what the sig fields of OpaqueClosure methods
should look like.
Keno added a commit that referenced this pull request Feb 1, 2021
The parameters of `sig` are not guaranteed to match the arguments
that were passed on method construction, since the type may have
been normalized. This will become more pronounced after #31681,
but with OpaqueClosure we now have some method whose sig is
Tuple{Vararg{Any}}, so we might as well make this change now.
Of course, we may want to print OpaqueClosure methods differently
in general, but that's a question for another time, once we've
worked out exactly what the sig fields of OpaqueClosure methods
should look like.
Keno added a commit that referenced this pull request Feb 1, 2021
The parameters of `sig` are not guaranteed to match the arguments
that were passed on method construction, since the type may have
been normalized. This will become more pronounced after #31681,
but with OpaqueClosure we now have some method whose sig is
Tuple{Vararg{Any}}, so we might as well make this change now.
Of course, we may want to print OpaqueClosure methods differently
in general, but that's a question for another time, once we've
worked out exactly what the sig fields of OpaqueClosure methods
should look like.
ElOceanografo pushed a commit to ElOceanografo/julia that referenced this pull request May 4, 2021
The parameters of `sig` are not guaranteed to match the arguments
that were passed on method construction, since the type may have
been normalized. This will become more pronounced after JuliaLang#31681,
but with OpaqueClosure we now have some method whose sig is
Tuple{Vararg{Any}}, so we might as well make this change now.
Of course, we may want to print OpaqueClosure methods differently
in general, but that's a question for another time, once we've
worked out exactly what the sig fields of OpaqueClosure methods
should look like.
antoine-levitt pushed a commit to antoine-levitt/julia that referenced this pull request May 9, 2021
The parameters of `sig` are not guaranteed to match the arguments
that were passed on method construction, since the type may have
been normalized. This will become more pronounced after JuliaLang#31681,
but with OpaqueClosure we now have some method whose sig is
Tuple{Vararg{Any}}, so we might as well make this change now.
Of course, we may want to print OpaqueClosure methods differently
in general, but that's a question for another time, once we've
worked out exactly what the sig fields of OpaqueClosure methods
should look like.
@vtjnash vtjnash closed this Aug 31, 2023
@vtjnash vtjnash deleted the kf/efficientntuple2 branch August 31, 2023 01:04
@vtjnash
Copy link
Sponsor Member

vtjnash commented Aug 31, 2023

Seems heavily conflicted now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants