Do nvcc, gcc, clang and msvc “respect” the __restr

2019-06-11 10:23发布

Suppose I have

struct s {
    int* __restrict__ p1;
    double v;
};

void foo(int* __restrict__ p2, struct s my_s) { /* ... */ }

Do the C++ compilers listed below respect the __restrict__ keywords in this case, and assume memory accesses through p2 cannot affect accesses through p1? Obviously this is compiler-dependent, since restrict is not a C++ keyword.

I'm mainly interested in the answer for gcc 4.9.x and nVIDIA CUDA 7.5's nvcc (when compiling device code of course, not when forwarding to a host compiler). An answer regarding current versions of clang, gcc and msvc++ would also be interesting.

2条回答
可以哭但决不认输i
2楼-- · 2019-06-11 11:02

GCC seems to indicate yes, but I would imagine under the hood it's being all brainy about these types of things and may completely ignore the fact that the keyword is present.

I'd also be willing to bet that if you profile your method with and without the restrict keywords, there'd be little to no difference.

If you do this I'd love to know the results.

There's also this answer which may be interesting to read.

Lastly, there's this blog which seems to indicate the nvcc supports the keyword.

Now I'm really curious about the results of profiling your code.

查看更多
甜甜的少女心
3楼-- · 2019-06-11 11:06

Microsoft C++ AMP (with MSVC++ 2015)

__restrict__ on pointers which are struct members is officially unsupported (emphasis mine):

The following are not allowed:

  • Recursion.
  • Variables declared with the volatile keyword.
  • Virtual functions.
  • Pointers to functions.
  • Pointers to member functions.
  • Pointers in structures.
  • Pointers to pointers.
  • goto statements.
  • Labeled statements.
  • try, catch, or throw statements.
  • Global variables.
  • Static variables. Use tile_static Keyword instead.
  • dynamic_cast casts.
  • The typeid operator.
  • asm declarations.
  • Varargs.

Microsoft Visual C++ 2015

In general rather than AMP C++ code, it's much less clear what MSVC supports. We see an example of __restrict__ being used in a union declaration - so not just a modified on a paremeter of a function - but the semantics are different that you would have for use in a struct.

查看更多
登录 后发表回答