optimizer: branchless abs via bit manipulation strength reduction#117
Merged
optimizer: branchless abs via bit manipulation strength reduction#117
Conversation
Add strength reduction pass that converts the branching abs pattern: if(x >= 0, x, 0 - x) into the branchless bit manipulation form: (x ^ (x >> 63)) - (x >> 63) This classic trick uses arithmetic right shift to create a sign mask (-1 for negative, 0 for non-negative), then XOR + subtract to flip the sign without branching. Benefits: - Eliminates branch misprediction penalty on abs() calls - Replaces if/else block (5+ WASM instructions) with 4 ALU ops (i64.shr_s, i64.xor, i64.shr_s, i64.sub) - Handles both trivial operands (vars/literals) and block-wrapped abs with let bindings (non-trivial operands like a+b) The optimization fires in the strength_reduce pass, matching the exact IR pattern that IRGen produces for abs(x). Includes 10 tests covering: - Optimizer pattern matching (trivial and block-wrapped forms) - Negative cases (non-abs patterns preserved) - End-to-end WAT generation (verifies branchless output) - Correctness via Wasmex execution (positive, negative, zero inputs) - Multi-abs functions (manhattan distance)
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.
Summary
Adds a strength reduction optimization that converts branching
abs(x)patterns into branchless bit manipulation.Before (branching)
After (branchless)
How it works
The classic branchless abs trick for signed 64-bit integers:
x >> 63produces a sign mask:0for non-negative,-1(all ones) for negativex ^ maskflips all bits when negative (one's complement), no-op when positive- maskadds 1 when negative (completing two's complement negation), no-op when positiveWASM impact
Before (branching abs):
After (branchless abs):
Eliminates branch misprediction and reduces code size.
Patterns matched
if(x >= 0, x, 0 - x)→ direct branchless forma+b): detects let-binding + if pattern from IRGen's abs loweringTests
10 new tests covering: