[PATCH] D75285: Mark restrict pointer or reference to const as invariant

Yaxun Liu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Feb 28 14:00:29 PST 2020


yaxunl added a comment.

In D75285#1897502 <https://reviews.llvm.org/D75285#1897502>, @jeroen.dobbelaere wrote:

> I don't think that 'restrict' is a good match for this behavior. For c++, the alias_set proposal (http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2014/n4150.pdf) would be a better match.
>  You would put the read access of *p in its own universe; or even better, something like
>
>   struct X {
>     int a;
>     char [[alias_set(MyOwnUniverseForX_b)]]  b;
>   };
>   
>
> Unfortunatly, there is no implementation yet.
>
> Imho, adding a '__attribute__((invariant))' or something similar (immutable ? const_invariant ?) would be a better approach. (hmm, I like 'immutable')
>
>   char test2(X *x) {
>     const char __attribute__((immutable))  *p = (const char __attribute__((immutable))*)&(x->b);
>     // for all i:  p[i] will never be modified.
>     return *p;
>   }
>   
>
> Extra precautions are probably needed to ensure that the initialization of x->b is separated from the usage of it.


Agree. An attribute like `__attribute__((immutable))` should be useful. It can be used on a pointer or reference and tells the compiler that
the content of the pointer or reference is invariant in the scope of that variable.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D75285/new/

https://reviews.llvm.org/D75285





More information about the cfe-commits mailing list