GCCに__declspec(Noalias)をポートする方法
-
10-12-2019 - |
質問
正しく入手した場合、__declspec(noalias)
は、パラメータとして渡されたポインタがエイリアスされていないことをコンパイラに指示します。
__declspec(noalias) void multiply(float * a, float * b, float * c)
{
...
}
.
言ったように、私が間違っていない場合は、ポインタタイプのすべてのパラメータで__restrict
を呼び出すこととは正確に同じです。しかし、すべての関数呼び出しを変更せずにそれをする方法はありますか?理想的には、その__declspec(noalias)
をプリプロセッサ定義で置き換えます。
解決
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.
他のヒント
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.