openmp optimization

I'm using openmp and want to insert an inline assembly code. But I don't want the compiler to optimize my inline assembly code. This is my test code:

#include <stdio.h>

#include <omp.h>

void magic_break (void)

{

asm (搒ethi 0x40000, %g0?;

}

main () {

int tid;

magic_break;

#pragma omp parallel private(tid)

{

tid = omp_get_thread_num();

printf("Hello World from thread = %d\n", tid);

}

magic_break;

return 0;

}

I compiled it using: cc -xopenmp test.c

While compiling, the optimization level will automatically change to -O3 to support -xopenmp, but my inline assembly code is being optimized as a "nop" after compiling. I don't want "sethi 0x40000, %g0" to be changed to "nop".

Does anyone know how to keep "sethi 0x40000, %g0" as what it is while compile with -O3 for openmp?

Thank you very much in advance!!!!

[967 byte] By [binla] at [2007-11-27 0:10:37]
# 1

Try separating the "magic_break" function and the main program in separate files. Then

you will have to compile them separately, but the compiler's optimizer will not be able

to "optimize away" your code. Right now, my guess is that the compiler can see your

routine since it is in the same file, and "helpfully" optimize it for you.

I thought of another thing that could be happening also. With UltraSPARC a nop instruction

is just a special case of sethi with the destination register %g0. (As you have done)

So maybe the disassembler displays your sethi as a nop instead.

jeremyweeka at 2007-7-11 16:12:16 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2
Put magic_break into a separate file and compile it without optimization. Better yet, put it in an assembler source file and just assemble it.
clamage45a at 2007-7-11 16:12:16 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3

Thank you very much for your reply.

You are right, that sethi with %g0 is indeed a nop. I need to run the program in a simulator, and the simulator needs to see this sethi instruction to take some actions. But now the compiler changes it to nop, and the simulator couldn't recognize it any more.

I tried to put the "magic_break" function in a separate file and compiled them separately. But it still doesn't work. The simulator couldn't see the "sethi" instruction. So I guess the compiler still change it to "nop".

Is there anyway to avoid the disassembler displaying sethi as a nop?

binla at 2007-7-11 16:12:16 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 4

What is the specific sethi instruction that is used for "nop"?

As a test, create an assembler file which has both your special instruction

and a "nop", and assemble it, and the use "dis" to disassemble it.

See if the two instructions are really different.

Also, if you determine in the end that the compiler really is changing one

instruction to a different one please file a bug using bugs.sun.com

We should fix. We've done a lot of improvements in asm() for Sun Studio 12

but I'm sure we have lots of bugs left.

--chris

ChrisQuenellea at 2007-7-11 16:12:16 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 5

> I'm using openmp and want to insert an inline

> assembly code. But I don't want the compiler to

> optimize my inline assembly code. This is my test

> code:

>

> #include <stdio.h>

> #include <omp.h>

>

> void magic_break (void)

> {

>asm (搒ethi 0x40000, %g0?;

>

>

> main () {

> int tid;

>

> magic_break;

>

> #pragma omp parallel private(tid)

>{

> tid = omp_get_thread_num();

>printf("Hello World from thread = %d\n", tid);

> }

>

> magic_break;

>

> return 0;

> }

>

> I compiled it using: cc -xopenmp test.c

>

> Does anyone know how to keep "sethi 0x40000, %g0" as

> what it is while compile with -O3 for openmp?

A better way to do this is with inline template files.

Create a file, magic_break.il, that looks like this:

.inline magic_break,0

.volatile

sethi 0x40000, %g0

.nonvolatile

.end

This inline template will act like the body of a function.

The .volatile/.nonvolatile will prevent the code from being optimized away.

Then include a declaration for magic_break like this:

void magic_break (void);

It can be either in the .c file or from a header file.

Then put a call to magic_break() in your code where you want it.

Then compile it like:

cc -O -xopenmp test.c magic_break.il

Hope that helps,

Peter.

peter.damrona at 2007-7-11 16:12:16 > top of Java-index,Development Tools,Solaris and Linux Development Tools...