Should I use __restrict on references?
Asked Answered
M

3

9

In the program I am coding, one of my function declarations goes like this:

bool parse( const sentence & __restrict sentence )
{
  // whatever
}

When I compile the code with Microsoft Visual Studio 2010 Express, the compiler complains:

warning C4227: anachronism used : qualifiers on reference are ignored

However, this page of GCC’s documentation says:

In addition to allowing restricted pointers, you can specify restricted references, which indicate that the reference is not aliased in the local context.

And the same page gives a very explicit example:

 void fn (int *__restrict__ rptr, int &__restrict__ rref)
 {
   /* ... */
 }

Did I misunderstand MVSC’s warning? or should I transform all my references into pointers so that __restrict applies?

Mooch answered 11/10, 2012 at 13:56 Comment(1)
What's the point of comparing MSVC and GCC here? __restrict is a vendor extension, so you have to play by each vendor's rules.Indonesian
I
10

C++ has no notion of restrict in the way C99 does.

However, several compiler vendors offer extensions to their C++ compilers, which they call __restrict (note the reserved name!). Given that those are extensions, their behaviour is determined by the com­pi­ler vendor. You will have to read the documentation and find out what this extension does in each com­pi­ler separately.

Just because two vendors chose the same name doesn't mean the extensions have anything in common.

Indonesian answered 11/10, 2012 at 14:20 Comment(2)
Can you give an example of where two extensions have the same name but different meaning for different compilers?Panchito
@RafaelSpring: Sure. In the DS9K compiler, every name starting with double underscores has a different meaning during every compilation run :-)Indonesian
A
1

Presumably since it starts with __ __restrict is an implementation-specific extension that can behave as each implementation desires. I imagine both compilers are correct in this case.

Instead of changing your references to pointers, why not avoid restrict entirely, instead using a profiler to find your hot spots, and only if it shows that such aliasing not covered by the C++ strict-alias rules is taking significant CPU time would I consider changing one specific reference to a pointer.

Allhallows answered 11/10, 2012 at 14:1 Comment(0)
B
1

Generally, you should. (But probably not in the function in question).

It's true that C++ doesn't have a restrict keyword, so one could argue you "can't" use it. But we all know that restrict is useful and even important for optimizing code, especially code that doesn't get inlined; and you should definitely indicate that different pointers in your code can't alias, if they can't alias. People often implicitly assume non-aliasing without restrict'ing their pointers.

But do allow for lack of compiler support. For example, and for G++/clang only:

#ifdef __GNUC__
// great, __restrict__ is available
#else
// too bad, can't use __restrict__
#define __restrict__ 
#endif

int foo(int* __restrict__ a, int* __restrict__ b);

Now, you might be thinking: "Ah, but I was asking about a reference!" - well... a reference parameter to a function is mostly a pointer with cute syntax. Sure, it's not the same thing, and there's lifetime extension rules, and rvalue references and what-not, but still.


Finally in your function specifically, it seems like sentence is the only pointer (unless you get other pointers from elsewhere in the function body). When that's the case, you needn't bother restricting it, since there's nothing for it to alias.

Boatbill answered 12/5, 2022 at 22:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.