- Sponsor
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
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
LICM for pure functions #29285
Comments
Sure, both of those workarounds could work, but I'd very much rather directly tell the compiler about a reasonable level of purity than push an orthogonal concern into the already-complicated broadcasting API. I must say, though: it is doing exactly what you requested and performing within 5% of the equivalent for loop. Folks rely on this behavior for non-pure functions like |
Dup of #20875 |
Yeah I know ;) So it might be much better to educate the user - I wouldn't mind if this issue "derails" into how to better inform the user of such gotchas! |
from the title change, I assume that @vtjnash is already on the right track for my next boiled down example: using BenchmarkTools
Base.@pure @noinline sopure(x) = x / 2
function test(x, y)
#c = sopure(y)
for i = 1:length(x)
@inbounds x[i] = sopure(y) # c
end
x
end
@btime test($(rand(10)), $(Ref(1.0))[]) # when commenting out c 14ns, with c 5ns This doesn't hoist, and If I'm not mistaken should be the most straightforward example for LICM? |
Now that the issue is pretty clearly outlined, my main question is: |
Would it be possible to enlighten me a bit about the feasibility and some background about this problem? Just a short statement, that I can tell to the people wondering why Julia's compiler has a problems with this ;) I feel, like I've seen a discussion about this before, but when I search for |
#9942 maybe relevant? |
I guess ;) |
Julia 1.0:
I just realized, that this is a gotcha one easily runs into, especially when using the
@.
macro:This likely happens because the compiler can't infer that sin is pure.
I realize, with having access to the call tree in the new lazy broadcast, we could solve this for a predefined set of functions.
First trick could be to just overload
broadcasted
for known signatures:this solves the problem for a chosen set of functions.
We could also consider, if we introduce a purity trait to make this easier for multiple argument functions:
I guess this has been discussed before, but I couldn't really find an issue about it...
The text was updated successfully, but these errors were encountered: