[lkml]   [2019]   [Sep]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
Messages in this thread
SubjectRe: [RFC] Improve memset
On Mon, Sep 16, 2019 at 10:25 AM Linus Torvalds
<> wrote:
> On Mon, Sep 16, 2019 at 2:18 AM Rasmus Villemoes
> <> wrote:
> >
> > Eh, this benchmark doesn't seem to provide any hints on where to set the
> > cut-off for a compile-time constant n, i.e. the 32 in
> Yes, you'd need to use proper fixed-size memset's with
> __builtin_memset() to test that case. Probably easy enough with some
> preprocessor macros to expand to a lot of cases.
> But even then it will not show some of the advantages of inlining the
> memset (quite often you have a "memset structure to zero, then
> initialize a couple of fields" pattern, and gcc does much better for
> that when it just inlines the memset to stores - to the point of just
> removing all the memset entirely and just storing a couple of zeroes
> between the fields you initialized).

After some experimentation, I think y'all are just doing it wrong.
GCC is very clever about this as long as it's given the chance. This
test, for example, generates excellent code:

#include <string.h>

__THROW __nonnull ((1)) __attribute__((always_inline)) void
*memset(void *s, int c, size_t n)
asm volatile ("nop");
return s;

/* generates 'nop' */
void zero(void *dest, size_t size)
__builtin_memset(dest, 0, size);

/* xorl %eax, %eax */
int test(void)
int x;
__builtin_memset(&x, 0, sizeof(x));
return x;

/* movl $0, (%rdi) */
void memset_a_bit(int *ptr)
__builtin_memset(ptr, 0, sizeof(*ptr));

So I'm thinking maybe compiler.h should actually do something like:

#define memset __builtin_memset

and we should have some appropriate magic so that the memset inline is
exempt from the macro. Or maybe there's some very clever way to put
all of this into the memset inline function. FWIW, this obviously
wrong code:

__THROW __nonnull ((1)) __attribute__((always_inline)) void
*memset(void *s, int c, size_t n)
__builtin_memset(s, c, n);
return s;

generates 'jmp memset'. It's not entirely clear to me exactly what's
happening here.


 \ /
  Last update: 2019-09-16 19:41    [W:0.164 / U:1.072 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site