Skip to content

Commit 85a8324

Browse files
committed
Querify
1 parent e287ddb commit 85a8324

File tree

6 files changed

+46
-14
lines changed

6 files changed

+46
-14
lines changed

compiler/rustc_codegen_ssa/src/mir/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -257,11 +257,11 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
257257
// Apply debuginfo to the newly allocated locals.
258258
fx.debug_introduce_locals(&mut start_bx);
259259

260-
let reachable_blocks = traversal::reachable_blocks_in_mono(mir, cx.tcx(), instance);
261-
262260
// The builders will be created separately for each basic block at `codegen_block`.
263261
// So drop the builder of `start_llbb` to avoid having two at the same time.
264262
drop(start_bx);
263+
264+
let reachable_blocks = cx.tcx().reachable_blocks(instance);
265265

266266
// Codegen the body of each block using reverse postorder
267267
for (bb, _) in traversal::reverse_postorder(mir) {

compiler/rustc_interface/src/passes.rs

+1
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,7 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
631631
rustc_lint::provide(providers);
632632
rustc_symbol_mangling::provide(providers);
633633
rustc_codegen_ssa::provide(providers);
634+
rustc_middle::mir::traversal::provide(providers);
634635
*providers
635636
});
636637

compiler/rustc_middle/src/mir/traversal.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
use crate::query::Providers;
2+
13
use super::*;
24

35
/// Preorder traversal of a graph.
4-
///
56
/// Preorder traversal is when each node is visited after at least one of its predecessors. If you
67
/// are familiar with some basic graph theory, then this performs a depth first search and returns
78
/// nodes in order of discovery time.
@@ -280,8 +281,7 @@ pub fn reverse_postorder<'a, 'tcx>(
280281
body.basic_blocks.reverse_postorder().iter().map(|&bb| (bb, &body.basic_blocks[bb]))
281282
}
282283

283-
/// Finds which basic blocks are actually reachable for a specific
284-
/// monomorphization of this body.
284+
/// Finds which basic blocks are actually reachable for a monomorphized [`Instance`].
285285
///
286286
/// This is allowed to have false positives; just because this says a block
287287
/// is reachable doesn't mean that's necessarily true. It's thus always
@@ -292,12 +292,12 @@ pub fn reverse_postorder<'a, 'tcx>(
292292
/// checks can be done without worrying about panicking.
293293
///
294294
/// This is mostly useful because it lets us skip lowering the `false` side
295-
/// of `if <T as Trait>::CONST`, as well as `intrinsics::debug_assertions`.
296-
pub fn reachable_blocks_in_mono<'tcx>(
297-
body: &Body<'tcx>,
295+
/// of `if <T as Trait>::CONST`, as well as [`intrinsics::ub_checks`].
296+
fn reachable_blocks<'tcx>(
298297
tcx: TyCtxt<'tcx>,
299298
instance: Instance<'tcx>,
300299
) -> BitSet<BasicBlock> {
300+
let body = tcx.instance_mir(instance.def);
301301
let mut visitor = MonoReachable {
302302
body,
303303
tcx,
@@ -348,3 +348,7 @@ impl<'a, 'tcx> MonoReachable<'a, 'tcx> {
348348
}
349349
}
350350
}
351+
352+
pub fn provide(providers: &mut Providers) {
353+
providers.reachable_blocks = reachable_blocks;
354+
}

compiler/rustc_middle/src/query/keys.rs

+17
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,15 @@ impl<'tcx> Key for ty::Instance<'tcx> {
8787
}
8888
}
8989

90+
impl<'tcx> AsLocalKey for ty::Instance<'tcx> {
91+
type LocalKey = Self;
92+
93+
#[inline(always)]
94+
fn as_local_key(&self) -> Option<Self::LocalKey> {
95+
self.def_id().is_local().then(|| *self)
96+
}
97+
}
98+
9099
impl<'tcx> Key for mir::interpret::GlobalId<'tcx> {
91100
type Cache<V> = DefaultCache<Self, V>;
92101

@@ -534,6 +543,14 @@ impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx ty::List<Ty<'tcx>>) {
534543
}
535544
}
536545

546+
impl<'tcx> Key for (ty::Instance<'tcx>, &'tcx mir::Body<'tcx>) {
547+
type Cache<V> = DefaultCache<Self, V>;
548+
549+
fn default_span(&self, tcx: TyCtxt<'_>) -> Span {
550+
self.0.default_span(tcx)
551+
}
552+
}
553+
537554
impl<'tcx> Key for (Ty<'tcx>, ty::ValTree<'tcx>) {
538555
type Cache<V> = DefaultCache<Self, V>;
539556

compiler/rustc_middle/src/query/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ use rustc_hir::def_id::{
7171
};
7272
use rustc_hir::lang_items::{LangItem, LanguageItems};
7373
use rustc_hir::{Crate, ItemLocalId, ItemLocalMap, TraitCandidate};
74+
use rustc_index::bit_set::BitSet;
7475
use rustc_index::IndexVec;
7576
use rustc_query_system::ich::StableHashingContext;
7677
use rustc_query_system::query::{try_get_cached, QueryCache, QueryMode, QueryState};
@@ -270,7 +271,7 @@ rustc_queries! {
270271
feedable
271272
}
272273

273-
query unsizing_params_for_adt(key: DefId) -> &'tcx rustc_index::bit_set::BitSet<u32>
274+
query unsizing_params_for_adt(key: DefId) -> &'tcx BitSet<u32>
274275
{
275276
arena_cache
276277
desc { |tcx|
@@ -460,7 +461,7 @@ rustc_queries! {
460461
}
461462

462463
/// Set of param indexes for type params that are in the type's representation
463-
query params_in_repr(key: DefId) -> &'tcx rustc_index::bit_set::BitSet<u32> {
464+
query params_in_repr(key: DefId) -> &'tcx BitSet<u32> {
464465
desc { "finding type parameters in the representation" }
465466
arena_cache
466467
no_hash
@@ -2251,6 +2252,11 @@ rustc_queries! {
22512252
query find_field((def_id, ident): (DefId, rustc_span::symbol::Ident)) -> Option<rustc_target::abi::FieldIdx> {
22522253
desc { |tcx| "find the index of maybe nested field `{ident}` in `{}`", tcx.def_path_str(def_id) }
22532254
}
2255+
2256+
query reachable_blocks(instance: ty::Instance<'tcx>) -> &'tcx BitSet<mir::BasicBlock> {
2257+
arena_cache
2258+
desc { |tcx| "determining reachable blocks in `{}`", tcx.def_path_str(instance.def_id()) }
2259+
}
22542260
}
22552261

22562262
rustc_query_append! { define_callbacks! }

compiler/rustc_monomorphize/src/collector.rs

+8-4
Original file line numberDiff line numberDiff line change
@@ -672,7 +672,7 @@ struct MirUsedCollector<'a, 'tcx> {
672672
visiting_call_terminator: bool,
673673
/// Set of functions for which it is OK to move large data into.
674674
skip_move_check_fns: Option<Vec<DefId>>,
675-
reachable_blocks: BitSet<mir::BasicBlock>,
675+
reachable_blocks: Option<&'tcx BitSet<mir::BasicBlock>>,
676676
}
677677

678678
impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
@@ -834,7 +834,11 @@ impl<'a, 'tcx> MirUsedCollector<'a, 'tcx> {
834834

835835
impl<'a, 'tcx> MirVisitor<'tcx> for MirUsedCollector<'a, 'tcx> {
836836
fn visit_basic_block_data(&mut self, block: mir::BasicBlock, data: &mir::BasicBlockData<'tcx>) {
837-
if self.reachable_blocks.contains(block) {
837+
if self
838+
.reachable_blocks
839+
.expect("we should only walk blocks with CollectionMode::UsedItems")
840+
.contains(block)
841+
{
838842
self.super_basic_block_data(block, data)
839843
}
840844
}
@@ -1420,9 +1424,9 @@ fn collect_items_of_instance<'tcx>(
14201424
visiting_call_terminator: false,
14211425
skip_move_check_fns: None,
14221426
reachable_blocks: if mode == CollectionMode::UsedItems {
1423-
mir::traversal::reachable_blocks_in_mono(body, tcx, instance)
1427+
Some(tcx.reachable_blocks(instance))
14241428
} else {
1425-
BitSet::new_filled(body.basic_blocks.len())
1429+
None
14261430
},
14271431
};
14281432

0 commit comments

Comments
 (0)