Re: [RFC PATCH] x86: prevent gcc from emitting rep movsq/stosq for inlined ops

From: Andrew Cooper
Date: Wed Apr 02 2025 - 14:17:13 EST


> Please make this a gcc bug-report instead - I really don't want to
> have random compiler-specific tuning options in the kernel. Because
> that whole memcpy-strategy thing is something that gets tuned by a lot
> of other compiler options (ie -march and different versions).

I've discussed this with PeterZ in the past, although I can't for the
life of me find the bugzilla ticket I thought I opened on the matter. 
(Maybe I never got that far).

The behaviour wanted is:

1) Convert to plain plain accesses (so they can be merged/combined/etc), or
2) Emit a library call

because we do provide forms that are better than the GCC-chosen "REP
MOVSQ with manual alignment" in the general case.

Taking a leaf out of the repoline book, the ideal library call(s) would be:

    CALL __x86_thunk_rep_{mov,stos}sb

using the REP ABI (parameters in %rcx/%rdi/etc), rather than the SYSV ABI.

For current/future processors, which have fast reps of all short/zero
flavours, we can even inline the REP {MOV,STO}S instruction to avoid the
call.

For older microarchitectures, they can reuse the existing memcpy/memset
implementations, just with marginally less parameter shuffling.

How does this sound?

~Andrew