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

Dead Instruction Elimination doesn't consider &mut parameters #7104

Closed
aakoshh opened this issue Jan 17, 2025 · 0 comments · Fixed by #7106
Closed

Dead Instruction Elimination doesn't consider &mut parameters #7104

aakoshh opened this issue Jan 17, 2025 · 0 comments · Fixed by #7106
Labels
bug Something isn't working

Comments

@aakoshh
Copy link
Contributor

aakoshh commented Jan 17, 2025

Aim

Discovered in #7072

During preprocessing functions we wanted to call the DIE pass on them to remove leftover instructions after doing mem2reg, but in one example described in the PR description it eliminated the entire function body, causing execution to fail.

Expected Behavior

Adding the DIE pass to the preprocessing of functions should not cause integration test failures.

Maxim said:

Looks like we need to pass some extra context to the can_eliminate_if_unused method on Instruction noir/compiler/noirc_evaluator/src/ssa/ir/instruction.rs

Bug

Here's the SSA before and after the preprocessing pass, when the DIE was called on every function:

Before:

After Removing Paired rc_inc & rc_decs:
acir(inline) fn main f0 {
  b0(v0: Field, v1: Field):
    v2 = allocate -> &mut Field
    store v0 at v2
    call f1(v2)
    v4 = load v2 -> Field
    v5 = eq v4, v1
    constrain v4 == v1
    return
}
acir(inline) fn Add10 f1 {
  b0(v0: &mut Field):
    v1 = load v0 -> Field
    v2 = load v0 -> Field
    v4 = add v2, Field 10
    store v4 at v0
    return
}

After:

After Preprocessing Functions:
acir(inline) fn main f0 {
  b0(v0: Field, v1: Field):
    v2 = allocate -> &mut Field
    store v0 at v2
    call f1(v2)
    v4 = load v2 -> Field
    v5 = eq v4, v1
    constrain v4 == v1
    return
}
acir(inline) fn Add10 f1 {
  b0(v0: &mut Field):
    return                              # Whole function body got eliminated!
}

It saw store v4 at v0 as a dead instruction, not realising that v0 can be used by the caller.

To Reproduce

$ cde test_programs/execution_success/traits_in_crates_1
$ cargo run -q -p nargo_cli -- --program-dir . execute --force --silence-warnings
error: Failed constraint
  ┌─ /Users/aakoshh/Work/aztec/noir/test_programs/execution_success/traits_in_crates_1/src/main.nr:4:12

4 │     assert(V.Q == y);
  │            --------

  = Call stack:
    1. /Users/aakoshh/Work/aztec/noir/test_programs/execution_success/traits_in_crates_1/src/main.nr:4:12

Failed to solve program: 'Cannot satisfy constraint'

Workaround

None

Workaround Description

The DIE pass has been commented out from preprocess_fns.

Additional Context

No response

Project Impact

None

Blocker Context

No response

Nargo Version

nargo version = 1.0.0-beta.1 noirc version = 1.0.0-beta.1+4e3ea96602c9d9e70c96e19c56ee7c1290d8f12f (git version hash: 4e3ea96, is dirty: false)

NoirJS Version

No response

Proving Backend Tooling & Version

No response

Would you like to submit a PR for this Issue?

None

Support Needs

No response

@aakoshh aakoshh added the bug Something isn't working label Jan 17, 2025
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Jan 17, 2025
@aakoshh aakoshh changed the title Dead Instruction Elimination doesn't consider mutable reference input parameters Dead Instruction Elimination doesn't consider &mut parameters Jan 17, 2025
@github-project-automation github-project-automation bot moved this from 📋 Backlog to ✅ Done in Noir Jan 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant