如何在GCC上陷入_Declspec(Noalias)
-
10-12-2019 - |
题
如果我得到它正确,__declspec(noalias)
会告诉编译器与参数中没有传递的指针都是别名。
__declspec(noalias) void multiply(float * a, float * b, float * c)
{
...
}
.
不同地说,如果我没有弄错的话,它完全等同于指针类型的每个参数上的调用生成的__restrict
。但是有没有办法在不改变所有函数调用的情况下做到这一点?理想情况下,我将用预处理器定义替换该生成的icetagcode。
解决方案
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.