flang icon indicating copy to clipboard operation
flang copied to clipboard

Wrong results with O2 when compile do loop statement

Open ghost opened this issue 4 years ago • 1 comments

Please consider this simple test program:

PROGRAM TEST
    INTEGER :: INT, IDX

    INT = 1
     DO IDX=1,65
        INT = ISHFT(INT,1)
        IF(INT.EQ.0) GOTO 10
    ENDDO

10  IF(IDX.NE.32)       GOTO 900

    PRINT *, 'PASS'
    RETURN

900 PRINT *, 'ERROR 900'

END

LLVM and Flang version:

LLVM  10.0.1, flang_20201023

Compile this case with O2, I got the error result:

$ flang -O2 test.f90 -o test 
ERROR 900

And when compiling with O0, the result is correct.

$ flang -O0 test.f90 -o test 
PASS

ghost avatar Dec 11 '20 09:12 ghost

Using following command line to get unoptimized IR: flang -O2 -mllvm -opt-bisect-limit=0 test-sim.f90 -S -emit-llvm -o test-sim.noopt.ll

I can see that the following instruction was generated by flang2:

%3 = shl nsw i32 %2, 1    //  nsw property

ISHFT performs the logical shift function. The generated instruction should not assume the nsw property.

So flang2 should generate llvm ir like %3 = shl i32 %2, 1.

Am I right?

zhaochuanfeng avatar Dec 14 '20 06:12 zhaochuanfeng