Skip to content

Commit

Permalink
Auto merge of rust-lang#123256 - saethlin:instsimplify-earlier, r=<try>
Browse files Browse the repository at this point in the history
Run InstSimplify before UnreachablePropagation

This is the pass ordering change I was musing about: rust-lang#122975 (comment)

r? `@ghost`
  • Loading branch information
bors committed Mar 31, 2024
2 parents 395f780 + 0da8676 commit 66e8eb9
Show file tree
Hide file tree
Showing 57 changed files with 147 additions and 149 deletions.
67 changes: 42 additions & 25 deletions compiler/rustc_mir_transform/src/instsimplify.rs
Expand Up @@ -11,42 +11,59 @@ use rustc_span::symbol::Symbol;
use rustc_target::abi::FieldIdx;
use rustc_target::spec::abi::Abi;

pub struct InstSimplify;
pub enum InstSimplify {
BeforeUnreachablePropagation,
Final,
}

impl<'tcx> MirPass<'tcx> for InstSimplify {
fn name(&self) -> &'static str {
match &self {
InstSimplify::BeforeUnreachablePropagation => {
"InstSimplify-before-unreachable-propagation"
}
InstSimplify::Final => "InstSimplify-final",
}
}

fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let ctx = InstSimplifyContext {
tcx,
local_decls: &body.local_decls,
param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
};
let preserve_ub_checks =
attr::contains_name(tcx.hir().krate_attrs(), sym::rustc_preserve_ub_checks);
for block in body.basic_blocks.as_mut() {
for statement in block.statements.iter_mut() {
match statement.kind {
StatementKind::Assign(box (_place, ref mut rvalue)) => {
if !preserve_ub_checks {
ctx.simplify_ub_check(&statement.source_info, rvalue);
}
ctx.simplify_bool_cmp(&statement.source_info, rvalue);
ctx.simplify_ref_deref(&statement.source_info, rvalue);
ctx.simplify_len(&statement.source_info, rvalue);
ctx.simplify_cast(rvalue);
inst_simplify(tcx, body);
}
}

fn inst_simplify<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let ctx = InstSimplifyContext {
tcx,
local_decls: &body.local_decls,
param_env: tcx.param_env_reveal_all_normalized(body.source.def_id()),
};
let preserve_ub_checks =
attr::contains_name(tcx.hir().krate_attrs(), sym::rustc_preserve_ub_checks);
for block in body.basic_blocks.as_mut() {
for statement in block.statements.iter_mut() {
match statement.kind {
StatementKind::Assign(box (_place, ref mut rvalue)) => {
if !preserve_ub_checks {
ctx.simplify_ub_check(&statement.source_info, rvalue);
}
_ => {}
ctx.simplify_bool_cmp(&statement.source_info, rvalue);
ctx.simplify_ref_deref(&statement.source_info, rvalue);
ctx.simplify_len(&statement.source_info, rvalue);
ctx.simplify_cast(rvalue);
}
_ => {}
}

ctx.simplify_primitive_clone(block.terminator.as_mut().unwrap(), &mut block.statements);
ctx.simplify_intrinsic_assert(block.terminator.as_mut().unwrap());
ctx.simplify_nounwind_call(block.terminator.as_mut().unwrap());
simplify_duplicate_switch_targets(block.terminator.as_mut().unwrap());
}

let terminator = block.terminator.as_mut().unwrap();
ctx.simplify_primitive_clone(terminator, &mut block.statements);
ctx.simplify_intrinsic_assert(terminator);
ctx.simplify_nounwind_call(terminator);
simplify_duplicate_switch_targets(terminator);
}
}

Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_mir_transform/src/lib.rs
Expand Up @@ -581,6 +581,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&remove_unneeded_drops::RemoveUnneededDrops,
// Type instantiation may create uninhabited enums.
&uninhabited_enum_branching::UninhabitedEnumBranching,
&instsimplify::InstSimplify::BeforeUnreachablePropagation,
&unreachable_prop::UnreachablePropagation,
&o1(simplify::SimplifyCfg::AfterUninhabitedEnumBranching),
// Inlining may have introduced a lot of redundant code and a large move pattern.
Expand All @@ -591,9 +592,9 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&ref_prop::ReferencePropagation,
&sroa::ScalarReplacementOfAggregates,
&match_branches::MatchBranchSimplification,
// inst combine is after MatchBranchSimplification to clean up Ne(_1, false)
// InstSimplify is after MatchBranchSimplification to clean up Ne(_1, false)
&multiple_return_terminators::MultipleReturnTerminators,
&instsimplify::InstSimplify,
&instsimplify::InstSimplify::Final,
&simplify::SimplifyLocals::BeforeConstProp,
&dead_store_elimination::DeadStoreElimination::Initial,
&gvn::GVN,
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/slice_len.rs
@@ -1,5 +1,5 @@
//@ unit-test: GVN
//@ compile-flags: -Zmir-enable-passes=+InstSimplify
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-unreachable-propagation
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
// EMIT_MIR_FOR_EACH_BIT_WIDTH

Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/dataflow-const-prop/slice_len.rs
@@ -1,6 +1,6 @@
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
//@ unit-test: DataflowConstProp
//@ compile-flags: -Zmir-enable-passes=+InstSimplify
//@ compile-flags: -Zmir-enable-passes=+InstSimplify-before-unreachable-propagation
// EMIT_MIR_FOR_EACH_BIT_WIDTH

// EMIT_MIR slice_len.main.DataflowConstProp.diff
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `eq_false` before InstSimplify
+ // MIR for `eq_false` after InstSimplify
- // MIR for `eq_false` before InstSimplify-before-unreachable-propagation
+ // MIR for `eq_false` after InstSimplify-before-unreachable-propagation

fn eq_false(_1: bool) -> u32 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `eq_true` before InstSimplify
+ // MIR for `eq_true` after InstSimplify
- // MIR for `eq_true` before InstSimplify-before-unreachable-propagation
+ // MIR for `eq_true` after InstSimplify-before-unreachable-propagation

fn eq_true(_1: bool) -> u32 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `false_eq` before InstSimplify
+ // MIR for `false_eq` after InstSimplify
- // MIR for `false_eq` before InstSimplify-before-unreachable-propagation
+ // MIR for `false_eq` after InstSimplify-before-unreachable-propagation

fn false_eq(_1: bool) -> u32 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `false_ne` before InstSimplify
+ // MIR for `false_ne` after InstSimplify
- // MIR for `false_ne` before InstSimplify-before-unreachable-propagation
+ // MIR for `false_ne` after InstSimplify-before-unreachable-propagation

fn false_ne(_1: bool) -> u32 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `ne_false` before InstSimplify
+ // MIR for `ne_false` after InstSimplify
- // MIR for `ne_false` before InstSimplify-before-unreachable-propagation
+ // MIR for `ne_false` after InstSimplify-before-unreachable-propagation

fn ne_false(_1: bool) -> u32 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `ne_true` before InstSimplify
+ // MIR for `ne_true` after InstSimplify
- // MIR for `ne_true` before InstSimplify-before-unreachable-propagation
+ // MIR for `ne_true` after InstSimplify-before-unreachable-propagation

fn ne_true(_1: bool) -> u32 {
debug x => _1;
Expand Down
18 changes: 9 additions & 9 deletions tests/mir-opt/instsimplify/bool_compare.rs
@@ -1,55 +1,55 @@
//@ unit-test: InstSimplify
//@ unit-test: InstSimplify-before-unreachable-propagation

// EMIT_MIR bool_compare.eq_true.InstSimplify.diff
// EMIT_MIR bool_compare.eq_true.InstSimplify-before-unreachable-propagation.diff
fn eq_true(x: bool) -> u32 {
// CHECK-LABEL: fn eq_true(
// CHECK-NOT: Eq(
if x == true { 0 } else { 1 }
}

// EMIT_MIR bool_compare.true_eq.InstSimplify.diff
// EMIT_MIR bool_compare.true_eq.InstSimplify-before-unreachable-propagation.diff
fn true_eq(x: bool) -> u32 {
// CHECK-LABEL: fn true_eq(
// CHECK-NOT: Eq(
if true == x { 0 } else { 1 }
}

// EMIT_MIR bool_compare.ne_true.InstSimplify.diff
// EMIT_MIR bool_compare.ne_true.InstSimplify-before-unreachable-propagation.diff
fn ne_true(x: bool) -> u32 {
// CHECK-LABEL: fn ne_true(
// CHECK: Not(
if x != true { 0 } else { 1 }
}

// EMIT_MIR bool_compare.true_ne.InstSimplify.diff
// EMIT_MIR bool_compare.true_ne.InstSimplify-before-unreachable-propagation.diff
fn true_ne(x: bool) -> u32 {
// CHECK-LABEL: fn true_ne(
// CHECK: Not(
if true != x { 0 } else { 1 }
}

// EMIT_MIR bool_compare.eq_false.InstSimplify.diff
// EMIT_MIR bool_compare.eq_false.InstSimplify-before-unreachable-propagation.diff
fn eq_false(x: bool) -> u32 {
// CHECK-LABEL: fn eq_false(
// CHECK: Not(
if x == false { 0 } else { 1 }
}

// EMIT_MIR bool_compare.false_eq.InstSimplify.diff
// EMIT_MIR bool_compare.false_eq.InstSimplify-before-unreachable-propagation.diff
fn false_eq(x: bool) -> u32 {
// CHECK-LABEL: fn false_eq(
// CHECK: Not(
if false == x { 0 } else { 1 }
}

// EMIT_MIR bool_compare.ne_false.InstSimplify.diff
// EMIT_MIR bool_compare.ne_false.InstSimplify-before-unreachable-propagation.diff
fn ne_false(x: bool) -> u32 {
// CHECK-LABEL: fn ne_false(
// CHECK-NOT: Ne(
if x != false { 0 } else { 1 }
}

// EMIT_MIR bool_compare.false_ne.InstSimplify.diff
// EMIT_MIR bool_compare.false_ne.InstSimplify-before-unreachable-propagation.diff
fn false_ne(x: bool) -> u32 {
// CHECK-LABEL: fn false_ne(
// CHECK-NOT: Ne(
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `true_eq` before InstSimplify
+ // MIR for `true_eq` after InstSimplify
- // MIR for `true_eq` before InstSimplify-before-unreachable-propagation
+ // MIR for `true_eq` after InstSimplify-before-unreachable-propagation

fn true_eq(_1: bool) -> u32 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `true_ne` before InstSimplify
+ // MIR for `true_ne` after InstSimplify
- // MIR for `true_ne` before InstSimplify-before-unreachable-propagation
+ // MIR for `true_ne` after InstSimplify-before-unreachable-propagation

fn true_ne(_1: bool) -> u32 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `redundant` before InstSimplify
+ // MIR for `redundant` after InstSimplify
- // MIR for `redundant` before InstSimplify-before-unreachable-propagation
+ // MIR for `redundant` after InstSimplify-before-unreachable-propagation

fn redundant(_1: *const &u8) -> *const &u8 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `roundtrip` before InstSimplify
+ // MIR for `roundtrip` after InstSimplify
- // MIR for `roundtrip` before InstSimplify-before-unreachable-propagation
+ // MIR for `roundtrip` after InstSimplify-before-unreachable-propagation

fn roundtrip(_1: *const u8) -> *const u8 {
debug x => _1;
Expand Down
6 changes: 3 additions & 3 deletions tests/mir-opt/instsimplify/casts.rs
@@ -1,4 +1,4 @@
//@ unit-test: InstSimplify
//@ unit-test: InstSimplify-before-unreachable-propagation
//@ compile-flags: -Zinline-mir
#![crate_type = "lib"]

Expand All @@ -7,15 +7,15 @@ fn generic_cast<T, U>(x: *const T) -> *const U {
x as *const U
}

// EMIT_MIR casts.redundant.InstSimplify.diff
// EMIT_MIR casts.redundant.InstSimplify-before-unreachable-propagation.diff
pub fn redundant<'a, 'b: 'a>(x: *const &'a u8) -> *const &'a u8 {
// CHECK-LABEL: fn redundant(
// CHECK: inlined generic_cast
// CHECK-NOT: as
generic_cast::<&'a u8, &'b u8>(x) as *const &'a u8
}

// EMIT_MIR casts.roundtrip.InstSimplify.diff
// EMIT_MIR casts.roundtrip.InstSimplify-before-unreachable-propagation.diff
pub fn roundtrip(x: *const u8) -> *const u8 {
// CHECK-LABEL: fn roundtrip(
// CHECK: _4 = _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `norm2` before InstSimplify
+ // MIR for `norm2` after InstSimplify
- // MIR for `norm2` before InstSimplify-before-unreachable-propagation
+ // MIR for `norm2` after InstSimplify-before-unreachable-propagation

fn norm2(_1: [f32; 2]) -> f32 {
debug x => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `norm2` before InstSimplify
+ // MIR for `norm2` after InstSimplify
- // MIR for `norm2` before InstSimplify-before-unreachable-propagation
+ // MIR for `norm2` after InstSimplify-before-unreachable-propagation

fn norm2(_1: [f32; 2]) -> f32 {
debug x => _1;
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/instsimplify/combine_array_len.rs
@@ -1,7 +1,7 @@
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY
//@ unit-test: InstSimplify
//@ unit-test: InstSimplify-before-unreachable-propagation

// EMIT_MIR combine_array_len.norm2.InstSimplify.diff
// EMIT_MIR combine_array_len.norm2.InstSimplify-before-unreachable-propagation.diff
fn norm2(x: [f32; 2]) -> f32 {
// CHECK-LABEL: fn norm2(
// CHECK-NOT: Len(
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/instsimplify/combine_clone_of_primitives.rs
@@ -1,7 +1,7 @@
//@ unit-test: InstSimplify
//@ unit-test: InstSimplify-before-unreachable-propagation
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

// EMIT_MIR combine_clone_of_primitives.{impl#0}-clone.InstSimplify.diff
// EMIT_MIR combine_clone_of_primitives.{impl#0}-clone.InstSimplify-before-unreachable-propagation.diff
#[derive(Clone)]
struct MyThing<T> {
v: T,
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone` before InstSimplify
+ // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone` after InstSimplify
- // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone` before InstSimplify-before-unreachable-propagation
+ // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone` after InstSimplify-before-unreachable-propagation

fn <impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone(_1: &MyThing<T>) -> MyThing<T> {
debug self => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone` before InstSimplify
+ // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone` after InstSimplify
- // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone` before InstSimplify-before-unreachable-propagation
+ // MIR for `<impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone` after InstSimplify-before-unreachable-propagation

fn <impl at $DIR/combine_clone_of_primitives.rs:5:10: 5:15>::clone(_1: &MyThing<T>) -> MyThing<T> {
debug self => _1;
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `adt_transmutes` before InstSimplify
+ // MIR for `adt_transmutes` after InstSimplify
- // MIR for `adt_transmutes` before InstSimplify-before-unreachable-propagation
+ // MIR for `adt_transmutes` after InstSimplify-before-unreachable-propagation

fn adt_transmutes() -> () {
let mut _0: ();
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `identity_transmutes` before InstSimplify
+ // MIR for `identity_transmutes` after InstSimplify
- // MIR for `identity_transmutes` before InstSimplify-before-unreachable-propagation
+ // MIR for `identity_transmutes` after InstSimplify-before-unreachable-propagation

fn identity_transmutes() -> () {
let mut _0: ();
Expand Down
@@ -1,5 +1,5 @@
- // MIR for `integer_transmutes` before InstSimplify
+ // MIR for `integer_transmutes` after InstSimplify
- // MIR for `integer_transmutes` before InstSimplify-before-unreachable-propagation
+ // MIR for `integer_transmutes` after InstSimplify-before-unreachable-propagation

fn integer_transmutes() -> () {
let mut _0: ();
Expand Down
8 changes: 4 additions & 4 deletions tests/mir-opt/instsimplify/combine_transmutes.rs
@@ -1,4 +1,4 @@
//@ unit-test: InstSimplify
//@ unit-test: InstSimplify-before-unreachable-propagation
//@ compile-flags: -C panic=abort
#![crate_type = "lib"]
#![feature(core_intrinsics)]
Expand All @@ -8,7 +8,7 @@
use std::intrinsics::mir::*;
use std::mem::{MaybeUninit, ManuallyDrop, transmute};

// EMIT_MIR combine_transmutes.identity_transmutes.InstSimplify.diff
// EMIT_MIR combine_transmutes.identity_transmutes.InstSimplify-before-unreachable-propagation.diff
pub unsafe fn identity_transmutes() {
// CHECK-LABEL: fn identity_transmutes(
// CHECK-NOT: as i32 (Transmute);
Expand All @@ -20,7 +20,7 @@ pub unsafe fn identity_transmutes() {
}

#[custom_mir(dialect = "runtime", phase = "initial")]
// EMIT_MIR combine_transmutes.integer_transmutes.InstSimplify.diff
// EMIT_MIR combine_transmutes.integer_transmutes.InstSimplify-before-unreachable-propagation.diff
pub unsafe fn integer_transmutes() {
// CHECK-LABEL: fn integer_transmutes(
// CHECK-NOT: _i32 as u32 (Transmute);
Expand All @@ -44,7 +44,7 @@ pub unsafe fn integer_transmutes() {
}
}

// EMIT_MIR combine_transmutes.adt_transmutes.InstSimplify.diff
// EMIT_MIR combine_transmutes.adt_transmutes.InstSimplify-before-unreachable-propagation.diff
pub unsafe fn adt_transmutes() {
// CHECK-LABEL: fn adt_transmutes(
// CHECK: as u8 (Transmute);
Expand Down

0 comments on commit 66e8eb9

Please sign in to comment.