-
-
Notifications
You must be signed in to change notification settings - Fork 144
perf(codegen): reach integer modulo fast path for i32 counters #6388
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
6 commits
Select commit
Hold shift + click to select a range
e589691
fix(api): register ws readyState in manifest
TheHypnoo 879820b
perf(codegen): reach integer modulo fast path for i32 counters
TheHypnoo e120ae1
Merge branch 'main' into perf/value-first-i32-modulo
TheHypnoo cf5a29b
docs(api): regenerate reference for ws readyState
TheHypnoo d55c606
Merge branch 'main' into perf/value-first-i32-modulo
TheHypnoo 2d48b7a
test(codegen): align POD coercion IR contract
TheHypnoo 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
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
144 changes: 144 additions & 0 deletions
144
crates/perry-codegen/tests/native_proof_regressions/integer_modulo.rs
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,144 @@ | ||
| use super::*; | ||
|
|
||
| fn modulo(left: Expr, right: Expr) -> Expr { | ||
| Expr::Binary { | ||
| op: BinaryOp::Mod, | ||
| left: Box::new(left), | ||
| right: Box::new(right), | ||
| } | ||
| } | ||
|
|
||
| fn factorial_shaped_ir(divisor: Expr) -> String { | ||
| compile_ir( | ||
| "value_first_i32_modulo.ts", | ||
| vec![ | ||
| number_let(1, "sum", true, int(0)), | ||
| number_let(3, "iterations", false, int(64)), | ||
| for_loop( | ||
| 2, | ||
| local(3), | ||
| vec![Stmt::Expr(Expr::LocalSet( | ||
| 1, | ||
| Box::new(add(local(1), modulo(local(2), divisor))), | ||
| ))], | ||
| ), | ||
| Stmt::Return(Some(local(1))), | ||
| ], | ||
| ) | ||
| } | ||
|
|
||
| fn assert_integer_modulo_with_negative_zero_repair(ir: &str) { | ||
| assert!( | ||
| ir.contains("srem i64"), | ||
| "signed i32 counter modulo a positive Expr::Integer should reach the existing integer remainder path:\n{ir}" | ||
| ); | ||
| assert!( | ||
| !ir.contains("frem double"), | ||
| "eligible signed i32 counter modulo must not retain floating remainder:\n{ir}" | ||
| ); | ||
| assert!( | ||
| ir.contains("icmp eq i64") | ||
| && ir.contains("fcmp olt double") | ||
| && ir.contains("fneg double") | ||
| && ir.contains("select i1"), | ||
| "integer remainder must retain the existing IEEE-754 negative-zero repair:\n{ir}" | ||
| ); | ||
| } | ||
|
|
||
| fn assert_floating_modulo(ir: &str) { | ||
| assert!( | ||
| ir.contains("frem double"), | ||
| "ineligible modulo shape must remain on floating remainder:\n{ir}" | ||
| ); | ||
| assert!( | ||
| !ir.contains("srem i64") && !ir.contains("srem i32"), | ||
| "ineligible modulo shape must not emit integer remainder:\n{ir}" | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn i32_counter_mod_positive_literal_reaches_integer_fast_path() { | ||
| assert_integer_modulo_with_negative_zero_repair(&factorial_shaped_ir(int(1000))); | ||
| } | ||
|
|
||
| #[test] | ||
| fn i32_counter_mod_unsafe_or_nonliteral_divisors_keep_frem() { | ||
| let cases = [ | ||
| ("integral_number", number(1000.0)), | ||
| ("zero", int(0)), | ||
| ("negative", int(-1)), | ||
| ("fractional", number(2.5)), | ||
| ("out_of_i32", int(i64::from(i32::MAX) + 1)), | ||
| ]; | ||
| for (case, divisor) in cases { | ||
| let ir = factorial_shaped_ir(divisor); | ||
| assert!( | ||
| ir.contains("frem double"), | ||
| "{case} divisor must remain on floating remainder:\n{ir}" | ||
| ); | ||
| assert!( | ||
| !ir.contains("srem i64") && !ir.contains("srem i32"), | ||
| "{case} divisor must not emit integer remainder:\n{ir}" | ||
| ); | ||
| } | ||
|
|
||
| let dynamic_ir = compile_ir( | ||
| "value_first_i32_modulo_dynamic_divisor.ts", | ||
| vec![ | ||
| number_let(1, "sum", true, int(0)), | ||
| number_let(3, "divisor", false, int(7)), | ||
| number_let(4, "iterations", false, int(64)), | ||
| for_loop( | ||
| 2, | ||
| local(4), | ||
| vec![Stmt::Expr(Expr::LocalSet( | ||
| 1, | ||
| Box::new(add(local(1), modulo(local(2), local(3)))), | ||
| ))], | ||
| ), | ||
| Stmt::Return(Some(local(1))), | ||
| ], | ||
| ); | ||
| assert_floating_modulo(&dynamic_ir); | ||
| } | ||
|
|
||
| #[test] | ||
| fn non_i32_left_operands_keep_frem() { | ||
| let f64_ir = compile_ir( | ||
| "value_first_f64_modulo.ts", | ||
| vec![ | ||
| number_let(1, "value", false, number(5.5)), | ||
| Stmt::Return(Some(modulo(local(1), int(2)))), | ||
| ], | ||
| ); | ||
| assert_floating_modulo(&f64_ir); | ||
|
|
||
| // A `>>> 0` initializer plus only `>>> 0` writes is the harness's existing | ||
| // way to obtain an unsigned i32 shadow slot. The specialization must not | ||
| // treat its native bit pattern as a signed dividend. | ||
| let u32_ir = compile_ir( | ||
| "value_first_u32_modulo.ts", | ||
| vec![ | ||
| number_let(1, "sum", true, int(0)), | ||
| number_let(3, "iterations", false, int(64)), | ||
| Stmt::For { | ||
| init: Some(Box::new(number_let(2, "i", true, ushr_zero(int(0))))), | ||
| condition: Some(Expr::Compare { | ||
| op: CompareOp::Lt, | ||
| left: Box::new(local(2)), | ||
| right: Box::new(local(3)), | ||
| }), | ||
| update: Some(Expr::LocalSet( | ||
| 2, | ||
| Box::new(ushr_zero(add(local(2), int(1)))), | ||
| )), | ||
| body: vec![Stmt::Expr(Expr::LocalSet( | ||
| 1, | ||
| Box::new(add(local(1), modulo(local(2), int(7)))), | ||
| ))], | ||
| }, | ||
| Stmt::Return(Some(local(1))), | ||
| ], | ||
| ); | ||
| assert_floating_modulo(&u32_ir); | ||
| } |
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,27 @@ | ||
| let sum = 0; | ||
| for (let i = 0; i < 10000; i++) { | ||
| sum = sum + (i % 1000); | ||
| } | ||
| console.log("positive-loop:" + sum); | ||
|
|
||
| let sawNegativeZero = false; | ||
| for (let i = -6; i <= 0; i++) { | ||
| const value = i % 3; | ||
| if (Object.is(value, -0)) { | ||
| sawNegativeZero = true; | ||
| } | ||
| } | ||
| console.log("negative-loop-zero:" + sawNegativeZero); | ||
|
|
||
| console.log("fractional:" + (5.5 % 2)); | ||
| console.log("nan:" + (NaN % 2)); | ||
| console.log("infinity:" + (Infinity % 2)); | ||
| console.log("zero-divisor:" + (5 % 0)); | ||
| console.log("infinite-divisor:" + (5 % Infinity)); | ||
| console.log("negative-zero:" + Object.is(-0 % 3, -0)); | ||
|
|
||
| let dynamicZero = 0; | ||
| console.log("dynamic-zero-divisor:" + (5 % dynamicZero)); | ||
| console.log("negative-divisor:" + (5 % -2)); | ||
| console.log("min-i32-negative-one:" + Object.is(-2147483648 % -1, -0)); | ||
| console.log("wide-safe-integer:" + (9007199254740991 % 97)); |
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 | 🟠 Major | ⚡ Quick win
Move acceptance coverage to unit tests.
As per coding guidelines, put acceptance coverage in unit tests visible to
cargo-test, because integration suites undercrates/*/tests/*.rsdo not run on every PR. Please move this regression test (and any other newly added coverage, such as theinteger_modulotests) into a#[cfg(test)]unit test module within thesrc/directory to ensure they are reliably executed in CI.🤖 Prompt for AI Agents
Source: Coding guidelines