Hi,

I'm trying to compile a program using inline asm with optimizations and I got my inline asm functions thrown out by the optimizer although I declared them as having side effects (by stating a memory clobber).
I wrote the following toy program to demonstrate the problem:

----------------------------------------------

import std.stdio;
import gcc.attribute;

@attribute("forceinline") ulong exchangeAndAdd(shared ulong *counter, ulong addition) { ulong retVal = void; // we don't want it initialized when dmd is used
      asm {
        "
          mov  %2, %0             ;
          lock                    ;
          xadd %0, (%1)           ;
        ":
        "=&r"(retVal) :
        "r"(counter), "r"(addition) :
        "memory";
      }
      return retVal;
}

ulong func1() {
  shared ulong a = 9;
  exchangeAndAdd(&a, 3);
  return a;
}

void main()
{
  ulong b;
  b = func1();
  writeln(b);
}

----------------------------------------------

Compiling it with and without optimizations gives a different output:
/opt/gdc/bin/gdc ./test.d -o/tmp/a.out
/tmp/a.out
12
/opt/gdc/bin/gdc -O3 -g ./test.d -o/tmp/a.out
/tmp/a.out
9

using gdb we can see that the call to exchangeAndAdd() is being thrown out in the optimized version:

(gdb) disas _D4test5func1FZm
Dump of assembler code for function _D4test5func1FZm:
   0x0000000000406460 <+0>:       movq   $0x9,-0x8(%rsp)
   0x0000000000406469 <+9>:       mov    -0x8(%rsp),%rax
   0x000000000040646e <+14>:      retq
End of assembler dump.


Is this a compiler bug or am I doing something wrong ?
btw, I'm using gdc 4.9.0.

thanks in advance,
Maor

Reply via email to