Skip to content
Draft
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
17 changes: 17 additions & 0 deletions compiler/rustc_middle/src/mir/visit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1415,6 +1415,23 @@ impl PlaceContext {
)
}

/// Returns `true` if this place context may be used to know the address of the given place.
pub fn may_observe_address(self) -> bool {
matches!(
self,
PlaceContext::NonMutatingUse(
NonMutatingUseContext::SharedBorrow
| NonMutatingUseContext::RawBorrow
| NonMutatingUseContext::FakeBorrow
) | PlaceContext::MutatingUse(
MutatingUseContext::Drop
| MutatingUseContext::Borrow
| MutatingUseContext::RawBorrow
| MutatingUseContext::AsmOutput
)
)
}

/// Returns `true` if this place context represents a storage live or storage dead marker.
#[inline]
pub fn is_storage_marker(self) -> bool {
Expand Down
9 changes: 2 additions & 7 deletions compiler/rustc_mir_dataflow/src/value_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexSet, StdEntry};
use rustc_data_structures::stack::ensure_sufficient_stack;
use rustc_index::IndexVec;
use rustc_index::bit_set::DenseBitSet;
use rustc_middle::mir::visit::{MutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::visit::{PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::{self, Ty, TyCtxt};
use tracing::debug;
Expand Down Expand Up @@ -917,12 +917,7 @@ pub fn excluded_locals(body: &Body<'_>) -> DenseBitSet<Local> {

impl<'tcx> Visitor<'tcx> for Collector {
fn visit_place(&mut self, place: &Place<'tcx>, context: PlaceContext, _location: Location) {
if (context.is_borrow()
|| context.is_address_of()
|| context.is_drop()
|| context == PlaceContext::MutatingUse(MutatingUseContext::AsmOutput))
&& !place.is_indirect()
{
if context.may_observe_address() && !place.is_indirect() {
// A pointer to a place could be used to access other places with the same local,
// hence we have to exclude the local completely.
self.result.insert(place.local);
Expand Down
315 changes: 191 additions & 124 deletions compiler/rustc_mir_transform/src/gvn.rs

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion compiler/rustc_mir_transform/src/ssa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,9 @@ impl SsaVisitor<'_, '_> {

impl<'tcx> Visitor<'tcx> for SsaVisitor<'_, 'tcx> {
fn visit_local(&mut self, local: Local, ctxt: PlaceContext, loc: Location) {
if ctxt.may_observe_address() {
self.borrowed_locals.insert(local);
}
match ctxt {
PlaceContext::MutatingUse(MutatingUseContext::Projection)
| PlaceContext::NonMutatingUse(NonMutatingUseContext::Projection) => bug!(),
Expand All @@ -237,7 +240,6 @@ impl<'tcx> Visitor<'tcx> for SsaVisitor<'_, 'tcx> {
PlaceContext::NonMutatingUse(
NonMutatingUseContext::SharedBorrow | NonMutatingUseContext::FakeBorrow,
) => {
self.borrowed_locals.insert(local);
self.check_dominates(local, loc);
self.direct_uses[local] += 1;
}
Expand Down
3 changes: 2 additions & 1 deletion tests/mir-opt/gvn.dereference_indexing.GVN.panic-abort.diff
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
+ nop;
StorageLive(_8);
StorageLive(_9);
_9 = copy (*_3);
- _9 = copy (*_3);
+ _9 = copy _1[_4];
_8 = opaque::<u8>(move _9) -> [return: bb2, unwind unreachable];
}

Expand Down
3 changes: 2 additions & 1 deletion tests/mir-opt/gvn.dereference_indexing.GVN.panic-unwind.diff
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@
+ nop;
StorageLive(_8);
StorageLive(_9);
_9 = copy (*_3);
- _9 = copy (*_3);
+ _9 = copy _1[_4];
_8 = opaque::<u8>(move _9) -> [return: bb2, unwind continue];
}

Expand Down
4 changes: 2 additions & 2 deletions tests/mir-opt/gvn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1065,8 +1065,8 @@ fn dereference_indexing(array: [u8; 2], index: usize) {
&array[i]
};

// CHECK-NOT: [{{.*}}]
// CHECK: [[tmp:_.*]] = copy (*[[a]]);
// CHECK-NOT: StorageDead([[i]]);
// CHECK: [[tmp:_.*]] = copy _1[[[i]]];
// CHECK: opaque::<u8>(move [[tmp]])
opaque(*a);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/mir-opt/gvn_repeat.index_place.GVN.diff
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
- // MIR for `index_place` before GVN
+ // MIR for `index_place` after GVN

fn index_place(_1: usize, _2: usize, _3: [i32; 5]) -> i32 {
let mut _0: i32;
let mut _4: &i32;

bb0: {
_4 = &_3[_1];
_1 = copy _2;
_0 = copy (*_4);
return;
}
}

3 changes: 1 addition & 2 deletions tests/mir-opt/gvn_repeat.repeat_local.GVN.diff
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
_4 = [copy _3; 5];
_5 = &_4[_1];
_1 = copy _2;
- _0 = copy (*_5);
+ _0 = copy _3;
_0 = copy (*_5);
return;
}
}
Expand Down
21 changes: 20 additions & 1 deletion tests/mir-opt/gvn_repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@

use std::intrinsics::mir::*;

// EMIT_MIR gvn_repeat.index_place.GVN.diff
#[custom_mir(dialect = "runtime")]
pub fn index_place(mut idx1: usize, idx2: usize, array: [i32; 5]) -> i32 {
// CHECK-LABEL: fn index_place(
// CHECK: let mut [[ELEM:.*]]: &i32;
// CHECK: _0 = copy (*[[ELEM]])
mir! {
let elem;
{
elem = &array[idx1];
idx1 = idx2;
RET = *elem;
Return()
}
}
}

// EMIT_MIR gvn_repeat.repeat_place.GVN.diff
#[custom_mir(dialect = "runtime")]
pub fn repeat_place(mut idx1: usize, idx2: usize, val: &i32) -> i32 {
Expand All @@ -29,7 +46,8 @@ pub fn repeat_place(mut idx1: usize, idx2: usize, val: &i32) -> i32 {
#[custom_mir(dialect = "runtime")]
pub fn repeat_local(mut idx1: usize, idx2: usize, val: i32) -> i32 {
// CHECK-LABEL: fn repeat_local(
// CHECK: _0 = copy _3
// CHECK: let mut [[ELEM:.*]]: &i32;
// CHECK: _0 = copy (*[[ELEM]]);
mir! {
let array;
let elem;
Expand All @@ -44,6 +62,7 @@ pub fn repeat_local(mut idx1: usize, idx2: usize, val: i32) -> i32 {
}

fn main() {
assert_eq!(index_place(0, 5, [0; 5]), 0);
assert_eq!(repeat_place(0, 5, &0), 0);
assert_eq!(repeat_local(0, 5, 0), 0);
}
Loading