Skip to content
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
128 changes: 69 additions & 59 deletions compiler/rustc_mir_transform/src/early_otherwise_branch.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::fmt::Debug;

use rustc_index::bit_set::DenseBitSet;
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, VisitPlacesWith, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::{Ty, TyCtxt};
use rustc_middle::ty::{self, Ty, TyCtxt};
use tracing::trace;

use super::simplify::simplify_cfg;
use super::ssa::SsaLocals;
use crate::PassPolicy;
use crate::patch::MirPatch;

Expand Down Expand Up @@ -102,11 +105,16 @@ impl<'tcx> crate::MirPass<'tcx> for EarlyOtherwiseBranch {
trace!("running EarlyOtherwiseBranch on {:?}", body.source);

let mut should_cleanup = false;
// The transform only writes fresh temporaries, so argument facts survive CFG changes.
let mut unchanged_arguments = None;
Comment thread
dianqk marked this conversation as resolved.
Resolved

// Also consider newly generated bbs in the same pass
for parent in body.basic_blocks.indices() {
let bbs = &*body.basic_blocks;
let Some(opt_data) = evaluate_candidate(tcx, body, parent) else { continue };
let Some(opt_data) = evaluate_candidate(tcx, body, parent, &mut unchanged_arguments)
else {
continue;
};

trace!("SUCCESS: found optimization possibility to apply: {opt_data:?}");

Expand Down Expand Up @@ -212,6 +220,7 @@ fn evaluate_candidate<'tcx>(
tcx: TyCtxt<'tcx>,
body: &Body<'tcx>,
parent: BasicBlock,
unchanged_arguments: &mut Option<UnchangedArguments>,
) -> Option<OptimizationData<'tcx>> {
let bbs = &body.basic_blocks;
// NB: If this BB is a cleanup, we may need to figure out what else needs to be handled.
Expand Down Expand Up @@ -300,11 +309,20 @@ fn evaluate_candidate<'tcx>(
child_place,
destination,
need_hoist_discriminant,
otherwise_is_empty_unreachable,
) {
return None;
}
}
// A stable address does not imply that the child place is initialized on the otherwise path.
// Keep discriminant hoisting restricted to exhaustive switches (#95162).
if !otherwise_is_empty_unreachable
&& (need_hoist_discriminant
Comment thread
dianqk marked this conversation as resolved.
Resolved
|| !unchanged_arguments
.get_or_insert_with(|| UnchangedArguments::new(tcx, body))
.can_hoist(body, child_place))
{
return None;
}
Some(OptimizationData {
destination,
child_place,
Expand All @@ -320,67 +338,11 @@ fn verify_candidate_branch<'tcx>(
place: Place<'tcx>,
destination: BasicBlock,
need_hoist_discriminant: bool,
otherwise_is_empty_unreachable: bool,
) -> bool {
// In order for the optimization to be correct, the terminator must be a `SwitchInt`.
let TerminatorKind::SwitchInt { discr: switch_op, targets } = &branch.terminator().kind else {
return false;
};
if !otherwise_is_empty_unreachable {
// Someone could write code like this:
// ```rust
// let Q = val;
// if discriminant(P) == otherwise {
// let ptr = &mut Q as *mut _ as *mut u8;
// // It may be difficult for us to effectively determine whether values are valid.
// // Invalid values can come from all sorts of corners.
// unsafe { *ptr = 10; }
// }
//
// match P {
// A => match Q {
// A => {
// // code
// }
// _ => {
// // don't use Q
// }
// }
// _ => {
// // don't use Q
// }
// };
// ```
//
// Hoisting the `discriminant(Q)` out of the `A` arm causes us to compute the discriminant of an
// invalid value, which is UB.
// In order to fix this, **we would either need to show that the discriminant computation of
// `place` is computed in all branches**.
// For <https://github.com/rust-lang/rust/issues/95162>, we adopt a conservative approach and
// consider only the `otherwise` branch has no statements and an unreachable terminator.
if need_hoist_discriminant {
return false;
}
// For <https://github.com/rust-lang/rust/issues/159591>:
// ```
// bb0: {
// switchInt(copy _1) -> [1: bb1, 2: bb2, otherwise: bb5];
// }
// bb1: {
// switchInt(copy (*_2)) -> [1: bb3, otherwise: bb5];
// }
// bb2: {
// switchInt(copy (*_2)) -> [2: bb4, otherwise: bb5];
// }
// ```
// We cannot hoist the dereference of `_2` to `bb0`,
// because execution can reach `bb5` without dereferencing `_2`.
if let Some(place) = switch_op.place()
&& !place.is_stable_offset()
{
return false;
}
}
if need_hoist_discriminant {
// If we need hoist discriminant, the branch must have exactly one statement.
let [statement] = branch.statements.as_slice() else {
Expand Down Expand Up @@ -419,3 +381,51 @@ fn verify_candidate_branch<'tcx>(
};
target_value == value
}

struct UnchangedArguments(DenseBitSet<Local>);

impl UnchangedArguments {
fn new<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> Self {
let ssa = SsaLocals::new(tcx, body, body.typing_env(tcx));
let mut arguments = DenseBitSet::new_empty(body.local_decls.len());
for arg in body.args_iter() {
if ssa.is_ssa(arg) && !ssa.borrowed_locals().contains(arg) {
arguments.insert(arg);
}
}
// SSA does not track storage lifetimes or invalidate moved places.
VisitPlacesWith(|place: Place<'_>, context: PlaceContext| {
if matches!(context, PlaceContext::NonMutatingUse(NonMutatingUseContext::Move))
|| context.is_storage_marker()
{
arguments.remove(place.local);
}
})
.visit_body(body);
Self(arguments)
}

fn can_hoist<'tcx>(&self, body: &Body<'tcx>, place: Place<'tcx>) -> bool {
// New temporaries added by this pass are outside the cached bitset.
if !self.0.contains_loose(place.local) {
return false;
}
let mut ty = body.local_decls[place.local].ty;
for elem in place.projection.iter() {
let ProjectionElem::Field(_, field_ty) = elem else {
return false;
};
// A valid union or enum need not have initialized bytes in every field.
let fields_are_valid = match ty.kind() {
ty::Tuple(_) => true,
ty::Adt(def, _) => def.is_struct(),
_ => false,
};
if !fields_are_valid {
return false;
}
ty = field_ty;
}
true
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
_4 = copy _1;
StorageLive(_5);
_5 = copy _2;
_3 = (move _4, move _5);
_3 = (copy _1, copy _2);
StorageDead(_5);
StorageDead(_4);
_7 = discriminant((_3.0: std::option::Option<u32>));
_7 = discriminant(_1);
switchInt(move _7) -> [1: bb2, 0: bb1, otherwise: bb5];
}

Expand All @@ -36,15 +36,15 @@
}

bb2: {
_6 = discriminant((_3.1: std::option::Option<u32>));
_6 = discriminant(_2);
switchInt(move _6) -> [1: bb3, 0: bb1, otherwise: bb5];
}

bb3: {
StorageLive(_8);
_8 = copy (((_3.0: std::option::Option<u32>) as Some).0: u32);
_8 = copy ((_1 as Some).0: u32);
StorageLive(_9);
_9 = copy (((_3.1: std::option::Option<u32>) as Some).0: u32);
_9 = copy ((_2 as Some).0: u32);
_0 = const 0_u32;
StorageDead(_9);
StorageDead(_8);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
_4 = copy _1;
StorageLive(_5);
_5 = copy _2;
_3 = (move _4, move _5);
_3 = (copy _1, copy _2);
StorageDead(_5);
StorageDead(_4);
_8 = discriminant((_3.0: std::option::Option<u32>));
_8 = discriminant(_1);
switchInt(move _8) -> [0: bb3, 1: bb2, otherwise: bb7];
}

Expand All @@ -37,12 +37,12 @@
}

bb2: {
_6 = discriminant((_3.1: std::option::Option<u32>));
_6 = discriminant(_2);
switchInt(move _6) -> [1: bb5, 0: bb1, otherwise: bb7];
}

bb3: {
_7 = discriminant((_3.1: std::option::Option<u32>));
_7 = discriminant(_2);
switchInt(move _7) -> [0: bb4, 1: bb1, otherwise: bb7];
}

Expand All @@ -53,9 +53,9 @@

bb5: {
StorageLive(_9);
_9 = copy (((_3.0: std::option::Option<u32>) as Some).0: u32);
_9 = copy ((_1 as Some).0: u32);
StorageLive(_10);
_10 = copy (((_3.1: std::option::Option<u32>) as Some).0: u32);
_10 = copy ((_2 as Some).0: u32);
_0 = const 0_u32;
StorageDead(_10);
StorageDead(_9);
Expand Down
20 changes: 10 additions & 10 deletions tests/mir-opt/early_otherwise_branch.opt3.EarlyOtherwiseBranch.diff
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
bb0: {
StorageLive(_3);
StorageLive(_4);
_4 = move _1;
_4 = copy _1;
StorageLive(_5);
_5 = move _2;
_3 = (move _4, move _5);
_5 = copy _2;
_3 = (copy _1, copy _2);
StorageDead(_5);
StorageDead(_4);
_9 = discriminant((_3.0: Option2<u32>));
_9 = discriminant(_1);
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
+ _12 = discriminant((_3.1: Option2<bool>));
+ _12 = discriminant(_2);
+ _13 = Ne(copy _9, move _12);
+ switchInt(move _13) -> [0: bb7, otherwise: bb1];
}
Expand All @@ -44,17 +44,17 @@
}

bb2: {
- _6 = discriminant((_3.1: Option2<bool>));
- _6 = discriminant(_2);
- switchInt(move _6) -> [0: bb7, otherwise: bb1];
- }
-
- bb3: {
- _7 = discriminant((_3.1: Option2<bool>));
- _7 = discriminant(_2);
- switchInt(move _7) -> [1: bb6, otherwise: bb1];
- }
-
- bb4: {
- _8 = discriminant((_3.1: Option2<bool>));
- _8 = discriminant(_2);
- switchInt(move _8) -> [2: bb5, otherwise: bb1];
- }
-
Expand All @@ -74,9 +74,9 @@
- bb7: {
+ bb4: {
StorageLive(_10);
_10 = copy (((_3.0: Option2<u32>) as Some).0: u32);
_10 = copy ((_1 as Some).0: u32);
StorageLive(_11);
_11 = copy (((_3.1: Option2<bool>) as Some).0: bool);
_11 = copy ((_2 as Some).0: bool);
_0 = const 0_u32;
StorageDead(_11);
StorageDead(_10);
Expand Down
20 changes: 10 additions & 10 deletions tests/mir-opt/early_otherwise_branch.opt4.EarlyOtherwiseBranch.diff
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@
bb0: {
StorageLive(_3);
StorageLive(_4);
_4 = move _1;
_4 = copy _1;
StorageLive(_5);
_5 = move _2;
_3 = (move _4, move _5);
_5 = copy _2;
_3 = (copy _1, copy _2);
StorageDead(_5);
StorageDead(_4);
_9 = discriminant((_3.0: Option2<u32>));
_9 = discriminant(_1);
- switchInt(move _9) -> [0: bb2, 1: bb3, 2: bb4, otherwise: bb9];
+ _12 = discriminant((_3.1: Option2<u32>));
+ _12 = discriminant(_2);
+ _13 = Ne(copy _9, move _12);
+ switchInt(move _13) -> [0: bb7, otherwise: bb1];
}
Expand All @@ -44,17 +44,17 @@
}

bb2: {
- _6 = discriminant((_3.1: Option2<u32>));
- _6 = discriminant(_2);
- switchInt(move _6) -> [0: bb7, otherwise: bb1];
- }
-
- bb3: {
- _7 = discriminant((_3.1: Option2<u32>));
- _7 = discriminant(_2);
- switchInt(move _7) -> [1: bb6, otherwise: bb1];
- }
-
- bb4: {
- _8 = discriminant((_3.1: Option2<u32>));
- _8 = discriminant(_2);
- switchInt(move _8) -> [2: bb5, otherwise: bb1];
- }
-
Expand All @@ -74,9 +74,9 @@
- bb7: {
+ bb4: {
StorageLive(_10);
_10 = copy (((_3.0: Option2<u32>) as Some).0: u32);
_10 = copy ((_1 as Some).0: u32);
StorageLive(_11);
_11 = copy (((_3.1: Option2<u32>) as Some).0: u32);
_11 = copy ((_2 as Some).0: u32);
_0 = const 0_u32;
StorageDead(_11);
StorageDead(_10);
Expand Down
Loading
Loading