https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118103
Bug ID: 118103
Summary: [15 Regression] GCC miscompile rvv intrinsics at
`-O3`, missing the `fsrm` instruction to the recover
status of frm CSR
Product: gcc
Version: 15.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: target
Assignee: unassigned at gcc dot gnu.org
Reporter: rvismith1115 at gmail dot com
Target Milestone: ---
Created attachment 59907
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=59907&action=edit
reduced testcase
code link: https://godbolt.org/z/E3dTrG1Gq
The function 'compute' is unrolled in the main function (.L8).
However, the `fsrm` instruction to the recover status of frm CSR is missed in
the unrolled function.
Compilation of 'compute' is correct:
```
.L4:
fsrmi 2
vsetvli a5,a3,e16,m1,ta,ma
vle16.v v1,0(a2)
...
vfnmadd.vv v1,v1,v1
fsrm a0
vfmsub.vv v1,v1,v1
```
Compilation of unrolled `compute` in the main function is incorrect:
```
.L8:
fsrmi 2
vsetvli a5,a4,e16,m1,ta,ma
vle16.v v1,0(a2)
...
vfnmadd.vv v1,v1,v1
vfmsub.vv v1,v1,v1
```
This leads to an incorrect calculation result:
O0 (correct)
> -0.051575 0.000278 0.000014 0.000016
O3 (incorrect)
> -0.051605 0.000278 0.000014 0.000016
Version info:
```
$ riscv64-unknown-elf-gcc --version
gcc () 15.0.0 20241117 (experimental)
Copyright (C) 2024 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
$ qemu-riscv64 --version
qemu-riscv64 version 9.1.0
Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers
```