When compiler decide it will use generated function

Hello,

I am reading and experimenting with generated functions. The docs mention to give the compiler an option to use or not use the generated function using the construction

function foo(x)
  if @generated
      :(x+x)
  else 
     x + x
  end
end

The documentation is not very clear about at which conditions the compiler decides to use it and when not? I would like to understand these conditions, because I have just found that @generated functions can be sometimes used to create a nicer code for Zygote (manually unrolling map for example).

Thanks for an answer,
Tomas

The reason the compiler isn’t clear about this is that it can do whatever it feels like, and we don’t want to document internal behavior (because if we do, people will rely on it).

5 Likes

It was introduced in https://github.com/JuliaLang/julia/pull/23168, you might find something of use in there.

Thanks a lot,

This was a good reading.

P.S. I do not know, which answer should I mark as a solution. Jeff Bezanson’s last comment of that pull request was interesteting. Also reading about possible tests of both was great.