[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <Y44ZxWYQkMN60a1E@rowland.harvard.edu>
Date: Mon, 5 Dec 2022 11:18:13 -0500
From: "stern@...land.harvard.edu" <stern@...land.harvard.edu>
To: Jonas Oberhauser <jonas.oberhauser@...wei.com>
Cc: Boqun Feng <boqun.feng@...il.com>,
"paulmck@...nel.org" <paulmck@...nel.org>,
"parri.andrea@...il.com" <parri.andrea@...il.com>,
"will@...nel.org" <will@...nel.org>,
"peterz@...radead.org" <peterz@...radead.org>,
"npiggin@...il.com" <npiggin@...il.com>,
"dhowells@...hat.com" <dhowells@...hat.com>,
"j.alglave@....ac.uk" <j.alglave@....ac.uk>,
"luc.maranget@...ia.fr" <luc.maranget@...ia.fr>,
"akiyks@...il.com" <akiyks@...il.com>,
"dlustig@...dia.com" <dlustig@...dia.com>,
"joel@...lfernandes.org" <joel@...lfernandes.org>,
"urezki@...il.com" <urezki@...il.com>,
"quic_neeraju@...cinc.com" <quic_neeraju@...cinc.com>,
"frederic@...nel.org" <frederic@...nel.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>
Subject: Re: [PATCH v2] tools: memory-model: Make plain accesses carry
dependencies
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
Powered by blists - more mailing lists