]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-30104: Use -fno-strict-aliasing on clang (#1376) (#1377)
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 2 May 2017 09:16:21 +0000 (11:16 +0200)
committerGitHub <noreply@github.com>
Tue, 2 May 2017 09:16:21 +0000 (11:16 +0200)
Python/dtoa.c is not compiled correctly with clang 4.0 and
optimization level -O2 or higher, because of an aliasing issue on the
double/ULong[2] union. Only compile dtoa.c with -fno-strict-aliasing.

LLVM bug report:
https://bugs.llvm.org//show_bug.cgi?id=31928
(cherry picked from commit 809101f14f27ddb394cd77c477470761ecf99f41)

Makefile.pre.in
configure
configure.ac

index e1436c3bfdca913c625ec2003e6ce48a8aeab2ee..f0450effad0f5add64acaa24e4563f4ef07b822a 100644 (file)
@@ -103,6 +103,8 @@ ARFLAGS=    @ARFLAGS@
 CFLAGSFORSHARED=@CFLAGSFORSHARED@
 # C flags used for building the interpreter object files
 PY_CORE_CFLAGS=        $(PY_CFLAGS) $(PY_CFLAGS_NODIST) $(PY_CPPFLAGS) $(CFLAGSFORSHARED) -DPy_BUILD_CORE
+# Strict or non-strict aliasing flags used to compile dtoa.c, see above
+CFLAGS_ALIASING=@CFLAGS_ALIASING@
 
 
 # Machine-dependent subdirectories
@@ -1532,6 +1534,13 @@ config.status:   $(srcdir)/configure
 .c.o:
        $(CC) -c $(PY_CORE_CFLAGS) -o $@ $<
 
+# bpo-30104: dtoa.c uses union to cast double to unsigned long[2]. clang 4.0
+# with -O2 or higher and strict aliasing miscompiles the ratio() function
+# causing rounding issues. Compile dtoa.c using -fno-strict-aliasing on clang.
+# https://bugs.llvm.org//show_bug.cgi?id=31928
+Python/dtoa.o: Python/dtoa.c
+       $(CC) -c $(PY_CORE_CFLAGS) $(CFLAGS_ALIASING) -o $@ $<
+
 # Run reindent on the library
 reindent:
        ./$(BUILDPYTHON) $(srcdir)/Tools/scripts/reindent.py -r $(srcdir)/Lib
index c84a8ec9e04a1a5d64ed2ef4046c7be9271d9187..f5fe5f459c394f85d1f24615825ed7747761c338 100755 (executable)
--- a/configure
+++ b/configure
@@ -666,6 +666,7 @@ OTHER_LIBTOOL_OPT
 UNIVERSAL_ARCH_FLAGS
 CFLAGS_NODIST
 BASECFLAGS
+CFLAGS_ALIASING
 OPT
 LLVM_PROF_FOUND
 target_os
@@ -6859,6 +6860,7 @@ esac
 # tweak OPT based on compiler and platform, only if the user didn't set
 # it on the command line
 
+
 if test "${OPT-unset}" = "unset"
 then
     case $GCC in
@@ -6871,30 +6873,49 @@ then
            WRAP="-fwrapv"
         fi
 
-        # Clang also needs -fwrapv
         case $CC in
-            *clang*) WRAP="-fwrapv"
-            ;;
+            *clang*)
+                cc_is_clang=1
+                ;;
+            *)
+                if $CC --version 2>&1 | grep -q clang
+                then
+                    cc_is_clang=1
+                else
+                    cc_is_clang=
+                fi
         esac
 
+        if test -n "${cc_is_clang}"
+        then
+            # Clang also needs -fwrapv
+            WRAP="-fwrapv"
+            # bpo-30104: disable strict aliasing to compile correctly dtoa.c,
+            # see Makefile.pre.in for more information
+            CFLAGS_ALIASING="-fno-strict-aliasing"
+        fi
+
        case $ac_cv_prog_cc_g in
        yes)
            if test "$Py_DEBUG" = 'true' ; then
                # Optimization messes up debuggers, so turn it off for
                # debug builds.
                 if "$CC" -v --help 2>/dev/null |grep -- -Og > /dev/null; then
-                    OPT="-g -Og -Wall $STRICT_PROTO"
+                    OPT="-g -Og -Wall"
                 else
-                    OPT="-g -O0 -Wall $STRICT_PROTO"
+                    OPT="-g -O0 -Wall"
                 fi
            else
-               OPT="-g $WRAP -O3 -Wall $STRICT_PROTO"
+               OPT="-g $WRAP -O3 -Wall"
            fi
            ;;
        *)
-           OPT="-O3 -Wall $STRICT_PROTO"
+           OPT="-O3 -Wall"
            ;;
        esac
+
+       OPT="$OPT $STRICT_PROTO"
+
        case $ac_sys_system in
            SCO_SV*) OPT="$OPT -m486 -DSCO5"
            ;;
index 779fe3764c7f5ab76286fa64605b6fcc9a9fce0f..21284d01684587e38ee75ec62f0f9ab6a3b544dd 100644 (file)
@@ -1403,6 +1403,7 @@ esac
 # tweak OPT based on compiler and platform, only if the user didn't set
 # it on the command line
 AC_SUBST(OPT)
+AC_SUBST(CFLAGS_ALIASING)
 if test "${OPT-unset}" = "unset"
 then
     case $GCC in
@@ -1415,30 +1416,49 @@ then
            WRAP="-fwrapv"
         fi
 
-        # Clang also needs -fwrapv
         case $CC in
-            *clang*) WRAP="-fwrapv"
-            ;;
+            *clang*)
+                cc_is_clang=1
+                ;;
+            *)
+                if $CC --version 2>&1 | grep -q clang
+                then
+                    cc_is_clang=1
+                else
+                    cc_is_clang=
+                fi
         esac
 
+        if test -n "${cc_is_clang}"
+        then
+            # Clang also needs -fwrapv
+            WRAP="-fwrapv"
+            # bpo-30104: disable strict aliasing to compile correctly dtoa.c,
+            # see Makefile.pre.in for more information
+            CFLAGS_ALIASING="-fno-strict-aliasing"
+        fi
+
        case $ac_cv_prog_cc_g in
        yes)
            if test "$Py_DEBUG" = 'true' ; then
                # Optimization messes up debuggers, so turn it off for
                # debug builds.
                 if "$CC" -v --help 2>/dev/null |grep -- -Og > /dev/null; then
-                    OPT="-g -Og -Wall $STRICT_PROTO"
+                    OPT="-g -Og -Wall"
                 else
-                    OPT="-g -O0 -Wall $STRICT_PROTO"
+                    OPT="-g -O0 -Wall"
                 fi
            else
-               OPT="-g $WRAP -O3 -Wall $STRICT_PROTO"
+               OPT="-g $WRAP -O3 -Wall"
            fi
            ;;
        *)
-           OPT="-O3 -Wall $STRICT_PROTO"
+           OPT="-O3 -Wall"
            ;;
        esac
+
+       OPT="$OPT $STRICT_PROTO"
+
        case $ac_sys_system in
            SCO_SV*) OPT="$OPT -m486 -DSCO5"
            ;;