Skip to content

Commit 02c2a35

Browse files
Discuss field-sensitivity and enums in context of MaybeLiveLocals
1 parent 175b8db commit 02c2a35

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

compiler/rustc_mir_dataflow/src/impls/liveness.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,37 @@ use crate::{AnalysisDomain, Backward, GenKill, GenKillAnalysis};
1111
/// exist. See [this `mir-dataflow` test][flow-test] for an example. You almost never want to use
1212
/// this analysis without also looking at the results of [`MaybeBorrowedLocals`].
1313
///
14+
/// ## Field-(in)sensitivity
15+
///
16+
/// As the name suggests, this analysis is field insensitive. If a projection of a variable `x` is
17+
/// assigned to (e.g. `x.0 = 42`), it does not "define" `x` as far as liveness is concerned. In fact,
18+
/// such an assignment is currently marked as a "use" of `x` in an attempt to be maximally
19+
/// conservative.
20+
///
21+
/// ## Enums and `SetDiscriminant`
22+
///
23+
/// Assigning a literal value to an `enum` (e.g. `Option<i32>`), does not result in a simple
24+
/// assignment of the form `_1 = /*...*/` in the MIR. For example, the following assignment to `x`:
25+
///
26+
/// ```
27+
/// x = Some(4);
28+
/// ```
29+
///
30+
/// compiles to this MIR
31+
///
32+
/// ```
33+
/// ((_1 as Some).0: i32) = const 4_i32;
34+
/// discriminant(_1) = 1;
35+
/// ```
36+
///
37+
/// However, `MaybeLiveLocals` **does** mark `x` (`_1`) as "killed" after a statement like this.
38+
/// That's because it treats the `SetDiscriminant` operation as a definition of `x`, even though
39+
/// the writes that actually initialized the locals happened earlier.
40+
///
41+
/// This makes `MaybeLiveLocals` unsuitable for certain classes of optimization normally associated
42+
/// with a live variables analysis, notably dead-store elimination. It's a dirty hack, but it works
43+
/// okay for the generator state transform (currently the main consumuer of this analysis).
44+
///
1445
/// [`MaybeBorrowedLocals`]: super::MaybeBorrowedLocals
1546
/// [flow-test]: https://github.com/rust-lang/rust/blob/a08c47310c7d49cbdc5d7afb38408ba519967ecd/src/test/ui/mir-dataflow/liveness-ptr.rs
1647
/// [liveness]: https://en.wikipedia.org/wiki/Live_variable_analysis

0 commit comments

Comments
 (0)