forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#107411 - cjgillot:dataflow-discriminant, r=…
…oli-obk Handle discriminant in DataflowConstProp cc ``@jachris`` r? ``@JakobDegen`` This PR attempts to extend the DataflowConstProp pass to handle propagation of discriminants. We handle this by adding 2 new variants to `TrackElem`: `TrackElem::Variant` for enum variants and `TrackElem::Discriminant` for the enum discriminant pseudo-place. The difficulty is that the enum discriminant and enum variants may alias each another. This is the issue of the `Option<NonZeroUsize>` test, which is the equivalent of rust-lang/unsafe-code-guidelines#84 with a direct write. To handle that, we generalize the flood process to flood all the potentially aliasing places. In particular: - any write to `(PLACE as Variant)`, either direct or through a projection, floods `(PLACE as OtherVariant)` for all other variants and `discriminant(PLACE)`; - `SetDiscriminant(PLACE)` floods `(PLACE as Variant)` for each variant. This implies that flooding is not hierarchical any more, and that an assignment to a non-tracked place may need to flood a tracked place. This is handled by `for_each_aliasing_place` which generalizes `preorder_invoke`. As we deaggregate enums by putting `SetDiscriminant` last, this allows to propagate the value of the discriminant. This refactor will allow to make rust-lang#107009 able to handle discriminants too.
- Loading branch information
Showing
9 changed files
with
408 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
tests/mir-opt/dataflow-const-prop/enum.mutate_discriminant.DataflowConstProp.diff
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
- // MIR for `mutate_discriminant` before DataflowConstProp | ||
+ // MIR for `mutate_discriminant` after DataflowConstProp | ||
|
||
fn mutate_discriminant() -> u8 { | ||
let mut _0: u8; // return place in scope 0 at $DIR/enum.rs:+0:29: +0:31 | ||
let mut _1: std::option::Option<NonZeroUsize>; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL | ||
let mut _2: isize; // in scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL | ||
|
||
bb0: { | ||
discriminant(_1) = 1; // scope 0 at $DIR/enum.rs:+4:13: +4:34 | ||
(((_1 as variant#1).0: NonZeroUsize).0: usize) = const 0_usize; // scope 0 at $DIR/enum.rs:+6:13: +6:64 | ||
_2 = discriminant(_1); // scope 0 at $SRC_DIR/core/src/intrinsics/mir.rs:LL:COL | ||
switchInt(_2) -> [0: bb1, otherwise: bb2]; // scope 0 at $DIR/enum.rs:+9:13: +12:14 | ||
} | ||
|
||
bb1: { | ||
_0 = const 1_u8; // scope 0 at $DIR/enum.rs:+15:13: +15:20 | ||
return; // scope 0 at $DIR/enum.rs:+16:13: +16:21 | ||
} | ||
|
||
bb2: { | ||
_0 = const 2_u8; // scope 0 at $DIR/enum.rs:+19:13: +19:20 | ||
unreachable; // scope 0 at $DIR/enum.rs:+20:13: +20:26 | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,52 @@ | ||
// unit-test: DataflowConstProp | ||
|
||
// Not trackable, because variants could be aliased. | ||
#![feature(custom_mir, core_intrinsics, rustc_attrs)] | ||
|
||
use std::intrinsics::mir::*; | ||
|
||
enum E { | ||
V1(i32), | ||
V2(i32) | ||
} | ||
|
||
// EMIT_MIR enum.main.DataflowConstProp.diff | ||
fn main() { | ||
// EMIT_MIR enum.simple.DataflowConstProp.diff | ||
fn simple() { | ||
let e = E::V1(0); | ||
let x = match e { E::V1(x) => x, E::V2(x) => x }; | ||
} | ||
|
||
#[rustc_layout_scalar_valid_range_start(1)] | ||
#[rustc_nonnull_optimization_guaranteed] | ||
struct NonZeroUsize(usize); | ||
|
||
// EMIT_MIR enum.mutate_discriminant.DataflowConstProp.diff | ||
#[custom_mir(dialect = "runtime", phase = "post-cleanup")] | ||
fn mutate_discriminant() -> u8 { | ||
mir!( | ||
let x: Option<NonZeroUsize>; | ||
{ | ||
SetDiscriminant(x, 1); | ||
// This assignment overwrites the niche in which the discriminant is stored. | ||
place!(Field(Field(Variant(x, 1), 0), 0)) = 0_usize; | ||
// So we cannot know the value of this discriminant. | ||
let a = Discriminant(x); | ||
match a { | ||
0 => bb1, | ||
_ => bad, | ||
} | ||
} | ||
bb1 = { | ||
RET = 1; | ||
Return() | ||
} | ||
bad = { | ||
RET = 2; | ||
Unreachable() | ||
} | ||
) | ||
} | ||
|
||
fn main() { | ||
simple(); | ||
mutate_discriminant(); | ||
} |
Oops, something went wrong.