On Fri, Jun 4, 2021 at 9:37 AM Peter Zijlstra <peterz@xxxxxxxxxxxxx> wrote: > > > > > Why is "volatile_if()" not just > > > > #define barier_true() ({ barrier(); 1; }) > > > > #define volatile_if(x) if ((x) && barrier_true()) > > Because we weren't sure compilers weren't still allowed to optimize the > branch away. This isn't about some "compiler folks think". The above CANNOT be compiled any other way than with a branch. A compiler that optimizes a branch away is simply broken. Of course, the actual condition (ie "x" above) has to be something that the compiler cannot statically determine is a constant, but since the whole - and only - point is that there will be a READ_ONCE() or similar there, that's not an issue. The compiler *cannot* just say "oh, I'll do that 'volatile asm barrier' whether the condition is true or not". That would be a fundamental compiler bug. It's as if we wrote if (x) y++; and the compiler went "Oh, I'll just increment 'y' unconditionally by one, I'm sure the programmer doesn't mind, the conditional on 'x' is immaterial". No. That's not a C compiler. That's a stinking piece of buggy shit. The compiler has to honor the conditional. In that "y++" case, a compiler can decide to do it without a branch, and basically rewrite the above as y += !!x; but with a "volatile asm", that would be a bug. Of course, we might want to make sure that the compiler doesn't go "oh, empty asm, I can ignore it", but if that's the case then it's not about "volatile_if()" any more, at that point it's "oh, the compiler broke our 'barrier()' implementation", and we have bigger issues. Linus