https://gcc.gnu.org/bugzilla/show_bug.cgi?id=129182
Bug ID: 129182
Summary: [ARM] Missed optimization: small constant-size
__builtin_memcpy stores emit out-of-line memcpy on
-mno-unaligned-access
Product: gcc
Version: 16.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: irogers at google dot com
Target Milestone: ---
### Problem Description
On ARM targets where hardware unaligned memory access is disabled or
unsupported (e.g., `-march=armv5te -marm -mno-unaligned-access`), GCC fails to
inline small, constant-size `__builtin_memcpy()` store operations (2, 4, and 8
bytes). Instead of expanding the store into inline byte stores (`strb`) and
shifts (`lsr`), GCC spills the source scalar to the stack and calls the
external `memcpy` function (`bl memcpy`).
In contrast, if an unaligned store is written using a packed struct type-pun
(`struct { uint32_t v; } __attribute__((__packed__)) *`), GCC generates an
optimal inline sequence of 4 `strb` instructions and register shifts with zero
stack overhead.
Because `memcpy` is the standard C mechanism to perform unaligned access
without violating strict aliasing rules, this codegen failure forces
performance-critical codebases (like the Linux kernel) to retain undefined
behavior (`__packed` pointer type punning) solely to avoid out-of-line `memcpy`
calls.
### Linux Kernel Impact
In Linux v7.0, commit a339671db64b ("vdso: Switch get/put_unaligned() from
packed struct to memcpy()") transitioned `get_unaligned` and `put_unaligned`
from packed structs to `__builtin_memcpy`.
This resulted in an immediate ~22% performance regression in `fio` random-write
benchmarks on UBIFS/NAND on an ARMv5TE SoC (NXP i.MX25), because filesystem
write paths that serialized integers into unaligned buffers began calling
`memcpy` repeatedly.
This is documented in the following LKML threads:
* Original regression report by Stefan Kerkmann:
https://lore.kernel.org/lkml/[email protected]/
* Core patch discussion thread:
https://lore.kernel.org/lkml/[email protected]/
The kernel was forced to introduce a workaround via commit 2727aca4439e ("vdso:
Add VDSO_UNALIGNED_PACKED config option") to restore the packed struct
implementation on `!HAVE_EFFICIENT_UNALIGNED_ACCESS` architectures.
### Minimal Reproducer & Godbolt Link
Interactive Compiler Explorer Link showing compiler lowering:
https://godbolt.org/z/WeEaTj37T
```c
#include <stdint.h>
#include <string.h>
extern "C" {
void store32_packed(uint32_t val, void *p) {
struct { uint32_t v; } __attribute__((__packed__)) *ptr = (typeof(ptr))p;
ptr->v = val;
}
void store32_memcpy(uint32_t val, void *p) {
__builtin_memcpy(p, &val, sizeof(val));
}
}
```
### Compiler Flags
`-O2 -march=armv5te -marm -mno-unaligned-access`
### Comparison of Emitted Assembly
`store32_packed`:
```armasm
strb r0, [r1]
lsr r3, r0, #8
strb r3, [r1, #1]
lsr r3, r0, #16
strb r3, [r1, #2]
lsr r3, r0, #24
strb r3, [r1, #3]
bx lr
```
`store32_memcpy`:
```armasm
str lr, [sp, #-4]!
sub sp, sp, #12
str r0, [sp, #4]
add r1, sp, #4
mov r2, #4
bl memcpy
add sp, sp, #12
ldr pc, [sp], #4
```
### Expected Behavior
`store32_memcpy` should recognize that `&val` is a local 4-byte scalar whose
address only escapes to a known 4-byte constant `__builtin_memcpy`, eliminate
the stack temporary via Scalar Replacement of Aggregates (SROA), and emit the
same 4 `strb` inline instructions as `store32_packed`.