[cfe-dev] Question about stripping address-space qualifier with C++

JinGu Kang jingu at codeplay.com
Tue Jan 13 08:15:31 PST 2015


Hi Mats,

We are also working on OpenCL. We have implemented some of OpenCL 
functions using clang's builtin functions. For example,

int atomic_or(volatile int local * p, int val) { return atomic_or(p, val); }
...
int atomic_max(volatile int global * p, int val) { return atomic_max(p, 
val); }
int atomic_max(volatile uint global * p, uint val) { return 
atomic_max(p, val); }
...

template <typename P, typename T>
T atomic_or(P p, const T t) {
   return __sync_fetch_and_or(p, t);
}

template <typename P, typename T>
int atomic_max(volatile int global *p, const int t) {
   if (__is_signedof(T))
     return __sync_fetch_and_max(reinterpret_cast<volatile T *>(p), t);
   else
     return __sync_fetch_and_umax(reinterpret_cast<volatile T *>(p), t);
}

'__sync_fetch_and_or' and '__sync_fetch_and_[u]max' function's 
prototypes are as follows:
BUILTIN(__sync_fetch_and_or, "v.", "t")
...
BUILTIN(__sync_fetch_and_min, "iiD*i", "n")
BUILTIN(__sync_fetch_and_max, "iiD*i", "n")
BUILTIN(__sync_fetch_and_umin, "UiUiD*Ui", "n")
BUILTIN(__sync_fetch_and_umax, "UiUiD*Ui", "n")

If builtin functions use custom type checking like 
'__sync_fetch_and_or', it is not problem. But we should match the 
function prototype in other cases like '__sync_fetch_and_max'. So we 
need to strip address space qualifier In order to call 
'__sync_fetch_and_max' function.

I think it depends on just only our implementation so I wanted to check 
some ways to strip address space qualifier.

Thanks,
JinGu Kang

On 01/13/2015 03:24 PM, mats petersson wrote:
> What is the overall problem you are trying to solve? In general,
> adress space casts either generate no code (no problem) or will modify
> the behaviour of the code (necessary for correct operation). I don't
> see much point in fixing the former, and breaking things by "fixing"
> the latter doesn't seem like a brilliant idea either...
>
> Can you give an example of C++ code that uses address space casts, and
> that you want to remove them in?
>
> (Caveat, I work on OpenCL, and we do use address space casts quite a bit)
>
> --
> Mats
>
> On 13 January 2015 at 11:48, JinGu Kang <jingu at codeplay.com> wrote:
>> Hi all,
>>
>> I am looking for some ways to strip address-space qualifiers with C++ from
>> clang. I think we can use the 'addrspacecast' instruction to do it in IR
>> level. Before doing it in an IR pass, I would like to try to do it with
>> clang. But I can not find some ways to strip it with C++ source code from
>> clang or generate 'addrspacecast' instructions with C++ from clang. Is there
>> some C++ syntax to do it such as reinterpret_cast? If you have some ideas,
>> please let me know.
>>
>> Thanks,
>> JinGu Kang
>> _______________________________________________
>> cfe-dev mailing list
>> cfe-dev at cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-dev




More information about the cfe-dev mailing list