Skip to content

Fix debugger stepping for consts assigned to local vars #141349

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

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
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 @@ -194,7 +194,7 @@ declare_passes! {
Final
};
mod simplify_comparison_integral : SimplifyComparisonIntegral;
mod single_use_consts : SingleUseConsts;
mod single_use_consts : SingleUseConsts { TempOnly, Full };
mod sroa : ScalarReplacementOfAggregates;
mod strip_debuginfo : StripDebugInfo;
mod unreachable_enum_branching : UnreachableEnumBranching;
Expand Down Expand Up @@ -710,7 +710,8 @@ pub(crate) fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'
&simplify::SimplifyLocals::AfterGVN,
&match_branches::MatchBranchSimplification,
&dataflow_const_prop::DataflowConstProp,
&single_use_consts::SingleUseConsts,
&single_use_consts::SingleUseConsts::TempOnly,
&single_use_consts::SingleUseConsts::Full,
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
&jump_threading::JumpThreading,
&early_otherwise_branch::EarlyOtherwiseBranch,
Expand Down
24 changes: 21 additions & 3 deletions compiler/rustc_mir_transform/src/single_use_consts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,24 @@ 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(super) struct SingleUseConsts;
pub(super) enum SingleUseConsts {
TempOnly,
Full,
}

impl<'tcx> crate::MirPass<'tcx> for SingleUseConsts {
fn name(&self) -> &'static str {
match self {
SingleUseConsts::TempOnly => "SingleUseConsts-temp-only",
SingleUseConsts::Full => "SingleUseConsts-full",
}
}

fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
sess.mir_opt_level() > 0
match self {
SingleUseConsts::TempOnly => sess.mir_opt_level() == 1,
SingleUseConsts::Full => sess.mir_opt_level() > 1,
}
}

fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
Expand All @@ -46,6 +59,11 @@ impl<'tcx> crate::MirPass<'tcx> for SingleUseConsts {
continue;
};

let local_has_debug_info = finder.locals_in_debug_info.contains(local);
if local_has_debug_info && matches!(self, SingleUseConsts::TempOnly) {
continue;
}

// We're only changing an operand, not the terminator kinds or successors
let basic_blocks = body.basic_blocks.as_mut_preserves_cfg();
let init_statement_kind = std::mem::replace(
Expand All @@ -61,7 +79,7 @@ impl<'tcx> crate::MirPass<'tcx> for SingleUseConsts {

let mut replacer = LocalReplacer { tcx, local, operand: Some(operand) };

if finder.locals_in_debug_info.contains(local) {
if local_has_debug_info {
for var_debug_info in &mut body.var_debug_info {
replacer.visit_var_debug_info(var_debug_info);
}
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-full
+ // MIR for `main` after SingleUseConsts-full

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-full
//@ 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-full.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-temp-only
+ // MIR for `assign_const_to_return` after SingleUseConsts-temp-only

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-temp-only
+ // MIR for `assign_const_to_return` after SingleUseConsts-temp-only

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-temp-only
+ // MIR for `if_const` after SingleUseConsts-temp-only

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-temp-only
+ // MIR for `if_const` after SingleUseConsts-temp-only

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

fn if_const_debug() -> i32 {
let mut _0: i32;
let _1: bool;
let _2: ();
let mut _3: bool;
scope 1 {
- debug my_bool => _1;
+ debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
debug my_bool => _1;
}

bb0: {
StorageLive(_1);
- _1 = const <T as MyTrait>::ASSOC_BOOL;
+ nop;
_1 = const <T as MyTrait>::ASSOC_BOOL;
StorageLive(_2);
_2 = do_whatever() -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_2);
StorageLive(_3);
- _3 = copy _1;
+ _3 = const <T as MyTrait>::ASSOC_BOOL;
_3 = copy _1;
switchInt(move _3) -> [0: bb3, otherwise: bb2];
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
- // MIR for `if_const_debug` before SingleUseConsts
+ // MIR for `if_const_debug` after SingleUseConsts
- // MIR for `if_const_debug` before SingleUseConsts-temp-only
+ // MIR for `if_const_debug` after SingleUseConsts-temp-only

fn if_const_debug() -> i32 {
let mut _0: i32;
let _1: bool;
let _2: ();
let mut _3: bool;
scope 1 {
- debug my_bool => _1;
+ debug my_bool => const <T as MyTrait>::ASSOC_BOOL;
debug my_bool => _1;
}

bb0: {
StorageLive(_1);
- _1 = const <T as MyTrait>::ASSOC_BOOL;
+ nop;
_1 = const <T as MyTrait>::ASSOC_BOOL;
StorageLive(_2);
_2 = do_whatever() -> [return: bb1, unwind continue];
}

bb1: {
StorageDead(_2);
StorageLive(_3);
- _3 = copy _1;
+ _3 = const <T as MyTrait>::ASSOC_BOOL;
_3 = copy _1;
switchInt(move _3) -> [0: bb3, otherwise: bb2];
}

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-temp-only
+ // MIR for `keep_parameter` after SingleUseConsts-temp-only

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-temp-only
+ // MIR for `keep_parameter` after SingleUseConsts-temp-only

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-temp-only
+ // MIR for `match_const` after SingleUseConsts-temp-only

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-temp-only
+ // MIR for `match_const` after SingleUseConsts-temp-only

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

fn match_const_debug() -> &str {
let mut _0: &str;
let _1: i32;
let _2: ();
scope 1 {
- debug my_int => _1;
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
debug my_int => _1;
}

bb0: {
StorageLive(_1);
- _1 = const <T as MyTrait>::ASSOC_INT;
+ nop;
_1 = const <T as MyTrait>::ASSOC_INT;
StorageLive(_2);
_2 = do_whatever() -> [return: bb1, unwind unreachable];
}

bb1: {
StorageDead(_2);
- switchInt(copy _1) -> [7: bb4, 42: bb3, otherwise: bb2];
+ switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb4, 42: bb3, otherwise: bb2];
switchInt(copy _1) -> [7: bb4, 42: bb3, otherwise: bb2];
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
- // MIR for `match_const_debug` before SingleUseConsts
+ // MIR for `match_const_debug` after SingleUseConsts
- // MIR for `match_const_debug` before SingleUseConsts-temp-only
+ // MIR for `match_const_debug` after SingleUseConsts-temp-only

fn match_const_debug() -> &str {
let mut _0: &str;
let _1: i32;
let _2: ();
scope 1 {
- debug my_int => _1;
+ debug my_int => const <T as MyTrait>::ASSOC_INT;
debug my_int => _1;
}

bb0: {
StorageLive(_1);
- _1 = const <T as MyTrait>::ASSOC_INT;
+ nop;
_1 = const <T as MyTrait>::ASSOC_INT;
StorageLive(_2);
_2 = do_whatever() -> [return: bb1, unwind continue];
}

bb1: {
StorageDead(_2);
- switchInt(copy _1) -> [7: bb4, 42: bb3, otherwise: bb2];
+ switchInt(const <T as MyTrait>::ASSOC_INT) -> [7: bb4, 42: bb3, otherwise: bb2];
switchInt(copy _1) -> [7: bb4, 42: bb3, otherwise: bb2];
}

bb2: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- // MIR for `never_used_debug` before SingleUseConsts-temp-only
+ // MIR for `never_used_debug` after SingleUseConsts-temp-only

fn never_used_debug() -> () {
let mut _0: ();
let _1: i32;
scope 1 {
debug my_int => _1;
}

bb0: {
StorageLive(_1);
_1 = const <T as MyTrait>::ASSOC_INT;
_0 = const ();
StorageDead(_1);
return;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
- // MIR for `never_used_debug` before SingleUseConsts-temp-only
+ // MIR for `never_used_debug` after SingleUseConsts-temp-only

fn never_used_debug() -> () {
let mut _0: ();
let _1: i32;
scope 1 {
debug my_int => _1;
}

bb0: {
StorageLive(_1);
_1 = const <T as MyTrait>::ASSOC_INT;
_0 = const ();
StorageDead(_1);
return;
}
}

This file was deleted.

This file was deleted.

Loading
Loading