Module core::intrinsics::mir
source · custom_mir
)Expand description
Rustc internal tooling for hand-writing MIR.
If for some reasons you are not writing rustc tests and have found yourself considering using this feature, turn back. This is exceptionally unstable. There is no attempt at all to make anything work besides those things which the rustc test suite happened to need. If you make a typo you’ll probably ICE. Really, this is not the solution to your problems. Consider instead supporting the stable MIR project group.
The documentation for this module describes how to use this feature. If you are interested in
hacking on the implementation, most of that documentation lives at
rustc_mir_building/src/build/custom/mod.rs
.
Typical usage will look like this:
#![feature(core_intrinsics, custom_mir)]
extern crate core;
use core::intrinsics::mir::*;
#[custom_mir(dialect = "built")]
pub fn simple(x: i32) -> i32 {
mir!(
let temp1: i32;
let temp2: _;
{
temp1 = x;
Goto(exit)
}
exit = {
temp2 = Move(temp1);
RET = temp2;
Return()
}
)
}
RunHopefully most of this is fairly self-explanatory. Expanding on some notable details:
- The
custom_mir
attribute tells the compiler to treat the function as being custom MIR. This attribute only works on functions - there is no way to insert custom MIR into the middle of another function. - The
dialect
andphase
parameters indicate which version of MIR you are inserting here. This will normally be the phase that corresponds to the thing you are trying to test. The phase can be omitted for dialects that have just one. - You should define your function signature like you normally would. Externally, this function can be called like any other function.
- Type inference works - you don’t have to spell out the type of all of your locals.
For now, all statements and terminators are parsed from nested invocations of the special functions provided in this module. We additionally want to (but do not yet) support more “normal” Rust syntax in places where it makes sense. Also, most kinds of instructions are not supported yet.