1
Fork 0

Fully clear the body.

This commit is contained in:
Camille GILLOT 2023-04-24 18:50:32 +00:00
parent 15e5072147
commit 0f857791ad

View file

@ -30,7 +30,7 @@ use rustc_middle::mir::visit::Visitor as _;
use rustc_middle::mir::{ use rustc_middle::mir::{
traversal, AnalysisPhase, Body, ClearCrossCrate, ConstQualifs, Constant, LocalDecl, MirPass, traversal, AnalysisPhase, Body, ClearCrossCrate, ConstQualifs, Constant, LocalDecl, MirPass,
MirPhase, Operand, Place, ProjectionElem, Promoted, RuntimePhase, Rvalue, SourceInfo, MirPhase, Operand, Place, ProjectionElem, Promoted, RuntimePhase, Rvalue, SourceInfo,
Statement, StatementKind, TerminatorKind, Statement, StatementKind, TerminatorKind, START_BLOCK,
}; };
use rustc_middle::ty::query::Providers; use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt}; use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
@ -491,9 +491,10 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
// //
// We don't usually need to worry about this kind of case, // We don't usually need to worry about this kind of case,
// since we would get a compilation error if the user tried // since we would get a compilation error if the user tried
// to call it. However, since we can do const propagation // to call it. However, since we optimize even without any
// even without any calls to the function, we need to make // calls to the function, we need to make sure that it even
// sure that it even makes sense to try to evaluate the body. // makes sense to try to evaluate the body.
//
// If there are unsatisfiable where clauses, then all bets are // If there are unsatisfiable where clauses, then all bets are
// off, and we just give up. // off, and we just give up.
// //
@ -515,16 +516,17 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None }); .filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) { if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) {
trace!("optimizations skipped for {:?}: found unsatisfiable predicates", body.source); trace!("optimizations skipped for {:?}: found unsatisfiable predicates", body.source);
// Clear the body to only contain a single `unreachable` statement.
let bbs = body.basic_blocks.as_mut();
bbs.raw.truncate(1);
bbs[START_BLOCK].statements.clear();
bbs[START_BLOCK].terminator_mut().kind = TerminatorKind::Unreachable;
body.var_debug_info.clear();
body.local_decls.raw.truncate(body.arg_count + 1);
pm::run_passes( pm::run_passes(
tcx, tcx,
body, body,
&[ &[&reveal_all::RevealAll, &dump_mir::Marker("PreCodegen")],
&reveal_all::RevealAll,
&simplify::SimplifyCfg::Final,
&simplify::SimplifyLocals::Final,
// Dump the end result for testing and debugging purposes.
&dump_mir::Marker("PreCodegen"),
],
Some(MirPhase::Runtime(RuntimePhase::Optimized)), Some(MirPhase::Runtime(RuntimePhase::Optimized)),
); );
return; return;