public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* Question about __builtin_ia32_mfence and memory barriers
@ 2013-06-04 22:58 dw
  2013-06-04 23:52 ` Ian Lance Taylor
  0 siblings, 1 reply; 10+ messages in thread
From: dw @ 2013-06-04 22:58 UTC (permalink / raw)
  To: gcc-help

The discussion below assumes 64bit code on an i386 processor.

My understanding is that the way to do a memory barrier in gcc is:

     asm ("" ::: "memory");

This creates a ReadWriteBarrier, but no processor fence.  To create a 
processor fence, you could do something like

     __builtin_ia32_mfence();

This will generate an mfence instruction, but (assembly code inspection 
suggests) no memory barrier.  I thought about just putting one after the 
other:

     asm ("" ::: "memory");
__builtin_ia32_mfence();

And this leads to my questions:

1) Am I right that __builtin_ia32_mfence() does not generate a memory 
barrier?
1) Is this "two statement thing" guaranteed to be safe?  Could the 
optimizer re-order instructions moving code in between the two? (Yes, I 
realize that the asm statement doesn't actually generate any output.  
But given my understanding of how the compiler processes code, I believe 
the question is still valid).
2) If it is not guaranteed to be safe, what is the use of 
__builtin_ia32_mfence()?  What value is there in preventing the 
*processor* from executing statements out of order if the *compiler* is 
just going to move them around?

I expect this would always work:

     asm ("mfence" ::: "memory");

But I would rather use the builtins if possible.

dw

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2013-06-13  3:44 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2013-06-04 22:58 Question about __builtin_ia32_mfence and memory barriers dw
2013-06-04 23:52 ` Ian Lance Taylor
2013-06-05  2:45   ` dw
2013-06-05  4:30     ` Ian Lance Taylor
2013-06-12  8:15   ` dw
2013-06-12 19:01     ` Ian Lance Taylor
2013-06-13  2:55       ` dw
2013-06-13  3:01       ` Chung-Ju Wu
2013-06-13  3:25         ` Ian Lance Taylor
2013-06-13  3:44         ` dw

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).