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

fix(brillig): Globals entry point reachability analysis #7188

Merged
merged 32 commits into from
Jan 29, 2025

Conversation

vezenovm
Copy link
Contributor

@vezenovm vezenovm commented Jan 24, 2025

Description

Problem*

Resolves #7158

Was not sure whether to label this as a feat or a fix. It fixes repeated global initialization across multiple Brillig entry points, however, it is a pretty meaningful change in how we generate globals so I thought about labelling it a feat. As I labelled the issue a bug I have used the fix prefix.

Summary*

We now track globals Brillig artifacts by their entry point rather than having a single globals artifact for all entry points.

A few changes have been made:

  1. We now track the used_globals by function rather than over the entire program.
  2. We made a new BrilligGlobals context structure to simplify code gen for globals.
  3. When generating BrilligGlobals we create some initial state from the function call graph.
    /// Both `used_globals` and `brillig_entry_points` need to be built
    /// from a function call graph.
    /// 
    /// Maps a Brillig function to the globals used in that function.
    /// This includes all globals used in functions called internally.
    used_globals: HashMap<FunctionId, HashSet<ValueId>>,
    /// Maps a Brillig entry point to all functions called in that entry point.
    /// This includes any nested calls as well, as we want to be able to associate
    /// any Brillig function with the appropriate global allocations.
    brillig_entry_points: HashMap<FunctionId, HashSet<FunctionId>>,
  1. We then declare the globals for each entry point by looping over brillig_entry_points.
  2. We construct a new mapping inner_call_to_entry_point
    /// Maps an inner call to its Brillig entry point
    /// This is simply used to simplify fetching global allocations when compiling
    /// individual Brillig functions.
    inner_call_to_entry_point: HashMap<FunctionId, Vec<FunctionId>>,
  1. After compiling the globals we instantiate a map of entry points to global allocations.
  2. The GlobalInit label has been updated to be associated with a function id representing an entry point. We now have GlobalInit(entry_point: FunctionId).

Additional Context

Most of the line additions are from the large global array included in test_programs/execution_success/global_var_regression_entry_points/src/consts.nr file. This change is most meaningful when tested with a large amount of globals across many entry points.

I compiled global_var_regression_entry_points with nargo compile --force --inliner-aggressiveness -100000000000000.
Running NOIR_LOG=trace nargo execute looking at nargo::ops::execute: close time.busy for multiple runs:
On master:
~35ms
This PR:
~7ms

About a 5x improvement in this example.

Looking at the execution report for rollup-block-root we look to see a large benefit:
This PR:

  {
    "artifact_name": "rollup-block-root",
    "time": "36.700s"
  },

While on master we are at ~41s. So about a 12% improvement.

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.

@vezenovm vezenovm marked this pull request as ready for review January 28, 2025 16:56
@vezenovm vezenovm requested a review from a team January 28, 2025 17:14
Copy link
Member

@TomAFrench TomAFrench left a comment

Choose a reason for hiding this comment

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

Generally looks good, some nits.

@vezenovm vezenovm requested a review from TomAFrench January 29, 2025 14:39
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 'Compilation Time'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.20.

Benchmark suite Current: abdf7b9 Previous: 784a562 Ratio
sha256_regression 1.27 s 0.989 s 1.28

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

CC: @TomAFrench

Copy link
Member

@TomAFrench TomAFrench left a comment

Choose a reason for hiding this comment

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

LGTM

@TomAFrench TomAFrench enabled auto-merge January 29, 2025 15:41
@TomAFrench TomAFrench added this pull request to the merge queue Jan 29, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Jan 29, 2025
@vezenovm vezenovm added this pull request to the merge queue Jan 29, 2025
Merged via the queue into master with commit bdcfd38 Jan 29, 2025
99 checks passed
@vezenovm vezenovm deleted the mv/globals-entry-point-reachability branch January 29, 2025 16:44
AztecBot added a commit to AztecProtocol/aztec-packages that referenced this pull request Jan 31, 2025
chore: bump gates diff (noir-lang/noir#7245)
feat: simplify subtraction from self to return zero (noir-lang/noir#7189)
feat: allow specifying multiple patterns in nargo test (noir-lang/noir#7186)
fix: Avoid type error when calling something with a type alias of a function (noir-lang/noir#7239)
feat: Allow resolved types in constructors (noir-lang/noir#7223)
chore: clarify to_radix docs examples (noir-lang/noir#7230)
chore: fix struct example (noir-lang/noir#7198)
feat(optimization): Add purity analysis to SSA (noir-lang/noir#7197)
chore: start tracking time to run critical library tests (noir-lang/noir#7221)
chore: Rework defunctionalize pass to not rely on DFG bugs (noir-lang/noir#7222)
fix(brillig): Globals entry point reachability analysis  (noir-lang/noir#7188)
chore: update docs to use devcontainer feature (noir-lang/noir#7206)
chore(ci): Add test for global vars entry points regression (noir-lang/noir#7209)
chore(ssa): Flip the SSA Brillig constraint check to off by default (noir-lang/noir#7211)
chore(docs): moving references to noir-starter to awesome-noir (noir-lang/noir#7203)
chore: build docs in the merge queue (noir-lang/noir#7218)
fix: correct reversed callstacks (noir-lang/noir#7212)
chore: exclude dependency fetching time from benchmarks (noir-lang/noir#7210)
feat(experimental): Support enums in comptime code (noir-lang/noir#7194)
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.

Reachability analysis for Brillig entry point globals initialization
3 participants