* [PATCH] Print bfloat16 DWARF types correctly
@ 2021-04-14 14:35 Luis Machado
2021-04-14 15:45 ` Willgerodt, Felix
2021-04-15 16:35 ` Tom Tromey
0 siblings, 2 replies; 9+ messages in thread
From: Luis Machado @ 2021-04-14 14:35 UTC (permalink / raw)
To: gdb-patches
Even if the DWARF information contains a bfloat16 base type (__bf16), a
variable of such type will still be printed using the IEEE half float format,
which is wrong.
This patch teaches GDB how to pick the bfloat16 format for __bf16 types in
DWARF (based on the base type name) and uses IEEE half float for all the other
16-bit float formats.
Tested on aarch64-linux/x86_64-linux.
OK?
gdb/ChangeLog:
YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
* arch-utils.c (default_floatformat_for_type): Handle bfloat16.
gdb/testsuite:
YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
* gdb.dwarf2/dw2-bfloat16.exp: New file.
---
gdb/arch-utils.c | 8 ++-
gdb/testsuite/gdb.dwarf2/dw2-bfloat16.exp | 82 +++++++++++++++++++++++
2 files changed, 89 insertions(+), 1 deletion(-)
create mode 100644 gdb/testsuite/gdb.dwarf2/dw2-bfloat16.exp
diff --git a/gdb/arch-utils.c b/gdb/arch-utils.c
index 0017e706ef1..e1d5afd6039 100644
--- a/gdb/arch-utils.c
+++ b/gdb/arch-utils.c
@@ -279,7 +279,13 @@ default_floatformat_for_type (struct gdbarch *gdbarch,
{
const struct floatformat **format = NULL;
- if (len == gdbarch_half_bit (gdbarch))
+ /* Check if this is a bfloat16 type. It has the same size as the
+ IEEE half float type, so we use the base type name to tell them
+ apart. */
+ if (name != nullptr && strcmp (name, "__bf16") == 0
+ && len == gdbarch_bfloat16_bit (gdbarch))
+ format = gdbarch_bfloat16_format (gdbarch);
+ else if (len == gdbarch_half_bit (gdbarch))
format = gdbarch_half_format (gdbarch);
else if (len == gdbarch_float_bit (gdbarch))
format = gdbarch_float_format (gdbarch);
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-bfloat16.exp b/gdb/testsuite/gdb.dwarf2/dw2-bfloat16.exp
new file mode 100644
index 00000000000..b7c394a3a3d
--- /dev/null
+++ b/gdb/testsuite/gdb.dwarf2/dw2-bfloat16.exp
@@ -0,0 +1,82 @@
+# Copyright 2021 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+#
+# This test checks if GDB can identify bfloat16 and IEEE half float types and
+# print them correctly.
+#
+load_lib dwarf.exp
+
+# This test can only be run on targets which support DWARF-2 and use gas.
+if {![dwarf2_support]} {
+ return 0
+}
+
+standard_testfile main.c -dw.S
+
+# Make some DWARF for the test.
+# 0x4049 is the equivalent of 3.141 for bfloat16.
+# 0x4248 is the equivalent of 3.1406 for IEEE half float.
+set asm_file [standard_output_file $srcfile2]
+Dwarf::assemble $asm_file {
+ global srcdir subdir srcfile
+
+ cu {} {
+ DW_TAG_compile_unit {
+ {DW_AT_language @DW_LANG_C}
+ {DW_AT_name $srcfile}
+ {DW_AT_comp_dir /tmp}
+ } {
+ declare_labels bf16_type fp16_type \
+ bf16_var fp16_var
+
+ bf16_type: DW_TAG_base_type {
+ {DW_AT_name __bf16}
+ {encoding @DW_ATE_float}
+ {DW_AT_byte_size 2 DW_FORM_sdata}
+ }
+
+ fp16_type: DW_TAG_base_type {
+ {DW_AT_name __fp16}
+ {encoding @DW_ATE_float}
+ {DW_AT_byte_size 2 DW_FORM_sdata}
+ }
+
+ bf16_var: DW_TAG_variable {
+ {DW_AT_name "bf16_1"}
+ {DW_AT_type :${bf16_type}}
+ {DW_AT_const_value 0x4049 DW_FORM_sdata}
+ }
+
+ fp16_var: DW_TAG_variable {
+ {DW_AT_name "fp16_1"}
+ {DW_AT_type :${fp16_type}}
+ {DW_AT_const_value 0x4248 DW_FORM_sdata}
+ }
+ }
+ }
+}
+
+if { [prepare_for_testing "failed to prepare" ${testfile} \
+ [list $srcfile $asm_file] {nodebug}] } {
+ return -1
+}
+
+if ![runto_main] {
+ return -1
+}
+
+# Make sure we can print both types correctly.
+gdb_test "print bf16_1" " = 3.141"
+gdb_test "print fp16_1" " = 3.1406"
--
2.25.1
^ permalink raw reply [flat|nested] 9+ messages in thread
* RE: [PATCH] Print bfloat16 DWARF types correctly
2021-04-14 14:35 [PATCH] Print bfloat16 DWARF types correctly Luis Machado
@ 2021-04-14 15:45 ` Willgerodt, Felix
2021-04-14 15:47 ` Luis Machado
2021-04-15 16:36 ` Tom Tromey
2021-04-15 16:35 ` Tom Tromey
1 sibling, 2 replies; 9+ messages in thread
From: Willgerodt, Felix @ 2021-04-14 15:45 UTC (permalink / raw)
To: Luis Machado, gdb-patches
>gdb/ChangeLog:
>
>YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
>
> * arch-utils.c (default_floatformat_for_type): Handle bfloat16.
>
>gdb/testsuite:
>
>YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
>
> * gdb.dwarf2/dw2-bfloat16.exp: New file.
It doesn't interfere with the avx512_bf16 support I added last year, as that is a register view only (no variables). I guess that is why you cc'ed me.
I don't know enough about it to judge if "__bf16" is the only string in use by different compilers. Looks ok to me other than that, but I am not an approver.
Regards,
Felix
Intel Deutschland GmbH
Registered Address: Am Campeon 10, 85579 Neubiberg, Germany
Tel: +49 89 99 8853-0, www.intel.de <http://www.intel.de>
Managing Directors: Christin Eisenschmid, Sharon Heck, Tiffany Doon Silva
Chairperson of the Supervisory Board: Nicole Lau
Registered Office: Munich
Commercial Register: Amtsgericht Muenchen HRB 186928
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Print bfloat16 DWARF types correctly
2021-04-14 15:45 ` Willgerodt, Felix
@ 2021-04-14 15:47 ` Luis Machado
2021-04-15 16:36 ` Tom Tromey
1 sibling, 0 replies; 9+ messages in thread
From: Luis Machado @ 2021-04-14 15:47 UTC (permalink / raw)
To: Willgerodt, Felix, gdb-patches
On 4/14/21 12:45 PM, Willgerodt, Felix wrote:
>> gdb/ChangeLog:
>>
>> YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
>>
>> * arch-utils.c (default_floatformat_for_type): Handle bfloat16.
>>
>> gdb/testsuite:
>>
>> YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
>>
>> * gdb.dwarf2/dw2-bfloat16.exp: New file.
>
>
> It doesn't interfere with the avx512_bf16 support I added last year, as that is a register view only (no variables). I guess that is why you cc'ed me.
I cc-ed you in case there was anything obviously wrong from x86's side.
It sounds like there isn't, so that's good. :-)
>
> I don't know enough about it to judge if "__bf16" is the only string in use by different compilers. Looks ok to me other than that, but I am not an approver.
That's fine. Thanks for checking.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Print bfloat16 DWARF types correctly
2021-04-14 15:45 ` Willgerodt, Felix
2021-04-14 15:47 ` Luis Machado
@ 2021-04-15 16:36 ` Tom Tromey
1 sibling, 0 replies; 9+ messages in thread
From: Tom Tromey @ 2021-04-15 16:36 UTC (permalink / raw)
To: Willgerodt, Felix via Gdb-patches
>>>>> ">" == Willgerodt, Felix via Gdb-patches <gdb-patches@sourceware.org> writes:
>> I don't know enough about it to judge if "__bf16" is the only string
>> in use by different compilers.
Yeah, though it's easy to add more if we discover them.
Tom
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Print bfloat16 DWARF types correctly
2021-04-14 14:35 [PATCH] Print bfloat16 DWARF types correctly Luis Machado
2021-04-14 15:45 ` Willgerodt, Felix
@ 2021-04-15 16:35 ` Tom Tromey
2021-04-15 16:40 ` Luis Machado
1 sibling, 1 reply; 9+ messages in thread
From: Tom Tromey @ 2021-04-15 16:35 UTC (permalink / raw)
To: Luis Machado via Gdb-patches
>>>>> "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:
Luis> YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
Luis> * arch-utils.c (default_floatformat_for_type): Handle bfloat16.
It's unfortunate to have to do it this way, but nevertheless I think the
patch is OK. Thank you.
Tom
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Print bfloat16 DWARF types correctly
2021-04-15 16:35 ` Tom Tromey
@ 2021-04-15 16:40 ` Luis Machado
2021-04-15 17:10 ` Tom Tromey
0 siblings, 1 reply; 9+ messages in thread
From: Luis Machado @ 2021-04-15 16:40 UTC (permalink / raw)
To: Tom Tromey, Luis Machado via Gdb-patches
On 4/15/21 1:35 PM, Tom Tromey wrote:
>>>>>> "Luis" == Luis Machado via Gdb-patches <gdb-patches@sourceware.org> writes:
>
> Luis> YYYY-MM-DD Luis Machado <luis.machado@linaro.org>
>
> Luis> * arch-utils.c (default_floatformat_for_type): Handle bfloat16.
>
> It's unfortunate to have to do it this way, but nevertheless I think the
> patch is OK. Thank you.
It is a bit iffy, given other compilers may name it differently.
I didn't find anything in the DWARF spec that differentiates float types
through a different encoding field. That would've been the best choice I
think.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Print bfloat16 DWARF types correctly
2021-04-15 16:40 ` Luis Machado
@ 2021-04-15 17:10 ` Tom Tromey
2021-04-16 14:21 ` Luis Machado
2021-04-22 19:34 ` Luis Machado
0 siblings, 2 replies; 9+ messages in thread
From: Tom Tromey @ 2021-04-15 17:10 UTC (permalink / raw)
To: Luis Machado via Gdb-patches; +Cc: Tom Tromey, Luis Machado
Luis> I didn't find anything in the DWARF spec that differentiates float
Luis> types through a different encoding field. That would've been the best
Luis> choice I think.
Yeah. We could add an extension of course, assuming the compilers are
changeable. However then we'd probably still want to support existing
executables.
Tom
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Print bfloat16 DWARF types correctly
2021-04-15 17:10 ` Tom Tromey
@ 2021-04-16 14:21 ` Luis Machado
2021-04-22 19:34 ` Luis Machado
1 sibling, 0 replies; 9+ messages in thread
From: Luis Machado @ 2021-04-16 14:21 UTC (permalink / raw)
To: Tom Tromey, Luis Machado via Gdb-patches
On 4/15/21 2:10 PM, Tom Tromey wrote:
> Luis> I didn't find anything in the DWARF spec that differentiates float
> Luis> types through a different encoding field. That would've been the best
> Luis> choice I think.
>
> Yeah. We could add an extension of course, assuming the compilers are
> changeable. However then we'd probably still want to support existing
> executables.
I've pushed this now. Thanks.
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: [PATCH] Print bfloat16 DWARF types correctly
2021-04-15 17:10 ` Tom Tromey
2021-04-16 14:21 ` Luis Machado
@ 2021-04-22 19:34 ` Luis Machado
1 sibling, 0 replies; 9+ messages in thread
From: Luis Machado @ 2021-04-22 19:34 UTC (permalink / raw)
To: Tom Tromey, Luis Machado via Gdb-patches
On 4/15/21 2:10 PM, Tom Tromey wrote:
> Luis> I didn't find anything in the DWARF spec that differentiates float
> Luis> types through a different encoding field. That would've been the best
> Luis> choice I think.
>
> Yeah. We could add an extension of course, assuming the compilers are
> changeable. However then we'd probably still want to support existing
> executables.
FTR, I've raised this with the compiler folks and we're discussing how
this should look like in DWARF. There's discussion going on about a
possible DWARF extension proposal.
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2021-04-22 19:34 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-14 14:35 [PATCH] Print bfloat16 DWARF types correctly Luis Machado
2021-04-14 15:45 ` Willgerodt, Felix
2021-04-14 15:47 ` Luis Machado
2021-04-15 16:36 ` Tom Tromey
2021-04-15 16:35 ` Tom Tromey
2021-04-15 16:40 ` Luis Machado
2021-04-15 17:10 ` Tom Tromey
2021-04-16 14:21 ` Luis Machado
2021-04-22 19:34 ` Luis Machado
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).