Skip to content

Commit

Permalink
rustc_mir: create the Integrator as soon as possible in MIR inlining.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Oct 21, 2020
1 parent 0ce4452 commit 9b21c50
Show file tree
Hide file tree
Showing 5 changed files with 96 additions and 97 deletions.
89 changes: 44 additions & 45 deletions compiler/rustc_mir/src/transform/inline.rs
Expand Up @@ -431,49 +431,6 @@ impl Inliner<'tcx> {
TerminatorKind::Call { args, destination: Some(destination), cleanup, .. } => {
debug!("inlined {:?} into {:?}", callsite.callee, caller_body.source);

let mut local_map = IndexVec::with_capacity(callee_body.local_decls.len());
let mut scope_map = IndexVec::with_capacity(callee_body.source_scopes.len());

for mut scope in callee_body.source_scopes.iter().cloned() {
// Map the callee scopes into the caller.
// FIXME(eddyb) this may ICE if the scopes are out of order.
scope.parent_scope = scope.parent_scope.map(|s| scope_map[s]);
scope.inlined_parent_scope = scope.inlined_parent_scope.map(|s| scope_map[s]);

if scope.parent_scope.is_none() {
let callsite_scope = &caller_body.source_scopes[callsite.source_info.scope];

// Attach the outermost callee scope as a child of the callsite
// scope, via the `parent_scope` and `inlined_parent_scope` chains.
scope.parent_scope = Some(callsite.source_info.scope);
assert_eq!(scope.inlined_parent_scope, None);
scope.inlined_parent_scope = if callsite_scope.inlined.is_some() {
Some(callsite.source_info.scope)
} else {
callsite_scope.inlined_parent_scope
};

// Mark the outermost callee scope as an inlined one.
assert_eq!(scope.inlined, None);
scope.inlined = Some((callsite.callee, callsite.source_info.span));
} else if scope.inlined_parent_scope.is_none() {
// Make it easy to find the scope with `inlined` set above.
scope.inlined_parent_scope = Some(scope_map[OUTERMOST_SOURCE_SCOPE]);
}

let idx = caller_body.source_scopes.push(scope);
scope_map.push(idx);
}

for loc in callee_body.vars_and_temps_iter() {
let mut local = callee_body.local_decls[loc].clone();

local.source_info.scope = scope_map[local.source_info.scope];

let idx = caller_body.local_decls.push(local);
local_map.push(idx);
}

// If the call is something like `a[*i] = f(i)`, where
// `i : &mut usize`, then just duplicating the `a[*i]`
// Place could result in two different locations if `f`
Expand Down Expand Up @@ -524,15 +481,57 @@ impl Inliner<'tcx> {
let mut integrator = Integrator {
block_idx: bb_len,
args: &args,
local_map,
scope_map,
local_map: IndexVec::with_capacity(callee_body.local_decls.len()),
scope_map: IndexVec::with_capacity(callee_body.source_scopes.len()),
destination: dest,
return_block,
cleanup_block: cleanup,
in_cleanup_block: false,
tcx: self.tcx,
};

for mut scope in callee_body.source_scopes.iter().cloned() {
// Map the callee scopes into the caller.
// FIXME(eddyb) this may ICE if the scopes are out of order.
scope.parent_scope = scope.parent_scope.map(|s| integrator.scope_map[s]);
scope.inlined_parent_scope =
scope.inlined_parent_scope.map(|s| integrator.scope_map[s]);

if scope.parent_scope.is_none() {
let callsite_scope = &caller_body.source_scopes[callsite.source_info.scope];

// Attach the outermost callee scope as a child of the callsite
// scope, via the `parent_scope` and `inlined_parent_scope` chains.
scope.parent_scope = Some(callsite.source_info.scope);
assert_eq!(scope.inlined_parent_scope, None);
scope.inlined_parent_scope = if callsite_scope.inlined.is_some() {
Some(callsite.source_info.scope)
} else {
callsite_scope.inlined_parent_scope
};

// Mark the outermost callee scope as an inlined one.
assert_eq!(scope.inlined, None);
scope.inlined = Some((callsite.callee, callsite.source_info.span));
} else if scope.inlined_parent_scope.is_none() {
// Make it easy to find the scope with `inlined` set above.
scope.inlined_parent_scope =
Some(integrator.scope_map[OUTERMOST_SOURCE_SCOPE]);
}

let idx = caller_body.source_scopes.push(scope);
integrator.scope_map.push(idx);
}

for loc in callee_body.vars_and_temps_iter() {
let mut local = callee_body.local_decls[loc].clone();

local.source_info.scope = integrator.scope_map[local.source_info.scope];

let idx = caller_body.local_decls.push(local);
integrator.local_map.push(idx);
}

for mut var_debug_info in callee_body.var_debug_info.drain(..) {
integrator.visit_var_debug_info(&mut var_debug_info);
caller_body.var_debug_info.push(var_debug_info);
Expand Down
38 changes: 19 additions & 19 deletions src/test/mir-opt/inline/inline_any_operand.bar.Inline.after.mir
Expand Up @@ -4,15 +4,15 @@ fn bar() -> bool {
let mut _0: bool; // return place in scope 0 at $DIR/inline-any-operand.rs:10:13: 10:17
let _1: fn(i32, i32) -> bool {foo}; // in scope 0 at $DIR/inline-any-operand.rs:11:9: 11:10
let mut _2: fn(i32, i32) -> bool {foo}; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:6
let mut _5: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _6: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _3: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _4: i32; // in scope 0 at $DIR/inline-any-operand.rs:12:5: 12:13
scope 1 {
debug f => _1; // in scope 1 at $DIR/inline-any-operand.rs:11:9: 11:10
scope 2 (inlined foo) { // at $DIR/inline-any-operand.rs:12:5: 12:13
debug x => _5; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
debug y => _6; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
let mut _3: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
let mut _4: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
debug x => _3; // in scope 2 at $DIR/inline-any-operand.rs:16:8: 16:9
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:16:16: 16:17
let mut _5: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
let mut _6: i32; // in scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
}
}

Expand All @@ -24,19 +24,19 @@ fn bar() -> bool {
// + literal: Const { ty: fn(i32, i32) -> bool {foo}, val: Value(Scalar(<ZST>)) }
StorageLive(_2); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:6
_2 = _1; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:6
StorageLive(_5); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_5 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_6); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_6 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_3); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
_3 = _5; // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
StorageLive(_4); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
_4 = _6; // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
_0 = Eq(move _3, move _4); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:11
StorageDead(_4); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
StorageDead(_3); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
StorageDead(_6); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_5); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_3 = const 1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
_4 = const -1_i32; // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_5); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
_5 = _3; // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:6
StorageLive(_6); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
_6 = _4; // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
_0 = Eq(move _5, move _6); // scope 2 at $DIR/inline-any-operand.rs:17:5: 17:11
StorageDead(_6); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
StorageDead(_5); // scope 2 at $DIR/inline-any-operand.rs:17:10: 17:11
StorageDead(_4); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_3); // scope 1 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_2); // scope 1 at $DIR/inline-any-operand.rs:12:12: 12:13
StorageDead(_1); // scope 0 at $DIR/inline-any-operand.rs:13:1: 13:2
return; // scope 0 at $DIR/inline-any-operand.rs:13:2: 13:2
Expand Down
Expand Up @@ -9,16 +9,16 @@ fn foo(_1: T, _2: &i32) -> i32 {
let mut _5: (&i32, &i32); // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
let mut _6: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:7: 16:8
let mut _7: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:10: 16:11
let mut _8: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
let mut _9: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
let mut _10: &i32; // in scope 0 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
scope 1 {
debug x => _3; // in scope 1 at $DIR/inline-closure-borrows-arg.rs:12:9: 12:10
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
debug r => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:14: 12:15
debug _s => _10; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:23: 12:25
let _8: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
debug r => _8; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:14: 12:15
debug _s => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:12:23: 12:25
let _10: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
scope 3 {
debug variable => _8; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
debug variable => _10; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
}
}
}
Expand All @@ -34,16 +34,16 @@ fn foo(_1: T, _2: &i32) -> i32 {
_7 = &(*_2); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:10: 16:11
(_5.0: &i32) = move _6; // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
(_5.1: &i32) = move _7; // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageLive(_8); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
_8 = move (_5.0: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageLive(_9); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
_9 = move (_5.0: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageLive(_10); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
_10 = move (_5.1: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageLive(_8); // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
_8 = _9; // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:24: 13:27
_0 = (*_9); // scope 3 at $DIR/inline-closure-borrows-arg.rs:14:9: 14:18
StorageDead(_8); // scope 2 at $DIR/inline-closure-borrows-arg.rs:15:5: 15:6
StorageDead(_10); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
_9 = move (_5.1: &i32); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageLive(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
_10 = _8; // scope 2 at $DIR/inline-closure-borrows-arg.rs:13:24: 13:27
_0 = (*_8); // scope 3 at $DIR/inline-closure-borrows-arg.rs:14:9: 14:18
StorageDead(_10); // scope 2 at $DIR/inline-closure-borrows-arg.rs:15:5: 15:6
StorageDead(_9); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageDead(_8); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageDead(_7); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:11: 16:12
StorageDead(_6); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:11: 16:12
StorageDead(_5); // scope 1 at $DIR/inline-closure-borrows-arg.rs:16:11: 16:12
Expand Down
Expand Up @@ -10,14 +10,14 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
let mut _6: &[closure@foo<T>::{closure#0}]; // in scope 0 at $DIR/inline-closure-captures.rs:12:5: 12:6
let mut _7: (i32,); // in scope 0 at $DIR/inline-closure-captures.rs:12:5: 12:9
let mut _8: i32; // in scope 0 at $DIR/inline-closure-captures.rs:12:7: 12:8
let mut _10: i32; // in scope 0 at $DIR/inline-closure-captures.rs:12:5: 12:9
let mut _9: i32; // in scope 0 at $DIR/inline-closure-captures.rs:12:5: 12:9
scope 1 {
debug x => _3; // in scope 1 at $DIR/inline-closure-captures.rs:11:9: 11:10
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure-captures.rs:12:5: 12:9
debug _q => _10; // in scope 2 at $DIR/inline-closure-captures.rs:11:14: 11:16
debug _q => _9; // in scope 2 at $DIR/inline-closure-captures.rs:11:14: 11:16
debug q => (*((*_6).0: &i32)); // in scope 2 at $DIR/inline-closure-captures.rs:10:23: 10:24
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:10:17: 10:18
let mut _9: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
let mut _10: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
}
}

Expand All @@ -37,14 +37,14 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
StorageLive(_8); // scope 1 at $DIR/inline-closure-captures.rs:12:7: 12:8
_8 = _2; // scope 1 at $DIR/inline-closure-captures.rs:12:7: 12:8
(_7.0: i32) = move _8; // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageLive(_10); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
_10 = move (_7.0: i32); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageLive(_9); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
_9 = move (_7.0: i32); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
(_0.0: i32) = (*((*_6).0: &i32)); // scope 2 at $DIR/inline-closure-captures.rs:11:19: 11:20
StorageLive(_9); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
_9 = (*((*_6).1: &T)); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
(_0.1: T) = move _9; // scope 2 at $DIR/inline-closure-captures.rs:11:18: 11:24
StorageDead(_9); // scope 2 at $DIR/inline-closure-captures.rs:11:23: 11:24
StorageDead(_10); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageLive(_10); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
_10 = (*((*_6).1: &T)); // scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
(_0.1: T) = move _10; // scope 2 at $DIR/inline-closure-captures.rs:11:18: 11:24
StorageDead(_10); // scope 2 at $DIR/inline-closure-captures.rs:11:23: 11:24
StorageDead(_9); // scope 1 at $DIR/inline-closure-captures.rs:12:5: 12:9
StorageDead(_8); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9
StorageDead(_7); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9
StorageDead(_6); // scope 1 at $DIR/inline-closure-captures.rs:12:8: 12:9
Expand Down

0 comments on commit 9b21c50

Please sign in to comment.