News for hackers. Stuff that matters.
Re:What's up with this new memory_order_consume memory order? by kazinator on Thursday May 25, 2023 @01:12AM (#36067243)
I would hope that this paradigm requires all participating accesses to be annotated:

  int i = load(p, memory_order_consume);
  if (i == 3) {
    int y = load(x[i], memory_order_consume);
  }
it seems like bad design to require ordering between plain loads and annotated loads. We want to identify the two locations that are important, so the compiler can reorder the others however it sees fit, without regard as to their placement w.r.t. those two.
Re:What's up with this new memory_order_consume memory order? by gpderetta on Thursday May 25, 2023 @03:45AM (#36068078)
No, loads from x need not be consume nor even need to be atomic. If the thread that last wrote to x and then i never touches x again, there is no possible data race. This is for example the case of for initialization guards.
Re:What's up with this new memory_order_consume memory order? by kazinator on Thursday May 25, 2023 @11:34AM (#36072084)
* * *
Re:What's up with this new memory_order_consume memory order? by gpderetta on Thursday May 25, 2023 @12:04PM (#36072498)
If p is like by a lock, the compiler won't reorder the access outside of the lock but it can still take into account that i is a known constant to simplify the address computation of x[i] into x[3]. This is perfectly safe for a mutex protected variable as the mutex acquire will, in addition to the compiler barrier, provide the required hardware barriers.

This is is catastrophic, if, instead of relying on an hardware barrier, we rely on a load-load dependency chain originating from i; rewriting x[i] into x[3] break the chain and now, while the compiler might still not reorder the access, the hardware very much can.

Re:What's up with this new memory_order_consume memory order? by kazinator on Thursday May 25, 2023 @01:30PM (#36073586)
> the hardware very much can.

If its name is DEC Alpha? ;)

Re:What's up with this new memory_order_consume memory order? by gpderetta on Thursday May 25, 2023 @02:32PM (#36074327)
No, alpha will reorder even if you have a dependency chain (so it will need a barrier no matter what). Power and ARM will reorder only if the chain is broken.