Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Simplify Postorder customization. #137299

Merged
merged 1 commit into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/mir/basic_blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ impl<'tcx> BasicBlocks<'tcx> {
#[inline]
pub fn reverse_postorder(&self) -> &[BasicBlock] {
self.cache.reverse_postorder.get_or_init(|| {
let mut rpo: Vec<_> = Postorder::new(&self.basic_blocks, START_BLOCK, ()).collect();
let mut rpo: Vec<_> = Postorder::new(&self.basic_blocks, START_BLOCK, None).collect();
rpo.reverse();
rpo
})
Expand Down
47 changes: 13 additions & 34 deletions compiler/rustc_middle/src/mir/traversal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,23 +104,21 @@ impl<'a, 'tcx> Iterator for Preorder<'a, 'tcx> {
/// ```
///
/// A Postorder traversal of this graph is `D B C A` or `D C B A`
pub struct Postorder<'a, 'tcx, C> {
pub struct Postorder<'a, 'tcx> {
basic_blocks: &'a IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
visited: DenseBitSet<BasicBlock>,
visit_stack: Vec<(BasicBlock, Successors<'a>)>,
root_is_start_block: bool,
extra: C,
/// A non-empty `extra` allows for a precise calculation of the successors.
extra: Option<(TyCtxt<'tcx>, Instance<'tcx>)>,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

preexisting: the variable name is a bit vague. would've been nice if it was called mono_-something.

}

impl<'a, 'tcx, C> Postorder<'a, 'tcx, C>
where
C: Customization<'tcx>,
{
impl<'a, 'tcx> Postorder<'a, 'tcx> {
pub fn new(
basic_blocks: &'a IndexSlice<BasicBlock, BasicBlockData<'tcx>>,
root: BasicBlock,
extra: C,
) -> Postorder<'a, 'tcx, C> {
extra: Option<(TyCtxt<'tcx>, Instance<'tcx>)>,
) -> Postorder<'a, 'tcx> {
let mut po = Postorder {
basic_blocks,
visited: DenseBitSet::new_empty(basic_blocks.len()),
Expand All @@ -140,7 +138,11 @@ where
return;
}
let data = &self.basic_blocks[bb];
let successors = C::successors(data, self.extra);
let successors = if let Some(extra) = self.extra {
data.mono_successors(extra.0, extra.1)
} else {
data.terminator().successors()
};
self.visit_stack.push((bb, successors));
}

Expand Down Expand Up @@ -198,10 +200,7 @@ where
}
}

impl<'tcx, C> Iterator for Postorder<'_, 'tcx, C>
where
C: Customization<'tcx>,
{
impl<'tcx> Iterator for Postorder<'_, 'tcx> {
type Item = BasicBlock;

fn next(&mut self) -> Option<BasicBlock> {
Expand Down Expand Up @@ -241,32 +240,12 @@ pub fn postorder<'a, 'tcx>(
reverse_postorder(body).rev()
}

/// Lets us plug in some additional logic and data into a Postorder traversal. Or not.
pub trait Customization<'tcx>: Copy {
fn successors<'a>(_: &'a BasicBlockData<'tcx>, _: Self) -> Successors<'a>;
}

impl<'tcx> Customization<'tcx> for () {
fn successors<'a>(data: &'a BasicBlockData<'tcx>, _: ()) -> Successors<'a> {
data.terminator().successors()
}
}

impl<'tcx> Customization<'tcx> for (TyCtxt<'tcx>, Instance<'tcx>) {
fn successors<'a>(
data: &'a BasicBlockData<'tcx>,
(tcx, instance): (TyCtxt<'tcx>, Instance<'tcx>),
) -> Successors<'a> {
data.mono_successors(tcx, instance)
}
}

pub fn mono_reachable_reverse_postorder<'a, 'tcx>(
body: &'a Body<'tcx>,
tcx: TyCtxt<'tcx>,
instance: Instance<'tcx>,
) -> Vec<BasicBlock> {
let mut iter = Postorder::new(&body.basic_blocks, START_BLOCK, (tcx, instance));
let mut iter = Postorder::new(&body.basic_blocks, START_BLOCK, Some((tcx, instance)));
let mut items = Vec::with_capacity(body.basic_blocks.len());
while let Some(block) = iter.next() {
items.push(block);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_utils/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub fn visit_local_usage(locals: &[Local], mir: &Body<'_>, location: Location) -
locals.len()
];

traversal::Postorder::new(&mir.basic_blocks, location.block, ())
traversal::Postorder::new(&mir.basic_blocks, location.block, None)
.collect::<Vec<_>>()
.into_iter()
.rev()
Expand Down
Loading