Skip to content

Commit 2727f10

Browse files
Improve docs for new framework
1 parent 47dce1b commit 2727f10

File tree

1 file changed

+35
-3
lines changed
  • src/librustc_mir/dataflow/generic

1 file changed

+35
-3
lines changed

src/librustc_mir/dataflow/generic/mod.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,36 @@
1-
//! A framework for expressing dataflow problems.
1+
//! A framework that can express both [gen-kill] and generic dataflow problems.
2+
//!
3+
//! There is another interface for dataflow in the compiler in `librustc_mir/dataflow/mod.rs`. The
4+
//! interface in this module will eventually [replace that one][design-meeting].
5+
//!
6+
//! To actually use this framework, you must implement either the `Analysis` or the
7+
//! `GenKillAnalysis` trait. If your transfer function can be expressed with only gen/kill
8+
//! operations, prefer `GenKillAnalysis` as it will perform better. Create an `Engine` using the
9+
//! appropriate constructor and call `iterate_to_fixpoint`. You can use a `ResultsCursor` to
10+
//! inspect the fixpoint solution to your dataflow problem.
11+
//!
12+
//! ```ignore(cross-crate-imports)
13+
//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>, did: DefId) {
14+
//! let analysis = MyAnalysis::new();
15+
//!
16+
//! // If `MyAnalysis` implements `GenKillAnalysis`.
17+
//! let results = Engine::new_gen_kill(tcx, body, did, analysis).iterate_to_fixpoint();
18+
//!
19+
//! // If `MyAnalysis` implements `Analysis`.
20+
//! // let results = Engine::new_generic(tcx, body, did, analysis).iterate_to_fixpoint();
21+
//!
22+
//! let mut cursor = ResultsCursor::new(body, results);
23+
//!
24+
//! for (_, statement_index) in body.block_data[START_BLOCK].statements.iter_enumerated() {
25+
//! cursor.seek_after(Location { block: START_BLOCK, statement_index });
26+
//! let state = cursor.get();
27+
//! println!("{:?}", state);
28+
//! }
29+
//! }
30+
//! ```
31+
//!
32+
//! [gen-kill]: https://en.wikipedia.org/wiki/Data-flow_analysis#Bit_vector_problems
33+
//! [design-meeting]https://github.com/rust-lang/compiler-team/issues/202
234
335
use std::io;
436

@@ -68,7 +100,7 @@ pub trait AnalysisDomain<'tcx>: BottomValue {
68100
}
69101
}
70102

71-
/// Define a dataflow problem with an arbitrarily complex transfer function.
103+
/// A dataflow problem with an arbitrarily complex transfer function.
72104
pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
73105
/// Updates the current dataflow state with the effect of evaluating a statement.
74106
fn apply_statement_effect(
@@ -134,7 +166,7 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> {
134166
);
135167
}
136168

137-
/// Define a gen/kill dataflow problem.
169+
/// A gen/kill dataflow problem.
138170
///
139171
/// Each method in this trait has a corresponding one in `Analysis`. However, these methods only
140172
/// allow modification of the dataflow state via "gen" and "kill" operations. By defining transfer

0 commit comments

Comments
 (0)