Skip to content

merge blocks optimization - #35

Merged
cfallin merged 3 commits into
bytecodealliance:mainfrom
Firestar99:merge_blocks
Nov 17, 2026
Merged

cfallin merged 3 commits into
bytecodealliance:mainfrom
Firestar99:merge_blocks

Conversation

@Firestar99

@Firestar99 Firestar99 commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

I've found that even simple rust programs such as:

#[unsafe(no_mangle)]
pub extern "C" fn bla(i: i32) -> i32 {
    (i - 2) * 5 + 2
}

compiled to wasm32-unknown-unknown and parsed by waffle contains multiple basic blocks:

function(i32) -> i32 {
  block0(v1: i32): # 
    # preds: 
    # succs: block1 ()
    v2 = i32const<5>  # i32  
    v3 = i32mul v1, v2 # i32  
    v4 = i32const<4294967288>  # i32  
    v5 = i32add v3, v4 # i32  
    br block1(v5)
  block1(v0: i32): # 
    # preds: block0 ()
    # succs: 
    return v0
}

I'm building a new codegen backend based on waffle, and it's a lot easier if I could just focus on basic instructions and ignore control flow for now. So I've made an optimization that merges waffle's bbs even in situations where empty_blocks bails out.

With this PR, the function above turns into:

function(i32) -> i32 {
  block0(v1: i32): # 
    # preds: 
    # succs: 
    v2 = i32const<5>  # i32  
    v3 = i32mul v1, v2 # i32  
    v4 = i32const<4294967288>  # i32  
    v5 = i32add v3, v4 # i32  
    return v0
  block1(): # 
    # preds: 
    # succs: 
    no_terminator
}

(Note that standard display does not show the v0 = v5 alias...)

For more examples, see the tests added by the first commit, which are modified by the second commit adding the merge_blocks opt. I've made use of rust-analyzer's expect-test crate so updating the output is trivial with UPDATE_EXPECT=1 env var. Also survives fuzzing for a few minutes with #34 (before dying on a parser unwrap).

Firestar99 added a commit to Firestar99/rust-gpu2 that referenced this pull request Nov 16, 2026

@cfallin cfallin left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks! One comment below but otherwise LGTM.

Comment thread src/passes/merge_blocks.rs Outdated
assert_eq!(succ.params.len(), target.args.len());
for (&(_, from), &to) in succ.params.iter().zip(target.args.iter()) {
body.values[from] = ValueDef::Alias(to);
body.value_blocks[from] = pred_id;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think this is correct: the argument passed to the blockparam may not be actually computed in the predecessor (it could be defined in any dominating block), right? I think we can copy it from value_blocks[to].

... actually I think we can use Function::set_alias. I also see that that doesn't update value_blocks (which was a later addition); please go ahead and update that as well while you're at it!

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

(This didn't trigger any failures locally within the crate because it is only a pub export to users, not used for any opt or codegen here; but we should keep it updated, and probably validate it too)

@Firestar99 Firestar99 Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Pushed a followup commit fixing the aliasing. Though I can't use body.set_alias(&mut self) there cause succ and target are borrowed from body, so I'll keep doing it manually to satisfy borrowck.

Firestar99 added a commit to Firestar99/rust-gpu2 that referenced this pull request Nov 17, 2026
@cfallin

cfallin commented Nov 17, 2026

Copy link
Copy Markdown
Member

Thanks!

@cfallin
cfallin merged commit d659fff into bytecodealliance:main Nov 17, 2026
3 checks passed
Firestar99 added a commit to Firestar99/rust-gpu2 that referenced this pull request Nov 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants