Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make liveness more precise for assignments to fields #73774

Merged
merged 3 commits into from
Jun 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/librustc_mir/dataflow/impls/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,27 @@ impl<'tcx, T> Visitor<'tcx> for TransferFunction<'_, T>
where
T: GenKill<Local>,
{
fn visit_place(&mut self, place: &mir::Place<'tcx>, context: PlaceContext, location: Location) {
let mir::Place { projection, local } = *place;

// We purposefully do not call `super_place` here to avoid calling `visit_local` for this
// place with one of the `Projection` variants of `PlaceContext`.
self.visit_projection(local, projection, context, location);

match DefUse::for_place(context) {
// Treat derefs as a use of the base local. `*p = 4` is not a def of `p` but a use.
Some(_) if place.is_indirect() => self.0.gen(local),

Some(DefUse::Def) if projection.is_empty() => self.0.kill(local),
Some(DefUse::Use) => self.0.gen(local),
_ => {}
}
}

fn visit_local(&mut self, &local: &Local, context: PlaceContext, _: Location) {
// Because we do not call `super_place` above, `visit_local` is only called for locals that
// do not appear as part of a `Place` in the MIR. This handles cases like the implicit use
// of the return place in a `Return` terminator or the index in an `Index` projection.
match DefUse::for_place(context) {
Some(DefUse::Def) => self.0.kill(local),
Some(DefUse::Use) => self.0.gen(local),
Expand Down Expand Up @@ -126,19 +146,22 @@ impl DefUse {
| MutatingUseContext::AsmOutput
| MutatingUseContext::Borrow
| MutatingUseContext::Drop
| MutatingUseContext::Projection
| MutatingUseContext::Retag,
)
| PlaceContext::NonMutatingUse(
NonMutatingUseContext::AddressOf
| NonMutatingUseContext::Copy
| NonMutatingUseContext::Inspect
| NonMutatingUseContext::Move
| NonMutatingUseContext::Projection
| NonMutatingUseContext::ShallowBorrow
| NonMutatingUseContext::SharedBorrow
| NonMutatingUseContext::UniqueBorrow,
) => Some(DefUse::Use),

PlaceContext::MutatingUse(MutatingUseContext::Projection)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => {
unreachable!("A projection could be a def or a use and must be handled separately")
}
}
}
}
32 changes: 32 additions & 0 deletions src/test/ui/mir-dataflow/liveness-projection.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#![feature(core_intrinsics, rustc_attrs)]

use std::intrinsics::rustc_peek;

#[rustc_mir(rustc_peek_liveness, stop_after_dataflow)]
fn foo() {
{
let mut x: (i32, i32) = (42, 0);

// Assignment to a projection does not cause `x` to become live
unsafe { rustc_peek(x); } //~ ERROR bit not set
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shouldn't this be after the assignment? Same with the read below

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Liveness propagates backwards, not forwards.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤦

x.1 = 42;

x = (0, 42);

// ...but a read from a projection does.
unsafe { rustc_peek(x); }
println!("{}", x.1);
}

{
let mut x = 42;

// Derefs are treated like a read of a local even if they are on the LHS of an assignment.
let p = &mut x;
unsafe { rustc_peek(&p); }
*p = 24;
unsafe { rustc_peek(&p); } //~ ERROR bit not set
}
}

fn main() {}
16 changes: 16 additions & 0 deletions src/test/ui/mir-dataflow/liveness-projection.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
error: rustc_peek: bit not set
--> $DIR/liveness-projection.rs:11:18
|
LL | unsafe { rustc_peek(x); }
| ^^^^^^^^^^^^^

error: rustc_peek: bit not set
--> $DIR/liveness-projection.rs:28:18
|
LL | unsafe { rustc_peek(&p); }
| ^^^^^^^^^^^^^^

error: stop_after_dataflow ended compilation

error: aborting due to 3 previous errors