Issue 234102
Summary [AArch64][SVE][StrictFP] Unused predicated FADD loses observable FP exception flags
Labels new issue
Assignees
Reporter matt-gretton-dann
    ## Summary

On current main built on AArch64:

With strict floating-point exception behaviour, Clang/LLVM eliminates an SVE `svadd_f32_z` call whose numerical result is unused. The equivalent scalar and Advanced SIMD additions are retained. This can silently lose the cumulative `FPSR.IOC` flag: adding `+infinity` and `-infinity` in an active lane generates an invalid-operation exception. No fast-math option is enabled.

This probably also applies to all other SVE floating-point intrinsics but I didn't dig deeper.

## Reproducer

This reproducer does not require SVE hardware, as the issue can be observed in the disassembly.

Save as `repro.c`:

```c
#include <arm_neon.h>
#include <arm_sve.h>
#pragma STDC FENV_ACCESS ON

void scalar(float a, float b) {
  float unused = a + b;
}

void neon(float a, float b) {
  float32x4_t unused = vaddq_f32(vdupq_n_f32(a), vdupq_n_f32(b));
}

void sve(float a, float b) {
  svfloat32_t unused = svadd_f32_z(svptrue_pat_b32(SV_VL4),
                                 svdup_f32(a), svdup_f32(b));
}
```

Compile with Clang:

```sh
clang -target aarch64-none-elf -march=armv8.2-a+sve+fp16 \
  -msve-vector-bits=scalable -ffreestanding -fno-fast-math \
  -ffp-contract=off -frounding-math -ffp-exception-behavior=strict \
  -x c -std=c17 -O2 -S repro.c -o repro.s
```

## Actual behavior

At `-O2`, `scalar` emits `fadd s0, s0, s1`, `neon` emits
`fadd v0.4s, v0.4s, v1.4s`, and `sve` is reduced to `ret` with no `fadd`:

```asm
scalar:
    fadd s0, s0, s1
    ret
neon:
    fadd v0.4s, v0.4s, v1.4s
    ret
sve:
    ret
```

## Expected behavior

Under the requested strict exception semantics, the SVE addition must retain its exception effect even when its result is unused. For `a = +infinity`, `b = -infinity`, and FPCR exception traps disabled, at least one of the four active SVE lanes must set `FPSR.IOC`, as the scalar and Neon additions can.

Returning without the addition leaves the flag unchanged.

## Architecture reference

Arm ARM issue M.d, [C8.2.155, predicated SVE FADD](https://support.arm.com/documentation/ddi0487/md/1818-isa_fadd_z_p_zz)
performs `FPAdd` on each active lane (Operation pseudocode, lines 117–136).  [J1.4.318, `FPAdd`](https://support.arm.com/documentation/ddi0487/md/8628-asl_shared_functions_float_fpadd_fpadd) sets `fpexc = TRUE` (lines 31–34) and calls `FPProcessException(FPExc_InvalidOp, fpcr)` for opposite-sign infinities (lines 46–52). [J1.4.359, `FPProcessException`](https://support.arm.com/documentation/ddi0487/md/8669-asl_shared_functions_float_fpprocessexception_fpprocessexception) sets the cumulative FPSR bit when trapping is disabled (lines 34–39, 71–76).  The [floating-point exceptions section](https://support.arm.com/documentation/ddi0487/md/061-beijddag) also states that predicated SVE FP instructions can generate exceptions for active elements (lines 29–35).

_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to