[cfe-dev] atomic intrinsics

Eric Christopher echristo at apple.com
Tue Oct 5 17:16:18 PDT 2010


On Oct 5, 2010, at 3:56 PM, Eric Christopher wrote:

> 
> On Oct 5, 2010, at 9:50 AM, Howard Hinnant wrote:
> 
>> I'm still working on <atomic>, as described below.  But I paused my development to write a "Compiler writer's guide to <atomic>" which I've placed here:
>> 
>> http://libcxx.llvm.org/atomic_design.html
>> 
>> This details exactly what intrinsics must appear, and in what form, and which are optional.  This document also describes how the library deals with optional intrinsics which are not supplied.  In a nutshell, the library calls the best intrinsic the compiler supplies, and if none are, locks a mutex to do the job.
>> 
>> Comments welcome.  Is this a design that the clang community can rally around?

How about something like this:

__atomic_load_seq_cst(__obj)

that is processed by the front end into the llvm IR intrinsic (unless it has special knowledge) and then either emitted by the backend as a call to that function or inlined as atomic code if the backend knows how to do that.  That way the compiler can make the choice, but we also don't get people using the "intrinsics" in a non-portable way thinking that it's the actual api, and just use the API - and the front end knows what to do.

This would then get us instead of:

// load

template <class _Tp>
_Tp
__load_seq_cst(_Tp const volatile* __obj)
{
   unique_lock<mutex> _(__not_atomic_mut());
   return *__obj;
}

// load bool

inline _LIBCPP_INLINE_VISIBILITY
bool
__choose_load_seq_cst(bool const volatile* __obj)
{
#if __has_feature(__atomic_load_seq_cst_b)
   return __atomic_load_seq_cst(__obj);
#else
   return __load_seq_cst(__obj);
#endif
}

Just a library call of __atomic_load_seq_cst that the backend will call if it doesn't know how to emit and so you only have to write:

// load

template <class _Tp>
_Tp
__atomic_load_seq_cst(_Tp const volatile* __obj)
{
   unique_lock<mutex> _(__not_atomic_mut());
   return *__obj;
}

and the front end would process it based on name and type depending on what it can do.  The backend can then implement 0, 1, N, or All of the intrinsics that can be lowered to target code.

You were mentioning a bit more to this in private mail, I'll let you summarize that here :)

-eric



More information about the cfe-dev mailing list