public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
To: libc-alpha <libc-alpha@sourceware.org>
Cc: post+sourceware.org@ralfj.de
Subject: Support for memcpy with equal source and destination
Date: Thu, 23 Nov 2023 09:14:00 -0300	[thread overview]
Message-ID: <1e8beece-f865-4309-a28f-6782135e2a8a@linaro.org> (raw)

The BZ#31055 [1] requests a guarantee that memcpy with equal source and
destination is well defined on glibc, since both gcc and clang already
emits code with this assumption [2] [3].  There is a WIP document [4] to
proper extend this requirement for C standard, with some extra requirement
(such as allowing NULL inputs).

The GCC bug report [5] have further information on why GCC developers thing
this is a reasonable assumption, most related to the codgen cost of adding
either the extra compare to correctly call mempcy or just change the call
to memmmove.

From glibc standpoint, as far I could check there is no implementation that
prevents it (although I am not sure for all of them for all input sizes).
The memcpy with equal source and destination will still triggers compiler
warnings due the restrict issues, but it should not matter to compiler 
auto-generated libcalls.

Another solution would be to provide an alternative symbol, similar to 
__memcmpeq, meant to be used by the compiler to libcall optimizations.
However, __memcmpeq did not have any adoption so far [6] [7].

If we adopt this constraint, I think it would require to add some testing
besides the manual documentation.  Some arch maintainers would also like
to check their implementation to add an early bailout optimization. I also
presume that the fortify builtins already handle it correctly.

I am not sure about other libc implementation, at least Musl seems unlikely
to provide such guarantee.

Thoughts?

[1] https://sourceware.org/bugzilla/show_bug.cgi?id=31055
[2] https://gcc.gnu.org/git/?p=gcc.git;a=commitdiff;h=7758cb4b53e8a33642709402ce582f769eb9fd18;hp=6ce952188ab39e303e4f63e474b5cba83b5b12fd
[3] https://reviews.llvm.org/D86993
[4] https://docs.google.com/document/d/1guH_HgibKrX7t9JfKGfWX2UCPyZOTLsnRfR6UleD1F8/edit
[5] https://gcc.gnu.org/bugzilla/show_bug.cgi?id=32667
[6] https://gcc.gnu.org/pipermail/gcc-patches/2022-June/596881.html
[7] https://reviews.llvm.org/D127461

             reply	other threads:[~2023-11-23 12:14 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-23 12:14 Adhemerval Zanella Netto [this message]
2023-11-25  7:48 ` Paul Eggert
2023-11-25  8:20   ` Ralf Jung
2023-11-25 17:11     ` Paul Eggert
2023-11-27 11:15       ` Ralf Jung
2023-11-27 11:46         ` Alexander Monakov
2023-11-27 12:34           ` Ralf Jung
2023-11-27 14:25             ` Alexander Monakov
2023-11-27 14:38 Wilco Dijkstra
2023-11-27 14:45 ` Ralf Jung
2023-11-27 14:53   ` Zack Weinberg
2023-11-27 15:02     ` enh
2023-11-27 19:28 Aaron Peter Bachmann
2023-11-27 19:39 ` Paul Eggert

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=1e8beece-f865-4309-a28f-6782135e2a8a@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=libc-alpha@sourceware.org \
    --cc=post+sourceware.org@ralfj.de \
    /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 read-only IMAP folder(s) and NNTP newsgroup(s).