How to recover MIR for analysis?

I currently doing some analysis on MIR.

Since MIR can be accessed from rustc_driver, such as run_compiler. We can just link our algorithm as a callback function. This method is only suitable when we analyze a single file.

However, I want to do some analysis on MIR from a project. Specifically, can we access MIR after cargo build?

If we generate MIR from --emit mir, we must recover MIR data structure from .mir file. How to do that?

I prefer to access MIR directly from API. But I do not figure out how to do that. Use cargo API?

If you compile all dependencies with -Zalways-encode-mir (for the standard library this requires -Zbuild-std), you can get not just the MIR for the current crate, but also for all dependencies through the rustc_driver api. --emit mir is a lossy debug representation of MIR. It is not meant to be parsed and in fact misses a lot of details like type definitions.

2 Likes

Sounds like you might be interested in GitHub - rust-lang/project-stable-mir: Define compiler intermediate representation usable by external tools ?

Yep(LOL). I implement my algorithm based on stable mir. But I have no idea about retrieving the (stable) MIR of a project instead of a single crate.

This paper (https://dl.acm.org/doi/10.1145/3640537.3641574) proposes a whole-program pointer analysis for Rust program and has open-sourced its implementation (GitHub - rustanlys/rupta: A Pointer Analysis Framework for Rust). While I haven't fully explored the details, it does collect all MIR to perform analysis.

1 Like

Thanks. I have figured out how to do that with cargo according to the rupta. Besides wrapping our program as a callback function for rustc, it also wraps our program as a compiler. Via cargo option RUSTC_WRAPPER, our program acts as a compile that cargo invokes. Thus, our program can easily communicate with cargo.

Thanks a lot. I find the solution.