MIR visitor implementation

I want to gather information on locals and regions from the MIR following the example for MIR visitor LocalUseVisitor example I call optimized_mir to get the MIR Body for run_pass (MirPass). I attempt to set the compiler options to avoid optimizations with mir-opt-level=0. However, the MIR is still missing info such as StorageLive and StorageDead. What am I missing? Advice and/or examples appreciated :slight_smile: compiling with: rustc 1.74.0-nightly

fn main() {
    let out = process::Command::new("rustc")
        .args(&["--print", "sysroot","-Z", "mir-opt-level=0"])
        .current_dir(".")
...
  rustc_interface::run_compiler(config, |compiler| {
        compiler.enter(|queries| {
            queries.global_ctxt().unwrap().enter(|tcx| {
                let fn_ids = tcx.mir_keys(());
                for fn_id in fn_ids {
                    let body_opt = tcx.optimized_mir(fn_id.to_def_id());

StorageLive and StorageDead are removed when optimizations are disabled. After borrowck their only user is the codegen backend to allow overlapping stack slots woth disjoint lifetimes. Something that doesn't happen anyway when optimizations are disabled. As such removing them early on when optimizations are disabled reduces memory usage and reduces the load on everything that processes MIR. Also note that even with optimizations enabled, StorageLive and StorageDead are not emitted for locals only used when unwinding. If neither statement is used on a local it is considered to always be alive.

Thanks for the reply. I follow the reasoning for removing them when no longer useful. So, how can I capture MIR, post borrowck, that contains the context info such as StorageLive and StorageDead? Thanks.

You could override the mir_drops_elaborated_and_const_checked query and run the original query inside it and then do your own analysis pass on the result of the original query.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.