public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Flags -g and -O give very different results
@ 2006-07-01  2:35 Michael P Friedlander
  2006-07-02 18:31 ` Andrew Haley
  2006-07-03 10:40 ` Brian Gough
  0 siblings, 2 replies; 8+ messages in thread
From: Michael P Friedlander @ 2006-07-01  2:35 UTC (permalink / raw)
  To: gcc-help

Hi Folks,

The behavior of a numerical code I'm working on varies
drastically depending on whether I've compiled it with
the  -g  or  -O  flags.

The code's behavior under -g is much more stable, and I'm
wondering if the -O flag is exposing a bug that I need to
fix.  Are there some gcc flags that I should try that might
guide me in finding the problem?  (I've already tried the
obvious  -Wall  which gives no warnings.)

In case it helps, I'm using both gcc 4.1.0 (SUSE Linux)
and on gcc 4.0.1 (Mac PPC) which give similar behavior.

Thanks!
--Michael



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

* Re: Flags -g and -O give very different results
  2006-07-01  2:35 Flags -g and -O give very different results Michael P Friedlander
@ 2006-07-02 18:31 ` Andrew Haley
  2006-07-02 19:28   ` Michael P Friedlander
  2006-07-03 10:40 ` Brian Gough
  1 sibling, 1 reply; 8+ messages in thread
From: Andrew Haley @ 2006-07-02 18:31 UTC (permalink / raw)
  To: Michael P Friedlander; +Cc: gcc-help

Michael P Friedlander writes:
 > Hi Folks,
 > 
 > The behavior of a numerical code I'm working on varies
 > drastically depending on whether I've compiled it with
 > the  -g  or  -O  flags.
 > 
 > The code's behavior under -g is much more stable, and I'm
 > wondering if the -O flag is exposing a bug that I need to
 > fix.  Are there some gcc flags that I should try that might
 > guide me in finding the problem?  (I've already tried the
 > obvious  -Wall  which gives no warnings.)

If your code does something different with/without -g, then that's a
bug in gcc.  -g shouldn't make any difference to the behaviour of your
program.

If your code does somethig different with -O, that's possibly a gcc
bug but it's probably a bug in your code.

If you use -fno-strict-aliasing and that makes a difference with -O,
then that's definitely a bug in your code.

Andrew.

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

* Re: Flags -g and -O give very different results
  2006-07-02 18:31 ` Andrew Haley
@ 2006-07-02 19:28   ` Michael P Friedlander
  2006-07-02 19:41     ` Tim Prince
                       ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Michael P Friedlander @ 2006-07-02 19:28 UTC (permalink / raw)
  To: Andrew Haley; +Cc: gcc-help

Thanks for pointing out various options, Andrew.

>> The behavior of a numerical code I'm working on varies
>> drastically depending on whether I've compiled it with
>> the  -g  or  -O  flags.
>>
>> The code's behavior under -g is much more stable, and I'm
>> wondering if the -O flag is exposing a bug that I need to
>> fix.  Are there some gcc flags that I should try that might
>> guide me in finding the problem?  (I've already tried the
>> obvious  -Wall  which gives no warnings.)
>
> If your code does something different with/without -g, then that's a
> bug in gcc.  -g shouldn't make any difference to the behaviour of your
> program.

-g makes no difference.

> If your code does somethig different with -O, that's possibly a gcc
> bug but it's probably a bug in your code.

-OO  and  -O  give different results.

> If you use -fno-strict-aliasing and that makes a difference with -O,
> then that's definitely a bug in your code.

I think you nailed it!  With -fno-strict-aliasing,  -O0  and  -O   
give identical results.  I tried -fstrict-aliasing  with -Wstrict- 
aliasing=2, but gcc doesn't issue any warnings.

Any pointers for how to track this sort of thing down?  What kind of  
things should I look for?

Thanks!
--Michael

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

* Re: Flags -g and -O give very different results
  2006-07-02 19:28   ` Michael P Friedlander
@ 2006-07-02 19:41     ` Tim Prince
  2006-07-02 21:00     ` Segher Boessenkool
                       ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Tim Prince @ 2006-07-02 19:41 UTC (permalink / raw)
  To: Michael P Friedlander; +Cc: Andrew Haley, gcc-help

Michael P Friedlander wrote:
> Thanks for pointing out various options, Andrew.
> 
>>> The behavior of a numerical code I'm working on varies
>>> drastically depending on whether I've compiled it with
>>> the  -g  or  -O  flags.
>>>
>>> The code's behavior under -g is much more stable, and I'm
>>> wondering if the -O flag is exposing a bug that I need to
>>> fix.  Are there some gcc flags that I should try that might
>>> guide me in finding the problem?  (I've already tried the
>>> obvious  -Wall  which gives no warnings.)
>>
>> If your code does something different with/without -g, then that's a
>> bug in gcc.  -g shouldn't make any difference to the behaviour of your
>> program.
> 
> -g makes no difference.
> 
>> If your code does somethig different with -O, that's possibly a gcc
>> bug but it's probably a bug in your code.
> 
> -OO  and  -O  give different results.
> 
>> If you use -fno-strict-aliasing and that makes a difference with -O,
>> then that's definitely a bug in your code.
> 
> I think you nailed it!  With -fno-strict-aliasing,  -O0  and  -O  give 
> identical results.  I tried -fstrict-aliasing  with -Wstrict-aliasing=2, 
> but gcc doesn't issue any warnings.
> 
> Any pointers for how to track this sort of thing down?  What kind of 
> things should I look for?
> 
-fstrict-aliasing asserts that your code complies with the C standard on 
typed aliasing.  Pointers to objects of incompatible type must not 
modify the same storage locations.  Code which violated this restriction 
was common, prior to the time when gcc introduced this option.

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

* Re: Flags -g and -O give very different results
  2006-07-02 19:28   ` Michael P Friedlander
  2006-07-02 19:41     ` Tim Prince
@ 2006-07-02 21:00     ` Segher Boessenkool
  2006-07-02 21:05     ` Brian Dessent
  2006-07-03  9:32     ` Andrew Haley
  3 siblings, 0 replies; 8+ messages in thread
From: Segher Boessenkool @ 2006-07-02 21:00 UTC (permalink / raw)
  To: Michael P Friedlander; +Cc: Andrew Haley, gcc-help

> Any pointers for how to track this sort of thing down?  What kind  
> of things should I look for?

Mostly casts from one pointer type to another (you probably can
ignore void* and char* though).

Things like

long f(int *x) {
	return *(long *)x;
}

are the most common problems.  So,

	*(<some type> *)<some data address>

[there are more subtle things as well -- but almost all of the
time, the problem is gross implicit violation of the C99 data
aliasing rules).


Segher

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

* Re: Flags -g and -O give very different results
  2006-07-02 19:28   ` Michael P Friedlander
  2006-07-02 19:41     ` Tim Prince
  2006-07-02 21:00     ` Segher Boessenkool
@ 2006-07-02 21:05     ` Brian Dessent
  2006-07-03  9:32     ` Andrew Haley
  3 siblings, 0 replies; 8+ messages in thread
From: Brian Dessent @ 2006-07-02 21:05 UTC (permalink / raw)
  To: gcc-help

Michael P Friedlander wrote:
 
> Any pointers for how to track this sort of thing down?  What kind of
> things should I look for?

The following post is referenced from the gcc docs or gcc website
somewhere, I don't recall exactly where, but it sums it up fairly well:
<http://mail-index.netbsd.org/tech-kern/2003/08/11/0001.html>

Brian

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

* Re: Flags -g and -O give very different results
  2006-07-02 19:28   ` Michael P Friedlander
                       ` (2 preceding siblings ...)
  2006-07-02 21:05     ` Brian Dessent
@ 2006-07-03  9:32     ` Andrew Haley
  3 siblings, 0 replies; 8+ messages in thread
From: Andrew Haley @ 2006-07-03  9:32 UTC (permalink / raw)
  To: Michael P Friedlander; +Cc: gcc-help

Michael P Friedlander writes:
 > Thanks for pointing out various options, Andrew.
 > 
 > >> The behavior of a numerical code I'm working on varies
 > >> drastically depending on whether I've compiled it with
 > >> the  -g  or  -O  flags.
 > >>
 > >> The code's behavior under -g is much more stable, and I'm
 > >> wondering if the -O flag is exposing a bug that I need to
 > >> fix.  Are there some gcc flags that I should try that might
 > >> guide me in finding the problem?  (I've already tried the
 > >> obvious  -Wall  which gives no warnings.)
 > >
 > > If your code does something different with/without -g, then that's a
 > > bug in gcc.  -g shouldn't make any difference to the behaviour of your
 > > program.
 > 
 > -g makes no difference.
 > 
 > > If your code does somethig different with -O, that's possibly a gcc
 > > bug but it's probably a bug in your code.
 > 
 > -OO  and  -O  give different results.
 > 
 > > If you use -fno-strict-aliasing and that makes a difference with -O,
 > > then that's definitely a bug in your code.
 > 
 > I think you nailed it!  With -fno-strict-aliasing,  -O0  and  -O   
 > give identical results.  I tried -fstrict-aliasing  with -Wstrict- 
 > aliasing=2, but gcc doesn't issue any warnings.
 > 
 > Any pointers for how to track this sort of thing down?  What kind of  
 > things should I look for?

Your code probably contains something like

  int n;
  *(short*)&n = 5;

i.e. you're accessing an object as something other than its underlying
type.  However, something as blatant as this would probably be caught
by gcc, so you should be looking for something more subtle.  Like
this, maybe:

   int n;
   void *p = &n;
   *(short*)p = 5;

Andrew.

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

* Re: Flags -g and -O give very different results
  2006-07-01  2:35 Flags -g and -O give very different results Michael P Friedlander
  2006-07-02 18:31 ` Andrew Haley
@ 2006-07-03 10:40 ` Brian Gough
  1 sibling, 0 replies; 8+ messages in thread
From: Brian Gough @ 2006-07-03 10:40 UTC (permalink / raw)
  To: Michael P Friedlander; +Cc: gcc-help

At Fri, 30 Jun 2006 19:35:03 -0700,
Michael P Friedlander wrote:
> The behavior of a numerical code I'm working on varies
> drastically depending on whether I've compiled it with
> the  -g  or  -O  flags.
> 
> The code's behavior under -g is much more stable, and I'm
> wondering if the -O flag is exposing a bug that I need to
> fix.  Are there some gcc flags that I should try that might
> guide me in finding the problem?  (I've already tried the
> obvious  -Wall  which gives no warnings.)

A couple of other generally useful options for numerical
programs: -W, -Wconversion

Also test your code for memory errors with valgrind or
-fmudflap.

Most likely you'll have to look at the code itself and
see where the differences come from.

-- 
Brian Gough

Network Theory Ltd,
Publishing Free Software Manuals --- http://www.network-theory.co.uk/

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

end of thread, other threads:[~2006-07-03 10:40 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-07-01  2:35 Flags -g and -O give very different results Michael P Friedlander
2006-07-02 18:31 ` Andrew Haley
2006-07-02 19:28   ` Michael P Friedlander
2006-07-02 19:41     ` Tim Prince
2006-07-02 21:00     ` Segher Boessenkool
2006-07-02 21:05     ` Brian Dessent
2006-07-03  9:32     ` Andrew Haley
2006-07-03 10:40 ` Brian Gough

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).