This is an archive of the discontinued LLVM Phabricator instance.

[clang][DebugInfo] lldb: Use preferred name's type when emitting DW_AT_names
Needs ReviewPublic

Authored by Michael137 on Feb 7 2023, 7:49 AM.

Details

Reviewers
aprantl
dblaikie
Summary

Summary

This patch makes debug-info generation aware of the
[[clang::preferred_name]] attribute. The attribute tells clang
to print the annotated class template as some typedef (e.g., in
diagnostics).

When printing a typename for emission into DW_AT_name this patch now
uses the preferred_name (if available). This is behind an LLDB tuning
because by default we try to avoid diverging GCC and Clang typename
format (which is why the PrintingPolicy::UsePreferredNames has
previously been disabled by default in CGDebugInfo).

Motivation

This will reduce noise in type summaries when showing variable
types in LLDB. E.g.,:

(lldb) v
(std::vector<std::basic_string<char> >) vec = size=0 {}

becomes

(lldb) v
(std::vector<std::string>) vec = size=0 {}

Testing

  • Added Clang test
  • Adjusted LLDB API tests

Diff Detail

Event Timeline

Michael137 created this revision.Feb 7 2023, 7:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 7 2023, 7:49 AM
Michael137 requested review of this revision.Feb 7 2023, 7:49 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptFeb 7 2023, 7:49 AM

The alternative would be attaching the preferred name as a new attribute or an existing attribute like DW_AT_description.

Nice! Does expr -- std::basic_string<char> s still work after this change? Not that anyone would want to type this over std::string ...

clang/test/CodeGen/debug-info-preferred-names.cpp
2

Is -debug-info-kind=limited needed here? Using it here is odd since LLDB doesn't really support it and favors =standalone instead.

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/shared_ptr/TestDataFormatterLibcxxSharedPtr.py
61

Out of curiosity: How does this function work when building the testsuite with a compiler that isn't Clang?
I vaguely recall there was someone in the community running a bot that built the LLDB testsuite against GCC.

Michael137 added inline comments.Feb 8 2023, 10:40 AM
clang/test/CodeGen/debug-info-preferred-names.cpp
2

Not needed, just leftover from different test. Will change

lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/shared_ptr/TestDataFormatterLibcxxSharedPtr.py
61

Good point, this should also check for the compiler really. I assume because GCC isn't at that version yet it just works on that GCC buildbot

Will change in a different patch since there's other tests with this condition

Nice! Does expr -- std::basic_string<char> s still work after this change? Not that anyone would want to type this over std::string ...

Yup that still works. We would still emit it as basic_string if that was typed out in the source and that's how LLDB would show it.

Michael137 retitled this revision from [WIP][clang][DebugInfo] lldb: Use preferred name's type when emitting DW_AT_names to [clang][DebugInfo] lldb: Use preferred name's type when emitting DW_AT_names.Feb 9 2023, 4:40 AM
Michael137 updated this revision to Diff 496089.Feb 9 2023, 4:44 AM
  • Reword commit
  • Use different debuginfo kind in tests
Michael137 added inline comments.Feb 9 2023, 8:02 AM
lldb/test/API/functionalities/data-formatter/data-formatter-stl/libcxx/shared_ptr/TestDataFormatterLibcxxSharedPtr.py
61

The alternative would be attaching the preferred name as a new attribute or an existing attribute like DW_AT_description.

I'd recommend a possible long-term solution would be simplified template names (so we don't have to worry about encoding this in the DW_AT_name anyway) and a "DW_AT_LLVM_preferred_name" or similar attribute on a type that refers to the typedef that is the preferred name. This would generalize further than only appearing in template names - the type of a variable inside a template would also gain the beneficial naming (eg: template<typename T> void f1(T t) { } - as it stands, the type of the variable t must be std::basic_string<char... - but if the DW_TAG_class_type for std::basic_string<char... had this preferred-name attribute on it, then a debugger could helpfully render the type by its preferred alias instead)

Alternatively, I suppose, the DWARF emission could just look at the preferred name and use that as the DW_AT_type in all cases anyway? Avoids needing a new attribute, etc, though would be a bit quirky in its own way.

Doing this ^ would also then fix the divergence introduced by changing the naming - and would ensure simple template names still match up with names rebuilt from the DW_TAG_template_type_parameters.

I'd recommend a possible long-term solution would be simplified template names (so we don't have to worry about encoding this in the DW_AT_name anyway) and a "DW_AT_LLVM_preferred_name" or similar attribute on a type that refers to the typedef that is the preferred name. This would generalize further than only appearing in template names - the type of a variable inside a template would also gain the beneficial naming (eg: template<typename T> void f1(T t) { } - as it stands, the type of the variable t must be std::basic_string<char... - but if the DW_TAG_class_type for std::basic_string<char... had this preferred-name attribute on it, then a debugger could helpfully render the type by its preferred alias instead)

That could be a neat solution. I probably asked this before, but what's the timeline with switching it on by default (if such plan is in the works at all)?

Alternatively, I suppose, the DWARF emission could just look at the preferred name and use that as the DW_AT_type in all cases anyway? Avoids needing a new attribute, etc, though would be a bit quirky in its own way.

This is how I first thought of implementing it, but ran into some circular dependency quirks and started working on this patch instead. Could take a second stab at doing so if that's the preference. Would be nice to not have this behind a tuning

I'd recommend a possible long-term solution would be simplified template names (so we don't have to worry about encoding this in the DW_AT_name anyway) and a "DW_AT_LLVM_preferred_name" or similar attribute on a type that refers to the typedef that is the preferred name. This would generalize further than only appearing in template names - the type of a variable inside a template would also gain the beneficial naming (eg: template<typename T> void f1(T t) { } - as it stands, the type of the variable t must be std::basic_string<char... - but if the DW_TAG_class_type for std::basic_string<char... had this preferred-name attribute on it, then a debugger could helpfully render the type by its preferred alias instead)

That could be a neat solution. I probably asked this before, but what's the timeline with switching it on by default (if such plan is in the works at all)?

I haven't especially planned that - though given it's been picked up by Fuschia and Chromium (at least in some build modes), and we got gdb and lldb mostly fixed, maybe it's worth considering. Defaulting on for lldb might be easier than for gdb (gdb has some outstanding index bugs with it). But generally I figure you Apple folks tend to be the ones who have more investment in what the lldb tuning should cover, or not?

If you folks want to try turning it on & we could see about turning it on by default for lldb tuning?

Alternatively, I suppose, the DWARF emission could just look at the preferred name and use that as the DW_AT_type in all cases anyway? Avoids needing a new attribute, etc, though would be a bit quirky in its own way.

This is how I first thought of implementing it, but ran into some circular dependency quirks and started working on this patch instead. Could take a second stab at doing so if that's the preference. Would be nice to not have this behind a tuning

yeah, happy to help with pointers, etc.

I think /maybe/ we had some design principle that DWARF features wouldn't be solely controlled by debugger tuning, the tuning only indicates defaults but tehy can be controlled by flags? Not sure if I'm remembering that quite right, though - maybe @probinson remembers more of that?

I think /maybe/ we had some design principle that DWARF features wouldn't be solely controlled by debugger tuning, the tuning only indicates defaults but tehy can be controlled by flags? Not sure if I'm remembering that quite right, though - maybe @probinson remembers more of that?

I guess maybe not - at least there's a handful of debugger tuning checks in similar code around here.

Alternatively, I suppose, the DWARF emission could just look at the preferred name and use that as the DW_AT_type in all cases anyway? Avoids needing a new attribute, etc, though would be a bit quirky in its own way.

Am I understanding you correctly that you suggest we add a new preferred name attribute to DICompositeType and then swap out all uses of those types with references to a typedef using the preferred name and pointing to the unmodified type declaration in AsmPrinter?
Why wouldn't we just implement this directly in CGDebugInfo and emit a DIDerivedType(name: "preferred", type: <original type>) and replace all uses of <original type> there? Then we wouldn't have to modify the IR format at all?