gfx opened PR #12483 from wado-lang:gfx/branch_hinting to bytecodealliance:main:
This PR adds support for the WebAssembly branch hinting proposal by parsing the
metadata.code.branch_hintcustom section and using the hints to mark cold blocks during Cranelift code generation.Implementation details:
- Parse branch hints in
ModuleTranslationfrom the custom section using wasmparser'sKnownCustom::BranchHintsreader- Store hints as a map from function index to (offset, taken) pairs
- Add
get_branch_hint()helper inFuncEnvironmentto look up hints by converting absolute byte offsets to function-relative offsets- Apply hints in
translate_br_if(): when a branch is marked unlikely (taken=false), mark the branch target as cold; when marked likely (taken=true), mark the fallthrough block as coldBranch hints are always parsed and applied when present in a module; no configuration flag is required.
A disassembly test is included to verify cold block annotations, but official WebAssembly spec tests for this proposal are not yet available.
Closes #9463
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
gfx requested wasmtime-compiler-reviewers for a review on PR #12483.
gfx requested fitzgen for a review on PR #12483.
gfx requested wasmtime-core-reviewers for a review on PR #12483.
gfx requested wasmtime-default-reviewers for a review on PR #12483.
gfx edited PR #12483:
This PR adds support for the WebAssembly branch hinting proposal by parsing the
metadata.code.branch_hintcustom section and using the hints to mark cold blocks during Cranelift code generation.Implementation details:
- Parse branch hints in
ModuleTranslationfrom the custom section using wasmparser'sKnownCustom::BranchHintsreader- Store hints as a map from function index to (offset, taken) pairs
- Add
get_branch_hint()helper inFuncEnvironmentto look up hints by converting absolute byte offsets to function-relative offsets- Apply hints in
translate_br_if(): when a branch is marked unlikely (taken=false), mark the branch target as cold; when marked likely (taken=true), mark the fallthrough block as coldBranch hints are always parsed and applied when present in a module; no configuration flag is required.
A disassembly test is included to verify cold block annotations, but official WebAssembly spec tests for this proposal are not yet available.
Closes #9463
Question: should I add a knob to enable/disable this feature? Currently, this is automatically turned on. I'm not sure I should add an option for it.
<!--
Please make sure you include the following information:
If this work has been discussed elsewhere, please include a link to that
conversation. If it was discussed in an issue, just mention "issue #...".Explain why this change is needed. If the details are in an issue already,
this can be brief.Our development process is documented in the Wasmtime book:
https://docs.wasmtime.dev/contributing-development-process.htmlPlease ensure all communication follows the code of conduct:
https://github.com/bytecodealliance/wasmtime/blob/main/CODE_OF_CONDUCT.md
-->
github-actions[bot] added the label wasmtime:docs on PR #12483.
alexcrichton submitted PR review.
alexcrichton created PR review comment:
This loop I think might be good to optimize in terms of translation will only lookup branch hints (I think?) in increasing order of offsets. That means that currently this code is an $O(n^2)$ loop as-implemented. That could be optimized by using a binary search here, but I think this could go one step further and, ideally, store a reference to the raw buffer of input wasm data (in theory) here. For example this could be a
.peekable()iterator over the raw wasm itself. Looking up viaget_branch_hintwould advance the iterator if it's at the matching position.Regardless I think it'll be needed to remove the quadratic behavior here, and I think ideally it'd be via an iterator-like approach to avoid the logarithmic nature of a binary search.
fitzgen submitted PR review:
Question: should I add a knob to enable/disable this feature? Currently, this is automatically turned on. I'm not sure I should add an option for it.
Yes, I think we should have a config knob for this.
And to follow our rules by the letter, we we shouldn't enable it by default until it is fuzzed (and has been fuzzed for a couple weeks). This would most likely involve adding support to
wasm-smithfor emitting branch hints.
fitzgen submitted PR review:
Question: should I add a knob to enable/disable this feature? Currently, this is automatically turned on. I'm not sure I should add an option for it.
Yes, I think we should have a config knob for this.
And to follow our rules by the letter, we we shouldn't enable it by default until it is fuzzed (and has been fuzzed for a couple weeks). This would most likely involve adding support to
wasm-smithfor emitting branch hints.
gfx updated PR #12483.
gfx submitted PR review.
gfx created PR review comment:
Ah, you are right.
How about bc62088d6f7b31712badb427fd37e18c3c410a95 for O(n) complexity?
cfallin submitted PR review.
cfallin created PR review comment:
What Alex is getting at, i think, is that the spec states that the hints are given in PC order. So why not traverse hints in order, taking them when they match as we visit increasing PC offsets? We should be able to that in O(n) time with O(1) space overhead, i.e., not building a HashMap at all.
cfallin edited PR review comment.
AlbertMarashi commented on PR #12483:
How nice, I was just going to start using this - great to see it's being worked on!
Last updated: Feb 24 2026 at 04:36 UTC