Oracle Blog

Compiling and performance

Using pragma opt

The Studio compiler has the ability to control the optimisation level that is applied to particular functions in an application. This can be useful if the functions are designed to work at a specific optimisation level, or if the application fails at a particular optimisation level, and you need to figure out where the problem lies.

The directive needs to be inserted into the source file. The format of the directive is
#pragma opt /level/ (/function/). This needs to be inserted into the code before the start of the function definition, but after the function header.

The code needs to be compiled with the flag -xmaxopt=level. This sets the maximum optimisation level for all functions in the file - including those tagged with #pragma opt.

We can see this in action using the following code snippet. This contains two identical functions, both return the square of a global variable. However, we are using #pragma opt to control the optimisation level of the function f().

Wow, loading d in o4 then copying it to o3 using o3=o4>>0 is one fancy way of loading a variable into o3... Good thing we're not writing so much asm by hand anymore if that's what efficient code looks like (and I haven't seen the new sparc T3 instructions documented anywhere).

It's a shift right zero which is used to sign extend to the upper bits of the destination register. A more efficient snippet of code would be to use an signed load (rather than the unsigned load actually used).

About

Darryl Gove is a senior engineer in the Solaris Studio team, working on optimising applications and benchmarks for current and future processors. He is also the author of the books:Multicore Application ProgrammingSolaris Application ProgrammingThe Developer's EdgeFree Download