public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias.burnus@physik.fu-berlin.de>
To: gcc-help@gcc.gnu.org
Subject: (gcc/)g++ and __restrict
Date: Fri, 20 Sep 2013 10:07:00 -0000	[thread overview]
Message-ID: <20130920100723.GA24605@physik.fu-berlin.de> (raw)

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];
}

             reply	other threads:[~2013-09-20 10:07 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-09-20 10:07 Tobias Burnus [this message]
2013-09-20 14:59 ` Ian Lance Taylor
2013-09-22 22:42   ` Tobias Burnus

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130920100723.GA24605@physik.fu-berlin.de \
    --to=tobias.burnus@physik.fu-berlin.de \
    --cc=gcc-help@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).