From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 9B234C4332F for ; Mon, 5 Dec 2022 16:18:19 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231800AbiLEQSS (ORCPT ); Mon, 5 Dec 2022 11:18:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46936 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230035AbiLEQSP (ORCPT ); Mon, 5 Dec 2022 11:18:15 -0500 Received: from netrider.rowland.org (netrider.rowland.org [192.131.102.5]) by lindbergh.monkeyblade.net (Postfix) with SMTP id CE412BCAB for ; Mon, 5 Dec 2022 08:18:14 -0800 (PST) Received: (qmail 621638 invoked by uid 1000); 5 Dec 2022 11:18:13 -0500 Date: Mon, 5 Dec 2022 11:18:13 -0500 From: "stern@rowland.harvard.edu" To: Jonas Oberhauser Cc: Boqun Feng , "paulmck@kernel.org" , "parri.andrea@gmail.com" , "will@kernel.org" , "peterz@infradead.org" , "npiggin@gmail.com" , "dhowells@redhat.com" , "j.alglave@ucl.ac.uk" , "luc.maranget@inria.fr" , "akiyks@gmail.com" , "dlustig@nvidia.com" , "joel@joelfernandes.org" , "urezki@gmail.com" , "quic_neeraju@quicinc.com" , "frederic@kernel.org" , "linux-kernel@vger.kernel.org" Subject: Re: [PATCH v2] tools: memory-model: Make plain accesses carry dependencies Message-ID: References: <4262e55407294a5989e766bc4dc48293@huawei.com> <20221203190226.GR4001@paulmck-ThinkPad-P17-Gen-1> <20221203204405.GW4001@paulmck-ThinkPad-P17-Gen-1> <20221203231122.GZ4001@paulmck-ThinkPad-P17-Gen-1> <43c7ea9ebdd14497b85633950b014240@huawei.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 05, 2022 at 01:42:46PM +0000, Jonas Oberhauser wrote: > > Besides, could you also explain a little bit why only "data;rfi" can be "carry-dep" but "ctrl;rfi" and "addr;rfi" cannot? I think it's because there are special cases when compilers can figure out a condition being true or an address being constant therefore break the dependency > > Oh, good question. A bit hard for me to write down the answer clearly > (which some people will claim that I don't understand it well myself, > but I beg to differ :) :( :) ). > > In a nutshell, it's because x ->data [Plain] ->rfi y ->... z fulfils > the same role as storing something in a register and then using it in > a subsequent computation; x ->ctrl y ->... z (and ->addr) don't. So > it's not due to smart compilers, just the fact that the other two > cases seem unrelated to the problem being solved, and including them > might introduce some unsoundness (not that I have checked if they do). More can be said here. Consider the following simple example: void P0(int *x, int *y) { int r1, r2; int a[10]; r1 = READ_ONCE(*x); a[r1] = 1; r2 = a[r1]; WRITE_ONCE(*y, r2); } There is an address dependency from the READ_ONCE to the plain store in a[r1]. Then there is an rfi and a data dependency to the WRITE_ONCE. But in this example, the WRITE_ONCE is _not_ ordered after the READ_ONCE, even though they are linked by (addr ; rfi ; data). The compiler knows that the value of r1 does not change between the two plain accesses, so it knows that it can optimize the code to be: r1 = READ_ONCE(*x); r2 = 1; WRITE_ONCE(*y, r2); a[r1] = r2; And then the CPU can execute the WRITE_ONCE before the READ_ONCE. This shows that (addr ; rfi) must not be included in the carry-deps relation. You may be able to come up with a similar argument for (ctrl ; rfi), although it might not be quite as clear. Alan