Re: [RFC] LKMM: Add volatile_if()

From: Segher Boessenkool
Date: Sun Jun 06 2021 - 14:45:16 EST


On Sun, Jun 06, 2021 at 11:04:49AM -0700, Linus Torvalds wrote:
> if (READ_ONCE(a)) {
> barrier();
> WRITE_ONCE(b,1);
> } else {
> barrier();
> WRITE_ONCE(b, 1);
> }
>
> and currently because gcc thinks "same exact code", it will actually
> optimize this to (pseudo-asm):
>
> LD A
> "empty asm"
> ST $1,B
>
> which is very much NOT equivalent to
>
> LD A
> BEQ over
> "empty asm"
> ST $1,B
> JMP join
>
> over:
> "empty asm"
> ST $1,B
>
> join:
>
> and that's the whole point of the barriers.

You didn't use a barrier with these semantics though. There is nothing
in that code that guarantees a branch.

> See, but it VIOLATES the semantics of the code.

The code violates your expectations of the code.

> You can't join those two empty asm's (and then remove the branch),
> because the semantics of the code really aren't the same any more if
> you do. Truly.

You truly should have written a branch in tthe asm if you truly wanted
a branch instruction.


Segher