Issue 239324
Summary [X86] fpext half to float with F16C sets FE_INVALID from stale upper lanes of vcvtph2ps
Labels backend:X86, llvm:codegen, new issue
Assignees
Reporter alexey-bataev
    With F16C and without AVX512-FP16, a non-strict `fpext half to float` is lowered to `vpinsrw $0` into a register that still holds old data, followed by `vcvtph2ps`. `vcvtph2ps` converts four halves, not one, so lanes 1-3 come from whatever was in the register before. If one of them is a signaling NaN, the invalid flag is set, even though the converted value in lane 0 is exact.

Repro (`repro.ll`):

```llvm
target triple = "x86_64-unknown-linux-gnu"

@_one_ = global half 1.0
@fmt = private constant [22 x i8] c"%f FE_INVALID set=%d\0A\00"

define float @ext(<8 x i16> %stale, ptr %p) {
  %h = load half, ptr %p, align 2
  %f = fpext half %h to float
  ret float %f
}

define i32 @main() {
  call i32 @feclearexcept(i32 61)
  ; 31745 = 0x7c01, a half signaling NaN, placed in the lanes vcvtph2ps also converts
  %f = call float @ext(<8 x i16> <i16 0, i16 31745, i16 31745, i16 31745, i16 0, i16 0, i16 0, i16 0>, ptr @one)
 %inv = call i32 @fetestexcept(i32 1)
  %set = icmp ne i32 %inv, 0
  %z = zext i1 %set to i32
  %d = fpext float %f to double
  call i32 (ptr, ...) @printf(ptr @fmt, double %d, i32 %z)
  ret i32 0
}

declare i32 @feclearexcept(i32)
declare i32 @fetestexcept(i32)
declare i32 @printf(ptr, ...)
```

```
$ llc -O2 -mtriple=x86_64-unknown-linux-gnu -mattr=+f16c -relocation-model=pic repro.ll -filetype=obj -o repro.o
$ clang repro.o -o repro -lm
$ ./repro
1.000000 FE_INVALID set=1
```

Expected `FE_INVALID set=0`. `-O0` gives the same result. Tested on trunk (24.0.0git).

Code for `@ext`:

```asm
	vpinsrw	$0, (%rdi), %xmm0, %xmm0
	vcvtph2ps	%xmm0, %xmm0
	retq
```

The constrained form (`llvm.experimental.constrained.fpext` with `fpexcept.strict`) zero-extends first and does not set the flag:

```asm
	movzwl	(%rdi), %eax
	vmovd	%eax, %xmm0
	vcvtph2ps	%xmm0, %xmm0
	retq
```

With `+avx512fp16` the code is `vmovsh` + `vcvtsh2ss`, which is not affected.

The difference comes from the f16 case in `X86TargetLowering::LowerFP_EXTEND`: the non-strict branch does `ANY_EXTEND` to i32 and `INSERT_VECTOR_ELT` into an `UNDEF` v4i32, while the strict branch inserts into a zero vector.

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

Reply via email to