Skip to content

Commit

Permalink
Don't generate goto's for dropping immediates
Browse files Browse the repository at this point in the history
When "dropping" an immediate value such as an Int, we no longer generate
an explicit goto to connect the current and next basic block. By doing
so, the clean up pass simply removes the entire block if no other
instructions are in the block, preventing us from generating many basic
blocks that simply contain a goto to the next block.

While this doesn't really affect performance, it makes the LLVM IR
smaller and makes it easier to visualize the MIR IR.

Changelog: fixed
  • Loading branch information
yorickpeterse committed Apr 4, 2024
1 parent 4f87499 commit 2f320e5
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
5 changes: 2 additions & 3 deletions compiler/src/mir/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4407,9 +4407,8 @@ impl<'a> LowerMethod<'a> {
/// 2. Basic blocks that implicitly flow into another block are updated to end
/// with a goto to said block.
///
/// These changes make it a bit easier to generate code from a MIR graph, as the
/// order in which we process blocks (besides starting with the root block) no
/// longer matters. It also makes the graph less noisy when visualising it.
/// These changes make it easier to visualize the MIR code, and result in a
/// smaller IR to pass to LLVM.
pub(crate) fn clean_up_basic_blocks(mir: &mut Mir) {
for method in mir.methods.values_mut() {
let blocks = &method.body.blocks;
Expand Down
18 changes: 9 additions & 9 deletions compiler/src/mir/specialize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1128,7 +1128,7 @@ impl<'a, 'b, 'c> ExpandDrop<'a, 'b, 'c> {

match typ.shape(self.db, self.shapes) {
Shape::Int | Shape::Float | Shape::Nil | Shape::Boolean => {
self.ignore_value(block_id, after_id, loc);
self.ignore_value(block_id, after_id);
}
Shape::Mut | Shape::Ref => {
self.drop_reference(block_id, after_id, val, loc);
Expand All @@ -1137,21 +1137,21 @@ impl<'a, 'b, 'c> ExpandDrop<'a, 'b, 'c> {
self.drop_atomic(block_id, after_id, val, loc);
}
Shape::Owned if typ.is_permanent(self.db) => {
self.ignore_value(block_id, after_id, loc);
self.ignore_value(block_id, after_id);
}
Shape::Owned => {
self.drop_owned(block_id, after_id, val, ins.dropper, loc);
}
}
}

fn ignore_value(
&mut self,
before_id: BlockId,
after_id: BlockId,
location: LocationId,
) {
self.block_mut(before_id).goto(after_id, location);
fn ignore_value(&mut self, before_id: BlockId, after_id: BlockId) {
// We don't generate a goto() here because:
//
// 1. If there are other instructions in the current block, the cleanup
// phase connects the current and next block explicitly for us.
// 2. If the current block is empty, this prevents a redundant basic
// block that only contains a goto to the next block.
self.method.body.add_edge(before_id, after_id);
}

Expand Down

0 comments on commit 2f320e5

Please sign in to comment.