Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2245544pxj; Sat, 5 Jun 2021 18:37:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzVNC33GXm8YDdqlbGiN6pU0LRPLqnpkOz/YRpOlGgIbU/GiaXh4Jjc7c6pCu/WLzqeaCFM X-Received: by 2002:aa7:ce03:: with SMTP id d3mr12667851edv.360.1622943422445; Sat, 05 Jun 2021 18:37:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622943422; cv=none; d=google.com; s=arc-20160816; b=wmaVtK+gL8rFFB24mzf8y0bnIc8upmDE9TEJr4MDlr7c9AoUD4cJJn+X/dnLsfe1MF VVdypppyPX6Tw5/earWMQuVIKy6QV+Cg/g1a85lkoGG238SuqZvKJpH/Eun6umx9wNhx QMmux8ZCP6d1vxdtjvSqbz6b+7IiaQkizISgYmYLMR/jNVyVFmQ2Y7NQqySV7GMwoYXI WYojCH8zE+uVDTWYuv8cz4RKJHUnKwjv9hwCX6mDQuIp+au02dffUwBXJ1yGWDdzLBaa QUIiX71tw94FvuxF/shw0W3ODaTgDgyW4D3/5d928Cl1DQ8ysPS46rVV+BHJ8FCA+tRn hQew== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date; bh=oMj4iAkrP1dVy2nYGNXXzdcODy6UlLgLqU5pEkENFw4=; b=xFLTW4FkRbXqBdyH7tgz37pMZUSgpOF/8CUOkoZcP+Sedbm6ZJtKb1s6vuv+jyxt4x 1Mf4UJa3rSHl7KJKTSFjUI6Hn0oH/9MB57KzweEwPF7lzZ6J9wJwGaTgMBpUzw7QAqGQ ahyStrHsSXUPLDeeZ8Tpc2zujEsHBQvhvknehWJ9l9wrhLSaGE3UBqWKQaCJKMiLM12f i7gzonjLWmYQahIH/CsOQCaDVoGC1fdVa1Aj6lXfmj7wldkvzvC975ry9Om6J2ly69If I/h2jJKDXkl26Sgq1U1/GavY0P+mzL6GCTAohYnH+UoN/me6amVMgM9Do6h9jfvIAZ43 bHTw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id e1si2770812ejm.185.2021.06.05.18.36.20; Sat, 05 Jun 2021 18:37:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230104AbhFFBax (ORCPT + 99 others); Sat, 5 Jun 2021 21:30:53 -0400 Received: from netrider.rowland.org ([192.131.102.5]:56167 "HELO netrider.rowland.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with SMTP id S230084AbhFFBaw (ORCPT ); Sat, 5 Jun 2021 21:30:52 -0400 Received: (qmail 1723601 invoked by uid 1000); 5 Jun 2021 21:29:03 -0400 Date: Sat, 5 Jun 2021 21:29:03 -0400 From: Alan Stern To: "Paul E. McKenney" Cc: Linus Torvalds , Peter Zijlstra , Will Deacon , Andrea Parri , Boqun Feng , Nick Piggin , David Howells , Jade Alglave , Luc Maranget , Akira Yokosawa , Linux Kernel Mailing List , linux-toolchains@vger.kernel.org, linux-arch Subject: Re: [RFC] LKMM: Add volatile_if() Message-ID: <20210606012903.GA1723421@rowland.harvard.edu> References: <20210604182708.GB1688170@rowland.harvard.edu> <20210604205600.GB4397@paulmck-ThinkPad-P17-Gen-1> <20210604214010.GD4397@paulmck-ThinkPad-P17-Gen-1> <20210605145739.GB1712909@rowland.harvard.edu> <20210606001418.GH4397@paulmck-ThinkPad-P17-Gen-1> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20210606001418.GH4397@paulmck-ThinkPad-P17-Gen-1> User-Agent: Mutt/1.10.1 (2018-07-13) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jun 05, 2021 at 05:14:18PM -0700, Paul E. McKenney wrote: > On Sat, Jun 05, 2021 at 10:57:39AM -0400, Alan Stern wrote: > > Indeed, the expansion of the currently proposed version of > > > > volatile_if (A) { > > B; > > } else { > > C; > > } > > > > is basically the same as > > > > if (A) { > > barrier(); > > B; > > } else { > > barrier(); > > C; > > } > That does sound good, but... > > Current compilers beg to differ at -O2: https://godbolt.org/z/5K55Gardn > > ------------------------------------------------------------------------ > #define READ_ONCE(x) (*(volatile typeof(x) *)&(x)) > #define WRITE_ONCE(x, val) (READ_ONCE(x) = (val)) > #define barrier() __asm__ __volatile__("": : :"memory") > > int x, y; > > int main(int argc, char *argv[]) > { > if (READ_ONCE(x)) { > barrier(); > WRITE_ONCE(y, 1); > } else { > barrier(); > WRITE_ONCE(y, 1); > } > return 0; > } > ------------------------------------------------------------------------ > > Both gcc and clang generate a load followed by a store, with no branch. > ARM gets the same results from both compilers. > > As Linus suggested, removing one (but not both!) invocations of barrier() > does cause a branch to be emitted, so maybe that is a way forward. > Assuming it is more than just dumb luck, anyway. :-/ Interesting. And changing one of the branches from barrier() to __asm__ __volatile__("nop": : :"memory") also causes a branch to be emitted. So even though the compiler doesn't "look inside" assembly code, it does compare two pieces at least textually and apparently assumes if they are identical then they do the same thing. Alan