Issue 244792
Summary [RISCV] RVV callee-saved registers restored from the wrong address in realigned frames
Labels new issue
Assignees
Reporter petar-jovanovic
    When a function has callee-saved vector registers (a `riscv_vector_cc` function, or an interrupt handler with V) and its stack is realigned, the prologue saves those registers **before** it realigns SP and sets up the base pointer. The epilogue then reloads them relative to the realigned SP or BP.

- **Frame addressed from SP:** whenever the realignment moves SP, the reload reads from a lower address than the save. The caller's vector registers come back corrupted (confirmed under qemu-riscv64), and whether a call fails depends on the SP at function entry.
- **Frame addressed from BP** (realignment plus a variable-sized object): the prologue stores through `s1` before `mv s1, sp`, i.e. through the caller's `s1`.

This is a silent miscompile: no assertion fires and `-verify-machineinstrs` is clean. The CFI for these registers is also wrong.

### Reproducer

```llvm
define riscv_vector_cc void @realign_sp() nounwind {
  %local = alloca i64, align 64
  store volatile i64 0, ptr %local
  call void asm sideeffect "", "~{v24}"()
  ret void
}

define riscv_vector_cc void @realign_bp(i64 %n) nounwind {
 %local = alloca i64, align 64
  %vla = alloca i8, i64 %n
  store volatile i64 0, ptr %local
  store volatile i8 0, ptr %vla
  call void asm sideeffect "", "~{v24}"()
  ret void
}
```

```
llc -mtriple=riscv64 -mattr=+v repro.ll -o -
```

Output (excerpt, annotated):

```
realign_sp:
	...
	addi	a0, sp, 112
	vs1r.v	v24, (a0)       # save, before the realignment
	andi	sp, sp, -64     # realign: SP moves down by 0, 16, 32 or 48
	...
	addi	a0, sp, 112
	vl1r.v	v24, (a0)       # reload, relative to the realigned SP

realign_bp:
	...
	addi	a1, s1, 112
	vs1r.v	v24, (a1)       # s1 still holds the caller's value
	andi	sp, sp, -64
	mv	s1, sp          # BP is only set up here
	...
	addi	a0, s1, 112
	vl1r.v	v24, (a0)
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to