Closed Bug 2075098 Opened 2 months ago Closed 2 months ago

Some rotation optimizations

Categories

(Core :: JavaScript Engine, enhancement, P3)

enhancement

Tracking

()

RESOLVED WONTFIX

People

(Reporter: mayankleoboy1, Unassigned)

References

Details

Attachments

(1 file, 1 obsolete file)

No description provided.

These build on top of the patches in bug 2074467.

Worked for 38s

Bug 2074467 — Follow-up rotation optimizations

Overall design

This eight-patch series builds on the three original Bug 2074467 patches, which introduce recognition and recovery support for MRotate.

The goal is to recognize more complementary-shift expressions as rotations, simplify rotations in MIR, and improve ARM64 instruction selection. The changes reuse existing optimization infrastructure rather than introducing another compiler-wide analysis pass.

Rotation recognition and simplification occur through the existing foldsTo() methods during GVN. The series does not add a post-truncation walk over MIR instructions. Consequently, patterns that become recognizable only after range-analysis truncation are intentionally outside its scope.

The implementation keeps Int32 rotation semantics separate from generic JavaScript arithmetic and limits transformations to patterns whose equivalence can be established from the MIR operands.

Patch breakdown

1. 0001 — Recover variable-count rotations on bailout

Extends RRotate recovery to handle arbitrary Int32 counts by masking the count to five bits. Effective zero is handled separately to avoid a C++ shift by 32. This provides recovery support for the variable-count rotations introduced by the following patch.

2. 0002 — Recognize complementary variable-count shifts

Extends MBitOr::foldsTo() to recognize expressions such as (x >>> n) | (x << (32 - n)) and (x << n) | (x >>> (32 - n)), including supported complementary subtraction forms. The matcher requires compatible Int32 MIR operands and creates an MRotate using the existing right-shift count. OR also supports effective-zero counts because x | x equals x.

3. 0003 — Recognize XOR-based rotations

Extends MBitXor::foldsTo() to use the complementary-shift matcher when both shifts have proven nonzero constant counts. The shifted bitfields are then disjoint, making XOR equivalent to OR. Variable counts are excluded because an effective-zero count makes XOR produce zero rather than the original input.

4. 0004 — Recognize rotations expressed as Int32 addition

Extends MAdd::foldsTo() to recognize the same disjoint, complementary constant-shift pattern in specialized Int32 addition. Generic JavaScript addition and variable counts are excluded; in particular, an effective-zero count would add the input to itself rather than rotate it.

5. 0005 — Fold constant and nested rotations in MIR

Adds MRotate::foldsTo() to simplify zero-count rotations, invariant inputs such as zero and minus one, constant inputs, and nested rotations with constant counts. Nested rotations are combined modulo 32. If an inner rotation has other consumers, those consumers continue using the original instruction.

6. 0006 — Recognize equivalent constant counts modulo 32

Extends complementary-shift matching to accept noncanonical constants with equivalent effective five-bit counts, such as 33 and 1. The matcher retains the existing MIR count operand; it does not create a replacement constant merely to recognize the pattern.

7. 0007 — Reuse an available right-rotation count

Recognizes an existing Int32 left rotation whose count is a truncated 32 - n or 0 - n, and rewrites it as a right rotation by n. This can avoid count negation when lowering a variable left rotation on ARM64. The transformation is restricted to Int32 input, result, and count operations; Int64 rotations remain unchanged.

8. 0008 — Fold eligible rotations into ARM64 logical instructions

Adds an ARM64 lowering path for a constant-count Int32 rotation used by a single eligible OR, XOR, or AND instruction. Rather than materializing a separate rotation result, the logical instruction uses ARM64’s rotated-register operand form. Rotations that require a separately available result, including those with additional uses or relevant guard and resume-point constraints, retain the ordinary lowering path.

Scope and validation

The series deliberately excludes the proposed post-truncation MIR scan, range-propagation changes, and BMI2 RORX work.

The included tests cover rotation arithmetic, count boundaries, nested folding, bailout-recovery scenarios, WebAssembly rotation semantics, and ARM64 instruction selection. The complete series has not yet been built or run against the exact Gecko revision containing the three prerequisite patches. Patch application, SpiderMonkey JIT tests, bailout behavior, ARM64 code generation, and performance measurements remain to be verified before landing.

Depends on: 2074467
Attachment #9647766 - Attachment is obsolete: true

These patches cover a bunch of additional possible optimizations, but it's not clear to me how much they matter in the real world. I asked Claude to speculate about cases where variable-count rotations would be valuable, and the best it could come up with is the PCG32 PRNG, which might be used for games / procedural generation. But that wouldn't even be covered by these patches, because it uses -rot & 31 to compute the second shift amount, not 32 - rot.

I think these patches would be a good starting point if we ever needed to optimize rotations more, but absent a motivating example, I don't think they're a priority right now.

Type: task → enhancement
Priority: -- → P3
Severity: -- → N/A
Status: NEW → RESOLVED
Closed: 2 months ago
Resolution: --- → WONTFIX
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: