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

Cannot bitshift without using a literal (runtime bitshift failure/regression) #2399

Closed
tsujp opened this issue Aug 22, 2023 · 2 comments · Fixed by #2739
Closed

Cannot bitshift without using a literal (runtime bitshift failure/regression) #2399

tsujp opened this issue Aug 22, 2023 · 2 comments · Fixed by #2739
Assignees
Labels
bug Something isn't working

Comments

@tsujp
Copy link
Contributor

tsujp commented Aug 22, 2023

Aim

To bitshift by values only known at runtime.

Expected Behavior

I'd expect this suite of tests to pass:

// PASSES.
fn bitshift_literal_0() -> u64 {
    let mut bits: u64 = 0;
    bits |= 1 << 0;

    bits
}

// PASSES.
fn bitshift_literal_4() -> u64 {
    let mut bits: u64 = 0;
    bits |= 1 << 4;

    bits
}

// FAILS.
fn bitshift_variable(idx: u64) -> u64 {
    let mut bits: u64 = 0;
    // ERROR:
    // [bitshift_error] Testing test_bitshift_variable... The application panicked (crashed).
    // Message:  internal error: entered unreachable code: ICE: Truncation attempted on non-integer
    // Location: crates/noirc_evaluator/src/ssa/ssa_gen/context.rs:318
    bits |= 1 << idx;

    bits
}

// PASSES.
#[test]
fn test_bitshift_literal() {
    let result_0 = bitshift_literal_0();
    assert(result_0 == 1);

    let result_4 = bitshift_literal_4();
    assert(result_4 == 16);
}

// FAILS.
#[test]
fn test_bitshift_variable() {
    let result_0 = bitshift_variable(0);
    assert(result_0 == 1);

    let result_4 = bitshift_variable(4);
    assert(result_4 == 16);
}

Bug

Function bitshift_variable fails with:

[bitshift_error] Testing test_bitshift_variable... The application panicked (crashed).
Message:  internal error: entered unreachable code: ICE: Truncation attempted on non-integer
Location: crates/noirc_evaluator/src/ssa/ssa_gen/context.rs:318

To Reproduce

  1. Use Nargo binary (versioned below).
  2. Paste in code above.
  3. Observe test failure.

Installation Method

Binary

Nargo Version

nargo 0.10.3 (git version hash: 2db759f, is dirty: false)

Additional Context

  • This same code passes on nargo 0.9.0 (git version hash: ef91286b920fb3e17c7368839a93ccad2441edc8, is dirty: false)
  • macOS aarch64.

Would you like to submit a PR for this Issue?

No

Support Needs

No response

@tsujp tsujp added the bug Something isn't working label Aug 22, 2023
@github-project-automation github-project-automation bot moved this to 📋 Backlog in Noir Aug 22, 2023
@jfecher
Copy link
Contributor

jfecher commented Aug 22, 2023

Looks possibly related to #2250. I wouldn't be surprised since both left and right shifts use the same pow method internally.

@tsujp
Copy link
Contributor Author

tsujp commented Aug 30, 2023

Looks possibly related to #2250. I wouldn't be surprised since both left and right shifts use the same pow method internally.

This may help but if all operands to the bitshift expression are variables then it works, so this is fine:

// Works
fn bitshift_variable(base: u64, idx: u64) -> u64 {
    let mut bits: u64 = 0;
    bits |= base << idx; // Same as in OP, literal `1` replaced with `base`.

    bits
}

Probably something to do with constant expression optimisation or something? I'm not too familiar with the compiler internals. I had a gander but nothing obvious to me.

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
4 participants