Is there some annotation that I can use to give permission to the compiler to elide e.g. x = foo(y)
if x
is not used? And would it possibly work across function-call boundaries? I'd like to do something like
function compute(input, callback)
x = foo(input)
y = bar(input)
z = baz(x, y)
return callback((;x, y, z))
end
and depending on what fields callback
accesses, perhaps only foo
is called. Or only bar
. Or both. Or all three.
I'm pretty sure Base.@pure
is the wrong answer.
I asked here: https://discourse.julialang.org/t/annotation-to-elide-function-call-if-the-return-value-is-unused/55904
I remember discussing this with @Lyndon White a while ago on Slack I think. We concluded essentially that this might be possible with a compiler pass, but in general it’s a hard thing to do due to Julia’s eager semantics.
I wonder what difficulties you foresee? It seems like you could "just" knock out lines from the SSA form where the right-hand side has the annotation and nothing refers to the left-hand side.
You must prove that the calculation of the unused outputs does not have any side effect which will no longer happen if you remove the calculation. I have no experience with this stuff so maybe that is trivial, but might not be, or might be expensive etc.
I agree with "You must prove" if you mean, "you, the entity that decides to place this annotation on the function call". It's not something the compiler needs to figure out.
In the example on discourse, I used printing to stdout to indicate that a computation was performed. That's a side-effect.
Is there a way to sneak extra attributes into the CodeInfo
using pushmeta!
or something similar? I thought @inline
would be a good example, but it appears to be special-cased in https://github.com/JuliaLang/julia/blob/master/src/method.c#L272
@Brian Chen I wish I knew the answer. The comment at the end of https://docs.julialang.org/en/v1.1/devdocs/meta/ slightly suggests the answer is no:
Not yet provided is a convenient infrastructure for parsing
:meta
expressions from C++.
But that doc hasn't been touched since 2016.
Last updated: Nov 06 2024 at 04:40 UTC