public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: "Paul E. McKenney" <paulmck@linux.ibm.com>
To: Richard Biener <richard.guenther@gmail.com>
Cc: Akshat Garg <xkspr7@gmail.com>,
	       Ramana Radhakrishnan <ramana.gcc@googlemail.com>,
	       gcc mailing list <gcc@gcc.gnu.org>
Subject: Re: Doubts regarding the _Dependent_ptr keyword
Date: Wed, 03 Jul 2019 16:34:00 -0000	[thread overview]
Message-ID: <20190703163355.GQ26519@linux.ibm.com> (raw)
In-Reply-To: <3C6A1B8D-33B6-4CC3-B88C-3A547601D4AD@gmail.com>

On Wed, Jul 03, 2019 at 05:47:56PM +0200, Richard Biener wrote:
> On July 3, 2019 5:14:58 PM GMT+02:00, "Paul E. McKenney" <paulmck@linux.ibm.com> wrote:
> >On Wed, Jul 03, 2019 at 12:39:41AM +0530, Akshat Garg wrote:
> >> On Tue, Jul 2, 2019 at 8:40 PM Paul E. McKenney
> ><paulmck@linux.ibm.com>
> >> wrote:
> >> 
> >> > On Tue, Jul 02, 2019 at 02:15:55PM +0100, Ramana Radhakrishnan
> >wrote:
> >> > > On Tue, Jul 2, 2019 at 1:38 PM Paul E. McKenney
> ><paulmck@linux.ibm.com>
> >> > wrote:
> >> > >
> >> > > >
> >> > > > Once a user-created non-dependent pointer is assigned to, it is
> >OK to
> >> > > > break the dependency.
> >> > >
> >> > > Ok, that's good.
> >> > > >
> >> > > > Or am I missing the point here?
> >> > >
> >> > > I was just trying to make sure we were on the same page. I wonder
> >if
> >> > > marking this volatile would be sufficient for prototyping. I
> >suspect
> >> > > we would need another flag somewhere which someone with gimple
> >> > > knowledge might be able to help us with.
> >> >
> >> > I expect that marking it as volatile would do the trick.  ;-)
> >> >
> >> >                                                         Thanx, Paul
> >> >
> >> So, marking this pointer as volatile will not allow the compiler to
> >> modify/optimize the statements, the pointer is appearing in. And we
> >don't
> >> need to push any other code inside any of the passes. Due to this, we
> >want
> >> to automatically say those dependent pointers are volatile and
> >introduce a
> >> new flag for this. Am I getting you guys correctly? Kindly, let me
> >know?
> >
> >While I suspect that this might work, it would suppress way more
> >optimizations than would be good.  For but one example, consider:
> >
> >	_Dependent_ptr int *p;
> >
> >	p = atomic_load_explicit(gp, memory_order_consume);
> >	a = p->a;
> >	b = p->b;
> >
> >If "p" is volatile, then the compiler will be prevented from keeping
> >it in a register, which would not make people coding fastpaths at
> >all happy.  ;-)
> >
> >Still, use of volatile might be a good technique for prototyping and
> >analysis of _Dependent_ptr.
> 
> With this example can you quickly summarize what kind of guarantees _Dependent_ptr gives and how a compiler
> Could possibly break those? 

First I suppose I should fix the bug in the above code.  Or one of the
bugs, at least.  :-/

	struct foo {
		int a;
		int b;
	};

	_Dependent_ptr struct foo *p;

	p = atomic_load_explicit(gp, memory_order_consume);
	a = p->a;
	b = p->b;

And then let me tweak the example a bit.  For the first tweak:

	struct foo {
		int a;
		int b;
	};

	struct foo default_foo = { .a = 42, .b = 43 };
	int *gp = &default_foo;

	...

	_Dependent_ptr int *p;

	p = atomic_load_explicit(gp, memory_order_consume);
	a = p->a;
	b = p->b;

Suppose that the compiler used feedback-driven optimization, and noticed
that the value of gp was almost always &default_foo.  The compiler might
decide to transform the last three lines as follows:

	p = atomic_load_explicit(gp, memory_order_consume);
	if (p == &default_foo) {
		a = default_foo.a;
		b = default_foo.b;
	} else {
		a = p->a;
		b = p->b;
	}

Now, as long as the value of gp had remained &default_foo for the full
duration of execution, no harm done.  But suppose the following code
was executing concurrently with the above transformed code:

	struct foo *q;

	q = malloc(sizeof(*q));
	assert(q);
	q->a = 1729;
	q->b = 1730;
	atomic_store_explicit(gp, q, memory_order_release);
	do_something();
	default_foo.a = 1;
	default_foo.b = 2;
	atomic_store_explicit(gp, &default_foo, memory_order_release);

In this case, if the memory_order_consume() came just after the pointer
was reset to &default_foo, it is possible that the transformed code
would set "a" to 42 and "b" to 43, which might not be what the guy
writing the code wanted to happen.

One of the purposes of _Dependent_ptr is to prevent this transformation.

This transformation can also happen if the developer's code contained a
comparison to &default_foo -- an ARM or PowerPC compiler backend, upon
seeing two pointers containing the same bits, would likely consider the
two pointers as being interchangeable, and thus might do the dereferences
using the copy that was not tagged with the hardware dependencies.

There are quite a few other examples.  The C++ standards committee
working papers shown below go through a number of them, in case the
above example is not convincing.  Or you could tell me what you would
like to see, and I would attempt to find/create a suitable example.

Does that help, or am I missing your point?

							Thanx, Paul

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p0098r0.pdf
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0190r4.pdf
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0462r1.pdf

> Richard. 
> 
> >
> >							Thanx, Paul
> >
> >> Akshat
> >> 
> >> >
> >> > > regards
> >> > > Ramana
> >> > >
> >> > > >
> >> > > >                                                         Thanx,
> >Paul
> >> > > >
> >> > > > > Ramana
> >> > > > > >>
> >> > > > > >>
> >> > > > > >> > Does this sounds like a workable plan for ? Let me know
> >your
> >> > thoughts. If this sounds good then, we can do this for all the
> >> > optimizations that may kill the dependencies at somepoint.
> >> > > > > >> >
> >> > > > > >> > -Akshat
> >> > > > >
> >> > > >
> >> > >
> >> >
> >> >
> 

  reply	other threads:[~2019-07-03 16:34 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <CAMEQ7Yw5mV+zoDV-kmOnWoy4+_CiDeUyJmipkkrTGhxninp3Hw@mail.gmail.com>
     [not found] ` <20190617023256.GJ26519@linux.ibm.com>
     [not found]   ` <CAMEQ7Yxp-o+4dPd_o=eaPYqpV1n7KfqMS_Un1U1n9-ut_HK_uw@mail.gmail.com>
     [not found]     ` <20190618211927.GY26519@linux.ibm.com>
     [not found]       ` <CAJA7tRYJPaFuvR+bC40kGdKOLVTHEFnD1SkBqF6oUcpv-bLMbw@mail.gmail.com>
     [not found]         ` <CAMEQ7YyN72aMAEsuKW4GQua_Vyi8Wd6F6BKcTo8dS_JcLm2TvQ@mail.gmail.com>
     [not found]           ` <CAMEQ7YxT4mwNvgr_iqxKAJE=yeuOmhcNkHL1ET-84hwv9uMwdQ@mail.gmail.com>
     [not found]             ` <CAJA7tRZVhVtabJLgWJ3BK3HhR2pf+X0fiG+sF=y2Y=PkWg8eTQ@mail.gmail.com>
     [not found]               ` <CAMEQ7YyLp0YymRiYD4O_1A=YswbbDBh0gQz9kGtw4t8mfvU4Jg@mail.gmail.com>
     [not found]                 ` <20190619164145.GJ26519@linux.ibm.com>
     [not found]                   ` <20190620140600.GA15142@linux.ibm.com>
     [not found]                     ` <CAMEQ7Yx=dg0TWcXKA8eZk=RCN+az116Y9whKGQSB3fRVZwbvQw@mail.gmail.com>
     [not found]                       ` <CAJA7tRbj6eoOj_fr+Nnm1rS=kdc0jPxkb77AoPCFrJEaaPsCSA@mail.gmail.com>
2019-06-25 16:20                         ` Akshat Garg
2019-07-02  0:29                           ` Akshat Garg
2019-07-02  0:59                             ` Paul E. McKenney
2019-07-02  1:42                               ` nick
2019-07-02 15:36                               ` Jason Merrill
2019-07-02 17:53                                 ` Richard Biener
2019-07-03 15:09                                   ` Paul E. McKenney
2019-07-02 19:37                                 ` Akshat Garg
2019-07-17 10:54                                 ` Akshat Garg
2019-07-22  0:27                                   ` Akshat Garg
2019-07-22  8:41                                     ` Richard Biener
2019-07-22  8:54                                       ` Akshat Garg
2019-07-22  9:46                                         ` Richard Biener
2019-07-23 20:11                                           ` Akshat Garg
2019-07-23 20:50                                             ` Akshat Garg
2019-07-02 10:22                             ` Ramana Radhakrishnan
2019-07-02 10:39                               ` Akshat Garg
2019-07-02 11:01                                 ` Ramana Radhakrishnan
2019-07-02 12:38                                   ` Paul E. McKenney
2019-07-02 13:16                                     ` Ramana Radhakrishnan
2019-07-02 15:09                                       ` Paul E. McKenney
2019-07-02 19:09                                         ` Akshat Garg
2019-07-03 15:16                                           ` Paul E. McKenney
2019-07-03 15:48                                             ` Richard Biener
2019-07-03 16:34                                               ` Paul E. McKenney [this message]
2019-07-04 11:00                                                 ` Richard Biener
2019-07-04 17:40                                                   ` Paul E. McKenney
2019-07-04 18:09                                                     ` Jakub Jelinek
2019-07-04 23:08                                                       ` Akshat Garg
2019-07-05 11:20                                                         ` Richard Biener
2019-07-05 15:38                                                           ` Akshat Garg
     [not found]                                                         ` <20190705014806.GM26519@linux.ibm.com>
     [not found]                                                           ` <CAMEQ7YyrjkGVhnLUCXMrbyicPkRZkpZvH_3KQ56cNh_6+Tr4iQ@mail.gmail.com>
     [not found]                                                             ` <CAMEQ7YzOqU68kQN99g_zhwJUkNaJa9q_Dv9pue+4NFK4rYubFg@mail.gmail.com>
     [not found]                                                               ` <20190707141928.GE26519@linux.ibm.com>
     [not found]                                                                 ` <CAMEQ7YyuzvN_OzU4oMJGaFMBpnQwkCt4=ZTo-AmSmvsr2dBOMw@mail.gmail.com>
2019-07-07 21:44                                                                   ` Akshat Garg

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=20190703163355.GQ26519@linux.ibm.com \
    --to=paulmck@linux.ibm.com \
    --cc=gcc@gcc.gnu.org \
    --cc=ramana.gcc@googlemail.com \
    --cc=richard.guenther@gmail.com \
    --cc=xkspr7@gmail.com \
    /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;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).