Come portare __declspec (Noalias) su GCC
-
10-12-2019 - |
Domanda
Se lo prendo correttamente, __declspec(noalias)
dice al compilatore che nessuno dei puntatori passati come parametri è alias.
__declspec(noalias) void multiply(float * a, float * b, float * c)
{
...
}
.
disse in modo diverso, se non sono sbagliato, è esattamente equivalente a chiamare __restrict
su ogni parametro del tipo di puntatore.Ma c'è un modo per farlo senza cambiare tutte le chiamate di funzione?Idealmente, sostituivo quel __declspec(noalias)
con una definizione di preprocessore.
Soluzione
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.
Altri suggerimenti
Just #define __declspec(x)
(to a blank definition). Omitting noalias/restrict will not make any change to the behavior of a correct program. All it does is create additional (usually very minor, with current compiler technology) opportunities for the compiler to optimize.