Skip to content

Commit 826f83f

Browse files
authored
bpo-30104: Only use -fno-strict-aliasing on dtoa.c (#1340)
On clang, only compile dtoa.c with -fno-strict-aliasing, use strict aliasing to compile all other C files.
1 parent 5a4e3d8 commit 826f83f

File tree

3 files changed

+17
-16
lines changed

3 files changed

+17
-16
lines changed

Makefile.pre.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ ARFLAGS= @ARFLAGS@
107107
CFLAGSFORSHARED=@CFLAGSFORSHARED@
108108
# C flags used for building the interpreter object files
109109
PY_CORE_CFLAGS= $(PY_CFLAGS) $(PY_CFLAGS_NODIST) $(PY_CPPFLAGS) $(CFLAGSFORSHARED) -DPy_BUILD_CORE
110+
# Strict or non-strict aliasing flags used to compile dtoa.c, see above
111+
CFLAGS_ALIASING=@CFLAGS_ALIASING@
110112

111113

112114
# Machine-dependent subdirectories
@@ -1536,6 +1538,13 @@ config.status: $(srcdir)/configure
15361538
.c.o:
15371539
$(CC) -c $(PY_CORE_CFLAGS) -o $@ $<
15381540

1541+
# bpo-30104: dtoa.c uses union to cast double to unsigned long[2]. clang 4.0
1542+
# with -O2 or higher and strict aliasing miscompiles the ratio() function
1543+
# causing rounding issues. Compile dtoa.c using -fno-strict-aliasing on clang.
1544+
# https://bugs.llvm.org//show_bug.cgi?id=31928
1545+
Python/dtoa.o: Python/dtoa.c
1546+
$(CC) -c $(PY_CORE_CFLAGS) $(CFLAGS_ALIASING) -o $@ $<
1547+
15391548
# Run reindent on the library
15401549
reindent:
15411550
./$(BUILDPYTHON) $(srcdir)/Tools/scripts/reindent.py -r $(srcdir)/Lib

configure

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,7 @@ OTHER_LIBTOOL_OPT
668668
UNIVERSAL_ARCH_FLAGS
669669
CFLAGS_NODIST
670670
BASECFLAGS
671+
CFLAGS_ALIASING
671672
OPT
672673
LLVM_PROF_FOUND
673674
target_os
@@ -6829,13 +6830,7 @@ then
68296830
then
68306831
# Clang also needs -fwrapv
68316832
WRAP="-fwrapv"
6832-
6833-
# bpo-30104: Python/dtoa.c requires to be build with
6834-
# -fno-strict-aliasing to fix compiler issue on the
6835-
# double/ULong[2] union using clang 4.0 and optimization level
6836-
# -O2 or higher
6837-
# https://bugs.llvm.org//show_bug.cgi?id=31928
6838-
ALIASING="-fno-strict-aliasing"
6833+
CFLAGS_ALIASING="-fno-strict-aliasing"
68396834
fi
68406835

68416836
case $ac_cv_prog_cc_g in
@@ -6857,7 +6852,7 @@ then
68576852
;;
68586853
esac
68596854

6860-
OPT="$OPT $STRICT_PROTO $ALIASING"
6855+
OPT="$OPT $STRICT_PROTO"
68616856

68626857
case $ac_sys_system in
68636858
SCO_SV*) OPT="$OPT -m486 -DSCO5"

configure.ac

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,7 @@ esac
14401440
# tweak OPT based on compiler and platform, only if the user didn't set
14411441
# it on the command line
14421442
AC_SUBST(OPT)
1443+
AC_SUBST(CFLAGS_ALIASING)
14431444
if test "${OPT-unset}" = "unset"
14441445
then
14451446
case $GCC in
@@ -1469,13 +1470,9 @@ then
14691470
then
14701471
# Clang also needs -fwrapv
14711472
WRAP="-fwrapv"
1472-
1473-
# bpo-30104: Python/dtoa.c requires to be build with
1474-
# -fno-strict-aliasing to fix compiler issue on the
1475-
# double/ULong[2] union using clang 4.0 and optimization level
1476-
# -O2 or higher
1477-
# https://bugs.llvm.org//show_bug.cgi?id=31928
1478-
ALIASING="-fno-strict-aliasing"
1473+
# bpo-30104: disable strict aliasing to compile correctly dtoa.c,
1474+
# see Makefile.pre.in for more information
1475+
CFLAGS_ALIASING="-fno-strict-aliasing"
14791476
fi
14801477

14811478
case $ac_cv_prog_cc_g in
@@ -1497,7 +1494,7 @@ then
14971494
;;
14981495
esac
14991496

1500-
OPT="$OPT $STRICT_PROTO $ALIASING"
1497+
OPT="$OPT $STRICT_PROTO"
15011498

15021499
case $ac_sys_system in
15031500
SCO_SV*) OPT="$OPT -m486 -DSCO5"

0 commit comments

Comments
 (0)