Skip to content

Commit

Permalink
Run SingleUseConsts before DeadStoreElimination::Initial
Browse files Browse the repository at this point in the history
  • Loading branch information
saethlin committed Jul 21, 2024
1 parent 89d3f11 commit 19c27fe
Show file tree
Hide file tree
Showing 19 changed files with 53 additions and 42 deletions.
3 changes: 2 additions & 1 deletion compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,7 +597,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&gvn::GVN,
&simplify::SimplifyLocals::AfterGVN,
&dataflow_const_prop::DataflowConstProp,
&single_use_consts::SingleUseConsts,
&single_use_consts::SingleUseConsts::Initial,
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
&jump_threading::JumpThreading,
&early_otherwise_branch::EarlyOtherwiseBranch,
Expand All @@ -607,6 +607,7 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&o1(remove_noop_landing_pads::RemoveNoopLandingPads),
&o1(simplify::SimplifyCfg::Final),
&copy_prop::CopyProp,
&single_use_consts::SingleUseConsts::Final,
&dead_store_elimination::DeadStoreElimination::Final,
&nrvo::RenameReturnPlace,
&simplify::SimplifyLocals::Final,
Expand Down
12 changes: 11 additions & 1 deletion compiler/rustc_mir_transform/src/single_use_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,23 @@ use rustc_middle::ty::TyCtxt;
///
/// It also removes *never*-used constants, since it had all the information
/// needed to do that too, including updating the debug info.
pub struct SingleUseConsts;
pub enum SingleUseConsts {
Initial,
Final,
}

impl<'tcx> MirPass<'tcx> for SingleUseConsts {
fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
}

fn name(&self) -> &'static str {
match self {
SingleUseConsts::Initial => "SingleUseConsts-initial",
SingleUseConsts::Final => "SingleUseConsts-final",
}
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
let mut finder = SingleUseConstsFinder {
ineligible_locals: BitSet::new_empty(body.local_decls.len()),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `main` before SingleUseConsts
+ // MIR for `main` after SingleUseConsts
- // MIR for `main` before SingleUseConsts-final
+ // MIR for `main` after SingleUseConsts-final

fn main() -> () {
let mut _0: ();
Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/const_debuginfo.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ test-mir-pass: SingleUseConsts
//@ test-mir-pass: SingleUseConsts-final
//@ compile-flags: -C overflow-checks=no -Zmir-enable-passes=+GVN -Zdump-mir-exclude-alloc-bytes

#![allow(unused)]
Expand All @@ -8,7 +8,7 @@ struct Point {
y: u32,
}

// EMIT_MIR const_debuginfo.main.SingleUseConsts.diff
// EMIT_MIR const_debuginfo.main.SingleUseConsts-final.diff
fn main() {
// CHECK-LABEL: fn main(
// CHECK: debug x => const 1_u8;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `assign_const_to_return` before SingleUseConsts
+ // MIR for `assign_const_to_return` after SingleUseConsts
- // MIR for `assign_const_to_return` before SingleUseConsts-initial
+ // MIR for `assign_const_to_return` after SingleUseConsts-initial

fn assign_const_to_return() -> bool {
let mut _0: bool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `assign_const_to_return` before SingleUseConsts
+ // MIR for `assign_const_to_return` after SingleUseConsts
- // MIR for `assign_const_to_return` before SingleUseConsts-initial
+ // MIR for `assign_const_to_return` after SingleUseConsts-initial

fn assign_const_to_return() -> bool {
let mut _0: bool;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `if_const` before SingleUseConsts
+ // MIR for `if_const` after SingleUseConsts
- // MIR for `if_const` before SingleUseConsts-initial
+ // MIR for `if_const` after SingleUseConsts-initial

fn if_const() -> i32 {
let mut _0: i32;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `if_const` before SingleUseConsts
+ // MIR for `if_const` after SingleUseConsts
- // MIR for `if_const` before SingleUseConsts-initial
+ // MIR for `if_const` after SingleUseConsts-initial

fn if_const() -> i32 {
let mut _0: i32;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `if_const_debug` before SingleUseConsts
+ // MIR for `if_const_debug` after SingleUseConsts
- // MIR for `if_const_debug` before SingleUseConsts-initial
+ // MIR for `if_const_debug` after SingleUseConsts-initial

fn if_const_debug() -> i32 {
let mut _0: i32;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `if_const_debug` before SingleUseConsts
+ // MIR for `if_const_debug` after SingleUseConsts
- // MIR for `if_const_debug` before SingleUseConsts-initial
+ // MIR for `if_const_debug` after SingleUseConsts-initial

fn if_const_debug() -> i32 {
let mut _0: i32;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `keep_parameter` before SingleUseConsts
+ // MIR for `keep_parameter` after SingleUseConsts
- // MIR for `keep_parameter` before SingleUseConsts-initial
+ // MIR for `keep_parameter` after SingleUseConsts-initial

fn keep_parameter(_1: i32) -> () {
debug other => _1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `keep_parameter` before SingleUseConsts
+ // MIR for `keep_parameter` after SingleUseConsts
- // MIR for `keep_parameter` before SingleUseConsts-initial
+ // MIR for `keep_parameter` after SingleUseConsts-initial

fn keep_parameter(_1: i32) -> () {
debug other => _1;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `match_const` before SingleUseConsts
+ // MIR for `match_const` after SingleUseConsts
- // MIR for `match_const` before SingleUseConsts-initial
+ // MIR for `match_const` after SingleUseConsts-initial

fn match_const() -> &str {
let mut _0: &str;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `match_const` before SingleUseConsts
+ // MIR for `match_const` after SingleUseConsts
- // MIR for `match_const` before SingleUseConsts-initial
+ // MIR for `match_const` after SingleUseConsts-initial

fn match_const() -> &str {
let mut _0: &str;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `match_const_debug` before SingleUseConsts
+ // MIR for `match_const_debug` after SingleUseConsts
- // MIR for `match_const_debug` before SingleUseConsts-initial
+ // MIR for `match_const_debug` after SingleUseConsts-initial

fn match_const_debug() -> &str {
let mut _0: &str;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `match_const_debug` before SingleUseConsts
+ // MIR for `match_const_debug` after SingleUseConsts
- // MIR for `match_const_debug` before SingleUseConsts-initial
+ // MIR for `match_const_debug` after SingleUseConsts-initial

fn match_const_debug() -> &str {
let mut _0: &str;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `never_used_debug` before SingleUseConsts
+ // MIR for `never_used_debug` after SingleUseConsts
- // MIR for `never_used_debug` before SingleUseConsts-initial
+ // MIR for `never_used_debug` after SingleUseConsts-initial

fn never_used_debug() -> () {
let mut _0: ();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- // MIR for `never_used_debug` before SingleUseConsts
+ // MIR for `never_used_debug` after SingleUseConsts
- // MIR for `never_used_debug` before SingleUseConsts-initial
+ // MIR for `never_used_debug` after SingleUseConsts-initial

fn never_used_debug() -> () {
let mut _0: ();
Expand Down
16 changes: 8 additions & 8 deletions tests/mir-opt/single_use_consts.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ test-mir-pass: SingleUseConsts
//@ test-mir-pass: SingleUseConsts-initial
//@ compile-flags: -C debuginfo=full
// EMIT_MIR_FOR_EACH_PANIC_STRATEGY

Expand All @@ -7,14 +7,14 @@ trait MyTrait {
const ASSOC_INT: i32;
}

// EMIT_MIR single_use_consts.if_const.SingleUseConsts.diff
// EMIT_MIR single_use_consts.if_const.SingleUseConsts-initial.diff
fn if_const<T: MyTrait>() -> i32 {
// CHECK-LABEL: fn if_const(
// CHECK: switchInt(const <T as MyTrait>::ASSOC_BOOL)
if T::ASSOC_BOOL { 7 } else { 42 }
}

// EMIT_MIR single_use_consts.match_const.SingleUseConsts.diff
// EMIT_MIR single_use_consts.match_const.SingleUseConsts-initial.diff
fn match_const<T: MyTrait>() -> &'static str {
// CHECK-LABEL: fn match_const(
// CHECK: switchInt(const <T as MyTrait>::ASSOC_INT)
Expand All @@ -25,7 +25,7 @@ fn match_const<T: MyTrait>() -> &'static str {
}
}

// EMIT_MIR single_use_consts.if_const_debug.SingleUseConsts.diff
// EMIT_MIR single_use_consts.if_const_debug.SingleUseConsts-initial.diff
fn if_const_debug<T: MyTrait>() -> i32 {
// CHECK-LABEL: fn if_const_debug(
// CHECK: my_bool => const <T as MyTrait>::ASSOC_BOOL;
Expand All @@ -37,7 +37,7 @@ fn if_const_debug<T: MyTrait>() -> i32 {
if my_bool { 7 } else { 42 }
}

// EMIT_MIR single_use_consts.match_const_debug.SingleUseConsts.diff
// EMIT_MIR single_use_consts.match_const_debug.SingleUseConsts-initial.diff
fn match_const_debug<T: MyTrait>() -> &'static str {
// CHECK-LABEL: fn match_const_debug(
// CHECK: my_int => const <T as MyTrait>::ASSOC_INT;
Expand All @@ -51,7 +51,7 @@ fn match_const_debug<T: MyTrait>() -> &'static str {
}
}

// EMIT_MIR single_use_consts.never_used_debug.SingleUseConsts.diff
// EMIT_MIR single_use_consts.never_used_debug.SingleUseConsts-initial.diff
#[allow(unused_variables)]
fn never_used_debug<T: MyTrait>() {
// CHECK-LABEL: fn never_used_debug(
Expand All @@ -62,14 +62,14 @@ fn never_used_debug<T: MyTrait>() {
let my_int = T::ASSOC_INT;
}

// EMIT_MIR single_use_consts.assign_const_to_return.SingleUseConsts.diff
// EMIT_MIR single_use_consts.assign_const_to_return.SingleUseConsts-initial.diff
fn assign_const_to_return<T: MyTrait>() -> bool {
// CHECK-LABEL: fn assign_const_to_return(
// CHECK: _0 = const <T as MyTrait>::ASSOC_BOOL;
T::ASSOC_BOOL
}

// EMIT_MIR single_use_consts.keep_parameter.SingleUseConsts.diff
// EMIT_MIR single_use_consts.keep_parameter.SingleUseConsts-initial.diff
fn keep_parameter<T: MyTrait>(mut other: i32) {
// CHECK-LABEL: fn keep_parameter(
// CHECK: _1 = const <T as MyTrait>::ASSOC_INT;
Expand Down

0 comments on commit 19c27fe

Please sign in to comment.