Repository navigation
Fix folded branch hints - #2578
Conversation
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.
fitzgen
left a comment
There was a problem hiding this comment.
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
|
Thank you for fixing this! Now that branch-hints is merged and there's a normative spec, I would be in favor making the (func
(@metadata.code.branch_hint "\00")
)(func
(@metadata.code.branch_hint "\00")
nop
)(func
(if (i32.const 0) (@metadata.code.branch_hint "\00")
(then))
)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. |
|
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 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 |
6404f7e to
ec1946a
Compare
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.
ec1946a to
f4c5337
Compare
|
I have updated the PR to reject misplaced annotations. |
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
This fixes the parsing and printing of branch hints, so that the hint is attached to the
ifand not to thelocal.getin the example below (from the test suite).The form previously emitted (with the branch hint right before the
then) is still accepted on parsing.