public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Albert Cahalan <albert@users.sf.net>
To: linux-kernel@vger.kernel.org
Cc: ahaas@airmail.net, brand@jupiter.cs.uni-dortmund.de,
	helgehaf@aitel.hist.no
Subject: Re: Is -fno-strict-aliasing still needed?
Date: 11 Feb 2003 14:52:33 -0500	[thread overview]
Message-ID: <1044993153.3133.517.camel@cube> (raw)

Quoting various people:

> The problem with strict aliasing is that it allows
> the compiler to assume that in:
> 
>      void somefunc(int *foo, int *bar)
> 
> foo and bar will _*never*_ point to the same memory area

Nope, because foo and bar are compatible types.
These are compatible:

int*, unsigned*, const signed int *, register int*, etc.

For a pointer to an integer type, it pretty much boils
down to "compatible ones are the same size on an Alpha".
There is a special exception for (char*) though, and
a poorly-documented "restrict" keyword to let the compiler
know when aliasing (pointing to the same memory) won't happen.

Here are some examples:

#define restrict __restrict__    // or do "gcc -std=c99"
int fn(int *foo, int *bar);          // may alias
int fn(int *foo, int *restrict bar);  // may NOT alias
int fn(int *foo, long *bar);        // may NOT alias
int fn(int *foo, char *bar);      // may alias (char exception)
int fn(int *foo, char *restrict bar); // restrict w/ (char*) ?
int fn(int *restrict foo, char *bar); // restrict w/ (char*) ?
// New gcc feature, due to Linus complaining:
typedef unsigned long __attribute__((__may_alias__)) ula;
int fn(int *foo, ula *bar);    // may alias

The big problem is testing. Lots of people assume that,
as long as alignment is satisfied, it is safe to cast
from one pointer type to another for accessing data.
This is not true. The half-legit solution to put both
pointers into a union, but that can get ugly. So some
determined person must go add __may_alias__ all over the
kernel, not missing any spot where it may be needed.

>> This is wrong.  Only if they are declared restrict.
>
> ... can they point to the same area.

You have that exactly backwards. With restrict, they
MAY NOT point to the same area. With (char*) they MAY.
Otherwise, it's a question of compatible types.

> That is exactly the problem: If you do nothing, the
> language definition assumes the programmer made sure
> (LOL!) that they don't point the same way. That's why
> the flag is needed in the first place, as nobody
> writes "restrict" all over the place.

I do. (http://procps.sf.net/) Ulrich Drepper does. (glibc)

You should see the description of how "restrict" can
interact with scope. Magic smoke may leak out of your brain.
IMHO the "restrict" keyword was really botched.

If you really want to get fancy, alter the language
to suit your desires:

-fargument-alias
-fargument-noalias 
-fargument-noalias-global

> I got biten by that when the optimizations (and the flag)
> were introduced into gcc (egcs branch perhaps?).

Me too, casting (double*) to (short*) to examine bits.
Now I don't do that. It's not legal in the C language.

-------
Grrr... why isn't Evolution good for editing code?



             reply	other threads:[~2003-02-11 19:46 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-11 19:52 Albert Cahalan [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-02-10 20:04 Is -fno-strict-aliasing still needed? Art Haas
2003-02-11  7:14 ` Horst von Brand
2003-02-11  9:12   ` Andreas Schwab
2003-02-11  9:39     ` Horst von Brand
2003-02-11  9:45       ` Andreas Schwab
2003-02-11 10:24   ` Helge Hafting

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=1044993153.3133.517.camel@cube \
    --to=albert@users.sf.net \
    --cc=ahaas@airmail.net \
    --cc=brand@jupiter.cs.uni-dortmund.de \
    --cc=helgehaf@aitel.hist.no \
    --cc=linux-kernel@vger.kernel.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