]> git.ipfire.org Git - thirdparty/gcc.git/commit
RISC-V: fix __builtin_round clobbering FP exceptions flags [PR121534]
authorVineet Gupta <vineetg@rivosinc.com>
Thu, 14 Aug 2025 03:20:34 +0000 (20:20 -0700)
committerVineet Gupta <vineetg@rivosinc.com>
Wed, 20 Aug 2025 00:39:09 +0000 (17:39 -0700)
commitf12a272169523823ff27d69f187f73c4211222ae
treee6906fc2d2d460e42682c471c89534c5b8cb8b3d
parent4fc8d0d371a88bd51ce00cb428f9a0797e0be19e
RISC-V: fix __builtin_round clobbering FP exceptions flags [PR121534]

__builtin_round() fails to save/restore FP exception flags around the FP
compare insn which can potentially clobber the same.

Worth noting that the fflags restore bracketing is slightly different
than the glibc implementation. Both FLT and FCVT can potentially clobber
fflags. gcc generates below where even if branch is not taken and FCVT
is not executed, FLT still executed. Thus FSFLAGS is placed AFTER the
label 'L3'. glibc implementation FLT can't clobber due to early NaN check,
so FSFLAGS can be moved under the branch, before the label.

| convert_float_to_float_round
| ...
|   frflags a5
|   fabs.s fa5,fa0
|   flt.s a4,fa5,fa4    <--- can clobber fflags
|   beq a4,zero,.L3
|     fcvt.w.s a4,fa0,rmm     <--- also
|     fcvt.s.w fa5,a4
|     fsgnj.s fa0,fa5,fa0
| .L3:
|    fsflags a5            <-- both code paths

Fixes: f652a35877e3 ("This is almost exclusively Jivan's work....")
PR target/121534

gcc/ChangeLog:

* config/riscv/riscv.md (round_pattern): save/restore fflags.

gcc/testsuite/ChangeLog:

* gcc.target/riscv/rvv/autovec/vls/math-nearbyint-1.c: Adjust
scan pattern for additional instances of frflags/fsrflags.

Signed-off-by: Vineet Gupta <vineetg@rivosinc.com>
gcc/config/riscv/riscv.md
gcc/testsuite/gcc.target/riscv/rvv/autovec/vls/math-nearbyint-1.c