llvm / llvm/llvm-project

Potential to use ldp/stp over multiple ldr/str's

Open
#191,480 2 comments 0 reactions 0 assignees View on GitHub
backend:AArch64 missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

https://godbolt.org/z/xd114jWGj
```c++
#include

struct PolicyStorage {
char buf[16];
};

struct PolicyFunc {
PolicyStorage buf;
void (*func)();
const void* policy;
};

void swap_custom(PolicyFunc& a, PolicyFunc& b) {
std::swap(a.func, b.func);
std::swap(a.policy, b.policy);
}
```

When compiled with `-target aarch64-linux-gnu -Oz` (and `-Os`) will produce

```asm
_Z11swap_customR10PolicyFuncS0_: // @_Z11swap_customR10PolicyFuncS0_
.cfi_startproc
// %bb.0:
ldr x8, [x1, #16]
ldr x9, [x0, #16]
str x8, [x0, #16]
str x9, [x1, #16]
ldr x8, [x1, #24]
ldr x9, [x0, #24]
str x8, [x0, #24]
str x9, [x1, #24]
ret
```

For getting maximal size savings though I think it should be possible to do

```asm
_Z11swap_customR10PolicyFuncS0_:
ldp x8, x9, [x1, #16]
ldp x10, x11, [x0, #16]
stp x8, x9, [x0, #16]
stp x10, x11, [x1, #16]
```

at the cost of 2 extra registers used. It could be for more complex functions, the higher register pressure could lead to more text via spilling/reloading, but it's probably worth keeping track of here.

Contributor guide

Open the contributing guide

Research direction

Start with the C++ example and Godbolt link, compiling for AArch64 with -Oz and -Os. Inspect the generated assembly around swap_custom and investigate whether the adjacent ldr/str operations can use ldp/stp while accounting for the extra register pressure. Done means the relevant size-optimized output uses paired operations where beneficial and remains correct.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
compilers, performance
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.