This is an archive of the discontinued LLVM Phabricator instance.

[Clang][DebugInfo] Emit narrower base types for structured binding declarations that bind to struct bitfields
ClosedPublic

Authored by vhscampos on Aug 9 2023, 1:41 AM.

Details

Summary

In cases where a structured binding declaration is made to a struct with
bitfields:

struct A {

unsigned int x : 16;
unsigned int y : 16;

} g;

auto [a, b] = g; // structured binding declaration

Clang assigns the 'unsigned int' DWARF base type to 'a' and 'b' because
this is their deduced C++ type in the structured binding declaration.

However, their actual type in memory is 'unsigned short' as they have 16
bits allocated for each.

This is a problem for debug information consumers: if the debug
information for 'a' has the 'unsigned int' base type, a debugger will
assume it has 4 bytes, whereas it actually has a length of 2, resulting
in a read (or write) past its length.

This patch mimics GCC's behaviour: in case of structured bindings to
bitfields, the binding declaration's DWARF base type is of the target's
integer type with the same bitwidth as the bitfield.

If no suitable integer type is found in the target, no debug information
is emitted anymore in order to prevent wrong debug output.

Diff Detail

Event Timeline

vhscampos created this revision.Aug 9 2023, 1:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 9 2023, 1:41 AM
vhscampos requested review of this revision.Aug 9 2023, 1:41 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 9 2023, 1:41 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
tmatheson accepted this revision.Aug 10 2023, 3:52 AM

LGTM with a few cosmetic suggestions.

clang/lib/CodeGen/CGDebugInfo.cpp
4772

Can Ty be null? If not then it might be more understandable to return Ty here. If it can be null, does it make sense to fall back to BD->getType() below?

clang/test/CodeGenCXX/debug-info-structured-binding-bitfield.cpp
2

This test looks suitable for update_cc1_checks, maybe with a --filter argument and each object in it's own function so that the relevant output is next to the code that it relates to.

7

This would be easier to check if the structs had meaningful names, e.g. S_16_16

This revision is now accepted and ready to land.Aug 10 2023, 3:52 AM
vhscampos updated this revision to Diff 549359.Aug 11 2023, 5:52 AM
  • Addressed the one comment regarding code.
  • Changed the test to use update_cc_test
vhscampos marked 3 inline comments as done.Aug 11 2023, 5:53 AM
vhscampos added inline comments.
clang/test/CodeGenCXX/debug-info-structured-binding-bitfield.cpp
7

I left naming as it was, but now used your suggested code structure and also used update_cc_test

vhscampos marked an inline comment as done.Aug 11 2023, 11:20 AM
aprantl added inline comments.Aug 14 2023, 10:17 AM
clang/test/CodeGenCXX/debug-info-structured-binding-bitfield.cpp
526

This test is going to to be a nightmare to maintain since it's hardcoding all the metadata numbering. Please use FileCheck variables ![[VAR:[0-9]+]] to refer to other fields. Also, this test is probably checking too much.
The primary thing this patch changes is the data types of the fields, so the CHECK lines should focus on that.

If no suitable integer type is found in the target, no debug information is emitted anymore in order to prevent wrong debug output.

Why is emitting a bitfield type not an option?

vhscampos updated this revision to Diff 550298.Aug 15 2023, 6:00 AM
  • Redone test to cover only what's needed.
vhscampos marked an inline comment as done.Aug 15 2023, 6:08 AM

@aprantl We have discussed internally a few options to implement correct debug information in this case. But this will be future work.

For the time being, we believe it is better to have no debug information rather than wrong debug info, as can be also concluded from the first line of https://llvm.org/docs/HowToUpdateDebugInfo.html

clang/test/CodeGenCXX/debug-info-structured-binding-bitfield.cpp
526

Ok. Redone the checks to cover only what's needed.

New test LGTM, thanks!