Tasks for the near term

Update July 27/11

1. Finish implementing __sync_mem_compare_exchange

      val = *expected;
      res = bool_compare_and_swap (ptr, val, desired, &return_value)
      if (!res)
         *expected = return_value;
       return res;

 __sync_mem_compare_exchange (&v, &count, count + 1, __SYNC_MEM_RELAXED,  __SYNC_MEM_RELAXED);
has code emitted like (from the .gimple file):
 count.2 = (long int) &count;
  D.2820 = (char) count.2; <<--- WTF?
  D.2821 = (long int) D.2820;
  D.2822 = (void *) D.2821;
  __sync_mem_compare_exchange_1 (&v, D.2822, D.2818, 0, 0);

2 - Change C++ wrappers to atomic_2.h to use the __sync_mem routines

libstdc++-v3/include/bits/atomic_2.h and atomic_base.h contain the current implementation of the c++ wrappers.

The memory_order_* enum for c++ should already align exactly with our __SYNC_MEM_* memory models, so they should be able to be passed directly through.

The existing implementations make calls to various __sync builtins, and emit various fences when they think they need to. MOst of these methods can be changed to simply call the underlying __sync_mem routine, complete with memory model. This is where we'll discover what was missed in the interface planning :-)

Eventually we'll have to deal with the issues that these __sync_mem routines aren't ALL lockfree/correct in all sizes (especially 16 byte values), and implement an atomic_1.h file which combines lock-free and locked algorithms, but we can punt on that for now.

3 - add x86 patterns for new __sync_mem routines. Other targets to follow

The pattern for mem_exchange has already been checked into the branch, we need to do all the rest of the operations. This is fairly simple I think since x86 doesnt need much in the way of fences. The implementation details for most of the instructions can be found here. Note this page summarizes a number of targets.

as you can see, there is little in the way of fences required, most loads and stores are simply a 'mov'. For the fetch_* routines, Im not sure if the can be implemented by issueing a 'LOCK' in front of the instrcutions or not.. (ie, instead of add mem,reg, the pattern issues lock; add mem,reg. this needs checking into. Maybe torvald or rth can answer that. If there isnt a good way to do them, let them resort to the default generic routine which maps to a compare_and_swap loop. (ie, don't implement a pattern)

One extra thing that will hopefully be smooth is that since there are no targets as yet, a minor amount of debugging of the new generic routines will have to be done to make sure patterns are actually emitted. That code hasn't really been exercised yet. Especially pay attention to the mem_flag_test_and_set, mem_flag_clear, and mem_*_fence patterns since they don't have type modifiers and go through a different codepath.

4 - Add tests for atomicity of __sync_mem routines

Tests in gcc.dg/sync_mem* check for the functionality of the routines, (ie, that sync_mem_exchange actually performs an exchange) but they do not test if that exchange is actually performed in an atomic way.

Aldy added the infrastructure to the test suites to enable gdb to single step through a function and utilize inferior function calls as a simulated second thread running to test that operations are executed atomically. There is an example testcase on the wiki for testing an artificial ATOMIC_STORE operation here.

Using that idea, we need to test all the __sync_mem routines for verify they are in fact atomic. I would recommend a different file for each word size.. ie char, short, int, long long, and __int128_t much like the gcc.dg/sync-mem-*{1-5}.c test cases. Those files contain all the necessary dejagnu preamble to only execute when a word size is supported. It is probably possible to put all the different __sync_mem functions in one file to be tested, which would be good since then we'd know at a glance whether *all* the __sync routines of a given size are correct. So that would result in 5 tests cases. btw, I expect to see most 16 byte versions (except exchange and maybe store) fail this test on x86_64. and probably most of the 8 byte ones on a 32 bit x86.

If its too much work to put them all in one test case, its fine to split them out and simply test one function per test case. Note we don't have to worry about all the different memory models or anything... simply make everything use __SYNC_MEM_SEQ_CST and forget the rest.

Try to test the max values of everything like gcc.dg/sync-mem-fetch-and-{1-5}.c does. (ie uses masks which are either all 0 or all 1.)

5 - look into parameter type checking of _``_sync_mem, and possibly all builtins

I noticed when I mis-implemented compare_exchange that for instance, it didnt matter whether I passed a reference or not in the second parameter: ie

    __sync_mem_compare_exchange (&v, &count, count + 1, _``_SYNC_MEM_RELAXED,  __SYNC_MEM_RELAXED);
and
    __sync_mem_compare_exchange (&v, count, count + 1, __SYNC_MEM_RELAXED,  __SYNC_MEM_RELAXED);

both silently passed just fine. I looked in builtins.c and discovered a lot of routines call 'validate_arglist' or 'validate_arg' to make sure things are the right type and issue errors if not. However when I tried that, everything passed just fine. upon deeper inspection, it turned out the parameters appear to come out of the front end forced into whatever type is required, like the issue in work item #1 where there is an extra cast. SO by the time the builtins are verifying their argument, they seem to HAVE to be in the right mode. I do not know if this is true for ALL builtins, but it should be easy enough to check some of the other ones in that file that are trying to verify their arguments and see if they also change types silently.

This would indicate a general problem that needs to be fixed. If the other types are being checked properly, then we need to figure out why the __sync_mem routines are screwed up, and add some appropriate verify_arg calls.