public inbox for gcc-bugs@sourceware.org help / color / mirror / Atom feed
From: "kobalicek.petr at gmail dot com" <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:43:33 +0000 [thread overview] Message-ID: <bug-103699-4-5AEVeSIqMN@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 #8 from Petr <kobalicek.petr at gmail dot com> --- My only problem is that A returns a different value compared to B, C, and D: uint32_t test_u32_a() { char 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); } uint32_t test_u32_b() { static char 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); } uint32_t test_u32_c() { thread_local char 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); } uint32_t test_u32_d(char array[16]) { writeU64be(array + 6, 0xAABBCCDDEEFF1213); return readU32be(array + 7); } And when you compile this, you would actually see that ALL functions evaluate to a constant (because it's known what the output will be), but only in A case the constant is different (of course because B, C, D have side effects): test_u32_a(): mov eax, 117967114 ret test_u32_b(): movabs rax, 1374442237689904042 mov QWORD PTR test_u32_b()::array[rip+6], rax mov eax, -1144201746 ret test_u32_c(): movabs rax, 1374442237689904042 mov QWORD PTR fs:test_u32_c()::array@tpoff+6, rax mov eax, -1144201746 ret test_u32_d(char*): movabs rax, 1374442237689904042 mov QWORD PTR [rdi+6], rax mov eax, -1144201746 ret So yeah, we can talk about breaking strict aliasing here, but it's just inconsistent. I would just expect all functions return the same value.
next prev parent reply other threads:[~2021-12-14 1:43 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 2021-12-14 1:43 ` kobalicek.petr at gmail dot com [this message] 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-5AEVeSIqMN@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).