Open Bug 2090266 Opened 12 days ago

Use add-with-shifted-register for i32 multiplication by (1 << C1) | (1 << C2) constants on ARM64

Categories

(Core :: JavaScript Engine: JIT, task, P3)

task

Tracking

()

People

(Reporter: iain, Unassigned, Mentored)

Details

(Keywords: good-first-bug)

Filing as a good first bug to learn workflows.

When Ion multiplies an int32 by a constant on ARM64, CodeGeneratorARM64::visitMulI handles -1, 0, 1, 2 and powers of two specially. Every other constant is moved into a scratch register and multiplied with mul:

mov     w16, #0x5
mul     w0, w0, w16

AArch64's add can shift its second operand for free. So when the constant has exactly two bits set, i.e. (1 << C1) | (1 << C2) with C1 > C2, we can use one or two cheap instructions instead:

x * 5  = x + (x << 2)           ->  add w0, w0, w0, lsl #2
x * 10 = (x + (x << 2)) << 1    ->  add w0, w0, w0, lsl #2
                                    lsl w0, w0, #1

The 32-bit ARM, MIPS, RISC-V and LoongArch backends already do this. See CodeGeneratorARM::visitMulI for a reference: look for the comment "If the constant cannot be encoded as (1 << C1), see if it can be encoded as (1 << C1) | (1 << C2)".

What to do, in the default: case of CodeGeneratorARM64::visitMulI (js/src/jit/arm64/CodeGenerator-arm64.cpp):

  • Inside the existing if (!mul->canOverflow() && constant > 0) block, after the power-of-two check, compute shift = FloorLog2(uint32_t(constant)) and rest = constant - (1 << shift).
  • If std::has_single_bit(rest), let restShift = std::countr_zero(rest), then:
    masm.Add(destreg32, lhsreg32, Operand(lhsreg32, vixl::LSL, shift - restShift));
    if (restShift != 0) masm.Lsl(destreg32, destreg32, restShift);
    return;
    (Operand(reg, vixl::LSL, n) is already used elsewhere in the same file.)
  • Only do this when the multiply cannot overflow, the same condition as the existing power-of-two shift: add/lsl don't give us the overflow check that the mul32 path does.
  • Leave the 64-bit (visitMulI64) and intptr (visitMulIntPtr) paths alone. They can be follow-up bugs.

Link to the code:
https://searchfox.org/firefox-main/rev/fa1e90ea670c4234c61b65a05dc02032b9bcde03/js/src/jit/arm64/CodeGenerator-arm64.cpp#231

Reference implementation (32-bit ARM):
https://searchfox.org/firefox-main/rev/fa1e90ea670c4234c61b65a05dc02032b9bcde03/js/src/jit/arm/CodeGenerator-arm.cpp#355

Tests:
Update js/src/jit-test/tests/wasm/binop-arm64-ion-codegen.js. The i32 case under "Test that multiplication by five yields a multiply" should now expect add w0, w0, w0, lsl #2; the i64 case stays as mul. Add a constant where the lower bit is not bit 0 (e.g. 10 or 12, so the trailing lsl is emitted), and one with three bits set (e.g. 11) that should still use mul. Keep the assertEq checks with both positive and negative inputs.
https://searchfox.org/firefox-main/rev/fa1e90ea670c4234c61b65a05dc02032b9bcde03/js/src/jit-test/tests/wasm/binop-arm64-ion-codegen.js#159

To verify the fix:

You don't need ARM64 hardware. On an x86-64 Linux machine, build a debug JS shell with the ARM64 simulator (which includes the disassembler the test needs) using a mozconfig containing:

ac_add_options --enable-project=js
ac_add_options --enable-debug
ac_add_options --enable-simulator=arm64

Then run:

./mach build
./mach jit-test wasm/binop-arm64-ion-codegen.js
./mach jit-test wasm ion

(On an ARM64 machine, e.g. an Apple Silicon Mac, a normal debug JS shell build works too.)

Tutorial to contribute:
https://firefox-source-docs.mozilla.org/contributing/contribution_quickref.html
https://firefox-source-docs.mozilla.org/contributing/stack_quickref.html

Please don't ask for the bug to be assigned. It will be automatically assigned to the first patch.

You need to log in before you can comment on or make changes to this bug.