Thursday, February 9, 2012

Controlling GCC Optimization with Pragmas, courtesy of U of I

Optimization Restriction Support

optimize Pragma

Use the optimize pragma to disable optimizations for specific functions. The pragma uses the following syntax:

Syntax

#pragma optimize("", on|off)

The compiler ignores first argument values. Valid second arguments for optimize are:

  • #pragma optimize("", off)- disables optimization

  • #pragma optimize("", on)- enables optimization

Specifying #pragma optimize("", off)disables optimization until either the compiler finds a matching #pragma optimize("", on) statement or until the compiler reaches the end of the translation unit. For example, in the following example optimizations are disabled for the alpha() function but not for omega().

Example: Disabling optimization for a single function

#pragma optimize("", off)

alpha() {

...

}

#pragma optimize("", on)

omega() {

...

}

In the following example, optimizations are disabled for both the alpha() and omega()functions.

Example: Disabling optimization for all functions

#pragma optimize("", off)

alpha() {

...

}

omega() {

...

}

optimization_level Pragma

Use the optimization_level pragma to control optimization for one function or all functions after its first occurrence. The pragma uses the following syntax:

Syntax

#pragma [intel|GCC] optimization_level n

where intel|GCC indicates the interpretation to use, and n is an integer value specifying an optimization level; valid values are:

  • 0 same optimizations as -O0

  • 1 same optimizations as -O1

  • 2 same optimizations as -O2

  • 3 same optimizations as -O3

For more information on the optimizations levels, see Enabling Automatic Optimizations.

On Linux* and Mac OS*, the scope of the optimization restriction can be affected by arguments passed to the -pragma-optimization-level compiler option as explained in the following table.

Syntax

Behavior

#pragma intel optimization_level n

Applies pragma only to the next function, using the specified optimization level, regardless of the argument passed to the -pragma-optimization-level option.

#pragma GCC optimization_level n

or

#pragma GCC optimization_level reset

Applies pragma to all subsequent functions, using the specified optimization level, regardless of the argument passed to the -pragma-optimization-level option.

Specifying reset reverses the effect of the most recent #pragma GCC optimization_levelstatement, by returning to the optimization level previously specified.

#pragma optimization_level n

Applies either the intel or GCC interpretation. Interpretation depends on argument passed to the -pragma-optimization-level option.

On Windows*, the pragma always uses the intel interpretation; the pragma is applied only the next function.

General behavior

Using the Intel interpretation of the pragma, you can use this pragma to restrict optimization for a specific function while optimizing the remaining application using a different, higher optimization level. For example, if you specify -O3 (Linux and Mac OS) for the application and specify #pragma optimization_level 1, the marked function will be optimized at the -O1 option level, while the remaining application will be optimized at the higher level. Place the pragma immediately before the function being affected, as shown below.

Example

#pragma intel optimization_level 1

gamma() {

...

}

If you are using the GCC interpretation, you can place the pragma in any location prior to the functions being affected.

In general, the pragma will optimize the function at the level specified for n; however, certain compiler optimizations, like Interprocedural Optimization (IPO), are not enabled or disabled during translation unit compilation. For example, if you enable IPO and a specific optimization level, IPO is enabled even for the function targeted by this pragma; however, IPO might not be fully implemented regardless of the optimization level specified at the command line. The reverse is also true.

Optimization Restriction Support:

'via Blog this'

No comments:

Post a Comment