[PATCH] D126953: Promote bf16 to f32 when the target doesn't support it

Craig Topper via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Sat Dec 10 13:05:00 PST 2022


craig.topper added a comment.

In D126953#3986514 <https://reviews.llvm.org/D126953#3986514>, @lebedev.ri wrote:

> In D126953#3986502 <https://reviews.llvm.org/D126953#3986502>, @craig.topper wrote:
>
>> In D126953#3986425 <https://reviews.llvm.org/D126953#3986425>, @lebedev.ri wrote:
>>
>>> FWIW, i agree with @arsenm, the legalization is wrong.
>>
>> The lowering is correct. Mantissa for ieee numbers are normalized by shifting left to avoid storing the first 1.
>>
>> Consider the number 1.5. In f32 it is stored as 0x3fc00000
>> sign = 0
>> exponent = 127
>> mantissa = 0x400000
>>
>> 1.5 in bfloat16 is 0x3fc0
>> sign = 0
>> exponent = 127
>> mantissa = 0x400
>
> Ok, i forgot that bit (i even implemented similar widening elsewhere previously!).
> So yes, this is identical except for subnormals
>
> - no subnormal normalization https://godbolt.org/z/TTqf9PeGc https://godbolt.org/z/TTqf9PeGc
> - with subnormal normalization https://godbolt.org/z/K88vh4xc8 https://alive2.llvm.org/ce/z/WeFY75
>
> My apologies...

It should be the same even for subnormals. The exponents in float32 and bfloat16 are the same width and use the same bias. A subnormal in bfloat16 can't be normalized in float32. The exponent can't get any smaller.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D126953/new/

https://reviews.llvm.org/D126953



More information about the llvm-commits mailing list