Just wondering if someone would confirm a few aliasing rules for me.
I know that aliasing (i.e load-store issues) could cause the following type of code to be suboptimal, because we can't assume that x, y, z
don't overlap:
// case 1:
void plus(size_t n, double *x, double *y, double *z)
{
for (size_t i = 0; i != n; ++i)
z[i] = x[i] + y[i];
}
I know that there's a C keyword __restrict
that hints to the compiler that it shouldn't consider the overlapping case, and hence potentially generate better code:
// case 2:
void plus(size_t n, double *__restrict x, double *__restrict y, double *__restrict z)
{ // as above... }
But how does aliasing work with C++ style code, where we would be dealing with container objects passed by reference, rather than the C-like examples above with raw pointers??
For instance, I'm assuming that there would be aliasing issues if we did the following:
// case 3:
void plus(std::vector<double> &x, std::vector<double> &y, std::vector<double> &z)
{ // similar to above... }
And to move to a less trivial example, does it make any difference if the underlying data types in the containers are different?? At the implementation level most containers dynamically manage storage with pointers, so it's not clear to me how the compiler could ensure that the following doesn't alias:
// case 4:
void foo(std::vector<mytype1> &x, std::vector<mytype2> &y)
{ // interwoven operations on x, y... }
I'm not trying to micro-optimise, but I'm wondering if it's currently better to pass restricted pointers to containers around, rather than references.
EDIT: To clear up some terminology, as pointed out: restrict
is the C99 keyword. There's also __restrict
and __restrict__
in various compilers, but they all do the same thing.
&x == &y
I don't believe there's any way they could overlap, assuming standards-compliant vector implementations. – Hopheadrestrict
is a C keyword (since C99), but__restrict
is not. As the__
indicates, it is a compiler extension. – Showboat