__attribute__((optimize("-O2"))) doesn't work

Chaoran Yang chaoran@rice.edu
Tue Dec 23 20:58:00 GMT 2014


> I disagree with Jonathan Wakely's response that only more complicated
> functions should be considered: to assess whether the omit-frame-pointer
> optimization is being applied, a very simple function such as 'int foo()
> { return 2; }' is quite useful.

I agree with Jeff. But I tried Jonathan’s suggestion just in case. A more complicated example shows that __attribute__((optimize(“O2”))) does have some effect in optimizing the code, however, it is not nearly as aggressive as using -O2 on the command line. I think this behavior is not mentioned in the document and is not justified IMO, since compiling a single function marked with __attribute__((optimize(“O2”))) and compiling a source file with a single function with -O2 is exactly the same thing. There is no interfering to GCC from any source e.g. existence of other functions.

This is the more complicated code I tested.

int fib(int n)
{
  if (n < 2) return n;

  int x, y;
  x = fib(n-1);
  y = fib(n-2);

  return x + y;
}

> I agree with the other commenters that it's important to follow the
> documentation of __attribute__((optimize)) closely.  When examples have
> errors, such as saying optimize("-fomit-frame-pointer") rather than the
> correct optimize("omit-frame-pointer"), or even worse when the examples
> are written with "smart quotes", it's too easy to attribute what is
> being seen to an error by the reporter. (however, my gcc seems to treat
> these nonsense arguments to optimize() as errors so probably the OP had
> them right in test code if not in the messages on this mailing list)

This is weird. I did use optimize(“-fomit-frame-pointer”) and it compiled without failure. optimize(“omit-frame-pointer”) achieves the same effect as optimize(“-fomit-frame-pointer”). Similarly, optimize(“-O2”) is the same as optimize(“O2”) for my GCC. Maybe it’s because I’m using GCC 4.9.2 and you’re using GCC 4.9.1? However I agree optimize(“omit-frame-pointer”) and optimize(“O2”) is the correct way to use it just to follow the documentation.

Thanks,
Chaoran



More information about the Gcc-help mailing list