Re: [RFC] LKMM: Add volatile_if()

Paul E. McKenney
2021-06-06T23:37:35Z

On Sun, Jun 06, 2021 at 03:26:16PM -0500, Segher Boessenkool wrote:
> On Sun, Jun 06, 2021 at 01:11:53PM -0700, Linus Torvalds wrote:
> > On Sun, Jun 6, 2021 at 12:56 PM Segher Boessenkool
> > <segher@kernel.crashing.org> wrote:
> > >
> > > Yes, I know. But it is literally the *only* way to *always* get a
> > > conditional branch: by writing one.
> >
> > The thing is, I don't actually believe you.
>
> Fortune favours the bold!
>
> > The barrier() thing can work - all we need to do is to simply make it
> > impossible for gcc to validly create anything but a conditional
> > branch.
>
> And the only foolproof way of doing that is by writing a branch.
>
> > If either side of the thing have an asm that cannot be combined, gcc
> > simply doesn't have any choice in the matter. There's no other valid
> > model than a conditional branch around it (of some sort - doing an
> > indirect branch that has a data dependency isn't wrong either, it just
> > wouldn't be something that a sane compiler would generate because it's
> > obviously much slower and more complicated).
>
> Or push something to the stack and return. Or rewrite the whole thing
> as an FSM. Or or or.
>
> (And yes, there are existing compilers that can do both of these things
> on some code).
>
> > We are very used to just making the compiler generate the code we
> > need. That is, fundamentally, what any use of inline asm is all about.
> > We want the compiler to generate all the common cases and all the
> > regular instructions.
> >
> > The conditional branch itself - and the instructions leading up to it
> > - are exactly those "common regular instructions" that we'd want the
> > compiler to generate. That is in fact more true here than for most
> > inline asm, exactly because there are so many different possible
> > combinations of conditional branches (equal, not equal, less than,..)
> > and so many ways to generate the code that generates the condition.
> >
> > So we are much better off letting the compiler do all that for us -
> > it's very much what the compiler is good at.
>
> Yes, exactly.
>
> I am saying that if you depend on that some C code you write will result
> in some particular machine code, without actually *forcing* the compiler
> to output that exact machine code, then you will be disappointed. Maybe
> not today, and maybe it will take years, if you are lucky.
>
> (s/forcing/instructing/ of course, compilers have feelings too!)

OK, I will bite...

What would you suggest as a way of instructing the compiler to emit the
conditional branch that we are looking for?

Thanx, Paul