How to dump the MIR of the std/alloc library inside rust project?

I would like to do some analyzing work on top of the MIR of std/alloc library. My goal is to watch the MIR that is relevant to memory allocation, including box, allocator, libc::malloc or HeapAlloc, etc.

We can use several ways to dump the MIR of a normal crate:

  1. setting RUSTFLAGS="--emit mir" in the environment variables.

  2. setting it in build.rustflags.

The rust project itself is built by x.py and rustbuild. After some exploring, I could not find a way to dump the MIR of a crate located inside the project. But I guess using the stage0/beta compiler to dump the MIR is sufficient.

I am sorry that I just find it under the build directory: build/<target>/stage0-std/<target>/release/deps/alloc-<hash>.mir

But I don't know how to close this topic. Sorry for the annoyance.

1 Like

The way I get the MIR dump is by setting RUSTFLAGS="--emit mir" in the environment variables.

But when I set it to RUSTFLAGS="-Z dump-mir-graphviz=yes" and run py .\x.py build .\library\alloc\ in the shell, the stage0 rustc reports error:

Caused by:
  process didn't exit successfully: `rust\build\<target>\stage0\bin\rustc.exe - ... -Z dump-mir-graphviz=yes ... ` (exit code: 1)
  --- stderr
  error: the option `Z` is only accepted on the nightly compiler

How could I use the nightly version or is there a way to dump the mir not by setting the RUSTFLAGS globally?

Setting RUSTFLAGS will cause rustbuild itself to be compiled with those flags too I think. Rustbuild is built using the bootstrap compiler without the escape hatch used to allow nightly features. You can use RUSTFLAGS_BOOTSTRAP or RUSTFLAGS_NOT_BOOTSTRAP depending on if you want to have it apply when compiling using the bootstrap compiler (--stage 0) or not (--stage 1 or --stage 2).

You can also use cargo -Zbuild-std build --target <target> on nightly to make cargo build the standard library instead of using the precompiled one. This will make RUSTFLAGS="--emit mir" apply to the standard library too I believe.

1 Like

They both work. Thanks!

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