Skip to content
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

chore: add compile-time assertions on generic arguments of stdlib functions #6981

Open
wants to merge 35 commits into
base: master
Choose a base branch
from

Conversation

michaeljklein
Copy link
Contributor

Description

Problem*

Resolves #6962

Summary*

Additional Context

Documentation*

Check one:

  • No documentation needed.
  • Documentation included in this PR.
  • [For Experimental Features] Documentation to be submitted in a separate PR.

PR Checklist*

  • I have tested the changes locally.
  • I have formatted the changes with Prettier and/or cargo fmt on default settings.

@michaeljklein michaeljklein changed the title Add compile-time assertions on generic arguments of stdlib functions chore: add compile-time assertions on generic arguments of stdlib functions Jan 7, 2025
Copy link
Contributor

github-actions bot commented Jan 7, 2025

Compilation Report

Program Compilation Time %
sha256_regression 1.050s 2%
regression_4709 0.819s 4%
ram_blowup_regression 16.300s 0%
rollup-root 3.490s -5%
rollup-merge 2.034s -4%
rollup-block-root-single-tx 137.000s -7%
rollup-block-root-empty 2.138s -5%
rollup-block-root 136.000s -5%
rollup-block-merge 3.608s 3%
rollup-base-public 28.040s -4%
rollup-base-private 11.960s 19%
private-kernel-tail 0.988s -2%
private-kernel-reset 6.242s 3%
private-kernel-inner 2.098s -6%

Copy link
Contributor

github-actions bot commented Jan 7, 2025

Execution Report

Program Execution Time %
sha256_regression 0.051s 1%
regression_4709 0.001s 0%
ram_blowup_regression 0.600s 1%
rollup-root 0.104s -1%
rollup-merge 0.006s 0%
rollup-block-root 37.000s 0%
rollup-block-merge 0.105s 0%
rollup-base-public 1.224s -1%
rollup-base-private 0.454s 1%
private-kernel-tail 0.019s -10%
private-kernel-reset 0.313s 0%
private-kernel-inner 0.068s -2%

Copy link
Contributor

github-actions bot commented Jan 7, 2025

Compilation Memory Report

Program Peak Memory
keccak256 77.68M
workspace 123.99M
regression_4709 424.20M
ram_blowup_regression 1.46G
rollup-root 601.09M
rollup-merge 494.09M
rollup-block-root-single-tx 16.06G
rollup-block-root-empty 488.22M
rollup-block-root 16.07G
rollup-block-merge 601.08M
rollup-base-public 2.38G
rollup-base-private 1.14G
private-kernel-tail 207.30M
private-kernel-reset 584.32M
private-kernel-inner 294.61M

Copy link
Contributor

github-actions bot commented Jan 7, 2025

Execution Memory Report

Program Peak Memory
keccak256 74.75M
workspace 123.88M
regression_4709 316.06M
ram_blowup_regression 512.64M
rollup-root 498.44M
rollup-merge 473.17M
rollup-block-root 1.22G
rollup-block-merge 498.45M
rollup-base-public 733.99M
rollup-base-private 590.63M
private-kernel-tail 180.75M
private-kernel-reset 245.36M
private-kernel-inner 208.77M

Copy link
Contributor

github-actions bot commented Jan 15, 2025

Changes to number of Brillig opcodes executed

Generated at commit: a5a812a56b6a12c455d1c373d0247d1e9f29eaeb, compared to commit: c8d5ce5e72f3171f24e4fe2b94ac098ba6d99ef6

🧾 Summary (10% most significant diffs)

Program Brillig opcodes (+/-) %
u128_inliner_zero +121 ❌ +0.27%
to_bytes_consistent_inliner_zero -35 ✅ -5.04%

Full diff report 👇
Program Brillig opcodes (+/-) %
u128_inliner_zero 44,453 (+121) +0.27%
to_bytes_consistent_inliner_zero 659 (-35) -5.04%

@michaeljklein michaeljklein marked this pull request as ready for review January 16, 2025 16:16
@TomAFrench
Copy link
Member

@michaeljklein Is this waiting on anything other than formatting?

@michaeljklein
Copy link
Contributor Author

@TomAFrench Nope, should be good to go

@TomAFrench
Copy link
Member

This has still got formatting issues. There's also a regression in u128 which I'm not sure on the cause of but these assertions shouldn't affect the final artifact.

@michaeljklein
Copy link
Contributor Author

@TomAFrench I was able to recreate the difference, but haven't been able to figure out why it's happening:

$ ~/.nargo/bin/nargo info --silence-warnings --profile-execution --json --inliner-aggressiveness 0
{"programs":[{"package_name":"u128","functions":[{"name":"main","opcodes":0}],"unconstrained_functions":[{"name":"main","opcodes":44332}]}]}

$ cargo run info --silence-warnings --profile-execution --json --inliner-aggressiveness 0
{"programs":[{"package_name":"u128","functions":[{"name":"main","opcodes":0}],"unconstrained_functions":[{"name":"main","opcodes":44453}]}]}

(44453 - 44332 = 121, the difference reported above)

When I run --show-ssa, I get the same result for both programs:

$ cargo run compile --show-ssa --inliner-aggressiveness 0
$ ~/.nargo/bin/nargo compile --show-ssa --inliner-aggressiveness 0

(Also when compiling with --force-brillig or when running execute.)

So I'm not sure why there's a ~0.2% difference, but it seems likely to be unrelated changes in master and/or a measurement error because 1) static_assert is rejected in Brillig, 2) the only compiler change in this PR is to add support for static_assert at comptime.

Copy link
Contributor

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'Execution Time'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: 55fcc95 Previous: 8804f0a Ratio
regression_4709 0.002 s 0.001 s 2

This comment was automatically generated by workflow using github-action-benchmark.

CC: @TomAFrench

Comment on lines 419 to 430
.unwrap_or_else(|err| {
let error_string: String =
err.iter().fold(String::new(), |mut output, diagnostic| {
let _ = write!(
output,
"{}\n---\n",
diagnostic_to_string(diagnostic, &file_manager)
);
output
});
panic!("Failed to compile:\n\n{}", error_string)
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going on here?

Copy link
Contributor Author

@michaeljklein michaeljklein Jan 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Patch so that test_transform_program_is_idempotent shows failing test_program's in a readable way.

I've simplified it a bit.

@TomAFrench
Copy link
Member

We took a look into the regression and the result is due to the changes in weights causing things that would otherwise be inlined to not be inlined. This will be resolved by #7148

static_assert(
N <= modulus_le_bytes().len(),
"N must be less than or equal to modulus_le_bytes().len()",
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't comment on it directly but one requirement of to_le_radix is that the radix is a power of 2.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also greater than 1 and lte 256

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I've added those three checks + basic tests that they're triggered. (wip)

michaeljklein and others added 3 commits January 22, 2025 12:43
…re tests, wip debugging test failures, cleanup diagnostic printing
…e debug 'static_assert's and only check radix when not is_unconstrained, simplify to nothing when invalid radix, check radix range when generating ACIR, add brillig-specific to_radix tests, add handling for unconstrained mode for acir to_radix tests
Copy link
Contributor

github-actions bot commented Jan 22, 2025

Changes to Brillig bytecode sizes

Generated at commit: a5a812a56b6a12c455d1c373d0247d1e9f29eaeb, compared to commit: c8d5ce5e72f3171f24e4fe2b94ac098ba6d99ef6

🧾 Summary (10% most significant diffs)

Program Brillig opcodes (+/-) %
u128_inliner_zero -10 ✅ -0.50%
to_bytes_consistent_inliner_zero -53 ✅ -40.46%

Full diff report 👇
Program Brillig opcodes (+/-) %
u128_inliner_zero 1,991 (-10) -0.50%
to_bytes_consistent_inliner_zero 78 (-53) -40.46%

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add compile-time assertions on generic arguments of stdlib functions
2 participants