rustc_mir_transform/
strip_debuginfo.rs

1use rustc_middle::mir::*;
2use rustc_middle::ty::TyCtxt;
3use rustc_session::config::MirStripDebugInfo;
4
5/// Conditionally remove some of the VarDebugInfo in MIR.
6///
7/// In particular, stripping non-parameter debug info for tiny, primitive-like
8/// methods in core saves work later, and nobody ever wanted to use it anyway.
9pub(super) struct StripDebugInfo;
10
11impl<'tcx> crate::MirPass<'tcx> for StripDebugInfo {
12    fn is_enabled(&self, sess: &rustc_session::Session) -> bool {
13        sess.opts.unstable_opts.mir_strip_debuginfo != MirStripDebugInfo::None
14    }
15
16    fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
17        match tcx.sess.opts.unstable_opts.mir_strip_debuginfo {
18            MirStripDebugInfo::None => return,
19            MirStripDebugInfo::AllLocals => {}
20            MirStripDebugInfo::LocalsInTinyFunctions
21                if let TerminatorKind::Return { .. } =
22                    body.basic_blocks[START_BLOCK].terminator().kind => {}
23            MirStripDebugInfo::LocalsInTinyFunctions => return,
24        }
25
26        body.var_debug_info.retain(|vdi| {
27            matches!(
28                vdi.value,
29                VarDebugInfoContents::Place(place)
30                    if place.local.as_usize() <= body.arg_count && place.local != RETURN_PLACE,
31            )
32        });
33    }
34
35    fn is_required(&self) -> bool {
36        true
37    }
38}