This is an archive of the discontinued LLVM Phabricator instance.

[compiler-rt][BF16] "bfloat -> float -> bfloat" round-trip conversions
ClosedPublic

Authored by vdonaldson on Aug 28 2023, 9:57 AM.

Details

Summary

Invoking compiler-rt function __truncsfbf2 to convert a zero 32-bit float
0x00000000 to a 16-bit bfloat value currently generates the denormal value
0x0040, rather than value 0x0000. Negative zero 0x80000000 is converted
to denormal 0x8040 rather than 0x8000.

This behavior is seen in flang code under development (not yet integrated)
that converts bfloat/REAL(KIND=3) argument values to float/REAL(KIND=4)
values and then converts those values back to bfloat/REAL(KIND=3). There
are other instances of the problem. A round-trip type conversion using
__truncsfbf2 of a denormal generates a different denormal, and an sNaN
is converted to a qNaN.

The problem is addressed in generic conversion function fp_trunc_impl.inc
by removing trailing 0 significand bits when the source and destination
type formats are identical except for the significand size. This condition
is met only for float -> bfloat conversions.

Round-trip conversions for at least some other type pairs have the same
problem. A solution in those cases would need to account for exponent
size differences. Those cases are not relevant to flang compilations
and are not addressed here. A broader solution might subsume this fix,
or this fix might remain useful as is.

There are no existing tests of bfloat conversion functionality in the
compiler-rt test directory. Tests for other conversions use a common
infrastructure that does not currently have support for bfloat conversions.
This patch does not attempt to add that infrastructure for this new case.
CodeGen test bfloat.ll checks bfloat adds and other operations that invoke
__truncsfbf2.

Diff Detail

Event Timeline

vdonaldson created this revision.Aug 28 2023, 9:57 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 28 2023, 9:57 AM
vdonaldson requested review of this revision.Aug 28 2023, 9:57 AM
klausler accepted this revision.Aug 28 2023, 10:02 AM
This revision is now accepted and ready to land.Aug 28 2023, 10:02 AM
PeteSteinfeld accepted this revision.Aug 28 2023, 12:25 PM

All builds and tests correctly and looks good.