mercredi 21 août 2019

How exactly is "interacting with Core.Compiler" undefined in a generated function?

The docs for generated functions at some point say the following:

Some operations that should not be attempted include:

  1. ...

  2. Interacting with the contents or methods of Core.Compiler in any way.

What exactly is meant by "interacting" in this context? Is just using things from Core.Compiler from within a generated function undefined behaviour?

My use case is to detect builtin functions from within an IRTools dynamo (which constructs generated functions), but you can refer to the following dummy code (inspired by Cassette.canrecurse), which contains the actual "interactions" I want to perform:

julia> @generated function foo(f, args...)
           mod = Core.Compiler.typename(f).module
           is_builtin = ((f <: Core.Builtin) && !(mod === Core.Compiler))
           if is_builtin
               quote
                   println("$f is builtin")
                   f(args...)
               end
           else
               :(f(args...))
           end
       end

foo (generic function with 1 method)

julia> foo(+, 1, 2)
3

julia> foo(Core.tuple, 1, 2)
tuple is builtin
(1, 2)

Which seems to work without problems.





Aucun commentaire:

Enregistrer un commentaire