Googles appar
Huvudmeny

Post a Comment On: cbloom rants

"07-31-10 - GCC Scheduling Barrier"

15 Comments -

1 – 15 of 15
Blogger cbloom said...

BTW the most compelling argument for signed int wrapping that I know is simply to write this :

a + b + c

what does that do? Is

(a + b) + c

the same as

a + (b + c)

? If signed ints don't wrap, the answer is no.

July 31, 2010 at 8:36 PM

Comment deleted

This comment has been removed by the author.

August 1, 2010 at 8:25 AM

Comment deleted

This comment has been removed by the author.

August 1, 2010 at 8:29 AM

Blogger   said...

I've never noticed this phenomenon on PowerPC architectures. Of course I've never looked for it either. I have done tons of inline assembly though.

I take it that this has occurred for you and is causing problems?

August 1, 2010 at 8:46 AM

Blogger cbloom said...

"I take it that this has occurred for you and is causing problems? "

No, so far as I can tell *in practice* on our current consoles, it actually is a scheduling barrier.

However, it disturbs me to make that part of my library when the GCC devs specifically say it is not a barrier.

I can just imagine the nightmare threading bugs that would occur if an instruction was moved there.

Sigh.

August 1, 2010 at 10:47 AM

Blogger Brian said...

I thought the GCC barrier was:
asm volatile("":::"memory");

August 2, 2010 at 1:13 PM

Anonymous Anonymous said...

asm volatile("":::"memory") will indeed force the barrier, but it also seems to discard previously loaded memory and to reload them, degrading performances. There might be a non-documented asm volatile("") - see http://gcc.gnu.org/ml/gcc/2003-04/msg01180.html

August 2, 2010 at 1:28 PM

Blogger cbloom said...

sylvain, in that very thread they say :

[quote]
> I observed that simply asm volatile (""), i.e., "old style asm" *does*
> do what I want.

That seems wrong to me. In any case, you definitely shouldn't
rely on this.
[/quote]

which is what I've been seeing. Currently asm volatile *does* seem to be what we want, but they are very defensive about not gauranteeing it. Sigh.

August 2, 2010 at 1:57 PM

Blogger Brian said...

So what semantics do you want for such a statement? A full memory barrier is fine, it has well defined semantics. Just not reordering statements but allowing the compiler to still use values it has stored has pretty messy semantics.

August 3, 2010 at 12:53 AM

Blogger cbloom said...

" So what semantics do you want for such a statement? A full memory barrier is fine, it has well defined semantics. Just not reordering statements but allowing the compiler to still use values it has stored has pretty messy semantics. "

The "memory" mark on an asm volatile is not a "memory barrier" ; it tells the compiler that all memory may have been trashed so everything has to be reloaded. That is needlessly inefficient. Basically it makes all your variables into volatiles. Note that it's also *insufficient* , if you actually want a real memory barrier, it does not generate one. So it's hard for me to imagine when you would want that behavior.

What I want is simply a "no compiler reorder". Of course that's pretty useless when you're just writing standard C since it has undefined memory semantics, but when I'm doing things that I know have very specific actual memory semantics, all I need to know is that the compiler won't reorder the things that I have very carefully ordered.

For example, LoadAcquire on x86 can be implemented as

x = *ptr
CompilerReadBarrier

August 3, 2010 at 1:03 AM

Blogger Brian said...

So what you really want is to denote a set of data that you need to explicitly control the access order to (and for which your ordering rules should apply to) and then provide barriers that limit reordering of accesses to just that state and no other state.
On 32-bit x86, is the "memory" designation really that expensive. At worse you will spill a handful of registers...

August 3, 2010 at 11:56 AM

Blogger cbloom said...

" So what you really want is to denote a set of data that you need to explicitly control the access order to (and for which your ordering rules should apply to) and then provide barriers that limit reordering of accesses to just that state and no other state. "

Well, yes, in theory, though in practice that's too much intellectual burden, so we just say "order everything".

" On 32-bit x86, is the "memory" designation really that expensive. At worse you will spill a handful of registers. "

Yeah I suspect that in fact the memory volatile is not a huge hit, but more investigation is needed.

August 4, 2010 at 10:00 AM

Blogger Brian said...

If you don't want to make the compiler spill all of the state out of registers at a scheduling barrier (i.e. "memory" designation), you have to tell it what state matters.

Otherwise, it might inline some other methods or something and the CSE pass might then eliminate your read by reusing a stored value.

August 4, 2010 at 11:03 PM

Blogger cbloom said...

No.

August 5, 2010 at 8:59 AM

Blogger Brian said...

I'm probably misunderstanding what semantics you want then. You don't want something that prevents moving reads forward. What you really want is a barrier that prevents moving stores down or up, pushing reads down, or moving volatile reads up?

The real problem with GCC is it wasn't designed to support this and we're really just subverting the mechanism for inline assembly to kind of get what we want.

August 5, 2010 at 12:47 PM

You can use some HTML tags, such as <b>, <i>, <a>

This blog does not allow anonymous comments.

Comment moderation has been enabled. All comments must be approved by the blog author.

You will be asked to sign in after submitting your comment.