Re: [PATCH v2] tools: memory-model: Make plain accesses carry dependencies

From: stern@xxxxxxxxxxxxxxxxxxx
Date: Mon Dec 05 2022 - 11:18:19 EST


On Mon, Dec 05, 2022 at 01:42:46PM +0000, Jonas Oberhauser wrote:
> > Besides, could you also explain a little bit why only "data;rfi" can be "carry-dep" but "ctrl;rfi" and "addr;rfi" cannot? I think it's because there are special cases when compilers can figure out a condition being true or an address being constant therefore break the dependency
>
> Oh, good question. A bit hard for me to write down the answer clearly
> (which some people will claim that I don't understand it well myself,
> but I beg to differ :) :( :) ).
>
> In a nutshell, it's because x ->data [Plain] ->rfi y ->... z fulfils
> the same role as storing something in a register and then using it in
> a subsequent computation; x ->ctrl y ->... z (and ->addr) don't. So
> it's not due to smart compilers, just the fact that the other two
> cases seem unrelated to the problem being solved, and including them
> might introduce some unsoundness (not that I have checked if they do).

More can be said here. Consider the following simple example:

void P0(int *x, int *y)
{
int r1, r2;
int a[10];

r1 = READ_ONCE(*x);
a[r1] = 1;
r2 = a[r1];
WRITE_ONCE(*y, r2);
}

There is an address dependency from the READ_ONCE to the plain store in
a[r1]. Then there is an rfi and a data dependency to the WRITE_ONCE.

But in this example, the WRITE_ONCE is _not_ ordered after the
READ_ONCE, even though they are linked by (addr ; rfi ; data). The
compiler knows that the value of r1 does not change between the two
plain accesses, so it knows that it can optimize the code to be:

r1 = READ_ONCE(*x);
r2 = 1;
WRITE_ONCE(*y, r2);
a[r1] = r2;

And then the CPU can execute the WRITE_ONCE before the READ_ONCE. This
shows that (addr ; rfi) must not be included in the carry-deps relation.

You may be able to come up with a similar argument for (ctrl ; rfi),
although it might not be quite as clear.

Alan