Skip to content

Commit 15e5072

Browse files
committed
Do not bother optimizing impossible functions.
1 parent 9e540df commit 15e5072

File tree

2 files changed

+49
-37
lines changed

2 files changed

+49
-37
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_middle::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeVisi
1818
use rustc_span::{def_id::DefId, Span, DUMMY_SP};
1919
use rustc_target::abi::{self, Align, HasDataLayout, Size, TargetDataLayout};
2020
use rustc_target::spec::abi::Abi as CallAbi;
21-
use rustc_trait_selection::traits;
2221

2322
use crate::MirPass;
2423
use rustc_const_eval::interpret::{
@@ -84,42 +83,6 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
8483
return;
8584
}
8685

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

12588
let dummy_body = &Body::new(

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ use rustc_middle::mir::{
3535
use rustc_middle::ty::query::Providers;
3636
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
3737
use rustc_span::sym;
38+
use rustc_trait_selection::traits;
3839

3940
#[macro_use]
4041
mod pass_manager;
@@ -481,6 +482,54 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
481482
WithMinOptLevel(1, x)
482483
}
483484

485+
// Check if it's even possible to satisfy the 'where' clauses
486+
// for this item.
487+
// This branch will never be taken for any normal function.
488+
// However, it's possible to `#!feature(trivial_bounds)]` to write
489+
// a function with impossible to satisfy clauses, e.g.:
490+
// `fn foo() where String: Copy {}`
491+
//
492+
// We don't usually need to worry about this kind of case,
493+
// since we would get a compilation error if the user tried
494+
// to call it. However, since we can do const propagation
495+
// even without any calls to the function, we need to make
496+
// sure that it even makes sense to try to evaluate the body.
497+
// If there are unsatisfiable where clauses, then all bets are
498+
// off, and we just give up.
499+
//
500+
// We manually filter the predicates, skipping anything that's not
501+
// "global". We are in a potentially generic context
502+
// (e.g. we are evaluating a function without substituting generic
503+
// parameters, so this filtering serves two purposes:
504+
//
505+
// 1. We skip evaluating any predicates that we would
506+
// never be able prove are unsatisfiable (e.g. `<T as Foo>`
507+
// 2. We avoid trying to normalize predicates involving generic
508+
// parameters (e.g. `<T as Foo>::MyItem`). This can confuse
509+
// the normalization code (leading to cycle errors), since
510+
// it's usually never invoked in this way.
511+
let predicates = tcx
512+
.predicates_of(body.source.def_id())
513+
.predicates
514+
.iter()
515+
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
516+
if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) {
517+
trace!("optimizations skipped for {:?}: found unsatisfiable predicates", body.source);
518+
pm::run_passes(
519+
tcx,
520+
body,
521+
&[
522+
&reveal_all::RevealAll,
523+
&simplify::SimplifyCfg::Final,
524+
&simplify::SimplifyLocals::Final,
525+
// Dump the end result for testing and debugging purposes.
526+
&dump_mir::Marker("PreCodegen"),
527+
],
528+
Some(MirPhase::Runtime(RuntimePhase::Optimized)),
529+
);
530+
return;
531+
}
532+
484533
// The main optimizations that we do on MIR.
485534
pm::run_passes(
486535
tcx,

0 commit comments

Comments
 (0)