[cfe-dev] _Float16 support

Kaylor, Andrew via cfe-dev cfe-dev at lists.llvm.org
Tue Jan 22 10:38:28 PST 2019


I'd like to start a discussion about how clang supports _Float16 for target architectures that don't have direct support for 16-bit floating point arithmetic.

The current clang language extensions documentation says, "If half-precision instructions are unavailable, values will be promoted to single-precision, similar to the semantics of __fp16 except that the results will be stored in single-precision." This is somewhat vague (to me) as to what is meant by promotion of values, and the part about results being stored in single-precision isn't what actually happens.

Consider this example:

_Float16 x;
_Float16 f(_Float16 y, _Float16 z) {
  x = y * z;
  return x;
}

When compiling with "-march=core-avx2" that results (after some trivial cleanup) in this IR:

@x = global half 0xH0000, align 2
define half @f(half, half) {
  %3 = fmul half %0, %1
  store half %3, half* @x
  ret half %3
}

That's not too unreasonable I suppose, except for the fact that it hasn't taken the lack of target support for half-precision arithmetic into account yet. That will happen in the selection DAG. The assembly code generated looks like this (with my annotations):

f:                                      # @f
# %bb.0:
       vcvtps2ph       xmm1, xmm1, 4             # Convert argument 1 from single to half
        vcvtph2ps       xmm1, xmm1                # Convert argument 1 back to single
        vcvtps2ph       xmm0, xmm0, 4            # Convert argument 0 from single to half
        vcvtph2ps       xmm0, xmm0                # Convert argument 0 back to single
        vmulss             xmm0, xmm0, xmm1   # xmm0 = xmm0*xmm1 (single precision)
        vcvtps2ph       xmm1, xmm0, 4            # Convert the single precision result to half
        vmovd             eax, xmm1                      # Move the half precision result to eax
        mov                 word ptr [rip + x], ax     # Store the half precision result in the global, x
        ret                                                             # Return the single precision result still in xmm0
.Lfunc_end0:
                                        # -- End function

Something odd has happened here, and it may not be obvious what it is. This code begins by converting xmm0 and xmm1 from single to half and then back to single. The first conversion is happening because the back end decided that it needed to change the types of the parameters to single precision but the function body is expecting half precision values. However, since the target can't perform the required computation with half precision values they must be converted back to single for the multiplication. The single precision result of the multiplication is converted to half precision to be stored in the global value, x, but the result is returned as single precision (via xmm0).

I'm not primarily worried about the extra conversions here. We can't get rid of them because we can't prove they aren't rounding, but that's a secondary issue. What I'm worried about is that we allowed/required the back end to improvise an ABI to satisfy the incoming IR, and the choice it made is questionable.

For a point of comparison, I looked at what gcc does. Currently, gcc only allows _Float16 in C, not C++, and if you try to use it with a target that doesn't have native support for half-precision arithmetic, it tells you "'_Float16' is not supported on this target." That seems preferable to making up an ABI on the fly.

I haven't looked at what happens with clang when compiling for other targets that don't have native support for half-precision arithmetic, but I would imagine that similar problems exist.

Thoughts?

Thanks,
Andy
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/cfe-dev/attachments/20190122/4af79538/attachment.html>


More information about the cfe-dev mailing list