comp.lang.ada
 help / color / mirror / Atom feed
From: Robert Eachus <rieachus@comcast.net>
Subject: Re: Portable memory barrier?
Date: Mon, 8 May 2017 08:56:08 -0700 (PDT)
Date: 2017-05-08T08:56:08-07:00	[thread overview]
Message-ID: <e87ac3c3-8390-4d1d-979d-0bcc7e4d3dde@googlegroups.com> (raw)
In-Reply-To: <oep7mj$1g3f$1@gioia.aioe.org>

One sentence summary: The CPU will play lots of tricks behind your back, but in such a way you can pretend your code works as you intended.  (Er, after you get the bugs out.)

On Monday, May 8, 2017 at 3:45:26 AM UTC-4, Dmitry A. Kazakov wrote:

> I don't believe either of the sequences can get reordered by the CPU, 
> but it is only belief and common sense.

Silly rabbit, Trix are for kids.  Oops, silly programmer, common sense has nothing to do with modern CPUs.

If I didn't make it clear, modern CPUs analyze which pending instructions can be done NOW, and they select some subset of those operations. This is called OoO (out of order) scheduling.  In addition, the CPU has a register file with lots of extra registers. It uses register renaming to insure that instructions "see" the right arguments, even if there are four or five different copies of the index in the register file.  This allows the CPU to put only one copy in the write pipe, and run the code faster than a naive counting of writes to memory suggests.  Finally, superscalar CPUs convert the instructions into micro-ops (name depends on CPU vendor) and then several of these micro-ops belonging to multiple instructions get executed all at once.

The CPU tries hard to eliminate reads and writes to main memory which are slow.  By putting the memory controller on the CPU, and having it look into the cache no matter the source of the request, even volatile memory can be virtualized.  (There are a few pages in memory which can't be virtualized, because they contain things like the real-time clock.  But that is a detail known to the CPU.)

All of this is done with state not associated with any real clock, but with the instructions as they execute.  If you think of a program counter as a pointer to the next instruction to be executed, and defining a state; modern CPUs have multiple states in play at the same time--and out of order.

Since in all this the CPU arranges things so that all of the outputs occur in the expected order. If you halt the CPU, then flush the cache, THEN there will be one point in the programmer's view of the code that corresponds to the current state--in other words, debuggers work.  (Assuming you look at the code as generated, or turn off optimization in the compiler.)

As far as this issue is concerned, the CPU implements the ISA, so you get the effects you expect from a program.  All of this is on an "as if" basis, and in fact the CPU is seldom in a reproducible state.

Is any of this important to a high-level language programmer?  Not really, unless they are working in hard real time. (Which I did.)  If you program in assembler, or more likely work on a compiler back-end, knowing which instructions fit together is important.  Even more important is coding so that an instruction that references a new cache line in memory is followed by as many instructions as possible that don't use that value.  Some times you run into cases where you want to take advantage of write combining or some other trick involving the write pipe--but today, smart compiler optimization is about accesses to main memory and not much else.


  reply	other threads:[~2017-05-08 15:56 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 [this message]
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
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