How to run lowered AST?

Is there an “official” way to run the CodeInfo objects that one gets from code_typed?

I’d like to be able to do some transformations on code based the types of intermediate results, and then run the transformed code. (I could work with the surface-syntax Expr and write my own machinery to figure out the types, but I’d rather avoid having to do that.) My plan was to get the lowered, typed IR from code_typed, transform that, and evaluate the transformed code. For the last step, I would need some kind of feval(f::Core.CodeInfo, args...).

I’ve seen this done in Cassette.jl using some advanced voodoo, but I was thinking there was maybe an easier way. (If not, I’ll probably end up using Cassette - It seems like a brilliant project.)

2 Likes

Sugar has a bit of this:
https://github.com/SimonDanisch/Sugar.jl/blob/master/test/runtests.jl#L32
I started rewriting Sugar to offer those features more as first class use cases.

Right now, it’s a bit of a monster, mainly written to work with Transpiler.jl, without much time invested into a real user facing API.

1 Like

I did look at Sugar, but I couldn’t find any documentation. Does get_func_expr convert the IR back into an Expr? Does this work in general, or only for a subset of Julia?

it’s just a subset for silly reasons… Btw, it should be relatively straight forward to replace the
unsupported expressions in the linear ir returned by code_typed…

This seems very useful. I will definitely have a look. Is there any way to learn more, other than reading the source code?

Wow! I haven’t quite figured everything out yet, but I think this is exactly what I was looking for.

Are you planning on updating Sugar to support Julia 0.7 ?

I am indeed, together with the new, user friendly API :slight_smile:

3 Likes

https://github.com/SimonDanisch/Sugar.jl/blob/sd/07/src/lambdas.jl#L268

I ported get_func_expr to the 0.7 branch… should be more stable now, but doesn’t sugar the ast. Let me know if that’s what you need!

Thanks!

…but I get an error message because postwalk (called by replace_slots, line 258) is undefined. I’ve switched to branch sd/07 on Sugar and Matcha. Am I missing something?

I shouldn’t have announced this before going to bed, especially without a CI^^
I added some tests and it should be fixed now :wink:

It works. Thanks!