News for hackers. Stuff that matters.
Re:ARM and Lock-Free Programming by runeks on Tuesday December 01, 2020 @01:25AM (#25262919)
I appreciate learning more about what, exactly, ARM's "weaker memory model" constitutes. It's clearer to me after reading this article.

I wonder how much the gain in performance of e.g. Apple's M1 chip, compared to an x86 CPU, can be attributed to this weaker constraint. Given that the M1 can outperform an x86 CPU even when emulating x86 code, perhaps it's not much.

Also, I suspect programming languages that are immutable by default will gain a larger advantage using ARM's weaker memory model, as the compiler can more often safely let the CPU perform reordering (due to not having to wait for a mutable variable being updated until it can execute a subsequent line of code which depends on this updated variable).

Re:ARM and Lock-Free Programming by Tuna-Fish on Tuesday December 01, 2020 @03:22AM (#25263461)
The reason it doesn't help them that much in performance is that the difference in the way x86/ARM manage memory is not that ARM reorders more and x86 reorders less, but that ARM reorders openly and x86 reorders things behind your back but makes sure everything is where you think they should be if/when you look.

This is the reason that the relatively primitive memory model of x86 has turned out to be so successful despite the consensus in the field being that it's way to restrictive for performance way back in the 90's. It turns out that the x86 memory model is an easy target for a complex reordering backend to provide as a "facade", between what is actually happening the in CPU's own L1 and buffers and what everything else thinks it's doing.

The only way weaker memory orderings benefit is that they don't need as large buffers and queues on the backend, as they can retire memory operations earlier. Except that if you just implement the ARM memory model as specified and without a similar facade, it will actually lose to modern x86, because the x86 chip can make use of the freedom provided by it's machinery to reorder even more without any of it ever being visible to the software. So you end up implementing a similar system as the x86, with only very minor gains from the fact that you can sometimes properly retire accesses earlier and thus get a little more use out of your buffers.

ARM, especially 64-bit ARM, has genuine advantages over x86. (Notably, decode.) The weak memory model is not one of them, outside the very weakest and tiniest cores (where it allows some reordering with a tiny load-store backend).

Re:ARM and Lock-Free Programming by gpderetta on Tuesday December 01, 2020 @05:05AM (#25263931)
Yes. In particular for a long time x86 had much cheaper memory barriers than the competition. Not only store release and load acquires were free, but sequentially consistent barriers were also fairly cheap. Intel had to add optimizations to make them fast much earlier, while weaker RISCs could get away with expensive barriers for much longer.

Now apparently M1 has very cheap atomics and barriers, to it is doing as much tracking and speculation as an x86 CPU. But probably they can get away with tracking fewer memory operations, which might improve both performance and power usage.

Re:ARM and Lock-Free Programming by my123 on Tuesday December 01, 2020 @03:44AM (#25263580)
And on Arm cores, you aren't obligated to provide a weak memory model. Some Arm vendors provide cores with stronger memory models, mainly TSO in Arm server land and sequential consistency for NVIDIA in-house CPUs.
Re:ARM and Lock-Free Programming by haberman on Tuesday December 01, 2020 @01:58PM (#25268514)
Retiring memory operations earlier must have some benefit though? If data is being passed between threads, earlier retirement of a store means the data is available earlier to other CPUs. For a mutex, it means the mutex is released sooner, no?
Re:ARM and Lock-Free Programming by MaxBarraclough on Tuesday December 01, 2020 @05:30AM (#25264050)
Given all of this, what approach does RISC-V take?

The RISC-V is presumably very 'greenfield' regarding its memory model, I imagine they've made their decision based on all this knowledge.

Re:ARM and Lock-Free Programming by my123 on Tuesday December 01, 2020 @06:02AM (#25264191)
A weak memory model. See https://riscv.org/wp-content/uploads/2018/05/14.25-15.00-RIS... (by Dan Lustig at Nvidia, NV is both very involved in Arm and RISC-V).
Re:ARM and Lock-Free Programming by zozbot234 on Tuesday December 01, 2020 @08:17AM (#25264839)
It has a weak memory model by default, but you can opt in to TSO (like x86) as an optional extension.
Re:ARM and Lock-Free Programming by my123 on Tuesday December 01, 2020 @08:19AM (#25264848)
And for Arm too, you can ship stronger memory models up to SC.

There's also the RCpc extension for memory accesses w/ another memory model there.

Re:ARM and Lock-Free Programming by jabl on Tuesday December 01, 2020 @01:05PM (#25267841)
Is RCpc still multi-copy atomic? (Which AFAIK is a guaranteed behavior of the Aarch64 memory model)
Re:ARM and Lock-Free Programming by my123 on Wednesday December 02, 2020 @01:59PM (#25279982)
No difference on that front, yep.
Re:ARM and Lock-Free Programming by deadmutex on Tuesday December 01, 2020 @03:45AM (#25263584)
Thank you for your insightful comment. Do you mind going a bit more into the decode advantage?
Re:ARM and Lock-Free Programming by my123 on Tuesday December 01, 2020 @03:48AM (#25263593)
32-bit fixed size instructions, that are aligned. That allows you to have 8-wide decoders for example.

On x86, you can't really do this/didn't happen because the instructions are 1 to 15 bytes long, and you'll need to decode the prior instruction at least in part to determine where your current instr starts. (currently 4-wide decoders are available for x86 uArches)

Re:ARM and Lock-Free Programming by mhh__ on Tuesday December 01, 2020 @04:18AM (#25263733)
Fixed width instructions are much easier to decode at the same time as their neighbours.

The density is a tradeoff you have to think about, but X86 is not a clean ISA.

For a more alien example, the mill cpu has very long variable length instructions for density reasons but because it's specifically designed around that and not needing any implicit parallelism they can use tricks to find instruction boundaries much more easily.

Re:ARM and Lock-Free Programming by Dylan16807 on Tuesday December 01, 2020 @04:25AM (#25263762)
The biggest thing for quickly finding instruction boundaries is probably not having prefixes, which is pretty straightforward.
Re:ARM and Lock-Free Programming by cesarb on Tuesday December 01, 2020 @07:27AM (#25264574)
Not having suffixes helps too. AFAIK, on x86 to find the length of an instruction, first you have to decode the instruction itself (the decoding can vary depending on the prefixes) to know if it is followed by a modRM byte, then decode the modRM byte to know whether it is followed by an immediate or not and how big that immediate is, and only then can you know where the next instruction is. The modRM byte (and the SIB byte) can be thought of as a "suffix" of sorts to the instruction.

Contrast for instance with RISC-V, where the first byte of every instruction has everything necessary to know the instruction length (and you only need 2 bits of that byte unless your core supports instructions longer than 4 bytes).

Re:ARM and Lock-Free Programming by l1k on Tuesday December 01, 2020 @10:04AM (#25265755)
I've you want to learn even more, take a look at Documentation/memory-barriers.txt in the Linux kernel source tree:

https://www.kernel.org/doc/html/latest/staging/index.html#me...

Re:ARM and Lock-Free Programming by CalChris on Tuesday December 01, 2020 @06:08AM (#25264217)
The M1 is outperforming an x86 CPU when emulating x86 code by translating that code into ARMv8 instructions. So if the x86 code was designed using x86's strong memory model and is then dynamically translated into ARMv8 and run with a weak memory model, there will be problems, no? Maybe Rosetta 2 handles this; that would be impressive.
Re:ARM and Lock-Free Programming by floatboth on Tuesday December 01, 2020 @06:14AM (#25264257)
No, Apple doesn't run the translated code with a weak memory model, Rosetta 2 toggles the total store ordering (which the M1 supports) on when running its code.

Microsoft's translator (and QEMU?) does insert extra barriers to make it run on a weak memory model, because they are designed to run on HW without TSO.

Re:ARM and Lock-Free Programming by CalChris on Tuesday December 01, 2020 @06:28AM (#25264325)
Yes, Apple added a CPU mode bit. From the article:

  Note: writing an x86 emulator on ARM forces you to deal with this in a particularly messy way because you never know when reordering will be a problem so you typically have to insert a lot of memory barriers. Or you can follow Apple’s strategy and add a CPU mode that enforces x86/x64 memory ordering, turning that on when emulating.
I'm a little surprised+happy the ARM architectural license allowed this.
Re:ARM and Lock-Free Programming by floatboth on Tuesday December 01, 2020 @06:40AM (#25264373)
Why wouldn't it? A stronger memory model doesn't break anything in the ISA. You can always go stronger, it's not a problem even for native applications, it would just make them slightly slower.

What is surprising with Apple is that they were allowed to add extra instructions for machine learning related math (yeah, CPU instructions in addition to a dedicated separate "neural engine" on the chip: https://news.ycombinator.com/item?id=24471699)

Re:ARM and Lock-Free Programming by CalChris on Tuesday December 01, 2020 @10:30PM (#25272945)
After doing a little web research, I'll conclude that it's impossible to know from the outside what the restrictions are on ARM architectural licensees other than they have to pass the ARM compatibility test suite. New instructions (probably not), new modes (looks like), dunno (definitely).

However, Apple is one of the founding ARM partners and it is rumored they have a special license (not all licenses are created equal). Clearly they are doing this mode thing. Whether Samsung could get away with the exact same thing is a good question. I'm going to guess that NVidia can.

Re:ARM and Lock-Free Programming by miohtama on Tuesday December 01, 2020 @08:37AM (#25264967)
How does the toggle work? Is it per thread, CPU affinity, per process, CPU flag or something totally different?
Re:ARM and Lock-Free Programming by abbeyj on Tuesday December 01, 2020 @10:53AM (#25266205)
It seems to be a CPU flag. This is managed by the kernel so that it is on for Rosetta 2 processes and off for everything else. See https://github.com/saagarjha/TSOEnabler
Re:ARM and Lock-Free Programming by saagarjha on Tuesday December 01, 2020 @02:48AM (#25263299)
The M1 chip can do uncontended atomics almost "for free", and offers a mode where it firms up its memory ordering without much performance loss, so I am not actually sure how much this gets them.
Re:ARM and Lock-Free Programming by KMag on Tuesday December 01, 2020 @05:12AM (#25263962)
Well, it gets them enough that they bothered implementing all of the circuitry for both memory models and an extra bit of thread state to switch between them. I'm sure they did a bunch of simulation before going with that decision. I'm sure they did some simulations before going down that road and found that it was worth it vs. just going with a more strict x86-like memory model all the time.
Re:ARM and Lock-Free Programming by cma on Tuesday December 01, 2020 @10:10AM (#25265818)
> I'm sure they did some simulations before going down that road and found that it was worth it vs. just going with a more strict x86-like memory model all the time.

Or they didn't want developers to rely on it so they can remove it in the future when they deprecate x64 (they deprecated x86 so it is only a matter of time).

Re:ARM and Lock-Free Programming by KMag on Tuesday December 01, 2020 @10:41PM (#25273008)
> Or they didn't want developers to rely on it

If simulations show that it's not currently worth the effort, but they want to keep the option open, that implies that they believe some technological change in the future may make the relaxed memory model bear fruit.

Actually, now that I think about it, another option is that the weaker memory model is more advantageous in lower power implementations, like iWatch, and preventing developers from relying on the stronger memory model available in M1 reduces the number of bugs in iWatch applications that share code with iPhone apps or OSX apps.