1
Fork 0

Auto merge of #115612 - cjgillot:const-prop-int, r=oli-obk

Improvements to dataflow const-prop

Partially cherry-picked from https://github.com/rust-lang/rust/pull/110719

r? `@oli-obk`
cc `@jachris`
This commit is contained in:
bors 2023-09-08 15:32:54 +00:00
commit 3cd97ed3c3
63 changed files with 1978 additions and 187 deletions

View file

@ -6,10 +6,10 @@ use rustc_const_eval::const_eval::CheckAlignment;
use rustc_const_eval::interpret::{ConstValue, ImmTy, Immediate, InterpCx, Scalar};
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::DefKind;
use rustc_middle::mir::visit::{MutVisitor, Visitor};
use rustc_middle::mir::visit::{MutVisitor, NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
use rustc_middle::ty::layout::TyAndLayout;
use rustc_middle::ty::{self, Ty, TyCtxt};
use rustc_middle::ty::{self, ScalarInt, Ty, TyCtxt};
use rustc_mir_dataflow::value_analysis::{
Map, State, TrackElem, ValueAnalysis, ValueAnalysisWrapper, ValueOrPlace,
};
@ -50,7 +50,7 @@ impl<'tcx> MirPass<'tcx> for DataflowConstProp {
let place_limit = if tcx.sess.mir_opt_level() < 4 { Some(PLACE_LIMIT) } else { None };
// Decide which places to track during the analysis.
let map = Map::from_filter(tcx, body, Ty::is_scalar, place_limit);
let map = Map::new(tcx, body, place_limit);
// Perform the actual dataflow analysis.
let analysis = ConstAnalysis::new(tcx, body, map);
@ -58,9 +58,13 @@ impl<'tcx> MirPass<'tcx> for DataflowConstProp {
.in_scope(|| analysis.wrap().into_engine(tcx, body).iterate_to_fixpoint());
// Collect results and patch the body afterwards.
let mut visitor = CollectAndPatch::new(tcx);
let mut visitor = CollectAndPatch::new(tcx, &body.local_decls);
debug_span!("collect").in_scope(|| results.visit_reachable_with(body, &mut visitor));
debug_span!("patch").in_scope(|| visitor.visit_body(body));
debug_span!("patch").in_scope(|| {
for (block, bbdata) in body.basic_blocks.as_mut_preserves_cfg().iter_enumerated_mut() {
visitor.visit_basic_block_data(block, bbdata);
}
})
}
}
@ -73,7 +77,7 @@ struct ConstAnalysis<'a, 'tcx> {
}
impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
type Value = FlatSet<ScalarTy<'tcx>>;
type Value = FlatSet<ScalarInt>;
const NAME: &'static str = "ConstAnalysis";
@ -172,9 +176,7 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
if let Some(overflow_target) = overflow_target {
let overflow = match overflow {
FlatSet::Top => FlatSet::Top,
FlatSet::Elem(overflow) => {
self.wrap_scalar(Scalar::from_bool(overflow), self.tcx.types.bool)
}
FlatSet::Elem(overflow) => FlatSet::Elem(overflow.into()),
FlatSet::Bottom => FlatSet::Bottom,
};
// We have flooded `target` earlier.
@ -182,6 +184,23 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
}
}
}
Rvalue::Cast(
CastKind::PointerCoercion(ty::adjustment::PointerCoercion::Unsize),
operand,
_,
) => {
let pointer = self.handle_operand(operand, state);
state.assign(target.as_ref(), pointer, self.map());
if let Some(target_len) = self.map().find_len(target.as_ref())
&& let operand_ty = operand.ty(self.local_decls, self.tcx)
&& let Some(operand_ty) = operand_ty.builtin_deref(true)
&& let ty::Array(_, len) = operand_ty.ty.kind()
&& let Some(len) = ConstantKind::Ty(*len).eval(self.tcx, self.param_env).try_to_scalar_int()
{
state.insert_value_idx(target_len, FlatSet::Elem(len), self.map());
}
}
_ => self.super_assign(target, rvalue, state),
}
}
@ -191,47 +210,77 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
rvalue: &Rvalue<'tcx>,
state: &mut State<Self::Value>,
) -> ValueOrPlace<Self::Value> {
match rvalue {
Rvalue::Cast(
kind @ (CastKind::IntToInt
| CastKind::FloatToInt
| CastKind::FloatToFloat
| CastKind::IntToFloat),
operand,
ty,
) => match self.eval_operand(operand, state) {
FlatSet::Elem(op) => match kind {
CastKind::IntToInt | CastKind::IntToFloat => {
self.ecx.int_to_int_or_float(&op, *ty)
}
CastKind::FloatToInt | CastKind::FloatToFloat => {
self.ecx.float_to_float_or_int(&op, *ty)
}
_ => unreachable!(),
let val = match rvalue {
Rvalue::Len(place) => {
let place_ty = place.ty(self.local_decls, self.tcx);
if let ty::Array(_, len) = place_ty.ty.kind() {
ConstantKind::Ty(*len)
.eval(self.tcx, self.param_env)
.try_to_scalar_int()
.map_or(FlatSet::Top, FlatSet::Elem)
} else if let [ProjectionElem::Deref] = place.projection[..] {
state.get_len(place.local.into(), self.map())
} else {
FlatSet::Top
}
.map(|result| ValueOrPlace::Value(self.wrap_immediate(result, *ty)))
.unwrap_or(ValueOrPlace::TOP),
_ => ValueOrPlace::TOP,
},
}
Rvalue::Cast(CastKind::IntToInt | CastKind::IntToFloat, operand, ty) => {
match self.eval_operand(operand, state) {
FlatSet::Elem(op) => self
.ecx
.int_to_int_or_float(&op, *ty)
.map_or(FlatSet::Top, |result| self.wrap_immediate(result)),
FlatSet::Bottom => FlatSet::Bottom,
FlatSet::Top => FlatSet::Top,
}
}
Rvalue::Cast(CastKind::FloatToInt | CastKind::FloatToFloat, operand, ty) => {
match self.eval_operand(operand, state) {
FlatSet::Elem(op) => self
.ecx
.float_to_float_or_int(&op, *ty)
.map_or(FlatSet::Top, |result| self.wrap_immediate(result)),
FlatSet::Bottom => FlatSet::Bottom,
FlatSet::Top => FlatSet::Top,
}
}
Rvalue::Cast(CastKind::Transmute, operand, _) => {
match self.eval_operand(operand, state) {
FlatSet::Elem(op) => self.wrap_immediate(*op),
FlatSet::Bottom => FlatSet::Bottom,
FlatSet::Top => FlatSet::Top,
}
}
Rvalue::BinaryOp(op, box (left, right)) => {
// Overflows must be ignored here.
let (val, _overflow) = self.binary_op(state, *op, left, right);
ValueOrPlace::Value(val)
val
}
Rvalue::UnaryOp(op, operand) => match self.eval_operand(operand, state) {
FlatSet::Elem(value) => self
.ecx
.unary_op(*op, &value)
.map(|val| ValueOrPlace::Value(self.wrap_immty(val)))
.unwrap_or(ValueOrPlace::Value(FlatSet::Top)),
FlatSet::Bottom => ValueOrPlace::Value(FlatSet::Bottom),
FlatSet::Top => ValueOrPlace::Value(FlatSet::Top),
FlatSet::Elem(value) => {
self.ecx.unary_op(*op, &value).map_or(FlatSet::Top, |val| self.wrap_immty(val))
}
FlatSet::Bottom => FlatSet::Bottom,
FlatSet::Top => FlatSet::Top,
},
Rvalue::Discriminant(place) => {
ValueOrPlace::Value(state.get_discr(place.as_ref(), self.map()))
Rvalue::NullaryOp(null_op, ty) => {
let Ok(layout) = self.tcx.layout_of(self.param_env.and(*ty)) else {
return ValueOrPlace::Value(FlatSet::Top);
};
let val = match null_op {
NullOp::SizeOf if layout.is_sized() => layout.size.bytes(),
NullOp::AlignOf if layout.is_sized() => layout.align.abi.bytes(),
NullOp::OffsetOf(fields) => layout
.offset_of_subfield(&self.ecx, fields.iter().map(|f| f.index()))
.bytes(),
_ => return ValueOrPlace::Value(FlatSet::Top),
};
ScalarInt::try_from_target_usize(val, self.tcx).map_or(FlatSet::Top, FlatSet::Elem)
}
_ => self.super_rvalue(rvalue, state),
}
Rvalue::Discriminant(place) => state.get_discr(place.as_ref(), self.map()),
_ => return self.super_rvalue(rvalue, state),
};
ValueOrPlace::Value(val)
}
fn handle_constant(
@ -242,9 +291,8 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
constant
.literal
.eval(self.tcx, self.param_env)
.try_to_scalar()
.map(|value| FlatSet::Elem(ScalarTy(value, constant.ty())))
.unwrap_or(FlatSet::Top)
.try_to_scalar_int()
.map_or(FlatSet::Top, FlatSet::Elem)
}
fn handle_switch_int<'mir>(
@ -261,9 +309,8 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
// We are branching on uninitialized data, this is UB, treat it as unreachable.
// This allows the set of visited edges to grow monotonically with the lattice.
FlatSet::Bottom => TerminatorEdges::None,
FlatSet::Elem(ScalarTy(scalar, _)) => {
let int = scalar.assert_int();
let choice = int.assert_bits(int.size());
FlatSet::Elem(scalar) => {
let choice = scalar.assert_bits(scalar.size());
TerminatorEdges::Single(targets.target_for_value(choice))
}
FlatSet::Top => TerminatorEdges::SwitchInt { discr, targets },
@ -271,16 +318,6 @@ impl<'tcx> ValueAnalysis<'tcx> for ConstAnalysis<'_, 'tcx> {
}
}
#[derive(Clone, PartialEq, Eq)]
struct ScalarTy<'tcx>(Scalar, Ty<'tcx>);
impl<'tcx> std::fmt::Debug for ScalarTy<'tcx> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
// This is used for dataflow visualization, so we return something more concise.
std::fmt::Display::fmt(&ConstantKind::Val(ConstValue::Scalar(self.0), self.1), f)
}
}
impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, map: Map) -> Self {
let param_env = tcx.param_env_reveal_all_normalized(body.source.def_id());
@ -295,32 +332,62 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
fn binary_op(
&self,
state: &mut State<FlatSet<ScalarTy<'tcx>>>,
state: &mut State<FlatSet<ScalarInt>>,
op: BinOp,
left: &Operand<'tcx>,
right: &Operand<'tcx>,
) -> (FlatSet<ScalarTy<'tcx>>, FlatSet<bool>) {
) -> (FlatSet<ScalarInt>, FlatSet<bool>) {
let left = self.eval_operand(left, state);
let right = self.eval_operand(right, state);
match (left, right) {
(FlatSet::Bottom, _) | (_, FlatSet::Bottom) => (FlatSet::Bottom, FlatSet::Bottom),
// Both sides are known, do the actual computation.
(FlatSet::Elem(left), FlatSet::Elem(right)) => {
match self.ecx.overflowing_binary_op(op, &left, &right) {
Ok((val, overflow, ty)) => (self.wrap_scalar(val, ty), FlatSet::Elem(overflow)),
Ok((Scalar::Int(val), overflow, _)) => {
(FlatSet::Elem(val), FlatSet::Elem(overflow))
}
_ => (FlatSet::Top, FlatSet::Top),
}
}
(FlatSet::Bottom, _) | (_, FlatSet::Bottom) => (FlatSet::Bottom, FlatSet::Bottom),
(_, _) => {
// Could attempt some algebraic simplifications here.
(FlatSet::Top, FlatSet::Top)
// Exactly one side is known, attempt some algebraic simplifications.
(FlatSet::Elem(const_arg), _) | (_, FlatSet::Elem(const_arg)) => {
let layout = const_arg.layout;
if !matches!(layout.abi, rustc_target::abi::Abi::Scalar(..)) {
return (FlatSet::Top, FlatSet::Top);
}
let arg_scalar = const_arg.to_scalar();
let Ok(arg_scalar) = arg_scalar.try_to_int() else {
return (FlatSet::Top, FlatSet::Top);
};
let Ok(arg_value) = arg_scalar.to_bits(layout.size) else {
return (FlatSet::Top, FlatSet::Top);
};
match op {
BinOp::BitAnd if arg_value == 0 => (FlatSet::Elem(arg_scalar), FlatSet::Bottom),
BinOp::BitOr
if arg_value == layout.size.truncate(u128::MAX)
|| (layout.ty.is_bool() && arg_value == 1) =>
{
(FlatSet::Elem(arg_scalar), FlatSet::Bottom)
}
BinOp::Mul if layout.ty.is_integral() && arg_value == 0 => {
(FlatSet::Elem(arg_scalar), FlatSet::Elem(false))
}
_ => (FlatSet::Top, FlatSet::Top),
}
}
(FlatSet::Top, FlatSet::Top) => (FlatSet::Top, FlatSet::Top),
}
}
fn eval_operand(
&self,
op: &Operand<'tcx>,
state: &mut State<FlatSet<ScalarTy<'tcx>>>,
state: &mut State<FlatSet<ScalarInt>>,
) -> FlatSet<ImmTy<'tcx>> {
let value = match self.handle_operand(op, state) {
ValueOrPlace::Value(value) => value,
@ -328,76 +395,76 @@ impl<'a, 'tcx> ConstAnalysis<'a, 'tcx> {
};
match value {
FlatSet::Top => FlatSet::Top,
FlatSet::Elem(ScalarTy(scalar, ty)) => self
.tcx
.layout_of(self.param_env.and(ty))
.map(|layout| FlatSet::Elem(ImmTy::from_scalar(scalar, layout)))
.unwrap_or(FlatSet::Top),
FlatSet::Elem(scalar) => {
let ty = op.ty(self.local_decls, self.tcx);
self.tcx
.layout_of(self.param_env.and(ty))
.map(|layout| FlatSet::Elem(ImmTy::from_scalar(scalar.into(), layout)))
.unwrap_or(FlatSet::Top)
}
FlatSet::Bottom => FlatSet::Bottom,
}
}
fn eval_discriminant(
&self,
enum_ty: Ty<'tcx>,
variant_index: VariantIdx,
) -> Option<ScalarTy<'tcx>> {
fn eval_discriminant(&self, enum_ty: Ty<'tcx>, variant_index: VariantIdx) -> Option<ScalarInt> {
if !enum_ty.is_enum() {
return None;
}
let discr = enum_ty.discriminant_for_variant(self.tcx, variant_index)?;
let discr_layout = self.tcx.layout_of(self.param_env.and(discr.ty)).ok()?;
let discr_value = Scalar::try_from_uint(discr.val, discr_layout.size)?;
Some(ScalarTy(discr_value, discr.ty))
let discr_value = ScalarInt::try_from_uint(discr.val, discr_layout.size)?;
Some(discr_value)
}
fn wrap_scalar(&self, scalar: Scalar, ty: Ty<'tcx>) -> FlatSet<ScalarTy<'tcx>> {
FlatSet::Elem(ScalarTy(scalar, ty))
}
fn wrap_immediate(&self, imm: Immediate, ty: Ty<'tcx>) -> FlatSet<ScalarTy<'tcx>> {
fn wrap_immediate(&self, imm: Immediate) -> FlatSet<ScalarInt> {
match imm {
Immediate::Scalar(scalar) => self.wrap_scalar(scalar, ty),
Immediate::Scalar(Scalar::Int(scalar)) => FlatSet::Elem(scalar),
_ => FlatSet::Top,
}
}
fn wrap_immty(&self, val: ImmTy<'tcx>) -> FlatSet<ScalarTy<'tcx>> {
self.wrap_immediate(*val, val.layout.ty)
fn wrap_immty(&self, val: ImmTy<'tcx>) -> FlatSet<ScalarInt> {
self.wrap_immediate(*val)
}
}
struct CollectAndPatch<'tcx> {
struct CollectAndPatch<'tcx, 'locals> {
tcx: TyCtxt<'tcx>,
local_decls: &'locals LocalDecls<'tcx>,
/// For a given MIR location, this stores the values of the operands used by that location. In
/// particular, this is before the effect, such that the operands of `_1 = _1 + _2` are
/// properly captured. (This may become UB soon, but it is currently emitted even by safe code.)
before_effect: FxHashMap<(Location, Place<'tcx>), ScalarTy<'tcx>>,
before_effect: FxHashMap<(Location, Place<'tcx>), ScalarInt>,
/// Stores the assigned values for assignments where the Rvalue is constant.
assignments: FxHashMap<Location, ScalarTy<'tcx>>,
assignments: FxHashMap<Location, ScalarInt>,
}
impl<'tcx> CollectAndPatch<'tcx> {
fn new(tcx: TyCtxt<'tcx>) -> Self {
Self { tcx, before_effect: FxHashMap::default(), assignments: FxHashMap::default() }
impl<'tcx, 'locals> CollectAndPatch<'tcx, 'locals> {
fn new(tcx: TyCtxt<'tcx>, local_decls: &'locals LocalDecls<'tcx>) -> Self {
Self {
tcx,
local_decls,
before_effect: FxHashMap::default(),
assignments: FxHashMap::default(),
}
}
fn make_operand(&self, scalar: ScalarTy<'tcx>) -> Operand<'tcx> {
fn make_operand(&self, scalar: ScalarInt, ty: Ty<'tcx>) -> Operand<'tcx> {
Operand::Constant(Box::new(Constant {
span: DUMMY_SP,
user_ty: None,
literal: ConstantKind::Val(ConstValue::Scalar(scalar.0), scalar.1),
literal: ConstantKind::Val(ConstValue::Scalar(scalar.into()), ty),
}))
}
}
impl<'mir, 'tcx>
ResultsVisitor<'mir, 'tcx, Results<'tcx, ValueAnalysisWrapper<ConstAnalysis<'_, 'tcx>>>>
for CollectAndPatch<'tcx>
for CollectAndPatch<'tcx, '_>
{
type FlowState = State<FlatSet<ScalarTy<'tcx>>>;
type FlowState = State<FlatSet<ScalarInt>>;
fn visit_statement_before_primary_effect(
&mut self,
@ -453,8 +520,8 @@ impl<'mir, 'tcx>
}
}
impl<'tcx> MutVisitor<'tcx> for CollectAndPatch<'tcx> {
fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
impl<'tcx> MutVisitor<'tcx> for CollectAndPatch<'tcx, '_> {
fn tcx(&self) -> TyCtxt<'tcx> {
self.tcx
}
@ -462,7 +529,8 @@ impl<'tcx> MutVisitor<'tcx> for CollectAndPatch<'tcx> {
if let Some(value) = self.assignments.get(&location) {
match &mut statement.kind {
StatementKind::Assign(box (_, rvalue)) => {
*rvalue = Rvalue::Use(self.make_operand(value.clone()));
let ty = rvalue.ty(self.local_decls, self.tcx);
*rvalue = Rvalue::Use(self.make_operand(*value, ty));
}
_ => bug!("found assignment info for non-assign statement"),
}
@ -475,33 +543,56 @@ impl<'tcx> MutVisitor<'tcx> for CollectAndPatch<'tcx> {
match operand {
Operand::Copy(place) | Operand::Move(place) => {
if let Some(value) = self.before_effect.get(&(location, *place)) {
*operand = self.make_operand(value.clone());
let ty = place.ty(self.local_decls, self.tcx).ty;
*operand = self.make_operand(*value, ty);
} else if !place.projection.is_empty() {
self.super_operand(operand, location)
}
}
_ => (),
Operand::Constant(_) => {}
}
}
fn process_projection_elem(
&mut self,
elem: PlaceElem<'tcx>,
location: Location,
) -> Option<PlaceElem<'tcx>> {
if let PlaceElem::Index(local) = elem
&& let Some(value) = self.before_effect.get(&(location, local.into()))
&& let Ok(offset) = value.try_to_target_usize(self.tcx)
&& let Some(min_length) = offset.checked_add(1)
{
Some(PlaceElem::ConstantIndex { offset, min_length, from_end: false })
} else {
None
}
}
}
struct OperandCollector<'tcx, 'map, 'a> {
state: &'a State<FlatSet<ScalarTy<'tcx>>>,
visitor: &'a mut CollectAndPatch<'tcx>,
struct OperandCollector<'tcx, 'map, 'locals, 'a> {
state: &'a State<FlatSet<ScalarInt>>,
visitor: &'a mut CollectAndPatch<'tcx, 'locals>,
map: &'map Map,
}
impl<'tcx, 'map, 'a> Visitor<'tcx> for OperandCollector<'tcx, 'map, 'a> {
impl<'tcx> Visitor<'tcx> for OperandCollector<'tcx, '_, '_, '_> {
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
match operand {
Operand::Copy(place) | Operand::Move(place) => {
match self.state.get(place.as_ref(), self.map) {
FlatSet::Top => (),
FlatSet::Elem(value) => {
self.visitor.before_effect.insert((location, *place), value);
}
FlatSet::Bottom => (),
}
if let Some(place) = operand.place() {
if let FlatSet::Elem(value) = self.state.get(place.as_ref(), self.map) {
self.visitor.before_effect.insert((location, place), value);
} else if !place.projection.is_empty() {
// Try to propagate into `Index` projections.
self.super_operand(operand, location)
}
_ => (),
}
}
fn visit_local(&mut self, local: Local, ctxt: PlaceContext, location: Location) {
if let PlaceContext::NonMutatingUse(NonMutatingUseContext::Copy | NonMutatingUseContext::Move) = ctxt
&& let FlatSet::Elem(value) = self.state.get(local.into(), self.map)
{
self.visitor.before_effect.insert((location, local.into()), value);
}
}
}
@ -572,7 +663,7 @@ impl<'mir, 'tcx: 'mir> rustc_const_eval::interpret::Machine<'mir, 'tcx> for Dumm
_bin_op: BinOp,
_left: &rustc_const_eval::interpret::ImmTy<'tcx, Self::Provenance>,
_right: &rustc_const_eval::interpret::ImmTy<'tcx, Self::Provenance>,
) -> interpret::InterpResult<'tcx, (interpret::Scalar<Self::Provenance>, bool, Ty<'tcx>)> {
) -> interpret::InterpResult<'tcx, (Scalar<Self::Provenance>, bool, Ty<'tcx>)> {
throw_unsup!(Unsupported("".into()))
}