Received: by 2002:a05:6a10:413:0:0:0:0 with SMTP id 19csp624329pxp; Sat, 19 Mar 2022 11:59:38 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxgOO1qtJTU9LdP3kVnTm3XLwpd0bb9NrGXUqIxzCFFbYAVPsWBcZLsFq3UdbE4kOHKJjtU X-Received: by 2002:a17:907:6d0b:b0:6df:e54c:ad1d with SMTP id sa11-20020a1709076d0b00b006dfe54cad1dmr2497849ejc.734.1647716378335; Sat, 19 Mar 2022 11:59:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1647716378; cv=none; d=google.com; s=arc-20160816; b=Ib3p+M9WE1GBeQwGH9bicceiw3WzdNMgm5Adme2Qe+YzrJDhftLko+DcwREdY+9T9j vh6Stfprr7q8HfihTxuO2FjqJGh2qBj/7X++dhRnAG70SAz+uVqSkRaaF8Sd4hq6fxQc FqqBuZ564kuP5Inz3l2O1zv1SNDYKPRGq5ZSkJFg+HGWLa3xmtrzSKd8hA+PXk2JaAal ZeTABbgh9hLFqExrd/fhHAgPFb/Dh5/OQtvsfISMACP4cUVkEX1ciuwUY+8/A5Ws3ym+ /RVDx9dllmfrfjRuspUT6NupQ+Ckn50VzubVj8BMOf4cqvWfIqGJRsgDYymgDSuvy02r 43jg== 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=lxyizMZU/aRBo+peR7gYF78ve+vMVd1HfiSB54B2BhQ=; b=xaNyjPQLk9FKjGABG5XA87+/1ujlL+pYgsfE0DeUZxU5BciPqp5gjbsm7qWL+hLi+R mfmCLnu8Ewxvbtr/ULajX/SesUhKKO+jPnspd5IoeQH+QBvGJ01Yin3anvS9rgfZIJ8S OERm9cErG9Q/kL8ZGOKmwJ96Ft6VmeAURGX3TWKQhzNaLR1igytiIQN8BAxxTimKUbe0 CrGdHJRJGjwT/BAkMobvSS0Y5ezuCxTPzcBFbWho/0ojeza6eNvNP7dec8tqgn0/5RS/ S5DYLP48/7BcNjVp4n8WO4S5RXsfe3H4okRBMhLU4QHbt4t4e+JqKkkw1mpnccnymg1a EFwQ== 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 b18-20020a50ccd2000000b00418c2b5bf94si6080021edj.630.2022.03.19.11.59.13; Sat, 19 Mar 2022 11:59:38 -0700 (PDT) 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 S241479AbiCRXMr (ORCPT + 99 others); Fri, 18 Mar 2022 19:12:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45666 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241477AbiCRXMo (ORCPT ); Fri, 18 Mar 2022 19:12:44 -0400 Received: from gate.crashing.org (gate.crashing.org [63.228.1.57]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id AC5DA111DE3; Fri, 18 Mar 2022 16:11:23 -0700 (PDT) Received: from gate.crashing.org (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id 22IN4QwF013108; Fri, 18 Mar 2022 18:04:26 -0500 Received: (from segher@localhost) by gate.crashing.org (8.14.1/8.14.1/Submit) id 22IN4P7s013107; Fri, 18 Mar 2022 18:04:25 -0500 X-Authentication-Warning: gate.crashing.org: segher set sender to segher@kernel.crashing.org using -f Date: Fri, 18 Mar 2022 18:04:25 -0500 From: Segher Boessenkool To: Linus Torvalds Cc: Andrew Cooper , Nick Desaulniers , "H. Peter Anvin" , Bill Wendling , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Nathan Chancellor , Juergen Gross , Peter Zijlstra , Andy Lutomirski , "llvm@lists.linux.dev" , LKML , linux-toolchains Subject: Re: [PATCH v5] x86: use builtins to read eflags Message-ID: <20220318230425.GT614@gate.crashing.org> References: <20220210223134.233757-1-morbo@google.com> <20220301201903.4113977-1-morbo@google.com> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.4.2.3i X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_PASS, SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 Thu, Mar 17, 2022 at 02:39:34PM -0700, Linus Torvalds wrote: > I think compiler people should see inline asm as a really *good* > thing, because it allows people to have that "I'm going to do > something that nobody normal does, look away now" kind of escape > clause, letting the compiler concentrate on the things it does best. There is a reason that we have the GCC inline asm extension at all. It is not because the compiler developers think it is useless ;-) It often *is* abused, or used in wrong, dangerous ways. This often will go undetected for a long time, for all the same reasons you want to have inline asm in the first place (it is an escape hatch, much of what you do with it is outside of what the compiler can (and does) know about)! > Yes, I realize inline asm isn't something compiler people love. And > yes, I realize that it might not give optimal code. Some of us do love it. But it should not be used when you do not need it, indeed to get better machine code genereated, but much more importantly to get more correct code, and much simpler code, simpler to read code. All these things are connected. > (And here I separate 'intrinsic' and generic compiler builtins. I love > the builtins that allow me to tell the compiler something, or the > compiler to tell me something about the code: __builtin_unreachable() > and __builtin_constant_p() are both wonderful examples of those kinds > of things) I don't know what you mean when you say "intrinsic". It is a worse name than a "built-in function", which is either not a function or not a built-in thing, so that name is rubbish; both "compiler intrinsic" and "intrinsic function" are even worse though! I like to say "builtin", which has no other meaning in the English language, so it cannot be easily understood to have properties it doesn't, not just from a misleading name anyway. And yes, __builtin_unreachable() is nicer than having to write "1/0" in all those places, it is much clearer. It is undefined behaviour to execute it exactly the same though. __builtin_constant_p() is a curious one: it returns whether the compiler knows its argument to be constant, which is something C does not define (*cannot* define). It is a curse as well as a blessing, moving from compiler bversion to compiler version can return false instead of true on the same expression, or the other way around, and the same when trying out different compilers of course. > But an intrinsic for some odd target-specific thing that can't even be > portably generalized? Give me inline asm any day. The vast majority of compiler builtins are for simple transformations that the machine can do, for example with vector instructions. Using such builtins does *not* instruct the compiler to use those machine insns, even if the builtin name would suggest that; instead, it asks to have code generated that has such semantics. So it can be optimised by the compiler, much more than what can be done with inline asm. It also can be optimised better by the compiler than if you would open-code the transforms (if you ask to frobnicate something, the compiler will know you want to frobnicate that thing, and it will not always recognise that is what you want if you just write it out in more general code). Well-chosen builtin names are also much more readable than the best inline asm can ever be, and it can express much more in a much smaller space, without so much opportunity to make mistakes, either. Builtins are good! > We can do things with inline asms that the compiler can't even _dream_ > of, like the whole instruction rewriting thing we use. Yes. Inline asm is good as well! Both things have their places. Some builtins are not well-conceived. And some inline asm has the same problem :-( > I'd much rather have a powerful generic concept (eg "asm goto" is > lovely) than a specialized intrinsic that does only one thing, and > doesn't even do it well because it's untested and badly defined. Being generally applicable is a double-edged sword. The trick is to have things more general when reasonable, and not when not. Segher