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

Commit 8aad989

Browse files
committed
Auto merge of rust-lang#130051 - cjgillot:clone-mir, r=<try>
Post-mono MIR opts
2 parents 26b5599 + 83effc2 commit 8aad989

File tree

19 files changed

+99
-108
lines changed

19 files changed

+99
-108
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3463,6 +3463,7 @@ dependencies = [
34633463
"rustc_macros",
34643464
"rustc_metadata",
34653465
"rustc_middle",
3466+
"rustc_mir_transform",
34663467
"rustc_monomorphize",
34673468
"rustc_query_system",
34683469
"rustc_serialize",

compiler/rustc_codegen_ssa/Cargo.toml

Lines changed: 1 addition & 0 deletions
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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,16 +14,15 @@ use super::FunctionCx;
1414
use crate::traits::*;
1515

1616
pub(crate) fn non_ssa_locals<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
17-
fx: &FunctionCx<'a, 'tcx, Bx>,
17+
fx: &FunctionCx<'a, '_, 'tcx, Bx>,
1818
) -> BitSet<mir::Local> {
19-
let mir = fx.mir;
19+
let mir = &fx.mir;
2020
let dominators = mir.basic_blocks.dominators();
2121
let locals = mir
2222
.local_decls
2323
.iter()
2424
.map(|decl| {
25-
let ty = fx.monomorphize(decl.ty);
26-
let layout = fx.cx.spanned_layout_of(ty, decl.source_info.span);
25+
let layout = fx.cx.spanned_layout_of(decl.ty, decl.source_info.span);
2726
if layout.is_zst() {
2827
LocalKind::ZST
2928
} else if fx.cx.is_backend_immediate(layout) || fx.cx.is_backend_scalar_pair(layout) {
@@ -70,7 +69,7 @@ enum LocalKind {
7069
}
7170

7271
struct LocalAnalyzer<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> {
73-
fx: &'mir FunctionCx<'a, 'tcx, Bx>,
72+
fx: &'mir FunctionCx<'a, 'mir, 'tcx, Bx>,
7473
dominators: &'mir Dominators<mir::BasicBlock>,
7574
locals: IndexVec<mir::Local, LocalKind>,
7675
}
@@ -110,10 +109,9 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> LocalAnalyzer<'mir, 'a, 'tcx,
110109
);
111110
if is_consume {
112111
let base_ty = place_base.ty(self.fx.mir, cx.tcx());
113-
let base_ty = self.fx.monomorphize(base_ty);
114112

115113
// ZSTs don't require any actual memory access.
116-
let elem_ty = base_ty.projection_ty(cx.tcx(), self.fx.monomorphize(elem)).ty;
114+
let elem_ty = base_ty.projection_ty(cx.tcx(), elem).ty;
117115
let span = self.fx.mir.local_decls[place_ref.local].source_info.span;
118116
if cx.spanned_layout_of(elem_ty, span).is_zst() {
119117
return;
@@ -237,7 +235,6 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
237235
let kind = &mut self.locals[local];
238236
if *kind != LocalKind::Memory {
239237
let ty = self.fx.mir.local_decls[local].ty;
240-
let ty = self.fx.monomorphize(ty);
241238
if self.fx.cx.type_needs_drop(ty) {
242239
// Only need the place if we're actually dropping it.
243240
*kind = LocalKind::Memory;

compiler/rustc_codegen_ssa/src/mir/block.rs

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ enum MergingSucc {
3737

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

45-
impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
45+
impl<'a, 'tcx> TerminatorCodegenHelper<'_, 'tcx> {
4646
/// Returns the appropriate `Funclet` for the current funclet, if on MSVC,
4747
/// either already previously cached, or newly created, by `landing_pad_for`.
4848
fn funclet<'b, Bx: BuilderMethods<'a, 'tcx>>(
4949
&self,
50-
fx: &'b mut FunctionCx<'a, 'tcx, Bx>,
50+
fx: &'b mut FunctionCx<'a, '_, 'tcx, Bx>,
5151
) -> Option<&'b Bx::Funclet> {
5252
let cleanup_kinds = fx.cleanup_kinds.as_ref()?;
5353
let funclet_bb = cleanup_kinds[self.bb].funclet_bb(self.bb)?;
@@ -73,7 +73,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
7373
/// stuff in it or next to it.
7474
fn llbb_with_cleanup<Bx: BuilderMethods<'a, 'tcx>>(
7575
&self,
76-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
76+
fx: &mut FunctionCx<'a, '_, 'tcx, Bx>,
7777
target: mir::BasicBlock,
7878
) -> Bx::BasicBlock {
7979
let (needs_landing_pad, is_cleanupret) = self.llbb_characteristics(fx, target);
@@ -97,7 +97,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
9797

9898
fn llbb_characteristics<Bx: BuilderMethods<'a, 'tcx>>(
9999
&self,
100-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
100+
fx: &mut FunctionCx<'a, '_, 'tcx, Bx>,
101101
target: mir::BasicBlock,
102102
) -> (bool, bool) {
103103
if let Some(ref cleanup_kinds) = fx.cleanup_kinds {
@@ -122,7 +122,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
122122

123123
fn funclet_br<Bx: BuilderMethods<'a, 'tcx>>(
124124
&self,
125-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
125+
fx: &mut FunctionCx<'a, '_, 'tcx, Bx>,
126126
bx: &mut Bx,
127127
target: mir::BasicBlock,
128128
mergeable_succ: bool,
@@ -151,7 +151,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
151151
/// return destination `destination` and the unwind action `unwind`.
152152
fn do_call<Bx: BuilderMethods<'a, 'tcx>>(
153153
&self,
154-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
154+
fx: &mut FunctionCx<'a, '_, 'tcx, Bx>,
155155
bx: &mut Bx,
156156
fn_abi: &'tcx FnAbi<'tcx, Ty<'tcx>>,
157157
fn_ptr: Bx::Value,
@@ -270,7 +270,7 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
270270
/// Generates inline assembly with optional `destination` and `unwind`.
271271
fn do_inlineasm<Bx: BuilderMethods<'a, 'tcx>>(
272272
&self,
273-
fx: &mut FunctionCx<'a, 'tcx, Bx>,
273+
fx: &mut FunctionCx<'a, '_, 'tcx, Bx>,
274274
bx: &mut Bx,
275275
template: &[InlineAsmTemplatePiece],
276276
operands: &[InlineAsmOperandRef<'tcx, Bx>],
@@ -337,9 +337,13 @@ impl<'a, 'tcx> TerminatorCodegenHelper<'tcx> {
337337
}
338338

339339
/// Codegen implementations for some terminator variants.
340-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
340+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, '_, 'tcx, Bx> {
341341
/// Generates code for a `Resume` terminator.
342-
fn codegen_resume_terminator(&mut self, helper: TerminatorCodegenHelper<'tcx>, bx: &mut Bx) {
342+
fn codegen_resume_terminator(
343+
&mut self,
344+
helper: TerminatorCodegenHelper<'_, 'tcx>,
345+
bx: &mut Bx,
346+
) {
343347
if let Some(funclet) = helper.funclet(self) {
344348
bx.cleanup_ret(funclet, None);
345349
} else {
@@ -356,7 +360,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
356360

357361
fn codegen_switchint_terminator(
358362
&mut self,
359-
helper: TerminatorCodegenHelper<'tcx>,
363+
helper: TerminatorCodegenHelper<'_, 'tcx>,
360364
bx: &mut Bx,
361365
discr: &mir::Operand<'tcx>,
362366
targets: &SwitchTargets,
@@ -496,7 +500,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
496500
#[tracing::instrument(level = "trace", skip(self, helper, bx))]
497501
fn codegen_drop_terminator(
498502
&mut self,
499-
helper: TerminatorCodegenHelper<'tcx>,
503+
helper: TerminatorCodegenHelper<'_, 'tcx>,
500504
bx: &mut Bx,
501505
source_info: &mir::SourceInfo,
502506
location: mir::Place<'tcx>,
@@ -505,7 +509,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
505509
mergeable_succ: bool,
506510
) -> MergingSucc {
507511
let ty = location.ty(self.mir, bx.tcx()).ty;
508-
let ty = self.monomorphize(ty);
509512
let drop_fn = Instance::resolve_drop_in_place(bx.tcx(), ty);
510513

511514
if let ty::InstanceKind::DropGlue(_, None) = drop_fn.def {
@@ -638,7 +641,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
638641

639642
fn codegen_assert_terminator(
640643
&mut self,
641-
helper: TerminatorCodegenHelper<'tcx>,
644+
helper: TerminatorCodegenHelper<'_, 'tcx>,
642645
bx: &mut Bx,
643646
terminator: &mir::Terminator<'tcx>,
644647
cond: &mir::Operand<'tcx>,
@@ -717,7 +720,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
717720

718721
fn codegen_terminate_terminator(
719722
&mut self,
720-
helper: TerminatorCodegenHelper<'tcx>,
723+
helper: TerminatorCodegenHelper<'_, 'tcx>,
721724
bx: &mut Bx,
722725
terminator: &mir::Terminator<'tcx>,
723726
reason: UnwindTerminateReason,
@@ -747,7 +750,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
747750
/// Returns `Some` if this is indeed a panic intrinsic and codegen is done.
748751
fn codegen_panic_intrinsic(
749752
&mut self,
750-
helper: &TerminatorCodegenHelper<'tcx>,
753+
helper: &TerminatorCodegenHelper<'_, 'tcx>,
751754
bx: &mut Bx,
752755
intrinsic: ty::IntrinsicDef,
753756
instance: Option<Instance<'tcx>>,
@@ -815,7 +818,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
815818

816819
fn codegen_call_terminator(
817820
&mut self,
818-
helper: TerminatorCodegenHelper<'tcx>,
821+
helper: TerminatorCodegenHelper<'_, 'tcx>,
819822
bx: &mut Bx,
820823
terminator: &mir::Terminator<'tcx>,
821824
func: &mir::Operand<'tcx>,
@@ -868,10 +871,9 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
868871
let abi = sig.abi();
869872

870873
let extra_args = &args[sig.inputs().skip_binder().len()..];
871-
let extra_args = bx.tcx().mk_type_list_from_iter(extra_args.iter().map(|op_arg| {
872-
let op_ty = op_arg.node.ty(self.mir, bx.tcx());
873-
self.monomorphize(op_ty)
874-
}));
874+
let extra_args = bx.tcx().mk_type_list_from_iter(
875+
extra_args.iter().map(|op_arg| op_arg.node.ty(self.mir, bx.tcx())),
876+
);
875877

876878
let fn_abi = match instance {
877879
Some(instance) => bx.fn_abi_of_instance(instance, extra_args),
@@ -1154,7 +1156,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11541156

11551157
fn codegen_asm_terminator(
11561158
&mut self,
1157-
helper: TerminatorCodegenHelper<'tcx>,
1159+
helper: TerminatorCodegenHelper<'_, 'tcx>,
11581160
bx: &mut Bx,
11591161
terminator: &mir::Terminator<'tcx>,
11601162
template: &[ast::InlineAsmTemplatePiece],
@@ -1196,8 +1198,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
11961198
InlineAsmOperandRef::Const { string }
11971199
}
11981200
mir::InlineAsmOperand::SymFn { ref value } => {
1199-
let const_ = self.monomorphize(value.const_);
1200-
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
1201+
if let ty::FnDef(def_id, args) = *value.const_.ty().kind() {
12011202
let instance = ty::Instance::resolve_for_fn_ptr(
12021203
bx.tcx(),
12031204
ty::ParamEnv::reveal_all(),
@@ -1238,7 +1239,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12381239
}
12391240
}
12401241

1241-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
1242+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, '_, 'tcx, Bx> {
12421243
pub fn codegen_block(&mut self, mut bb: mir::BasicBlock) {
12431244
let llbb = match self.try_llbb(bb) {
12441245
Some(llbb) => llbb,
@@ -1293,7 +1294,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12931294
&mut self,
12941295
bx: &mut Bx,
12951296
bb: mir::BasicBlock,
1296-
terminator: &'tcx mir::Terminator<'tcx>,
1297+
terminator: &mir::Terminator<'tcx>,
12971298
) -> MergingSucc {
12981299
debug!("codegen_terminator: {:?}", terminator);
12991300

compiler/rustc_codegen_ssa/src/mir/constant.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@ use crate::errors;
99
use crate::mir::operand::OperandRef;
1010
use crate::traits::*;
1111

12-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, '_, 'tcx, Bx> {
1313
pub fn eval_mir_constant_to_operand(
1414
&self,
1515
bx: &mut Bx,
1616
constant: &mir::ConstOperand<'tcx>,
1717
) -> OperandRef<'tcx, Bx::Value> {
1818
let val = self.eval_mir_constant(constant);
19-
let ty = self.monomorphize(constant.ty());
20-
OperandRef::from_const(bx, val, ty)
19+
OperandRef::from_const(bx, val, constant.ty())
2120
}
2221

2322
pub fn eval_mir_constant(&self, constant: &mir::ConstOperand<'tcx>) -> mir::ConstValue<'tcx> {
2423
// `MirUsedCollector` visited all required_consts before codegen began, so if we got here
2524
// there can be no more constants that fail to evaluate.
26-
self.monomorphize(constant.const_)
25+
constant
26+
.const_
2727
.eval(self.cx.tcx(), ty::ParamEnv::reveal_all(), constant.span)
2828
.expect("erroneous constant missed by mono item collection")
2929
}
@@ -37,7 +37,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
3737
&self,
3838
constant: &mir::ConstOperand<'tcx>,
3939
) -> Result<Result<ty::ValTree<'tcx>, Ty<'tcx>>, ErrorHandled> {
40-
let uv = match self.monomorphize(constant.const_) {
40+
let uv = match constant.const_ {
4141
mir::Const::Unevaluated(uv, _) => uv.shrink(),
4242
mir::Const::Ty(_, c) => match c.kind() {
4343
// A constant that came from a const generic but was then used as an argument to old-style
@@ -55,7 +55,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
5555
// const generic, and get rid of this entire function.
5656
other => span_bug!(constant.span, "{other:#?}"),
5757
};
58-
let uv = self.monomorphize(uv);
5958
self.cx.tcx().const_eval_resolve_for_typeck(ty::ParamEnv::reveal_all(), uv, constant.span)
6059
}
6160

@@ -65,7 +64,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6564
bx: &Bx,
6665
constant: &mir::ConstOperand<'tcx>,
6766
) -> (Bx::Value, Ty<'tcx>) {
68-
let ty = self.monomorphize(constant.ty());
67+
let ty = constant.ty();
6968
let ty_is_simd = ty.is_simd();
7069
// FIXME: ideally we'd assert that this is a SIMD type, but simd_shuffle
7170
// in its current form relies on a regular array being passed as an

compiler/rustc_codegen_ssa/src/mir/coverageinfo.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,10 @@ use rustc_middle::mir::SourceScope;
44
use super::FunctionCx;
55
use crate::traits::*;
66

7-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
7+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, '_, 'tcx, Bx> {
88
pub fn codegen_coverage(&self, bx: &mut Bx, kind: &CoverageKind, scope: SourceScope) {
99
// Determine the instance that coverage data was originally generated for.
10-
let instance = if let Some(inlined) = scope.inlined_instance(&self.mir.source_scopes) {
11-
self.monomorphize(inlined)
12-
} else {
13-
self.instance
14-
};
10+
let instance = scope.inlined_instance(&self.mir.source_scopes).unwrap_or(self.instance);
1511

1612
// Handle the coverage info in a backend-specific way.
1713
bx.add_coverage(instance, kind);

compiler/rustc_codegen_ssa/src/mir/debuginfo.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ fn calculate_debuginfo_offset<
201201
DebugInfoOffset { direct_offset, indirect_offsets, result: place }
202202
}
203203

204-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
204+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, '_, 'tcx, Bx> {
205205
pub fn set_debug_loc(&self, bx: &mut Bx, source_info: mir::SourceInfo) {
206206
bx.set_span(source_info.span);
207207
if let Some(dbg_loc) = self.dbg_loc(source_info) {
@@ -279,9 +279,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
279279
// FIXME(eddyb) is this `+ 1` needed at all?
280280
let kind = VariableKind::ArgumentVariable(arg_index + 1);
281281

282-
let arg_ty = self.monomorphize(decl.ty);
283-
284-
self.cx.create_dbg_var(name, arg_ty, dbg_scope, kind, span)
282+
self.cx.create_dbg_var(name, decl.ty, dbg_scope, kind, span)
285283
},
286284
)
287285
} else {
@@ -457,13 +455,13 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
457455
};
458456

459457
let var_ty = if let Some(ref fragment) = var.composite {
460-
self.monomorphize(fragment.ty)
458+
fragment.ty
461459
} else {
462460
match var.value {
463461
mir::VarDebugInfoContents::Place(place) => {
464462
self.monomorphized_place_ty(place.as_ref())
465463
}
466-
mir::VarDebugInfoContents::Const(c) => self.monomorphize(c.ty()),
464+
mir::VarDebugInfoContents::Const(c) => c.ty(),
467465
}
468466
};
469467

compiler/rustc_codegen_ssa/src/mir/intrinsic.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ fn memset_intrinsic<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
4949
bx.memset(dst, val, size, align, flags);
5050
}
5151

52-
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
52+
impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, '_, 'tcx, Bx> {
5353
/// In the `Err` case, returns the instance that should be called instead.
5454
pub fn codegen_intrinsic_call(
5555
bx: &mut Bx,

0 commit comments

Comments
 (0)