Skip to content

Commit bd27131

Browse files
davidtwcopietroalbini
authored andcommitted
Add helpful logging statements.
This commit adds logging statements to `promote_consts` and `qualify_consts` to make it easier to understand what it is doing.
1 parent d08a0a8 commit bd27131

File tree

2 files changed

+29
-1
lines changed

2 files changed

+29
-1
lines changed

src/librustc_mir/transform/promote_consts.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ pub enum TempState {
5353

5454
impl TempState {
5555
pub fn is_promotable(&self) -> bool {
56+
debug!("is_promotable: self={:?}", self);
5657
if let TempState::Defined { uses, .. } = *self {
5758
uses > 0
5859
} else {
@@ -88,6 +89,7 @@ impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
8889
&index: &Local,
8990
context: PlaceContext<'tcx>,
9091
location: Location) {
92+
debug!("visit_local: index={:?} context={:?} location={:?}", index, context, location);
9193
// We're only interested in temporaries
9294
if self.mir.local_kind(index) != LocalKind::Temp {
9395
return;
@@ -97,10 +99,15 @@ impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
9799
// then it's constant and thus drop is noop.
98100
// Storage live ranges are also irrelevant.
99101
if context.is_drop() || context.is_storage_marker() {
102+
debug!(
103+
"visit_local: context.is_drop={:?} context.is_storage_marker={:?}",
104+
context.is_drop(), context.is_storage_marker(),
105+
);
100106
return;
101107
}
102108

103109
let temp = &mut self.temps[index];
110+
debug!("visit_local: temp={:?}", temp);
104111
if *temp == TempState::Undefined {
105112
match context {
106113
PlaceContext::Store |
@@ -121,6 +128,7 @@ impl<'tcx> Visitor<'tcx> for TempCollector<'tcx> {
121128
PlaceContext::Borrow {..} => true,
122129
_ => context.is_nonmutating_use()
123130
};
131+
debug!("visit_local: allowed_use={:?}", allowed_use);
124132
if allowed_use {
125133
*uses += 1;
126134
return;

src/librustc_mir/transform/qualify_consts.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ impl<'a, 'tcx> Qualif {
8484
}
8585

8686
/// What kind of item we are in.
87-
#[derive(Copy, Clone, PartialEq, Eq)]
87+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
8888
enum Mode {
8989
Const,
9090
Static,
@@ -383,6 +383,7 @@ impl<'a, 'tcx> Qualifier<'a, 'tcx, 'tcx> {
383383
// Collect all the temps we need to promote.
384384
let mut promoted_temps = BitSet::new_empty(self.temp_promotion_state.len());
385385

386+
debug!("qualify_const: promotion_candidates={:?}", self.promotion_candidates);
386387
for candidate in &self.promotion_candidates {
387388
match *candidate {
388389
Candidate::Ref(Location { block: bb, statement_index: stmt_idx }) => {
@@ -414,6 +415,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
414415
&local: &Local,
415416
_: PlaceContext<'tcx>,
416417
_: Location) {
418+
debug!("visit_local: local={:?}", local);
417419
let kind = self.mir.local_kind(local);
418420
match kind {
419421
LocalKind::ReturnPointer => {
@@ -435,6 +437,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
435437
}
436438

437439
if !self.temp_promotion_state[local].is_promotable() {
440+
debug!("visit_local: (not promotable) local={:?}", local);
438441
self.add(Qualif::NOT_PROMOTABLE);
439442
}
440443

@@ -451,6 +454,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
451454
place: &Place<'tcx>,
452455
context: PlaceContext<'tcx>,
453456
location: Location) {
457+
debug!("visit_place: place={:?} context={:?} location={:?}", place, context, location);
454458
match *place {
455459
Place::Local(ref local) => self.visit_local(local, context, location),
456460
Place::Promoted(_) => bug!("promoting already promoted MIR"),
@@ -557,6 +561,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
557561
}
558562

559563
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
564+
debug!("visit_operand: operand={:?} location={:?}", operand, location);
560565
self.super_operand(operand, location);
561566

562567
match *operand {
@@ -591,6 +596,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
591596
}
592597

593598
fn visit_rvalue(&mut self, rvalue: &Rvalue<'tcx>, location: Location) {
599+
debug!("visit_rvalue: rvalue={:?} location={:?}", rvalue, location);
594600
// Recurse through operands and places.
595601
if let Rvalue::Ref(region, kind, ref place) = *rvalue {
596602
let mut is_reborrow = false;
@@ -696,6 +702,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
696702
}
697703
}
698704

705+
debug!("visit_rvalue: forbidden_mut={:?}", forbidden_mut);
699706
if forbidden_mut {
700707
self.add(Qualif::NOT_CONST);
701708
} else {
@@ -709,15 +716,19 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
709716
}
710717
place = &proj.base;
711718
}
719+
debug!("visit_rvalue: place={:?}", place);
712720
if let Place::Local(local) = *place {
713721
if self.mir.local_kind(local) == LocalKind::Temp {
722+
debug!("visit_rvalue: local={:?}", local);
714723
if let Some(qualif) = self.local_qualif[local] {
715724
// `forbidden_mut` is false, so we can safely ignore
716725
// `MUTABLE_INTERIOR` from the local's qualifications.
717726
// This allows borrowing fields which don't have
718727
// `MUTABLE_INTERIOR`, from a type that does, e.g.:
719728
// `let _: &'static _ = &(Cell::new(1), 2).1;`
729+
debug!("visit_rvalue: qualif={:?}", qualif);
720730
if (qualif - Qualif::MUTABLE_INTERIOR).is_empty() {
731+
debug!("visit_rvalue: candidate={:?}", candidate);
721732
self.promotion_candidates.push(candidate);
722733
}
723734
}
@@ -815,6 +826,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
815826
bb: BasicBlock,
816827
kind: &TerminatorKind<'tcx>,
817828
location: Location) {
829+
debug!("visit_terminator_kind: bb={:?} kind={:?} location={:?}", bb, kind, location);
818830
if let TerminatorKind::Call { ref func, ref args, ref destination, .. } = *kind {
819831
self.visit_operand(func, location);
820832

@@ -967,6 +979,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
967979
let candidate = Candidate::Argument { bb, index: i };
968980
if is_shuffle && i == 2 {
969981
if this.qualif.is_empty() {
982+
debug!("visit_terminator_kind: candidate={:?}", candidate);
970983
this.promotion_candidates.push(candidate);
971984
} else {
972985
span_err!(this.tcx.sess, this.span, E0526,
@@ -983,6 +996,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
983996
return
984997
}
985998
if this.qualif.is_empty() {
999+
debug!("visit_terminator_kind: candidate={:?}", candidate);
9861000
this.promotion_candidates.push(candidate);
9871001
} else {
9881002
this.tcx.sess.span_err(this.span,
@@ -1056,6 +1070,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
10561070
dest: &Place<'tcx>,
10571071
rvalue: &Rvalue<'tcx>,
10581072
location: Location) {
1073+
debug!("visit_assign: dest={:?} rvalue={:?} location={:?}", dest, rvalue, location);
10591074
self.visit_rvalue(rvalue, location);
10601075

10611076
// Check the allowed const fn argument forms.
@@ -1104,10 +1119,12 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
11041119
}
11051120

11061121
fn visit_source_info(&mut self, source_info: &SourceInfo) {
1122+
debug!("visit_source_info: source_info={:?}", source_info);
11071123
self.span = source_info.span;
11081124
}
11091125

11101126
fn visit_statement(&mut self, bb: BasicBlock, statement: &Statement<'tcx>, location: Location) {
1127+
debug!("visit_statement: bb={:?} statement={:?} location={:?}", bb, statement, location);
11111128
self.nest(|this| {
11121129
this.visit_source_info(&statement.source_info);
11131130
match statement.kind {
@@ -1131,6 +1148,7 @@ impl<'a, 'tcx> Visitor<'tcx> for Qualifier<'a, 'tcx, 'tcx> {
11311148
bb: BasicBlock,
11321149
terminator: &Terminator<'tcx>,
11331150
location: Location) {
1151+
debug!("visit_terminator: bb={:?} terminator={:?} location={:?}", bb, terminator, location);
11341152
self.nest(|this| this.super_terminator(bb, terminator, location));
11351153
}
11361154
}
@@ -1197,6 +1215,7 @@ impl MirPass for QualifyAndPromoteConstants {
11971215
hir::BodyOwnerKind::Static(hir::MutMutable) => Mode::StaticMut,
11981216
};
11991217

1218+
debug!("run_pass: mode={:?}", mode);
12001219
if mode == Mode::Fn || mode == Mode::ConstFn {
12011220
// This is ugly because Qualifier holds onto mir,
12021221
// which can't be mutated until its scope ends.
@@ -1239,6 +1258,7 @@ impl MirPass for QualifyAndPromoteConstants {
12391258
// In `const` and `static` everything without `StorageDead`
12401259
// is `'static`, we don't have to create promoted MIR fragments,
12411260
// just remove `Drop` and `StorageDead` on "promoted" locals.
1261+
debug!("run_pass: promoted_temps={:?}", promoted_temps);
12421262
for block in mir.basic_blocks_mut() {
12431263
block.statements.retain(|statement| {
12441264
match statement.kind {

0 commit comments

Comments
 (0)