| Issue |
239539
|
| Summary |
[X86][LoopVectorize] Loops calling `_mm_cvt_ss2si` / `_mm_cvtt_ss2si` are never vectorized
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
mbhade-amd
|
Loops that convert floats with `_mm_cvt_ss2si(_mm_set_ss(x))` or `_mm_cvtt_ss2si(_mm_set_ss(x))` stay scalar, although `cvtps2dq` /
`cvttps2dq` compute the same per-lane result. Opus uses this pattern for `float2int` on SSE targets ([`celt/float_cast.h`](https://github.com/xiph/opus/blob/main/celt/float_cast.h)).
```c
#include <xmmintrin.h>
static inline int float2int(float x) { return _mm_cvt_ss2si(_mm_set_ss(x)); }
void convert(const float *restrict in, int *restrict out, int n) {
for (int i = 0; i < n; i++)
out[i] = float2int(in[i]);
}
```
Clang trunk, `-O3 -march=x86-64-v4 -Rpass-analysis=loop-vectorize` ([Compiler Explorer](https://godbolt.org/z/qq8z6YzY5)):
```
repro.c:3:61: remark: loop not vectorized: instruction return type cannot be vectorized
repro.c:3:47: remark: loop not vectorized: call instruction cannot be vectorized
```
Two things block it:
1. **No generic intrinsic has the same semantics.** `cvtss2si` returns
`0x80000000` for NaN and out-of-range input. `llvm.lrint` / `fptosi` return
`freeze poison` / `poison`, and `llvm.fptosi.sat` returns 0 for NaN, so
folding to them is not a refinement.
2. **The X86 intrinsic cannot be widened.** `llvm.x86.sse.cvtss2si` is
`i32 (<4 x float>)` with no overloaded types, and it is not accepted by
`llvm::isTriviallyVectorizable`.
**Question:** what is the preferred upstream way to vectorize these loops while keeping the exact `0x80000000` result? For example, an overloaded X86 intrinsic that the vectorizer may widen, or a generic intrinsic like `llvm.fptosi.sat` that returns `0x80000000` on invalid input. Other
suggestions are welcome.
Details are in the RFC: RFC_LINK.
Related: #120169 and the 2020 thread
[TriviallyVectorizable Attribute in TD file](https://discourse.llvm.org/t/triviallyvectorizable-attribute-in-td-file/57028).
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs