Skip to content

Commit

Permalink
Auto merge of #94427 - cjgillot:inline-fresh-expn, r=oli-obk
Browse files Browse the repository at this point in the history
Only create a single expansion for each inline integration.

The inlining integrator used to create one expansion for each span from the callee body.
This PR reverses the logic to create a single expansion for the whole call,
which is more consistent with how macro expansions work for macros.

This should remove the large memory regression in #91743.
  • Loading branch information
bors committed Feb 28, 2022
2 parents 427cf81 + e77e4fc commit 48132ca
Show file tree
Hide file tree
Showing 51 changed files with 502 additions and 497 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_errors/src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ pub trait Emitter {
"this derive macro expansion".into()
}
ExpnKind::Macro(MacroKind::Bang, _) => "this macro invocation".into(),
ExpnKind::Inlined => "the inlined copy of this code".into(),
ExpnKind::Root => "in the crate root".into(),
ExpnKind::Inlined => "this inlined function call".into(),
ExpnKind::Root => "the crate root".into(),
ExpnKind::AstPass(kind) => kind.descr().into(),
ExpnKind::Desugaring(kind) => {
format!("this {} desugaring", kind.descr()).into()
Expand Down
24 changes: 14 additions & 10 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use rustc_middle::mir::*;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::subst::Subst;
use rustc_middle::ty::{self, ConstKind, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
use rustc_span::{hygiene::ExpnKind, ExpnData, Span};
use rustc_span::{hygiene::ExpnKind, ExpnData, LocalExpnId, Span};
use rustc_target::spec::abi::Abi;

use super::simplify::{remove_dead_blocks, CfgSimplifier};
Expand Down Expand Up @@ -543,6 +543,16 @@ impl<'tcx> Inliner<'tcx> {
// Copy the arguments if needed.
let args: Vec<_> = self.make_call_args(args, &callsite, caller_body, &callee_body);

let mut expn_data = ExpnData::default(
ExpnKind::Inlined,
callsite.source_info.span,
self.tcx.sess.edition(),
None,
None,
);
expn_data.def_site = callee_body.span;
let expn_data =
LocalExpnId::fresh(expn_data, self.tcx.create_stable_hashing_context());
let mut integrator = Integrator {
args: &args,
new_locals: Local::new(caller_body.local_decls.len())..,
Expand All @@ -553,8 +563,7 @@ impl<'tcx> Inliner<'tcx> {
cleanup_block: cleanup,
in_cleanup_block: false,
tcx: self.tcx,
callsite_span: callsite.source_info.span,
body_span: callee_body.span,
expn_data,
always_live_locals: BitSet::new_filled(callee_body.local_decls.len()),
};

Expand Down Expand Up @@ -787,8 +796,7 @@ struct Integrator<'a, 'tcx> {
cleanup_block: Option<BasicBlock>,
in_cleanup_block: bool,
tcx: TyCtxt<'tcx>,
callsite_span: Span,
body_span: Span,
expn_data: LocalExpnId,
always_live_locals: BitSet<Local>,
}

Expand Down Expand Up @@ -835,12 +843,8 @@ impl<'tcx> MutVisitor<'tcx> for Integrator<'_, 'tcx> {
}

fn visit_span(&mut self, span: &mut Span) {
let mut expn_data =
ExpnData::default(ExpnKind::Inlined, *span, self.tcx.sess.edition(), None, None);
expn_data.def_site = self.body_span;
// Make sure that all spans track the fact that they were inlined.
*span =
self.callsite_span.fresh_expansion(expn_data, self.tcx.create_stable_hashing_context());
*span = span.fresh_expansion(self.expn_data);
}

fn visit_place(&mut self, place: &mut Place<'tcx>, context: PlaceContext, location: Location) {
Expand Down
21 changes: 8 additions & 13 deletions compiler/rustc_span/src/hygiene.rs
Original file line number Diff line number Diff line change
Expand Up @@ -874,19 +874,13 @@ impl Span {
/// other compiler-generated code to set per-span properties like allowed unstable features.
/// The returned span belongs to the created expansion and has the new properties,
/// but its location is inherited from the current span.
pub fn fresh_expansion(self, expn_data: ExpnData, ctx: impl HashStableContext) -> Span {
self.fresh_expansion_with_transparency(expn_data, Transparency::Transparent, ctx)
}

pub fn fresh_expansion_with_transparency(
self,
expn_data: ExpnData,
transparency: Transparency,
ctx: impl HashStableContext,
) -> Span {
let expn_id = LocalExpnId::fresh(expn_data, ctx).to_expn_id();
pub fn fresh_expansion(self, expn_id: LocalExpnId) -> Span {
HygieneData::with(|data| {
self.with_ctxt(data.apply_mark(SyntaxContext::root(), expn_id, transparency))
self.with_ctxt(data.apply_mark(
SyntaxContext::root(),
expn_id.to_expn_id(),
Transparency::Transparent,
))
})
}

Expand All @@ -903,7 +897,8 @@ impl Span {
allow_internal_unstable,
..ExpnData::default(ExpnKind::Desugaring(reason), self, edition, None, None)
};
self.fresh_expansion(expn_data, ctx)
let expn_id = LocalExpnId::fresh(expn_data, ctx);
self.fresh_expansion(expn_id)
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/passes/check_code_block_syntax.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ use rustc_errors::{emitter::Emitter, Applicability, Diagnostic, Handler};
use rustc_middle::lint::LintDiagnosticBuilder;
use rustc_parse::parse_stream_from_source_str;
use rustc_session::parse::ParseSess;
use rustc_span::hygiene::{AstPass, ExpnData, ExpnKind, LocalExpnId};
use rustc_span::source_map::{FilePathMapping, SourceMap};
use rustc_span::{hygiene::AstPass, ExpnData, ExpnKind, FileName, InnerSpan, DUMMY_SP};
use rustc_span::{FileName, InnerSpan, DUMMY_SP};

use crate::clean;
use crate::core::DocContext;
Expand Down Expand Up @@ -46,7 +47,8 @@ impl<'a, 'tcx> SyntaxChecker<'a, 'tcx> {
None,
None,
);
let span = DUMMY_SP.fresh_expansion(expn_data, self.cx.tcx.create_stable_hashing_context());
let expn_id = LocalExpnId::fresh(expn_data, self.cx.tcx.create_stable_hashing_context());
let span = DUMMY_SP.fresh_expansion(expn_id);

let is_empty = rustc_driver::catch_fatal_errors(|| {
parse_stream_from_source_str(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
let mut _6: usize; // in scope 0 at $DIR/deduplicate_blocks.rs:4:9: 4:37
let mut _7: bool; // in scope 0 at $DIR/deduplicate_blocks.rs:4:9: 4:37
scope 1 (inlined core::str::<impl str>::as_bytes) { // at $DIR/deduplicate_blocks.rs:3:11: 3:23
debug self => _3; // in scope 1 at $DIR/deduplicate_blocks.rs:3:11: 3:23
let mut _8: &str; // in scope 1 at $DIR/deduplicate_blocks.rs:3:11: 3:23
debug self => _3; // in scope 1 at $SRC_DIR/core/src/str/mod.rs:LL:COL
let mut _8: &str; // in scope 1 at $SRC_DIR/core/src/str/mod.rs:LL:COL
scope 2 {
}
}
Expand All @@ -21,12 +21,12 @@
StorageLive(_2); // scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:23
StorageLive(_3); // scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:23
_3 = _1; // scope 0 at $DIR/deduplicate_blocks.rs:3:11: 3:23
StorageLive(_8); // scope 2 at $DIR/deduplicate_blocks.rs:3:11: 3:23
_8 = _3; // scope 2 at $DIR/deduplicate_blocks.rs:3:11: 3:23
- _2 = transmute::<&str, &[u8]>(move _8) -> bb14; // scope 2 at $DIR/deduplicate_blocks.rs:3:11: 3:23
+ _2 = transmute::<&str, &[u8]>(move _8) -> bb12; // scope 2 at $DIR/deduplicate_blocks.rs:3:11: 3:23
StorageLive(_8); // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL
_8 = _3; // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL
- _2 = transmute::<&str, &[u8]>(move _8) -> bb14; // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL
+ _2 = transmute::<&str, &[u8]>(move _8) -> bb12; // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL
// mir::Constant
// + span: $DIR/deduplicate_blocks.rs:3:11: 3:23
// + span: $SRC_DIR/core/src/str/mod.rs:LL:COL
// + literal: Const { ty: unsafe extern "rust-intrinsic" fn(&str) -> &[u8] {transmute::<&str, &[u8]>}, val: Value(Scalar(<ZST>)) }
}

Expand Down Expand Up @@ -97,7 +97,7 @@

- bb14: {
+ bb12: {
StorageDead(_8); // scope 2 at $DIR/deduplicate_blocks.rs:3:11: 3:23
StorageDead(_8); // scope 2 at $SRC_DIR/core/src/str/mod.rs:LL:COL
StorageDead(_3); // scope 0 at $DIR/deduplicate_blocks.rs:3:22: 3:23
_6 = Len((*_2)); // scope 0 at $DIR/deduplicate_blocks.rs:4:9: 4:37
_7 = Ge(move _6, const 4_usize); // scope 0 at $DIR/deduplicate_blocks.rs:4:9: 4:37
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
- debug z => _3; // in scope 3 at $DIR/cycle.rs:11:9: 11:10
+ debug z => _4; // in scope 3 at $DIR/cycle.rs:11:9: 11:10
scope 4 (inlined std::mem::drop::<i32>) { // at $DIR/cycle.rs:14:5: 14:12
debug _x => _6; // in scope 4 at $DIR/cycle.rs:14:5: 14:12
debug _x => _6; // in scope 4 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
scope 2 {
}
scope 3 (inlined std::mem::drop::<u32>) { // at $DIR/union.rs:15:5: 15:27
debug _x => _4; // in scope 3 at $DIR/union.rs:15:5: 15:27
debug _x => _4; // in scope 3 at $SRC_DIR/core/src/mem/mod.rs:LL:COL
}
}

Expand Down
22 changes: 11 additions & 11 deletions src/test/mir-opt/inline/inline_any_operand.bar.Inline.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ fn bar() -> bool {
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 => _3; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
debug y => _4; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _5: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
let mut _6: i32; // in scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
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 @@ -28,13 +28,13 @@ fn bar() -> bool {
_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:12:5: 12:13
_5 = _3; // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageLive(_6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
_6 = _4; // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
_0 = Eq(move _5, move _6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_6); // scope 2 at $DIR/inline-any-operand.rs:12:5: 12:13
StorageDead(_5); // scope 2 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
Expand Down
6 changes: 3 additions & 3 deletions src/test/mir-opt/inline/inline_closure.foo.Inline.after.mir
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ fn foo(_1: T, _2: i32) -> i32 {
scope 1 {
debug x => _3; // in scope 1 at $DIR/inline-closure.rs:11:9: 11:10
scope 2 (inlined foo::<T>::{closure#0}) { // at $DIR/inline-closure.rs:12:5: 12:12
debug _t => _8; // in scope 2 at $DIR/inline-closure.rs:12:5: 12:12
debug _q => _9; // in scope 2 at $DIR/inline-closure.rs:12:5: 12:12
debug _t => _8; // in scope 2 at $DIR/inline-closure.rs:11:14: 11:16
debug _q => _9; // in scope 2 at $DIR/inline-closure.rs:11:18: 11:20
}
}

Expand All @@ -34,7 +34,7 @@ fn foo(_1: T, _2: i32) -> i32 {
_8 = move (_5.0: i32); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
StorageLive(_9); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
_9 = move (_5.1: i32); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
_0 = _8; // scope 2 at $DIR/inline-closure.rs:12:5: 12:12
_0 = _8; // scope 2 at $DIR/inline-closure.rs:11:22: 11:24
StorageDead(_9); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
StorageDead(_8); // scope 1 at $DIR/inline-closure.rs:12:5: 12:12
StorageDead(_7); // scope 1 at $DIR/inline-closure.rs:12:11: 12:12
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ fn foo(_1: T, _2: &i32) -> i32 {
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 => _8; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
debug _s => _9; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
let _10: &i32; // in scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
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 => _10; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
debug variable => _10; // in scope 3 at $DIR/inline-closure-borrows-arg.rs:13:13: 13:21
}
}
}
Expand All @@ -38,10 +38,10 @@ fn foo(_1: T, _2: &i32) -> i32 {
_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.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:16:5: 16:12
_10 = _8; // scope 2 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
_0 = (*_10); // scope 3 at $DIR/inline-closure-borrows-arg.rs:16:5: 16:12
StorageDead(_10); // scope 2 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 = (*_10); // 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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ fn foo(_1: T, _2: i32) -> (i32, T) {
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 => _9; // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
debug q => (*((*_6).0: &i32)); // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
debug t => (*((*_6).1: &T)); // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
let mut _10: i32; // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
let mut _11: T; // in scope 2 at $DIR/inline-closure-captures.rs:12:5: 12:9
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 _10: i32; // in scope 2 at $DIR/inline-closure-captures.rs:11:19: 11:20
let mut _11: T; // in scope 2 at $DIR/inline-closure-captures.rs:11:22: 11:23
}
}

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

0 comments on commit 48132ca

Please sign in to comment.