Conversation
Fixes WebAssembly#7672. `LocalCSE` rewrites repeated subexpressions as a `local.tee` followed by a `local.get`, so two identical operands to a relational comparison can reach `OptimizeInstructions` as `(rel (local.tee $n X) (local.get $n))`. The pass has a helper for exactly this shape (`areMatchingTeeAndGet`) but consults it only in the `RefEq` and `Select` handlers, not in `optimizeRelational`. The comparison survives into the `-O3` output and blocks subsequent dead-code elimination. Add the fold at the top of `optimizeRelational`. The result of a self-comparison is a constant (`gt`/`lt`/`ne` -> 0, `ge`/`le`/`eq` -> 1). X is evaluated once for its side effects and then dropped, so the reproduction's load chain preserves its original trap behavior. Refs WebAssembly#6719 -- the drop-before-unreachable fix that made the residual `drop; unreachable` visible in the issue's output.
…to the output while still producing the tee
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem:
LocalCSErewrites repeated subexpressions as alocal.teefollowed by a
local.get. When the two operands of a relationalcomparison are exactly such a pair,
OptimizeInstructionsdid notrecognize them as equal, so the comparison survived into the
-O3output and blocked dead-code elimination. The reproduction in #7672
is a module where
(i32.gt_s (local.tee $0 X) (local.get $0))should fold to a constant.
Fix: Add a fold at the top of
optimizeRelationalinsrc/passes/OptimizeInstructions.cpp. The pass already has a helper(
areMatchingTeeAndGet) that recognizes the tee/get pattern; it wasconsulted in the
RefEqandSelecthandlers but not in therelational path. The fold evaluates
Xonce for its side effects,drops it, and returns the constant result (
gt/lt/ne-> 0,ge/le/eq-> 1). It does not duplicateX, which matters whenXcan trap — the reproduction is a chain of loads.Verification:
test/lit/passes/optimize-instructions-tee-get-comparison.wastexercises the tee/get fold in isolation.
python3 check.py litpasses (1002 tests).-O3output contains zerolocal.teeandzero
i32.gt_s(both greps return 0); before the fix, both returned 1.Fixes #7672.