gallivm: fix half to float conversions with llvm 11
authorRoland Scheidegger <sroland@vmware.com>
Wed, 29 Apr 2020 04:38:12 +0000 (06:38 +0200)
committerMarge Bot <eric+marge@anholt.net>
Thu, 30 Apr 2020 01:21:21 +0000 (01:21 +0000)
LLVM 11 removes the intrinsic for half to float conversion, so use the fpext
function instead. This function actually works now with half to float, albeit
a quick experiment showed at least the x86 backend cannot lower it itself if
the cpu doesn't support it natively and tries to call external library, which
crashes (and would be very slow anyway as it would be lowered to scalar code),
so for now only use it where we previously used the f16c intrinsic.

Closes: https://gitlab.freedesktop.org/mesa/mesa/issues/2603
Reviewed-by: Dave Airlie <airlied@redhat.com>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4800>

src/gallium/auxiliary/gallivm/lp_bld_conv.c

index 1321545808ab12d586b4630857c17edb95054880..51eb3c31aee039a600756fc5251bf66d8c1923fd 100644 (file)
@@ -103,19 +103,33 @@ lp_build_half_to_float(struct gallivm_state *gallivm,
 
    if (util_cpu_caps.has_f16c &&
        (src_length == 4 || src_length == 8)) {
-      const char *intrinsic = NULL;
-      if (src_length == 4) {
-         src = lp_build_pad_vector(gallivm, src, 8);
-         intrinsic = "llvm.x86.vcvtph2ps.128";
-      }
-      else {
-         intrinsic = "llvm.x86.vcvtph2ps.256";
+      if (LLVM_VERSION_MAJOR < 11) {
+         const char *intrinsic = NULL;
+         if (src_length == 4) {
+            src = lp_build_pad_vector(gallivm, src, 8);
+            intrinsic = "llvm.x86.vcvtph2ps.128";
+         }
+         else {
+            intrinsic = "llvm.x86.vcvtph2ps.256";
+         }
+         return lp_build_intrinsic_unary(builder, intrinsic,
+                                         lp_build_vec_type(gallivm, f32_type), src);
+      } else {
+         /*
+          * XXX: could probably use on other archs as well.
+          * But if the cpu doesn't support it natively it looks like the backends still
+          * can't lower it and will try to call out to external libraries, which will crash.
+          */
+         /*
+          * XXX: lp_build_vec_type() would use int16 vector. Probably need to revisit
+          * this at some point.
+          */
+         src = LLVMBuildBitCast(builder, src,
+                                LLVMVectorType(LLVMHalfTypeInContext(gallivm->context), src_length), "");
+         return LLVMBuildFPExt(builder, src, lp_build_vec_type(gallivm, f32_type), "");
       }
-      return lp_build_intrinsic_unary(builder, intrinsic,
-                                      lp_build_vec_type(gallivm, f32_type), src);
    }
 
-   /* Convert int16 vector to int32 vector by zero ext (might generate bad code) */
    h = LLVMBuildZExt(builder, src, int_vec_type, "");
    return lp_build_smallfloat_to_float(gallivm, f32_type, h, 10, 5, 0, true);
 }