Bug 1610053

Summary: SystemZ combines away AND masks that are necessary for i128 shifts
Product: DevTools Reporter: Josh Stone <jistone>
Component: llvmAssignee: Tom Stellard <tstellar>
Status: CLOSED ERRATA QA Contact: Miloš Prchlík <mprchlik>
Severity: unspecified Docs Contact:
Priority: unspecified    
Version: llvm-toolset-7CC: emachado, jistone, law, mcermak, tborcin
Target Milestone: rc   
Target Release: 2018.4   
Hardware: s390x   
OS: Unspecified   
Whiteboard:
Fixed In Version: llvm-toolset-6.0-llvm-6.0.1-3.el7 Doc Type: No Doc Update
Doc Text:
undefined
Story Points: ---
Clone Of: Environment:
Last Closed: 2018-11-13 15:19:35 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1562186, 1562192, 1591301, 1611608    

Description Josh Stone 2018-07-30 23:31:15 UTC
I reduced Rust bug 1562192 to this:

    #![feature(test)]

    extern crate test;

    #[inline(never)]
    fn foo(f: f32) -> i128 {
        let sign_bit = 1u32 << 31;
        let significand_bits = 23;
        let exponent_bias = 127;

        let a_abs = f.to_bits() & !sign_bit;
        let exponent = (a_abs >> significand_bits) - exponent_bias;
        1i128 << (exponent - significand_bits)
    }

    fn main() {
        let f = test::black_box(0x8000_00 as f32);
        println!("{:x}", foo(f));
    }

Compile it optimized with: env RUSTC_BOOTSTRAP=1 rustc -O [file.rs]

On s390x, this produces the following IR:

    ; i128::foo
    ; Function Attrs: noinline norecurse nounwind readnone uwtable
    define internal fastcc i128 @_ZN4i1283foo17h5b7dd13bfb54356eE(float %f) unnamed_addr #2 {
    start:
      %0 = bitcast float %f to i32
      %1 = lshr i32 %0, 23
      %2 = add nuw nsw i32 %1, 106
      %3 = and i32 %2, 127
      %4 = zext i32 %3 to i128
      %5 = shl i128 1, %4
      ret i128 %5
    }

Note the "add 106" instead of subtracting exponent_bias and significand_bits, but the "and 127" is supposed to mask that back into range.

SystemZTargetLowering::combineSHIFTROT deems that AND useless and removes it.  That might be ok for native instructions, but "shl i128" calls __ashlti3, which does require the value to be in range.

I have a patch review here: <https://reviews.llvm.org/D50018>, but the first comment says SHL poison makes that transform was wrong even for smaller integers.

Comment 1 Tom Stellard 2018-08-07 04:44:24 UTC
Fixed in llvm-toolset-6.0-llvm-6.0.1-3.el7.

Comment 3 Miloš Prchlík 2018-10-01 12:49:53 UTC
Verified with build llvm-toolset-6.0-llvm-6.0.1-5.el7.

Comment 5 errata-xmlrpc 2018-11-13 15:19:35 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2018:3583