Stream: helpdesk (published)

Topic: Annotation to elide function call if return value is unused


view this post on Zulip Gustavo Goretkin (Feb 23 2021 at 03:09):

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.

view this post on Zulip Gustavo Goretkin (Feb 24 2021 at 01:17):

I asked here: https://discourse.julialang.org/t/annotation-to-elide-function-call-if-the-return-value-is-unused/55904

view this post on Zulip Mason Protter (Feb 24 2021 at 01:41):

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.

view this post on Zulip Gustavo Goretkin (Feb 24 2021 at 02:32):

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.

view this post on Zulip Fredrik Bagge Carlson (Feb 24 2021 at 17:41):

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.

view this post on Zulip Gustavo Goretkin (Feb 24 2021 at 21:50):

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.

view this post on Zulip Gustavo Goretkin (Feb 24 2021 at 21:51):

In the example on discourse, I used printing to stdout to indicate that a computation was performed. That's a side-effect.

view this post on Zulip Brian Chen (Feb 25 2021 at 00:50):

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

view this post on Zulip Gustavo Goretkin (Mar 11 2021 at 17:41):

@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: Oct 02 2023 at 04:34 UTC