Skip to content

Commit e881ea0

Browse files
committed
Auto merge of #125025 - saethlin:clone-your-body, r=<try>
[perf experiments] Clone all MIR bodies We keep saying things like cloning all the MIR would be expensive, but... how expensive actually?
2 parents f8e5660 + 17184f8 commit e881ea0

File tree

21 files changed

+288
-103
lines changed

21 files changed

+288
-103
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -3832,6 +3832,7 @@ dependencies = [
38323832
"rustc_macros",
38333833
"rustc_metadata",
38343834
"rustc_middle",
3835+
"rustc_mir_transform",
38353836
"rustc_monomorphize",
38363837
"rustc_query_system",
38373838
"rustc_serialize",

compiler/rustc_codegen_cranelift/src/base.rs

+3-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use cranelift_codegen::CodegenError;
55
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext};
66
use cranelift_module::ModuleError;
77
use rustc_ast::InlineAsmOptions;
8+
use rustc_codegen_ssa::mir::make_codegen_mir;
89
use rustc_index::IndexVec;
910
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
1011
use rustc_middle::ty::adjustment::PointerCoercion;
@@ -40,7 +41,8 @@ pub(crate) fn codegen_fn<'tcx>(
4041
let symbol_name = tcx.symbol_name(instance).name.to_string();
4142
let _timer = tcx.prof.generic_activity_with_arg("codegen fn", &*symbol_name);
4243

43-
let mir = tcx.instance_mir(instance.def);
44+
let mir = make_codegen_mir(tcx, instance);
45+
let mir = &mir;
4446
let _mir_guard = crate::PrintOnPanic(|| {
4547
let mut buf = Vec::new();
4648
with_no_trimmed_paths!({
@@ -302,19 +304,10 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
302304
.generic_activity("codegen prelude")
303305
.run(|| crate::abi::codegen_fn_prelude(fx, start_block));
304306

305-
let reachable_blocks = traversal::mono_reachable_as_bitset(fx.mir, fx.tcx, fx.instance);
306-
307307
for (bb, bb_data) in fx.mir.basic_blocks.iter_enumerated() {
308308
let block = fx.get_block(bb);
309309
fx.bcx.switch_to_block(block);
310310

311-
if !reachable_blocks.contains(bb) {
312-
// We want to skip this block, because it's not reachable. But we still create
313-
// the block so terminators in other blocks can reference it.
314-
fx.bcx.ins().trap(TrapCode::UnreachableCodeReached);
315-
continue;
316-
}
317-
318311
if bb_data.is_cleanup {
319312
// Unwinding after panicking is not supported
320313
continue;

compiler/rustc_codegen_cranelift/src/common.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ pub(crate) fn create_wrapper_function(
283283
unwind_context.add_function(wrapper_func_id, &ctx, module.isa());
284284
}
285285

286-
pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
286+
pub(crate) struct FunctionCx<'m, 'clif, 'tcx> {
287287
pub(crate) cx: &'clif mut crate::CodegenCx,
288288
pub(crate) module: &'m mut dyn Module,
289289
pub(crate) tcx: TyCtxt<'tcx>,
@@ -294,7 +294,7 @@ pub(crate) struct FunctionCx<'m, 'clif, 'tcx: 'm> {
294294

295295
pub(crate) instance: Instance<'tcx>,
296296
pub(crate) symbol_name: String,
297-
pub(crate) mir: &'tcx Body<'tcx>,
297+
pub(crate) mir: &'m Body<'tcx>,
298298
pub(crate) fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
299299

300300
pub(crate) bcx: FunctionBuilder<'clif>,

compiler/rustc_codegen_ssa/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ rustc_index = { path = "../rustc_index" }
2727
rustc_macros = { path = "../rustc_macros" }
2828
rustc_metadata = { path = "../rustc_metadata" }
2929
rustc_middle = { path = "../rustc_middle" }
30+
rustc_mir_transform = { path = "../rustc_mir_transform" }
3031
rustc_monomorphize = { path = "../rustc_monomorphize" }
3132
rustc_query_system = { path = "../rustc_query_system" }
3233
rustc_serialize = { path = "../rustc_serialize" }

compiler/rustc_codegen_ssa/src/mir/analyze.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
1313
use rustc_middle::{bug, span_bug};
1414
use tracing::debug;
1515

16-
pub fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
17-
fx: &FunctionCx<'a, 'tcx, Bx>,
16+
pub fn non_ssa_locals<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
17+
fx: &FunctionCx<'body, 'a, 'tcx, Bx>,
1818
) -> BitSet<mir::Local> {
1919
let mir = fx.mir;
2020
let dominators = mir.basic_blocks.dominators();
@@ -69,9 +69,9 @@ enum LocalKind {
6969
SSA(DefLocation),
7070
}
7171

72-
struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
73-
fx: &'mir FunctionCx<'a, 'tcx, Bx>,
74-
dominators: &'mir Dominators<mir::BasicBlock>,
72+
struct LocalAnalyzer<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
73+
fx: &'body FunctionCx<'body, 'a, 'tcx, Bx>,
74+
dominators: &'body Dominators<mir::BasicBlock>,
7575
locals: IndexVec<mir::Local, LocalKind>,
7676
}
7777

compiler/rustc_codegen_ssa/src/mir/block.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,17 @@ enum MergingSucc {
3939

4040
/// Used by `FunctionCx::codegen_terminator` for emitting common patterns
4141
/// e.g., creating a basic block, calling a function, etc.
42-
struct TerminatorCodegenHelper<'tcx> {
42+
struct TerminatorCodegenHelper<'body, 'tcx> {
4343
bb: mir::BasicBlock,
44-
terminator: &'tcx mir::Terminator<'tcx>,
44+
terminator: &'body mir::Terminator<'tcx>,
4545
}
4646

47-
impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
47+
impl<'body, 'a, 'tcx> TerminatorCodegenHelper<'body, 'tcx> {
4848
/// Returns the appropriate `Funclet` for the current funclet, if on MSVC,
4949
/// either already previously cached, or newly created, by `landing_pad_for`.
5050
fn funclet<'b, Bx: BuilderMethods<'a, 'tcx>>(
5151
&self,
52-
fx: &'b mut FunctionCx<'a, 'tcx, Bx>,
52+
fx: &'b mut FunctionCx<'body, 'a, 'tcx, Bx>,
5353
) -> Option<&'b Bx::Funclet> {
5454
let cleanup_kinds = fx.cleanup_kinds.as_ref()?;
5555
let funclet_bb = cleanup_kinds[self.bb].funclet_bb(self.bb)?;
@@ -75,7 +75,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
7575
/// stuff in it or next to it.
7676
fn llbb_with_cleanup<Bx: BuilderMethods<'a, 'tcx>>(
7777
&self,
78-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
78+
fx: &mut FunctionCx<'body, 'a, 'tcx, Bx>,
7979
target: mir::BasicBlock,
8080
) -> Bx::BasicBlock {
8181
let (needs_landing_pad, is_cleanupret) = self.llbb_characteristics(fx, target);
@@ -99,7 +99,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
9999

100100
fn llbb_characteristics<Bx: BuilderMethods<'a, 'tcx>>(
101101
&self,
102-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
102+
fx: &mut FunctionCx<'body, 'a, 'tcx, Bx>,
103103
target: mir::BasicBlock,
104104
) -> (bool, bool) {
105105
if let Some(ref cleanup_kinds) = fx.cleanup_kinds {
@@ -124,7 +124,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
124124

125125
fn funclet_br<Bx: BuilderMethods<'a, 'tcx>>(
126126
&self,
127-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
127+
fx: &mut FunctionCx<'body, 'a, 'tcx, Bx>,
128128
bx: &mut Bx,
129129
target: mir::BasicBlock,
130130
mergeable_succ: bool,
@@ -153,7 +153,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
153153
/// return destination `destination` and the unwind action `unwind`.
154154
fn do_call<Bx: BuilderMethods<'a, 'tcx>>(
155155
&self,
156-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
156+
fx: &mut FunctionCx<'body, 'a, 'tcx, Bx>,
157157
bx: &mut Bx,
158158
fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
159159
fn_ptr: Bx::Value,
@@ -272,7 +272,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
272272
/// Generates inline assembly with optional `destination` and `unwind`.
273273
fn do_inlineasm<Bx: BuilderMethods<'a, 'tcx>>(
274274
&self,
275-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
275+
fx: &mut FunctionCx<'body, 'a, 'tcx, Bx>,
276276
bx: &mut Bx,
277277
template: &[InlineAsmTemplatePiece],
278278
operands: &[InlineAsmOperandRef<'tcx, Bx>],
@@ -339,9 +339,13 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
339339
}
340340

341341
/// Codegen implementations for some terminator variants.
342-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
342+
impl<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'body, 'a, 'tcx, Bx> {
343343
/// Generates code for a `Resume` terminator.
344-
fn codegen_resume_terminator(&mut self, helper: TerminatorCodegenHelper<'tcx>, bx: &mut Bx) {
344+
fn codegen_resume_terminator(
345+
&mut self,
346+
helper: TerminatorCodegenHelper<'body, 'tcx>,
347+
bx: &mut Bx,
348+
) {
345349
if let Some(funclet) = helper.funclet(self) {
346350
bx.cleanup_ret(funclet, None);
347351
} else {
@@ -358,7 +362,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
358362

359363
fn codegen_switchint_terminator(
360364
&mut self,
361-
helper: TerminatorCodegenHelper<'tcx>,
365+
helper: TerminatorCodegenHelper<'body, 'tcx>,
362366
bx: &mut Bx,
363367
discr: &mir::Operand<'tcx>,
364368
targets: &SwitchTargets,
@@ -498,7 +502,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
498502
#[tracing::instrument(level = "trace", skip(self, helper, bx))]
499503
fn codegen_drop_terminator(
500504
&mut self,
501-
helper: TerminatorCodegenHelper<'tcx>,
505+
helper: TerminatorCodegenHelper<'body, 'tcx>,
502506
bx: &mut Bx,
503507
source_info: &mir::SourceInfo,
504508
location: mir::Place<'tcx>,
@@ -640,7 +644,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
640644

641645
fn codegen_assert_terminator(
642646
&mut self,
643-
helper: TerminatorCodegenHelper<'tcx>,
647+
helper: TerminatorCodegenHelper<'body, 'tcx>,
644648
bx: &mut Bx,
645649
terminator: &mir::Terminator<'tcx>,
646650
cond: &mir::Operand<'tcx>,
@@ -719,7 +723,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
719723

720724
fn codegen_terminate_terminator(
721725
&mut self,
722-
helper: TerminatorCodegenHelper<'tcx>,
726+
helper: TerminatorCodegenHelper<'body, 'tcx>,
723727
bx: &mut Bx,
724728
terminator: &mir::Terminator<'tcx>,
725729
reason: UnwindTerminateReason,
@@ -749,7 +753,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
749753
/// Returns `Some` if this is indeed a panic intrinsic and codegen is done.
750754
fn codegen_panic_intrinsic(
751755
&mut self,
752-
helper: &TerminatorCodegenHelper<'tcx>,
756+
helper: &TerminatorCodegenHelper<'body, 'tcx>,
753757
bx: &mut Bx,
754758
intrinsic: Option<ty::IntrinsicDef>,
755759
instance: Option<Instance<'tcx>>,
@@ -818,7 +822,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
818822

819823
fn codegen_call_terminator(
820824
&mut self,
821-
helper: TerminatorCodegenHelper<'tcx>,
825+
helper: TerminatorCodegenHelper<'body, 'tcx>,
822826
bx: &mut Bx,
823827
terminator: &mir::Terminator<'tcx>,
824828
func: &mir::Operand<'tcx>,
@@ -1170,7 +1174,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11701174

11711175
fn codegen_asm_terminator(
11721176
&mut self,
1173-
helper: TerminatorCodegenHelper<'tcx>,
1177+
helper: TerminatorCodegenHelper<'body, 'tcx>,
11741178
bx: &mut Bx,
11751179
terminator: &mir::Terminator<'tcx>,
11761180
template: &[ast::InlineAsmTemplatePiece],
@@ -1254,7 +1258,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12541258
}
12551259
}
12561260

1257-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1261+
impl<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'body, 'a, 'tcx, Bx> {
12581262
pub fn codegen_block(&mut self, mut bb: mir::BasicBlock) {
12591263
let llbb = match self.try_llbb(bb) {
12601264
Some(llbb) => llbb,
@@ -1309,7 +1313,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13091313
&mut self,
13101314
bx: &mut Bx,
13111315
bb: mir::BasicBlock,
1312-
terminator: &'tcx mir::Terminator<'tcx>,
1316+
terminator: &'body mir::Terminator<'tcx>,
13131317
) -> MergingSucc {
13141318
debug!("codegen_terminator: {:?}", terminator);
13151319

compiler/rustc_codegen_ssa/src/mir/constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use rustc_target::abi::Abi;
1010

1111
use super::FunctionCx;
1212

13-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
13+
impl<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'body, 'a, 'tcx, Bx> {
1414
pub fn eval_mir_constant_to_operand(
1515
&self,
1616
bx: &mut Bx,

compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::mir::SourceScope;
55

66
use super::FunctionCx;
77

8-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
8+
impl<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'body, 'a, 'tcx, Bx> {
99
pub fn codegen_coverage(&self, bx: &mut Bx, kind: &CoverageKind, scope: SourceScope) {
1010
// Determine the instance that coverage data was originally generated for.
1111
let instance = if let Some(inlined) = scope.inlined_instance(&self.mir.source_scopes) {

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ fn calculate_debuginfo_offset<
203203
DebugInfoOffset { direct_offset, indirect_offsets, result: place }
204204
}
205205

206-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
206+
impl<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'body, 'a, 'tcx, Bx> {
207207
pub fn set_debug_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) {
208208
bx.set_span(source_info.span);
209209
if let Some(dbg_loc) = self.dbg_loc(source_info) {

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ fn memset_intrinsic<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
5454
bx.memset(dst, val, size, align, flags);
5555
}
5656

57-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
57+
impl<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'body, 'a, 'tcx, Bx> {
5858
/// In the `Err` case, returns the instance that should be called instead.
5959
pub fn codegen_intrinsic_call(
6060
bx: &mut Bx,

compiler/rustc_codegen_ssa/src/mir/locals.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ impl<'tcx, V> Locals<'tcx, V> {
3434
}
3535
}
3636

37-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
37+
impl<'body, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'body, 'a, 'tcx, Bx> {
3838
pub(super) fn initialize_locals(&mut self, values: Vec<LocalRef<'tcx, Bx::Value>>) {
3939
assert!(self.locals.values.is_empty());
4040
// FIXME(#115215): After #115025 get's merged this might not be necessary

0 commit comments

Comments
 (0)