public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/111211] New: No warning for iterator going out of scope
@ 2023-08-28  7:42 lehua.ding at rivai dot ai
  2023-08-28  7:44 ` [Bug c/111211] " lehua.ding at rivai dot ai
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: lehua.ding at rivai dot ai @ 2023-08-28  7:42 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 111211
           Summary: No warning for iterator going out of scope
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lehua.ding at rivai dot ai
  Target Milestone: ---

Compiler Explorer: https://godbolt.org/z/o4qPbovaz

There is a warning reported when the number of iterations is less than or equal
to 7, and no warning reported when it is greater than 7. I feel this behavior
is a bit strange.

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

* [Bug c/111211] No warning for iterator going out of scope
  2023-08-28  7:42 [Bug c/111211] New: No warning for iterator going out of scope lehua.ding at rivai dot ai
@ 2023-08-28  7:44 ` lehua.ding at rivai dot ai
  2023-08-28  9:27 ` rguenth at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: lehua.ding at rivai dot ai @ 2023-08-28  7:44 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from Lehua Ding <lehua.ding at rivai dot ai> ---
Reproduce:

Compile Command: gcc -O3 -Wall -Wextra

C Code:

```
#include <stdint.h>

int foo (uint64_t ddr0_addr_access)
{
    uint64_t check[1] = {0};

    for (int k = 0; k < 7; k += 1)
    {
        asm volatile ("nop" : "=r"(check[k]) : "r"(ddr0_addr_access));
    }
    return 0;
}

int foo2 (uint64_t ddr0_addr_access)
{
    uint64_t check[1] = {0};

    for (int k = 0; k < 8; k += 1)
    {
        asm volatile ("nop" : "=r"(check[k]) : "r"(ddr0_addr_access));
    }
    return 0;
}
```

Output:

<source>: In function 'foo':
<source>:9:41: warning: array subscript 1 is above array bounds of
'uint64_t[1]' {aka 'long unsigned int[1]'} [-Warray-bounds=]
    9 |         asm volatile ("nop" : "=r"(check[k]) : "r"(ddr0_addr_access));
      |                                    ~~~~~^~~
<source>:5:14: note: while referencing 'check'
    5 |     uint64_t check[1] = {0};
      |              ^~~~~
<source>:9:41: warning: array subscript 2 is above array bounds of
'uint64_t[1]' {aka 'long unsigned int[1]'} [-Warray-bounds=]
    9 |         asm volatile ("nop" : "=r"(check[k]) : "r"(ddr0_addr_access));
      |                                    ~~~~~^~~
<source>:5:14: note: while referencing 'check'
    5 |     uint64_t check[1] = {0};
      |              ^~~~~
<source>:9:41: warning: array subscript 3 is above array bounds of
'uint64_t[1]' {aka 'long unsigned int[1]'} [-Warray-bounds=]
    9 |         asm volatile ("nop" : "=r"(check[k]) : "r"(ddr0_addr_access));
      |                                    ~~~~~^~~
<source>:5:14: note: while referencing 'check'
    5 |     uint64_t check[1] = {0};
      |              ^~~~~
<source>:9:41: warning: array subscript 4 is above array bounds of
'uint64_t[1]' {aka 'long unsigned int[1]'} [-Warray-bounds=]
    9 |         asm volatile ("nop" : "=r"(check[k]) : "r"(ddr0_addr_access));
      |                                    ~~~~~^~~
<source>:5:14: note: while referencing 'check'
    5 |     uint64_t check[1] = {0};
      |              ^~~~~
<source>:9:41: warning: array subscript 5 is above array bounds of
'uint64_t[1]' {aka 'long unsigned int[1]'} [-Warray-bounds=]
    9 |         asm volatile ("nop" : "=r"(check[k]) : "r"(ddr0_addr_access));
      |                                    ~~~~~^~~
<source>:5:14: note: while referencing 'check'
    5 |     uint64_t check[1] = {0};
      |              ^~~~~
<source>:9:41: warning: array subscript 6 is above array bounds of
'uint64_t[1]' {aka 'long unsigned int[1]'} [-Warray-bounds=]
    9 |         asm volatile ("nop" : "=r"(check[k]) : "r"(ddr0_addr_access));
      |                                    ~~~~~^~~
<source>:5:14: note: while referencing 'check'
    5 |     uint64_t check[1] = {0};
      |              ^~~~~
Compiler returned: 0

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

* [Bug c/111211] No warning for iterator going out of scope
  2023-08-28  7:42 [Bug c/111211] New: No warning for iterator going out of scope lehua.ding at rivai dot ai
  2023-08-28  7:44 ` [Bug c/111211] " lehua.ding at rivai dot ai
@ 2023-08-28  9:27 ` rguenth at gcc dot gnu.org
  2023-08-28  9:30 ` lehua.ding at rivai dot ai
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: rguenth at gcc dot gnu.org @ 2023-08-28  9:27 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Richard Biener <rguenth at gcc dot gnu.org> ---
We diagnose this after unrolling, so the difference is whether we unroll or
not.

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

* [Bug c/111211] No warning for iterator going out of scope
  2023-08-28  7:42 [Bug c/111211] New: No warning for iterator going out of scope lehua.ding at rivai dot ai
  2023-08-28  7:44 ` [Bug c/111211] " lehua.ding at rivai dot ai
  2023-08-28  9:27 ` rguenth at gcc dot gnu.org
@ 2023-08-28  9:30 ` lehua.ding at rivai dot ai
  2023-08-28 13:13 ` pinskia at gcc dot gnu.org
  2023-08-28 13:54 ` [Bug tree-optimization/111211] No warning for iterator going out of scope for writing to array of inline-asm pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: lehua.ding at rivai dot ai @ 2023-08-28  9:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Lehua Ding <lehua.ding at rivai dot ai> ---
(In reply to Richard Biener from comment #2)
> We diagnose this after unrolling, so the difference is whether we unroll or
> not.

But based on the assembly code it looks like both are unrolled.

foo:
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        xor     eax, eax
        ret
foo2:
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        nop
        xor     eax, eax
        ret

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

* [Bug c/111211] No warning for iterator going out of scope
  2023-08-28  7:42 [Bug c/111211] New: No warning for iterator going out of scope lehua.ding at rivai dot ai
                   ` (2 preceding siblings ...)
  2023-08-28  9:30 ` lehua.ding at rivai dot ai
@ 2023-08-28 13:13 ` pinskia at gcc dot gnu.org
  2023-08-28 13:54 ` [Bug tree-optimization/111211] No warning for iterator going out of scope for writing to array of inline-asm pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-28 13:13 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Lehua Ding from comment #3)
> (In reply to Richard Biener from comment #2)
> > We diagnose this after unrolling, so the difference is whether we unroll or
> > not.
> 
> But based on the assembly code it looks like both are unrolled.
> 
> foo:
>         nop
>         nop
>         nop
>         nop
>         nop
>         nop
>         nop
>         xor     eax, eax
>         ret
> foo2:
>         nop
>         nop
>         nop
>         nop
>         nop
>         nop
>         nop
>         nop
>         xor     eax, eax
>         ret

At different times in the pipeline and the warning happens before the second
unrollinb

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

* [Bug tree-optimization/111211] No warning for iterator going out of scope for writing to array of inline-asm
  2023-08-28  7:42 [Bug c/111211] New: No warning for iterator going out of scope lehua.ding at rivai dot ai
                   ` (3 preceding siblings ...)
  2023-08-28 13:13 ` pinskia at gcc dot gnu.org
@ 2023-08-28 13:54 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-08-28 13:54 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |inline-asm
            Summary|No warning for iterator     |No warning for iterator
                   |going out of scope          |going out of scope for
                   |                            |writing to array of
                   |                            |inline-asm

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note this is only an issue with inline-asm really and only if write directly in
the array.

If we change the code slightly:
```
#include <stdint.h>
int foo2 (uint64_t ddr0_addr_access)
{
    uint64_t check[1] = {0};

    for (int k = 0; k < 8; k += 1)
    {
        int t;
        asm volatile ("nop" : "=r"(t) : "r"(ddr0_addr_access));
        check[k] = t;
    }
    return 0;
}
```

GCC does warn (though slightly different):
```
<source>:11:18: warning: iteration 1 invokes undefined behavior
[-Waggressive-loop-optimizations]
   11 |         check[k] = t;
      |         ~~~~~~~~~^~~
<source>:7:23: note: within this loop
    7 |     for (int k = 0; k < 8; k += 1)
      |                     ~~^~~
```

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

end of thread, other threads:[~2023-08-28 13:54 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-28  7:42 [Bug c/111211] New: No warning for iterator going out of scope lehua.ding at rivai dot ai
2023-08-28  7:44 ` [Bug c/111211] " lehua.ding at rivai dot ai
2023-08-28  9:27 ` rguenth at gcc dot gnu.org
2023-08-28  9:30 ` lehua.ding at rivai dot ai
2023-08-28 13:13 ` pinskia at gcc dot gnu.org
2023-08-28 13:54 ` [Bug tree-optimization/111211] No warning for iterator going out of scope for writing to array of inline-asm pinskia 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).