This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
(gcc/)g++ and __restrict
- From: Tobias Burnus <tobias dot burnus at physik dot fu-berlin dot de>
- To: gcc-help at gcc dot gnu dot org
- Date: Fri, 20 Sep 2013 12:07:27 +0200
- Subject: (gcc/)g++ and __restrict
- Authentication-results: sourceware.org; auth=none
Hi all,
I was wondering how to convey to GCC that two pointers do not alias.
That works nicely in the argument list,
void foo(mytype *__restrict__ arg)
However, it does not seem to work for C++'s member variables. Even
if one has on the class the declaration with __restrict. Neither does
casting to a restrict pointer work.
(The issue of example 1 also applies C99.)
In example 1, one gets:
test.cc:17:3: note: loop versioned for vectorization because of
possible aliasing
That's unchanged when one uncomments the casts. However, when one
#defines RESTRICT as __restrict, the loop versioning disappears.
In example 2, have the same problem: I get loop versioning and neither the
__restrict__ in the class definition nor the restrict casting has any
effect whatsoever.
Is this to be expected? And, if so, how can one otherwise convay
this information?
Tobias
PS: For the big code, using a static array instead of a allocatable pointer
gives a speed-up of up to 40% - and I am highly suspicious that this is
mainly due to alias analysis.
PPS: For loops, using #pragma ivdep should help (and does with Intel; for
GCC it's still on my to-do list). And also the forced vectorization with
#pragma simd (Cilk+/OpenMPv4) should help. Still, __restrict__ should
go beyond. (Using Fortran's allocatables is also a solution ;-)
Example 1:
#define ASSUME_ALIGNED(lvalueptr, align) \
lvalueptr = \
( __typeof(lvalueptr))( \
__builtin_assume_aligned(lvalueptr, align))
//#define RESTRICT __restrict__
#define RESTRICT
typedef double mytype;
void test(int size, mytype *RESTRICT a, mytype *RESTRICT b, mytype *RESTRICT c) {
ASSUME_ALIGNED(a, 64);
ASSUME_ALIGNED(b, 64);
ASSUME_ALIGNED(c, 64);
// a = (mytype* __restrict__) a;
// b = (mytype* __restrict__) b;
// c = (mytype* __restrict__) c;
for (int i = 0; i < size; ++i)
a[i] = b[i] + c[i];
}
Example 2:
#define ASSUME_ALIGNED(lvalueptr, align) \
lvalueptr = \
( __typeof(lvalueptr))( \
__builtin_assume_aligned(lvalueptr, align))
//#define RESTRICT __restrict__
#define RESTRICT
typedef double mytype;
void test(int size, mytype *RESTRICT a, mytype *RESTRICT b, mytype *RESTRICT c) {
ASSUME_ALIGNED(a, 64);
ASSUME_ALIGNED(b, 64);
ASSUME_ALIGNED(c, 64);
a = (mytype* __restrict__) a;
b = (mytype* __restrict__) b;
c = (mytype* __restrict__) c;
for (int i = 0; i < size; ++i)
a[i] = b[i] + c[i];
}