(gcc/)g++ and __restrict

Tobias Burnus burnus@net-b.de
Sun Sep 22 22:42:00 GMT 2013

Am 20.09.2013 16:59, schrieb Ian Lance Taylor:
> On Fri, Sep 20, 2013 at 3:07 AM, Tobias Burnus
> <tobias.burnus@physik.fu-berlin.de> wrote:
>> I was wondering how to convey to GCC that two pointers do not alias.
>> That works nicely in the argument list,
>>    void foo(mytype *__restrict__ arg)
>> However, it does not seem to work for C++'s member variables. Even
>> if one has on the class the declaration with __restrict. Neither does
>> casting to a restrict pointer work.
> I'm not sure how that could work.  By definition the restrict
> qualifier is only meaningful in a specific context, with respect to
> other pointers with the restrict qualifier.  It would not make sense
> to declare that every restrict qualified pointer in the entire program
> may not conflict with every other restrict qualified pointer in the
> entire program.  That would be too limiting.

I admit that placing it on a C++ member variable is not the best place. 
However, being desperate, I thought I'd try it - and GCC accepted it. 
(As did Intel's icpc.)

> Another possibly more profitable approach would be to define a new
> function attribute.  In functions with that attribute we would declare
> that none of the pointers that were not based on each other could
> alias.  This would be difficult to use correctly but it would be
> fairly precise and might be sufficient for what you need.

Actually, I would find some casting approach best, namely:
   ptr = (__typeof(ptr) *__restrict) ptr;
or something like that. That would allow to put the restrict qualifier 
exactly on the pointer one wants to mark as such. I think one can 
already use such a cast for "volatile". Similarly, one can use ptr = 
__builtin_assume_aligned(ptr, size), which is also an assignment way to 
add an attribute to a pointer.

However, putting an attribute on the function itself would also work for me.


More information about the Gcc-help mailing list