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

feat: simplify simple conditionals for brillig #7205

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

Conversation

guipublic
Copy link
Contributor

@guipublic guipublic commented Jan 28, 2025

Description

Problem*

Resolves #6394

Summary*

Add a pass which simplify simple if statements in brillig functions

Additional Context

The PR is working fine now, the memory consumption alert does not seem to be up-to-date.
The report from the last commit is correct: https://github.com/noir-lang/noir/actions/runs/13119725684/artifacts/2528887358

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.

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 Memory'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: 56ef385 Previous: 8d39337 Ratio
rollup-block-root 4900 MB 1230 MB 3.98

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

CC: @TomAFrench

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 'Test Suite Duration'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: b38e806 Previous: d5d6cb7 Ratio
noir-lang_schnorr_ 1 s 0 s +∞
noir-lang_noir_string_search_ 1 s 0 s +∞
noir-lang_noir_base64_ 2 s 1 s 2

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

CC: @TomAFrench

Copy link
Contributor

github-actions bot commented Jan 31, 2025

Changes to number of Brillig opcodes executed

Generated at commit: 4e06927a2f4d011dcfbdf012facb3314130615c5, compared to commit: 3a42eb5c68f9616f0ebe367c894f0376ba41e0ef

🧾 Summary (10% most significant diffs)

Program Brillig opcodes (+/-) %
brillig_conditional_inliner_max +4 ❌ +14.81%
brillig_conditional_inliner_zero +4 ❌ +14.81%
binary_operator_overloading_inliner_max +26 ❌ +13.98%
dont_deduplicate_call_inliner_max -2 ✅ -8.70%
dont_deduplicate_call_inliner_min -2 ✅ -8.70%
dont_deduplicate_call_inliner_zero -2 ✅ -8.70%
brillig_block_parameter_liveness_inliner_max -1,983 ✅ -68.05%
brillig_block_parameter_liveness_inliner_min -1,983 ✅ -68.05%
brillig_block_parameter_liveness_inliner_zero -1,983 ✅ -68.05%

Full diff report 👇
Program Brillig opcodes (+/-) %
brillig_conditional_inliner_max 31 (+4) +14.81%
brillig_conditional_inliner_zero 31 (+4) +14.81%
binary_operator_overloading_inliner_max 212 (+26) +13.98%
brillig_conditional_inliner_min 45 (+3) +7.14%
trait_impl_base_type_inliner_zero 75 (+5) +7.14%
trait_impl_base_type_inliner_min 275 (+12) +4.56%
slices_inliner_max 3,410 (+134) +4.09%
u128_inliner_max 25,191 (+908) +3.74%
slice_regex_inliner_max 3,361 (+108) +3.32%
slices_inliner_min 4,424 (+132) +3.08%
slice_regex_inliner_zero 4,167 (+108) +2.66%
u128_inliner_zero 41,881 (+908) +2.22%
inline_decompose_hint_brillig_call_inliner_min 280 (+6) +2.19%
embedded_curve_ops_inliner_min 680 (+14) +2.10%
embedded_curve_ops_inliner_zero 416 (+8) +1.96%
u128_inliner_min 49,421 (+908) +1.87%
slices_inliner_zero 3,668 (+66) +1.83%
slice_regex_inliner_min 8,743 (+106) +1.23%
binary_operator_overloading_inliner_zero 232 (+2) +0.87%
debug_logs_inliner_max 5,168 (+30) +0.58%
debug_logs_inliner_zero 5,477 (+30) +0.55%
binary_operator_overloading_inliner_min 404 (+2) +0.50%
derive_inliner_min 617 (+2) +0.33%
derive_inliner_zero 332 (+1) +0.30%
array_sort_inliner_min 680 (+2) +0.29%
debug_logs_inliner_min 10,347 (+30) +0.29%
sha256_var_witness_const_regression_inliner_zero 7,007 (+6) +0.09%
sha256_inliner_zero 11,439 (+9) +0.08%
6_inliner_zero 4,464 (+3) +0.07%
conditional_regression_short_circuit_inliner_zero 4,544 (+3) +0.07%
sha256_var_witness_const_regression_inliner_max 6,452 (+3) +0.05%
sha256_inliner_max 13,685 (+3) +0.02%
sha256_var_size_regression_inliner_max 15,725 (-1) -0.01%
array_dynamic_blackbox_input_inliner_min 22,394 (-4) -0.02%
array_dynamic_blackbox_input_inliner_zero 21,307 (-4) -0.02%
array_dynamic_blackbox_input_inliner_max 17,525 (-4) -0.02%
poseidon_bn254_hash_inliner_zero 182,200 (-285) -0.16%
poseidon_bn254_hash_width_3_inliner_zero 182,200 (-285) -0.16%
poseidon_bn254_hash_inliner_max 151,454 (-285) -0.19%
poseidon_bn254_hash_width_3_inliner_max 151,454 (-285) -0.19%
6_array_inliner_min 2,553 (-8) -0.31%
6_array_inliner_zero 2,483 (-8) -0.32%
6_array_inliner_max 1,529 (-8) -0.52%
main_bool_arg_inliner_max 68 (-2) -2.86%
main_bool_arg_inliner_min 68 (-2) -2.86%
main_bool_arg_inliner_zero 68 (-2) -2.86%
dont_deduplicate_call_inliner_max 21 (-2) -8.70%
dont_deduplicate_call_inliner_min 21 (-2) -8.70%
dont_deduplicate_call_inliner_zero 21 (-2) -8.70%
brillig_block_parameter_liveness_inliner_max 931 (-1,983) -68.05%
brillig_block_parameter_liveness_inliner_min 931 (-1,983) -68.05%
brillig_block_parameter_liveness_inliner_zero 931 (-1,983) -68.05%

Copy link
Contributor

github-actions bot commented Jan 31, 2025

Changes to Brillig bytecode sizes

Generated at commit: 4e06927a2f4d011dcfbdf012facb3314130615c5, compared to commit: 3a42eb5c68f9616f0ebe367c894f0376ba41e0ef

🧾 Summary (10% most significant diffs)

Program Brillig opcodes (+/-) %
regression_5435_inliner_max -9 ✅ -21.95%
regression_5435_inliner_min -9 ✅ -21.95%
regression_5435_inliner_zero -9 ✅ -21.95%
brillig_block_parameter_liveness_inliner_max -3,996 ✅ -81.57%
brillig_block_parameter_liveness_inliner_min -3,996 ✅ -81.57%
brillig_block_parameter_liveness_inliner_zero -3,996 ✅ -81.57%

Full diff report 👇
Program Brillig opcodes (+/-) %
slice_regex_inliner_zero 1,730 (+26) +1.53%
slices_inliner_max 2,164 (+32) +1.50%
slices_inliner_zero 2,120 (+28) +1.34%
slice_regex_inliner_min 2,151 (+24) +1.13%
slice_regex_inliner_max 2,345 (+26) +1.12%
slices_inliner_min 2,565 (+28) +1.10%
regression_5045_inliner_min 226 (+2) +0.89%
inline_decompose_hint_brillig_call_inliner_min 245 (+2) +0.82%
debug_logs_inliner_max 5,150 (+24) +0.47%
debug_logs_inliner_zero 5,447 (+24) +0.44%
embedded_curve_ops_inliner_min 517 (+2) +0.39%
debug_logs_inliner_min 9,775 (+24) +0.25%
u128_inliner_min 2,283 (-2) -0.09%
u128_inliner_zero 1,978 (-2) -0.10%
poseidon_bn254_hash_width_3_inliner_max 5,267 (-6) -0.11%
poseidon_bn254_hash_inliner_max 5,267 (-6) -0.11%
poseidon_bn254_hash_inliner_zero 4,696 (-6) -0.13%
poseidon_bn254_hash_width_3_inliner_zero 4,696 (-6) -0.13%
embedded_curve_ops_inliner_zero 360 (-1) -0.28%
sha256_var_size_regression_inliner_max 1,719 (-5) -0.29%
u128_inliner_max 2,686 (-10) -0.37%
trait_impl_base_type_inliner_min 249 (-1) -0.40%
array_dynamic_blackbox_input_inliner_min 1,278 (-7) -0.54%
binary_operator_overloading_inliner_min 356 (-2) -0.56%
array_sort_inliner_min 353 (-2) -0.56%
derive_inliner_zero 331 (-2) -0.60%
derive_inliner_min 589 (-4) -0.67%
array_dynamic_blackbox_input_inliner_zero 1,025 (-7) -0.68%
array_sort_inliner_max 290 (-2) -0.68%
array_sort_inliner_zero 290 (-2) -0.68%
array_dynamic_blackbox_input_inliner_max 839 (-7) -0.83%
binary_operator_overloading_inliner_zero 226 (-2) -0.88%
trait_impl_base_type_inliner_zero 65 (-1) -1.52%
brillig_conditional_inliner_min 45 (-1) -2.17%
6_array_inliner_min 445 (-10) -2.20%
6_array_inliner_zero 435 (-10) -2.25%
6_array_inliner_max 376 (-10) -2.59%
binary_operator_overloading_inliner_max 383 (-15) -3.77%
main_bool_arg_inliner_max 65 (-3) -4.41%
main_bool_arg_inliner_min 65 (-3) -4.41%
main_bool_arg_inliner_zero 65 (-3) -4.41%
dont_deduplicate_call_inliner_max 23 (-3) -11.54%
dont_deduplicate_call_inliner_min 23 (-3) -11.54%
dont_deduplicate_call_inliner_zero 23 (-3) -11.54%
regression_5435_inliner_max 32 (-9) -21.95%
regression_5435_inliner_min 32 (-9) -21.95%
regression_5435_inliner_zero 32 (-9) -21.95%
brillig_block_parameter_liveness_inliner_max 903 (-3,996) -81.57%
brillig_block_parameter_liveness_inliner_min 903 (-3,996) -81.57%
brillig_block_parameter_liveness_inliner_zero 903 (-3,996) -81.57%

@guipublic guipublic marked this pull request as ready for review January 31, 2025 17:11
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: 06d26db Previous: a9e9850 Ratio
global_var_regression_entry_points 0.009 s 0.007 s 1.29

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

CC: @TomAFrench

@guipublic guipublic requested a review from a team February 4, 2025 10:15
Copy link
Contributor

@vezenovm vezenovm left a comment

Choose a reason for hiding this comment

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

Did an initial scan. Do you know why we are regressing in brillig_conditional?

BrilligVariable::BrilligArray(else_array),
) => {
// Pointer to the array which result from the if-else
let pointer = self.brillig_context.allocate_register();
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need to allocate another register here? Can we just use the result from above and result.extract_register()?

let mut instruction = self.function.dfg[id].clone();
instruction.map_values_mut(|id| self.resolve(id));
self.function.dfg.set_instruction(id, instruction);
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
}
}

nit just to separate the } from the next comment

if mapping.contains_key(k) {
unreachable!("cannot merge key");
}
if mapping.contains_key(v) {
Copy link
Contributor

Choose a reason for hiding this comment

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

We want to check contains_key for the value here?

BinaryOp::Lt => 5,
BinaryOp::And
| BinaryOp::Or
| BinaryOp::Xor => 1, //todo
Copy link
Contributor

Choose a reason for hiding this comment

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

This TODO doesn't have a description and there are a couple TODOs in this function. Could you make issues to handle them if we are not going to in this PR?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Happy to address these in this PR. The point is to provide realistic brillig cost for each opcode. The idea is to improve execution speed so the cost should be the runtime cost in whatever unit as along as it is the same for all opcodes. My numbers can certainly be improved.
I will add a warning instead of a todo at the beginning of the function to indicate that the numbers are estimates and can be improved.

Comment on lines +195 to +197
let index = dfg.get_numeric_constant(*index);
let mut ok_bound = false;
if let Some(index) = index {
Copy link
Contributor

@vezenovm vezenovm Feb 5, 2025

Choose a reason for hiding this comment

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

Suggested change
let index = dfg.get_numeric_constant(*index);
let mut ok_bound = false;
if let Some(index) = index {
let mut ok_bound = false;
if let (Some(index), Some(len)) = (dfg.get_numeric_constant(*index), dfg.try_get_array_length(*array)) {

I think something like this would be more idiomatic and reduces nesting

Comment on lines +373 to +374
let po = post_order.as_slice();
for block in po {
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
let po = post_order.as_slice();
for block in po {
for block in post_order.as_slice() {

function: &Function,
) -> Option<BasicConditional> {
// jump overhead is the cost for doing the conditional and jump around the blocks
// I use 10 as a rough estimate, real cost is less.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// I use 10 as a rough estimate, real cost is less.
// We use 10 as a rough estimate, the real cost is less.

let mut successors = cfg.successors(block);
let mut result = None;
// a conditional must have 2 branches
if successors.len() == 2 {
Copy link
Contributor

Choose a reason for hiding this comment

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

Let's just early return with None if successors.len() != 2 to reduce this nesting

call_stack: _,
}) = function.dfg[block].terminator()
{
if left == *then_destination {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this would be more readable if we did something like:

let (block_then, block_else) = if left == *then_destination {
   (Some(left), None)
} else if left == *else_destination {
   (None, Some(left))
};
result = Some(BasicConditional {
       block_entry: block,
       block_then,
       block_else,
       block_exit: right,
});

call_stack: _,
}) = function.dfg[block].terminator()
{
if right == *else_destination {
Copy link
Contributor

Choose a reason for hiding this comment

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

Same thing as above for the else if left_successors_len == 1 && next_left == Some(right) { case

@TomAFrench
Copy link
Member

Do you know why we are regressing in brillig_conditional?

Agreed that it would be good to have more clarity on this. We've got some serious benefits when conditionally mutating large structs but there's quite a few regressions.

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.

Flatten simple if-else statements in brillig to avoid unnecessary jump statements
3 participants