Skip to content

Commit aef5074

Browse files
committed
Add a load of debug logging around ConstProp
1 parent b1a6cf4 commit aef5074

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+3
Original file line numberDiff line numberDiff line change
@@ -623,13 +623,15 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
623623
///
624624
/// This is public because it is used by [priroda](https://github.com/oli-obk/priroda) to get an
625625
/// OpTy from a local.
626+
#[instrument(skip(self, layout, frame))]
626627
pub fn local_to_op(
627628
&self,
628629
frame: &Frame<'mir, 'tcx, M::Provenance, M::FrameExtra>,
629630
local: mir::Local,
630631
layout: Option<TyAndLayout<'tcx>>,
631632
) -> InterpResult<'tcx, OpTy<'tcx, M::Provenance>> {
632633
let layout = self.layout_of_local(frame, local, layout)?;
634+
trace!(?frame.locals, "here's the locals");
633635
let op = *frame.locals[local].access()?;
634636
if matches!(op, Operand::Immediate(_)) {
635637
if layout.is_unsized() {
@@ -638,6 +640,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
638640
throw_inval!(ConstPropNonsense);
639641
}
640642
}
643+
trace!(?op, "the op");
641644
Ok(OpTy { op, layout })
642645
}
643646

compiler/rustc_mir_transform/src/const_prop.rs

+31-5
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
360360
ConstPropagator { ecx, tcx, param_env, local_decls: &body.local_decls, patch }
361361
}
362362

363+
#[instrument(skip(self), level = "debug")]
363364
fn get_const(&self, place: Place<'tcx>) -> Option<OpTy<'tcx>> {
364365
let op = match self.ecx.eval_place_to_op(place, None) {
365366
Ok(op) => {
@@ -381,10 +382,14 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
381382

382383
// Try to read the local as an immediate so that if it is representable as a scalar, we can
383384
// handle it as such, but otherwise, just return the value as is.
384-
Some(match self.ecx.read_immediate_raw(&op) {
385+
let r = Some(match self.ecx.read_immediate_raw(&op) {
385386
Ok(Right(imm)) => imm.into(),
386387
_ => op,
387-
})
388+
});
389+
390+
trace!("found = {r:?}");
391+
392+
r
388393
}
389394

390395
/// Remove `local` from the pool of `Locals`. Allows writing to them,
@@ -394,6 +399,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
394399
ecx.machine.written_only_inside_own_block_locals.remove(&local);
395400
}
396401

402+
#[instrument(skip(self), level = "debug")]
397403
fn check_rvalue(&mut self, rvalue: &Rvalue<'tcx>) -> Option<()> {
398404
// Perform any special handling for specific Rvalue types.
399405
// Generally, checks here fall into one of two categories:
@@ -509,6 +515,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
509515
}
510516
}
511517

518+
#[instrument(skip(self), level = "debug")]
512519
fn replace_with_const(&mut self, place: Place<'tcx>) -> Option<Const<'tcx>> {
513520
// This will return None if the above `const_prop` invocation only "wrote" a
514521
// type whose creation requires no write. E.g. a coroutine whose initial state
@@ -611,6 +618,7 @@ impl CanConstProp {
611618
}
612619

613620
impl<'tcx> Visitor<'tcx> for CanConstProp {
621+
#[instrument(skip(self), level = "debug")]
614622
fn visit_place(&mut self, place: &Place<'tcx>, mut context: PlaceContext, loc: Location) {
615623
use rustc_middle::mir::visit::PlaceContext::*;
616624

@@ -623,6 +631,7 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
623631
self.visit_projection(place.as_ref(), context, loc);
624632
}
625633

634+
#[instrument(skip(self), level = "debug")]
626635
fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) {
627636
use rustc_middle::mir::visit::PlaceContext::*;
628637
match context {
@@ -681,15 +690,22 @@ impl<'tcx> Visitor<'tcx> for CanConstProp {
681690
}
682691

683692
impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
693+
#[instrument(skip(self), level = "debug")]
684694
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
685695
self.super_operand(operand, location);
696+
697+
trace!("about to do it");
698+
686699
if let Some(place) = operand.place()
687700
&& let Some(value) = self.replace_with_const(place)
688701
{
702+
trace!(?place, ?value, "know whats going on");
689703
self.patch.before_effect.insert((location, place), value);
690704
}
691705
}
692706

707+
#[instrument(skip(self), level = "debug")]
708+
693709
fn visit_projection_elem(
694710
&mut self,
695711
_: PlaceRef<'tcx>,
@@ -704,6 +720,7 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
704720
}
705721
}
706722

723+
#[instrument(skip(self), level = "debug")]
707724
fn visit_assign(&mut self, place: &Place<'tcx>, rvalue: &Rvalue<'tcx>, location: Location) {
708725
self.super_assign(place, rvalue, location);
709726

@@ -714,14 +731,14 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
714731
_ if place.is_indirect() => {}
715732
ConstPropMode::NoPropagation => self.ensure_not_propagated(place.local),
716733
ConstPropMode::OnlyInsideOwnBlock | ConstPropMode::FullConstProp => {
734+
trace!("trying to do some const-prop");
717735
if let Some(()) = self.eval_rvalue_with_identities(rvalue, *place) {
718736
// If this was already an evaluated constant, keep it.
719737
if let Rvalue::Use(Operand::Constant(c)) = rvalue
720738
&& let Const::Val(..) = c.const_
721739
{
722740
trace!(
723-
"skipping replace of Rvalue::Use({:?} because it is already a const",
724-
c
741+
"skipping replace of Rvalue::Use({c:?}) because it is already a const"
725742
);
726743
} else if let Some(operand) = self.replace_with_const(*place) {
727744
self.patch.assignments.insert(location, operand);
@@ -748,8 +765,12 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
748765
}
749766
}
750767

768+
#[instrument(skip(self), level = "trace")]
751769
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
752-
trace!("visit_statement: {:?}", statement);
770+
{
771+
let frame = &self.ecx.frame().locals;
772+
trace!(?frame, "initial frame");
773+
}
753774

754775
// We want to evaluate operands before any change to the assigned-to value,
755776
// so we recurse first.
@@ -790,6 +811,11 @@ impl<'tcx> Visitor<'tcx> for ConstPropagator<'_, 'tcx> {
790811
// In both cases, this does not matter, as those reads would be UB anyway.
791812
_ => {}
792813
}
814+
815+
{
816+
let frame = &self.ecx.frame().locals;
817+
trace!(?frame, "final frame");
818+
}
793819
}
794820

795821
fn visit_basic_block_data(&mut self, block: BasicBlock, data: &BasicBlockData<'tcx>) {

0 commit comments

Comments
 (0)