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.
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
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.
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.
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.
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:
__restrict
used__restrict
using assert
, since passing aliasing pointers to a function expecting them to be nonaliasing can lead to hard to track down bugs.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With