Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 9b21c50

Browse files
committed
rustc_mir: create the Integrator as soon as possible in MIR inlining.
1 parent 0ce4452 commit 9b21c50

File tree

5 files changed

+96
-97
lines changed

5 files changed

+96
-97
lines changed

compiler/rustc_mir/src/transform/inline.rs

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -431,49 +431,6 @@ impl Inliner<'tcx> {
431431
TerminatorKind::Call { args, destination: Some(destination), cleanup, .. } => {
432432
debug!("inlined {:?} into {:?}", callsite.callee, caller_body.source);
433433

434-
let mut local_map = IndexVec::with_capacity(callee_body.local_decls.len());
435-
let mut scope_map = IndexVec::with_capacity(callee_body.source_scopes.len());
436-
437-
for mut scope in callee_body.source_scopes.iter().cloned() {
438-
// Map the callee scopes into the caller.
439-
// FIXME(eddyb) this may ICE if the scopes are out of order.
440-
scope.parent_scope = scope.parent_scope.map(|s| scope_map[s]);
441-
scope.inlined_parent_scope = scope.inlined_parent_scope.map(|s| scope_map[s]);
442-
443-
if scope.parent_scope.is_none() {
444-
let callsite_scope = &caller_body.source_scopes[callsite.source_info.scope];
445-
446-
// Attach the outermost callee scope as a child of the callsite
447-
// scope, via the `parent_scope` and `inlined_parent_scope` chains.
448-
scope.parent_scope = Some(callsite.source_info.scope);
449-
assert_eq!(scope.inlined_parent_scope, None);
450-
scope.inlined_parent_scope = if callsite_scope.inlined.is_some() {
451-
Some(callsite.source_info.scope)
452-
} else {
453-
callsite_scope.inlined_parent_scope
454-
};
455-
456-
// Mark the outermost callee scope as an inlined one.
457-
assert_eq!(scope.inlined, None);
458-
scope.inlined = Some((callsite.callee, callsite.source_info.span));
459-
} else if scope.inlined_parent_scope.is_none() {
460-
// Make it easy to find the scope with `inlined` set above.
461-
scope.inlined_parent_scope = Some(scope_map[OUTERMOST_SOURCE_SCOPE]);
462-
}
463-
464-
let idx = caller_body.source_scopes.push(scope);
465-
scope_map.push(idx);
466-
}
467-
468-
for loc in callee_body.vars_and_temps_iter() {
469-
let mut local = callee_body.local_decls[loc].clone();
470-
471-
local.source_info.scope = scope_map[local.source_info.scope];
472-
473-
let idx = caller_body.local_decls.push(local);
474-
local_map.push(idx);
475-
}
476-
477434
// If the call is something like `a[*i] = f(i)`, where
478435
// `i : &mut usize`, then just duplicating the `a[*i]`
479436
// Place could result in two different locations if `f`
@@ -524,15 +481,57 @@ impl Inliner<'tcx> {
524481
let mut integrator = Integrator {
525482
block_idx: bb_len,
526483
args: &args,
527-
local_map,
528-
scope_map,
484+
local_map: IndexVec::with_capacity(callee_body.local_decls.len()),
485+
scope_map: IndexVec::with_capacity(callee_body.source_scopes.len()),
529486
destination: dest,
530487
return_block,
531488
cleanup_block: cleanup,
532489
in_cleanup_block: false,
533490
tcx: self.tcx,
534491
};
535492

493+
for mut scope in callee_body.source_scopes.iter().cloned() {
494+
// Map the callee scopes into the caller.
495+
// FIXME(eddyb) this may ICE if the scopes are out of order.
496+
scope.parent_scope = scope.parent_scope.map(|s| integrator.scope_map[s]);
497+
scope.inlined_parent_scope =
498+
scope.inlined_parent_scope.map(|s| integrator.scope_map[s]);
499+
500+
if scope.parent_scope.is_none() {
501+
let callsite_scope = &caller_body.source_scopes[callsite.source_info.scope];
502+
503+
// Attach the outermost callee scope as a child of the callsite
504+
// scope, via the `parent_scope` and `inlined_parent_scope` chains.
505+
scope.parent_scope = Some(callsite.source_info.scope);
506+
assert_eq!(scope.inlined_parent_scope, None);
507+
scope.inlined_parent_scope = if callsite_scope.inlined.is_some() {
508+
Some(callsite.source_info.scope)
509+
} else {
510+
callsite_scope.inlined_parent_scope
511+
};
512+
513+
// Mark the outermost callee scope as an inlined one.
514+
assert_eq!(scope.inlined, None);
515+
scope.inlined = Some((callsite.callee, callsite.source_info.span));
516+
} else if scope.inlined_parent_scope.is_none() {
517+
// Make it easy to find the scope with `inlined` set above.
518+
scope.inlined_parent_scope =
519+
Some(integrator.scope_map[OUTERMOST_SOURCE_SCOPE]);
520+
}
521+
522+
let idx = caller_body.source_scopes.push(scope);
523+
integrator.scope_map.push(idx);
524+
}
525+
526+
for loc in callee_body.vars_and_temps_iter() {
527+
let mut local = callee_body.local_decls[loc].clone();
528+
529+
local.source_info.scope = integrator.scope_map[local.source_info.scope];
530+
531+
let idx = caller_body.local_decls.push(local);
532+
integrator.local_map.push(idx);
533+
}
534+
536535
for mut var_debug_info in callee_body.var_debug_info.drain(..) {
537536
integrator.visit_var_debug_info(&mut var_debug_info);
538537
caller_body.var_debug_info.push(var_debug_info);

src/test/mir-opt/inline/inline_any_operand.bar.Inline.after.mir

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ fn bar() -> bool {
44
let mut _0: bool; // return place in scope 0 at $DIR/inline-any-operand.rs:10:13: 10:17
55
let _1: fn(i32, i32) -> bool {foo}; // in scope 0 at $DIR/inline-any-operand.rs:11:9: 11:10
66
let mut _2: fn(i32, i32) -> bool {foo}; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:6
7-
let mut _5: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
8-
let mut _6: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
7+
let mut _3: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
8+
let mut _4: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
99
scope 1 {
1010
debug f => _1; // in scope 1 at $DIR/inline-any-operand.rs:11:9: 11:10
1111
scope 2 (inlined foo) { // at $DIR/inline-any-operand.rs:12:5: 12:13
12-
debug x => _5; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
13-
debug y => _6; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
14-
let mut _3: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
15-
let mut _4: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
12+
debug x => _3; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
13+
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
14+
let mut _5: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
15+
let mut _6: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
1616
}
1717
}
1818

@@ -24,19 +24,19 @@ fn bar() -> bool {
2424
// + literal: Const { ty: fn(i32, i32) -> bool {foo}, val: Value(Scalar(<ZST>)) }
2525
StorageLive(_2); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:6
2626
_2 = _1; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:6
27-
StorageLive(_5); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
28-
_5 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
29-
StorageLive(_6); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
30-
_6 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
31-
StorageLive(_3); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
32-
_3 = _5; // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
33-
StorageLive(_4); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
34-
_4 = _6; // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
35-
_0 = Eq(move _3, move _4); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:11
36-
StorageDead(_4); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
37-
StorageDead(_3); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
38-
StorageDead(_6); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
39-
StorageDead(_5); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
27+
StorageLive(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
28+
_3 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
29+
StorageLive(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
30+
_4 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
31+
StorageLive(_5); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
32+
_5 = _3; // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
33+
StorageLive(_6); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
34+
_6 = _4; // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
35+
_0 = Eq(move _5, move _6); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:11
36+
StorageDead(_6); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
37+
StorageDead(_5); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
38+
StorageDead(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
39+
StorageDead(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
4040
StorageDead(_2); // scope 1 at $DIR/inline-any-operand.rs:12:12: 12:13
4141
StorageDead(_1); // scope 0 at $DIR/inline-any-operand.rs:13:1: 13:2
4242
return; // scope 0 at $DIR/inline-any-operand.rs:13:2: 13:2

src/test/mir-opt/inline/inline_closure_borrows_arg.foo.Inline.after.mir

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ fn foo(_1: T, _2: &i32) -> i32 {
99
let mut _5: (&i32, &i32); // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
1010
let mut _6: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:7: 16:8
1111
let mut _7: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:10: 16:11
12+
let mut _8: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
1213
let mut _9: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
13-
let mut _10: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
1414
scope 1 {
1515
debug x => _3; // in scope 1 at $DIR/inline-closure-borrows-arg.rs:12:9: 12:10
1616
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
17-
debug r => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:14: 12:15
18-
debug _s => _10; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:23: 12:25
19-
let _8: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
17+
debug r => _8; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:14: 12:15
18+
debug _s => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:23: 12:25
19+
let _10: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
2020
scope 3 {
21-
debug variable => _8; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
21+
debug variable => _10; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
2222
}
2323
}
2424
}
@@ -34,16 +34,16 @@ fn foo(_1: T, _2: &i32) -> i32 {
3434
_7 = &(*_2); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:10: 16:11
3535
(_5.0: &i32) = move _6; // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
3636
(_5.1: &i32) = move _7; // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
37+
StorageLive(_8); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
38+
_8 = move (_5.0: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
3739
StorageLive(_9); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
38-
_9 = move (_5.0: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
39-
StorageLive(_10); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
40-
_10 = move (_5.1: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
41-
StorageLive(_8); // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
42-
_8 = _9; // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:24: 13:27
43-
_0 = (*_9); // scope 3 at $DIR/inline-closure-borrows-arg.rs:14:9: 14:18
44-
StorageDead(_8); // scope 2 at $DIR/inline-closure-borrows-arg.rs:15:5: 15:6
45-
StorageDead(_10); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
40+
_9 = move (_5.1: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
41+
StorageLive(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
42+
_10 = _8; // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:24: 13:27
43+
_0 = (*_8); // scope 3 at $DIR/inline-closure-borrows-arg.rs:14:9: 14:18
44+
StorageDead(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:15:5: 15:6
4645
StorageDead(_9); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
46+
StorageDead(_8); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
4747
StorageDead(_7); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:11: 16:12
4848
StorageDead(_6); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:11: 16:12
4949
StorageDead(_5); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:11: 16:12

src/test/mir-opt/inline/inline_closure_captures.foo.Inline.after.mir

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
1010
let mut _6: &[closure@foo<T>::{closure#0}]; // in scope 0 at $DIR/inline-closure-captures.rs:12:5: 12:6
1111
let mut _7: (i32,); // in scope 0 at $DIR/inline-closure-captures.rs:12:5: 12:9
1212
let mut _8: i32; // in scope 0 at $DIR/inline-closure-captures.rs:12:7: 12:8
13-
let mut _10: i32; // in scope 0 at $DIR/inline-closure-captures.rs:12:5: 12:9
13+
let mut _9: i32; // in scope 0 at $DIR/inline-closure-captures.rs:12:5: 12:9
1414
scope 1 {
1515
debug x => _3; // in scope 1 at $DIR/inline-closure-captures.rs:11:9: 11:10
1616
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure-captures.rs:12:5: 12:9
17-
debug _q => _10; // in scope 2 at $DIR/inline-closure-captures.rs:11:14: 11:16
17+
debug _q => _9; // in scope 2 at $DIR/inline-closure-captures.rs:11:14: 11:16
1818
debug q => (*((*_6).0: &i32)); // in scope 2 at $DIR/inline-closure-captures.rs:10:23: 10:24
1919
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:10:17: 10:18
20-
let mut _9: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
20+
let mut _10: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
2121
}
2222
}
2323

@@ -37,14 +37,14 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
3737
StorageLive(_8); // scope 1 at $DIR/inline-closure-captures.rs:12:7: 12:8
3838
_8 = _2; // scope 1 at $DIR/inline-closure-captures.rs:12:7: 12:8
3939
(_7.0: i32) = move _8; // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
40-
StorageLive(_10); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
41-
_10 = move (_7.0: i32); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
40+
StorageLive(_9); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
41+
_9 = move (_7.0: i32); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
4242
(_0.0: i32) = (*((*_6).0: &i32)); // scope 2 at $DIR/inline-closure-captures.rs:11:19: 11:20
43-
StorageLive(_9); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
44-
_9 = (*((*_6).1: &T)); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
45-
(_0.1: T) = move _9; // scope 2 at $DIR/inline-closure-captures.rs:11:18: 11:24
46-
StorageDead(_9); // scope 2 at $DIR/inline-closure-captures.rs:11:23: 11:24
47-
StorageDead(_10); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
43+
StorageLive(_10); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
44+
_10 = (*((*_6).1: &T)); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
45+
(_0.1: T) = move _10; // scope 2 at $DIR/inline-closure-captures.rs:11:18: 11:24
46+
StorageDead(_10); // scope 2 at $DIR/inline-closure-captures.rs:11:23: 11:24
47+
StorageDead(_9); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
4848
StorageDead(_8); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9
4949
StorageDead(_7); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9
5050
StorageDead(_6); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9

0 commit comments

Comments
 (0)