public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: Jakub Jelinek <jakub@redhat.com>
Cc: gcc-patches@gcc.gnu.org
Subject: Re: [PATCH][libatomic] Fix return value in libat_test_and_set
Date: Thu, 24 Mar 2022 11:01:30 +0100	[thread overview]
Message-ID: <8228eeda-83a4-a5c7-7100-803d7d741de2@suse.de> (raw)
In-Reply-To: <YjwzmYCbsRQmNjP7@tucnak>

On 3/24/22 10:02, Jakub Jelinek wrote:
> On Thu, Mar 24, 2022 at 09:28:15AM +0100, Tom de Vries via Gcc-patches wrote:
>> Hi,
>>
>> On nvptx (using a Quadro K2000 with driver 470.103.01) I ran into this:
>> ...
>> FAIL: gcc.dg/atomic/stdatomic-flag-2.c -O1 execution test
>> ...
>> which mimimized to:
>> ...
>>    #include <stdatomic.h>
>>    atomic_flag a = ATOMIC_FLAG_INIT;
>>    int main () {
>>      if ((atomic_flag_test_and_set) (&a))
>>        __builtin_abort ();
>>      return 0;
>>    }
>> ...
>>
>> The atomic_flag_test_and_set is implemented using __atomic_test_and_set_1,
>> which corresponds to the "word-sized compare-and-swap loop" version of
>> libat_test_and_set in libatomic/tas_n.c.
>>
>> The semantics of a test-and-set is that the return value is "true if and only
>> if the previous contents were 'set'".
>>
>> But the code uses:
>> ...
>>    return woldval != 0;
>> ...
>> which means it doesn't look only at the byte that was either set or not set,
>> but at the entire word.
>>
>> Fix this by using instead:
>> ...
>>    return (woldval & wval) == wval;
> 
> Shouldn't that be instead
>    return (woldval & ((UWORD) -1 << shift)) != 0;
> or
>    return (woldval & ((UWORD) ~(UWORD) 0 << shift)) != 0;
> ?

Well, I used '(woldval & wval) == wval' based on the fact that the set 
operation uses a bitor:
...
   wval = (UWORD)__GCC_ATOMIC_TEST_AND_SET_TRUEVAL << shift;
   woldval = __atomic_load_n (wptr, __ATOMIC_RELAXED);
   do
     {
       t = woldval | wval;
...
so apparently we do not care here about bits not in 
__GCC_ATOMIC_TEST_AND_SET_TRUEVAL (or alternatively, we care but assume 
that they're 0).

AFAIU, it would have been more precise to compare the entire byte with 
__GCC_ATOMIC_TEST_AND_SET_TRUEVAL, but then it would have made sense to 
set the entire byte in the set part as well.

Anyway, that doesn't seem to be what you're proposing.  During 
investigation of the failure I found that the address used is 
word-aligned, so shift becomes 0 in that case.  AFAICT, the fix you're 
proposing is a nop for shift == 0, and indeed, it doesn't fix the 
failure I'm observing.

> The exact __GCC_ATOMIC_TEST_AND_SET_TRUEVAL varies (the most usual
> value is 1, but sparc uses 0xff and m68k/sh use 0x80), falseval is
> always 0 though and (woldval & wval) == wval
> is testing whether some bits of the oldval are all set rather than
> whether the old byte was 0.
> Say for trueval 1 it tests whether the least significant bit is set,
> for 0x80 if the most significant bit of the byte is set, for
> 0xff whether all bits are set.

Yes, I noticed that.

AFAIU, the proposed patch ddrt under the assumption that we don't care 
about bits not set in __GCC_ATOMIC_TEST_AND_SET_TRUEVAL.

If that's not acceptable, I can submit a patch that doesn't have that 
assumption, and tests the entire byte (but should I also fix the set 
operation then?).

Thanks,
- Tom



  reply	other threads:[~2022-03-24 10:01 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-24  8:28 Tom de Vries
2022-03-24  9:02 ` Jakub Jelinek
2022-03-24 10:01   ` Tom de Vries [this message]
2022-03-24 10:59     ` Jakub Jelinek
2022-03-24 12:08       ` Tom de Vries
2022-03-24 12:25         ` Jakub Jelinek

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=8228eeda-83a4-a5c7-7100-803d7d741de2@suse.de \
    --to=tdevries@suse.de \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jakub@redhat.com \
    /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).