comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Portable memory barrier?
Date: Wed, 10 May 2017 20:02:20 -0500
Date: 2017-05-10T20:02:20-05:00	[thread overview]
Message-ID: <of0d6s$2q1$1@franka.jacob-sparre.dk> (raw)
In-Reply-To: df31d3f8-636f-4702-affe-d5a0aa0c898a@googlegroups.com

"Robert Eachus" <rieachus@comcast.net> wrote in message 
news:df31d3f8-636f-4702-affe-d5a0aa0c898a@googlegroups.com...
On Tuesday, May 9, 2017 at 3:57:50 PM UTC-4, Randy Brukardt wrote:
> "Robert Eachus" <rieachus@comcast.net> wrote in message
> news:077e7f6a-5a7b-4b88-a16f-7672aec18a17@googlegroups.com...
> ...
>> >The compiler will play some games, and certainly will leave Write_Index 
>> >in
>> >a register if possible.
>>
>> It better not is Write_Index is Atomic.
>
>First, if FIFO has Atomic_Components, then it is not possible. 
>Atomic_Components would be
>overkill, but I haven't seen those declarations here.  If FIFO has just 
>Volatile_Components (or
>no attribute) then this is possible.  But what is "this"?

????

Write_Index needs to be atomic in order for the OP's algorithm to work. 
Atomic objects fall under C.6(20/5) -- all reads and updates are included in 
the external effect of the program. Optimizing them out (in any way) is 
wrong (excepting of course a true as-if optimization, but that doesn't make 
any sense in this case). In particular, saving the value in a register is 
useless, because you still have to read the object again at the next 
reference.

>Sloppy wording, I should have said "keep a copy of Write_Index".  The write 
>to Write_Index
>will occur to cache on modern systems where the whole system is 
>cache-coherent.  But that is
>a detail.  What I was specifically referring to was that there are two 
>subexpressions
>Write_Index+1.  As far as I can tell, if there are no synchronization 
>points between them, the
>(overall system including) the compiler can do one read of Write_Index, 
>then evaluate the
>expression once--or twice.

Definitely not (again assuming Write_Index is atomic). One has to make the 
reads of memory for an atomic object, saving copies is wrong. And there is 
no real value to eliminating the +1 -- in-register operations are 
essentially free on modern architechtures.

Since the rest of your discussion is based on an incorrect premise, I'm 
going to skip it. (Other than to say, if you need the fastest possible code, 
don't make anything volatile or atomic! They're for synchronization, which 
necessarily is going to be slower than the usual code, since it has to be 
predictable.)

...
>> Only the compiler vendor need worry about this level of discussion.
>> Honestly, I doubt anyone else is qualified. (I'm not, and I AM a compiler
>> vendor... ;-)
>
>I used to be a compiler writer, on at least three Ada compilers, but the 
>deep level
>understanding of what is going on comes from my work on radar systems. 
>There
>access times in microseconds are important, and sometimes in nanoseconds. 
>(I think
>the worst real-time requirement I ever saw was for 200 nanoseconds. Ouch!)

I know that (and so am I), but my complaint was that the discussion isn't 
helpful to the OPs question (which is how to do this in hopefully portable 
Ada); it's the compiler's job to get this right and one hopes that the only 
time anyone needs to worry about this is if they have a bug that they think 
is caused by the compiler doing this wrong. (And this is when AdaCore 
support contracts pay for themselves, because you can dump the problem on 
them and let them figure it out. And it's the sort of problem that makes 
those contracts so expensive, because it is HARD work!!)

                               Randy.





  reply	other threads:[~2017-05-11  1:02 UTC|newest]

Thread overview: 65+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-06  2:23 Portable memory barrier? Jere
2017-05-06  8:47 ` Jeffrey R. Carter
2017-05-06 14:17   ` Jere
2017-05-06 19:08     ` Dennis Lee Bieber
2017-05-06 19:26     ` Jeffrey R. Carter
2017-05-06 19:41     ` Jeffrey R. Carter
2017-05-06 20:42       ` Niklas Holsti
2017-05-09 19:49     ` Randy Brukardt
2017-05-09 22:07       ` Jere
2017-05-11  1:14         ` Randy Brukardt
2017-05-10 18:28       ` Shark8
2017-05-11  1:17         ` Randy Brukardt
2017-05-11 16:23           ` Jeffrey R. Carter
2017-05-07 20:18 ` Robert Eachus
2017-05-08  7:45   ` Dmitry A. Kazakov
2017-05-08 15:56     ` Robert Eachus
2017-05-08 16:22       ` Dmitry A. Kazakov
2017-05-08 18:39         ` Robert Eachus
2017-05-08 19:18         ` Robert Eachus
2017-05-08 21:09           ` Dmitry A. Kazakov
2017-05-08 23:24             ` Robert Eachus
2017-05-09  0:30               ` Jere
2017-05-09  4:02                 ` Robert Eachus
2017-05-09  4:32                 ` Robert Eachus
2017-05-09  4:44                   ` Robert Eachus
2017-05-09 22:26                   ` Jere
2017-05-09 20:01                 ` Randy Brukardt
2017-05-09 19:57               ` Randy Brukardt
2017-05-10  0:51                 ` Jere
2017-05-10  5:25                   ` J-P. Rosen
2017-05-10 22:56                     ` Jere
2017-05-11  7:36                       ` Dmitry A. Kazakov
2017-05-13 20:25                         ` Jere
2017-05-10  7:13                   ` Dmitry A. Kazakov
2017-05-10 16:45                     ` Robert Eachus
2017-05-10 17:28                       ` Simon Wright
2017-05-10 23:21                     ` Jere
2017-05-11  0:47                       ` Randy Brukardt
2017-05-13 20:11                         ` Jere
2017-05-15 22:33                           ` Randy Brukardt
2017-05-10 23:30                     ` Jere
2017-05-11  0:38                     ` Randy Brukardt
2017-05-10 16:38                   ` Jeffrey R. Carter
2017-05-10 23:40                     ` Jere
2017-05-10 16:19                 ` Robert Eachus
2017-05-11  1:02                   ` Randy Brukardt [this message]
2017-05-11  1:51                     ` Robert Eachus
2017-05-15 22:45                       ` Randy Brukardt
2017-05-08 20:29         ` Niklas Holsti
2017-05-08 21:09           ` Dmitry A. Kazakov
2017-05-09  4:34             ` Niklas Holsti
2017-05-09  6:16               ` Niklas Holsti
2017-05-09  8:34                 ` Dmitry A. Kazakov
2017-05-09 20:18                 ` Randy Brukardt
2017-05-09 20:10           ` Randy Brukardt
2017-05-09  0:05         ` Jere
2017-05-09  8:26           ` Dmitry A. Kazakov
2017-05-09 19:53         ` Randy Brukardt
2017-05-09 20:27           ` Dmitry A. Kazakov
2017-05-11  0:35             ` Randy Brukardt
2017-05-11  8:24               ` Dmitry A. Kazakov
2017-05-15 22:53                 ` Randy Brukardt
2017-05-18 17:44                   ` Dmitry A. Kazakov
2017-05-18 21:01                     ` Randy Brukardt
2017-05-19  7:54                       ` Dmitry A. Kazakov
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox