Live data from Hacker News

There's No Such Thing as “Implicitly Atomic”

belkadan.com

21–30 of 47 posts

Re: There's No Such Thing as “Implicitly Atomic”

#21

If a language is self-hosting that means it must guarantee translation of code that is not explicitly atomic in that language to instructions which are explicitly atomic in the CPU ISA to implement language explicit atomics. I don't feel that the article take is useful or informative and amounts to gatekeeping ISA parallelism.

Have you ever heard of "CMPXCHG8B"?

never heard of it mate

Re: There's No Such Thing as “Implicitly Atomic”

#23
post #15

> Even that isn’t guaranteed. If you don’t say “atomic”, the compiler might decide to split up a store to optimize for speed or code size! This isn’t hypothetical; Greg Parker ran into this with libobjc. Maybe I'm tired and lack imagination, but please enlighten me. How can splitting up a store speed something up or reduce the code size? Assuming it is aligned and on a 32bit native CPU.

The linked thread doesn't say exactly how, but I can construct some way in my head: Final 32-bit store effectively stores two 16-bit values or'ed together as high-16 and low-16. You can get this without any shifting and simple addition and the proper values. If these two sixteen bit values are calculated along two very different paths which have different lengths, the compiler might opt to spill one of them early, on…

Or if I understood it correctly, there are platforms like CHERI which have metadata about memory, for example to detect invalid access. This metadata might race with the data itself.

Re: There's No Such Thing as “Implicitly Atomic”

#27
post #15

> Even that isn’t guaranteed. If you don’t say “atomic”, the compiler might decide to split up a store to optimize for speed or code size! This isn’t hypothetical; Greg Parker ran into this with libobjc. Maybe I'm tired and lack imagination, but please enlighten me. How can splitting up a store speed something up or reduce the code size? Assuming it is aligned and on a 32bit native CPU.

The value being written was composed from multiple inputs. Instead of instructions to combine those inputs into a register and store them, it performed multiple partial stores of the values it was composed from. Think two u16s being ORed into a u32.

Maybe it chose this optimization due to register pressure, and not wanting to spill values that were still going to be used?

Re: There's No Such Thing as “Implicitly Atomic”

#28
post #24
post #2

Also, I half remember an architecture that essentially put no upper bounds on when a normal write would be visible to other cores without a barrier.

Alpha?

I can't quite remember and my Google Fu is failing me at the moment.

I think it was something about how L1 wouldn't flush down to the inclusive L2 until commanded to, so external reads hit the stale L2.

Alpha being a pain here would be on brand, but I think this was a different arch.

Re: There's No Such Thing as “Implicitly Atomic”

#29
post #4

The FreeBSD kernel only runs on platforms where 32-bit sized and aligned ordinary loads and stores are atomic; this is a requirement that it demands of the hardware. This may not be (extremely) portable, or may not work for Swift, or userspace under some very weird runtime environments, but it is true of the vast majority of CPU hardware out there.

It is important to separate the hardware and the language. The article is about Swift and it doesn't matter what the hardware guarantees exist for 32-bit loads and stores. The is the compiler's job. If the language doesn't guarantee that a particular action will product an atomic 32-bit load or store than it may be compiled to something different.

This is why it is important to tell the language what you mean. Even in C non-atomic access don't have these guarantees. It may seem pointless because in 99% of cases that "non-atomic" store compiles to the exact same instructions as a relaxed store. But that is just because you are getting lucky. The language doesn't guarantee that and with the atomic store the compiler is well within its right to emit something different (like two stores, spilling dirty values, ...).

Re: There's No Such Thing as “Implicitly Atomic”

#30
post #22

I assume Rust concurrency guarantees will prevent races and have this covered. Except of course if you use unsafe. Right?

Will prevent data races , yes.

Of course I meant data races. Or am I barking up the wrong tree?

And of course only if you use safe Rust.

Post reply on HN