26

I am working on a project that relies on compiler optimisations but I need some code not to be optimised by GCC. Is this possible?

limp
  • 889
  • 2
  • 14
  • 22
  • 2
    possible duplicate of [How to prevent gcc optimizing some statements in C?](http://stackoverflow.com/questions/2219829/how-to-prevent-gcc-optimizing-some-statements-in-c) – ulidtko Mar 25 '14 at 21:01

3 Answers3

34

GCC 4.4 has an attribute for that:

int foo(int i) __attribute__((optimize("-O3")));

It is documented at: https://gcc.gnu.org/onlinedocs/gcc-5.1.0/gcc/Function-Attributes.html#index-g_t_0040code_007boptimize_007d-function-attribute-3195

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
sehe
  • 374,641
  • 47
  • 450
  • 633
  • 1
    Excellent! That worked great! I was wondering if I add the attribute into a function, does it apply in all its subfunctions as well or only in the function's locally executed code? – limp Apr 07 '11 at 13:09
  • 1
    @limp Only in the function body (how else, because it could call functions that are called from _other_ places that _do_ have optimizations enabled...) – sehe Oct 10 '12 at 12:42
  • 1
    Do you really need the hyphen before O3? – Adam Hunyadi Nov 30 '17 at 09:30
  • 1
    In GCC 9.1 for ARM, the compiler complained and asked for the attribute to be before the declaration. – tothphu Mar 25 '21 at 01:23
21

GCC has since 4.4. the #pragma GCC optimize ("whatever"). I would also recommend to wrap the particular code, that is annotated with this pragma with #pragma GCC push_options and #pragma GCC pop_options. The first will save the options as they were before your change, the later will restore them afterwards and the rest of the code will compile with the global options.

For details on the whatever string, you should look into the gcc doc, here the most important part of it: Arguments can either be numbers or strings. Numbers are assumed to be an optimization level. Strings that begin with O are assumed to be an optimization option, while other options are assumed to be used with a -f prefix..

That means if you dont want any optimizations on your particular code your whatever should just be "0".

flolo
  • 15,148
  • 4
  • 32
  • 57
  • I've tried that by I got an "#pragma GCC optimize is not allowed inside functions" error. Is it possible to enable/disable optimisations inside a function scope? – limp Apr 07 '11 at 12:10
  • @limp: oh no, that does not work. This pragma makes practically nothing than add the corrosponding attribute flag to the function(s). So if you really want to change options on a small part of a function you have at least to move it to an own subfuction. Its not possible to make inside functions, as many optimizations work on whole functions and technically it isnt possible to change the behaviour of just some parts of it. – flolo Apr 07 '11 at 12:33
  • Ok, so AFAIU that means that #pragma GCC optimize has a file scope. If I put in the beggining of a source file #pragma GCC push_options #pragma GCC optimize ("O0") and then #pragma GCC pop_options in the end of the file, I guess all the code/functions that is contained in this file, will not be optimised, am I right? – limp Apr 07 '11 at 12:50
  • @limb: Yes, beginning and end will treat the whole file. But you can make it more fine granular. The granularity is function - i.e. you can optimize functions in a file different (you have to declare between the functions) - you just cant do it inside function to treat a single function different. – flolo Apr 07 '11 at 13:11
  • 1
    It's worth to note that Clang also works with `#pragma GCC` for compatibility reasons. – xdevs23 May 17 '17 at 16:37
3

You can put that piece of code into a different file and compile it without optimisations.

Or try to use the #pragma directive:

#pragma optimize level=0 

Or even better start and stop optimization with :

#pragma OPTIMIZE ON 
#pragma OPTIMIZE OFF
Heisenbug
  • 38,762
  • 28
  • 132
  • 190
  • Is there something I should do in order to enable compiler support for #pragma directives or it comes by default on GGC >= 4.4? – limp Apr 07 '11 at 11:44
  • 2
    Are all of the following supported by GCC?: #pragma GCC optimize 1 #pragma GCC optimize 0 #pragma OPTIMIZE OFF #pragma OPTIMIZE ON #pragma optimize ("", off) #pragma optimize ("", on) Also, can I use these pragmas inside functions? – limp Apr 07 '11 at 12:01
  • @limp: depends on your gcc version. @fiolo pointed out that is from 4.4 . – Heisenbug Apr 07 '11 at 12:03
  • Also on the platform it seems. On Raspbian (armv7l) these are not supported despite GCC is version 4.9.2 :(. `__attribute__` seems to work. – Jan Wielemaker Oct 24 '17 at 17:57