Resolve GlobalRef in a generated function

I’m using @dynamo from IRTools to rewrite all calls inside a passed function. There is a set of functions - let’s call them primitives - that I don’t want to rewrite, and thus I need to check if a particular function is in this set during code transformation. However:

  1. IRTools.@dynamo utilizes Julia’s generated functions to let us transform code. As we know, generated functions act during compilation (or rather compilation-evaluation loop).
  2. In the transformed code functions are represented as unresolved GlobalRefs. E.g. function + may be represented as GlobalRef(Base, :+) or GlobalRef(Main, :+), or in some other similar way.
  3. In normal circumstances I would resolve GlobalRef to function instance using eval, however eval cannot be used in generated functions.

My current approach is to generate all possible GlobalRefs that can appear in the code, but it doesn’t seem to be very robust.

Is there a way to get a GlobalRef(some_module, function_name) and resolve it a generated function?

1 Like

You don’t need eval here, you can just use getfield or getproperty:

ex = GlobalRef(Base, :+)
getproperty(ex.mod, ex.name)

#+RESULTS:
: + (generic function with 207 methods)

3 Likes

Indeed! I felt it should be very easy and it is. Thanks a lot!

1 Like