Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run InstSimplify before UnreachablePropagation #123256

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Run InstSimplify before UnreachablePropagation
  • Loading branch information
saethlin committed Mar 31, 2024
commit 1d4fa84a17a6d13749353703b6d96855e3c00875
67 changes: 42 additions & 25 deletions compiler/rustc_mir_transform/src/instsimplify.rs
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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/incremental/hashes/call_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ pub fn change_to_ufcs() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir,typeck")]
#[rustc_clean(cfg="cfail6")]
Expand Down
2 changes: 1 addition & 1 deletion tests/mir-opt/const_prop/slice_len.rs
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Loading
Loading