Skip to content

Commit

Permalink
Rollup merge of #116315 - cjgillot:cpl-clean, r=petrochenkov
Browse files Browse the repository at this point in the history
Do not check for impossible predicates in const-prop lint.

The enclosing query already checks for them, and replaces the body with a single `unreachable` if they are indeed impossible.
  • Loading branch information
matthiaskrgr committed Oct 11, 2023
2 parents 8ddc0df + 465a82f commit 9b7e0af
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 50 deletions.
57 changes: 10 additions & 47 deletions compiler/rustc_mir_transform/src/const_prop_lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ use rustc_middle::ty::{
};
use rustc_span::Span;
use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
use rustc_trait_selection::traits;

use crate::const_prop::CanConstProp;
use crate::const_prop::ConstPropMachine;
Expand All @@ -35,9 +34,9 @@ use crate::MirLint;
/// Severely regress performance.
const MAX_ALLOC_LIMIT: u64 = 1024;

pub struct ConstProp;
pub struct ConstPropLint;

impl<'tcx> MirLint<'tcx> for ConstProp {
impl<'tcx> MirLint<'tcx> for ConstPropLint {
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
if body.tainted_by_errors.is_some() {
return;
Expand All @@ -49,61 +48,25 @@ impl<'tcx> MirLint<'tcx> for ConstProp {
}

let def_id = body.source.def_id().expect_local();
let is_fn_like = tcx.def_kind(def_id).is_fn_like();
let is_assoc_const = tcx.def_kind(def_id) == DefKind::AssocConst;
let def_kind = tcx.def_kind(def_id);
let is_fn_like = def_kind.is_fn_like();
let is_assoc_const = def_kind == DefKind::AssocConst;

// Only run const prop on functions, methods, closures and associated constants
if !is_fn_like && !is_assoc_const {
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
trace!("ConstProp skipped for {:?}", def_id);
trace!("ConstPropLint skipped for {:?}", def_id);
return;
}

let is_generator = tcx.type_of(def_id.to_def_id()).instantiate_identity().is_generator();
// FIXME(welseywiser) const prop doesn't work on generators because of query cycles
// computing their layout.
if is_generator {
trace!("ConstProp skipped for generator {:?}", def_id);
if let DefKind::Generator = def_kind {
trace!("ConstPropLint skipped for generator {:?}", def_id);
return;
}

// Check if it's even possible to satisfy the 'where' clauses
// for this item.
// This branch will never be taken for any normal function.
// However, it's possible to `#!feature(trivial_bounds)]` to write
// a function with impossible to satisfy clauses, e.g.:
// `fn foo() where String: Copy {}`
//
// We don't usually need to worry about this kind of case,
// since we would get a compilation error if the user tried
// to call it. However, since we can do const propagation
// even without any calls to the function, we need to make
// sure that it even makes sense to try to evaluate the body.
// If there are unsatisfiable where clauses, then all bets are
// off, and we just give up.
//
// We manually filter the predicates, skipping anything that's not
// "global". We are in a potentially generic context
// (e.g. we are evaluating a function without substituting generic
// parameters, so this filtering serves two purposes:
//
// 1. We skip evaluating any predicates that we would
// never be able prove are unsatisfiable (e.g. `<T as Foo>`
// 2. We avoid trying to normalize predicates involving generic
// parameters (e.g. `<T as Foo>::MyItem`). This can confuse
// the normalization code (leading to cycle errors), since
// it's usually never invoked in this way.
let predicates = tcx
.predicates_of(def_id.to_def_id())
.predicates
.iter()
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) {
trace!("ConstProp skipped for {:?}: found unsatisfiable predicates", def_id);
return;
}

trace!("ConstProp starting for {:?}", def_id);
trace!("ConstPropLint starting for {:?}", def_id);

// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
// constants, instead of just checking for const-folding succeeding.
Expand All @@ -112,7 +75,7 @@ impl<'tcx> MirLint<'tcx> for ConstProp {
let mut linter = ConstPropagator::new(body, tcx);
linter.visit_body(body);

trace!("ConstProp done for {:?}", def_id);
trace!("ConstPropLint done for {:?}", def_id);
}
}

Expand Down
4 changes: 1 addition & 3 deletions compiler/rustc_mir_transform/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&elaborate_box_derefs::ElaborateBoxDerefs,
&generator::StateTransform,
&add_retag::AddRetag,
&Lint(const_prop_lint::ConstProp),
&Lint(const_prop_lint::ConstPropLint),
];
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
}
Expand Down Expand Up @@ -554,8 +554,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
&const_prop::ConstProp,
&gvn::GVN,
&dataflow_const_prop::DataflowConstProp,
//
// Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0.
&const_debuginfo::ConstDebugInfo,
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
&early_otherwise_branch::EarlyOtherwiseBranch,
Expand Down

0 comments on commit 9b7e0af

Please sign in to comment.