public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Is -fno-strict-aliasing still needed?
@ 2003-02-10 20:04 Art Haas
  2003-02-11  7:14 ` Horst von Brand
  0 siblings, 1 reply; 7+ messages in thread
From: Art Haas @ 2003-02-10 20:04 UTC (permalink / raw)
  To: linux-kernel

Hi.

I ask because I've just built a kernel without using that flag -
linus-2.5 BK from this morning, probably missing the 2.5.60 release by
a few hours. I'm now running with the kernel, and things are working
normally. So, my success with my strictly aliased kernel has made me curious
if the continuing use of the flag has outlived its usefulness. I'm
running on i586, compiling with gcc-3.2.2 (CVS). Possibly the flag is
needed for other chipsets or older compilers, or particular bits of code
that I don't compile. Still, I thought I'd ask and see. Maybe other
people can try building without '-fno-strict-aliasing' and see what sort
of results they get.

I'm not including my config file to save space, but can send it if
asked.

Art Haas
-- 
They that can give up essential liberty to obtain a little temporary safety
deserve neither liberty nor safety.
 -- Benjamin Franklin, Historical Review of Pennsylvania, 1759

^ permalink raw reply	[flat|nested] 7+ messages in thread
* Re: Is -fno-strict-aliasing still needed?
@ 2003-02-11 19:52 Albert Cahalan
  0 siblings, 0 replies; 7+ messages in thread
From: Albert Cahalan @ 2003-02-11 19:52 UTC (permalink / raw)
  To: linux-kernel; +Cc: ahaas, brand, helgehaf

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?



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2003-02-11 19:46 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
  -- strict thread matches above, loose matches on Subject: below --
2003-02-11 19:52 Albert Cahalan

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox