Skip to content

Commit 59113ce

Browse files
committed
Add post-mono passes to make mono-reachable analysis more accurate
1 parent 794c124 commit 59113ce

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+322
-631
lines changed

compiler/rustc_codegen_cranelift/src/base.rs

+1-10
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ pub(crate) fn codegen_fn<'tcx>(
4343
let symbol_name = tcx.symbol_name(instance).name.to_string();
4444
let _timer = tcx.prof.generic_activity_with_arg("codegen fn", &*symbol_name);
4545

46-
let mir = tcx.instance_mir(instance.def);
46+
let mir = tcx.codegen_mir(instance);
4747
let _mir_guard = crate::PrintOnPanic(|| {
4848
let mut buf = Vec::new();
4949
with_no_trimmed_paths!({
@@ -305,19 +305,10 @@ fn codegen_fn_body(fx: &mut FunctionCx<'_, '_, '_>, start_block: Block) {
305305
.generic_activity("codegen prelude")
306306
.run(|| crate::abi::codegen_fn_prelude(fx, start_block));
307307

308-
let reachable_blocks = traversal::mono_reachable_as_bitset(fx.mir, fx.tcx, fx.instance);
309-
310308
for (bb, bb_data) in fx.mir.basic_blocks.iter_enumerated() {
311309
let block = fx.get_block(bb);
312310
fx.bcx.switch_to_block(block);
313311

314-
if !reachable_blocks.contains(bb) {
315-
// We want to skip this block, because it's not reachable. But we still create
316-
// the block so terminators in other blocks can reference it.
317-
fx.bcx.ins().trap(TrapCode::user(1 /* unreachable */).unwrap());
318-
continue;
319-
}
320-
321312
if bb_data.is_cleanup {
322313
// Unwinding after panicking is not supported
323314
continue;

compiler/rustc_codegen_ssa/src/base.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ pub(crate) fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
412412
// release builds.
413413
info!("codegen_instance({})", instance);
414414

415-
mir::codegen_mir::<Bx>(cx, instance);
415+
mir::lower_mir::<Bx>(cx, instance);
416416
}
417417

418418
/// Creates the `main` function which will initialize the rust runtime and call

compiler/rustc_codegen_ssa/src/mir/block.rs

-10
Original file line numberDiff line numberDiff line change
@@ -1279,16 +1279,6 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
12791279
}
12801280
}
12811281

1282-
pub(crate) fn codegen_block_as_unreachable(&mut self, bb: mir::BasicBlock) {
1283-
let llbb = match self.try_llbb(bb) {
1284-
Some(llbb) => llbb,
1285-
None => return,
1286-
};
1287-
let bx = &mut Bx::build(self.cx, llbb);
1288-
debug!("codegen_block_as_unreachable({:?})", bb);
1289-
bx.unreachable();
1290-
}
1291-
12921282
fn codegen_terminator(
12931283
&mut self,
12941284
bx: &mut Bx,

compiler/rustc_codegen_ssa/src/mir/mod.rs

+6-21
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_index::IndexVec;
44
use rustc_index::bit_set::DenseBitSet;
55
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
66
use rustc_middle::mir::{UnwindTerminateReason, traversal};
7-
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, HasTypingEnv, TyAndLayout};
7+
use rustc_middle::ty::layout::{FnAbiOf, HasTyCtxt, TyAndLayout};
88
use rustc_middle::ty::{self, Instance, Ty, TyCtxt, TypeFoldable, TypeVisitableExt};
99
use rustc_middle::{bug, mir, span_bug};
1010
use rustc_target::callconv::{FnAbi, PassMode};
@@ -126,12 +126,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
126126
where
127127
T: Copy + TypeFoldable<TyCtxt<'tcx>>,
128128
{
129-
debug!("monomorphize: self.instance={:?}", self.instance);
130-
self.instance.instantiate_mir_and_normalize_erasing_regions(
131-
self.cx.tcx(),
132-
self.cx.typing_env(),
133-
ty::EarlyBinder::bind(value),
134-
)
129+
value
135130
}
136131
}
137132

@@ -164,15 +159,15 @@ impl<'tcx, V: CodegenObject> LocalRef<'tcx, V> {
164159
///////////////////////////////////////////////////////////////////////////
165160

166161
#[instrument(level = "debug", skip(cx))]
167-
pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
162+
pub fn lower_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
168163
cx: &'a Bx::CodegenCx,
169164
instance: Instance<'tcx>,
170165
) {
171166
assert!(!instance.args.has_infer());
172167

173168
let llfn = cx.get_fn(instance);
174169

175-
let mir = cx.tcx().instance_mir(instance.def);
170+
let mir = cx.tcx().codegen_mir(instance);
176171

177172
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
178173
debug!("fn_abi: {:?}", fn_abi);
@@ -233,7 +228,8 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
233228
fx.compute_per_local_var_debug_info(&mut start_bx).unzip();
234229
fx.per_local_var_debug_info = per_local_var_debug_info;
235230

236-
let traversal_order = traversal::mono_reachable_reverse_postorder(mir, cx.tcx(), instance);
231+
let traversal_order: Vec<_> =
232+
traversal::reverse_postorder(mir).map(|(block, _data)| block).collect();
237233
let memory_locals = analyze::non_ssa_locals(&fx, &traversal_order);
238234

239235
// Allocate variable and temp allocas
@@ -293,20 +289,9 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
293289
// So drop the builder of `start_llbb` to avoid having two at the same time.
294290
drop(start_bx);
295291

296-
let mut unreached_blocks = DenseBitSet::new_filled(mir.basic_blocks.len());
297292
// Codegen the body of each reachable block using our reverse postorder list.
298293
for bb in traversal_order {
299294
fx.codegen_block(bb);
300-
unreached_blocks.remove(bb);
301-
}
302-
303-
// FIXME: These empty unreachable blocks are *mostly* a waste. They are occasionally
304-
// targets for a SwitchInt terminator, but the reimplementation of the mono-reachable
305-
// simplification in SwitchInt lowering sometimes misses cases that
306-
// mono_reachable_reverse_postorder manages to figure out.
307-
// The solution is to do something like post-mono GVN. But for now we have this hack.
308-
for bb in unreached_blocks.iter() {
309-
fx.codegen_block_as_unreachable(bb);
310295
}
311296
}
312297

compiler/rustc_middle/src/mir/basic_blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ impl<'tcx> BasicBlocks<'tcx> {
7979
#[inline]
8080
pub fn reverse_postorder(&self) -> &[BasicBlock] {
8181
self.cache.reverse_postorder.get_or_init(|| {
82-
let mut rpo: Vec<_> = Postorder::new(&self.basic_blocks, START_BLOCK, None).collect();
82+
let mut rpo: Vec<_> = Postorder::new(&self.basic_blocks, START_BLOCK).collect();
8383
rpo.reverse();
8484
rpo
8585
})

compiler/rustc_middle/src/mir/mod.rs

+1-82
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use crate::ty::codec::{TyDecoder, TyEncoder};
3535
use crate::ty::print::{FmtPrinter, Printer, pretty_print_const, with_no_trimmed_paths};
3636
use crate::ty::visit::TypeVisitableExt;
3737
use crate::ty::{
38-
self, AdtDef, GenericArg, GenericArgsRef, Instance, InstanceKind, List, Ty, TyCtxt, TypingEnv,
38+
self, AdtDef, GenericArg, GenericArgsRef, InstanceKind, List, Ty, TyCtxt, TypingEnv,
3939
UserTypeAnnotationIndex,
4040
};
4141

@@ -598,74 +598,6 @@ impl<'tcx> Body<'tcx> {
598598
self.injection_phase.is_some()
599599
}
600600

601-
/// If this basic block ends with a [`TerminatorKind::SwitchInt`] for which we can evaluate the
602-
/// discriminant in monomorphization, we return the discriminant bits and the
603-
/// [`SwitchTargets`], just so the caller doesn't also have to match on the terminator.
604-
fn try_const_mono_switchint<'a>(
605-
tcx: TyCtxt<'tcx>,
606-
instance: Instance<'tcx>,
607-
block: &'a BasicBlockData<'tcx>,
608-
) -> Option<(u128, &'a SwitchTargets)> {
609-
// There are two places here we need to evaluate a constant.
610-
let eval_mono_const = |constant: &ConstOperand<'tcx>| {
611-
// FIXME(#132279): what is this, why are we using an empty environment here.
612-
let typing_env = ty::TypingEnv::fully_monomorphized();
613-
let mono_literal = instance.instantiate_mir_and_normalize_erasing_regions(
614-
tcx,
615-
typing_env,
616-
crate::ty::EarlyBinder::bind(constant.const_),
617-
);
618-
mono_literal.try_eval_bits(tcx, typing_env)
619-
};
620-
621-
let TerminatorKind::SwitchInt { discr, targets } = &block.terminator().kind else {
622-
return None;
623-
};
624-
625-
// If this is a SwitchInt(const _), then we can just evaluate the constant and return.
626-
let discr = match discr {
627-
Operand::Constant(constant) => {
628-
let bits = eval_mono_const(constant)?;
629-
return Some((bits, targets));
630-
}
631-
Operand::Move(place) | Operand::Copy(place) => place,
632-
};
633-
634-
// MIR for `if false` actually looks like this:
635-
// _1 = const _
636-
// SwitchInt(_1)
637-
//
638-
// And MIR for if intrinsics::ub_checks() looks like this:
639-
// _1 = UbChecks()
640-
// SwitchInt(_1)
641-
//
642-
// So we're going to try to recognize this pattern.
643-
//
644-
// If we have a SwitchInt on a non-const place, we find the most recent statement that
645-
// isn't a storage marker. If that statement is an assignment of a const to our
646-
// discriminant place, we evaluate and return the const, as if we've const-propagated it
647-
// into the SwitchInt.
648-
649-
let last_stmt = block.statements.iter().rev().find(|stmt| {
650-
!matches!(stmt.kind, StatementKind::StorageDead(_) | StatementKind::StorageLive(_))
651-
})?;
652-
653-
let (place, rvalue) = last_stmt.kind.as_assign()?;
654-
655-
if discr != place {
656-
return None;
657-
}
658-
659-
match rvalue {
660-
Rvalue::NullaryOp(NullOp::UbChecks, _) => Some((tcx.sess.ub_checks() as u128, targets)),
661-
Rvalue::Use(Operand::Constant(constant)) => {
662-
let bits = eval_mono_const(constant)?;
663-
Some((bits, targets))
664-
}
665-
_ => None,
666-
}
667-
}
668-
669601
/// For a `Location` in this scope, determine what the "caller location" at that point is. This
670602
/// is interesting because of inlining: the `#[track_caller]` attribute of inlined functions
671603
/// must be honored. Falls back to the `tracked_caller` value for `#[track_caller]` functions,
@@ -1346,19 +1278,6 @@ impl<'tcx> BasicBlockData<'tcx> {
13461278
pub fn is_empty_unreachable(&self) -> bool {
13471279
self.statements.is_empty() && matches!(self.terminator().kind, TerminatorKind::Unreachable)
13481280
}
1349-
1350-
/// Like [`Terminator::successors`] but tries to use information available from the [`Instance`]
1351-
/// to skip successors like the `false` side of an `if const {`.
1352-
///
1353-
/// This is used to implement [`traversal::mono_reachable`] and
1354-
/// [`traversal::mono_reachable_reverse_postorder`].
1355-
pub fn mono_successors(&self, tcx: TyCtxt<'tcx>, instance: Instance<'tcx>) -> Successors<'_> {
1356-
if let Some((bits, targets)) = Body::try_const_mono_switchint(tcx, instance, self) {
1357-
targets.successors_for_value(bits)
1358-
} else {
1359-
self.terminator().successors()
1360-
}
1361-
}
13621281
}
13631282

13641283
///////////////////////////////////////////////////////////////////////////

compiler/rustc_middle/src/mir/traversal.rs

+1-111
Original file line numberDiff line numberDiff line change
@@ -109,22 +109,18 @@ pub struct Postorder<'a, 'tcx> {
109109
visited: DenseBitSet<BasicBlock>,
110110
visit_stack: Vec<(BasicBlock, Successors<'a>)>,
111111
root_is_start_block: bool,
112-
/// A non-empty `extra` allows for a precise calculation of the successors.
113-
extra: Option<(TyCtxt<'tcx>, Instance<'tcx>)>,
114112
}
115113

116114
impl<'a, 'tcx> Postorder<'a, 'tcx> {
117115
pub fn new(
118116
basic_blocks: &'a IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
119117
root: BasicBlock,
120-
extra: Option<(TyCtxt<'tcx>, Instance<'tcx>)>,
121118
) -> Postorder<'a, 'tcx> {
122119
let mut po = Postorder {
123120
basic_blocks,
124121
visited: DenseBitSet::new_empty(basic_blocks.len()),
125122
visit_stack: Vec::new(),
126123
root_is_start_block: root == START_BLOCK,
127-
extra,
128124
};
129125

130126
po.visit(root);
@@ -138,11 +134,7 @@ impl<'a, 'tcx> Postorder<'a, 'tcx> {
138134
return;
139135
}
140136
let data = &self.basic_blocks[bb];
141-
let successors = if let Some(extra) = self.extra {
142-
data.mono_successors(extra.0, extra.1)
143-
} else {
144-
data.terminator().successors()
145-
};
137+
let successors = data.terminator().successors();
146138
self.visit_stack.push((bb, successors));
147139
}
148140

@@ -240,20 +232,6 @@ pub fn postorder<'a, 'tcx>(
240232
reverse_postorder(body).rev()
241233
}
242234

243-
pub fn mono_reachable_reverse_postorder<'a, 'tcx>(
244-
body: &'a Body<'tcx>,
245-
tcx: TyCtxt<'tcx>,
246-
instance: Instance<'tcx>,
247-
) -> Vec<BasicBlock> {
248-
let mut iter = Postorder::new(&body.basic_blocks, START_BLOCK, Some((tcx, instance)));
249-
let mut items = Vec::with_capacity(body.basic_blocks.len());
250-
while let Some(block) = iter.next() {
251-
items.push(block);
252-
}
253-
items.reverse();
254-
items
255-
}
256-
257235
/// Returns an iterator over all basic blocks reachable from the `START_BLOCK` in no particular
258236
/// order.
259237
///
@@ -301,91 +279,3 @@ pub fn reverse_postorder<'a, 'tcx>(
301279
{
302280
body.basic_blocks.reverse_postorder().iter().map(|&bb| (bb, &body.basic_blocks[bb]))
303281
}
304-
305-
/// Traversal of a [`Body`] that tries to avoid unreachable blocks in a monomorphized [`Instance`].
306-
///
307-
/// This is allowed to have false positives; blocks may be visited even if they are not actually
308-
/// reachable.
309-
///
310-
/// Such a traversal is mostly useful because it lets us skip lowering the `false` side
311-
/// of `if <T as Trait>::CONST`, as well as [`NullOp::UbChecks`].
312-
///
313-
/// [`NullOp::UbChecks`]: rustc_middle::mir::NullOp::UbChecks
314-
pub fn mono_reachable<'a, 'tcx>(
315-
body: &'a Body<'tcx>,
316-
tcx: TyCtxt<'tcx>,
317-
instance: Instance<'tcx>,
318-
) -> MonoReachable<'a, 'tcx> {
319-
MonoReachable::new(body, tcx, instance)
320-
}
321-
322-
/// [`MonoReachable`] internally accumulates a [`DenseBitSet`] of visited blocks. This is just a
323-
/// convenience function to run that traversal then extract its set of reached blocks.
324-
pub fn mono_reachable_as_bitset<'a, 'tcx>(
325-
body: &'a Body<'tcx>,
326-
tcx: TyCtxt<'tcx>,
327-
instance: Instance<'tcx>,
328-
) -> DenseBitSet<BasicBlock> {
329-
let mut iter = mono_reachable(body, tcx, instance);
330-
while let Some(_) = iter.next() {}
331-
iter.visited
332-
}
333-
334-
pub struct MonoReachable<'a, 'tcx> {
335-
body: &'a Body<'tcx>,
336-
tcx: TyCtxt<'tcx>,
337-
instance: Instance<'tcx>,
338-
visited: DenseBitSet<BasicBlock>,
339-
// Other traversers track their worklist in a Vec. But we don't care about order, so we can
340-
// store ours in a DenseBitSet and thus save allocations because DenseBitSet has a small size
341-
// optimization.
342-
worklist: DenseBitSet<BasicBlock>,
343-
}
344-
345-
impl<'a, 'tcx> MonoReachable<'a, 'tcx> {
346-
pub fn new(
347-
body: &'a Body<'tcx>,
348-
tcx: TyCtxt<'tcx>,
349-
instance: Instance<'tcx>,
350-
) -> MonoReachable<'a, 'tcx> {
351-
let mut worklist = DenseBitSet::new_empty(body.basic_blocks.len());
352-
worklist.insert(START_BLOCK);
353-
MonoReachable {
354-
body,
355-
tcx,
356-
instance,
357-
visited: DenseBitSet::new_empty(body.basic_blocks.len()),
358-
worklist,
359-
}
360-
}
361-
362-
fn add_work(&mut self, blocks: impl IntoIterator<Item = BasicBlock>) {
363-
for block in blocks.into_iter() {
364-
if !self.visited.contains(block) {
365-
self.worklist.insert(block);
366-
}
367-
}
368-
}
369-
}
370-
371-
impl<'a, 'tcx> Iterator for MonoReachable<'a, 'tcx> {
372-
type Item = (BasicBlock, &'a BasicBlockData<'tcx>);
373-
374-
fn next(&mut self) -> Option<(BasicBlock, &'a BasicBlockData<'tcx>)> {
375-
while let Some(idx) = self.worklist.iter().next() {
376-
self.worklist.remove(idx);
377-
if !self.visited.insert(idx) {
378-
continue;
379-
}
380-
381-
let data = &self.body[idx];
382-
383-
let targets = data.mono_successors(self.tcx, self.instance);
384-
self.add_work(targets);
385-
386-
return Some((idx, data));
387-
}
388-
389-
None
390-
}
391-
}

0 commit comments

Comments
 (0)