llvm / llvm/llvm-project

[AArch64] Missed shrink-wrapping of frame record into noreturn call block

Open
#218,615 1 comment 0 reactions 0 assignees View on GitHub
backend:AArch64 llvm:codegen missed-optimization
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

[AArch64] Missed shrink-wrapping of frame record into noreturn call block

https://godbolt.org/z/vPK5zarvv

### LLVM

Clang emits the frame record at function entry:

```asm
block_gimme:
stp x29, x30, [sp, #-16]!
mov x29, sp
...
```

All normal returns therefore restore the frame:

```asm
ldp x29, x30, [sp], #16
ret
```

The only call in the function is the noreturn call on the default switch path:

```asm
.LBB0_9:
adrp x0, .L.str
add x0, x0, :lo12:.L.str
bl Perl_croak
```
So the hot/non-calling paths still pay for the frame record even though they do not need it.

### GCC output

GCC keeps the normal return paths frame-free:

```asm
.L1:
ret

.L2:
mov w0, 1
ret
```
It sinks the frame record into the block containing the noreturn call:

```asm
.L24:
stp x29, x30, [sp, -16]!
mov w1, w0
adrp x0, .LC0
mov x29, sp
add x0, x0, :lo12:.LC0
bl Perl_croak
```

This is the behavior I would expect from shrink-wrapping here: the stp x29, x30 and mov x29, sp frame-record setup are only needed on the path that actually calls Perl_croak.

### Expected behavior

LLVM should ideally sink the AArch64 frame record into the block containing the only call, similar to GCC:

```asm
stp x29, x30, [sp, #-16]!
mov x29, sp
bl Perl_croak
```
The normal return paths should remain frame-free. The issue is specifically the missed sinking of the frame record (stp x29, x30 + mov x29, sp) into the noreturn call block.

Contributor guide

Open the contributing guide

Research direction

Start with the Godbolt reproduction linked in the issue and trace LLVM's AArch64 shrink-wrapping and frame-record handling. Compare the generated assembly with GCC's output, then verify that the frame setup is limited to the noreturn Perl_croak block while normal return paths remain frame-free.

Written by the indexing model from the issue text.

Assessment

Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Active
Clarity
Mostly clear
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.