Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Situation with `restrict` keyword/attribute in C++ standard

In short, restrict is supposed to tell the compiler that the pointers cannot point into the same memory location. Which is very useful for, say, function arguments and further compiler optimization. In scientific computing, restrict is very widely used.

Currently, restrict keyword is only a part of C99, but not a part of C++. We know that a lot of C++ compilers support __restrict__ as an extension. This question also talks in detail about what restrict and __restrict__ do.

Now, the discussion in the aforementioned question happened a long time ago and does not talk about C++17, C++20, nor plans for future standards. I found n3988 proposal that discusses restrict-like aliases in C++, complexities with richer syntaxis in C++, and potential remedies.

According to the IBM blog (2014), n3988 was encouraged for future work.

This question talks about the history of restrict and C++ without anything conclusive regarding the actual implementation and mentions the papers I already listed or the one mentioned in the comments (p1296).

I was not able to find anything beyond that on the plans of supporting restrict in the upcoming C++ (as far as I know, it's not a part of C++17). It seems like a very useful functionality, so I wonder

  • if I missed something in terms of proposals/discussion?
  • is there other information on the restrict usage in C++?
  • are there alternative ways to make the compiler optimizations (allowed by __restrict__) possible by using only "standard" functionality?
like image 215
Anton Menshov Avatar asked May 23 '19 21:05

Anton Menshov


People also ask

What is the use of restrict keyword in C?

In the C programming language, restrict is a keyword, introduced by the C99 standard, that can be used in pointer declarations. By adding this type qualifier, a programmer hints to the compiler that for the lifetime of the pointer, no other pointer will be used to access the object to which it points.

What are restricted pointers?

Restricted pointers in C99 The C99 keyword restrict is an indication to the compiler that different object pointer types and function parameter arrays do not point to overlapping regions of memory. This enables the compiler to perform optimizations that might otherwise be prevented because of possible aliasing.

What does restrict mean C++?

restrict says that two pointers cannot point to overlapping memory regions. The most common usage is for function arguments. This restricts how the function can be called, but allows for more compile optimizations. If the caller does not follow the restrict contract, undefined behavior can occur.

What is const char restrict?

For example, char const * restrict is a type. Actually, it only applies to pointer types, i.e. T * restrict . So int restrict is invalid, but int * restrict is valid. In a function, a parameter T * restrict p means that the allocated object pointed at by p is only pointed at by p .


1 Answers

Nothing like C’s restrict is in even C++20. The paper already mentioned was well-received at a preliminary presentation in November 2018, perhaps because it avoids the critical difficulty with a qualifier—that no one, even in C, understands how it interacts with the rest of the type system. Part of this is because adding restrict doesn’t change the meaning of any one pointer, but affects its relationship with some set of other pointers (whose membership is not well-specified) based on what arithmetic is performed with them later. Another part is because C++ allows so many operations with types: what would std::vector<T *restrict> mean, and what would be the type of indexing a std::vector<T> &restrict?

Just what practical optimization opportunity will be offered by such a contract-based approach is not yet clear; there are still many unanswered questions about contracts and optimization in general.

like image 97
Davis Herring Avatar answered Sep 21 '22 09:09

Davis Herring