llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Missed Optimization: Failing to coalesce multiple stores from `memcpy()`

Open napcakes opened this issue 1 year ago • 2 comments

Given:

void f(uint8_t *out, uint8_t x, uint8_t y)
{
	const uint8_t in[] = {x, y};
	memcpy(out, in, sizeof in);
}

Clang generates:

define dso_local void @f(ptr noundef writeonly captures(none) initializes((0, 2)) %out, i8 noundef zeroext %x, i8 noundef zeroext %y) local_unnamed_addr {
entry:
  store i8 %x, ptr %out, align 1
  %in.sroa.4.0..sroa_idx = getelementptr inbounds nuw i8, ptr %out, i64 1
  store i8 %y, ptr %in.sroa.4.0..sroa_idx, align 1
  ret void
}

With a curious workaround:

inline static void *memcpy2(void *restrict out, const void *restrict in)
{
	__builtin_memcpy_inline(out, in, 2);
	return out;
}

#define xmemcpy(out, in, size) \
	((size) == 2 ? memcpy2((out), (in)) : memcpy((out), (in), (size))

void g(uint8_t *out, uint8_t x, uint8_t y)
{
	const uint8_t in[] = {x, y};
	xmemcpy(out, in, sizeof in);
}

It generates:

define dso_local void @g(ptr noundef writeonly captures(none) initializes((0, 2)) %out, i8 noundef zeroext %x, i8 noundef zeroext %y) local_unnamed_addr {
entry:
  %in.sroa.4.0.insert.ext = zext i8 %y to i16
  %in.sroa.4.0.insert.shift = shl nuw i16 %in.sroa.4.0.insert.ext, 8
  %in.sroa.0.0.insert.ext = zext i8 %x to i16
  %in.sroa.0.0.insert.insert = or disjoint i16 %in.sroa.4.0.insert.shift, %in.sroa.0.0.insert.ext
  store i16 %in.sroa.0.0.insert.insert, ptr %out, align 1
  ret void
}

Which affects codegen; x86_64:

f:
        mov     byte ptr [rdi], sil
        mov     byte ptr [rdi + 1], dl
        ret

g:
        shl     edx, 8
        or      edx, esi
        mov     word ptr [rdi], dx
        ret

arm8v-a:

f:
        strb    w1, [x0]
        strb    w2, [x0, #1]
        ret

g:
        bfi     w1, w2, #8, #24
        strh    w1, [x0]
        ret

Godbolt.

napcakes avatar Mar 13 '25 08:03 napcakes

Might be a seperate issue, but same thing happens with structs:

struct S {
	uint8_t x;
	uint8_t y;
};

void f(struct S *out, uint8_t x, uint8_t y)
{
	struct S in = {x, y};
	*out = in;
}

GCC also fails on this.

Godbolt.

napcakes avatar Mar 14 '25 02:03 napcakes

Would this be considered a target specific optimization? I don't know if g is generally optimal to f in the above examples, as g is a sequence of 3 instructions all with dependencies on each other, while f consists of 2 independent instructions. Some hardware will coalesce the stores in the store buffer reducing the cache impact of the extra store in f.

The Arm bfi instruction allows for g to exist in two instruction, so maybe this should be made into an Arm specific issue (though I don't know what the typical latency of the arm bfi instruction is)?

bababuck avatar Mar 14 '25 22:03 bababuck