| Issue |
244445
|
| Summary |
[Flang] Flang has no strict floating-point exception mode, so flag-raising operations are deleted or speculated
|
| Labels |
flang
|
| Assignees |
|
| Reporter |
junfengd-nv
|
Flang has no equivalent of Clang's `-ffp-exception-behavior=strict`. The driver rejects that flag, `-ftrapping-math`, and `-ffp-model=strict`. `docs/FlangDriver.md` says Flang has no support for strict floating point and always behaves as if `-fno-trapping-math` were set.
Ordinary `fadd/fdiv` is treated as safe to speculate, and constant folding of a division by zero keeps the IEEE result (`+Inf`) but drops the sticky divide-by-zero flag. Both are correct only when the program has promised that floating-point exceptions can be ignored. A program that inspects the sticky flags, which the Fortran `ieee_exceptions` module allows, observes a different exception state from the one the source describes. To support the correctness of ieee exception usage, I suggest to support the option "-ffp-exception-behavior=strict`.
**Requested behavior**, matching Clang `-ffp-exception-behavior=strict`:
- Accept `-ffp-exception-behavior={ignore,strict}`. Default remains `ignore` (today's behavior).
- For `strict`, lower floating-point operations that originate in the user's
source to `llvm.experimental.constrained.*` with `round.tonearest` and
`fpexcept.strict`, and mark the function `strictfp`. Rounding mode and FMA
contraction stay as they are.
- Operations introduced by the compiler (trip-count arithmetic, strength
reduction, other synthesized divides) stay ordinary arithmetic and do not
record exceptions.
## 1. Constant folding removes an inexact addition
`1.0 + 0.1` is not exact in binary floating point. With halting enabled for `ieee_inexact`, executing the addition must trap. Folding it to the rounded
result keeps the stored value and discards the exception.
```fortran
program inexact_trap
use ieee_exceptions
implicit none
real :: z
call ieee_set_halting_mode(ieee_inexact, .true.)
z = gen_inexact(1.0, 0.1)
print *, "NO TRAP"
contains
function gen_inexact(a, b) result(c)
real, intent(in) :: a, b
real :: c
c = a + b
end function gen_inexact
end program inexact_trap
```
Flang current behavior:
- `-O0` executes the add and dies with `SIGFPE`.
- `-O3` prints `NO TRAP`. The IR stores the constant `1.1` and contains no `fadd`.
Expect: Under `-ffp-exception-behavior=strict` the user addition must stay a constrained `fadd`. The program must trap at `-O3`, the same as at `-O0`. The same applies to other floating-point operations that raise a flag.
## 2. A guarded divide is speculated
The source divides only when the denominator is not zero. `-O3` still emits the divide unconditionally and selects the result afterward.
```fortran
subroutine repro(a, b, c, r, s)
real, intent(in) :: a, b, c
real, intent(out) :: r, s
real :: t
t = (a + b) * c + a * b + c
if (a /= 0.0) then
r = b / a
else
r = 1.0e6
endif
s = t
end subroutine
```
`t` is there so the branch is not the whole body. Flang 23.1.1 `-O3` produces:
```llvm
%cmp = fcmp une float %a, 0.000000e+00
%div = fdiv float %b, %a
%sel = select i1 %cmp, float %div, float 1.000000e+06
```
The value stored in `r` is correct, but the divide executes for a zero `a` and raises the divide-by-zero flag. With `ieee_set_halting_mode(ieee_divide_by_zero, .true.)`, a backend that keeps this select traps on a path that does not divide in the source.
On x86_64, CodeGenPrepare puts `divss` back behind the branch, so the trap does not fire even though the IR above is already speculated. Under `-ffp-exception-behavior=strict` the user divide must stay a constrained `fdiv` on the taken side of the branch.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs