ReviewOS

zig-utils/zig-js

JIT: execute profiled Number bitwise operations inside native loops

#1018
Closed glennmichael123 opened this 9 days ago ·
9 days ago

Parent: #1017 Umbrella: #473 Follows: ac67db5

Measured problem

After the primitive String length fix, frozen representative_json keeps checksum 324952086 and improves exact-parent median 400.774 ms -> 364.558 ms, but JIT remains 2.2% slower than forced VM (365.050 ms vs 357.070 ms, interleaved seven-sample medians).

A bounded native-operation trace shows the stable inner-iteration callback sequence:

  • load_var x2
  • get_prop x2
  • call_with_this x2
  • bit_and x1

The bit_and at bytecode origin 100 implements (record.values[i % 3] + i + lane) & 65535. It is one native-to-runtime callback per inner iteration despite both operands taking the Number path.

Scope

  • Add an architecture-neutral guarded direct path for Number bitwise operations used by optimizer native regions, starting with bit_and.
  • Implement exact ECMAScript ToInt32/ToUint32 behavior for fractional, negative-zero, NaN, infinite, and wrapping inputs.
  • Preserve canonical runtime callbacks for BigInt, objects/coercion, mixed numeric kinds, exceptions, and unsupported targets.
  • Keep step accounting, deopt reconstruction, GC roots, invalidation, and no-GIL synchronization unchanged.
  • Measure callback-count and wall-time deltas on the frozen base/variant plus anti-specialization controls.

Acceptance

  • Focused native tests prove exact Number edge cases and callback fallback for BigInt/object inputs.
  • The frozen JSON bit_and executes without a runtime-operation callback and preserves checksum 324952086.
  • ReleaseFast, ReleaseSafe+TSan, required-VM agreement, affected test262 bitwise cases, OOM/deep-input, and real-GC evidence pass.
  • Seven-sample exact-parent and JIT-on/off evidence shows no hidden control regression; #1017 is closed only if JIT no longer loses to forced VM.

Guardrails

No source/workload recognition, threshold suppression, changed checksum, disabled JIT, Number-only semantic shortcut without guards, or omitted BigInt/coercion behavior.

9 days ago

Landed the first guarded Number path in 8ca0d15.

Measured production evidence on the frozen representative_json row:

  • checksum remains 324952086;
  • native runtime-operation callbacks: 1,680,750 -> 1,440,643 (-14.3%);
  • exact-parent seven-sample median: 356.671 ms -> 349.735 ms (-1.9%);
  • same-binary JIT/no-JIT median: 350.892 ms vs 351.799 ms (JIT 0.3% ahead).

The focused direct/fallback semantics test passes in ReleaseFast and ReleaseSafe+TSan. The issue stays open while I add explicit BigInt/object fallback coverage and complete the required-VM, affected test262, OOM/deep-input, real-GC, and frozen control gates.

9 days ago

Additional qualification:

  • b966630 adds explicit BigInt and coercive-object callback witnesses; focused ReleaseFast passes.
  • current ReleaseFast test262 runner: test/language/expressions/bitwise-and is 30 pass / 0 fail.
  • tree vs required VM: 30 agree / 0 diverge.
  • exact implementation binaries for ac67db51 -> 8ca0d15b are now rebuilt from Git archives against real zig-gc 0285ef80; final control collection is waiting for an unrelated Home test to release CPU.
9 days ago

Completed on main with guarded Number semantics and explicit fallback coverage.

Landed:

  • 8ca0d15: execute guarded uint32 bit_and inside optimizer regions
  • b966630: pin BigInt and observable-object coercion fallbacks
  • a1a8f09: seven-pair exact-parent base/variant/control evidence
  • 0d2c6cf: consolidated dated report and README/docs summary

Measured result after #1017:

  • representative_json retired instructions: 0.9903945x parent
  • representative_json_variant: 0.9947436x parent
  • four controls: 0.9999024x to 1.0010478x

Qualification: affected bitwise test262 30/30 in tree and required-VM modes; focused fallback coverage passes ReleaseFast and ReleaseSafe+TSan. Checksums match and no callback path is admitted into the native fast path.

Sign in to comment on this issue.