Skip to content

Fix fallthrough for desc operands - #9089

Merged
tlively merged 2 commits into
mainfrom
fix-fallthrough-br-on
Sep 9, 2026
Merged

Fix fallthrough for desc operands#9089
tlively merged 2 commits into
mainfrom
fix-fallthrough-br-on

Conversation

@tlively

@tlively tlively commented Sep 9, 2026

Copy link
Copy Markdown
Member

getImmediateFallthroughPtr needs to make sure that the fallthrough value is not modified or otherwise rendered unusable by the side effects of expressions that are evaluated after the fallthrough expression but before the top-level expression. Previously the only such expression was the br_if condition, and getImmediateFallthroughPtr only looked through the br_if if the condition did not have side effects that would interfere with the rest of the fallthrough expression.

Make this effect analysis slightly more precise by using orderedBefore instead of canReorder. Also add similar effects analysis for the desc operands of RefCast and BrOn expressions, if it exists. These operands are also evaluated after the fallthrough expression.

Finally, update areConsecutiveAndEqual in OptimizeInstructions to look through RefCast and BrOn expressions despite any effects that may occur in their desc operands; it will do its own more precise effects analysis afterward.

`getImmediateFallthroughPtr` needs to make sure that the fallthrough value is not modified or otherwise rendered unusable by the side effects of expressions that are evaluated after the fallthrough expression but before the top-level expression. Previously the only such expression was the br_if condition, and `getImmediateFallthroughPtr` only looked through the br_if if the condition did not have side effects that would interfere with the rest of the fallthrough expression.

Make this effect analysis slightly more precise by using `orderedBefore` instead of `canReorder`. Also add similar effects analysis for the `desc` operands of `RefCast` and `BrOn` expressions, if it exists. These operands are also evaluated after the fallthrough expression.

Finally, update `areConsecutiveAndEqual` in `OptimizeInstructions` to look through `RefCast` and `BrOn` expressions despite any effects that may occur in their `desc` operands; it will do its own more precise effects analysis afterward.
@tlively
tlively requested a review from a team as a code owner September 9, 2026 20:58
@tlively
tlively requested review from kripken and removed request for a team September 9, 2026 20:58
)
)
)
(i32.const -1)

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.

Does this function need a return value?

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.

(also below, if so)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This goes along with the return of the ref.eq. The test would work without the return, but the benefit of having the return value is that it will make bugs much more obvious when this function is used as a fuzzing seed. With the return values present, --fuzz-exec is able to tell that these functions are misoptimized without the fixes in this PR.

I can remove it if you prefer, though, since it's not immediately useful in the test itself.

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.

Oh, I see. Fair enough, sgtm to keep it.

;; NTRAP-NEXT: (i32.const -1)
;; NTRAP-NEXT: )
(func $br_on_cast_desc_eq_fail-side-effects-in-desc (param $x (ref null $struct)) (param $d (ref null $desc)) (result i32)
(drop

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.

Please add a comment explaining what is tested here.

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.

(also below)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Done. Copied the comments from the above test variants. The only thing different here is that these three tests use the *_fail variant of the instruction.

@tlively
tlively enabled auto-merge (squash) September 9, 2026 23:00
@tlively
tlively merged commit 5d93aba into main Sep 9, 2026
16 checks passed
@tlively
tlively deleted the fix-fallthrough-br-on branch September 9, 2026 23:21
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