You are not logged in.

#1 2010-02-07 21:20:43

vkumar
Member
Registered: 2008-10-06
Posts: 166

g++: program works with -O0, crashes horribly with -O2

The gdb backtraces for the -O2 version reveal that the issue is in "0x0000000000402a16 in new_qt(int) ()";
http://pastebin.com/m54edcd2e

If I get rid of __attribute__((__packed__)), I get an even more exotic crash;

#5  0x00007ffff7103690 in malloc () from /lib/libc.so.6
#6  0x00007ffff793a51d in operator new(unsigned long) () from /usr/local/lib64/libstdc++.so.6
#7  0x00000000004022a9 in qt_memstore::replenish() ()

I never actually use the 'new' operator in 'qt_memstore::replenish', but I do call the same 'new_qt' function which is where everything broke in the packed-struct version.

The unoptimized version (tested with both the packed attribute and without it) are working pretty well;

[vk@vk hashlife]$ ./hlife grids/test1.hlg
Generation: 0
   X
XXXX

XXXX

I'm using g++ 4.5.0 from svn, but I can replicate the exact same results with the current Arch g++. So I'm guessing this whole thing is my fault. Besides just turning optimization off, is there a fix for this? If you need more of the source to look at, I'd be happy to post it somewhere. Thanks in advance.

edit:
I've got a diff of the assembler output of the un/optimized versions. If you'd like to take a look at it, let me know where to post it.. (it's a little over 6000 lines long)

Last edited by vkumar (2010-02-07 21:25:49)


div curl F = 0

Offline

#2 2010-02-08 06:44:55

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: g++: program works with -O0, crashes horribly with -O2

Just out of curiosity and because I had to read up on it because I've never seen
something like this before:

Have you tried omitting this stuff:

__attribute__((__packed__))

The guy here says
that gcc's automatic memory alignment is *good* and you should only disable it
if you exactly know what you're doing (not implicating you don't!).

Offline

#3 2010-02-08 13:36:00

sr
Member
Registered: 2009-10-12
Posts: 51

Re: g++: program works with -O0, crashes horribly with -O2

Nothing to do with the attribute, really:

 
> g++ -Wall -O2 -ggdb m54edcd2e.cpp
m54edcd2e.cpp: In function 'qtree* init_qtree(int, bool)':
m54edcd2e.cpp:21: warning: no return statement in function returning non-void

Offline

#4 2010-02-08 15:32:19

Cerebral
Forum Fellow
From: Waterloo, ON, CA
Registered: 2005-04-08
Posts: 3,108
Website

Re: g++: program works with -O0, crashes horribly with -O2

Yeah, as sr kinda hints at with his output, your init_qtree method might want to return something.

Offline

#5 2010-02-08 22:33:54

vkumar
Member
Registered: 2008-10-06
Posts: 166

Re: g++: program works with -O0, crashes horribly with -O2

Well this is embarrassing! Thank you.

I thought if I compiled with -Wextra, g++ would catch all of my silly mistakes. I spent a long time wondering about this... and am now adding '-Wall' to my CXXFLAGS smile.

the_jsz:

Have you tried omitting this stuff:
__attribute__((__packed__))

Yep, and I posted the results in my original post. I'd like to pack the struct because, though more memory accesses are required, I save 7 bytes for every tree node. And with a quadtree with a height of 10, that's a savings of 9786707 bytes, or roughly 9.3 megabytes.


div curl F = 0

Offline

#6 2010-02-09 06:57:07

the_isz
Member
Registered: 2009-04-14
Posts: 280

Re: g++: program works with -O0, crashes horribly with -O2

vkumar wrote:

Yep, and I posted the results in my original post. I'd like to pack the struct because, though more memory accesses are required, I save 7 bytes for every tree node. And with a quadtree with a height of 10, that's a savings of 9786707 bytes, or roughly 9.3 megabytes.

Sorry about that, I'm ashamed I didn't read your post well enough and I had
hoped it might go unnoticed^^ Well, thanks for having me learn something new smile

Offline

Board footer

Powered by FluxBB