-
-
Notifications
You must be signed in to change notification settings - Fork 161
codegen: static magnitude bound keeps affine indices from wrapping i64 (#9294 follow-up) #9318
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
proggeramlug
merged 1 commit into
PerryTS:main
from
proggeramlug:fix/affine-magnitude-bound
Aug 31, 2026
Merged
Changes from all commits
Commits
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,29 @@ | ||
| **The affine index materialization can no longer wrap i64** (#9294 | ||
| follow-up, from a review flag on its sibling PR). | ||
|
|
||
| #9294 computed `a[<affine>]` indices in i64 on the claim that proven-i32 | ||
| leaves cannot overflow it. That is true for one multiply — `|i32 * i32|` is | ||
| at most 2^62 — and false beyond it: three chained near-2^31 factors reach | ||
| 2^93, wrap i64, and a wrapped value that happens to land inside `[0, len)` | ||
| passes the unsigned bounds check and reads a DIFFERENT element than the | ||
| generic path, silently — JS computes the index in doubles, goes out of | ||
| bounds, and yields `undefined`. | ||
|
|
||
| Measured honestly: the wrap is LATENT today, not live. Neither a | ||
| const-folded spelling nor parameter leaves of a triple-multiply chain | ||
| currently reach the affine lowering — admission happens to be blocked by | ||
| which locals carry i32 shadow slots, an accident of unrelated analyses | ||
| rather than a guarantee. Widening shadow coverage is a plausible future | ||
| change, and it would have turned this into a silent wrong-read with no | ||
| failing test anywhere. | ||
|
|
||
| The fix is a static magnitude bound, `affine_index_magnitude_bound`: | ||
| interval arithmetic in i128 at match time with every leaf at its i32 | ||
| extreme, admitting a tree only when its worst case fits i63. Admission | ||
| therefore costs nothing at run time; `i * size + k` (2^62 + 2^31) stays | ||
| admitted and matmul's numbers are unchanged, while any tree that could wrap | ||
| declines to the generic path. One shared predicate gates both the matcher | ||
| and the lowering, so the two cannot drift. A tripwire test pins the exact | ||
| 2^64 tree (`2^21 * 2^22 * (2^21 + k)`) to node's `NaN` under both collector | ||
| modes — it passes today on both sides and exists to fail the moment | ||
| admission widens past the bound. | ||
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,81 @@ | ||
| //! The affine index materialization cannot wrap i64 (#9294 follow-up). | ||
| //! | ||
| //! #9294 computed `a[<affine>]` indices in i64 on the claim that proven-i32 | ||
| //! leaves cannot overflow it. True for one multiply (|i32 * i32| <= 2^62), | ||
| //! false beyond: `2^21 * 2^22 * (2^21 + k)` at `k = 0` is exactly 2^64, the | ||
| //! i64 computation wraps to 0, the wrapped index passes the unsigned bounds | ||
| //! check, and the fast path reads `a[0]` — the WRONG element, silently — | ||
| //! where JS computes the index in doubles, goes out of bounds, and yields | ||
| //! `undefined` (NaN after the add). Flagged by review on the follow-up PR. | ||
| //! | ||
| //! The fix is a static magnitude bound (`affine_index_magnitude_bound`): | ||
| //! interval arithmetic in i128 at match time with every leaf at its i32 | ||
| //! extreme, admitting the tree only when its worst case fits i63 — so | ||
| //! admission costs nothing at run time and the matcher and the lowering | ||
| //! share one predicate. `i * size + k` (2^62 + 2^31) stays admitted; this | ||
| //! tree (~2^74) declines to the generic path. | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::process::Command; | ||
|
|
||
| fn perry_bin() -> PathBuf { | ||
| PathBuf::from(env!("CARGO_BIN_EXE_perry")) | ||
| } | ||
|
|
||
| const SOURCE: &str = r#" | ||
| function run(a: number[]): number { | ||
| const x = 2097152; | ||
| const y = 4194304; | ||
| const z = 2097152; | ||
| let s = 0.0; | ||
| for (let k = 0; k < 1; k++) { | ||
| s = s * 1.0 + a[x * y * (z + k)]; | ||
| } | ||
| return s; | ||
| } | ||
| const a: number[] = []; | ||
| for (let i = 0; i < 64; i++) a.push(7.5 + i); | ||
| console.log("s:" + run(a)); | ||
| "#; | ||
|
|
||
| /// The wrapped read must not happen: node's answer is NaN (the index is far | ||
| /// out of bounds in double arithmetic), and the wrap would print `s:7.5` — | ||
| /// element 0, in bounds, wrong. | ||
| #[test] | ||
| fn a_multiply_chain_that_wraps_i64_declines_to_the_generic_path() { | ||
| let dir = tempfile::tempdir().expect("tempdir"); | ||
| let entry = dir.path().join("main.ts"); | ||
| let output = dir.path().join("main_bin"); | ||
| std::fs::write(&entry, SOURCE).expect("write entry"); | ||
| let compile = Command::new(perry_bin()) | ||
| .current_dir(dir.path()) | ||
| .arg("compile") | ||
| .arg(&entry) | ||
| .arg("-o") | ||
| .arg(&output) | ||
| .env("PERRY_NO_CACHE", "1") | ||
| .output() | ||
| .expect("run perry compile"); | ||
| assert!( | ||
| compile.status.success(), | ||
| "perry compile failed\nstderr:\n{}", | ||
| String::from_utf8_lossy(&compile.stderr) | ||
| ); | ||
| for moving_gc in [false, true] { | ||
| let mut command = Command::new(&output); | ||
| command.current_dir(dir.path()); | ||
| if moving_gc { | ||
| command | ||
| .env("PERRY_GC_FORCE_EVACUATE", "1") | ||
| .env("PERRY_GC_VERIFY_EVACUATION", "1"); | ||
| } | ||
| let run = command.output().expect("run binary"); | ||
| assert!(run.status.success()); | ||
| assert_eq!( | ||
| String::from_utf8_lossy(&run.stdout), | ||
| "s:NaN\n", | ||
| "a wrapped affine index read an in-bounds element the generic path \ | ||
| never touches (moving_gc={moving_gc})" | ||
| ); | ||
| } | ||
| } |
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.
📐 Maintainability & Code Quality | 🟡 Minor | ⚡ Quick win
Avoid an unintended ATX heading.
Prefix
#9294with text such asIssueso this line is normal prose and markdownlint does not report MD018.🧰 Tools
🪛 markdownlint-cli2 (0.23.2)
[warning] 4-4: No space after hash on atx style heading
(MD018, no-missing-space-atx)
🤖 Prompt for AI Agents
Source: Linters/SAST tools