Re: [RFC] LKMM: Add volatile_if()
From: Segher Boessenkool <hidden>
Date: 2021-06-04 16:14:27
Also in:
linux-arch, lkml
Hi! On Fri, Jun 04, 2021 at 12:12:07PM +0200, Peter Zijlstra wrote:
With optimizing compilers becoming more and more agressive and C so far refusing to acknowledge the concept of control-dependencies even while we keep growing the amount of reliance on them, things will eventually come apart.
Yes, C is still not a portable assembler.
There have been talks with toolchain people on how to resolve this; one suggestion was allowing the volatile qualifier on branch statements like 'if', but so far no actual compiler has made any progress on this.
"if" is not a "branch statement".
quoted hunk ↗ jump to hunk
--- a/arch/powerpc/include/asm/barrier.h +++ b/arch/powerpc/include/asm/barrier.h@@ -80,6 +80,19 @@ do { \ ___p1; \ }) +#ifndef __ASSEMBLY__ +/* Guarantee a conditional branch that depends on @cond. */ +static __always_inline bool volatile_cond(bool cond) +{ + asm_volatile_goto("and. %0,%0,%0; bne %l[l_yes]" + : : "r" (cond) : "cc", "memory" : l_yes); + return false; +l_yes: + return true; +} +#define volatile_cond volatile_cond +#endif
"cmpwi" is ever so slightly better than "and.". And you can write "cr0" instead of "cc" more explicitely (it means the same thing though). I didn't find a description of the expected precise semantics anywhere in this patch. This however is the most important thing required here! Segher