llvm / llvm/llvm-project

[Flang] Power operator (`**`) result differs between -O0 and -O1 when the exponent is a variable

Open
#176,281 3 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

```
Version of flang : 22.0.0git(2f7e218017db69454ea84e1849fcb9a11b1f7fef)/AArch64
```

On an Arm64 (AArch64) system using Flang:
With -O0:
`r**3` and `r**i` (with i = 3) produce identical results.
With -O1:
only `r**i` (where the exponent is a variable) produces a different value compared to `r**3`.

This behavior does not occur in other compilers such as gfortran or ifx.

Why does Flang produce a different result under -O1 only when the exponent is a variable?
Is this behavior intended or a possible bug?
Are there recommended compiler flags in Flang to obtain invariant numerical results across optimization levels?

sample.f90
```fortran
subroutine sub(r)
print *, r **3
end subroutine sub
subroutine sub2(r,i)
print *, r **i
end subroutine sub2
p= 1.2345678
call sub(p)
call sub2(p, 3)
end
```
flang
```
$ flang sample.f90 -O0; ./a.out
1.8816757
1.8816757

$ flang sample.f90 -O1; ./a.out
1.8816757
1.8816758

$ flang sample.f90 -Ofast; ./a.out
flang-22: warning: argument '-Ofast' is deprecated; use '-O3 -ffast-math -fstack-arrays' for the same behavior, or '-O3 -fstack-arrays' to enable only conforming optimizations [-Wdeprecated-ofast]
1.8816757
1.8816758

$ flang sample.f90 -O3 -fstack-arrays; ./a.out
1.8816757
1.8816758
```

gfortran
```
$ gfortran sample.f90 -O0; ./a.out
1.88167572
1.88167572

$ gfortran sample.f90 -O1; ./a.out
1.88167572
1.88167572

$ gfortran sample.f90 -Ofast; ./a.out
1.88167572
1.88167572
```

ifx
```
$ ifx sample.f90 -O0; ./a.out
1.881676
1.881676

$ ifx sample.f90 -O1; ./a.out
1.881676
1.881676

$ ifx sample.f90 -Ofast; ./a.out
1.881676
1.881676
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.