mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: "stern@rowland.harvard.edu" <stern@rowland.harvard.edu>
To: Boqun Feng <boqun.feng@gmail.com>
Cc: Jonas Oberhauser <jonas.oberhauser@huawei.com>,
	"paulmck@kernel.org" <paulmck@kernel.org>,
	"parri.andrea@gmail.com" <parri.andrea@gmail.com>,
	"will@kernel.org" <will@kernel.org>,
	"peterz@infradead.org" <peterz@infradead.org>,
	"npiggin@gmail.com" <npiggin@gmail.com>,
	"dhowells@redhat.com" <dhowells@redhat.com>,
	"j.alglave@ucl.ac.uk" <j.alglave@ucl.ac.uk>,
	"luc.maranget@inria.fr" <luc.maranget@inria.fr>,
	"akiyks@gmail.com" <akiyks@gmail.com>,
	"dlustig@nvidia.com" <dlustig@nvidia.com>,
	"joel@joelfernandes.org" <joel@joelfernandes.org>,
	"urezki@gmail.com" <urezki@gmail.com>,
	"quic_neeraju@quicinc.com" <quic_neeraju@quicinc.com>,
	"frederic@kernel.org" <frederic@kernel.org>,
	"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>
Subject: Re: [PATCH v2] tools: memory-model: Make plain accesses carry dependencies
Date: Thu, 8 Dec 2022 16:37:35 -0500	[thread overview]
Message-ID: <Y5JZH0GGiqA53Ozf@rowland.harvard.edu> (raw)
In-Reply-To: <Y4+rn0lsrdCB3pMj@boqun-archlinux>

On Tue, Dec 06, 2022 at 12:52:47PM -0800, Boqun Feng wrote:
> On Tue, Dec 06, 2022 at 12:46:58PM -0800, Boqun Feng wrote:
> > Thank you, Alan! One question though, can a "smart" compiler optimize
> > out the case below, with the same logic?
> > 
> > 	void P0(int *x, int *y, int *a)
> > 	{
> > 		int r1, r2;
> > 
> > 		r1 = READ_ONCE(*x); // A
> > 
> > 		*a = r1 & 0xffff; // B
> > 
> > 		r2 = *a & 0xffff0000; // C
> > 
> > 		WRITE_ONCE(*y, r2); // D
> > 
> > 	}
> > 
> > I think we have A ->data B ->rfi C ->data D, however a "smart" compiler
> > can figure out that r2 is actually zero, right? And the code get
> > optimized to:
> > 
> > 	r1 = READ_ONCE(*x);
> > 	r2 = 0;
> > 	WRITE_ONCE(*y, r2);
> > 	*a = r1 & 0xffff;
> > 
> > and break the dependency.

Yes, that could happen.

> > I know that our memory model is actually unware of the differences of
> > syntatics dependencies vs semantics syntatics, so one may argue that in
> > the (data; rfi) example above the compiler optimization is outside the
> > scope of LKMM, but won't the same reasoning apply to the (addr; rfi)
> > example from you? The WRITE_ONCE() _syntatically_ depends on load of
> > a[r1], therefore even a "smart" compiler can figure out the value, LKMM
> 
> I guess it should be that r2 (i.e. the load of a[r1]) _syntatically_
> depends on the value of r1.

Yes.  But more to the point, the LKMM already has this problem for 
ordinary dependencies.  If you do:

	r1 = READ_ONCE(*x);
	r2 = r1 & 0x0000ffff;
	r3 = r2 & 0xffff0000;
	WRITE_ONCE(*y, r3);

then the LKMM will think there is a dependency (because there is a 
_syntactic_ dependency), but the compiler is likely to realize that 
there isn't a _semantic_ dependency and will destroy the ordering.

We warn people about this possibility, and the same warning applies to 
dependencies carried by plain accesses.  So I don't think this is a 
reason to object to Jonas's carries-dep relation.

Alan

  reply	other threads:[~2022-12-08 21:37 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-02 12:51 Jonas Oberhauser
2022-12-02 14:44 ` Akira Yokosawa
2022-12-02 18:49 ` Boqun Feng
2022-12-03 11:58   ` Jonas Oberhauser
2022-12-03 19:02     ` Paul E. McKenney
2022-12-03 20:14       ` Boqun Feng
2022-12-03 20:40         ` Paul E. McKenney
2022-12-03 20:34       ` stern
2022-12-03 20:44         ` Paul E. McKenney
2022-12-03 20:52           ` Boqun Feng
2022-12-03 21:16             ` Paul E. McKenney
2022-12-03 21:32           ` stern
2022-12-03 23:11             ` Paul E. McKenney
2022-12-04  0:15               ` Jonas Oberhauser
2022-12-04  8:33                 ` Boqun Feng
2022-12-05 13:42                   ` Jonas Oberhauser
2022-12-05 16:18                     ` stern
2022-12-06 20:46                       ` Boqun Feng
2022-12-06 20:52                         ` Boqun Feng
2022-12-08 21:37                           ` stern [this message]
2022-12-05 19:49                     ` Boqun Feng
2022-12-07  1:43                     ` Boqun Feng
2022-12-08 21:06                       ` Boqun Feng
2022-12-09  0:01                         ` Paul E. McKenney

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y5JZH0GGiqA53Ozf@rowland.harvard.edu \
    --to=stern@rowland.harvard.edu \
    --cc=akiyks@gmail.com \
    --cc=boqun.feng@gmail.com \
    --cc=dhowells@redhat.com \
    --cc=dlustig@nvidia.com \
    --cc=frederic@kernel.org \
    --cc=j.alglave@ucl.ac.uk \
    --cc=joel@joelfernandes.org \
    --cc=jonas.oberhauser@huawei.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=luc.maranget@inria.fr \
    --cc=npiggin@gmail.com \
    --cc=parri.andrea@gmail.com \
    --cc=paulmck@kernel.org \
    --cc=peterz@infradead.org \
    --cc=quic_neeraju@quicinc.com \
    --cc=urezki@gmail.com \
    --cc=will@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®