[Flang-dev] dmax1 comparison

xoviat xoviat at gmail.com
Wed Sep 26 16:29:25 EDT 2018



From: xoviat
Sent: Wednesday, September 26, 2018 2:38 PM
To: flang-dev at lists.flang-compiler.org
Subject: dmax1 comparison


Take the following code:

        program main
          double precision resabs, abserr, infn
          
          abserr = infinity()*0
          resabs = infinity()

          infn = infinity()

          print *, "abserr :", abserr
          print *, "resabs :", resabs
          print *, "infn:", infn
                    
          abserr = dmax1(infn, abserr)
         
          print *, "abserr :", abserr
  
        contains
          double precision function infinity()
            implicit none
            double precision :: x
            x = 0
            infinity=-log(x)
          end function infinity

          
        end program main

When compiled with gfortran, the following is the result:

     abserr :                       NaN
     resabs :                  Infinity
     infn:                  Infinity
     abserr :                  Infinity
        
When compiled with flang, the following is the result:

     abserr :                       NaN
     resabs :                       Inf
     infn:                       Inf
     abserr :                       NaN

     
Flang fails to correctly compare the Inf and NaN value when the dmax1 function is called. 
The cause of this failure is flang's incorrect output of the "fcmp" LLVM instruction:

        %68 = bitcast %struct.BSS1* @.BSS1 to i8*, !dbg !26
        %69 = getelementptr i8, i8* %68, i64 8, !dbg !26
        %70 = bitcast i8* %69 to double*, !dbg !26
        %71 = load double, double* %70, align 8, !dbg !26
        %72 = bitcast %struct.BSS1* @.BSS1 to i8*, !dbg !26
        %73 = getelementptr i8, i8* %72, i64 16, !dbg !26
        %74 = bitcast i8* %73 to double*, !dbg !26
        %75 = load double, double* %74, align 8, !dbg !26
------->%76 = fcmp ogt double %75, %71, !dbg !26
        %77 = select i1 %76, double %75, double %71, !dbg !26
        %78 = bitcast %struct.BSS1* @.BSS1 to i8*, !dbg !26
        %79 = getelementptr i8, i8* %78, i64 8, !dbg !26
        %80 = bitcast i8* %79 to double*, !dbg !26
        store double %77, double* %80, align 8, !dbg !26
        
"fcmp" requires that any instruction which may compare an NaN value with another value be
classified as "unordered." The following LLVM IR yields the correct results:

        %68 = bitcast %struct.BSS1* @.BSS1 to i8*, !dbg !26
        %69 = getelementptr i8, i8* %68, i64 8, !dbg !26
        %70 = bitcast i8* %69 to double*, !dbg !26
        %71 = load double, double* %70, align 8, !dbg !26
        %72 = bitcast %struct.BSS1* @.BSS1 to i8*, !dbg !26
        %73 = getelementptr i8, i8* %72, i64 16, !dbg !26
        %74 = bitcast i8* %73 to double*, !dbg !26
        %75 = load double, double* %74, align 8, !dbg !26
------->%76 = fcmp ugt double %75, %71, !dbg !26
        %77 = select i1 %76, double %75, double %71, !dbg !26
        %78 = bitcast %struct.BSS1* @.BSS1 to i8*, !dbg !26
        %79 = getelementptr i8, i8* %78, i64 8, !dbg !26
        %80 = bitcast i8* %79 to double*, !dbg !26
        store double %77, double* %80, align 8, !dbg !26
        
When run, the result is as follows:

     abserr :                       NaN
     resabs :                       Inf
     infn:                       Inf
     abserr :                       Inf


flang2.exe appears to require the "-ieee" flag to be present in order to correctly generate the
required instructions, addition to -x 15 being set correctly. However, gfortran generates the correct
instructions without any addtional flags, and this failure was quite painful to debug. In addition, I
was unable to generate the correct instructions through flags only--source changes to flang2.exe were
needed.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.flang-compiler.org/pipermail/flang-dev_lists.flang-compiler.org/attachments/20180926/bc18d162/attachment.html>


More information about the flang-dev mailing list