From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.99.103.135 with SMTP id b129mr491736pgc.127.1494304356643; Mon, 08 May 2017 21:32:36 -0700 (PDT) X-Received: by 10.157.35.104 with SMTP id k37mr1327728otd.14.1494304356598; Mon, 08 May 2017 21:32:36 -0700 (PDT) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!news.eternal-september.org!feeder.eternal-september.org!news.glorb.com!c26no1647360itd.0!news-out.google.com!m134ni180itb.0!nntp.google.com!c26no1641564itd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Mon, 8 May 2017 21:32:36 -0700 (PDT) In-Reply-To: <8a968aae-79e4-421b-ba4b-e0a9a33ce0db@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=2601:191:8303:2100:5985:2c17:9409:aa9c; posting-account=fdRd8woAAADTIlxCu9FgvDrUK4wPzvy3 NNTP-Posting-Host: 2601:191:8303:2100:5985:2c17:9409:aa9c References: <0fc56bf7-1cfa-4776-9c47-a573db315c5f@googlegroups.com> <7b0c08eb-be62-4d14-ae99-cad038ad0a62@googlegroups.com> <077e7f6a-5a7b-4b88-a16f-7672aec18a17@googlegroups.com> <8a968aae-79e4-421b-ba4b-e0a9a33ce0db@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <7b1446ef-8e5f-4f3f-803d-b5a2f47bae0e@googlegroups.com> Subject: Re: Portable memory barrier? From: Robert Eachus Injection-Date: Tue, 09 May 2017 04:32:36 +0000 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:46717 Date: 2017-05-08T21:32:36-07:00 List-Id: Argh! I just accidentally deleted a long post due apparently to a bug in t= he new version of Windows 10. Anyway: On Monday, May 8, 2017 at 8:30:02 PM UTC-4, Jere wrote: > On Monday, May 8, 2017 at 7:24:42 PM UTC-4, Robert Eachus wrote: > > On Monday, May 8, 2017 at 5:09:45 PM UTC-4, Dmitry A. Kazakov wrote: > >=20 > > > This strategy does not prevent reordering writes: > > >=20 > > > FIFO (Write_Index + 1) :=3D Element; > > > Write_Index :=3D Write_Index + 1; > > >=20 > > > into > > >=20 > > > Write_Index :=3D Write_Index + 1; > > > FIFO (Write_Index) :=3D Element; > > >=20 > > > which would be a bug. That requires reordering writes, which is not allowed in x86 and AMD64. Th= ere are fence instructions on RISC machines that may be needed. There are = three fence instructions on x86. They are rarely used, and should be non-e= xistent in user code. This is an instance where you need a fence on x86, o= r just to change a couple of instructions to RMW (Atomic): https://bartoszm= ilewski.com/2008/11/05/who-ordered-memory-fences-on-an-x86/ > > What happens is that the code is broken into: > >=20 > > r1 :=3D Write_Index; > > ... > > r2 :=3D r1 x Element'Length; > > r3 :=3D r2 + FIFO'First; > > r4 :=3D Element; > > (r3) :=3D r4; > > r1 :=3D r1 + 1; > > (Write_Index'Address) :=3D r1; > >=20 > > (Parentheses write to the address.) Oops! I misread the example. The right code is: =20 r1 :=3D Write_Index; ... r1 :=3D r1 + 1; -- added r2 :=3D r1 x Element'Length; r3 :=3D r2 + FIFO'First; r4 :=3D Element; (r3) :=3D r4; -- deleted from here. (Write_Index'Address) :=3D r1; The important point is that the two writes to memory cannot be reordered. = They will be put into the write pipe in that order. > I am still digesting some of this, so forgive the simple question: >=20 > What prevents the last 3 lines from being reordered like this: > r1 :=3D r1 + 1; > (Write_Index'Address) :=3D r1; > (r3) :=3D r4; See above. Writes cannot be moved past writes. In practice the write pipe= does some magic combining writes to the same cache line, etc. But it main= tains the order on writes to cache. If you have the right equipment you ca= n peek and see that the writes to main memory are misordered, or even missi= ng. But without a signal analyzer or some such, you can't peek at main mem= ory bypassing the cache.=20 > By the way, thank you for the very detailed explanation. I can't say > that I fully understand it all yet (as probably indicated from my questio= n). =20 > I'm probably gonna have to reread it a few times. The material here fleshed out is probably a full semester course. I taught= compiler construction as a graduate course in the past, and I would now in= sist on two semesters, or break it into front-end and code generation cours= es.