public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto
@ 2020-10-16 10:25 pqfrml at gmail dot com
  2020-10-16 10:29 ` [Bug c++/97456] " pqfrml at gmail dot com
                   ` (11 more replies)
  0 siblings, 12 replies; 13+ messages in thread
From: pqfrml at gmail dot com @ 2020-10-16 10:25 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

            Bug ID: 97456
           Summary: An incorrect optimization causes a function to always
                    return the same value when using -flto
           Product: gcc
           Version: 10.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: pqfrml at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/f65cTb < Minimal example

To the best of my knowledge this is incorrect behavior and it has not yet been
reported.

This is my minimal example:

A floating point reference is passed twice to a function,
the function constructs one std::complex(called a) from the reference,
it constructs another zeroed std::complex(called b),
it assigns b + a to b,
it has a branch, where if b is equal to zero, the function returns 1,
if not it continues and prints b.imag() before returning 0.

For this to work the compiler cannot know of input value in advance which is
why I have used a random number in the example. It can also be user input or
something else opaque at compile time.
The function must be called at least twice for the behavior to appear.
-flto must be on and -O2 or -O3

-fsanitize=undefined causes the bug to disappear, -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations doesn't make a difference and -Wall and
-Wextra gives no warnings.

The behavior appeared in 10.1 and is present in gcc trunk on godolt.
It is consistent on godbolt, my stationary PC and my laptop.
The files I will give you are from my stationary.

The minimum compilation command is:
g++ -O2 -flto -std=c++17 bug.cpp

and the files I will give you used:
g++ -v -save-temps -O2 -flto -std=c++17 bug.cpp -Wall -Wextra \
-fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations

The unexpected behaviour is that it always returns the 1 from the branch, even
when it is impossible for it to evaluate to true, as it should be in my
example.
b needs to be used somehow after the branch, and the easiest way I have found
is console output.

My stationary computer is using Manjaro Linux and I am using the default gcc
from the package manager.

Compiler output:
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id
--enable-lto --enable-multilib --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror
gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O2' '-flto' '-std=c++17' '-Wall'
'-Wextra' '-fno-strict-aliasing' '-fwrapv' '-fno-aggressive-loop-optimizations'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/cc1plus -E -quiet -v -D_GNU_SOURCE
bug.cpp -mtune=generic -march=x86-64 -std=c++17 -Wall -Wextra -flto
-fno-strict-aliasing -fwrapv -fno-aggressive-loop-optimizations -O2
-fpch-preprocess -o bug.ii
ignoring nonexistent directory
"/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../x86_64-pc-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0

/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/x86_64-pc-linux-gnu

/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../include/c++/10.2.0/backward
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include
 /usr/local/include
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O2' '-flto' '-std=c++17' '-Wall'
'-Wextra' '-fno-strict-aliasing' '-fwrapv' '-fno-aggressive-loop-optimizations'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/cc1plus -fpreprocessed bug.ii -quiet
-dumpbase bug.cpp -mtune=generic -march=x86-64 -auxbase bug -O2 -Wall -Wextra
-std=c++17 -version -flto -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -o bug.s
GNU C++17 (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.2.0, MPFR version
4.1.0, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU C++17 (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.2.0, MPFR version
4.1.0, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
Compiler executable checksum: 2affcd71b0797e2421aae734ab600c81
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O2' '-flto' '-std=c++17' '-Wall'
'-Wextra' '-fno-strict-aliasing' '-fwrapv' '-fno-aggressive-loop-optimizations'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 as -v --64 -o bug.o bug.s
GNU assembler version 2.35 (x86_64-pc-linux-gnu) using BFD version (GNU
Binutils) 2.35
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O2' '-flto' '-std=c++17' '-Wall'
'-Wextra' '-fno-strict-aliasing' '-fwrapv' '-fno-aggressive-loop-optimizations'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/collect2 -plugin
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/liblto_plugin.so
-plugin-opt=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper
-plugin-opt=-fresolution=bug.res -plugin-opt=-pass-through=-lgcc_s
-plugin-opt=-pass-through=-lgcc -plugin-opt=-pass-through=-lc
-plugin-opt=-pass-through=-lgcc_s -plugin-opt=-pass-through=-lgcc -flto
--build-id --eh-frame-hdr --hash-style=gnu -m elf_x86_64 -dynamic-linker
/lib64/ld-linux-x86-64.so.2 -pie
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/Scrt1.o
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/crti.o
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/crtbeginS.o
-L/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0
-L/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib -L/lib/../lib
-L/usr/lib/../lib -L/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../.. bug.o
-lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/crtendS.o
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/crtn.o
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper -fresolution=bug.res
-flinker-output=pie bug.o 
/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto-wrapper -fresolution=bug.res
-flinker-output=pie bug.o 
g++ @/tmp/ccZAfZSN
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id
--enable-lto --enable-multilib --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror
gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (GCC) 
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fPIC' '-O2' '-v'
'-save-temps' '-O2' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-fltrans-output-list=/tmp/ccFLP8uN.ltrans.out' '-fwpa'
'-fresolution=bug.res' '-flinker-output=pie' '-shared-libgcc'
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto1 -quiet -dumpbase bug.o
-mtune=generic -march=x86-64 -auxbase bug -O2 -O2 -Wextra -version -fno-openmp
-fno-openacc -fPIC -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations
-fltrans-output-list=/tmp/ccFLP8uN.ltrans.out -fwpa -fresolution=bug.res
-flinker-output=pie @/tmp/ccq1CCRU
GNU GIMPLE (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.2.0, MPFR version
4.1.0, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU GIMPLE (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.2.0, MPFR version
4.1.0, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/../lib/:/lib/../lib/../lib/:/usr/lib/../lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../:/lib/:/usr/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fPIC' '-O2' '-v'
'-save-temps' '-O2' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-fltrans-output-list=/tmp/ccFLP8uN.ltrans.out' '-fwpa'
'-fresolution=bug.res' '-flinker-output=pie' '-shared-libgcc'
[Leaving LTRANS /tmp/ccFLP8uN.ltrans.out]
g++ @/tmp/ccRp94jQ
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-pc-linux-gnu
Configured with: /build/gcc/src/gcc/configure --prefix=/usr --libdir=/usr/lib
--libexecdir=/usr/lib --mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://bugs.archlinux.org/
--enable-languages=c,c++,ada,fortran,go,lto,objc,obj-c++,d --with-isl
--with-linker-hash-style=gnu --with-system-zlib --enable-__cxa_atexit
--enable-cet=auto --enable-checking=release --enable-clocale=gnu
--enable-default-pie --enable-default-ssp --enable-gnu-indirect-function
--enable-gnu-unique-object --enable-install-libiberty --enable-linker-build-id
--enable-lto --enable-multilib --enable-plugin --enable-shared
--enable-threads=posix --disable-libssp --disable-libstdcxx-pch
--disable-libunwind-exceptions --disable-werror
gdc_include_dir=/usr/include/dlang/gdc
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 10.2.0 (GCC) 
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fPIC' '-O2' '-v'
'-save-temps' '-O2' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-fltrans-output-list=/tmp/ccFLP8uN.ltrans.out' '-fltrans' '-o'
'/tmp/ccFLP8uN.ltrans0.ltrans.o' '-shared-libgcc'
 /usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/lto1 -quiet -dumpbase
ccFLP8uN.ltrans0.o -mtune=generic -march=x86-64 -auxbase-strip
/tmp/ccFLP8uN.ltrans0.ltrans.o -O2 -O2 -Wextra -version -fno-openmp
-fno-openacc -fPIC -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations
-fltrans-output-list=/tmp/ccFLP8uN.ltrans.out -fltrans @/tmp/ccePO4q3 -o
ccFLP8uN.ltrans0.s
GNU GIMPLE (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.2.0, MPFR version
4.1.0, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
GNU GIMPLE (GCC) version 10.2.0 (x86_64-pc-linux-gnu)
        compiled by GNU C version 10.2.0, GMP version 6.2.0, MPFR version
4.1.0, MPC version 1.1.0, isl version isl-0.21-GMP

GGC heuristics: --param ggc-min-expand=100 --param ggc-min-heapsize=131072
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fPIC' '-O2' '-v'
'-save-temps' '-O2' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-fltrans-output-list=/tmp/ccFLP8uN.ltrans.out' '-fltrans' '-o'
'/tmp/ccFLP8uN.ltrans0.ltrans.o' '-shared-libgcc'
 as -v --64 -o /tmp/ccFLP8uN.ltrans0.ltrans.o ccFLP8uN.ltrans0.s
GNU assembler version 2.35 (x86_64-pc-linux-gnu) using BFD version (GNU
Binutils) 2.35
COMPILER_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/
LIBRARY_PATH=/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/../lib/:/lib/../lib/../lib/:/usr/lib/../lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../../lib/:/lib/../lib/:/usr/lib/../lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../:/lib/:/usr/lib/:/usr/lib/gcc/x86_64-pc-linux-gnu/10.2.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-c' '-fno-openmp' '-fno-openacc' '-fPIC' '-O2' '-v'
'-save-temps' '-O2' '-Wextra' '-fno-strict-aliasing' '-fwrapv'
'-fno-aggressive-loop-optimizations' '-shared-libgcc' '-mtune=generic'
'-march=x86-64' '-fltrans-output-list=/tmp/ccFLP8uN.ltrans.out' '-fltrans' '-o'
'/tmp/ccFLP8uN.ltrans0.ltrans.o' '-shared-libgcc'
[Leaving LTRANS /tmp/ccFLP8uN.ltrans0.o]
[Leaving a.out.lto_wrapper_args]
[Leaving /tmp/ccFLP8uN.ltrans0.ltrans.o]
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-O2' '-flto' '-std=c++17' '-Wall'
'-Wextra' '-fno-strict-aliasing' '-fwrapv' '-fno-aggressive-loop-optimizations'
'-shared-libgcc' '-mtune=generic' '-march=x86-64'

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug c++/97456] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
@ 2020-10-16 10:29 ` pqfrml at gmail dot com
  2020-10-16 11:00 ` [Bug ipa/97456] [10/11 Regression] " rguenth at gcc dot gnu.org
                   ` (10 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: pqfrml at gmail dot com @ 2020-10-16 10:29 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

--- Comment #1 from Anonym Spaghetti <pqfrml at gmail dot com> ---
Created attachment 49384
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=49384&action=edit
Preprocessed file

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug ipa/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
  2020-10-16 10:29 ` [Bug c++/97456] " pqfrml at gmail dot com
@ 2020-10-16 11:00 ` rguenth at gcc dot gnu.org
  2020-10-16 12:06 ` marxin at gcc dot gnu.org
                   ` (9 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: rguenth at gcc dot gnu.org @ 2020-10-16 11:00 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

Richard Biener <rguenth at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |wrong-code
     Ever confirmed|0                           |1
            Summary|An incorrect optimization   |[10/11 Regression] An
                   |causes a function to always |incorrect optimization
                   |return the same value when  |causes a function to always
                   |using -flto                 |return the same value when
                   |                            |using -flto
   Last reconfirmed|                            |2020-10-16
   Target Milestone|---                         |10.3
          Component|c++                         |ipa
             Status|UNCONFIRMED                 |NEW
                 CC|                            |jamborm at gcc dot gnu.org,
                   |                            |marxin at gcc dot gnu.org
           Priority|P3                          |P2

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
Confirmed with -fwhole-program -O3  IPA SRA messes things up here cloning wrong
and producing the strange

wrong.isra (floatD.41 ISRA.85D.75004)
{
  complex floatD.45 c$_M_valueD.75007;
  floatD.41 & posD.75005;
  floatD.41 & posD.75006;
  floatD.41 _1;
  complex floatD.45 _2;
  complex floatD.45 _3;
  floatD.41 _4;
  floatD.41 _5;
  floatD.41 _6;
  doubleD.42 _7;
  floatD.41 _8;

  <bb 6> [local count: 1073741824]:

  <bb 2> [local count: 1073741824]:
  _1 = ISRA.85_10(D);

eventually mixing up param/replacement here?

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug ipa/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
  2020-10-16 10:29 ` [Bug c++/97456] " pqfrml at gmail dot com
  2020-10-16 11:00 ` [Bug ipa/97456] [10/11 Regression] " rguenth at gcc dot gnu.org
@ 2020-10-16 12:06 ` marxin at gcc dot gnu.org
  2020-10-16 14:51 ` jamborm at gcc dot gnu.org
                   ` (8 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: marxin at gcc dot gnu.org @ 2020-10-16 12:06 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

--- Comment #3 from Martin Liška <marxin at gcc dot gnu.org> ---
A bit reduced test-case:

$ #include <complex>

auto wrong(float &pos) -> int {
  auto a = std::complex(pos, pos);
  auto b = std::complex(0.0f, 0.0f);

  b = b + a;

  if (b == 0.0f)
    return 1;

  printf("%f\n", b.imag());
  return 0;
}

float val2 = 1.710780f;
float val3;

int main(int argc, char **argv) {
  auto val = argc == 1 ? val2 : val3;

  printf("val is %f\n", val);

  return (wrong(val), wrong(val));
}

It's not easy to bisect as it depends on definitions in complex.h.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug ipa/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (2 preceding siblings ...)
  2020-10-16 12:06 ` marxin at gcc dot gnu.org
@ 2020-10-16 14:51 ` jamborm at gcc dot gnu.org
  2020-10-16 15:56 ` jamborm at gcc dot gnu.org
                   ` (7 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jamborm at gcc dot gnu.org @ 2020-10-16 14:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

--- Comment #4 from Martin Jambor <jamborm at gcc dot gnu.org> ---
Looking at Martin's reduced testcase....

(In reply to Richard Biener from comment #2)
> Confirmed with -fwhole-program -O3  IPA SRA messes things up here cloning
> wrong
> and producing the strange
> 
> wrong.isra (floatD.41 ISRA.85D.75004)
> {
[...]
> 
>   <bb 6> [local count: 1073741824]:
> 
>   <bb 2> [local count: 1073741824]:
>   _1 = ISRA.85_10(D);
> 
> eventually mixing up param/replacement here?

...this is simple conversion of a parameter passed by reference to one
passed by value.  I'm good at overlooking things but at least at the
moment I cannot see anything wrong with it.

Rather, I believe it is cplxlower1 which turns

  <bb 2> [local count: 1073741824]:
  a$_M_value_21 = COMPLEX_EXPR <ISRA.18_10(D), ISRA.18_10(D)>;

into:

  <bb 2> [local count: 1073741824]:
  a$_M_value_21 = COMPLEX_EXPR <a$_M_value$real_10(D), a$_M_value$real_10(D)>;

i.e. it replaces two uses of the parameter default-def with two
uninitialized value variable default-defs.

When I disable IPA-SRA, the pass starts with:

  <bb 2> [local count: 1073741824]:
  _1 = *pos_6(D);
  a$_M_value_21 = COMPLEX_EXPR <_1, _1>;

and changes it into:

  <bb 2> [local count: 1073741824]:
  a$_M_value$real_1 = *pos_6(D);
  a$_M_value_21 = COMPLEX_EXPR <a$_M_value$real_1, a$_M_value$real_1>;

...so my hypothesis is that cplxlower does not handle good
default-defs properly.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug ipa/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (3 preceding siblings ...)
  2020-10-16 14:51 ` jamborm at gcc dot gnu.org
@ 2020-10-16 15:56 ` jamborm at gcc dot gnu.org
  2020-10-16 18:53 ` jamborm at gcc dot gnu.org
                   ` (6 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jamborm at gcc dot gnu.org @ 2020-10-16 15:56 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

--- Comment #5 from Martin Jambor <jamborm at gcc dot gnu.org> ---
And indeed the following avoids the issue:

diff --git a/gcc/tree-complex.c b/gcc/tree-complex.c
index 2e54bbb917c..71ad7c18523 100644
--- a/gcc/tree-complex.c
+++ b/gcc/tree-complex.c
@@ -570,8 +570,10 @@ set_component_ssa_name (tree ssa_name, bool imag_p, tree
value)
       /* Replace an anonymous base value with the variable from cvc_lookup.
         This should result in better debug info.  */
       if (SSA_NAME_VAR (ssa_name)
-         && (!SSA_NAME_VAR (value) || DECL_IGNORED_P (SSA_NAME_VAR (value)))
-         && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name)))
+         && !DECL_IGNORED_P (SSA_NAME_VAR (ssa_name))
+         && (!SSA_NAME_VAR (value)
+             || (DECL_IGNORED_P (SSA_NAME_VAR (value))
+                 && !SSA_NAME_IS_DEFAULT_DEF (value))))
        {
          comp = get_component_var (SSA_NAME_VAR (ssa_name), imag_p);
          replace_ssa_name_symbol (value, comp);

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug ipa/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (4 preceding siblings ...)
  2020-10-16 15:56 ` jamborm at gcc dot gnu.org
@ 2020-10-16 18:53 ` jamborm at gcc dot gnu.org
  2020-10-16 18:54 ` [Bug tree-optimization/97456] " jamborm at gcc dot gnu.org
                   ` (5 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jamborm at gcc dot gnu.org @ 2020-10-16 18:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

--- Comment #6 from Martin Jambor <jamborm at gcc dot gnu.org> ---
I have posted the patch to the mailing list:

https://gcc.gnu.org/pipermail/gcc-patches/2020-October/556399.html

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug tree-optimization/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (5 preceding siblings ...)
  2020-10-16 18:53 ` jamborm at gcc dot gnu.org
@ 2020-10-16 18:54 ` jamborm at gcc dot gnu.org
  2020-10-19 17:22 ` cvs-commit at gcc dot gnu.org
                   ` (4 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: jamborm at gcc dot gnu.org @ 2020-10-16 18:54 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

Martin Jambor <jamborm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
          Component|ipa                         |tree-optimization

--- Comment #7 from Martin Jambor <jamborm at gcc dot gnu.org> ---
Changing component to tree-optimization.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug tree-optimization/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (6 preceding siblings ...)
  2020-10-16 18:54 ` [Bug tree-optimization/97456] " jamborm at gcc dot gnu.org
@ 2020-10-19 17:22 ` cvs-commit at gcc dot gnu.org
  2020-10-26 16:33 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-10-19 17:22 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Martin Jambor <jamborm@gcc.gnu.org>:

https://gcc.gnu.org/g:619f91eaa8c8a50f1f9d3e7b96ee837037f0e806

commit r11-4075-g619f91eaa8c8a50f1f9d3e7b96ee837037f0e806
Author: Martin Jambor <mjambor@suse.cz>
Date:   Mon Oct 19 19:21:10 2020 +0200

    cplxlower: Avoid a transform when looking at a default definition

    In PR 97456, IPA-SRA triggers a bug in tree-complex.c where it
    converts:

     <bb 2>
       a$_M_value_21 = COMPLEX_EXPR <ISRA.18_10(D), ISRA.18_10(D)>;

    (where ISRA.18 is IPA-SRA created PARM_DECL with DECL_IGNORED_P set,
    which is why it only happens with IPA-SRA) into:

      <bb 2>
        a$_M_value_21 = COMPLEX_EXPR <a$_M_value$real_10(D),
a$_M_value$real_10(D)>;

    i.e. it replaces two uses of the parameter default-def with two
    uninitialized default-defs of a new variable - all in hope to produce
    code with better debug info.

    This patch fixes it by avoiding the transform when the SSA_NAME to be
    replaced is a default definition.

    gcc/ChangeLog:

    2020-10-19  Martin Jambor  <mjambor@suse.cz>

            PR tree-optimization/97456
            * tree-complex.c (set_component_ssa_name): Do not replace ignored
decl
            default definitions with new component vars.  Reorder if
conditions.

    gcc/testsuite/ChangeLog:

    2020-10-19  Martin Jambor  <mjambor@suse.cz>

            PR tree-optimization/97456
            * gcc.dg/tree-ssa/pr97456.c: New test.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug tree-optimization/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (7 preceding siblings ...)
  2020-10-19 17:22 ` cvs-commit at gcc dot gnu.org
@ 2020-10-26 16:33 ` cvs-commit at gcc dot gnu.org
  2020-10-26 16:34 ` jamborm at gcc dot gnu.org
                   ` (2 subsequent siblings)
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-10-26 16:33 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Martin Jambor
<jamborm@gcc.gnu.org>:

https://gcc.gnu.org/g:d99b9dea37adfb88de2af121d31eed1c06b174b4

commit r10-8945-gd99b9dea37adfb88de2af121d31eed1c06b174b4
Author: Martin Jambor <mjambor@suse.cz>
Date:   Mon Oct 19 19:21:10 2020 +0200

    cplxlower: Avoid a transform when looking at a default definition

    In PR 97456, IPA-SRA triggers a bug in tree-complex.c where it
    converts:

     <bb 2>
       a$_M_value_21 = COMPLEX_EXPR <ISRA.18_10(D), ISRA.18_10(D)>;

    (where ISRA.18 is IPA-SRA created PARM_DECL with DECL_IGNORED_P set,
    which is why it only happens with IPA-SRA) into:

      <bb 2>
        a$_M_value_21 = COMPLEX_EXPR <a$_M_value$real_10(D),
a$_M_value$real_10(D)>;

    i.e. it replaces two uses of the parameter default-def with two
    uninitialized default-defs of a new variable - all in hope to produce
    code with better debug info.

    This patch fixes it by avoiding the transform when the SSA_NAME to be
    replaced is a default definition.

    gcc/ChangeLog:

    2020-10-19  Martin Jambor  <mjambor@suse.cz>

            PR tree-optimization/97456
            * tree-complex.c (set_component_ssa_name): Do not replace ignored
decl
            default definitions with new component vars.  Reorder if
conditions.

    gcc/testsuite/ChangeLog:

    2020-10-19  Martin Jambor  <mjambor@suse.cz>

            PR tree-optimization/97456
            * gcc.dg/tree-ssa/pr97456.c: New test.

    (cherry picked from commit 619f91eaa8c8a50f1f9d3e7b96ee837037f0e806)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug tree-optimization/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (8 preceding siblings ...)
  2020-10-26 16:33 ` cvs-commit at gcc dot gnu.org
@ 2020-10-26 16:34 ` jamborm at gcc dot gnu.org
  2022-03-30 11:51 ` cvs-commit at gcc dot gnu.org
  2022-03-30 11:53 ` jamborm at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: jamborm at gcc dot gnu.org @ 2020-10-26 16:34 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

Martin Jambor <jamborm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|NEW                         |RESOLVED

--- Comment #10 from Martin Jambor <jamborm at gcc dot gnu.org> ---
Fixed on both master and the gcc-10 branch.  Thanks for reporting.

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug tree-optimization/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (9 preceding siblings ...)
  2020-10-26 16:34 ` jamborm at gcc dot gnu.org
@ 2022-03-30 11:51 ` cvs-commit at gcc dot gnu.org
  2022-03-30 11:53 ` jamborm at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-03-30 11:51 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

--- Comment #11 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-9 branch has been updated by Martin Jambor
<jamborm@gcc.gnu.org>:

https://gcc.gnu.org/g:1dc4bed43760c4e291e072aa0c8c450f5775e25f

commit r9-10000-g1dc4bed43760c4e291e072aa0c8c450f5775e25f
Author: Martin Jambor <mjambor@suse.cz>
Date:   Wed Mar 30 13:47:24 2022 +0200

    cplxlower: Avoid a transform when looking at a default definition

    In PR 97456, IPA-SRA triggers a bug in tree-complex.c where it
    converts:

     <bb 2>
       a$_M_value_21 = COMPLEX_EXPR <ISRA.18_10(D), ISRA.18_10(D)>;

    (where ISRA.18 is IPA-SRA created PARM_DECL with DECL_IGNORED_P set,
    which is why it only happens with IPA-SRA) into:

      <bb 2>
        a$_M_value_21 = COMPLEX_EXPR <a$_M_value$real_10(D),
a$_M_value$real_10(D)>;

    i.e. it replaces two uses of the parameter default-def with two
    uninitialized default-defs of a new variable - all in hope to produce
    code with better debug info.

    This patch fixes it by avoiding the transform when the SSA_NAME to be
    replaced is a default definition.

    gcc/ChangeLog:

    2020-10-19  Martin Jambor  <mjambor@suse.cz>

            PR tree-optimization/97456
            PR middle-end/105071
            * tree-complex.c (set_component_ssa_name): Do not replace ignored
decl
            default definitions with new component vars.  Reorder if
conditions.

    gcc/testsuite/ChangeLog:

    2020-10-19  Martin Jambor  <mjambor@suse.cz>

            PR tree-optimization/97456
            * gcc.dg/tree-ssa/pr97456.c: New test.

    (cherry picked from commit 619f91eaa8c8a50f1f9d3e7b96ee837037f0e806)

^ permalink raw reply	[flat|nested] 13+ messages in thread

* [Bug tree-optimization/97456] [10/11 Regression] An incorrect optimization causes a function to always return the same value when using -flto
  2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
                   ` (10 preceding siblings ...)
  2022-03-30 11:51 ` cvs-commit at gcc dot gnu.org
@ 2022-03-30 11:53 ` jamborm at gcc dot gnu.org
  11 siblings, 0 replies; 13+ messages in thread
From: jamborm at gcc dot gnu.org @ 2022-03-30 11:53 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97456

Martin Jambor <jamborm at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dlong at cadence dot com

--- Comment #12 from Martin Jambor <jamborm at gcc dot gnu.org> ---
*** Bug 105071 has been marked as a duplicate of this bug. ***

^ permalink raw reply	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-03-30 11:53 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-16 10:25 [Bug c++/97456] New: An incorrect optimization causes a function to always return the same value when using -flto pqfrml at gmail dot com
2020-10-16 10:29 ` [Bug c++/97456] " pqfrml at gmail dot com
2020-10-16 11:00 ` [Bug ipa/97456] [10/11 Regression] " rguenth at gcc dot gnu.org
2020-10-16 12:06 ` marxin at gcc dot gnu.org
2020-10-16 14:51 ` jamborm at gcc dot gnu.org
2020-10-16 15:56 ` jamborm at gcc dot gnu.org
2020-10-16 18:53 ` jamborm at gcc dot gnu.org
2020-10-16 18:54 ` [Bug tree-optimization/97456] " jamborm at gcc dot gnu.org
2020-10-19 17:22 ` cvs-commit at gcc dot gnu.org
2020-10-26 16:33 ` cvs-commit at gcc dot gnu.org
2020-10-26 16:34 ` jamborm at gcc dot gnu.org
2022-03-30 11:51 ` cvs-commit at gcc dot gnu.org
2022-03-30 11:53 ` jamborm at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).