Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp6150435rwb; Mon, 5 Dec 2022 08:33:37 -0800 (PST) X-Google-Smtp-Source: AA0mqf4gpzrKLCpkEecGgLgxtG3lapx0SWhJARxaIO13Zl52AyhGasydCRWhifY4MzYd40ZRRt46 X-Received: by 2002:a63:481c:0:b0:477:ec30:b2c7 with SMTP id v28-20020a63481c000000b00477ec30b2c7mr41475817pga.526.1670258016778; Mon, 05 Dec 2022 08:33:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1670258016; cv=none; d=google.com; s=arc-20160816; b=Iv3Wx2lqC8E3Jjo5O1Pl2JaDBUiyZ8WCDfAQbgPyhFHC8+U1f1/Jp+i3AT25lcQPg4 XWZzr8T8xphbDFhVhTdb+q7bZK4x+2oDP7H7PvArkVV4O5ZZMZCqMdzmrHoWWH2W3wC4 L7XP4Y3xI3Xslw6bMKdXJObqsM2VMuAhhz/AAsddZcPqTfRcf/tk2AN5KzKsxHWB+5jR o9B4Syy7uV01K4LKl2CSXCSarWJMTF3Eg+paT0uZhajDLUOkse96AXbqHESDhV/1zm6o VW8dehIi4nXQCNR15CKlz719IX+/F59shjXkv9Kk+VBotIDKQEQt6WEm30QiHvk8k7n/ sHtw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date; bh=YldrF56DA9RyTaYpd3ZpfhpT65CNw5aQMu1Fvgw2dw4=; b=XS3KBRNEFujKLGU989GBVITgviToepHeZAqo5mzjK07i9gyupoHGHFnKjD54d3jB+G scMf5swjFnl1y1DwH+ozala6vGbNBDziriepai33RmENxAc9csOj8MdgPYAaz5DUFunF o2uAS9N0Fn7Hf+ZP/Hc0rBTrl19EFtQLxPP/ZMAqzNj2Y2w7fb+kJdBeMnQ+eYj8P0Io rPL51ovv/2FKWLokvUAFIA8G89lh1WmlpjWdHqWQIHcnWj7Z27Ns3N1oebEVqeYK/W7n BwXvtDpQfqYEs3kWpo/O46KThQHhHT0GxDzsT94irNAWJa+pwyumzozP/4cx6NpdEpXA 3NHA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id u190-20020a6385c7000000b00478c967a865si1708268pgd.125.2022.12.05.08.33.25; Mon, 05 Dec 2022 08:33:36 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231779AbiLEQSR (ORCPT + 81 others); Mon, 5 Dec 2022 11:18:17 -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: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,SPF_HELO_PASS,SPF_PASS autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net 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