public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization
@ 2023-09-21 11:27 aegges at web dot de
  2023-09-21 16:13 ` [Bug c++/111517] [12/13 Regression] " pinskia at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: aegges at web dot de @ 2023-09-21 11:27 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111517
           Summary: Optimization -O1 removes necessary loop for
                    initialization
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: aegges at web dot de
  Target Milestone: ---

We have found a regression in gcc 12 (and later) in comparison to gcc 11 (and
clang) which occurs if optimization (at least -O1) is enabled.
In the following C++ code the member variables a1 and e1 are initialized in a
loop in a init() method which is called in the constructor.  

#include <iostream>
#include <array>

class TestClass
{
public:
   TestClass()
   { 
       for(int i=0; i<20; i++)
       {
          a1[i] = i;
       }

       init();
    }

    void init();
    unsigned int a1[20];
    unsigned char e1[20][20];
};

void TestClass::init()
{
        for (int b1 = 0; b1 < 20; b1++)
        {
                for (int b2 = 0; b2 < 20; b2++)
                {
                        e1[b1][b2] = a1[b2];
                }
        }
}

TestClass tmp;

int main()
{
    for(int i=0; i < 20 ; i++)
        std::cout << std::to_string(tmp.e1[i][i]) << " ";
}
expected output: (gcc <= 11 or gcc >=12 w/o optimization)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

actual output: 8gcc >=12 with optimization)
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 


I have used godbolt to verify the different behavior (see
https://godbolt.org/z/MnoPE8Yfr )

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

* [Bug c++/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization
  2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
@ 2023-09-21 16:13 ` pinskia at gcc dot gnu.org
  2023-09-21 16:19 ` [Bug tree-optimization/111517] " pinskia at gcc dot gnu.org
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-21 16:13 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|Optimization -O1 removes    |[12/13 Regression]
                   |necessary loop for          |Optimization -O1 removes
                   |initialization              |necessary loop for
                   |                            |initialization
      Known to work|14.0                        |
   Target Milestone|---                         |11.5

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
-fno-ivopts allows the code to pass.

The difference between GCC 13 and the trunk is:
trunk:
  _30 = &MEM[(unsigned int *)0B + -320B + ivtmp.27_37 + ivtmp.16_4 * 4];
  _1 = *_30;

vs:
GCC 13:
  _1 = MEM[(unsigned int *)0B + -320B + ivtmp.27_36 + ivtmp.16_4 * 4];


And the call 

  TestClass::init (&tmp);

is still there in _GLOBAL__sub_I__ZN9TestClass4initEv ...

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

* [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization
  2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
  2023-09-21 16:13 ` [Bug c++/111517] [12/13 Regression] " pinskia at gcc dot gnu.org
@ 2023-09-21 16:19 ` pinskia at gcc dot gnu.org
  2023-09-21 18:34 ` theodort at inf dot ethz.ch
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-21 16:19 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2023-09-21
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
On the subject of:
  _1 = MEM[(unsigned int *)0B + -320B + ivtmp.27_36 + ivtmp.16_4 * 4];

during the local-pure-const2 pass:

    Indirect ref write is not const/pure
vs:
  NULL memory access; terminating BB


But that MEM is not exactly an NULL Pointer access ...


  _39 = (sizetype) this_9(D);
  ivtmp.27_38 = _39 * 18446744073709551613;
  ivtmp.28_42 = _39 + 100;


This is also definitely IV-OPTs messing up ...

Though before GCC 12 it had that too but the local-pure-const2 pass it thought
something different about it:

  scanning: _1 = MEM[(unsigned int *)0B + -320B + ivtmp.27_34 + ivtmp.16_3 *
4];
    Indirect ref to local or readonly memory is OK

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

* [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization
  2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
  2023-09-21 16:13 ` [Bug c++/111517] [12/13 Regression] " pinskia at gcc dot gnu.org
  2023-09-21 16:19 ` [Bug tree-optimization/111517] " pinskia at gcc dot gnu.org
@ 2023-09-21 18:34 ` theodort at inf dot ethz.ch
  2023-09-21 18:46 ` [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization since r12-5915-ge93809f62363ba pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: theodort at inf dot ethz.ch @ 2023-09-21 18:34 UTC (permalink / raw)
  To: gcc-bugs

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

Theodoros Theodoridis <theodort at inf dot ethz.ch> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |theodort at inf dot ethz.ch

--- Comment #3 from Theodoros Theodoridis <theodort at inf dot ethz.ch> ---
It bisects to r12-5915-ge93809f6236

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

* [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization since r12-5915-ge93809f62363ba
  2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
                   ` (2 preceding siblings ...)
  2023-09-21 18:34 ` theodort at inf dot ethz.ch
@ 2023-09-21 18:46 ` pinskia at gcc dot gnu.org
  2023-09-21 18:50 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-21 18:46 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|11.5                        |12.4

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

* [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization since r12-5915-ge93809f62363ba
  2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
                   ` (3 preceding siblings ...)
  2023-09-21 18:46 ` [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization since r12-5915-ge93809f62363ba pinskia at gcc dot gnu.org
@ 2023-09-21 18:50 ` pinskia at gcc dot gnu.org
  2023-09-22  5:46 ` rguenth at gcc dot gnu.org
  2023-09-22  8:04 ` aegges at web dot de
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-09-21 18:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
r0-126272-g8fdc414d439bc7148e079d27220e59 adds check_loadstore but IVOPTS can
sometimes produce these TARGET_MEM_REF with 0 as first operand ...

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

* [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization since r12-5915-ge93809f62363ba
  2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
                   ` (4 preceding siblings ...)
  2023-09-21 18:50 ` pinskia at gcc dot gnu.org
@ 2023-09-22  5:46 ` rguenth at gcc dot gnu.org
  2023-09-22  8:04 ` aegges at web dot de
  6 siblings, 0 replies; 8+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-09-22  5:46 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #5 from Richard Biener <rguenth at gcc dot gnu.org> ---
duplicate

*** This bug has been marked as a duplicate of bug 110702 ***

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

* [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization since r12-5915-ge93809f62363ba
  2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
                   ` (5 preceding siblings ...)
  2023-09-22  5:46 ` rguenth at gcc dot gnu.org
@ 2023-09-22  8:04 ` aegges at web dot de
  6 siblings, 0 replies; 8+ messages in thread
From: aegges at web dot de @ 2023-09-22  8:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Axel Mueller <aegges at web dot de> ---
I can confirm that the code now works with trunk version on godbolt.
Thanks for the quick analysis (and of course the fix). Looking forward to the
12.4 fix release.

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

end of thread, other threads:[~2023-09-22  8:04 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-09-21 11:27 [Bug c++/111517] New: Optimization -O1 removes necessary loop for initialization aegges at web dot de
2023-09-21 16:13 ` [Bug c++/111517] [12/13 Regression] " pinskia at gcc dot gnu.org
2023-09-21 16:19 ` [Bug tree-optimization/111517] " pinskia at gcc dot gnu.org
2023-09-21 18:34 ` theodort at inf dot ethz.ch
2023-09-21 18:46 ` [Bug tree-optimization/111517] [12/13 Regression] Optimization -O1 removes necessary loop for initialization since r12-5915-ge93809f62363ba pinskia at gcc dot gnu.org
2023-09-21 18:50 ` pinskia at gcc dot gnu.org
2023-09-22  5:46 ` rguenth at gcc dot gnu.org
2023-09-22  8:04 ` aegges at web dot de

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).