Skip to content

incorrect code generation for i686 release build for 1.47 beta and nightly #76042

Closed

Description

@tspiteri
/// ```rust
/// assert_eq!(fixed::i124f4_wrapping_mul(3 << 4, 2 << 4), 6 << 4);
/// ```
#[inline]
pub fn i124f4_wrapping_mul(lhs: i128, rhs: i128) -> i128 {
    let (ans, _) = mul_overflow(lhs, rhs, 4);
    ans
}

#[inline]
fn mul_overflow(lhs: i128, rhs: i128, frac_nbits: u32) -> (i128, bool) {
    let (lh, ll) = hi_lo(lhs);
    let (rh, rl) = hi_lo(rhs);
    let ll_rl = ll.wrapping_mul(rl);
    let lh_rl = lh.wrapping_mul(rl);
    let ll_rh = ll.wrapping_mul(rh);
    let lh_rh = lh.wrapping_mul(rh);

    let col01 = ll_rl as u128;
    let (col01_hi, col01_lo) = (col01 >> 64, col01 & !(!0 << 64));
    let partial_col12 = lh_rl + col01_hi as i128;
    let (col12, carry_col3) = carrying_add(partial_col12, ll_rh);
    let (col12_hi, col12_lo) = hi_lo(col12);
    let ans01 = shift_lo_up_unsigned(col12_lo) + col01_lo;
    let ans23 = lh_rh + col12_hi + shift_lo_up(carry_col3);
    let ret = combine_lo_then_shl(ans23, ans01, frac_nbits);
    println!(
        "combine_lo_then_shl({}, {}, {}) -> ({}, {})",
        ans23, ans01, frac_nbits, ret.0, ret.1
    );
    ret
}

#[inline]
fn hi_lo(this: i128) -> (i128, i128) {
    (this >> 64, this & !(!0 << 64))
}

#[inline]
fn combine_lo_then_shl(this: i128, lo: u128, shift: u32) -> (i128, bool) {
    if shift == 128 {
        (this, false)
    } else if shift == 0 {
        let ans = lo as i128;
        (ans, this != if ans < 0 { -1 } else { 0 })
    } else {
        let lo = (lo >> shift) as i128;
        let hi = this << (128 - shift);
        let ans = lo | hi;
        (ans, this >> shift != if ans < 0 { -1 } else { 0 })
    }
}

#[inline]
fn carrying_add(this: i128, rhs: i128) -> (i128, i128) {
    let (sum, overflow) = this.overflowing_add(rhs);
    let carry = if overflow {
        if sum < 0 {
            1
        } else {
            -1
        }
    } else {
        0
    };
    (sum, carry)
}

#[inline]
fn shift_lo_up(this: i128) -> i128 {
    debug_assert!(this >> 64 == 0);
    this << 64
}

#[inline]
fn shift_lo_up_unsigned(this: i128) -> u128 {
    debug_assert!(this >> 64 == 0);
    (this << 64) as u128
}

The test passes for 1.46.0 i686 stable release builds, for i686 beta/nightly debug builds, and for x86_64 beta/nightly release builds, but fails for i686 beta/nightly release builds.

CI logs from this code on GitLab:

https://gitlab.com/tspiteri/fixed/-/pipelines/183281142

Activity

  1. added
    C-bug
    on Aug 28, 2020
  2. added
    I-prioritize
    regression-from-stable-to-beta
    T-compiler
    on Aug 28, 2020
  3. tspiteri commented on Aug 28, 2020

    @tspiteri
    ContributorAuthor

    The println! prints combine_lo_then_shl(0, 1536, 4) -> (1536, true), but combine_lo_then_shl(0, 1536, 4) should return (96, false).

  4. added
    E-needs-bisection
    on Aug 28, 2020
  5. LeSeulArtichaut commented on Aug 28, 2020

    @LeSeulArtichaut
    Contributor

    Let's try to find the culprit for this regression. Trying to reduce the example would be nice too.
    @rustbot ping cleanup

  6. rustbot commented on Aug 28, 2020

    @rustbot
    Collaborator

    Hey Cleanup Crew ICE-breakers! This bug has been identified as a good
    "Cleanup ICE-breaking candidate". In case it's useful, here are some
    instructions for tackling these sorts of bugs. Maybe take a look?
    Thanks! <3

    cc @AminArria @camelid @chrissimpkins @contrun @DutchGhost @elshize @ethanboxx @h-michael @HallerPatrick @hdhoang @hellow554 @imtsuki @kanru @KarlK90 @LeSeulArtichaut @MAdrianMattocks @matheus-consoli @mental32 @nmccarty @Noah-Kennedy @pard68 @PeytonT @pierreN @Redblueflame @RobbieClarken @RobertoSnap @robjtede @SarthakSingh31 @senden9 @shekohex @sinato @spastorino @turboladen @woshilapin @yerke

  7. added
    I-unsound
    on Aug 28, 2020
  8. added
    P-critical
    and removed
    I-prioritize
    on Aug 29, 2020
  9. Dylan-DPC-zz commented on Aug 29, 2020

    @Dylan-DPC-zz

    Marking this as P-critical based on the wg-prioritization discussion

  10. tspiteri commented on Aug 29, 2020

    @tspiteri
    ContributorAuthor

    I have a reduced test case.

    /// ```rust
    /// assert_eq!(rust76042::foo(), 8);
    /// ```
    #[inline]
    pub fn foo() -> i128 {
        bar().0
    }
    
    #[inline]
    fn bar() -> (i128, bool) {
        let a = 0;
        let b = 128;
        let shift = 4;
        let ret = baz(a, b, shift);
        // should print "baz(0, 128, 4) -> (8, false)"
        println!("baz({}, {}, {}) -> ({}, {})", a, b, shift, ret.0, ret.1);
        ret
    }
    
    #[inline]
    fn baz(a: i128, b: i128, shift: u32) -> (i128, bool) {
        if shift == 128 {
            (a, false)
        } else {
            (b >> shift, a >> shift != 0)
        }
    }

    Note that if I move the doc test to a lib test, I cannot trigger this bug. On i686 beta/nightly release test, the doc test fails with:

    ---- src/lib.rs - foo (line 1) stdout ----
    Test executable failed (exit code 101).
    stdout:
    baz(0, 128, 4) -> (128, true)
    stderr:
    thread 'main' panicked at 'assertion failed: `(left == right)`
      left: `128`,
     right: `8`', src/lib.rs:4:1
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
    

    baz is somehow returning (128, true) instead of (8, false).

    https://gitlab.com/tspiteri/rust76042/-/pipelines/183379486

  11. tmiasko commented on Aug 29, 2020

    @tmiasko
    Contributor

    Minimized:

    fn foo(a: i128, b: i128, s: u32) -> (i128, i128) {
        if s == 128 {
            (0, 0)
        } else {
            (b >> s, a >> s)
        }
    }
    fn main() {
        let r = foo(0, 8, 1);
        if r.0 != 4 {
            panic!();
        }
    }
    rustc --crate-type=bin  a.rs --target i686-unknown-linux-gnu -Coverflow-checks=off -Ccodegen-units=1 -Copt-level=1
    ./a
    rustc --crate-type=bin  a.rs --target i686-unknown-linux-gnu -Coverflow-checks=off -Ccodegen-units=1 -Copt-level=0
    ./a
    thread 'main' panicked at 'explicit panic', a.rs:11:9
    note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
  12. 5 remaining items

  13. tspiteri commented on Sep 24, 2020

    @tspiteri
    ContributorAuthor

    I cannot reproduce the failure with rustc 1.48.0-nightly (8b40853 2020-09-23) which includes #77063.

  14. LeSeulArtichaut commented on Sep 24, 2020

    @LeSeulArtichaut
    Contributor

    Letting this issue open to track a beta backport of #77063.
    Also, it might be good to have add a test in the suite for this issue. @tspiteri Would you mind filing a PR with your reproduction? Otherwise I can do it myself.

  15. tspiteri commented on Sep 24, 2020

    @tspiteri
    ContributorAuthor

    @LeSeulArtichaut I don't know how to write this kind of test PR, please go ahead.

  16. tspiteri commented on Sep 30, 2020

    @tspiteri
    ContributorAuthor

    Now the beta is working fine as well for me.

  17. added
    P-medium
    and removed
    E-needs-bisection
    P-critical
    on Sep 30, 2020
  18. added a commit that references this issue on Nov 13, 2020
  19. added a commit that references this issue on Nov 13, 2020
  20. added
    O-x86_32
    and removed on Oct 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

A-LLVMC-bugI-unsoundO-x86_32P-mediumT-compilerregression-from-stable-to-beta

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions