public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/103724] New: warning
@ 2021-12-15  3:05 f.heckenbach@fh-soft.de
  2021-12-15  3:11 ` [Bug c/103724] invalid warning: iteration 7 invokes undefined behavior f.heckenbach@fh-soft.de
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: f.heckenbach@fh-soft.de @ 2021-12-15  3:05 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103724
           Summary: warning
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: f.heckenbach@fh-soft.de
  Target Milestone: ---

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

* [Bug c/103724] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
@ 2021-12-15  3:11 ` f.heckenbach@fh-soft.de
  2021-12-18  9:08 ` [Bug tree-optimization/103724] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
                   ` (9 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: f.heckenbach@fh-soft.de @ 2021-12-15  3:11 UTC (permalink / raw)
  To: gcc-bugs

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

Frank Heckenbach <f.heckenbach@fh-soft.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
            Summary|warning                     |invalid warning: iteration
                   |                            |7 invokes undefined
                   |                            |behavior

--- Comment #1 from Frank Heckenbach <f.heckenbach@fh-soft.de> ---
% cat test.c 
int d = 0, b = 8, a[8][8];

int main ()
{
  for (int c = 0; c < 11; c++)
    {
      d = a[c >= b ? c - b : c][c + 1 >= b ? c + 1 - b : c + 1];
      if (b && c)
        d = c;
    }
}
% gcc -O3 test.c
test.c: In function 'main':
test.c:7:32: warning: iteration 7 invokes undefined behavior
[-Waggressive-loop-optimizations]
    7 |       d = a[c >= b ? c - b : c][c + 1 >= b ? c + 1 - b : c + 1];
      |           ~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
test.c:5:3: note: within this loop
    5 |   for (int c = 0; c < 11; c++)
      |   ^~~

Many things are strange about this warning:

- First, I think it's invalid -- the "?:" ensures the array indexes are in
bounds, all variables are initialized,so I don't see what would be UB.

- It claims UB occurs on iteration 7, but the warning disappears when I change
the loop condition to "c < 10" (which would also include iteration 7)

- The "if" statement with both conditions and the assignment is necessary to
trigger the warning, although it has no actual effect.

- The warning seems to apply to the 2nd array index, but already disappears
when I turn the first index into "0" without changing the 2nd one.

- It disappears when I make all variables local or all global.

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

* [Bug tree-optimization/103724] [9/10/11/12 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
  2021-12-15  3:11 ` [Bug c/103724] invalid warning: iteration 7 invokes undefined behavior f.heckenbach@fh-soft.de
@ 2021-12-18  9:08 ` pinskia at gcc dot gnu.org
  2021-12-28 19:53 ` jakub at gcc dot gnu.org
                   ` (8 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: pinskia at gcc dot gnu.org @ 2021-12-18  9:08 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|---                         |9.5
             Status|UNCONFIRMED                 |NEW
      Known to fail|                            |6.3.0, 6.4.0, 7.1.0
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-12-18
      Known to work|                            |5.5.0, 6.1.0, 6.2.0
            Summary|invalid warning: iteration  |[9/10/11/12 Regression]
                   |7 invokes undefined         |invalid warning: iteration
                   |behavior                    |7 invokes undefined
                   |                            |behavior

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Confirmed. GCC is completely unrolling this loop and then not knowing that b is
8 warns about it. Though I do find it interesting how 6.2.0 doesn't warn but
6.3.0 does.

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

* [Bug tree-optimization/103724] [9/10/11/12 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
  2021-12-15  3:11 ` [Bug c/103724] invalid warning: iteration 7 invokes undefined behavior f.heckenbach@fh-soft.de
  2021-12-18  9:08 ` [Bug tree-optimization/103724] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
@ 2021-12-28 19:53 ` jakub at gcc dot gnu.org
  2022-01-18 14:07 ` rguenth at gcc dot gnu.org
                   ` (7 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2021-12-28 19:53 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jakub at gcc dot gnu.org

--- Comment #3 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Seems the reason we warn is that we first unswitch the loop because of the
(useless) b != 0 test in the loop, and so end up with:
  if (b == 0)
    for (int c = 0; c < 11; c++)
      d = a[c][c + 1];
  else
    for (int c = 0; c < 11; c++)
      {
        d = a[c >= b ? c - b : c][c + 1 >= b ? c + 1 - b : c + 1];
        if (b && c)
          d = c;
      }
(the c >= b and c + 1 >= b comparisons in the first loop can be simplified
because of the [0, 11] range for c), and on the first loop it indeed would
invoke UB in 7th iteration.
So the warning although it isn't clear from it (and the compiler doesn't know
either) is about that if b is 0, which is something the user loop tests for,
then it invokes UB, otherwise it isn't known whether it does or doesn't.
This is a general problem with middle-end warnings, after unswitching, jump
threading and similar optimizations it is unclear what code has been
specialized on something that really ever happens in the code at runtime and
what is dead.

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

* [Bug tree-optimization/103724] [9/10/11/12 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
                   ` (2 preceding siblings ...)
  2021-12-28 19:53 ` jakub at gcc dot gnu.org
@ 2022-01-18 14:07 ` rguenth at gcc dot gnu.org
  2022-01-20  3:34 ` f.heckenbach@fh-soft.de
                   ` (6 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-01-18 14:07 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |dmalcolm at gcc dot gnu.org,
                   |                            |msebor at gcc dot gnu.org

--- Comment #4 from Richard Biener <rguenth at gcc dot gnu.org> ---
One thing we could do is annotate struct loop * with the (high level)
optimizations we've applied so that when we emit this warning we could say

note: this loop is the copy generated by loop unswitching where b == 0

or so.  Or maybe at least show

note: this loop was unswitched

the optimization-report/opt-info machinery might have a way to "queue"
such stuff.  For sure not in a directly usable way.  Likewise jump threadings
could record BB origins as "part of a threaded path covering conditions at
loc1, loc2" or so.

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

* [Bug tree-optimization/103724] [9/10/11/12 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
                   ` (3 preceding siblings ...)
  2022-01-18 14:07 ` rguenth at gcc dot gnu.org
@ 2022-01-20  3:34 ` f.heckenbach@fh-soft.de
  2022-03-09 13:36 ` rguenth at gcc dot gnu.org
                   ` (5 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: f.heckenbach@fh-soft.de @ 2022-01-20  3:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #5 from Frank Heckenbach <f.heckenbach@fh-soft.de> ---
(In reply to Richard Biener from comment #4)
> One thing we could do is annotate struct loop * with the (high level)
> optimizations we've applied so that when we emit this warning we could say
> 
> note: this loop is the copy generated by loop unswitching where b == 0
> 
> or so.  Or maybe at least show
> 
> note: this loop was unswitched

If this is meant to apply to user-visible warnings, I'm not sure I'd like it. I
usually run with -Werror, and this would still be a warning turned error then,
wouldn't it?

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

* [Bug tree-optimization/103724] [9/10/11/12 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
                   ` (4 preceding siblings ...)
  2022-01-20  3:34 ` f.heckenbach@fh-soft.de
@ 2022-03-09 13:36 ` rguenth at gcc dot gnu.org
  2022-03-09 15:22 ` f.heckenbach@fh-soft.de
                   ` (4 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-03-09 13:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #6 from Richard Biener <rguenth at gcc dot gnu.org> ---
(In reply to Frank Heckenbach from comment #5)
> (In reply to Richard Biener from comment #4)
> > One thing we could do is annotate struct loop * with the (high level)
> > optimizations we've applied so that when we emit this warning we could say
> > 
> > note: this loop is the copy generated by loop unswitching where b == 0
> > 
> > or so.  Or maybe at least show
> > 
> > note: this loop was unswitched
> 
> If this is meant to apply to user-visible warnings, I'm not sure I'd like
> it. I usually run with -Werror, and this would still be a warning turned
> error then, wouldn't it?

Yes.  It might provide you with hints how to work around things though,
like do if (c) since b must be != 0.

That said, I don't think we can reasonably do something on the GCC side here.

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

* [Bug tree-optimization/103724] [9/10/11/12 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
                   ` (5 preceding siblings ...)
  2022-03-09 13:36 ` rguenth at gcc dot gnu.org
@ 2022-03-09 15:22 ` f.heckenbach@fh-soft.de
  2022-05-27  9:46 ` [Bug tree-optimization/103724] [10/11/12/13 " rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: f.heckenbach@fh-soft.de @ 2022-03-09 15:22 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from Frank Heckenbach <f.heckenbach@fh-soft.de> ---
(In reply to Richard Biener from comment #6)
> (In reply to Frank Heckenbach from comment #5)
> > (In reply to Richard Biener from comment #4)
> > > One thing we could do is annotate struct loop * with the (high level)
> > > optimizations we've applied so that when we emit this warning we could say
> > > 
> > > note: this loop is the copy generated by loop unswitching where b == 0
> > > 
> > > or so.  Or maybe at least show
> > > 
> > > note: this loop was unswitched
> > 
> > If this is meant to apply to user-visible warnings, I'm not sure I'd like
> > it. I usually run with -Werror, and this would still be a warning turned
> > error then, wouldn't it?
> 
> Yes.  It might provide you with hints how to work around things though,
> like do if (c) since b must be != 0.

That was just a simplified test case. In my real code, there's nothing to
simplify from a user's point of view.

> That said, I don't think we can reasonably do something on the GCC side here.

How about an option or warning level (if not default) to just omit the warning
in the cases you'd say "note: this loop was unswitched"?

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

* [Bug tree-optimization/103724] [10/11/12/13 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
                   ` (6 preceding siblings ...)
  2022-03-09 15:22 ` f.heckenbach@fh-soft.de
@ 2022-05-27  9:46 ` rguenth at gcc dot gnu.org
  2022-06-28 10:47 ` jakub at gcc dot gnu.org
                   ` (2 subsequent siblings)
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-05-27  9:46 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|9.5                         |10.4

--- Comment #8 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 9 branch is being closed

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

* [Bug tree-optimization/103724] [10/11/12/13 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
                   ` (7 preceding siblings ...)
  2022-05-27  9:46 ` [Bug tree-optimization/103724] [10/11/12/13 " rguenth at gcc dot gnu.org
@ 2022-06-28 10:47 ` jakub at gcc dot gnu.org
  2022-12-20 13:31 ` rguenth at gcc dot gnu.org
  2023-07-07 10:41 ` [Bug tree-optimization/103724] [11/12/13/14 " rguenth at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-06-28 10:47 UTC (permalink / raw)
  To: gcc-bugs

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

Jakub Jelinek <jakub at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Target Milestone|10.4                        |10.5

--- Comment #9 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
GCC 10.4 is being released, retargeting bugs to GCC 10.5.

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

* [Bug tree-optimization/103724] [10/11/12/13 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
                   ` (8 preceding siblings ...)
  2022-06-28 10:47 ` jakub at gcc dot gnu.org
@ 2022-12-20 13:31 ` rguenth at gcc dot gnu.org
  2023-07-07 10:41 ` [Bug tree-optimization/103724] [11/12/13/14 " rguenth at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-12-20 13:31 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P3                          |P2

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

* [Bug tree-optimization/103724] [11/12/13/14 Regression] invalid warning: iteration 7 invokes undefined behavior
  2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
                   ` (9 preceding siblings ...)
  2022-12-20 13:31 ` rguenth at gcc dot gnu.org
@ 2023-07-07 10:41 ` rguenth at gcc dot gnu.org
  10 siblings, 0 replies; 12+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-07-07 10:41 UTC (permalink / raw)
  To: gcc-bugs

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

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

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

--- Comment #10 from Richard Biener <rguenth at gcc dot gnu.org> ---
GCC 10 branch is being closed.

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

end of thread, other threads:[~2023-07-07 10:41 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-12-15  3:05 [Bug c/103724] New: warning f.heckenbach@fh-soft.de
2021-12-15  3:11 ` [Bug c/103724] invalid warning: iteration 7 invokes undefined behavior f.heckenbach@fh-soft.de
2021-12-18  9:08 ` [Bug tree-optimization/103724] [9/10/11/12 Regression] " pinskia at gcc dot gnu.org
2021-12-28 19:53 ` jakub at gcc dot gnu.org
2022-01-18 14:07 ` rguenth at gcc dot gnu.org
2022-01-20  3:34 ` f.heckenbach@fh-soft.de
2022-03-09 13:36 ` rguenth at gcc dot gnu.org
2022-03-09 15:22 ` f.heckenbach@fh-soft.de
2022-05-27  9:46 ` [Bug tree-optimization/103724] [10/11/12/13 " rguenth at gcc dot gnu.org
2022-06-28 10:47 ` jakub at gcc dot gnu.org
2022-12-20 13:31 ` rguenth at gcc dot gnu.org
2023-07-07 10:41 ` [Bug tree-optimization/103724] [11/12/13/14 " rguenth 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).