linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Rasmus Villemoes <linux@rasmusvillemoes.dk>
To: Arvind Sankar <nivedita@alum.mit.edu>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>
Cc: "Nick Desaulniers" <ndesaulniers@google.com>,
	"Dávid Bolvanský" <david.bolvansky@gmail.com>,
	"Eli Friedman" <efriedma@quicinc.com>,
	"Linus Torvalds" <torvalds@linux-foundation.org>,
	"H. Peter Anvin" <hpa@zytor.com>,
	"Masahiro Yamada" <masahiroy@kernel.org>,
	"Andrew Morton" <akpm@linux-foundation.org>,
	"Thomas Gleixner" <tglx@linutronix.de>,
	"Ingo Molnar" <mingo@redhat.com>,
	"Borislav Petkov" <bp@alien8.de>,
	"Michal Marek" <michal.lkml@markovi.net>,
	"Linux Kbuild mailing list" <linux-kbuild@vger.kernel.org>,
	LKML <linux-kernel@vger.kernel.org>,
	"Kees Cook" <keescook@chromium.org>,
	"Tony Luck" <tony.luck@intel.com>,
	"Dmitry Vyukov" <dvyukov@google.com>,
	"Michael Ellerman" <mpe@ellerman.id.au>,
	"Joe Perches" <joe@perches.com>,
	"Joel Fernandes" <joel@joelfernandes.org>,
	"Daniel Axtens" <dja@axtens.net>,
	"Andy Shevchenko" <andriy.shevchenko@linux.intel.com>,
	"Alexandru Ardelean" <alexandru.ardelean@analog.com>,
	"Yury Norov" <yury.norov@gmail.com>,
	"maintainer:X86 ARCHITECTURE (32-BIT AND 64-BIT)"
	<x86@kernel.org>, "Ard Biesheuvel" <ardb@kernel.org>,
	"Paul E . McKenney" <paulmck@kernel.org>,
	"Daniel Kiper" <daniel.kiper@oracle.com>,
	"Bruce Ashfield" <bruce.ashfield@gmail.com>,
	"Marco Elver" <elver@google.com>,
	"Vamshi K Sthambamkadi" <vamshi.k.sthambamkadi@gmail.com>
Subject: Re: [PATCH 0/4] -ffreestanding/-fno-builtin-* patches
Date: Fri, 21 Aug 2020 08:45:52 +0200	[thread overview]
Message-ID: <721e6979-e83b-2238-4763-a8cf99b57631@rasmusvillemoes.dk> (raw)
In-Reply-To: <20200820175617.GA604994@rani.riverdale.lan>

On 20/08/2020 19.56, Arvind Sankar wrote:
> On Thu, Aug 20, 2020 at 04:56:02PM +0200, Rasmus Villemoes wrote:
>> On 18/08/2020 23.41, Arvind Sankar wrote:
>>>
>>> Note that -fno-builtin-foo seems to mean slightly different things in
>>> clang and gcc. From experimentation, clang will neither optimize a call
>>> to foo, nor perform an optimization that introduces a call to foo. gcc
>>> will avoid optimizing calls to foo, but it can still generate new calls
>>> to foo while optimizing something else. Which means that
>>> -fno-builtin-{bcmp,stpcpy} only solves things for clang, not gcc. It's
>>> just that gcc doesn't seem to have implemented those optimizations.
>>>
>>
>> I think it's more than that. I've always read gcc's documentation
>>
>> '-fno-builtin'
>> '-fno-builtin-FUNCTION'
>>      Don't recognize built-in functions that do not begin with
>>      '__builtin_' as prefix. ...
>>
>>      GCC normally generates special code to handle certain built-in
>>      functions more efficiently; for instance, calls to 'alloca' may
>>      become single instructions which adjust the stack directly, and
>>      calls to 'memcpy' may become inline copy loops.
>>      ...
>>
>> to mean exactly that observed above and nothing more, i.e. that
>> -fno-builtin-foo merely means that gcc stops treating a call of a
>> function named foo to mean a call to a function implementing the
>> standard function by that name (and hence allows it to e.g. replace a
>> memcpy(d, s, 1) by byte load+store). It does not mean to prevent
>> emitting calls to foo, and I don't think it ever will - it's a bit sad
>> that clang has chosen to interpret these options differently.
> 
> That documentation is misleading, as it also goes on to say:
> "...nor can you change the behavior of the functions by linking with a
> different library"
> which implies that you _can_ change the behavior if you use the option,
> and which is what your "i.e." is saying as well.
> 
> My point is that this is not completely true: in gcc, foo by default is
> defined to be __builtin_foo, and -fno-builtin-foo simply removes this
> definition. So the effect is just that calls to foo in the original
> source will be left alone.

Yes, this is a much better way of putting it. And with -fbuiltin-foo in
effect, the compiler just needs to transform the code in some way as-if
the standard function by that name was called, which it can of course
decide to implement by emitting such a call, but it can also open-code
it - or synthesize it using other std functions.

> But in order for an optimization that introduces a new call to foo to be
> valid, foo _must_ have standard semantics: strchr(s,'\0') is not s +
> strlen(s) unless strlen has standard semantics.

Correct. So I agree that -fno-builtin-strlen should prevent the compiler
from generating calls to strlen() that don't appear in the code.

This is an oversight in
> gcc's optimizations: it converts to s + __builtin_strlen(s), which then
> (normally) becomes s + strlen(s).
> 
> Check out this horror: https://godbolt.org/z/a1r9fK
> 
> Clang will disable this optimization if -fno-builtin-strlen is
> specified.
>
> Clang's interpretation is more useful for embedded, since you can use
> -fno-builtin-foo and avoid calling __builtin_foo directly, and be
> guaranteed that there will be no calls to foo that you didn't write
> explicitly (outside of memcpy/memset/memcmp). In this case you are free
> to implement foo with non-standard semantics, or avoid implementing it
> altogether, and be reasonably confident that it will all work.

Yeah, except that the list of -fno-builtin-foo one would have to pass is
enourmous, so for targets with a somewhat wonky libc, I'd much rather be
able to do a blanket -fno-builtin, and then manually check their memcpy,
memset and memcmp implementations and opt back in with
-fbuiltin-mem{cpy,set,cmp} so that small constant-size memcpys do get
properly open-coded.

The advice in gcc's documentation of just #definining memcpy() to
__builtin_memcpy() doesn't work in the real world (for example it breaks
C++ code that uses std::memcpy(...)).

>> Thinking out load, it would be useful if both compilers grew
>>
>>   -fassume-provided-std-foo
>>
>> and
>>
>>   -fno-assume-provided-std-foo
>>
>> options to tell the compiler that a function named foo with standard
>> semantics can be assumed (or not) to be provided by the execution
>> environment; i.e. one half of what -f(no-)builtin-foo apparently does
>> for clang currently.
> 
> Not following: -fno-assume-provided-std-foo sounds like it would have
> exactly the same semantics as Clang's -fno-builtin-foo, except maybe in
> addition it should cause the compiler to error on seeing __builtin_foo
> if it can't implement that without calling foo.

Yeah, I think you've convinced me there's no use for a separate option
to prevent inventing calls to foo() - I was mostly thinking of it as a
way to avoid having to provide each and every libc function that may
have been half-way standardized at some point. But if one doesn't
provide, say, bcmp, the code base certainly doesn't use bcmp itself, so
one doesn't lose anything by just using -fno-builtin-bcmp; there are no
explicit bcmp() uses that fail to get optimized.

Rasmus

  parent reply	other threads:[~2020-08-21  6:46 UTC|newest]

Thread overview: 68+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-17 22:02 [PATCH 0/4] -ffreestanding/-fno-builtin-* patches Nick Desaulniers
2020-08-17 22:02 ` [PATCH 1/4] Makefile: add -fno-builtin-stpcpy Nick Desaulniers
2020-08-17 22:31   ` H. Peter Anvin
2020-08-17 23:36     ` Nick Desaulniers
2020-08-18 19:21     ` Kees Cook
2020-08-18  7:10   ` Ard Biesheuvel
2020-08-18  7:25     ` Greg KH
2020-08-18  7:29       ` Ard Biesheuvel
2020-08-18  7:34         ` Greg KH
2020-08-18 19:23   ` Kees Cook
2020-08-17 22:02 ` [PATCH 2/4] Revert "lib/string.c: implement a basic bcmp" Nick Desaulniers
2020-08-18  5:44   ` Nathan Chancellor
2020-08-18 18:00     ` Nick Desaulniers
2020-08-18 19:24       ` Kees Cook
2020-08-17 22:02 ` [PATCH 3/4] x86/boot: use -fno-builtin-bcmp Nick Desaulniers
2020-08-18 19:24   ` Kees Cook
2020-08-17 22:02 ` [PATCH 4/4] x86: don't build CONFIG_X86_32 as -ffreestanding Nick Desaulniers
2020-08-18 19:24   ` Kees Cook
2021-01-07  0:27   ` Fangrui Song
2022-04-07 15:34   ` [tip: x86/build] x86/build: Don't " tip-bot2 for Nick Desaulniers
2022-04-07 17:01     ` Nick Desaulniers
2022-04-07 22:28       ` Borislav Petkov
2020-08-17 22:44 ` [PATCH 0/4] -ffreestanding/-fno-builtin-* patches H. Peter Anvin
2020-08-18 17:56   ` Nick Desaulniers
2020-08-18 19:02     ` H. Peter Anvin
2020-08-18 19:13       ` Linus Torvalds
2020-08-18 19:25         ` Nick Desaulniers
2020-08-18 19:58           ` Nick Desaulniers
2020-08-19 12:19             ` Clement Courbet
2020-08-18 20:24         ` Arvind Sankar
2020-08-18 20:27           ` Nick Desaulniers
2020-08-18 20:58             ` Nick Desaulniers
2020-08-18 21:41               ` Arvind Sankar
2020-08-18 21:51                 ` Dávid Bolvanský
2020-08-18 21:59                 ` Nick Desaulniers
2020-08-18 22:05                   ` Dávid Bolvanský
2020-08-18 23:22                     ` Nick Desaulniers
2020-08-20 14:56                 ` Rasmus Villemoes
2020-08-20 17:56                   ` Arvind Sankar
2020-08-20 18:05                     ` Dávid Bolvanský
2020-08-20 23:33                     ` Linus Torvalds
2020-08-21 17:29                       ` Arvind Sankar
2020-08-21 17:54                         ` Linus Torvalds
2020-08-21 18:02                           ` Linus Torvalds
2020-08-21 19:14                             ` Arvind Sankar
2020-08-21 19:23                               ` Linus Torvalds
2020-08-21 19:57                           ` Arvind Sankar
2020-08-21 20:03                             ` Peter Zijlstra
2020-08-21 21:39                             ` Linus Torvalds
2020-08-22  0:12                               ` Nick Desaulniers
2020-08-22 12:20                                 ` David Laight
2020-08-21  6:45                     ` Rasmus Villemoes [this message]
2020-08-24 15:57                 ` Masahiro Yamada
2020-08-24 17:34                   ` Arvind Sankar
2020-08-25  7:10                     ` Nick Desaulniers
2020-08-25  7:31                       ` Nick Desaulniers
2020-08-25 12:28                       ` Masahiro Yamada
2020-08-25 14:02                         ` Nick Desaulniers
2020-08-26 13:28                           ` Masahiro Yamada
2020-08-18 21:53               ` David Laight
2020-08-20 22:41               ` H. Peter Anvin
2020-08-20 23:17                 ` Arvind Sankar
2020-08-18 19:35       ` Nick Desaulniers
2020-08-18 22:25 ` Arvind Sankar
2020-08-18 22:59   ` Nick Desaulniers
2020-08-18 23:51     ` Arvind Sankar
2020-08-19  0:20     ` Arvind Sankar
2020-08-19  8:26   ` David Laight

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=721e6979-e83b-2238-4763-a8cf99b57631@rasmusvillemoes.dk \
    --to=linux@rasmusvillemoes.dk \
    --cc=akpm@linux-foundation.org \
    --cc=alexandru.ardelean@analog.com \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=ardb@kernel.org \
    --cc=bp@alien8.de \
    --cc=bruce.ashfield@gmail.com \
    --cc=daniel.kiper@oracle.com \
    --cc=david.bolvansky@gmail.com \
    --cc=dja@axtens.net \
    --cc=dvyukov@google.com \
    --cc=efriedma@quicinc.com \
    --cc=elver@google.com \
    --cc=hpa@zytor.com \
    --cc=joe@perches.com \
    --cc=joel@joelfernandes.org \
    --cc=keescook@chromium.org \
    --cc=linux-kbuild@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=masahiroy@kernel.org \
    --cc=michal.lkml@markovi.net \
    --cc=mingo@redhat.com \
    --cc=mpe@ellerman.id.au \
    --cc=ndesaulniers@google.com \
    --cc=nivedita@alum.mit.edu \
    --cc=paulmck@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=tony.luck@intel.com \
    --cc=torvalds@linux-foundation.org \
    --cc=vamshi.k.sthambamkadi@gmail.com \
    --cc=x86@kernel.org \
    --cc=yury.norov@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).