Topic: Faking a dependency, for consume-semantics


Author: Al Grant <algrant@myrealbox.com>
Date: Wed, 5 Jun 2013 23:13:32 -0700 (PDT)
Raw View
Sometimes we want to fake a data-dependency in order
to use consume-semantics - we can use idioms like x & 0
or x ^ x, and by 1.10#9 the implementation must treat this
as carrying a dependency:

  x = ready.load(std::memory_order_consume);
  if (x)
     y = *(&buffer + (x & 0));   // fake dependency on x

I couldn't find a standard way to do this, i.e. a counterpart
to std::kill_dependency.

By my reading of 1.10#9, we can do this:

  template<typename P, typename V>
  P& with_dependency(P& p, V) { return p; }

  x = ready.load(std::memory_order_consume);
  if (x)
    y = with_dependency(buffer, x);

We could even define it as variadic:

  template<typename P>
  P& with_dependency(P& p, ...) { return p; }

Can we indeed create a data-dependency this way?

If so, it seems that compilers must not only avoid optimizing
away explicit fake data-dependencies (of the x&0 kind), ensuring
that they appear as data-dependent instruction sequences
on memory-models where consume-semantics needs this,
but actually synthesize such code to create data-dependencies
on values whose only "use" is to be passed as unused operands
to a function.


--
[ comp.std.c++ is moderated.  To submit articles, try posting with your ]
[ newsreader.  If that fails, use mailto:std-cpp-submit@vandevoorde.com ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html                      ]