| Issue |
233426
|
| Summary |
[libc] Cortex-M55 memcpy grows from 270 to 412 bytes and now uses MVE after SLP runtime alias check versioning in loops (#226955)
|
| Labels |
libc,
llvm:SLPVectorizer
|
| Assignees |
|
| Reporter |
Prabhuk
|
### Summary
Since fa10e634e2be9c16a7fdf01aefe0ed45ab6d1b47 ([SLP] Support runtime alias checks versioning for blocks in loops, #226955) the SLP vectorizer versions the 64-byte copy loop of llvm-libc's Cortex-M `memcpy` (`libc/src/string/memory_utils/arm/inline_memcpy.h`) on targets with a vector unit. For the `armv8.1m.main-none-eabi` (Cortex-M55, `+mve.fp`) runtimes build this grows `memcpy` from **270 B to 412 B (+142 B, +52.6%)**:
| Section in `memcpy.cpp.obj` (`armv8.1m.main-none-eabi/libc.a`) | Before fa10e634e2be | After | Delta |
| --- | ---: | ---: | ---: |
| `.text.memcpy` | 14 B | 14 B | 0 B |
| `.text._ZN..._25inline_memcpy_arm_mid_endEPNS_3cpp4byteEPKS1_j` | 256 B | 398 B | **+142 B** |
Cortex-M85 (`+mve`) and 32-bit ARMv7-A/ARMv8-A `+neon` builds of the same file regress similarly (+146 B / +100 B).
### What happens
`inline_memcpy_arm_mid_end` deliberately copies word by word (`consume_by_block<64, BlockOp::kByWord, kUnknown>`: a non-unrolled loop around 16 fully unrolled 4-byte `ldr`/`str` pairs) and `inline_memcpy_arm` deliberately hides the `__restrict` qualifiers from the compiler (`asm volatile("" : "+r"(dst), "+r"(src))`) to keep that exact load/store sequence.
With #226955 the SLP vectorizer vectorizes the 16 word copies into four `<4 x i32>` (MVE `vldrb.u8`/`vstrb.8`) loads/stores, cannot prove that `dst` and `src` do not overlap, and versions the loop body: every iteration now evaluates an overlap check (`dst + 64 > src && src + 64 > dst`), and the complete scalar body is kept as a fallback, plus extra callee-saved register traffic (`push.w {r8, r9, r10}`).
`memcpy.cpp` is compiled with `-O3` even in `MinSizeRel` builds (`libc_opt_high_flag` in `add_implementation()`), so the `optsize` guard in `BoUpSLP::canVersionBlockForRuntimeChecks()` does not apply.
```text
=== after fa10e634e2be (excerpt of the 64-byte loop) ===
24: adds r6, r1, r3
26: adds r5, r0, r3
28: add.w r8, r6, #0x40
2c: add.w r9, r5, #0x40
30: cmp r8, r5
32: cset r8, hi
36: cmp r9, r6
38: cset r4, hi
3c: tst.w r8, r4
40: bne 0x74 ; -> duplicated scalar fallback (16 x ldr/str)
42: vldrb.u8 q0, [r6]
46: vstrb.8 q0, [r5]
... (4 x vldrb.u8/vstrb.8) ...
6c: le lr, 0x24
74: ldr r4, [r1, r3] ; scalar fallback
76: str r4, [r0, r3]
... (16 x ldr/str) ...
```
### `memcpy` now executes MVE instructions
Before this change the Cortex-M `memcpy` was integer-only. It now contains MVE vector loads/stores (`vldrb.u8`/`vstrb.8`), so on M-profile cores calling `memcpy` before CP10/CP11 are enabled in `CPACR` raises a NOCP UsageFault. Reset handlers that copy `.data` with `memcpy` before enabling the FPU/MVE (which worked with the previous integer-only implementation) now fault during early boot.
### Reproducer
```bash
clang++ --target=armv8.1m.main-unknown-none-eabi -mthumb -mfloat-abi=hard \
-march=armv8.1-m.main+mve.fp+fp.dp -mcpu=cortex-m55 -Os -O3 \
-ffreestanding -nostdinc++ -fno-exceptions -fno-rtti -ffunction-sections -fdata-sections \
-Illvm-project/libc -Illvm-project/libc/include \
-DLIBC_NAMESPACE=__llvm_libc_24_0_0_git -DLIBC_COPT_PUBLIC_PACKAGING \
-c llvm-project/libc/src/string/memcpy.cpp -o memcpy.o
llvm-size -A memcpy.o
```
Adding `-fno-slp-vectorize` (or `-mllvm -slp-vectorize-with-runtime-alias-checks=false`) restores the previous code byte for byte; the SLP vectorizer did not change anything in this file on any 32-bit ARM configuration before fa10e634e2be.
### Proposed fix
Build the ARM `memcpy` with `-fno-tree-slp-vectorize` in `libc/src/string/CMakeLists.txt` (PR to follow).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs