atomicModifyMutVar#: cas() is not inlined

Ömer Sinan Ağacan omeragacan at
Wed Jul 27 09:15:16 UTC 2016

This is from definition of stg_atomicModifyMutVarzh(): (for threaded runtime)

      x = StgMutVar_var(mv);
      StgThunk_payload(z,1) = x;
      (h) = ccall cas(mv + SIZEOF_StgHeader + OFFSET_StgMutVar_var, x, y);
      if (h != x) { goto retry; }

cas() is defined in includes/stg/SMP.h like this:

    cas(StgVolatilePtr p, StgWord o, StgWord n)
        return __sync_val_compare_and_swap(p, o, n);

I think this is a function we want to make sure to inline everywhere, right?
It's compiled to a single instruction on my x86_64 Linux laptop.

    >>> disassemble cas
    Dump of assembler code for function cas:
       0x0000000000027240 <+0>:     mov    %rsi,%rax
       0x0000000000027243 <+3>:     lock cmpxchg %rdx,(%rdi)
       0x0000000000027248 <+8>:     retq
    End of assembler dump.

But it seems like it's not really inlined in Cmm functions:

    >>> disassemble stg_atomicModifyMutVarzh
    Dump of assembler code for function stg_atomicModifyMutVarzh:
       0x0000000000046738 <+120>:   callq  0x27240 <cas>
    End of assembler dump.

I guess the problem is that we can't inline C code in Cmm, but I was wondering
if this is important enough to try to fix maybe. Has anyone here looked at some
profiling info to see how much time spent on this cas() call when threads are
blocked in `atomicModifyIORef` etc?

More information about the ghc-devs mailing list