Skip to content

fix(ssa_interpreter): push_back and pop_back to slices with padding#9320

Merged
aakoshh merged 4 commits intomasterfrom
af/9312-fix-ssa-inter-pop-mixed
Jul 25, 2025
Merged

fix(ssa_interpreter): push_back and pop_back to slices with padding#9320
aakoshh merged 4 commits intomasterfrom
af/9312-fix-ssa-inter-pop-mixed

Conversation

@aakoshh
Copy link
Contributor

@aakoshh aakoshh commented Jul 24, 2025

Description

Problem*

Resolves #9312

Summary*

Change slice_push_back and slice_push_front to take into account any potential extra items in the slice which are beyond its length. Push and pop must operate before this region. This is achieved by removing any potential extras, since they are never accessed by the interpreter.

Additional Context

This situation arises when we have something like this code:

let a: [u32] = [1, 2];
let b: [u32] = [3, 4, 5, 6]; 
let c: [u32] = if p { a } else { b };
let (d, e) = c.pop_back();
let f = c.push_back(7);

The SSA pass that removes the IfElse expressions creates a slice like this:

c_len = p * 2 + (1 - p) * 4
c_data = [p * a[0] + (1-p) * b[0], p * a[1] + (1-p) * b[1], b[2], b[3]]

If p is true, c ends up looking like [1,2,5,6], and after pushing we want it to be (3, [1,2,7,5,6]), or if we pop then it should return (1, [1,5,6], 2) (length, data, elem).

Initially I appended the extra items back onto the slice, but recognised some alternatives:

  • We could drop the extras; at least I can't think of a situation outside of printing where they would come into play.
  • We could overwrite the extras in push_back, rather than grow the slice, to reclaim space; not like the interpreter is necessarily built to be efficient, just to maybe match what ACIR or Brillig might do.

I checked what ACIR passes to the print oracle after a push/pop, but it doesn't looks like something the interpreter aims to match:

fn main(a: bool) {
    let s0: [u32] = if a { &[10, 20] } else { &[30, 40, 50] };
    println(s0);
    let (s1, _) = s0.pop_back();
    println(s1);
    let s2 = s0.push_back(60);
    println(s2);
}

The values passed to the foreign function look like this:

args[0] = Single(2)
args[1] = Array([10, 20, 0])
&[10, 20]
args[0] = Single(1)
args[1] = Array([10, 20, 0])
&[10]
args[0] = Single(3)
args[1] = Array([10, 20, 60, 60])
&[10, 20, 60]

So popping didn't make the array shorter, just decreased the length, and pushing both filled the extra element and added a new one.

For this reason I concluded that the extra elements shouldn't be relied on to have any particular content, and it's best if we truncate the slices in the interpreter during pushing and popping, rather than go the extra mile to keep the extra elements. I didn't update other slice operations though.

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.

@aakoshh aakoshh requested a review from jfecher July 24, 2025 19:51
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: 2302d21 Previous: 42429ab Ratio
sha512-100-bytes 1.969 s 1.592 s 1.24

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: 2302d21 Previous: 42429ab Ratio
test_report_AztecProtocol_aztec-packages_noir-projects_noir-protocol-circuits_crates_private-kernel-lib 2 s 1 s 2
test_report_AztecProtocol_aztec-packages_noir-projects_noir-protocol-circuits_crates_rollup-lib 4 s 2 s 2
test_report_AztecProtocol_aztec-packages_noir-projects_noir-protocol-circuits_crates_types 2 s 1 s 2
test_report_zkpassport_noir-ecdsa_ 2 s 1 s 2

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

CC: @TomAFrench

@aakoshh aakoshh changed the title fix(ssa-interpreter): push_back and pop_back to slices with padding fix(ssa_interpreter): push_back and pop_back to slices with padding Jul 25, 2025
@jfecher
Copy link
Contributor

jfecher commented Jul 25, 2025

and pushing both filled the extra element and added a new one.

I wonder if this could be related to any of the performance issues with slices.

Maybe this could happen if e.g. you have a if foo { slice = slice.push_back(..) } which causes us to merge a slice of length N with one of length N + 1, so you'd get these empty spaces pushed every loop effectively and not used by the next push back.

@aakoshh aakoshh requested a review from vezenovm July 25, 2025 15:22
@aakoshh aakoshh added this pull request to the merge queue Jul 25, 2025
Merged via the queue into master with commit dd0bc23 Jul 25, 2025
103 checks passed
@aakoshh aakoshh deleted the af/9312-fix-ssa-inter-pop-mixed branch July 25, 2025 16:19
github-merge-queue bot pushed a commit to AztecProtocol/aztec-packages that referenced this pull request Aug 4, 2025
Automated pull of nightly from the
[noir](https://github.com/noir-lang/noir) programming language, a
dependency of Aztec.
BEGIN_COMMIT_OVERRIDE
fix: forbid self-referencing type aliases
(noir-lang/noir#9103)
chore: add a mem2reg test for when all references need to be invalidated
(noir-lang/noir#9377)
fix(ssa): Do not check ArrayGet/Set as unreachable for Brillig
(noir-lang/noir#9376)
chore: use SSA parser in all mem2reg tests
(noir-lang/noir#9372)
fix: trait where clause check fixes
(noir-lang/noir#9369)
fix: Correct doc comments for SSA passes
(noir-lang/noir#9371)
fix: prevent `SignedField::from(i128::MIN)` from crashing
(noir-lang/noir#9366)
fix: allow constants in the type-system to be negative
(noir-lang/noir#9360)
feat: show circuit output as a value of the program's return type
(noir-lang/noir#9364)
feat: add `FunctionDefinition::visibility`
(noir-lang/noir#9363)
chore(docs): Add example for `$crate` in docs
(noir-lang/noir#9361)
fix: Prevent accidental tuple sharing in comptime code
(noir-lang/noir#9313)
fix: perserve purities after SSA normalization
(noir-lang/noir#9355)
fix: modulo overflow in comptime
(noir-lang/noir#9348)
fix: handle short-syntax for trait constraints on trait generics
(noir-lang/noir#9167)
chore: enhance trait constraint comment
(noir-lang/noir#9358)
fix: replace implicitly added named generics with fresh type vars in
check_trait_impl_where_clause_matches_trait_where_clause
(noir-lang/noir#9352)
fix: push definition trait constraints after trait item constraint
(noir-lang/noir#9354)
chore(ci): Update status of noir_json_parser
(noir-lang/noir#9351)
fix(ssa): Keep reference count increments for array set values
(noir-lang/noir#9344)
chore: remove unused `compile_workspace`
(noir-lang/noir#9353)
chore: try printing byte arrays as strings in the SSA interpreter
(noir-lang/noir#9346)
feat(lsp): allow opening noir stdlib files
(noir-lang/noir#9339)
fix: do u128 operations with u128, not i128
(noir-lang/noir#9345)
chore(acir): ACIR parser error handling for blackbox inputs/outputs
(noir-lang/noir#9342)
fix: prevent invalid types in test/fuzz functions
(noir-lang/noir#9343)
chore(lsp): avoid redundant type checking
(noir-lang/noir#9337)
feat(acir): Parse ACIR memory and call opcodes
(noir-lang/noir#9331)
fix(ssa_gen): Add constraint on slice length before popping
(noir-lang/noir#9323)
chore: impl for u16 conversions
(noir-lang/noir#9314)
fix: substitute bindings in type before canonicalization
(noir-lang/noir#9328)
fix(ssa_interpreter): `push_back` and `pop_back` to slices with padding
(noir-lang/noir#9320)
fix: wildcard type should be allowed in lambda parameter types
(noir-lang/noir#9325)
chore: graceful handling of SIGPIPE
(noir-lang/noir#9075)
feat: return unsolvable opcode from `CircuitSimulator`
(noir-lang/noir#8943)
fix: allow nested fmtstr (noir-lang/noir#9309)
feat: Initial ACIR parser (arithmetic exprs and black box functions)
(noir-lang/noir#9316)
fix(mem2reg): Register aliases when the `IfElse` result in a reference
(noir-lang/noir#9305)
fix: Make Ssa-gen use existing reference when compiling `&mut
foo.bar.baz` (noir-lang/noir#9307)
fix: top-level item in dependency isn't always visible
(noir-lang/noir#9295)
fix(ssa-interpreter): Return error if slice length is 0 during popping
(noir-lang/noir#9308)
chore: Release Noir(1.0.0-beta.9)
(noir-lang/noir#9184)
chore(LSP): simplify code lens request handling
(noir-lang/noir#9279)
chore: add regression tests for #6383
(noir-lang/noir#9302)
fix: disallow `_` in signatures and struct members
(noir-lang/noir#9301)
fix: check associated types after validating where clause when looking
up trait impls, plus some unification fixes
(noir-lang/noir#9265)
chore: Add fmtstr to coercions list
(noir-lang/noir#9300)
chore: Add a helper function `fmtstr::as_quoted_str`
(noir-lang/noir#9293)
chore(docs): Copy Type Coercions docs into v1.0.0-beta.8 versioned docs
(noir-lang/noir#9298)
feat: only inject "out of bounds" checks in brillig
(noir-lang/noir#9200)
END_COMMIT_OVERRIDE

---------

Co-authored-by: AztecBot <tech@aztecprotocol.com>
Co-authored-by: Maxim Vezenov <mvezenov@gmail.com>
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.

SSA interpreter returns wrong result when popping the back of merged slice

2 participants