public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* Support for memcpy with equal source and destination
@ 2023-11-23 12:14 Adhemerval Zanella Netto
  2023-11-25  7:48 ` Paul Eggert
  0 siblings, 1 reply; 14+ messages in thread
From: Adhemerval Zanella Netto @ 2023-11-23 12:14 UTC (permalink / raw)
  To: libc-alpha; +Cc: post+sourceware.org

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

^ permalink raw reply	[flat|nested] 14+ messages in thread
* Support for memcpy with equal source and destination
@ 2023-11-27 14:38 Wilco Dijkstra
  2023-11-27 14:45 ` Ralf Jung
  0 siblings, 1 reply; 14+ messages in thread
From: Wilco Dijkstra @ 2023-11-27 14:38 UTC (permalink / raw)
  To: post; +Cc: 'GNU C Library'

Hi Ralf,

> However, at that point it seems unclear why that branch should live inside 
> `memcpy`, rather than being performed by the caller. The entire argument made 
> all along by compiler developers (as I understood it) was that the existing 
> `memcpy` are already working fine for the src==dest case; if new branches need 
> to be added, that's a different discussion.

Existing implementations don't need (or want) any extra branches. We also don't
want to complicate inline memcpy expansions. They all work fine if src==dst.

Paul's example was to show that with restrict you could write a conformant
C implementation. I think it is sufficient to cast away restrict without adding an
extra branch (the existing generic C version does this).

>> When n is zero, this implementation also supports NULL dest or src, though 
>> that's a separate issue.
>
> Yeah I'd like to see that guarantee as well, if possible. :)

That should also work fine on existing implementations.

Cheers,
Wilco

^ permalink raw reply	[flat|nested] 14+ messages in thread
* Support for memcpy with equal source and destination
@ 2023-11-27 19:28 Aaron Peter Bachmann
  2023-11-27 19:39 ` Paul Eggert
  0 siblings, 1 reply; 14+ messages in thread
From: Aaron Peter Bachmann @ 2023-11-27 19:28 UTC (permalink / raw)
  To: libc-alpha

In response to https://sourceware.org/pipermail/libc-alpha/2023-November/152961.html
(Since I am not subscribed, I cannot answer directly.)
>Here's a sample implementation. When dest == src this function doesn't 
>dereference either pointer, and this satisfies the C standard's rules 
>for 'restrict'.
I interpret the C-std differently.
Unfortunately, I am not entirely sure my interpretation is correct.
>
>   #include <string.h>
>
I think, the function signature is a promise that *dest and *src do not overlap.
This implies dest != src.
>   void *
>   memcpy (void *restrict dest, void const *restrict src, size_t n)
>   {
The comparison (dest != src) does not make dest based on src.
The comparison (dest != src) does not make src based on dest.
Thus, a conforming compiler could just remove the test which seems to be true always according to the function signature.
gcc and clang do not do so presently.
>     if (dest != src)
>       {
>	char *d = dest;
>	char const *s = src;
>	for (; n != 0; n--)
>	  *d++ = *s++;
>       }
>     return dest;
>   }
Regards, Aaron Peter Bachmann


^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-11-27 19:39 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-23 12:14 Support for memcpy with equal source and destination Adhemerval Zanella Netto
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

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).