Tasks for the near term

Updated Aug 18/2011

1. Integrate compare_and_swap(CAS) as a tree code... maybe.

2 - Create a builtin which indicates which __sync_mem routines support true atomicity.

__sync routines exist for up to 16 bytes values, but their execution may not actually be atomic. There is a testsuite to verify which size objects are truly atomic in gcc.dg/memmodel/sync*.

A builtin function needs to be provided, along with some required configure bits such that the compiler can indicate if a type or typesize is properly supported or not. ie, something like __sync_is_lock_free_atomic (typesize) which returns a true or false based on what the target supports. I suspect this information will have to be manually set up in one of the configure files somehow.

3 - Create new C++ wrappers in atomic_1.h for hybrid locked/lock-free atomics.

Utilizing the __sync_lock_free builtin, the C++ compiler will be able to detect whether there is lock-free support for a given typesize.

Based on that, it can determine whether to utilize the lock-free templates in atomic_2.h or the locked versions in atomic_1.h. This will require some other fudging around, but it should be possible generate a full proper compilation for all atomic types, using the lock free routines for any types which match the supported integral types.

bkoz would be the primary reference here if Highlander is not around.

4 - 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.

rth is the primary contact here if Highlander is not around.

5 - look into parameter type checking of __sync_mem

The parameters are massaged in c-family/c-common.c::resolve_overloaded_builtin, sync_resolve_return, and sync_resolve_params.

Previous to this, all the __sync were of the format T __Sync (T *, T, T, ...) so these 3 routines figured out what type T was and cast the return and all parameters to T and then to the type of the builtins's argument, usually BT_I{1,2,4,8,16}, which is an unsigned value. ie if it the builtin type was 1 byte, BT_I1, we'd see something like (BT_I1)(char)actual_param where BT_I1 maps to type_for_size(1), which I think is unsigned char.

The new __sync_mem routines are similar, but the memory model parameter is a BT_INT. This means that currently the memory model parameter on a call for 16 bytes values would be cast to an __int128_t, and then back to an int. Which is quite ugly and silly.

The right thing would be to change these routines to look at all the arguments and only do these casts when the underlying type of the builtin argument is not a pointer and the same size as the real base (T) (picked up from the pointer in parameter 1). Since the memory_model argument is a BT_INT, we'll only get the cast on the memory model parameter when it's size matches T (either long or int).. and then its a harmless cast.

Extra parameters can be thrown on the end as well and no errors are reported, this dates back to the variadic versions the IA64 stuff required. the new routines should complain if there are extra parameters, or they dont match the appropriate types.

The reason for all this casting is to prevent a bunch of compiler warnings when passing in pointers for compare and swap and to avoid signed/unsigned conversions issues which may cause miscompilations.

rth knows all about this code, so questions can go to him if Highlander isnt around.