Re: [RFC] LKMM: Add volatile_if()

From: Marco Elver
Date: Wed Jun 09 2021 - 08:44:47 EST


On Tue, 8 Jun 2021 at 17:30, Segher Boessenkool
<segher@xxxxxxxxxxxxxxxxxxx> wrote:
> On Tue, Jun 08, 2021 at 01:22:58PM +0200, Peter Zijlstra wrote:
> > Works for me; and note how it mirrors how we implemented volatile_if()
> > in the first place, by doing an expression wrapper.
> >
> > __builtin_ctrl_depends(expr) would have to:
> >
> > - ensure !__builtin_const_p(expr) (A)
>
> Why would it be an error if __builtin_constant_p(expr)? In many
> programs the compiler can figure out some expression does never change.
> Having a control dependency on sometthing like that is not erroneous.
>
> > - imply an acquire compiler fence (B)
> > - ensure cond-branch is emitted (C)
>
> (C) is almost impossible to do. This should be reformulated to talk
> about the effect of the generated code, instead.
>
> > *OR*
> >
> > - ensure !__builtin_const_p(expr); (A)
> > - upgrade the load in @expr to load-acquire (D)
>
> So that will only work if there is exactly one read from memory in expr?
> That is problematic.
>
> This needs some work.

There is a valid concern that something at the level of the memory
model requires very precise specification in terms of language
semantics and not generated code. Otherwise it seems difficult to get
compiler folks onboard. And coming up with such a specification may
take a while, especially if we have to venture in the realm of the
C11/C++11 memory model while still trying to somehow make it work for
the LKMM. That seems like a very tricky maze we may want to avoid.

An alternative design would be to use a statement attribute to only
enforce (C) ("__attribute__((mustcontrol))" ?). The rest can be
composed through existing primitives I think (the compiler barriers
need optimizing though), which should give us ctrl_depends().

At least for Clang, it should be doable: https://reviews.llvm.org/D103958

Thanks,
-- Marco