Bug 107713 - Wrong implementation atomic_exchange<short> on LoongArch
Summary: Wrong implementation atomic_exchange<short> on LoongArch
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: target (show other bugs)
Version: 13.0
: P3 normal
Target Milestone: 12.3
Assignee: Not yet assigned to anyone
URL: https://gcc.gnu.org/pipermail/gcc-pat...
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2022-11-16 03:07 UTC by Jinyang He
Modified: 2022-11-20 03:56 UTC (History)
5 users (show)

See Also:
Host:
Target: loongarch64-linux-gnu
Build:
Known to work:
Known to fail: 12.2.0, 13.0
Last reconfirmed: 2022-11-16 00:00:00


Attachments
preprocessed file (151 bytes, text/plain)
2022-11-16 03:07 UTC, Jinyang He
Details
a simple test (691 bytes, text/x-csrc)
2022-11-16 07:14 UTC, Jinyang He
Details
testcase (85 bytes, text/plain)
2022-11-16 11:31 UTC, Xi Ruoyao
Details
testcase (287 bytes, text/x-c++src)
2022-11-16 11:48 UTC, Xi Ruoyao
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Jinyang He 2022-11-16 03:07:34 UTC
Created attachment 53905 [details]
preprocessed file

Target: loongarch64-unknown-linux-gnu
Configured with: ../configure --prefix=/usr --libdir=/usr/lib64 --build=x86_64-cross-linux-gnu --host=loongarch64-unknown-linux-gnu --target=loongarch64-unknown-linux-gnu --enable-__cxa_atexit --enable-threads=posix --with-system-zlib --enable-libstdcxx-time --enable-checking=release --with-build-sysroot=/opt/mylaos/sysroot --enable-default-pie --enable-languages=c,c++,fortran,objc,obj-c++,lto
gcc version 13.0.0 20220801 (experimental) (GCC) 

test_of_sync_lock_test_and_set.c:
char lock;
char hello()
{
  return __sync_lock_test_and_set (&lock, 1);
}

Cmdline:
gcc test_of_sync_lock_test_and_set.c -S --save-temps

Output Fragment:
        1:
        ll.w    $r12,$r16,0
        and     $r19,$r12,$r14
        bne     $r19,$r15,2f          <--- Wrong
        and     $r19,$r12,$r17
        or      $r19,$r19,$r18
        sc.w    $r19,$r16,0
        beq     $zero,$r19,1b
        b       3f
        2:
        dbar    0x700
        3:
Comment 1 Jinyang He 2022-11-16 07:14:46 UTC
Created attachment 53906 [details]
a simple test
Comment 2 Xi Ruoyao 2022-11-16 11:27:20 UTC
Confirmed.
Comment 3 Xi Ruoyao 2022-11-16 11:31:43 UTC
Created attachment 53907 [details]
testcase

A more straightforward test (in C++).
Comment 4 Xi Ruoyao 2022-11-16 11:48:07 UTC
Created attachment 53908 [details]
testcase

Ouch, uploaded a wrong file.
Comment 5 GCC Commits 2022-11-18 07:05:20 UTC
The master branch has been updated by LuluCheng <chenglulu@gcc.gnu.org>:

https://gcc.gnu.org/g:f0024bfb228f94e60e06dc32a4983e40a9b90be5

commit r13-4136-gf0024bfb228f94e60e06dc32a4983e40a9b90be5
Author: Jinyang He <hejinyang@loongson.cn>
Date:   Thu Nov 17 14:38:52 2022 +0800

    LoongArch: Fix atomic_exchange expanding [PR107713]
    
    We used to expand atomic_exchange_n(ptr, new, mem_order) for subword types
    into something like:
    
        {
          __typeof__(*ptr) t = atomic_load_n(ptr, mem_order);
          atomic_compare_exchange_n(ptr, &t, new, true, mem_order, mem_order);
          return t;
        }
    
    It's incorrect because another thread may store a different value into *ptr
    after atomic_load_n.  Then atomic_compare_exchange_n will not store into
    *ptr, but atomic_exchange_n should always perform the store.
    
    gcc/ChangeLog:
    
            PR target/107713
            * config/loongarch/sync.md
            (atomic_cas_value_exchange_7_<mode>): New define_insn.
            (atomic_exchange): Use atomic_cas_value_exchange_7_si instead of
            atomic_cas_value_cmp_and_7_si.
    
    gcc/testsuite/ChangeLog:
    
            PR target/107713
            * gcc.target/loongarch/pr107713-1.c: New test.
            * gcc.target/loongarch/pr107713-2.c: New test.
Comment 6 Xi Ruoyao 2022-11-18 09:01:29 UTC
Fixed for trunk.  Should we backport it to gcc-12 branch too?
Comment 7 chenglulu 2022-11-18 09:13:43 UTC
(In reply to Xi Ruoyao from comment #6)
> Fixed for trunk.  Should we backport it to gcc-12 branch too?

I don't know what the problem is, I always fail when I backport.
If it is convenient for you, could you help me backport?
Comment 8 GCC Commits 2022-11-19 12:37:41 UTC
The releases/gcc-12 branch has been updated by Xi Ruoyao <xry111@gcc.gnu.org>:

https://gcc.gnu.org/g:2adcbcc69a1d5d9554042f09ec35e72bf39fb56f

commit r12-8918-g2adcbcc69a1d5d9554042f09ec35e72bf39fb56f
Author: Jinyang He <hejinyang@loongson.cn>
Date:   Thu Nov 17 14:38:52 2022 +0800

    LoongArch: Fix atomic_exchange expanding [PR107713]
    
    We used to expand atomic_exchange_n(ptr, new, mem_order) for subword types
    into something like:
    
        {
          __typeof__(*ptr) t = atomic_load_n(ptr, mem_order);
          atomic_compare_exchange_n(ptr, &t, new, true, mem_order, mem_order);
          return t;
        }
    
    It's incorrect because another thread may store a different value into *ptr
    after atomic_load_n.  Then atomic_compare_exchange_n will not store into
    *ptr, but atomic_exchange_n should always perform the store.
    
    gcc/ChangeLog:
    
            PR target/107713
            * config/loongarch/sync.md
            (atomic_cas_value_exchange_7_<mode>): New define_insn.
            (atomic_exchange): Use atomic_cas_value_exchange_7_si instead of
            atomic_cas_value_cmp_and_7_si.
    
    gcc/testsuite/ChangeLog:
    
            PR target/107713
            * gcc.target/loongarch/pr107713-1.c: New test.
            * gcc.target/loongarch/pr107713-2.c: New test.
    
    (cherry picked from commit f0024bfb228f94e60e06dc32a4983e40a9b90be5)
Comment 9 Xi Ruoyao 2022-11-19 12:38:27 UTC
Fixed for gcc-12 too.
Comment 10 chenglulu 2022-11-20 03:56:37 UTC
(In reply to Xi Ruoyao from comment #9)
> Fixed for gcc-12 too.

Thanks! ^v^