public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "x at maxxsoft dot net" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug target/114130] New: RISC-V: `__atomic_compare_exchange` does not use sign-extended value
Date: Tue, 27 Feb 2024 13:42:30 +0000	[thread overview]
Message-ID: <bug-114130-4@http.gcc.gnu.org/bugzilla/> (raw)

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

            Bug ID: 114130
           Summary: RISC-V: `__atomic_compare_exchange` does not use
                    sign-extended value
           Product: gcc
           Version: 13.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: x at maxxsoft dot net
  Target Milestone: ---

GCC 13.2 does not generate sign-extension for value to be compared with the
result of `lr.w` instruction in `__atomic_compare_exchange`:
https://godbolt.org/z/nafKhPa1Y

Code:

```
void foo(uint32_t *p) {
    uintptr_t x = *(uintptr_t *)p;
    uint32_t e = !p ? 0 : (uintptr_t)p >> 1;
    uint32_t d = (uintptr_t)x;
    __atomic_compare_exchange(p, &e, &d, 0, __ATOMIC_RELAXED,
__ATOMIC_RELAXED);
}
```

Assembly generated by `gcc -O3`:

```
foo:
        ld      a4,0(a0)
        srli    a5,a0,1
         1: lr.w a3,0(a0); bne a3,a5,1f; sc.w a2,a4,0(a0); bnez a2,1b; 1:
        ret
```

Which `a5` should be sign-extended, since the RISC-V ISA manual says `lr.w`
returns a sign-extended value in RV64.

But `gcc -O3 -fno-delete-null-pointer-checks` generates correct code:

```
foo:
        ld      a4,0(a0)
        li      a5,0
        beq     a0,zero,.L2
        srli    a5,a0,1
        sext.w  a5,a5
.L2:
         1: lr.w a3,0(a0); bne a3,a5,1f; sc.w a2,a4,0(a0); bnez a2,1b; 1:
        ret
```

`gcc -O3 -fno-tree-ter`'s output is slight different, but also sign-extended.

`clang -O3` always generates correct code:

```
foo:                                    # @foo
        lw      a1, 0(a0)
        srli    a2, a0, 1
        sext.w  a2, a2
.LBB0_1:                                # =>This Inner Loop Header: Depth=1
        lr.w    a3, (a0)
        bne     a3, a2, .LBB0_3
        sc.w    a4, a1, (a0)
        bnez    a4, .LBB0_1
.LBB0_3:
        ret
```

             reply	other threads:[~2024-02-27 13:42 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-27 13:42 x at maxxsoft dot net [this message]
2024-02-28  5:14 ` [Bug target/114130] " pinskia at gcc dot gnu.org
2024-02-28  8:01 ` [Bug target/114130] [11/12/13/14 Regression] RISC-V: `__atomic_compare_exchange` does not use sign-extended value for RV64 rguenth at gcc dot gnu.org
2024-02-29  3:05 ` cvs-commit at gcc dot gnu.org
2024-04-11  1:21 ` [Bug target/114130] [11/12/13 " cvs-commit at gcc dot gnu.org
2024-04-11  6:52 ` cvs-commit at gcc dot gnu.org
2024-04-12  8:06 ` [Bug target/114130] [11 " cvs-commit at gcc dot gnu.org
2024-04-12  8:06 ` kito 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-114130-4@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: link
Be 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).