Skip to content

Commit f71b3e2

Browse files
authored
Rollup merge of #89532 - ecstatic-morse:maybe-live-locals-enum, r=oli-obk,tmiasko
Document behavior of `MaybeLiveLocals` regarding enums and field-senstivity This arose from a [discussion on Zulip](https://rust-lang.zulipchat.com/#narrow/stream/189540-t-compiler.2Fwg-mir-opt/topic/MaybeLiveLocals.20and.20Discriminants) where a new contributor attempted to implement a dead-store elimination pass using this analysis. They ran into a nasty hack around `SetDiscriminant` the effect of which is to lets handle assignments of literals to enum-typed locals (e.g. `x = Some(4)`) correctly. This took me a while to figure out. Document this oddity, so the next person will have an easier time, and add a test to enshrine the current behavior. r? ``@tmiasko``
2 parents 048b0fd + 9f9f7f6 commit f71b3e2

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed

compiler/rustc_mir_dataflow/src/impls/liveness.rs

+31
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
+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#![feature(core_intrinsics, rustc_attrs)]
2+
3+
use std::intrinsics::rustc_peek;
4+
5+
#[rustc_mir(rustc_peek_liveness, stop_after_dataflow)]
6+
fn foo() -> Option<i32> {
7+
let mut x = None;
8+
9+
// `x` is live here since it is used in the next statement...
10+
rustc_peek(x);
11+
12+
dbg!(x);
13+
14+
// But not here, since it is overwritten below
15+
rustc_peek(x); //~ ERROR rustc_peek: bit not set
16+
17+
x = Some(4);
18+
19+
x
20+
}
21+
22+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: rustc_peek: bit not set
2+
--> $DIR/liveness-enum.rs:15:5
3+
|
4+
LL | rustc_peek(x);
5+
| ^^^^^^^^^^^^^
6+
7+
error: stop_after_dataflow ended compilation
8+
9+
error: aborting due to 2 previous errors
10+

0 commit comments

Comments
 (0)