LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-09-2008, 10:12 AM   #1
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Rep: Reputation: 18
excluding code from gcc optimization


Hello.

I recently implemented an evolutionary algorithm which creates valid machine code expressions. This obviously speeds up my experiments a great deal. However, when I go to optimise the C++ code that uses the evolved machine code instructions; the machine code is executed incorrectly.

After a great deal of trial and error, I isolated the cause of the incorrect execution to Gcc's optimisation algorithms. For example, when using O1 optimisation, I have to include the flag "-fno-tree-fre" to get the code to execute correctly. When using O3 optimisation I have to include the flag "-fno-gcse" to get the code to execute correctly.

Unfortunately, these solutions are not consistent and the incorrect execution of code has started to occur again, in a far more complex context.

I'm certain that the problem arises from gcc believing that my dynamically generated code is dead code of some sort and that the manner in which gcc attempts to remove the percieved dead code is different within each context.

This probably accounts for the inconsistency of the compiler flags in solving the problem. Is anyone aware of a pragma / compiler directive which can tell gcc to disable optimisation for specfic area of code?.

I have also tried explicitly including every flag for O3 optimisation, this does not produce the same result as simply specifiying "O3" optiimsation at compile time, does anyone understand why this is so?

Thanks
Gearoid
 
Old 03-09-2008, 11:03 AM   #2
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
I don't think so, but you can always isolate that source it its own file and build it with -O0. That's essentially what you're looking for. The problem with optimizing a single part of the source is that it might use headers that are optimized in other cases. You might also want to use __attribute__ ((packed)) for all structures involved in case for some strange reason the difference in optimization causes them to align differently.
ta0kira

Last edited by ta0kira; 03-09-2008 at 11:05 AM.
 
Old 03-10-2008, 12:28 PM   #3
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
I have had some success using the "volatile" keyword, however, I am again having difficulties isolating which parts of my code to declare volatile.
 
Old 03-10-2008, 02:30 PM   #4
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
Why use optimization in the first place, then? If there are static parts of your program that need it then I suggest making that into a library so your only dynamic code is that which is generated.
ta0kira
 
Old 03-11-2008, 07:31 AM   #5
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
I'm inclined to agree but if it was just a case of avoiding the problem I wouldn't be here at the forums, I'm simply irked that I do not understand exactly why this is happening. In my experience such issues rarely go away and its usually best to get to the bottom of it.

Whats really driving me nuts is the fact that in some cases it works (the volatile keyword) and in some cases it doesn't, I'm working backwards from a failed example to a working one in the hope that I'll be able to isolate the problem.

Gearoid.
 
Old 03-11-2008, 07:58 AM   #6
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
Ok, thats done, my only consistent strategy is to declare volatile the function pointer (to the dynamic code), the return value of that function and any variables which control loops that the function is executed in.

I can understand the return value and the function pointer, but surely the looping variables should be implicitly volatile once they operate on volatile variables?, anyway shit happens.

Gearoid
 
Old 03-11-2008, 08:09 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
So what type of code is generated? It sounds to me like it might be non-standard code or that it relies on certain expectations that are otherwise construed as "undefined" by the various standards out there. Do you have an example of the generated code?
ta0kira
 
Old 03-12-2008, 05:09 AM   #8
gearoid_murphy
Member
 
Registered: Mar 2006
Location: Ireland
Distribution: Debian Etch
Posts: 72

Original Poster
Rep: Reputation: 18
The generated code always follows this pattern :
{
save the frame pointer;
set the frame pointer = stack ptr

if statements with floating point or integer condtional arguments returning various values.

put return value in eax.
pop the frame ptr
return
}

I had thought that the code might be breaking some caller/callee saves protocol, because some of my debugging seemed to indicate this, however, disabling the feature did not result in properly executed code.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
gcc compiler optimization question dcfnef Programming 2 01-17-2007 07:58 AM
GCC 3.3.3 SMP optimization spaceman27 Linux - General 2 04-14-2005 03:47 AM
GCC 3.4/3.3 Optimization Flags monohouse Linux - Software 0 10-28-2004 10:48 AM
gcc multiple optimization levels ? Ikebo Programming 2 07-11-2004 11:26 PM
GCC Processor Optimization ? Tomasfuego Programming 1 12-20-2003 02:59 PM


All times are GMT -5. The time now is 08:46 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration