4 ms·
There is a yolo way that actually works, which would be to change it to a relaxed load followed by an acquire signal fence.
by bonzini 9mo ago
There is a yolo way that actually works, which would be to change it to a relaxed load followed by an acquire signal fence.
- loeg 9mo agoIs that any better than just using an acquire load?
- gpderetta 9mo agoIt is cheaper on ARM and POWER. But I'm not sure it is always safe. The standard has very complex rules for consume to make sure that the compiler didn't break the dependencies. edit: and those rules where so complex that compilers decided where not implementable or not worth it.
- bonzini 9mo agoThe rules were there to explain what optimizations remained possible. Here no optimization is possible at the compiler level, and only the processor retains freedom because we know it won't use it. It is nasty, but it's very similar to how Linux does it (volatile read + __asm__("") compiler barrier).
- comex 9mo agoThis is still unsound (in both C and Rust), because the compiler can break data dependencies by e.g. replacing a value with a different value known to be equal to it. A compiler barrier doesn't prevent this. (Neither would a hardware barrier, but with a hardware barrier it doesn't matter if data dependencies are broken.) The difficulty of ensuring the compiler will never break data dependencies is why compilers never properly implemented consume. Yet at the same time, this kind of optimization is actually very rare in non-pathological code, which is why Linux has been able to get away with assuming it won't happen.
- gpderetta 9mo agoIn principle a compiler could convert the data dependency into to a control dependency (for example, after PGO after checking against the most likely value), and those are fairly fragile. I guess in practice mainstream compilers do not do it and relaxed+signal fence works for now, but the fact that compilers have been reluctant to use it to implement consume means that they are reluctant to commit to it. In any case I think you work on GCC, so you probably know the details better than me. edit: it seems that ARM specifically does not respect control dependencies. But I might misreading the MM.
- jojomodding 9mo agoshouldn't it be preceded?
- gpderetta 9mo agoNo, you want to sequence any subsequent loads to after the acquire/consume load.