-
-
Notifications
You must be signed in to change notification settings - Fork 156
perf(codegen): #6794 follow-up (a) — region-scoped i32 chains in the ta_i32 masked-window copy #6844
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
perf(codegen): #6794 follow-up (a) — region-scoped i32 chains in the ta_i32 masked-window copy #6844
Changes from all commits
Commits
Show all changes
2 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,9 @@ | ||
| ### Changed | ||
|
|
||
| - Region masked-window versioner (`#6794` follow-up): in the `ta_i32` fast copy, | ||
| bind locals whose every in-region write is strictly-i32-bounded to a | ||
| region-scoped i32 shadow slot, so a `>>>`/`&`/`^`/`| 0` bit-mixing chain on an | ||
| untyped-init local (the bcryptjs `_encipher` shape, `l = lr[off]`) stays in | ||
| native i32 instead of paying a ToInt32 tower per op. Removes the residual | ||
| ToInt32 towers LLVM cannot fold on its own — ~11% on `bcryptjs.compareSync` | ||
| with `Int32Array` S-boxes — with no change to the plain-array or slow copies. |
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,103 @@ | ||
| // #6794 follow-up (a): region masked-window i32 chains. The straight-line | ||
| // region versioner's ta_i32 fast copy binds locals whose every in-region write | ||
| // is strictly-i32-bounded to a region-scoped i32 shadow slot, so a | ||
| // `>>>`/`&`/`^`/`| 0` bit-mixing chain on an UNTYPED-init local (the bcryptjs | ||
| // `_encipher` shape: `l = lr[off]` is not statically i32-bounded, so `l` never | ||
| // earns a static i32 slot) stays in native i32 instead of paying a ToInt32 | ||
| // tower per op. Every shape below must produce byte-identical output to Node | ||
| // whether the i32 refinement fires or the region deopts. | ||
|
|
||
| // Canonical Blowfish-F round shape on untyped Int32Array params, l/r init from | ||
| // a dynamic `lr[off]` read (disqualifies the static i32 slot -> exercises the | ||
| // region-scoped one). | ||
| function feistel(S: any, P: any, lr: any, off: number): number { | ||
| let l = lr[off]; | ||
| let r = lr[off + 1]; | ||
| l = (l ^ P[0]) | 0; | ||
| r = (r ^ ((((S[(l >>> 24) & 0xff] + S[256 + ((l >>> 16) & 0xff)]) | 0) ^ S[512 + ((l >>> 8) & 0xff)]) + S[768 + (l & 0xff)]) ^ P[1]) | 0; | ||
| l = (l ^ ((((S[(r >>> 24) & 0xff] + S[256 + ((r >>> 16) & 0xff)]) | 0) ^ S[512 + ((r >>> 8) & 0xff)]) + S[768 + (r & 0xff)]) ^ P[2]) | 0; | ||
| r = (r ^ ((((S[(l >>> 24) & 0xff] + S[256 + ((l >>> 16) & 0xff)]) | 0) ^ S[512 + ((l >>> 8) & 0xff)]) + S[768 + (l & 0xff)]) ^ P[3]) | 0; | ||
| return (l ^ r) | 0; | ||
| } | ||
|
|
||
| // Overflow: two Int32Array elements summed feed a bitwise op — the i32 chain | ||
| // must WRAP (mod 2^32), matching `(a + b) | 0`, not saturate. | ||
| function overflow(S: any, seed: number): number { | ||
| let x = seed | 0; | ||
| x = (x ^ S[0]) | 0; | ||
| x = (((S[1 + (x & 1)] + S[2 + (x & 1)]) | 0) ^ S[3 + (x & 3)]) | 0; | ||
| x = (((S[4 + (x & 1)] + S[5 + (x & 1)]) | 0) ^ S[6 + (x & 3)]) | 0; | ||
| x = (((S[7 + (x & 1)] + S[0]) | 0) ^ S[1]) | 0; | ||
| x = (x ^ S[2]) | 0; | ||
| return x | 0; | ||
| } | ||
|
|
||
| // Post-region: the local is read as a full Number (with a fractional add) AND | ||
| // as an unsigned int AFTER the region — the double shadow must carry the | ||
| // correct signed-i32 value out of the fast copy. | ||
| function postRead(S: any, seed: number): string { | ||
| let x = seed | 0; | ||
| x = (x ^ S[0]) | 0; | ||
| x = (((S[1] + S[2]) | 0) ^ S[3]) | 0; | ||
| x = (((S[4] + S[5]) | 0) ^ S[6]) | 0; | ||
| x = (x ^ S[7]) | 0; | ||
| return (x + 0.5).toFixed(1) + "|" + (x >>> 0).toString(16); | ||
| } | ||
|
|
||
| // Un-refine: a NON-strict (fractional Mul) write mid-region must keep the local | ||
| // off the i32 slot for the whole region, staying byte-exact. | ||
| function unrefine(S: any, seed: number): number { | ||
| let x = seed | 0; | ||
| x = (x ^ S[0]) | 0; | ||
| x = (((S[1] + S[2]) | 0) ^ S[3]) | 0; | ||
| x = x * 1.5; | ||
| x = (((S[4] + S[5]) | 0) ^ S[6]) | 0; | ||
| x = (x ^ S[7]) | 0; | ||
| return (x + 100000) | 0; | ||
| } | ||
|
|
||
| // Plain-Array variant (untyped param that is NOT a typed array): the plain_f64 | ||
| // region tier fires, where the i32-slot refinement is deliberately disabled — | ||
| // output must still match. | ||
| function feistelPlain(S: any, P: any, lr: any, off: number): number { | ||
| let l = lr[off]; | ||
| let r = lr[off + 1]; | ||
| l = (l ^ P[0]) | 0; | ||
| r = (r ^ ((((S[(l >>> 24) & 0xff] + S[256 + ((l >>> 16) & 0xff)]) | 0) ^ S[512 + ((l >>> 8) & 0xff)]) + S[768 + (l & 0xff)]) ^ P[1]) | 0; | ||
| l = (l ^ ((((S[(r >>> 24) & 0xff] + S[256 + ((r >>> 16) & 0xff)]) | 0) ^ S[512 + ((r >>> 8) & 0xff)]) + S[768 + (r & 0xff)]) ^ P[2]) | 0; | ||
| return (l ^ r) | 0; | ||
| } | ||
|
|
||
| const S = new Int32Array(1024); | ||
| for (let i = 0; i < 1024; i++) S[i] = ((i * 2654435761) ^ (i << 28)) | 0; // negatives + near-overflow | ||
| const P = new Int32Array(18); | ||
| for (let i = 0; i < 18; i++) P[i] = (i * 0x9e3779b1) | 0; | ||
| const lr = new Int32Array(2); | ||
|
|
||
| let feAcc = 0 | 0; | ||
| let ovAcc = 0 | 0; | ||
| let unAcc = 0 | 0; | ||
| for (let i = 0; i < 20000; i++) { | ||
| lr[0] = feAcc; | ||
| lr[1] = i; | ||
| feAcc = (feAcc ^ feistel(S, P, lr, 0)) | 0; | ||
| ovAcc = (ovAcc ^ overflow(S, i)) | 0; | ||
| unAcc = (unAcc ^ unrefine(S, i)) | 0; | ||
| } | ||
| console.log("feistel=" + feAcc); | ||
| console.log("overflow=" + ovAcc); | ||
| console.log("unrefine=" + unAcc); | ||
| console.log("postRead=" + postRead(S, 0x12345678 | 0)); | ||
|
|
||
| const Sp: number[] = new Array(1024); | ||
| for (let i = 0; i < 1024; i++) Sp[i] = ((i * 2654435761) ^ (i << 28)) | 0; | ||
| const Pp: number[] = new Array(18); | ||
| for (let i = 0; i < 18; i++) Pp[i] = (i * 0x9e3779b1) | 0; | ||
| const lrp: number[] = [0, 0]; | ||
| let plainAcc = 0 | 0; | ||
| for (let i = 0; i < 20000; i++) { | ||
| lrp[0] = plainAcc; | ||
| lrp[1] = i; | ||
| plainAcc = (plainAcc ^ feistelPlain(Sp, Pp, lrp, 0)) | 0; | ||
| } | ||
| console.log("feistelPlain=" + plainAcc); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🎯 Functional Correctness | 🟠 Major | ⚡ Quick win
Recursively inspect writes before promoting an i32 slot.
This scan only sees top-level
LocalSet/Updatestatements. A nested assignment or update inside an RHS is ignored, so a fractional/full-f64 write can leaveas_i32enabled and later reads can use a truncated i32 shadow. Traverse expression children, as the existing strict-write collector does.Proposed fix
fn region_i32_bounded_write_locals(stmts: &[Stmt]) -> std::collections::HashSet<u32> { let empty = std::collections::HashSet::new(); let mut written = std::collections::HashSet::new(); let mut disqualified = std::collections::HashSet::new(); + fn visit_expr( + expr: &Expr, + empty: &std::collections::HashSet<u32>, + written: &mut std::collections::HashSet<u32>, + disqualified: &mut std::collections::HashSet<u32>, + ) { + match expr { + Expr::LocalSet(id, value) => { + written.insert(*id); + if !crate::collectors::is_strictly_i32_bounded_expr( + value, empty, empty, empty, empty, &mut |_| {}, + ) { + disqualified.insert(*id); + } + visit_expr(value, empty, written, disqualified); + } + Expr::Update { id, .. } => { + disqualified.insert(*id); + } + _ => perry_hir::walker::walk_expr_children(expr, &mut |child| { + visit_expr(child, empty, written, disqualified); + }), + } + } + for stmt in stmts { - match stmt { - Stmt::Expr(Expr::LocalSet(id, value)) => { /* ... */ } - Stmt::Expr(Expr::Update { id, .. }) => { /* ... */ } - _ => {} + if let Stmt::Expr(expr) = stmt { + visit_expr(expr, &empty, &mut written, &mut disqualified); } }📝 Committable suggestion
🤖 Prompt for AI Agents