https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129269
Bug ID: 129269
Summary: [15/16/17 Regression] wrong code at -O2 with
-fno-math-errno: std::log(0.5*erfc-like(x)) inlined
into a loop
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Keywords: wrong-code
Severity: normal
Priority: P3
Component: tree-optimization
Assignee: unassigned at gcc dot gnu.org
Reporter: kapelner at gmail dot com
Target Milestone: ---
Host: x86_64-linux-gnu
Target: x86_64-linux-gnu
Build: x86_64-linux-gnu
Created attachment 66383
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66383&action=edit
repro.ii: preprocessed testcase (g++ -O2 -fno-math-errno repro.ii && ./a.out)
At `-O2 -fno-math-errno` (also `-O3`) the attached program prints wrong values
and exits 1.
With `-O0`, `-O1`, or `-O2` without `-fno-math-errno` it prints the correct
values
(-4.164852955, -10.136300500, -10.594935532).
$ g++ -O2 -fno-math-errno repro.cpp && ./a.out
4.491482422 expected -4.16485296
-nan expected -10.13630051
-nan expected -10.59493555
Regression: GCC 14.1 is correct; 15.1, 15.2, 15.3, 16.1, 16.2 and trunk (17.0.0
20261202) are wrong.
These are upstream builds (Compiler Explorer), so the bug is not specific to a
distribution's patches.
Plain `-O2` is correct on every version.
The program has no undefined behaviour that I can find, and no value is ever
outside the domain
of `std::log` or `std::exp`. Following the checks on https://gcc.gnu.org/bugs/
(all with
`-O2 -fno-math-errno`, GCC 15.2):
- `-Wall -Wextra`: no warnings; output still wrong.
- `-fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations`: output
still wrong.
- `-fsanitize=address,undefined`: no run-time errors reported (the bug does not
show in that build).
`-fsanitize=address` alone: no errors, and the output is still wrong.
Valgrind is also clean.
- `-D_GLIBCXX_ASSERTIONS -D_GLIBCXX_DEBUG`: no assertion failures; output still
wrong.
- `-ffloat-store` and `-ffp-contract=off`: output still wrong. This is not a
rounding difference:
the results are a positive log-probability and NaN.
Observations:
- Correct with: `-O0`, `-O1`, `-O2` (errno on), `-fno-inline`, `-fno-tree-pre`,
`-fno-tree-forwprop`,
`-fno-thread-jumps`, `-fno-builtin`, `-fsanitize=undefined`; GCC 14.1; clang
19.1.7 and 21.1.8 at all levels.
- Wrong with upstream GCC 15.1, 15.2, 15.3, 16.1, 16.2 and trunk 20261202; also
Ubuntu 15.2.0-16ubuntu1
and an Ubuntu GCC 16 snapshot (16.0.1 20260322 r16-8246-g569ace1fa50).
- Both `log_pnorm_lower` and `log_pnorm_upper` must be called in the loop body;
removing the unused
`lp` call hides the bug, as does dropping the `fast_log_pnorm` fallback, so
it looks like
an interaction between the two inlined copies of the erfc code and the
`std::log` calls
(-fno-math-errno lets log be treated as const, so the guarded calls can be
moved/merged).
- Not yet bisected to a commit; the regression is somewhere between 14.1 and
15.1.
Real-world impact: this is a reduction of a probit-regression likelihood in the
R package EDI;
the unreduced code gave log(1-Phi(4.055)) = -9.698 instead of -10.595 and the
optimizer ended at NaN.
Compiler (`g++ -v`, upstream build that reproduces it)
Target: x86_64-linux-gnu
Configured with: ../gcc-15.2.0/configure
--prefix=/opt/compiler-explorer/gcc-build/staging
--enable-libstdcxx-backtrace=yes --build=x86_64-linux-gnu
--host=x86_64-linux-gnu --target=x86_64-linux-gnu --disable-bootstrap
--enable-multiarch --with-abi=m64 --with-multilib-list=m32,m64,mx32
--enable-multilib --enable-clocale=gnu
--enable-languages=c,c++,fortran,ada,objc,obj-c++,go,d,m2,rust,cobol
--enable-ld=yes --enable-gold=yes --enable-libstdcxx-debug
--enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto
--enable-plugins --enable-threads=posix
--with-pkgversion=Compiler-Explorer-Build-gcc--binutils-2.44
Thread model: posix
gcc version 15.2.0 (Compiler-Explorer-Build-gcc--binutils-2.44)
Trunk, also wrong: `gcc version 17.0.0 20261202 (experimental)`, configured the
same way from
`../gcc-trunk-20261202/configure` (Compiler Explorer build
531fc99cb65d53417d52e3e1861f228f7b0174a6).
Local build where it was first found:
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu
15.2.0-16ubuntu1' --with-bugurl=file:///usr/share/doc/gcc-15/README.Bugs
--enable-languages=c,ada,c++,go,d,fortran,objc,obj-c++,m2,rust,cobol,algol68
--prefix=/usr --with-gcc-major-version-only --program-suffix=-15
--program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id
--libexecdir=/usr/libexec --without-included-gettext --enable-threads=posix
--libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu
--enable-libstdcxx-debug --enable-libstdcxx-time=yes
--with-default-libstdcxx-abi=new --enable-libstdcxx-backtrace
--enable-gnu-unique-object --disable-vtable-verify --enable-plugin
--enable-default-pie --with-system-zlib --enable-libphobos-checking=release
--with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch
--disable-werror --enable-cet --with-arch-32=i686 --with-abi=m64
--with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic
--enable-offload-targets=nvptx-none=/build/gcc-15-j35TAX/gcc-15-15.2.0/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-15-j35TAX/gcc-15-15.2.0/debian/tmp-gcn/usr
--enable-offload-defaulted --without-cuda-driver --enable-checking=release
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
--with-build-config=bootstrap-lto-lean --enable-link-serialization=2
gcc version 15.2.0 (Ubuntu 15.2.0-16ubuntu1)
Command line: `g++ -O2 -fno-math-errno repro.cpp && ./a.out` (no compiler
output; the program exits 1).
Attachments:
- repro.ii (attached with this report): the preprocessed source (`g++ -E -O2
-fno-math-errno repro.cpp`). It reproduces the
bug when compiled directly with `g++ -O2 -fno-math-errno repro.ii`.
- repro.cpp (attached separately): the same program before preprocessing (70
lines; only <cmath> and <cstdio> are included).