Packages for instrumenting / rewriting Julia code (at Expr level)?

I know of Cassette.jl but my impression is that it requires knowledge about lower-level Julia compilation/execution stages. Are there other good options/packages/approaches for instrumenting Julia code at the Expr level?

By searching, I’ve found the following related packages (in order by latest commit date):

MacroTools.jl - tools for working with Julia code and expressions, March 23rd 2020
Reduce.jl - Symbolic parser generator for Julia language expressions, March 18th 2020
MLStyle.jl - … highly optimized and extensible pattern matching, and killer tools for meta-programming, Feb 9th 2020
Espresso.jl - Expression transformation package, Feb 8th 2020
ExpressionUtils.jl - Utilities for working with the Julia Expr type, July 27th 2016

but would be great if someone has experience with using some of them and their respective pros and cons. I have several use cases in mind, all related to rewriting Julia code at Expr level in order to e.g. track execution of basic blocks, branch coverage, as well as for mutation testing and code slicing.

3 Likes

I have found GitHub - FluxML/IRTools.jl: Mike's Little Intermediate Representation to be slightly more approachable than Cassette.jl

Thanks @baggepinnen.

On a related note for this thread, Tim Holy’s Revise.jl can also be used to get ahold of the “surface” AST of Julia methods (and then process it with some of the tools mentioned above), see example code here:

https://github.com/timholy/Revise.jl/issues/458

1 Like

@baggepinnen thanks for the advice, I found IRTools really nice and easy to work with. Sharing example of how it can be used for code tracing/instrumentation here, in case someone else has related needs:

https://github.com/robertfeldt/CodeTracer.jl/blob/main/spikes/irtools_instrument_for_basic_block_coverage.jl

3 Likes