Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__restrict pointer aliasing with only one pointer

Is there any advantage to specifying the MSVC/GCC non-standard __restrict qualifier on a function pointer parameter if it is the only pointer parameter? For example,

int longCalculation(int a, int* __restrict b)

My guess is it should allow better optimization since it implies b does not point to a, but all examples I've seen __restrict two pointers to indicate no aliasing between them.

like image 880
jarmond Avatar asked Aug 07 '12 18:08

jarmond


People also ask

What is pointer aliasing in C++?

Pointer aliasing in C++ inhibits vectorization, other optimizations, and, hence, performance. Read how to help the compiler work smarter. Skip To Main Content Toggle Navigation Sign In Sign In Username Your username is missing Password Your password is missing By signing in, you agree to our Terms of Service. Remember me

What is the difference between pointed-at and aliased pointer types?

The pointed-at types are different, but the pointed-at type through which the access is made is a pointer to character: unsigned char a1 = p[0]; // First byte of 'f'. unsigned char a4 = p[3]; // Last byte of 'f'. Conversely, aliased pointer access is not defined if the pointed-at types are fundamentally different.

What is the strict aliasing rule?

They introduced rules that state when pointer aliasing must not happen. Enter the strict aliasing rule. To facilitate compiler optimization, the strict aliasing rule demands that (in simple words) pointers to incompatible types never alias.

Why do we use strict aliasing in GCC?

This is done because they referred to the same memory location. GCC compiler makes an assumption that pointers of different types will never point to the same memory location i.e., alias of each other. Strict aliasing rule helps the compiler to optimize the code.


1 Answers

As mentioned in the comments b can't point to a anyways, so there is no aliasing potential there anyways. So if the function is pure in the sense that it works only on its parameters there shouldn't be any real benefits.

However if the function uses global variables internally then __restrict might offer benefits once again, since it makes clear that b doesn't point to any of those global variables.

An interesting case might be the situation where you allocate and deallocate memory inside the function. The compiler could theoretically be sure that b doesn't point to that memory, however whether or not it realizes that I'm not sure and might depend how the allocation is called.

Personally however I prefer to keep __restrict out of the signature and do something like this

int longCalculation(int a, int* b){ 
   assert(...);//ensure that b doesn't point to anything used
   int* __restrict bx = b;
   ...
}

IMO this has the following advantages:

  • The function signature doesn't expose the non standard __restrict used
  • The ability to ensure that the variables actually conform to __restrict using assert, since passing aliasing pointers to a function expecting them to be nonaliasing can lead to hard to track down bugs.
like image 68
Grizzly Avatar answered Sep 28 '22 10:09

Grizzly