Emitting MIR for analysis

Hi there,

I was wondering if there was a way to emit the MIR representation of code similar to how you can tell rust to emit llvm-ir with --emit=llvm-ir.

I would like to use this to compile arbitrary crates and then search their MIR representation for patterns that could be optimized.

I am very new to Rust and hope you could point me to the right direction.

There is some support for exporting the MIR representation of a function to graphviz files but the best approach for analyzing MIR would probably be to work directly in the compiler. Then you wouldn't have to reparse it and all type information is right there.

You can find an implementation a MIR transformation pass under src/librustc_mir/transform:
https://dxr.mozilla.org/rust/source/src/librustc_mir/transform

Note that MIR is still kind of experimental so the code infrastructure around is subject to change.

1 Like

I researched and wrote up some details about the current methods for looking at the compiler's HIR/MIR a few days ago here: https://www.reddit.com/r/rust/comments/3se4m5/pr_for_the_first_mir_optimization_pass_rust/cwxc3mp

1 Like