Skip to content

Commit 0a3713a

Browse files
committed
Auto merge of #156858 - cjgillot:post-mono-mir-opts, r=<try>
Add post-mono MIR optimizations
2 parents 54333ff + c885a8d commit 0a3713a

60 files changed

Lines changed: 868 additions & 914 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

Cargo.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3814,6 +3814,7 @@ dependencies = [
38143814
"rustc_macros",
38153815
"rustc_metadata",
38163816
"rustc_middle",
3817+
"rustc_mir_transform",
38173818
"rustc_serialize",
38183819
"rustc_session",
38193820
"rustc_span",
@@ -4459,7 +4460,6 @@ dependencies = [
44594460
name = "rustc_monomorphize"
44604461
version = "0.0.0"
44614462
dependencies = [
4462-
"rustc_abi",
44634463
"rustc_data_structures",
44644464
"rustc_errors",
44654465
"rustc_hir",

compiler/rustc_attr_parsing/src/attributes/prototype.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ fn parse_dialect(
9393
sym::analysis => MirDialect::Analysis,
9494
sym::built => MirDialect::Built,
9595
sym::runtime => MirDialect::Runtime,
96+
sym::mono => MirDialect::Mono,
9697

9798
_ => {
9899
cx.adcx().expected_specific_argument(span, &[sym::analysis, sym::built, sym::runtime]);
@@ -136,43 +137,28 @@ fn check_custom_mir(
136137
failed: &mut bool,
137138
) {
138139
let attr_span = cx.attr_span;
140+
let Some((phase, phase_span)) = phase else { return };
139141
let Some((dialect, dialect_span)) = dialect else {
140-
if let Some((_, phase_span)) = phase {
142+
*failed = true;
143+
cx.emit_err(session_diagnostics::CustomMirPhaseRequiresDialect { attr_span, phase_span });
144+
return;
145+
};
146+
147+
match (dialect, phase) {
148+
(MirDialect::Built, _)
149+
| (MirDialect::Analysis, MirPhase::Optimized)
150+
| (MirDialect::Mono, MirPhase::PostCleanup) => {
141151
*failed = true;
142-
cx.emit_err(session_diagnostics::CustomMirPhaseRequiresDialect {
152+
cx.emit_err(session_diagnostics::CustomMirIncompatibleDialectAndPhase {
153+
dialect,
154+
phase,
143155
attr_span,
156+
dialect_span,
144157
phase_span,
145158
});
146159
}
147-
return;
148-
};
149-
150-
match dialect {
151-
MirDialect::Analysis => {
152-
if let Some((MirPhase::Optimized, phase_span)) = phase {
153-
*failed = true;
154-
cx.emit_err(session_diagnostics::CustomMirIncompatibleDialectAndPhase {
155-
dialect,
156-
phase: MirPhase::Optimized,
157-
attr_span,
158-
dialect_span,
159-
phase_span,
160-
});
161-
}
162-
}
163-
164-
MirDialect::Built => {
165-
if let Some((phase, phase_span)) = phase {
166-
*failed = true;
167-
cx.emit_err(session_diagnostics::CustomMirIncompatibleDialectAndPhase {
168-
dialect,
169-
phase,
170-
attr_span,
171-
dialect_span,
172-
phase_span,
173-
});
174-
}
175-
}
176-
MirDialect::Runtime => {}
160+
(MirDialect::Analysis, MirPhase::Initial | MirPhase::PostCleanup)
161+
| (MirDialect::Runtime, MirPhase::Initial | MirPhase::PostCleanup | MirPhase::Optimized)
162+
| (MirDialect::Mono, MirPhase::Initial | MirPhase::Optimized) => {}
177163
}
178164
}

compiler/rustc_codegen_cranelift/src/abi/mod.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_
263263
.mir
264264
.args_iter()
265265
.map(|local| {
266-
let arg_ty = fx.monomorphize(fx.mir.local_decls[local].ty);
266+
let arg_ty = fx.mir.local_decls[local].ty;
267267

268268
// Adapted from https://github.com/rust-lang/rust/blob/145155dc96757002c7b2e9de8489416e2fdbbd57/src/librustc_codegen_llvm/mir/mod.rs#L442-L482
269269
if Some(local) == fx.mir.spread_arg {
@@ -352,7 +352,7 @@ pub(crate) fn codegen_fn_prelude<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, start_
352352
}
353353

354354
for local in fx.mir.vars_and_temps_iter() {
355-
let ty = fx.monomorphize(fx.mir.local_decls[local].ty);
355+
let ty = fx.mir.local_decls[local].ty;
356356
let layout = fx.layout_of(ty);
357357

358358
let is_ssa = ssa_analyzed[local].is_ssa(fx, ty);
@@ -461,9 +461,9 @@ pub(crate) fn codegen_terminator_call<'tcx>(
461461
};
462462

463463
let extra_args = &args[fn_sig.inputs().skip_binder().len()..];
464-
let extra_args = fx.tcx.mk_type_list_from_iter(
465-
extra_args.iter().map(|op_arg| fx.monomorphize(op_arg.node.ty(fx.mir, fx.tcx))),
466-
);
464+
let extra_args = fx
465+
.tcx
466+
.mk_type_list_from_iter(extra_args.iter().map(|op_arg| op_arg.node.ty(fx.mir, fx.tcx)));
467467
let fn_abi = if let Some(instance) = instance {
468468
FullyMonomorphizedLayoutCx(fx.tcx).fn_abi_of_instance(instance, extra_args)
469469
} else {

compiler/rustc_codegen_cranelift/src/base.rs

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ pub(crate) fn codegen_fn<'tcx>(
4444
let symbol_name = tcx.symbol_name(instance).name.to_string();
4545
let _timer = tcx.prof.generic_activity_with_arg("codegen fn", &*symbol_name);
4646

47-
let mir = tcx.instance_mir(instance.def);
47+
let mir = rustc_mir_transform::build_codegen_mir(tcx, instance);
4848
let _mir_guard = crate::PrintOnPanic(|| {
4949
let mut buf = Vec::new();
5050
with_no_trimmed_paths!({
@@ -282,10 +282,8 @@ fn verify_func(tcx: TyCtxt<'_>, writer: &crate::pretty_clif::CommentWriter, func
282282
}
283283

284284
fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
285-
let arg_uninhabited = fx
286-
.mir
287-
.args_iter()
288-
.any(|arg| fx.layout_of(fx.monomorphize(fx.mir.local_decls[arg].ty)).is_uninhabited());
285+
let arg_uninhabited =
286+
fx.mir.args_iter().any(|arg| fx.layout_of(fx.mir.local_decls[arg].ty).is_uninhabited());
289287
if arg_uninhabited {
290288
fx.bcx.append_block_params_for_function_params(fx.block_map[START_BLOCK]);
291289
fx.bcx.switch_to_block(fx.block_map[START_BLOCK]);
@@ -297,19 +295,10 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
297295
.generic_activity("codegen prelude")
298296
.run(|| crate::abi::codegen_fn_prelude(fx, start_block));
299297

300-
let reachable_blocks = traversal::mono_reachable_as_bitset(fx.mir, fx.tcx, fx.instance);
301-
302298
for (bb, bb_data) in fx.mir.basic_blocks.iter_enumerated() {
303299
let block = fx.get_block(bb);
304300
fx.bcx.switch_to_block(block);
305301

306-
if !reachable_blocks.contains(bb) {
307-
// We want to skip this block, because it's not reachable. But we still create
308-
// the block so terminators in other blocks can reference it.
309-
fx.bcx.ins().trap(TrapCode::user(1 /* unreachable */).unwrap());
310-
continue;
311-
}
312-
313302
if bb_data.is_cleanup {
314303
if cfg!(not(feature = "unwinding")) {
315304
continue;
@@ -698,8 +687,8 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
698687
ref operand,
699688
to_ty,
700689
) => {
701-
let from_ty = fx.monomorphize(operand.ty(&fx.mir.local_decls, fx.tcx));
702-
let to_layout = fx.layout_of(fx.monomorphize(to_ty));
690+
let from_ty = operand.ty(&fx.mir.local_decls, fx.tcx);
691+
let to_layout = fx.layout_of(to_ty);
703692
match *from_ty.kind() {
704693
ty::FnDef(def_id, args) => {
705694
let func_ref = fx.get_function_ref(
@@ -722,7 +711,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
722711
ref operand,
723712
to_ty,
724713
) => {
725-
let to_layout = fx.layout_of(fx.monomorphize(to_ty));
714+
let to_layout = fx.layout_of(to_ty);
726715
let operand = codegen_operand(fx, operand);
727716
lval.write_cvalue(fx, operand.cast_pointer_to(to_layout));
728717
}
@@ -752,7 +741,6 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
752741
) => {
753742
let operand = codegen_operand(fx, operand);
754743
let from_ty = operand.layout().ty;
755-
let to_ty = fx.monomorphize(to_ty);
756744

757745
fn is_wide_ptr<'tcx>(fx: &FunctionCx<'_, '_, 'tcx>, ty: Ty<'tcx>) -> bool {
758746
ty.builtin_deref(true).is_some_and(|pointee_ty| {
@@ -824,8 +812,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
824812
}
825813
Rvalue::Repeat(ref operand, times) => {
826814
let operand = codegen_operand(fx, operand);
827-
let times = fx
828-
.monomorphize(times)
815+
let times = times
829816
.try_to_target_usize(fx.tcx)
830817
.expect("expected monomorphic const in codegen");
831818
if operand.layout().size.bytes() == 0 {
@@ -862,7 +849,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
862849
if matches!(**kind, AggregateKind::RawPtr(..)) =>
863850
{
864851
let ty = to_place_and_rval.1.ty(&fx.mir.local_decls, fx.tcx);
865-
let layout = fx.layout_of(fx.monomorphize(ty));
852+
let layout = fx.layout_of(ty);
866853
let [data, meta] = &*operands.raw else {
867854
bug!("RawPtr fields: {operands:?}");
868855
};
@@ -957,8 +944,7 @@ fn codegen_stmt<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, cur_block: Block, stmt:
957944
fn codegen_array_len<'tcx>(fx: &mut FunctionCx<'_, '_, 'tcx>, place: CPlace<'tcx>) -> Value {
958945
match *place.layout().ty.kind() {
959946
ty::Array(_elem_ty, len) => {
960-
let len = fx
961-
.monomorphize(len)
947+
let len = len
962948
.try_to_target_usize(fx.tcx)
963949
.expect("expected monomorphic const in codegen") as i64;
964950
fx.bcx.ins().iconst(fx.pointer_type, len)
@@ -981,7 +967,7 @@ pub(crate) fn codegen_place<'tcx>(
981967
}
982968
PlaceElem::OpaqueCast(ty) => bug!("encountered OpaqueCast({ty}) in codegen"),
983969
PlaceElem::UnwrapUnsafeBinder(ty) => {
984-
cplace = cplace.place_transmute_type(fx, fx.monomorphize(ty));
970+
cplace = cplace.place_transmute_type(fx, ty);
985971
}
986972
PlaceElem::Field(field, _ty) => {
987973
cplace = cplace.place_field(fx, field);

compiler/rustc_codegen_cranelift/src/common.rs

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use cranelift_codegen::isa::TargetFrontendConfig;
22
use cranelift_frontend::{FunctionBuilder, FunctionBuilderContext, Variable};
33
use rustc_abi::{Float, Integer, Primitive};
44
use rustc_index::IndexVec;
5-
use rustc_middle::ty::TypeFoldable;
65
use rustc_middle::ty::layout::{
76
self, FnAbiError, FnAbiOfHelpers, FnAbiRequest, LayoutError, LayoutOfHelpers,
87
};
@@ -342,17 +341,6 @@ impl<'tcx> HasTargetSpec for FunctionCx<'_, '_, 'tcx> {
342341
}
343342

344343
impl<'tcx> FunctionCx<'_, '_, 'tcx> {
345-
pub(crate) fn monomorphize<T>(&self, value: T) -> T
346-
where
347-
T: TypeFoldable<TyCtxt<'tcx>> + Copy,
348-
{
349-
self.instance.instantiate_mir_and_normalize_erasing_regions(
350-
self.tcx,
351-
ty::TypingEnv::fully_monomorphized(),
352-
ty::EarlyBinder::bind(value),
353-
)
354-
}
355-
356344
pub(crate) fn clif_type(&self, ty: Ty<'tcx>) -> Option<Type> {
357345
clif_type_from_ty(self.tcx, ty)
358346
}

compiler/rustc_codegen_cranelift/src/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ pub(crate) fn eval_mir_constant<'tcx>(
7878
fx: &FunctionCx<'_, '_, 'tcx>,
7979
constant: &ConstOperand<'tcx>,
8080
) -> (ConstValue, Ty<'tcx>) {
81-
let cv = fx.monomorphize(constant.const_);
81+
let cv = constant.const_;
8282
// This cannot fail because we checked all required_consts in advance.
8383
let val = cv
8484
.eval(fx.tcx, ty::TypingEnv::fully_monomorphized(), constant.span)

compiler/rustc_codegen_cranelift/src/inline_asm.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ pub(crate) fn codegen_inline_asm_terminator<'tcx>(
111111
.span_err(span, "asm! and global_asm! sym operands are not yet supported");
112112
}
113113

114-
let const_ = fx.monomorphize(value.const_);
114+
let const_ = value.const_;
115115
if let ty::FnDef(def_id, args) = *const_.ty().kind() {
116116
let instance = ty::Instance::resolve_for_fn_ptr(
117117
fx.tcx,

compiler/rustc_codegen_cranelift/src/intrinsics/simd.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ pub(super) fn codegen_simd_intrinsic_call<'tcx>(
180180
}
181181

182182
// Make sure this is actually a SIMD vector.
183-
let idx_ty = fx.monomorphize(idx.node.ty(fx.mir, fx.tcx));
183+
let idx_ty = idx.node.ty(fx.mir, fx.tcx);
184184
if !idx_ty.is_simd()
185185
|| !matches!(idx_ty.simd_size_and_type(fx.tcx).1.kind(), ty::Uint(ty::UintTy::U32))
186186
{

compiler/rustc_codegen_cranelift/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ extern crate rustc_hir;
2323
extern crate rustc_incremental;
2424
extern crate rustc_index;
2525
extern crate rustc_log;
26+
extern crate rustc_mir_transform;
2627
extern crate rustc_session;
2728
extern crate rustc_span;
2829
extern crate rustc_symbol_mangling;

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_lint_defs = { path = "../rustc_lint_defs" }
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_serialize = { path = "../rustc_serialize" }
3132
rustc_session = { path = "../rustc_session" }
3233
rustc_span = { path = "../rustc_span" }

0 commit comments

Comments
 (0)