-
-
Notifications
You must be signed in to change notification settings - Fork 159
perf(codegen): lower Math.imul and typed-array-param reads to native ops #6850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| ### Changed | ||
|
|
||
| - Lower two integer-math JS primitives that were compiled as runtime function | ||
| calls to native machine ops, closing an ~9x AOT-vs-JIT gap on hot integer | ||
| kernels (hashes/PRNGs/mixers/ciphers): | ||
| - **`Math.imul(a, b)`** now lowers to a single native `mul i32` whenever both | ||
| operands are provably in-range i32 (multiplication mod 2^32 has identical | ||
| low 32 bits for signed and unsigned operands, so this is exact). | ||
| Non-finite / fractional / `>2^32` operands keep the `js_math_imul` runtime | ||
| helper so JS `ToUint32`/`ToInt32` semantics (`NaN`/`±Infinity` -> 0) are | ||
| preserved. This also fixes the accumulator case `a = Math.imul(a, K)` where | ||
| the constant `K` exceeds `i32::MAX` (e.g. the golden-ratio mixer constant | ||
| `0x9e3779b1` = 2654435761): the i32 fast path now accepts integer literals | ||
| representable in 32 bits under either a signed or unsigned interpretation. | ||
| - **Reading a typed-array element through a parameter** (`S[i]` where | ||
| `S: Int32Array` etc. is a function parameter, in an i32/`ToInt32` context) | ||
| now lowers to a checked inline native load — a runtime guard (pointer + | ||
| inline-storage `PERRY_TA_VIEW_GUARD` + kind-cache) and a header-length | ||
| bounds check gate a bare width-correct load, an in-kind out-of-bounds read | ||
| yields `0` (`== ToInt32(undefined)`, the only observable value in that | ||
| context), and every rejected shape (view/detached/resizable backing, wrong | ||
| runtime kind) defers to the new `js_typed_array_read_int32` runtime | ||
| fallback. Perry already emitted bare loads for typed-array *locals* with | ||
| proven bounds (#6750); this extends the recognition to *parameters*, whose | ||
| length and storage are unknown at compile time. Plain-value parameter reads | ||
| still observe `undefined` out of bounds. | ||
| - On a 40M-iteration `Int32Array`-parameter bit-mixer that combines both | ||
| primitives, the two fallbacks previously cascaded the whole hot loop into | ||
| slow f64 `ToInt32` towers (`js_math_imul` x3, `js_typed_array_get` x3, | ||
| ~60 `sitofp`/`fptosi`/`select`); both runtime-call families now reach zero | ||
| and the read/multiply chain stays in native i32. |
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Math.imul lowering to a native `mul i32` when both operands are provably | ||
| // in-range i32 (perry-codegen expr/math_simple.rs generic arm + | ||
| // expr/i32_fast_path.rs i32-native / accumulator path). Multiplication mod 2^32 | ||
| // has identical low 32 bits for signed and unsigned operands, so the native | ||
| // path is exact — but only for provable i32 operands. Non-finite / fractional / | ||
| // >2^32 operands MUST keep JS ToUint32/ToInt32 semantics via the runtime | ||
| // helper. Every result must match `node --experimental-strip-types` exactly. | ||
|
|
||
| // --- Edge cases the native path must NOT take (kept on the runtime helper) --- | ||
| console.log(Math.imul(NaN, 5)); // 0 (NaN -> ToInt32 -> 0) | ||
| console.log(Math.imul(Infinity, 5)); // 0 | ||
| console.log(Math.imul(-Infinity, 3)); // 0 | ||
| console.log(Math.imul(1.9, 2)); // 2 (1.9 -> ToInt32 -> 1) | ||
| console.log(Math.imul(2 ** 32 + 3, 1)); // 3 (ToUint32(2^32+3) = 3) | ||
|
|
||
| // --- Boundary / >i32::MAX constants the native path handles exactly --- | ||
| console.log(Math.imul(0x7fffffff, 2)); // -2 (wraps at i32 boundary) | ||
| console.log(Math.imul(0xffffffff, 5)); // -5 (0xffffffff -> -1 as i32) | ||
| console.log(Math.imul(-5, -3)); // 15 | ||
| console.log(Math.imul(0x9e3779b1, 3)); // multiplier > i32::MAX | ||
| console.log(Math.imul(0x9e3779b1 | 0, 0x85ebca6b | 0)); | ||
|
|
||
| // --- Nested native imul (result of imul is itself a provable i32) --- | ||
| console.log(Math.imul(Math.imul(3, 7), 5)); // 105 | ||
|
|
||
| // --- Variable i32 operands --- | ||
| let p = 123456789 | 0; | ||
| let q = -987654321 | 0; | ||
| console.log(Math.imul(p, q)); | ||
|
|
||
| // --- The i32-accumulator chain: `a = Math.imul(a, K)` on a local with an i32 | ||
| // slot, whose constant K exceeds i32::MAX — the exact shape that failed to | ||
| // lower before the Integer-gate fix (0x9e3779b1 = 2654435761 > i32::MAX). --- | ||
| function mix(x: number): number { | ||
| let a = x | 0; | ||
| a = Math.imul(a, 0x9e3779b1); | ||
| a = (a ^ (a >>> 15)) | 0; | ||
| a = Math.imul(a, 0x85ebca6b); | ||
| a = (a ^ (a >>> 13)) | 0; | ||
| a = Math.imul(a, 0xc2b2ae35); | ||
| a = (a ^ (a >>> 16)) | 0; | ||
| return a | 0; | ||
| } | ||
| let acc = 0 | 0; | ||
| for (let i = 0; i < 5000; i++) acc = (acc ^ mix(acc ^ i)) | 0; | ||
| console.log(acc); | ||
|
|
||
| // --- imul feeding `| 0` and arithmetic, in a tight loop (hash-like) --- | ||
| function fnv1aish(seed: number): number { | ||
| let h = seed | 0; | ||
| for (let i = 0; i < 32; i++) { | ||
| h = (h ^ i) | 0; | ||
| h = Math.imul(h, 0x01000193); // 16777619, a prime > i16 but < i32 | ||
| } | ||
| return h | 0; | ||
| } | ||
| console.log(fnv1aish(0x811c9dc5 | 0)); | ||
| console.log(fnv1aish(1), fnv1aish(-1), fnv1aish(0)); | ||
|
|
||
| // --- Scoping guard: the 32-bit-literal relaxation is confined to Math.imul. | ||
| // A plain `*` computes its product in f64 (precision loss above 2^53), so | ||
| // `x * BIGLIT | 0` must NOT be lowered to an exact `mul i32` — it must stay | ||
| // `ToInt32(f64_product)`, matching Node. `+`/`-`/bitwise with a >i32::MAX | ||
| // literal stay f64-exact too. --- | ||
| let g = 5 | 0; | ||
| g = (g + 3000000000) | 0; | ||
| console.log(g); // Add: sum < 2^53, exact | ||
| g = (g * 2654435761) | 0; | ||
| console.log(g); // Mul: product > 2^53 -> f64 rounding, NOT exact mul i32 | ||
| g = (g ^ 0x9e3779b1) | 0; | ||
| console.log(g); // bitwise with >i32::MAX literal | ||
| console.log((123456789 * 2654435761) | 0); // large product | 0 | ||
| console.log((0xffffffff * 0xffffffff) | 0); // 2^64-ish product | 0 | ||
| console.log((2000000000 * 2000000000) | 0); // two large i32 values | ||
| console.log((1000003 * 1000033) | 0); // product < 2^53 (exact either way) | ||
|
|
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| // Reading a typed-array element through a *parameter* (erased length / storage). | ||
| // perry-codegen expr/i32_fast_path.rs lowers an i32/ToInt32-context read of a | ||
| // typed-array PARAM to a checked inline native load (runtime guard: pointer + | ||
| // inline-storage PERRY_TA_VIEW_GUARD + kind-cache; header-length bounds check; | ||
| // bare load; 0 on in-kind OOB == ToInt32(undefined); slow fallback | ||
| // js_typed_array_read_int32 for view/detached/wrong-kind). A plain-value read | ||
| // still observes `undefined` OOB. Every line must match | ||
| // `node --experimental-strip-types` exactly. | ||
|
|
||
| // ---- i32-context (bitwise) reads, one per integer kind, in a loop ---- | ||
| function xorI32(S: Int32Array, n: number): number { | ||
| let a = 0 | 0; | ||
| for (let i = 0; i < n; i++) a = (a ^ S[i & 7]) | 0; | ||
| return a | 0; | ||
| } | ||
| function xorI8(S: Int8Array, n: number): number { | ||
| let a = 0 | 0; | ||
| for (let i = 0; i < n; i++) a = (a ^ S[i & 7]) | 0; | ||
| return a | 0; | ||
| } | ||
| function xorU8(S: Uint8Array, n: number): number { | ||
| let a = 0 | 0; | ||
| for (let i = 0; i < n; i++) a = (a ^ S[i & 7]) | 0; | ||
| return a | 0; | ||
| } | ||
| function xorU8C(S: Uint8ClampedArray, n: number): number { | ||
| let a = 0 | 0; | ||
| for (let i = 0; i < n; i++) a = (a ^ S[i & 7]) | 0; | ||
| return a | 0; | ||
| } | ||
| function xorI16(S: Int16Array, n: number): number { | ||
| let a = 0 | 0; | ||
| for (let i = 0; i < n; i++) a = (a ^ S[i & 7]) | 0; | ||
| return a | 0; | ||
| } | ||
| function xorU16(S: Uint16Array, n: number): number { | ||
| let a = 0 | 0; | ||
| for (let i = 0; i < n; i++) a = (a ^ S[i & 7]) | 0; | ||
| return a | 0; | ||
| } | ||
| function xorU32(S: Uint32Array, n: number): number { | ||
| let a = 0 | 0; | ||
| for (let i = 0; i < n; i++) a = (a ^ S[i & 7]) | 0; | ||
| return a | 0; | ||
| } | ||
|
|
||
| const i32 = Int32Array.from([-5, 100000, -2000000000, 7, 0x7fffffff, -1, 42, 999]); | ||
| const i8 = Int8Array.from([-5, 100, -128, 7, 127, -1, 42, 99]); | ||
| const u8 = Uint8Array.from([1, 200, 255, 7, 128, 0, 42, 99]); | ||
| const u8c = Uint8ClampedArray.from([1, 200, 255, 7, 128, 0, 42, 99]); | ||
| const i16 = Int16Array.from([-5, 30000, -32768, 7, 32767, -1, 42, 999]); | ||
| const u16 = Uint16Array.from([1, 60000, 65535, 7, 32768, 0, 42, 999]); | ||
| const u32 = Uint32Array.from([1, 4000000000, 0xffffffff, 7, 0x80000000, 0, 42, 999]); | ||
|
|
||
| console.log("i32", xorI32(i32, 8)); | ||
| console.log("i8", xorI8(i8, 8)); | ||
| console.log("u8", xorU8(u8, 8)); | ||
| console.log("u8c", xorU8C(u8c, 8)); | ||
| console.log("i16", xorI16(i16, 8)); | ||
| console.log("u16", xorU16(u16, 8)); | ||
| console.log("u32", xorU32(u32, 8)); | ||
|
|
||
| // ---- OOB in i32-context: reads past length contribute 0 (ToInt32(undefined)) ---- | ||
| function xorOob(S: Int32Array): number { | ||
| let a = 12345 | 0; | ||
| for (let i = 0; i < 16; i++) a = (a ^ S[i]) | 0; // i = 8..15 are OOB | ||
| return a | 0; | ||
| } | ||
| console.log("oob", xorOob(i32)); | ||
|
|
||
| // A negative & fractional index in i32-context also read as 0. | ||
| function readMasked(S: Int32Array, i: number): number { | ||
| return (99 ^ S[i]) | 0; | ||
| } | ||
| console.log("neg", readMasked(i32, -1)); // S[-1] -> undefined -> 0 -> 99 ^ 0 | ||
| console.log("frac", readMasked(i32, 3.9)); // fractional -> undefined -> 0 | ||
| console.log("in", readMasked(i32, 3)); // in-bounds element 7 | ||
|
|
||
| // ---- plain-value reads: OOB / negative / fractional must be `undefined` ---- | ||
| function readAt(S: Int32Array, i: number): number | undefined { | ||
| return S[i]; | ||
| } | ||
| console.log("v0", readAt(i32, 0), "v7", readAt(i32, 7)); | ||
| console.log("voob", readAt(i32, 8)); // undefined | ||
| console.log("vneg", readAt(i32, -1)); // undefined | ||
| console.log("vfrac", readAt(i32, 1.5)); // undefined | ||
| console.log("vstr", String(readAt(i32, 8))); // "undefined" | ||
| console.log("veq", readAt(i32, 8) === undefined); // true | ||
|
|
||
| // ---- Float64Array param (element width != 4) ---- | ||
| function sumF64(S: Float64Array, n: number): number { | ||
| let s = 0; | ||
| for (let i = 0; i < n; i++) s += S[i]; | ||
| return s; | ||
| } | ||
| function readF64(S: Float64Array, i: number): number | undefined { | ||
| return S[i]; | ||
| } | ||
| function truncF64(S: Float64Array, i: number): number { | ||
| return S[i] | 0; // i32-context: float -> ToInt32 | ||
| } | ||
| const f64 = Float64Array.from([1.5, 2.25, -3.75, 100.125, 1e12 + 0.5]); | ||
| console.log("f64sum", sumF64(f64, 5)); | ||
| console.log("f64read", readF64(f64, 1), readF64(f64, 10)); | ||
| console.log("f64trunc", truncF64(f64, 0), truncF64(f64, 2), truncF64(f64, 4), truncF64(f64, 99)); | ||
|
|
||
| // ---- Float32Array param (width 4, but float kind — stays on runtime read) ---- | ||
| function readF32(S: Float32Array, i: number): number | undefined { | ||
| return S[i]; | ||
| } | ||
| const f32 = Float32Array.from([0.5, -1.5, 2.5]); | ||
| console.log("f32", readF32(f32, 0), readF32(f32, 2), readF32(f32, 9)); | ||
|
|
||
| // ---- view over an ArrayBuffer (non-inline storage -> slow fallback path) ---- | ||
| function viewXor(S: Int32Array, n: number): number { | ||
| let a = 0 | 0; | ||
| for (let i = 0; i < n; i++) a = (a ^ S[i]) | 0; | ||
| return a | 0; | ||
| } | ||
| function viewRead(S: Int32Array, i: number): number | undefined { | ||
| return S[i]; | ||
| } | ||
| const ab = new ArrayBuffer(16); | ||
| const view = new Int32Array(ab); | ||
| view[0] = 111; | ||
| view[1] = -222; | ||
| view[2] = 333; | ||
| view[3] = -444; | ||
| console.log("view", viewXor(view, 4), viewRead(view, 1), viewRead(view, 8)); | ||
|
|
||
| // ---- detached buffer: reads are undefined (plain) / 0 (i32-context) ---- | ||
| const ab2 = new ArrayBuffer(16); | ||
| const det = new Int32Array(ab2); | ||
| det[0] = 7; | ||
| det[1] = 9; | ||
| console.log("predetach", viewRead(det, 0), viewXor(det, 4)); | ||
| ab2.transfer(); // detach ab2 (and its view `det`) | ||
| console.log("postdetach-plain", viewRead(det, 0)); // undefined | ||
| console.log("postdetach-i32", viewXor(det, 4)); // 0 (all OOB after detach) | ||
|
|
||
| // ---- fractional index in i32 context must NOT take the checked native path ---- | ||
| // (regression: the fast path lowers the index via ToInt32, so `S[3.9]` would | ||
| // read element 3; JS reads a fractional typed-array index as undefined -> 0.) | ||
| function fracI32(S: Int32Array): number { | ||
| return S[3.9] | 0; | ||
| } | ||
| function fracVar(S: Int32Array, i: number): number { | ||
| return S[i] | 0; | ||
| } | ||
| const fr = new Int32Array([10, 20, 30, 40, 50]); | ||
| console.log("frac-lit", fracI32(fr)); // 0 (not 40) | ||
| console.log("frac-var", fracVar(fr, 2.5)); // 0 (not 30) | ||
| console.log("int-var", fracVar(fr, 3)); // 40 (integer var still fast+correct) |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.