Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2551136pxj; Sun, 6 Jun 2021 06:06:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxK14ltkPPE2jCdKt22tHbc8Co0fpA2S37D9rUSSxMt2KSJ4TSAVYrWYFtVKLdJX3u/tPMw X-Received: by 2002:a05:6402:1a25:: with SMTP id be5mr14910613edb.369.1622984789792; Sun, 06 Jun 2021 06:06:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622984789; cv=none; d=google.com; s=arc-20160816; b=E40Wi/z7HjhtEjTNTQwZXNUQKQLC73EALbEVwvCDKcFlGWi7tR7UojB+SGC9+PDbv1 WMqlf9GxKeJNI22+333/v98K2hyXoseRLIG2tFFmf9nqkODyb1lgcLhdydFo3ypQI6nb qJIqMMOiKOBFBydQEbsFJ4zrDiGpQMxX3+gfzczy1OEHaFzDslUrpexV3+gi/Zm4bQF8 HEwe8lNfTI/RUFo+hifJgqec1efG0Fuemg1aMC7UVS5Oyzy6vxkTLZNsUDeHUSXZgxAi AYdlQc+0F23fVGbaSR1XJt6CCCkbPHtkmFpZQy30Srdvx7cZNZ9wiQFp71UOUGRyyIde Iv7Q== 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=bv+uJvUm3DVXrewXf0plgpkqjv5ctUnDQ29iEzkllaM=; b=BFTW2iqlpLs21NEaPjSbDfB41Nf5qnrhIHLX63t2W+qkiHo+c9tiWZYvX+eyfuoDPs UrKL7ud9kN4YXR2/lxH4CMOEBOTyq3Rw5aLX1u1zakvxHNdan6Dy0DH3bHPQRS4UDwO9 qGyTB7ZqqZMMaxi1zk7h1HDohw5BCZ3dy2yPf1lq7Duv/x+2RN6iDkZvgFV8cn9Z6jQ9 QfCBfjKoLzjzzz4c2b32X/t1UDJu34iK/gEE9tpdKLPMcuD7Qy1BEzi+65UbQhq0TD+j b3l5jfKt4lr7Vr8Hr9SD7H/3UDvVlhUJSqW+SjpOaF8wHWxu9sobNStzAuSUrxInvzEu RxfQ== 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 bt8si9995196ejb.153.2021.06.06.06.06.06; Sun, 06 Jun 2021 06:06:29 -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 S230090AbhFFNGI (ORCPT + 99 others); Sun, 6 Jun 2021 09:06:08 -0400 Received: from gate.crashing.org ([63.228.1.57]:44515 "EHLO gate.crashing.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229776AbhFFNGG (ORCPT ); Sun, 6 Jun 2021 09:06:06 -0400 Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 156CxvIK028160; Sun, 6 Jun 2021 07:59:57 -0500 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 156CxtIb028148; Sun, 6 Jun 2021 07:59:55 -0500 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Sun, 6 Jun 2021 07:59:55 -0500 From: Segher Boessenkool To: Linus Torvalds Cc: Alan Stern , "Paul E. McKenney" , 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: <20210606125955.GT18427@gate.crashing.org> References: <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> <20210606012903.GA1723421@rowland.harvard.edu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Jun 05, 2021 at 08:41:00PM -0700, Linus Torvalds wrote: > On Sat, Jun 5, 2021 at 6:29 PM Alan Stern wrote: > > 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. > > That's actually a feature in some cases, ie the ability to do CSE on > asm statements (ie the "always has the same output" optimization that > the docs talk about). > > So gcc has always looked at the asm string for that reason, afaik. GCC does not pretend it can understand the asm. But it can see when two asm statements are identical. > I think it's something of a bug when it comes to "asm volatile", but > the documentation isn't exactly super-specific. Why would that be? "asm volatile" does not prevent optimisation. It says this code has some unspecified side effect, and that is all! All the usual C rules cover everything needed: the same side effects have to be executed in the same order on the real machine as they would on the abstract machine. > There is a statement of "Under certain circumstances, GCC may > duplicate (or remove duplicates of) your assembly code when > optimizing" and a suggestion of using "%=" to generate a unique > instance of an asm. "%=" outputs a number unique for every output instruction (the whole asm is one instruction; these are GCC internal instructions, not the same thing as machine instructions). This will not help here. The actual thing the manual says is Under certain circumstances, GCC may duplicate (or remove duplicates of) your assembly code when optimizing. This can lead to unexpected duplicate symbol errors during compilation if your 'asm' code defines symbols or labels. Using '%=' may help resolve this problem. It helps prevent duplicated symbols and labels. It does not do much else. > Which might actually be a good idea for "barrier()", just in case. > However, the problem with that is that I don't think we are guaranteed > to have a universal comment character for asm statements. That's right. But ";#" works on most systems, you may be able to use that? > IOW, it might be a good idea to do something like > > #define barrier() \ > __asm__ __volatile__("# barrier %=": : :"memory") > > but I'm not 100% convinced that '#' is always a comment in asm code, > so the above might not actually build everywhere. Some assemblers use ";", some use "!", and there are more variations. But this will not do what you want. "%=" is output as a unique number *after* everything GCC has done with the asm. > However, *testing* the above (in my config, where '#' does work as a > comment character) shows that gcc doesn't actually consider them to be > distinct EVEN THEN, and will still merge two barrier statements. Yes, the insns have the same templates, will output the exact same to the generated assembler code, so are CSEd. > So the gcc docs are actively wrong, and %= does nothing - it will > still compare as the exact same inline asm, because the string > equality testing is apparently done before any expansion. They are not wrong. Maybe the doc could be clearer though? Patches welcome. > Something like this *does* seem to work: > > #define ____barrier(id) __asm__ __volatile__("#" #id: : :"memory") > #define __barrier(id) ____barrier(id) > #define barrier() __barrier(__COUNTER__) > > which is "interesting" or "disgusting" depending on how you happen to feel. __COUNTER__ is a preprocessor thing, much more like what you want here: this does its work *before* everything the compiler does, while %= does its thing *after* :-) (Not that I actually understand what you are trying to do with this). Segher