public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "pinskia at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org> To: gcc-bugs@gcc.gnu.org Subject: [Bug c++/103699] Reading or writing a constant unaligned value is wrongly optimized causing an incorrect result (GCC-11 and up) Date: Tue, 14 Dec 2021 01:28:50 +0000 [thread overview] Message-ID: <bug-103699-4-uklt0x4daZ@http.gcc.gnu.org/bugzilla/> (raw) In-Reply-To: <bug-103699-4@http.gcc.gnu.org/bugzilla/> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103699 --- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> --- (In reply to Petr from comment #6) > For now I have disabled unaligned load/store optimizations in my projects > when dealing with GCC 11 and upwards. As I mentioned you could just add the may_alias attribute and it will work too. You were just lucky that you only ran into this issue for GCC 11 really > I still think that GCC is wrong in this case regardless of strict aliasing. > The code in func_u32() is essentially creating a constant, and GCC 11+ is > the only compiler returning it wrong and also inconsistently between > optimization levels. No you are still violating C/C++ aliasing rules. No matter what you think. It is undefined behavior. Strict aliasing is only enabled for -O2 and above which is why it might look inconsistent. Plus since it is undefined behavior different optimizations level will always have different behavior. Again the following code: uint8_t array[16] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; writeU64be(array + 6, 0xAABBCCDDEEFF1213); return readU32be(array + 7); Which does basically: uint8_t array[16] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; *(unsigned long*)(array+6) = __builtin_bswap64(0xAABBCCDDEEFF1213); return *(unsigned*)(array+7); is undefined as you write to array+6 as an unsigned long but then read array+7 as an unsigned type. Plus aligned(1) attribute has nothing to do with the issue here: Take: static unsigned g(unsigned *a) { return *a; } unsigned f(void) { unsigned char array[16] {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15}; *(unsigned long*)(array) = __builtin_bswap64(0xAABBCCDDEEFF1213); return g((unsigned*)(array)); } ----- CUT --- Compile it with -O2 -fno-early-inlining and you will same bad results as you had got with aligned.
next prev parent reply other threads:[~2021-12-14 1:28 UTC|newest] Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top 2021-12-14 0:00 [Bug c++/103699] New: Reading or writing unaligned integers is wrongly optimized " kobalicek.petr at gmail dot com 2021-12-14 0:21 ` [Bug c++/103699] Reading or writing a constant unaligned value is wrongly optimized causing an incorrect result " pinskia at gcc dot gnu.org 2021-12-14 0:30 ` kobalicek.petr at gmail dot com 2021-12-14 0:33 ` kobalicek.petr at gmail dot com 2021-12-14 0:38 ` kobalicek.petr at gmail dot com 2021-12-14 0:42 ` pinskia at gcc dot gnu.org 2021-12-14 1:10 ` kobalicek.petr at gmail dot com 2021-12-14 1:28 ` pinskia at gcc dot gnu.org [this message] 2021-12-14 1:43 ` kobalicek.petr at gmail dot com 2021-12-14 2:05 ` pinskia at gcc dot gnu.org 2021-12-14 12:26 ` kobalicek.petr at gmail dot com 2021-12-14 12:31 ` jakub at gcc dot gnu.org 2021-12-14 12:41 ` kobalicek.petr at gmail dot com 2021-12-14 12:57 ` jakub at gcc dot gnu.org 2021-12-14 12:58 ` jakub at gcc dot gnu.org 2021-12-14 13:09 ` kobalicek.petr at gmail dot com 2021-12-14 13:24 ` pinskia at gcc dot gnu.org 2021-12-14 13:28 ` kobalicek.petr at gmail dot com 2021-12-14 13:34 ` jakub at gcc dot gnu.org
Reply instructions: You may reply publicly to this message via plain-text email using any one of the following methods: * Save the following mbox file, import it into your mail client, and reply-to-all from there: mbox Avoid top-posting and favor interleaved quoting: https://en.wikipedia.org/wiki/Posting_style#Interleaved_style * Reply using the --to, --cc, and --in-reply-to switches of git-send-email(1): git send-email \ --in-reply-to=bug-103699-4-uklt0x4daZ@http.gcc.gnu.org/bugzilla/ \ --to=gcc-bugzilla@gcc.gnu.org \ --cc=gcc-bugs@gcc.gnu.org \ /path/to/YOUR_REPLY https://kernel.org/pub/software/scm/git/docs/git-send-email.html * If your mail client supports setting the In-Reply-To header via mailto: links, try the mailto: linkBe sure your reply has a Subject: header at the top and a blank line before the message body.
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).