Skip to content

Take integer constants as AArch64 immediates - #20

Open
Qard wants to merge 2 commits into
mainfrom
perf/loop-constants
Open

Qard wants to merge 2 commits into
mainfrom
perf/loop-constants

Conversation

@Qard

@Qard Qard commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Standalone codegen PR for the loop-constant items of the #17 kernel-parity plan. It is based on main and does not depend on #17 or #18. It is now ready to merge, pending CI.

Commits

  1. Take integer constants as AArch64 immediates (16ef2735c)
    • Exact integer constants held as int64 are no longer written to their home when defined. A use with an immediate form reads the value directly:
      • add and subtract with a 12-bit magnitude, optionally shifted by 12; negative addends swap ADD and SUB
      • int64 multiply by 2^k (LSL) or by 2^k+1 (shifted ADD)
      • int64 compares against an immediate, using CMN for negative constants
      • 32-bit &, | and ^ with logical immediates (new AND/ORR/EOR/ANDS immediate encodings), plus |0, ^0 and &-1 as a plain move
      • Math.imul by 2^k, 2^k+1 or 2^k−1
      • stores to integer slots, built directly in the slot's home
    • Every other use writes the constant out just before that use.
    • value() throws if anything reads a constant that hasn't been written out yet, so a missed path fails compilation instead of reading garbage.
  2. Type the SP base-register rejection case as an instruction (4b41dad17): fixes a test typecheck error.

Dropped by measurement:

  • Loop and function alignment. No policy improves any kernel, and none makes timings placement-stable; details below.
  • P5 loop hoisting of multi-instruction constants. After immediates, the only such constant in a hot loop is numeric's imul constant, and it is not on the loop-carried chain. Hoisting it would need a register held across the loop, which this emitter does not have.
  • Typed-array data-pointer hoisting does not fall out of this work, for the same reason.

Results (15 interleaved rounds, Node 26.13.1, timing lock)

Frozen v7, main vs this PR:

  • cycles: −1.2% (median of paired rounds; this PR is better in 12 of 15)
  • instructions: −2.0% (1623.1G → 1591.0G)
  • wall: 71.49 → 71.22 s
  • RSS: unchanged
  • output: identical (68c11052…)
  • image size: 77.2 → 76.6 MB

Typed suite, main vs this PR, in ms:

  • Faster:
    • numeric 46.2 → 45.2
    • strings 25.2 → 24.6
    • typedArrays 47.9 → 46.8
    • stringBuild 53.3 → 52.3
    • calls 50.2 → 49.7
  • Unchanged within noise: allocation, compiler, closures, arrays, escape
  • Slower, but from placement, not code:
    • stringEquality 12.3 → 12.4: every code change in that kernel is an instruction removed, and the copies shifted by 16/48 bytes measured 12.5.
    • mapSet 46.6 → 47.0: the intervals overlap, and the shifted copies measured 47.1/46.8.
  • Code size: __text −1.8%.

#18 string bench, #18 head vs #18 with this PR cherry-picked (one trivial conflict). Three placement variants of this PR were measured: unshifted, and code shifted by 16 and 48 bytes.

  • codeUnits: 14.1 → 12.2 / 12.5 / 12.1 ms
  • codeUnitsTwoByte: 13.2 → 11.1 / 11.5 / 11.2 ms
  • Math.imul(c, 31) on the loop-carried chain is now LSL+SUB, and no constants are materialized in the loop.
  • utf8TwoByte: 5.1 → 5.3 / 5.4 / 5.1. The 48-byte-shifted copy matches base, so this is placement; the codec's code is strictly shorter.
  • All other kernels are within placement spread.

Alignment (function entries 32/64, innermost loop headers 32/64, and both together):

  • No speed gain on any kernel. Some cases are worse: loop-64 makes arrays 7% slower, and function+loop-64 makes calls 5% slower.
  • Size cost on frozen v7: function-32 +0.13%, function-64 +0.28%, loop-32 +5.1%, loop-64 +6.2%.
  • No stability gain. Two function-64 images that differ only by a 64-byte shift of all code still differ by 7% on stringEquality (13.5 vs 12.6 ms) and 2.6% on calls. The noise comes from coarser placement (page or predictor aliasing), which alignment does not control.

Validation

  • Codegen tests: 520/520, and 522/522 with the PR cherry-picked onto One-byte (Latin-1) string representation #18.
  • check:concurrent locally: typecheck, test-file classification, boundaries and format all pass. 6,123 concurrent tests pass; the one failure needs a local Test262 checkout, which CI has.
  • Native-subset check: "checked". Stage-1 bootstrap passes.
  • stage2 == stage3: identical, 50,527,242 bytes.
  • value() never threw a deferred-constant error across the full compiler build, the typed suite, the string bench or frozen v7.
  • The logical-immediate encoder matches llvm-mc on all 1,302 valid 32-bit patterns and rejects the rest; an independent DecodeBitMasks cross-checks it.
  • New native fixture tools/native-integer-immediates.test.ts passes under LLDB and matches Node. It covers:
    • negative and shifted add immediates
    • CMN compares
    • bitmask, identity and non-encodable logical constants
    • imul by 2^k, 2^k±1, 2^31 and −1
    • a constant used both as an immediate and in a register
    • constant stores to slots

🤖 Generated with Claude Code

@Qard
Qard force-pushed the perf/loop-constants branch from bcd2ae1 to 79f2775 Compare November 24, 2026 01:08
Exact integer constants held as int64 are no longer written to their
home where they are defined. A use with an immediate form reads the
value instead:

- int64 addition and subtraction of a 12-bit magnitude, optionally
  shifted by 12 (negative addends flip ADD and SUB);
- int64 multiplication by 2^k (LSL) or 2^k + 1 (ADD, shifted);
- int64 comparisons against such a magnitude on the right (CMN for
  negative constants);
- 32-bit `&`, `|` and `^` with a logical immediate (new AND, ORR, EOR
  and ANDS immediate encodings), or the identities `| 0`, `^ 0` and
  `& -1` as a W move;
- Math.imul by 2^k, 2^k + 1 or 2^k - 1 (LSL, shifted ADD, LSL + SUB);
- stores to integer slots, built directly in the slot's home.

Every other use writes the constant to its home just before that use.
value() rejects a home access to a constant still deferred, so a path
that misses the materialization fails compilation instead of reading an
unwritten home; type queries use typeOf().

In main's typedArrays loops each constant cost `mov x9, #k; mov xN, x9`
plus a register operand per iteration; they now compile to forms such
as `cmp x28, #1, lsl #12`, `add x20, x28, #1` and `and w21, w19, #0xff`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Qard
Qard force-pushed the perf/loop-constants branch from 79f2775 to 16ef273 Compare November 24, 2026 01:23
The logical-immediate rejection test passed "sp" as a data register
directly, which the test typecheck rejects (TS2322); cast it as the other
register-role rejection cases do.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Qard Qard changed the title Take loop constants as immediates; align loops (P4/P5) Take integer constants as AArch64 immediates Nov 24, 2026
@Qard
Qard marked this pull request as ready for review November 24, 2026 03:16
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.

1 participant