Skip to content

Commit

Permalink
Only account for debuginfo if the user requests it.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Apr 21, 2024
1 parent 8cf135e commit 107bd38
Show file tree
Hide file tree
Showing 33 changed files with 243 additions and 174 deletions.
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::mir::visit::Visitor;
use rustc_middle::mir::*;
Expand All @@ -31,10 +32,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 @@ -11,14 +11,14 @@ use rustc_middle::mir::visit::*;
use rustc_middle::mir::*;
use rustc_middle::ty::TypeVisitableExt;
use rustc_middle::ty::{self, Instance, InstanceDef, ParamEnv, Ty, TyCtxt};
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 std::iter;
use std::ops::{Range, RangeFrom};
Expand Down Expand Up @@ -699,14 +699,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
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,
}
})
}
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"]
#![feature(generic_nonzero)]

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
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
39 changes: 12 additions & 27 deletions tests/mir-opt/dead-store-elimination/cycle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,43 +4,28 @@
//@ needs-unwind
//@ unit-test: DeadStoreElimination-initial

#![feature(core_intrinsics, custom_mir)]
use std::intrinsics::mir::*;

#[inline(never)]
fn cond() -> bool {
false
}

// EMIT_MIR cycle.cycle.DeadStoreElimination-initial.diff
#[custom_mir(dialect = "runtime", phase = "post-cleanup")]
fn cycle(mut x: i32, mut y: i32, mut z: i32) {
// CHECK-LABEL: fn cycle(
// CHECK-NOT: {{_.*}} = {{_.*}};
// CHECK-NOT: {{_.*}} = move {{_.*}};

// We use custom MIR to avoid generating debuginfo, that would force to preserve writes.
mir!(
let condition: bool;
{
Call(condition = cond(), ReturnTo(bb1), UnwindContinue())
}
bb1 = {
match condition { true => bb2, _ => ret }
}
bb2 = {
let temp = z;
z = y;
y = x;
x = temp;
Call(condition = cond(), ReturnTo(bb1), UnwindContinue())
}
ret = {
Return()
}
)
// CHECK-NOT: {{_.*}} =
// CHECK: {{_.*}} = cond()
// CHECK-NOT: {{_.*}} =
// CHECK: _0 = const ();
// CHECK-NOT: {{_.*}} =
while cond() {
let temp = z;
z = y;
y = x;
x = temp;
}
}

fn main() {
// CHECK-LABEL: fn main(
cycle(1, 2, 3);
}
1 change: 0 additions & 1 deletion tests/mir-opt/issue_101973.inner.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
StorageLive(_4);
StorageLive(_5);
_5 = _1;
nop;
- StorageLive(_14);
- _14 = BitAnd(_5, const 255_u32);
- _4 = BitOr(const 0_u32, move _14);
Expand Down
1 change: 0 additions & 1 deletion tests/mir-opt/issue_101973.inner.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
StorageLive(_4);
StorageLive(_5);
_5 = _1;
nop;
- StorageLive(_14);
- _14 = BitAnd(_5, const 255_u32);
- _4 = BitOr(const 0_u32, move _14);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@

bb2: {
StorageLive(_7);
_7 = &(*_2)[0 of 3];
StorageLive(_8);
_8 = &(*_2)[1 of 3];
StorageLive(_9);
_9 = &(*_2)[2 of 3];
StorageDead(_9);
StorageDead(_8);
StorageDead(_7);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,8 @@

bb2: {
StorageLive(_7);
_7 = &(*_2)[0 of 3];
StorageLive(_8);
_8 = &(*_2)[1 of 3];
StorageLive(_9);
_9 = &(*_2)[2 of 3];
StorageDead(_9);
StorageDead(_8);
StorageDead(_7);
Expand Down
Loading

0 comments on commit 107bd38

Please sign in to comment.