public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug other/115437] New: Wrong optimized code - GCC didn't detect a variable is modified
@ 2024-06-11 15:19 Explorer09 at gmail dot com
  2024-06-11 15:25 ` [Bug other/115437] " pinskia at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Explorer09 at gmail dot com @ 2024-06-11 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115437
           Summary: Wrong optimized code - GCC didn't detect a variable is
                    modified
           Product: gcc
           Version: 12.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: other
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Explorer09 at gmail dot com
  Target Milestone: ---

Note: I cannot tell which component this bug belongs to. Please help me update
the "component" field for this bug report.

Tested in Compiler Explorer: https://godbolt.org/z/c6fKjMboK

## Test code

```c
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>

__attribute__((noinline)) \
uint32_t utf8_to_ascii(const uint8_t **sequence)
{
    if (**sequence <= 0x7F) {
        return *(*sequence)++;
    }
    (*sequence)++;
    return (uint32_t)-1;
}

int main(void)
{
    static const char str[] = { 'A', 'B', 'C', '\x00' };
    static const uint8_t str2[] = { 'A', 'B', 'C', '\x00' };
    uint32_t code_point;

    {
        const char *str_ptr;
        str_ptr = str;
        code_point = utf8_to_ascii((const uint8_t **)&str_ptr);
        printf("%p %p\n", str, str_ptr);
        printf("src: %lu byte(s) parsed\n", (unsigned long)(str_ptr - str));
    }
    {
        const uint8_t *str_ptr2;
        str_ptr2 = (const uint8_t *)str;
        code_point = utf8_to_ascii(&str_ptr2);

        const char *str_ptr;
        str_ptr = (const char *)str_ptr2;
        printf("%p %p\n", str, str_ptr);
        printf("src: %lu byte(s) parsed\n", (unsigned long)(str_ptr - str));
    }
    {
        const uint8_t *str_ptr2;
        str_ptr2 = str2;
        code_point = utf8_to_ascii(&str_ptr2);

        printf("%p %p\n", str2, str_ptr2);
        printf("src: %lu byte(s) parsed\n", (unsigned long)(str_ptr2 - str2));
    }
    return 0;
}
```

## Expected result 

All three blocks in main() should print "src: 1 byte(s) parsed".

## Actual result

x86_64 gcc 12.1 with "-O2" option would have the first block print "src: 0
byte(s) parsed". "-Os" would also produce the same error result.

"-O0" and "-O1" are fine as far as I have tested.

GCC versions 12.1 to 14.1 are those that are affected.

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

* [Bug other/115437] Wrong optimized code - GCC didn't detect a variable is modified
  2024-06-11 15:19 [Bug other/115437] New: Wrong optimized code - GCC didn't detect a variable is modified Explorer09 at gmail dot com
@ 2024-06-11 15:25 ` pinskia at gcc dot gnu.org
  2024-06-11 15:29 ` pinskia at gcc dot gnu.org
  2024-06-11 19:04 ` Explorer09 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-11 15:25 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
         Resolution|---                         |INVALID

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
There is an alias violation here.
Use -fno-strict-aliasing or as you shown use the correct pointer type
originally.

That is stores to `uint8_t *` and loads from `char *` are considered different
aliasing sets and GCC optimizes based on that.

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

* [Bug other/115437] Wrong optimized code - GCC didn't detect a variable is modified
  2024-06-11 15:19 [Bug other/115437] New: Wrong optimized code - GCC didn't detect a variable is modified Explorer09 at gmail dot com
  2024-06-11 15:25 ` [Bug other/115437] " pinskia at gcc dot gnu.org
@ 2024-06-11 15:29 ` pinskia at gcc dot gnu.org
  2024-06-11 19:04 ` Explorer09 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: pinskia at gcc dot gnu.org @ 2024-06-11 15:29 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|INVALID                     |DUPLICATE

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
.

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

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

* [Bug other/115437] Wrong optimized code - GCC didn't detect a variable is modified
  2024-06-11 15:19 [Bug other/115437] New: Wrong optimized code - GCC didn't detect a variable is modified Explorer09 at gmail dot com
  2024-06-11 15:25 ` [Bug other/115437] " pinskia at gcc dot gnu.org
  2024-06-11 15:29 ` pinskia at gcc dot gnu.org
@ 2024-06-11 19:04 ` Explorer09 at gmail dot com
  2 siblings, 0 replies; 4+ messages in thread
From: Explorer09 at gmail dot com @ 2024-06-11 19:04 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Kang-Che Sung <Explorer09 at gmail dot com> ---
Now I come to realize that the C standard doesn't say that a pointer type is
"compatible" with any other pointer type. So that "Foo **" type and "Bar **"
type are assumed to never alias. This is troubling because I saw many APIs that
would take a "pointer to pointer" argument in order to output address to a
particular object (for example, asprintf()), and it would be difficult to write
concise code while strictly conforming to the so-called strict aliasing rule.

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

end of thread, other threads:[~2024-06-11 19:04 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-06-11 15:19 [Bug other/115437] New: Wrong optimized code - GCC didn't detect a variable is modified Explorer09 at gmail dot com
2024-06-11 15:25 ` [Bug other/115437] " pinskia at gcc dot gnu.org
2024-06-11 15:29 ` pinskia at gcc dot gnu.org
2024-06-11 19:04 ` Explorer09 at gmail dot com

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