[llvm] [X86] Respect denormal mode in f32-to-bf16 conversions (PR #221052)

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Sep 13 13:19:24 PDT 2026


================
@@ -0,0 +1,110 @@
+; RUN: llc < %s -mtriple=x86_64-linux-gnu -mattr=+avx512bf16,+avx512vl -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,AVX512BF16
+; RUN: llc < %s -mtriple=x86_64-linux-gnu -mattr=+avxneconvert -verify-machineinstrs | FileCheck %s --check-prefixes=CHECK,AVXNECONVERT
+
+; VCVTNEPS2BF16 ignores MXCSR and treats input denormals as signed zero. Only
+; use it when the function's f32 input denormal mode has the same behavior.
+; Otherwise round with integer arithmetic instead of calling __truncsfbf2.
+
+define bfloat @fptrunc_default(float %x) nounwind {
+; CHECK-LABEL: fptrunc_default:
+; CHECK-NOT:     vcvtneps2bf16
+; CHECK-NOT:     __truncsfbf2
+; CHECK:         vucomiss %xmm0, %xmm0
+; CHECK:         shrl $16
+; CHECK-NOT:     vcvtneps2bf16
+; CHECK-NOT:     __truncsfbf2
+; CHECK:         retq
+  %r = fptrunc float %x to bfloat
+  ret bfloat %r
+}
+
+define bfloat @fptrunc_preservesign(float %x) nounwind denormal_fpenv(preservesign) {
+; AVX512BF16-LABEL: fptrunc_preservesign:
+; AVX512BF16:       vcvtneps2bf16 %xmm0, %xmm0
+; AVX512BF16:       retq
+;
+; AVXNECONVERT-LABEL: fptrunc_preservesign:
+; AVXNECONVERT:       {vex} vcvtneps2bf16 %xmm0, %xmm0
+; AVXNECONVERT:       retq
+  %r = fptrunc float %x to bfloat
+  ret bfloat %r
+}
+
+; Positive-zero mode cannot use an instruction that preserves the input sign.
+define bfloat @fptrunc_positivezero(float %x) nounwind denormal_fpenv(positivezero) {
----------------
arsenm wrote:

X86 doesn't have a positive zero mode, no point in testing it 

https://github.com/llvm/llvm-project/pull/221052


More information about the llvm-commits mailing list