linux-kernel.vger.kernel.org archive mirror
 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-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 10:24   ` Helge Hafting
  0 siblings, 2 replies; 7+ messages in thread
From: Horst von Brand @ 2003-02-11  7:14 UTC (permalink / raw)
  To: Art Haas; +Cc: Linux Kernel Mailing List

Art Haas <ahaas@airmail.net> said:
> 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.

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 (at the same
struct, or into the same array, etc). There is no way to check for this in
the compiler in general (the function and the call might be in different
files, many functions are being called via pointers, ...).

That it did not bite you (yet, or perhaps you haven't noticed) doesn't mean
anything. Perhaps the compiler didn't make use of it (as of this version),
perhaps you did not hit a problem with optimized code (yet). Or perhaps the
kernel is clean WRT this. Your bet.
-- 
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513

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

* Re: Is -fno-strict-aliasing still needed?
  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 10:24   ` Helge Hafting
  1 sibling, 1 reply; 7+ messages in thread
From: Andreas Schwab @ 2003-02-11  9:12 UTC (permalink / raw)
  To: Horst von Brand; +Cc: Art Haas, Linux Kernel Mailing List

Horst von Brand <brand@jupiter.cs.uni-dortmund.de> writes:

|> Art Haas <ahaas@airmail.net> said:
|> > 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.
|> 
|> 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

This is wrong.  Only if they are declared restrict.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: Is -fno-strict-aliasing still needed?
  2003-02-11  9:12   ` Andreas Schwab
@ 2003-02-11  9:39     ` Horst von Brand
  2003-02-11  9:45       ` Andreas Schwab
  0 siblings, 1 reply; 7+ messages in thread
From: Horst von Brand @ 2003-02-11  9:39 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: Art Haas, Linux Kernel Mailing List, brand

Andreas Schwab <schwab@suse.de> said:
> Horst von Brand <brand@jupiter.cs.uni-dortmund.de> writes:
> 
> |> Art Haas <ahaas@airmail.net> said:
> |> > 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.
> |> 
> |> 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
> 
> This is wrong.  Only if they are declared restrict.

... can they point to the same area. 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 got biten by
that when the optimizations (and the flag) were introduced into gcc (egcs
branch perhaps?).
-- 
Dr. Horst H. von Brand                   User #22616 counter.li.org
Departamento de Informatica                     Fono: +56 32 654431
Universidad Tecnica Federico Santa Maria              +56 32 654239
Casilla 110-V, Valparaiso, Chile                Fax:  +56 32 797513

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

* Re: Is -fno-strict-aliasing still needed?
  2003-02-11  9:39     ` Horst von Brand
@ 2003-02-11  9:45       ` Andreas Schwab
  0 siblings, 0 replies; 7+ messages in thread
From: Andreas Schwab @ 2003-02-11  9:45 UTC (permalink / raw)
  To: Horst von Brand; +Cc: Art Haas, Linux Kernel Mailing List, brand

Horst von Brand <brand@jupiter.cs.uni-dortmund.de> writes:

|> Andreas Schwab <schwab@suse.de> said:
|> > Horst von Brand <brand@jupiter.cs.uni-dortmund.de> writes:
|> > 
|> > |> Art Haas <ahaas@airmail.net> said:
|> > |> > 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.
|> > |> 
|> > |> 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
|> > 
|> > This is wrong.  Only if they are declared restrict.
|> 
|> ... can they point to the same area. 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.

Why are you insisting on spreading misinformation?

Pointers that are _not_ declared as restricted can alias _any_ other
non-restricted pointer of the same target type.

Andreas.

-- 
Andreas Schwab, SuSE Labs, schwab@suse.de
SuSE Linux AG, Deutschherrnstr. 15-19, D-90429 Nürnberg
Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."

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

* Re: Is -fno-strict-aliasing still needed?
  2003-02-11  7:14 ` Horst von Brand
  2003-02-11  9:12   ` Andreas Schwab
@ 2003-02-11 10:24   ` Helge Hafting
  1 sibling, 0 replies; 7+ messages in thread
From: Helge Hafting @ 2003-02-11 10:24 UTC (permalink / raw)
  To: Horst von Brand; +Cc: linux-kernel, Art Haas

Horst von Brand wrote:
> 
> Art Haas <ahaas@airmail.net> said:
> > 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.
> 
> 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 (at the same
> struct, or into the same array, etc). There is no way to check for this in
> the compiler in general (the function and the call might be in different
> files, many functions are being called via pointers, ...).

I though pointers to the same type, such as int *, could alias still.

But pointers to different types, such as int* and short* is assumed
to never clash with strict aliasing.  And this bites linux
because it sometimes choose to see "two adjacent shorts as one int" for
performance reasons.  

I remember the flag was introduced because some IP or TCP
code do exactly this, and converting it all to unions would
render that code unreadable.

Helge Hafting

^ 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
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;
as well as URLs for NNTP newsgroup(s).