Issue 79257
Summary [Flang] TSVC s314: needs fast-math flags in function attributes for vectorization
Labels performance, flang:ir
Assignees
Reporter yus3710-fj
    Flang can't vectorize the loop in `s314` of [TSVC](https://www.netlib.org/benchmark/vectors) while Clang can vectorize the loop written in C.

```fortran
! Fortran version
      do 1 nl = 1,ntimes
      x = a(1)
      do 10 i = 2,n
        if(a(i) .gt. x) x = a(i)
   10 continue
      call dummy(ld,n,a,b,c,d,e,aa,bb,cc,x)
   1  continue
```

```c
// C version
for (int nl = 0; nl < ntimes; nl++) {
  x = a[0];
  for (int i = 1; i < n; i++) {
    if (a[i] > x) {
      x = a[i];
    }
 }
  dummy(a, b, c, d, e, aa, bb, cc, x);
}
```

```console
$ flang-new -v -Ofast s314.f -S -Rpass=vector
flang-new version 18.0.0 (https://github.com/llvm/llvm-project.git 2759e47067ea286f6302adcfe93b653cfaf6f2eb)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /path/to/install/bin
Found candidate GCC installation: /path/to/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Selected GCC installation: /path/to/lib/gcc/aarch64-unknown-linux-gnu/11.2.0
Candidate multilib: .;@m64
Selected multilib: .;@m64
 "/path/to/install/bin/flang-new" -fc1 -triple aarch64-unknown-linux-gnu -S -fcolor-diagnostics -mrelocation-model pic -pic-level 2 -pic-is-pie -ffast-math -target-cpu generic -target-feature +neon -target-feature +v8a -fstack-arrays -fversion-loops-for-stride -Rpass=vector -O3 -o s314.s -x f95-cpp-input s314.f
$ clang -Ofast s314.c -S -Rpass=vector
/path/to/s314.c:17:3: remark: vectorized loop (vectorization width: 4, interleaved count: 2) [-Rpass=loop-vectorize]
   17 | for (int i = 0; i < LEN; i++) {
      | ^
```

I thought fast-math flags are only needed for `fcmp` (#74263) , but it was insufficient (or wrong).

The following function should return `true` to recognize the loop as the max reduction.

https://github.com/llvm/llvm-project/blob/c41472dbafd0dcacd943a95a9a099c1942d50394/llvm/lib/Analysis/IVDescriptors.cpp#L805-L814

The max/min reduction is assumed to be the code like `select(fcmp(...))`.
This function returns `true` for `FCmpInst`, but not for `SelectInst` which can't have fast-math flags.

Clang generates fast-math flags in function attiributes and that makes the function return `true` for `SelectInst`.

```llvm
define dso_local noundef i32 @s314() local_unnamed_addr #0 {
  :
}
:
attributes #0 = { nounwind uwtable "approx-func-fp-math"="true" "denormal-fp-math"="preserve-sign,preserve-sign" "min-legal-vector-width"="0" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cmov,+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" "unsafe-fp-math"="true" }
```
`"no-nans-fp-math"="true"` and `"no-signed-zeros-fp-math"="true"` are necessary for vectorization.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to