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

Only preserve DebugInfo in DeadStoreElimination if requested. #106852

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
15 changes: 11 additions & 4 deletions compiler/rustc_mir_transform/src/dead_store_elimination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
//! will still not cause any further changes.
//!

use crate::simplify::preserve_debug_even_if_never_generated;
use crate::util::is_within_packed;
use rustc_middle::bug;
use rustc_middle::mir::visit::Visitor;
Expand All @@ -32,10 +33,16 @@ pub fn eliminate<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {

// If the user requests complete debuginfo, mark the locals that appear in it as live, so
// we don't remove assignements to them.
let mut always_live = debuginfo_locals(body);
always_live.union(&borrowed_locals);

let mut live = MaybeTransitiveLiveLocals::new(&always_live)
let mut always_live;
let always_live = if preserve_debug_even_if_never_generated(tcx) {
always_live = debuginfo_locals(body);
always_live.union(&borrowed_locals);
&always_live
} else {
&borrowed_locals
};

let mut live = MaybeTransitiveLiveLocals::new(always_live)
.into_engine(tcx, body)
.iterate_to_fixpoint()
.into_results_cursor(body);
Expand Down
13 changes: 3 additions & 10 deletions compiler/rustc_mir_transform/src/inline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::TypeVisitableExt;
use rustc_middle::ty::{self, Instance, InstanceKind, ParamEnv, Ty, TyCtxt, TypeFlags};
use rustc_session::config::{DebugInfo, OptLevel};
use rustc_session::config::OptLevel;
use rustc_span::source_map::Spanned;
use rustc_span::sym;
use rustc_target::abi::FieldIdx;
use rustc_target::spec::abi::Abi;

use crate::cost_checker::CostChecker;
use crate::simplify::simplify_cfg;
use crate::simplify::{preserve_debug_even_if_never_generated, simplify_cfg};
use crate::util;
use crate::validate::validate_types;
use std::iter;
Expand Down Expand Up @@ -719,14 +719,7 @@ impl<'tcx> Inliner<'tcx> {
// Insert all of the (mapped) parts of the callee body into the caller.
caller_body.local_decls.extend(callee_body.drain_vars_and_temps());
caller_body.source_scopes.extend(&mut callee_body.source_scopes.drain(..));
if self
.tcx
.sess
.opts
.unstable_opts
.inline_mir_preserve_debug
.unwrap_or(self.tcx.sess.opts.debuginfo != DebugInfo::None)
{
if preserve_debug_even_if_never_generated(self.tcx) {
// Note that we need to preserve these in the standard library so that
// people working on rust can build with or without debuginfo while
// still getting consistent results from the mir-opt tests.
Expand Down
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
10 changes: 10 additions & 0 deletions compiler/rustc_mir_transform/src/simplify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ use rustc_index::{Idx, IndexSlice, IndexVec};
use rustc_middle::mir::visit::{MutVisitor, MutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::TyCtxt;
use rustc_session::config::DebugInfo;
use smallvec::SmallVec;

pub enum SimplifyCfg {
Expand Down Expand Up @@ -581,3 +582,12 @@ impl<'tcx> MutVisitor<'tcx> for LocalUpdater<'tcx> {
*l = self.map[*l].unwrap();
}
}

pub(crate) fn preserve_debug_even_if_never_generated(tcx: TyCtxt<'_>) -> bool {
tcx.sess.opts.unstable_opts.inline_mir_preserve_debug.unwrap_or_else(|| {
match tcx.sess.opts.debuginfo {
DebugInfo::None | DebugInfo::LineDirectivesOnly | DebugInfo::LineTablesOnly => false,
DebugInfo::Limited | DebugInfo::Full => true,
}
})
}
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
2 changes: 1 addition & 1 deletion tests/codegen/slice-ref-equality.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//@ compile-flags: -O -Zmerge-functions=disabled
//@ compile-flags: -O -Zmerge-functions=disabled -Cdebuginfo=0
#![crate_type = "lib"]

use std::num::NonZero;
Expand Down
1 change: 1 addition & 0 deletions tests/crashes/101962.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ known-bug: #101962
//@ compile-flags: -Cdebuginfo=2

#![feature(core_intrinsics)]

Expand Down
1 change: 1 addition & 0 deletions tests/crashes/79409.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
//@ known-bug: #79409
//@ compile-flags: -Cdebuginfo=2

#![feature(extern_types)]
#![feature(unsized_locals)]
Expand Down
4 changes: 2 additions & 2 deletions tests/incremental/hashes/enum_constructors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,9 +318,9 @@ pub fn change_constructor_variant_c_like() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,optimized_mir")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,optimized_mir")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail6")]
pub fn change_constructor_variant_c_like() {
let _x = Clike::C;
Expand Down
6 changes: 3 additions & 3 deletions tests/incremental/hashes/for_loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub fn change_loop_body() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail6")]
pub fn change_loop_body() {
let mut _x = 0;
Expand Down Expand Up @@ -180,7 +180,7 @@ pub fn add_loop_label_to_break() {
#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail6")]
pub fn add_loop_label_to_break() {
let mut _x = 0;
Expand Down
12 changes: 6 additions & 6 deletions tests/incremental/hashes/let_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ pub fn change_mutability_of_slot() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck,optimized_mir")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,typeck,optimized_mir")]
#[rustc_clean(cfg="cfail6")]
Expand Down Expand Up @@ -176,7 +176,7 @@ pub fn change_mutability_of_binding_in_pattern() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck,optimized_mir")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,typeck,optimized_mir")]
#[rustc_clean(cfg="cfail6")]
Expand All @@ -193,9 +193,9 @@ pub fn add_initializer() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck,optimized_mir")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes,typeck")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,typeck,optimized_mir")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes,typeck")]
#[rustc_clean(cfg="cfail6")]
pub fn add_initializer() {
let _x: i16 = 3i16;
Expand All @@ -210,9 +210,9 @@ pub fn change_initializer() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail6")]
pub fn change_initializer() {
let _x = 5u16;
Expand Down
4 changes: 2 additions & 2 deletions tests/incremental/hashes/loop_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub fn change_loop_body() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail6")]
pub fn change_loop_body() {
let mut _x = 0;
Expand Down
2 changes: 1 addition & 1 deletion tests/incremental/hashes/match_expressions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ pub fn change_mutability_of_binding_in_pattern(x: u32) -> u32 {

// Ignore optimized_mir in cfail2, the only change to optimized MIR is a span.
#[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
10 changes: 5 additions & 5 deletions tests/incremental/hashes/while_loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ pub fn change_loop_body() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail6")]
pub fn change_loop_body() {
let mut _x = 0;
Expand All @@ -53,9 +53,9 @@ pub fn change_loop_condition() {
}

#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail6")]
pub fn change_loop_condition() {
let mut _x = 0;
Expand Down Expand Up @@ -211,7 +211,7 @@ pub fn change_continue_label() {
#[cfg(not(any(cfail1,cfail4)))]
#[rustc_clean(cfg="cfail2", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail3")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes")]
#[rustc_clean(cfg="cfail5", except="opt_hir_owner_nodes, optimized_mir")]
#[rustc_clean(cfg="cfail6")]
pub fn change_continue_label() {
let mut _x = 0;
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
Expand Up @@ -2,31 +2,70 @@
+ // MIR for `cycle` after DeadStoreElimination-initial

fn cycle(_1: i32, _2: i32, _3: i32) -> () {
debug x => _1;
debug y => _2;
debug z => _3;
let mut _0: ();
let mut _4: bool;
let mut _5: i32;
let mut _4: ();
let mut _5: bool;
let _6: i32;
let mut _7: i32;
let mut _8: i32;
let mut _9: i32;
let mut _10: !;
let _11: ();
let mut _12: !;
scope 1 {
debug temp => _6;
}

bb0: {
_4 = cond() -> [return: bb1, unwind continue];
goto -> bb1;
}

bb1: {
switchInt(_4) -> [1: bb2, otherwise: bb3];
StorageLive(_5);
_5 = cond() -> [return: bb2, unwind continue];
}

bb2: {
- _5 = _3;
- _3 = _2;
- _2 = _1;
- _1 = _5;
switchInt(move _5) -> [0: bb4, otherwise: bb3];
}

bb3: {
StorageLive(_6);
- _6 = _3;
+ nop;
StorageLive(_7);
- _7 = _2;
- _3 = move _7;
+ nop;
+ nop;
StorageDead(_7);
StorageLive(_8);
- _8 = _1;
- _2 = move _8;
+ nop;
+ nop;
_4 = cond() -> [return: bb1, unwind continue];
StorageDead(_8);
StorageLive(_9);
- _9 = _6;
- _1 = move _9;
+ nop;
+ nop;
StorageDead(_9);
- _4 = const ();
+ nop;
StorageDead(_6);
StorageDead(_5);
goto -> bb1;
}

bb3: {
bb4: {
StorageLive(_11);
_0 = const ();
StorageDead(_11);
StorageDead(_5);
return;
}
}
Expand Down
Loading
Loading