This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: __attribute__((optimize("-O2"))) doesn't work
- From: Chaoran Yang <chaoran at rice dot edu>
- To: Jeff Epler <jepler at unpythonic dot net>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Tue, 23 Dec 2014 14:58:20 -0600
- Subject: Re: __attribute__((optimize("-O2"))) doesn't work
- Authentication-results: sourceware.org; auth=none
- References: <127B0179-8F1C-4CE6-8318-33E8AA76E7A6 at rice dot edu> <20141223195046 dot GA94056 at unpythonic dot net>
> 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