Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp17886pxa; Tue, 18 Aug 2020 14:19:41 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw+4ntVP+MMeERlhyoqgI0Y4Uvu4ANlkYf9J3dTeU1nrhs/z4D56u3mIcgY5fPBJPejIGvw X-Received: by 2002:a17:906:248b:: with SMTP id e11mr21543813ejb.280.1597785581522; Tue, 18 Aug 2020 14:19:41 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597785581; cv=none; d=google.com; s=arc-20160816; b=EpYkoX+91O1IssjZgBILd6oRsEIz79A/rxhO0pBmJavJfXDeUh6FIsxV2acmkaGXJ9 eP6QpRQBj6lP09OeRo4uFQPe3Qe5yTv8+paatnlfIao6LtgcM7FZZcqBcdBr0l1m7PQP VUmEwOmT/isrFrlgohusrN9Rx1rx69rgu+Q+pll6KaxNLUyWLup9n6iGWHBXl1C1Dj5N ZgElXYQpfriRUS0wKi92q6bs8UftH+uFG/LxfN2GXdgrVJOf5bVb+Sn4IndR1PM5kmP/ RWlzU1L2U42SWwSxWUHeYFbNTQz5WRX7lyR/U98Ag+M4MrN3S1gtDtuBITo0YjzIwwxn Zwvw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=vr+5B+GKrgPurReyWi008Qfh1JfHLe7PchQovbrkqeA=; b=kGURbMmgecb0FKLrlUXcJM3pqfV4jOXLauNPGqRFi2608h36BZeombeG/uBAO5jVTE P9VhmVxeTsM6XpvAsHW+WRkS64ZGDCKiIcTIiHiXyEwrzfaPS4H0mMI9T8Xn31dOS6NT j8TdgdJTO/5YSVxCkDs42c1JCSRoFmcMzfKcTEN+9INay6mN1h3UEDL5D5OKA69uAaWs joFjCsNY8HB9nuwWaUVztsTATpvwYXbucBlO+x/S+fSVY+xOqr+YNLlNQGF78xZfkbZ6 OAnjegnj9yRMKkeFZiV5xJ37/IFEW3wErGpFGufP+DydIA/dgWbnyUV0jBxJLKMPn+38 veuQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=X5TDa7Nm; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id l8si14444103ejq.668.2020.08.18.14.19.16; Tue, 18 Aug 2020 14:19:41 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=X5TDa7Nm; 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; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726874AbgHRU7F (ORCPT + 99 others); Tue, 18 Aug 2020 16:59:05 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56898 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726829AbgHRU7D (ORCPT ); Tue, 18 Aug 2020 16:59:03 -0400 Received: from mail-pg1-x541.google.com (mail-pg1-x541.google.com [IPv6:2607:f8b0:4864:20::541]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A875C061389 for ; Tue, 18 Aug 2020 13:59:03 -0700 (PDT) Received: by mail-pg1-x541.google.com with SMTP id g33so10315940pgb.4 for ; Tue, 18 Aug 2020 13:59:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=vr+5B+GKrgPurReyWi008Qfh1JfHLe7PchQovbrkqeA=; b=X5TDa7NmLSLO6xcFo2isrF5p8YaF8NNZhKzH/moB3vJcgBV/l8ZxwiIdax8/Z4mAvn 5q6c62wCy/yTBc+pPwJAcV+9K+xZLodI1IQ2GXIHhpHugIhFLwLFFyB4YbDdg2IuEHcz xifD+weo3yENDACy1YGbMkOpdIK8zBS9QLWbCdKtT+dzWVCXFhWxGDe9NaRk+cwu9cXh 1ow3dMfewwuAU6IFOhohi5xBxPIVXIgbaCx2JtiWM9g1i0m8E2viCSWkOUsYTGKvnq+C ui7OR2V9Mk8Ok3a757Uws7hqQKdWz5l+GMHUg3iPaHb8Ky9xjA3+5Yt0V2qr0/SjzJqs 6hQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=vr+5B+GKrgPurReyWi008Qfh1JfHLe7PchQovbrkqeA=; b=I8HLqsOoWRVo/+nbBcsUUpr2QuKPjm056hRIadMa7UTnQ38+XABQb0ygdJXkem2h2g I3z6WVummdqXoDaEcCza+g7B9+sDiI5Euwdb8/uSAalXzSAQeN4uUMCwOul/Nmyhs0Gd M8HTejel7h74MQ695CGNdXFY9eZbYg9YpRGGU9KjJ8SeBE3AaCNfs3AHdjWLEXJHiOCJ T9D2nZdv8WXGYqZkdL92GwrNkbPqCYMveoFfvwgCqHOx1Elulj9M6bEWfvVyBqqEt6Kf YNlSBQ6hfSNrJ0mjHTCc6lmqoUQiEuJEyJmrqja/ZvYdUZV3s2LdUyjc4JwofeC9vx7Y 305A== X-Gm-Message-State: AOAM531q+HsA4+jz5o4+qG5tdNgCGRqWWz/aT1lZBCcPvnCkQFXZX1EC OWqYBy05qp1mZRdLfLOLeRhnYVJKdKQvRWeypK0tSw== X-Received: by 2002:a63:a119:: with SMTP id b25mr14306933pgf.10.1597784342724; Tue, 18 Aug 2020 13:59:02 -0700 (PDT) MIME-Version: 1.0 References: <20200817220212.338670-1-ndesaulniers@google.com> <76071c24-ec6f-7f7a-4172-082bd574d581@zytor.com> <20200818202407.GA3143683@rani.riverdale.lan> In-Reply-To: From: Nick Desaulniers Date: Tue, 18 Aug 2020 13:58:51 -0700 Message-ID: Subject: Re: [PATCH 0/4] -ffreestanding/-fno-builtin-* patches To: Arvind Sankar , =?UTF-8?B?RMOhdmlkIEJvbHZhbnNrw70=?= , Eli Friedman Cc: Linus Torvalds , "H. Peter Anvin" , Masahiro Yamada , Andrew Morton , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Michal Marek , Linux Kbuild mailing list , LKML , Kees Cook , Tony Luck , Dmitry Vyukov , Michael Ellerman , Joe Perches , Joel Fernandes , Daniel Axtens , Andy Shevchenko , Alexandru Ardelean , Yury Norov , "maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)" , Ard Biesheuvel , "Paul E . McKenney" , Daniel Kiper , Bruce Ashfield , Marco Elver , Vamshi K Sthambamkadi Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Aug 18, 2020 at 1:27 PM Nick Desaulniers wrote: > > On Tue, Aug 18, 2020 at 1:24 PM Arvind Sankar wrote: > > > > On Tue, Aug 18, 2020 at 12:13:22PM -0700, Linus Torvalds wrote: > > > On Tue, Aug 18, 2020 at 12:03 PM H. Peter Anvin wrote: > > > > > > > > I'm not saying "change the semantics", nor am I saying that playing > > > > whack-a-mole *for a limited time* is unreasonable. But I would like to go back > > > > to the compiler authors and get them to implement such a #pragma: "this > > > > freestanding implementation *does* support *this specific library function*, > > > > and you are free to call it." > > > > > > I'd much rather just see the library functions as builtins that always > > > do the right thing (with the fallback being "just call the standard > > > function"). > > > > > > IOW, there's nothing wrong with -ffreestanding if you then also have > > > __builtin_memcpy() etc, and they do the sane compiler optimizations > > > for memcpy(). > > > > > > What we want to avoid is the compiler making *assumptions* based on > > > standard names, because we may implement some of those things > > > differently. > > > > > > > -ffreestanding as it stands today does have __builtin_memcpy and > > friends. But you need to then use #define memcpy __builtin_memcpy etc, > > which is messy and also doesn't fully express what you want. #pragma, or > > even just allowing -fbuiltin-foo options would be useful. I do really like the idea of -fbuiltin-foo. For example, you'd specify: -ffreestanding -fbuiltin-bcmp as an example. `-ffreestanding` would opt you out of ALL libcall optimizations, `-fbuiltin-bcmp` would then opt you back in to transforms that produce bcmp. That way you're informing the compiler more precisely about the environment you'd be targeting. It feels symmetric to existing `-fno-` flags (clang makes -f vs -fno- pretty easy when there is such symmetry). And it's already convention that if you specify multiple conflicting compiler flags, then the latter one specified "wins." In that sense, turning back on specific libcalls after disabling the rest looks more ergonomic to me. Maybe Eli or David have thoughts on why that may or may not be as ergonomic or possible to implement as I imagine? > > > > The two compilers have some peculiarities, which means you really can't > > have functions with the same name that do something else if you want to > > use builtins at all, and can also lead to missed optimizations. > > > > For eg, __builtin_strchr(s,'\0') can be optimized to strlen. gcc will > > optimize it that way even if -ffreestanding is used (so strlen has to > > mean strlen), while clang won't, so it misses a potential optimization. > > This is admittedly a silly example, but you could imagine something like > > strncpy being optimized to memcpy+memset if the source length was > > previously computed. > > > > PS: clang optimizes sprintf, but doesn't provide __builtin_sprintf? > > https://bugs.llvm.org/show_bug.cgi?id=47224 > -- > Thanks, > ~Nick Desaulniers -- Thanks, ~Nick Desaulniers