public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/103069] New: cmpxchg isn't optimized
@ 2021-11-03 19:08 hjl.tools at gmail dot com
  2021-11-03 20:53 ` [Bug target/103069] " thiago at kde dot org
                   ` (22 more replies)
  0 siblings, 23 replies; 24+ messages in thread
From: hjl.tools at gmail dot com @ 2021-11-03 19:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 103069
           Summary: cmpxchg isn't optimized
           Product: gcc
           Version: 12.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: hjl.tools at gmail dot com
                CC: crazylht at gmail dot com, wwwhhhyyy333 at gmail dot com
            Blocks: 103065
  Target Milestone: ---
            Target: i386,x86-64

>From the CPU's point of view, getting a cache line for writing is more
expensive than reading.  See Appendix A.2 Spinlock in:

https://www.intel.com/content/dam/www/public/us/en/documents/white-papers/xeon-lock-scaling-analysis-paper.pdf

The full compare and swap will grab the cache line exclusive and causes
excessive cache line bouncing.

[hjl@gnu-cfl-2 pr102566]$ cat e.c 
int
f3 (int *a)
{
  return __atomic_fetch_or (a, 0x40000000, __ATOMIC_RELAXED);
}
[hjl@gnu-cfl-2 pr102566]$ gcc -S -O2 x.c 
[hjl@gnu-cfl-2 pr102566]$ cat x.s 
        .file   "x.c"
        .text
        .p2align 4
        .globl  foo
        .type   foo, @function
foo:
.LFB0:
        .cfi_startproc
        movl    v(%rip), %eax
.L2:
        movl    %eax, %ecx
        movl    %eax, %edx
        orl     $1, %ecx
        lock cmpxchgl   %ecx, v(%rip)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
GCC should first emit a normal load, check and jump to .L2 if cmpxchgl
may fail.  Before jump to .L2, PAUSE should be inserted to to yield the
CPU to another hyperthread and to save power. It also serves to slightly
limit the rate of accesses on the processor interconnect.
        jne     .L2
        movl    %edx, %eax
        andl    $1, %eax
        ret
        .cfi_endproc
.LFE0:
        .size   foo, .-foo
        .ident  "GCC: (GNU) 11.2.1 20211019 (Red Hat 11.2.1-6)"
        .section        .note.GNU-stack,"",@progbits
[hjl@gnu-cfl-2 pr102566]$


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=103065
[Bug 103065] [meta] atomic operations aren't optimized

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

end of thread, other threads:[~2023-05-08 12:23 UTC | newest]

Thread overview: 24+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-03 19:08 [Bug target/103069] New: cmpxchg isn't optimized hjl.tools at gmail dot com
2021-11-03 20:53 ` [Bug target/103069] " thiago at kde dot org
2021-11-04 21:25 ` thiago at kde dot org
2021-11-15 11:10 ` cvs-commit at gcc dot gnu.org
2021-11-15 14:26 ` hjl.tools at gmail dot com
2021-11-18  8:31 ` cvs-commit at gcc dot gnu.org
2022-01-24 23:49 ` hjl.tools at gmail dot com
2022-01-24 23:52 ` hjl.tools at gmail dot com
2022-01-24 23:53 ` hjl.tools at gmail dot com
2022-01-24 23:55 ` hjl.tools at gmail dot com
2022-01-25  0:04 ` thiago at kde dot org
2022-02-15  8:59 ` wwwhhhyyy333 at gmail dot com
2022-02-22  3:36 ` cvs-commit at gcc dot gnu.org
2022-02-22  3:38 ` wwwhhhyyy333 at gmail dot com
2022-02-22  4:16 ` thiago at kde dot org
2022-02-22  8:21 ` wwwhhhyyy333 at gmail dot com
2022-02-22 18:05 ` thiago at kde dot org
2022-02-22 18:41 ` jakub at gcc dot gnu.org
2022-02-22 20:25 ` thiago at kde dot org
2022-02-23  3:35 ` wwwhhhyyy333 at gmail dot com
2022-02-23  4:06 ` thiago at kde dot org
2022-04-13  8:18 ` cvs-commit at gcc dot gnu.org
2022-05-06  8:31 ` jakub at gcc dot gnu.org
2023-05-08 12:23 ` 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).