Skip to content

[Branch Hinting] Add branch hint handling in RemoveUnusedBrs #7706

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

Merged
merged 5 commits into from
Jul 9, 2025

Conversation

kripken
Copy link
Member

@kripken kripken commented Jul 9, 2025

Add some utilities for easily updating/copying/clearing branch hints, then use
those in the pass.

As a drive-by, move flip() from wasm-builder.h, which everyone was including
but only this one cpp file was using, and update it in the new location (this
avoids including the new branch hints header in a central place).

Written manually then verified by fuzzing with #7704

@kripken kripken requested a review from tlively July 9, 2025 20:02
if (!likely) {
// We are writing an empty hint. Do not create an empty annotation if one
// did not exist.
if (!func->codeAnnotations.count(expr)) {
Copy link
Member

Choose a reason for hiding this comment

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

This could be combined into the previous condition with &&.

Comment on lines +102 to +106
// As |applyAndTo|, but now the condition on |to| the OR of |from1| and |from2|.
inline void applyOrTo(Expression* from1,
Expression* from2,
Expression* to,
Function* func) {
Copy link
Member

Choose a reason for hiding this comment

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

From the name and doc comment, I would have expected this to set to as likely if from1 is likely or from2 is likely. I would not have expected it to reason about branch probabilities at all. Can we move this reasoning up into the callers so we don't have such a misleading method name?

Copy link
Member Author

Choose a reason for hiding this comment

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

Moving the logic into the callers would require duplicating the large internal comment - it is not trivial how to handle this, so a helper seems natural?

How about applyCombinedOrConditionTo?

ExpressionManipulator::nop(br);
BranchHints::clear(br, getFunction());
Copy link
Member

Choose a reason for hiding this comment

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

What happens if we are left with a branch hint on a nop?

Copy link
Member Author

Choose a reason for hiding this comment

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

The binary reader and writer ignore such things, but the text printer is simpler and will print it. This just avoids it being printed out.

(func $join-br_ifs-no (param $x i32) (param $y i32)
;; One is missing a hint, so we clear the hint.
(block $out
(@metadata.code.branch_hint "\00")
Copy link
Member

Choose a reason for hiding this comment

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

If this hint were a 1, it would make sense to keep, but I don't think we would currently keep it.

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point, this really should be handled. Done (and other feedback too).

(br_if $out
(local.get $x)
)
(@metadata.code.branch_hint "\00")
Copy link
Member

Choose a reason for hiding this comment

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

Similarly if this were a 1.

Comment on lines +165 to +169
(@metadata.code.branch_hint "\00")
(br_if $out
(local.get $x)
)
(@metadata.code.branch_hint "\01")
Copy link
Member

Choose a reason for hiding this comment

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

If $x is unlikely but $y is likely, then $x or $y should still be likely, so it seems best to keep the likely hint in this case.

(if
(local.get $x)
(then
(br_if $out
Copy link
Member

Choose a reason for hiding this comment

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

It's probably worth testing the case when only the br_if has the hint as well.

Co-authored-by: Thomas Lively <[email protected]>
@kripken kripken merged commit c3f7e2e into WebAssembly:main Jul 9, 2025
16 checks passed
@kripken kripken deleted the rubrs.fixes branch July 9, 2025 23:51
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