-
Notifications
You must be signed in to change notification settings - Fork 153
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
RFC: Allow indexing with non-static ranges to produce static arrays #703
base: master
Are you sure you want to change the base?
Conversation
Interesting, thanks for submitting this. There's a lot of syntactic appeal in having The issue I see is that people will be tempted to use this for non-constant ranges So to the extent that Also some quick testing made me worried about whether this works as expected:
I think |
Thanks for the review!
Right, I should have clarified some of the thought process leading to this PR. In #633, we observed that constant folding works in Looking at the IR from |
Cool thanks for the extra explanation. My worry here is that indexing an array with a range is quite natural to do deep in a call tree (the case of "not near enough" so that constant propagation fails). Indexing with a non-constant range in generic array code is also natural. Both of these cases would behave worse after this change, I assume. Conversely, generic code which is passed an So I think there's some pros and cons of doing this, and without it being clear what the best approach is, it may be better just not to break things. But it would be interesting to see how far you can push it to make it work well and whether it helps in practical use cases :-) By the way I feel that construction is somewhat different: in that case the user is likely to be explicitly naming the type |
Ah, good point. I think I'm convinced that the analogy to the constructor does not hold. I think another approach for nicer slicing API is to use StaticNumbers.jl and do One solution may be to define a "contagious" static number subtype such that:
This way, we can just write One concern is that if it is OK to return non- |
Oh, that's very clever indeed. And might even be fully satisfying now that JuliaLang/julia#33946 is merged! |
I'd be happy to take a
To expand on why I think this is rather satisfying: in 1.4 the user may refer to both ends of the array, eg As a completely different — perhaps complimentary — idea; it might also be possible to have an efficient non-allocating slice type which carries a reference to At a meta level, the dissatisfying thing about all these options (even the very clever idea of hooking |
I guess a safer implementation is to require all the operands to be static numbers for the special static number to be contagious. That is to say, we have
I think it'd be great to have this optionally and practically super useful. But isn't it like |
For info: In StaticNumbers, I've implemented a macro that makes the static-ness contagious within a block of code. This makes it possible to do indexing using variables and get an Array/SArray depending on whether everything is static in computing the index. Example:
|
Yeah you're right about that. That's why I put "probably like" in quotes... it seems practical and useful but also kind of wrong.
That does seem like the safe and practical thing to do. If we could semantically hand out immutable arrays from indexing and the optimizer could constant propagate short lived temporaries of that type we'd be in a good situation. It seems like a lot to ask of the compiler but perhaps small tensor-like objects are fundamental enough that we can expect the compiler to know about them. |
I agree with this - for years I have always planned to deprecate this package once that is working reliably! |
Haha yes! But that's just a small part of the story; all those small-sized linear solvers and other fancy things will presumably need a home. And while const-prop will be ok for arrays which live on the stack and don't escape... something like |
This PR expands
getindex
definition so that non-static indices still produces static arrays. It also includes some tests to make sure that constant folding works withjulia
>= 1.2.