Skip to content

Commit 7e704af

Browse files
committed
Add some useful comments.
Describing some things that took me a long time to understand.
1 parent 0066acf commit 7e704af

File tree

5 files changed

+24
-12
lines changed

5 files changed

+24
-12
lines changed

compiler/rustc_mir_dataflow/src/framework/cursor.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,16 @@ use rustc_middle::mir::{self, BasicBlock, Location};
88

99
use super::{Analysis, Direction, Effect, EffectIndex, Results};
1010

11-
/// Allows random access inspection of the results of a dataflow analysis.
11+
/// Allows random access inspection of the results of a dataflow analysis. Use this when you want
12+
/// to inspect domain values only in certain locations; use `ResultsVisitor` if you want to inspect
13+
/// domain values in many or all locations.
1214
///
13-
/// This cursor only has linear performance within a basic block when its statements are visited in
14-
/// the same order as the `DIRECTION` of the analysis. In the worst case—when statements are
15-
/// visited in *reverse* order—performance will be quadratic in the number of statements in the
16-
/// block. The order in which basic blocks are inspected has no impact on performance.
15+
/// Because `Results` only has domain values for the entry of each basic block, these inspections
16+
/// involve some amount of domain value recomputations. This cursor only has linear performance
17+
/// within a basic block when its statements are visited in the same order as the `DIRECTION` of
18+
/// the analysis. In the worst case—when statements are visited in *reverse* order—performance will
19+
/// be quadratic in the number of statements in the block. The order in which basic blocks are
20+
/// inspected has no impact on performance.
1721
pub struct ResultsCursor<'mir, 'tcx, A>
1822
where
1923
A: Analysis<'tcx>,

compiler/rustc_mir_dataflow/src/framework/direction.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ use super::{Analysis, Effect, EffectIndex, Results, SwitchIntTarget};
99

1010
pub trait Direction {
1111
const IS_FORWARD: bool;
12-
1312
const IS_BACKWARD: bool = !Self::IS_FORWARD;
1413

14+
/// Called by `iterate_to_fixpoint` during initial analysis computation.
1515
fn apply_effects_in_block<'mir, 'tcx, A>(
1616
analysis: &mut A,
1717
body: &mir::Body<'tcx>,
@@ -22,7 +22,8 @@ pub trait Direction {
2222
) where
2323
A: Analysis<'tcx>;
2424

25-
/// Applies all effects between the given `EffectIndex`s.
25+
/// Called by `ResultsCursor` to recompute the domain value for a location
26+
/// in a basic block. Applies all effects between the given `EffectIndex`s.
2627
///
2728
/// `effects.start()` must precede or equal `effects.end()` in this direction.
2829
fn apply_effects_in_range<'tcx, A>(
@@ -34,6 +35,9 @@ pub trait Direction {
3435
) where
3536
A: Analysis<'tcx>;
3637

38+
/// Called by `ResultsVisitor` to recompute the analysis domain values for
39+
/// all locations in a basic block (starting from the entry value stored
40+
/// in `Results`) and to visit them with `vis`.
3741
fn visit_results_in_block<'mir, 'tcx, A>(
3842
state: &mut A::Domain,
3943
block: BasicBlock,
@@ -222,7 +226,6 @@ impl Direction for Backward {
222226

223227
vis.visit_block_end(state);
224228

225-
// Terminator
226229
let loc = Location { block, statement_index: block_data.statements.len() };
227230
let term = block_data.terminator();
228231
results.analysis.apply_before_terminator_effect(state, term, loc);

compiler/rustc_mir_dataflow/src/framework/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
//! The `impls` module contains several examples of dataflow analyses.
99
//!
1010
//! Then call `iterate_to_fixpoint` on your type that impls `Analysis` to get a `Results`. From
11-
//! there, you can use a `ResultsCursor` to inspect the fixpoint solution to your dataflow problem,
12-
//! or implement the `ResultsVisitor` interface and use `visit_results`. The following example uses
11+
//! there, you can use a `ResultsCursor` to inspect the fixpoint solution to your dataflow problem
12+
//! (good for inspecting a small number of locations), or implement the `ResultsVisitor` interface
13+
//! and use `visit_results` (good for inspecting many or all locations). The following example uses
1314
//! the `ResultsCursor` approach.
1415
//!
1516
//! ```ignore (cross-crate-imports)

compiler/rustc_mir_dataflow/src/framework/results.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@ use crate::errors::{
2020

2121
pub type EntrySets<'tcx, A> = IndexVec<BasicBlock, <A as Analysis<'tcx>>::Domain>;
2222

23-
/// A dataflow analysis that has converged to fixpoint.
23+
/// A dataflow analysis that has converged to fixpoint. It only holds the domain values at the
24+
/// entry of each basic block. Domain values in other parts of the block are recomputed on the fly
25+
/// by visitors (i.e. `ResultsCursor`, or `ResultsVisitor` impls).
2426
#[derive(Clone)]
2527
pub struct Results<'tcx, A>
2628
where

compiler/rustc_mir_dataflow/src/framework/visitor.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ pub fn visit_results<'mir, 'tcx, A>(
2626
}
2727
}
2828

29-
/// A visitor over the results of an `Analysis`.
29+
/// A visitor over the results of an `Analysis`. Use this when you want to inspect domain values in
30+
/// many or all locations; use `ResultsCursor` if you want to inspect domain values only in certain
31+
/// locations.
3032
pub trait ResultsVisitor<'mir, 'tcx, A>
3133
where
3234
A: Analysis<'tcx>,

0 commit comments

Comments
 (0)