Skip to content

Commit e90b521

Browse files
committed
--emit=mir now emits both mir_for_ctfe and optimized_mir for const fn
1 parent 41a732d commit e90b521

File tree

4 files changed

+86
-11
lines changed

4 files changed

+86
-11
lines changed

compiler/rustc_mir/src/util/pretty.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -273,25 +273,35 @@ pub fn write_mir_pretty<'tcx>(
273273

274274
let mut first = true;
275275
for def_id in dump_mir_def_ids(tcx, single) {
276-
let body = match tcx.hir().body_const_context(def_id.expect_local()) {
277-
// For `const fn` we want to render the optimized MIR. If you want the mir used in
278-
// ctfe, you can dump the MIR after the `Deaggregator` optimization pass.
279-
None | Some(rustc_hir::ConstContext::ConstFn) => tcx.optimized_mir(def_id),
280-
Some(_) => tcx.mir_for_ctfe(def_id),
281-
};
282-
283276
if first {
284277
first = false;
285278
} else {
286279
// Put empty lines between all items
287280
writeln!(w)?;
288281
}
289282

290-
write_mir_fn(tcx, body, &mut |_, _| Ok(()), w)?;
291-
292-
for body in tcx.promoted_mir(def_id) {
293-
writeln!(w)?;
283+
let render_body = |w: &mut dyn Write, body| -> io::Result<()> {
294284
write_mir_fn(tcx, body, &mut |_, _| Ok(()), w)?;
285+
286+
for body in tcx.promoted_mir(def_id) {
287+
writeln!(w)?;
288+
write_mir_fn(tcx, body, &mut |_, _| Ok(()), w)?;
289+
}
290+
Ok(())
291+
};
292+
match tcx.hir().body_const_context(def_id.expect_local()) {
293+
None => render_body(w, tcx.optimized_mir(def_id))?,
294+
// For `const fn` we want to render the optimized MIR. If you want the mir used in
295+
// ctfe, you can dump the MIR after the `Deaggregator` optimization pass.
296+
Some(rustc_hir::ConstContext::ConstFn) => {
297+
render_body(w, tcx.optimized_mir(def_id))?;
298+
writeln!(w)?;
299+
writeln!(w, "// MIR FOR CTFE")?;
300+
// Do not use `render_body`, as that would render the promoteds again, but these
301+
// are shared between mir_for_ctfe and optimized_mir
302+
write_mir_fn(tcx, tcx.mir_for_ctfe(def_id), &mut |_, _| Ok(()), w)?;
303+
}
304+
Some(_) => render_body(w, tcx.mir_for_ctfe(def_id))?,
295305
}
296306
}
297307
Ok(())
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-include ../../run-make-fulldeps/tools.mk
2+
3+
all:
4+
$(RUSTC) main.rs --emit=mir -o "$(TMPDIR)"/dump.mir
5+
6+
ifdef RUSTC_BLESS_TEST
7+
cp "$(TMPDIR)"/dump.mir dump.mir
8+
else
9+
$(DIFF) dump.mir "$(TMPDIR)"/dump.mir
10+
endif
+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// WARNING: This output format is intended for human consumers only
2+
// and is subject to change without notice. Knock yourself out.
3+
fn main() -> () {
4+
let mut _0: (); // return place in scope 0 at main.rs:8:11: 8:11
5+
let _1: i32; // in scope 0 at main.rs:9:5: 9:10
6+
7+
bb0: {
8+
StorageLive(_1); // scope 0 at main.rs:9:5: 9:10
9+
_1 = foo() -> bb1; // scope 0 at main.rs:9:5: 9:10
10+
// mir::Constant
11+
// + span: main.rs:9:5: 9:8
12+
// + literal: Const { ty: fn() -> i32 {foo}, val: Value(Scalar(<ZST>)) }
13+
}
14+
15+
bb1: {
16+
StorageDead(_1); // scope 0 at main.rs:9:10: 9:11
17+
_0 = const (); // scope 0 at main.rs:8:11: 10:2
18+
return; // scope 0 at main.rs:10:2: 10:2
19+
}
20+
}
21+
22+
fn foo() -> i32 {
23+
let mut _0: i32; // return place in scope 0 at main.rs:4:19: 4:22
24+
25+
bb0: {
26+
_0 = const 11_i32; // scope 0 at main.rs:5:5: 5:10
27+
return; // scope 0 at main.rs:6:2: 6:2
28+
}
29+
}
30+
31+
// MIR FOR CTFE
32+
fn foo() -> i32 {
33+
let mut _0: i32; // return place in scope 0 at main.rs:4:19: 4:22
34+
let mut _1: (i32, bool); // in scope 0 at main.rs:5:5: 5:10
35+
36+
bb0: {
37+
_1 = CheckedAdd(const 5_i32, const 6_i32); // scope 0 at main.rs:5:5: 5:10
38+
assert(!move (_1.1: bool), "attempt to compute `{} + {}`, which would overflow", const 5_i32, const 6_i32) -> bb1; // scope 0 at main.rs:5:5: 5:10
39+
}
40+
41+
bb1: {
42+
_0 = move (_1.0: i32); // scope 0 at main.rs:5:5: 5:10
43+
return; // scope 0 at main.rs:6:2: 6:2
44+
}
45+
}
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// emit-mir
2+
// check-pass
3+
4+
const fn foo() -> i32 {
5+
5 + 6
6+
}
7+
8+
fn main() {
9+
foo();
10+
}

0 commit comments

Comments
 (0)