Skip to content

Fix folded branch hints - #2578

Merged
alexcrichton merged 3 commits into
bytecodealliance:mainfrom
vouillon:fix-folded-branch-hints
Jul 28, 2026
Merged

alexcrichton merged 3 commits into
bytecodealliance:mainfrom
vouillon:fix-folded-branch-hints

Conversation

@vouillon

Copy link
Copy Markdown
Contributor

This fixes the parsing and printing of branch hints, so that the hint is attached to the if and not to the local.get in the example below (from the test suite).

        (@metadata.code.branch_hint "\01")
        (if (result i32) (local.get 1)
          (then (call $dummy) (i32.const 9))
          (else (call $dummy) (i32.const 10))
        )

The form previously emitted (with the branch hint right before the then) is still accepted on parsing.

        (if (result i32) (local.get 1)
          (@metadata.code.branch_hint "\01")
          (then (call $dummy) (i32.const 9))
          (else (call $dummy) (i32.const 10))
        )

vouillon added 2 commits July 24, 2026 15:19
The branch hint annotation `(@metadata.code.branch_hint ...)` records the
instruction it applies to by index. This index was recorded eagerly as the
index of the next instruction to be pushed, which is correct for the flat
form (e.g. `... (@...) if`) but wrong for the folded form
(e.g. `(@...) (if (cond) ...)`): there the operands of the folded
instruction are pushed first and the head instruction (the `if` or
`br_if`) is pushed afterwards, so the hint ended up attached to the first
operand instead of the branch instruction.

Defer the index assignment instead: keep the parsed annotation pending and
attach it to the head instruction of the following (flat or folded) form
when that instruction is actually pushed. This also accepts the annotation
appearing inside the folded form, after the operands (e.g.
`(if (cond) (@...) (then ...))` or `(br_if ... (@...))`). Using both a
preceding and a trailing annotation on one folded instruction targets it
twice and is rejected as a duplicate, matching two adjacent annotations.
The folded printer emitted the `(@metadata.code.branch_hint ...)` annotation
inside the folded form: as a pseudo-operand between an `if`'s condition and
its `(then`, and after a `br_if`'s operand. The annotation applies to the
`if` / `br_if` instruction, so print it on its own line immediately before
that instruction instead, matching the flat printer and the natural input
form.
@vouillon
vouillon requested a review from a team as a code owner July 24, 2026 13:34
@vouillon
vouillon requested review from fitzgen and removed request for a team July 24, 2026 13:34

@fitzgen fitzgen 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.

Seems reasonable to me but I think @alexcrichton should probably take a look, since I've really only done minor work on the wast crate

@keithw

keithw commented Jul 24, 2026 •

Copy link
Copy Markdown
Contributor

Thank you for fixing this! Now that branch-hints is merged and there's a normative spec, I would be in favor making the wast or wasmparser crate stricter to match the reference interpreter, and be able to catch printing errors automatically in the future. This PR is being generous in having wast continue to support the previously printed (incorrect) syntax, but I would tend to think that's not necessary. E.g., the reference interpreter rejects all three of these:

(func
 (@metadata.code.branch_hint "\00")
)
$ ./wasm -u -ca -cr /tmp/f1.wat 
/tmp/f1.wat:2.2-2.29: custom annotation syntax error: @metadata.code.branch_hint annotation: invalid placement
(func
 (@metadata.code.branch_hint "\00")
 nop
)
$ ./wasm -ca -cr /tmp/f2.wat
/tmp/f2.wat:2.2-2.35: custom validation error: @metadata.code.branch_hint annotation: invalid target
(func
 (if (i32.const 0) (@metadata.code.branch_hint "\00")
  (then))
)
$ ./wasm -ca -cr /tmp/f3.wat
/tmp/f3.wat:5.7-5.34: custom annotation syntax error: @metadata.code.branch_hint annotation: invalid placement

Currently (pre and post this PR) wasm-tools accepts these as well-formed and valid. I think it probably should reject them too... and this will make it easier to catch future issues when it does the roundtrip tests.

@alexcrichton

Copy link
Copy Markdown
Member

Thanks! I agree with @keithw that there's no need to keep parsing the old syntax and it's ok to just break things here. I think there's code in wast trying to parse the older syntax and that can all be deleted.

I'll note though that @keithw syntax like:

(func
 (@metadata.code.branch_hint "\00")
 nop
)

will be relatively difficult to reject I think as that requires running the validator to determine that it's not actually tied to an if or br_if.

@vouillon
vouillon force-pushed the fix-folded-branch-hints branch from 6404f7e to ec1946a Compare July 28, 2026 09:13
A branch hint annotation must immediately precede the instruction it applies
to. Until now a hint placed inside a folded form (between an `if`'s condition
and its `(then`, or after a `br_if`'s operand) was accepted, and a dangling
annotation with no following instruction was silently dropped. Reject both:
an annotation that isn't consumed by an immediately following instruction is
now an error.
@vouillon
vouillon force-pushed the fix-folded-branch-hints branch from ec1946a to f4c5337 Compare July 28, 2026 09:30
@vouillon

Copy link
Copy Markdown
Contributor Author

I have updated the PR to reject misplaced annotations.
I'm not sure what to do about branch hints on non-branch instructions. Maybe it make more sense to handle that during validation. The test suite has a test which asserts that this is a validation error; not a parsing error..

@alexcrichton
alexcrichton added this pull request to the merge queue Jul 28, 2026
Merged via the queue into bytecodealliance:main with commit 373b5bd Jul 28, 2026
37 checks passed
social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
wasm-tools 1.255.0

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## What's Changed
* Fix generating deeply nested WIT types in `wit-smith` by @alexcrichton in bytecodealliance/wasm-tools#2576
* wasm-tools mutate: validate the input module before mutating by @SAY-5 in bytecodealliance/wasm-tools#2577
* link tag imports to exports instead of synthesizing tags by @dicej in bytecodealliance/wasm-tools#2580
* Fix folded branch hints by @vouillon in bytecodealliance/wasm-tools#2578
* support cooperative multithreading ABI in `wit_component::linking` by @dicej in bytecodealliance/wasm-tools#2581
* avoid calling `__wasm_init_task` more than once in `init` module by @dicej in bytecodealliance/wasm-tools#2582
* Support `__wasm_{g,s}et_{tls_base,stack_pointer}` in `wit-component` by @alexcrichton in bytecodealliance/wasm-tools#2584
* fix table resolution for `thread.new-indirect` intrinsic calls by @dicej in bytecodealliance/wasm-tools#2585
* Release wasm-tools 1.255.0 by @github-actions[bot] in bytecodealliance/wasm-tools#2586


**Full Changelog**: https://github.com/bytecodealliance/wasm-tools/compare/v1.254.0...v1.255.0</pre>
  <p>View the full release notes at <a href="https://github.com/bytecodealliance/wasm-tools/releases/tag/v1.255.0">https://github.com/bytecodealliance/wasm-tools/releases/tag/v1.255.0</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!15533
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.

4 participants