Search code examples
cvisual-c++gccaliasrestrict-qualifier

How to port __declspec(noalias) on GCC


If I get it correctly, __declspec(noalias) tells the compiler that none of the pointers passed as parameters is aliased.

__declspec(noalias) void multiply(float * a, float * b, float * c)
{
    ...
}

Said differently, if I’m not mistaken , it’s exactly equivalent to calling __restrict on every parameter of pointer type. But is there a way to do it without changing all the function calls? Ideally, I would replace that __declspec(noalias) with a preprocessor definition.


Solution

  • I think you're interpreting noalias incorrectly; it is not the same as specifying __restrict on each parameter. In the example you reference from MSDN, it means that a, b, and c don't modify or reference any global state (of the current compilation unit), but they are free to alias one another. You could also specify __restrict on each one to indicate that they do not alias each other. I'm not sure why you mention changing all the function calls in this scenario; as long as no caller aliases the arguments, nothing changes at the call site. You should review all the calls, but they needn't change unless you need to remove aliasing. Specifically, __restrict is not needed at the call site.

    The only analogue in GCC would be to specify __restrict (or more commonly for GCC, __restrict__) on any global pointer variable declarations in the same source file (which are of compatible types). See here about file-scope restrict-qualified pointers. Of course, there's no guarantee that GCC will behave any differently, since restrict is often considered only at function scope. Microsoft clearly introduced noalias to enable a specific optimization they introduced.

    Long story short, there's no macro trick here (aside from the one mentioned by R to ignore __declspec() entirely on GCC). The best you can do is add __restrict to any non-aliased pointer parameter and global variable declarations.