The clang dSYM contained more than 3,000 copies of llvm::StringRef, one in nearly every compile unit. The C++ One Definition Rule (ODR) allows the DWARF linker in dsymutil to emit a type only once. With DWARF 5 input, the parallel linker treated StringRef::npos as a global constant. Every unit that kept npos, also kept the whole StringRef class with it. Uniquing static data members with their classes made the dSYM more than 20% smaller.

Static Data Members in DWARF

In the previous post, a.cpp and b.cpp shared a header defining geo::Point. Here, Point has one more member, a static constant named dimensions.

// geo.h
namespace geo {
struct Point {
  static const int dimensions = 2;
  int x, y;
};
inline int twice(const Point &p) { return 2 * (p.x + p.y); }
}

// a.cpp
#include "geo.h"
int use_b(const geo::Point &p);
int main() { geo::Point p{1, 2}; return geo::twice(p) + use_b(p); }

// b.cpp
#include "geo.h"
int use_b(const geo::Point &p) { return geo::twice(p); }

In DWARF 4, the declaration of dimensions is a DW_TAG_member with a DW_AT_declaration attribute. In DWARF 5, it is a DW_TAG_variable, which lets the debugger tell static and non-static members apart by their tags. Clang has used the new tag for DWARF 5 since version 18.

Compiling b.cpp with -gdwarf-5 gives these entries for dimensions and x in b.o.

DW_TAG_structure_type
  DW_AT_name ("Point")

  DW_TAG_variable
    DW_AT_name ("dimensions")
    DW_AT_type (0x0000009a "const int")
    DW_AT_external (true)
    DW_AT_declaration (true)
    DW_AT_const_value (2)

  DW_TAG_member
    DW_AT_name ("x")
    DW_AT_type (0x0000008c "int")
    DW_AT_data_member_location (0x00)

Because dimensions is initialized in the class, its declaration has a DW_AT_const_value but no location.

Liveness and Placement

The parallel linker uses the debug map to decide which debug info entries (DIEs) to keep. A function or variable is a root if the debug map has a relocation for it. The exception is a variable with only a constant value. Because it has no address to look up, it’s kept, unless it’s local to a function. That’s what made Point::dimensions a root in both object files.

A DIE that’s kept can go in its own unit, in the shared type unit, or both. Where it ends up depends on whether the function or variable only describes code or data belonging to a single object file. Wherever the DIE is kept is also where its ancestors are kept.

Duplicated Types

From dimensions, both Point and geo were kept in the same unit, together with Point’s members x and y, to keep the struct complete.

The type unit also contained a copy of Point, because of the local variable p in main (which is a Point) and the parameter of use_b (which is const Point &). Here there was no dimensions, because a variable is never kept in both its own unit and the type unit.

In the figures below, the DIEs from a.o are blue, those from b.o are orange, and linker-generated entries are green.

Before the fix: the units of a.cpp and b.cpp each contain a struct Point, and the struct Point in the type unit has no dimensions

Despite neither source file using dimensions, the member still kept a complete Point in both compile units.

Without the in-class initializer, the declaration would have had no constant value to make it a root. With -gdwarf-4, the declaration was represented by a DW_TAG_member, which also avoided the problem.

The Fix

The declaration of a static data member is part of its class and doesn’t describe storage. If there’s an out-of-class definition, it gets a separate DIE that records the member’s address and refers back to the declaration.

The fix consists of placing a DW_TAG_variable declaration in the type unit whenever its parent is a class, struct or union, regardless of how it was reached. For dimensions, the parent walk stays in the type unit all the way up to geo, and Point appears only once, with all three members.

After the fix: the type unit contains one struct Point with x, y and dimensions, and the units of a.cpp and b.cpp contain no struct Point

Results

Most of LLVM includes the StringRef header, which declares static constexpr size_t npos = ~size_t(0);. Before the fix, almost all of the roughly 3,200 compile units in the clang dSYM had a copy of StringRef, each with about a hundred child DIEs. Another example was llvm::APInt, which declares a few constants of its own and ended up with about 2,800 copies.

The fix saves more than 250 MB in .debug_info, or nearly 40% of the section. The name index, per-unit string offsets and abbreviation tables also shrink, although less dramatically. Overall, the clang dSYM goes down by more than 20%, from ~1.5 GB to ~1.2 GB, while still describing the same code.

Mixed DWARF Versions

Compiling the two files, a.cpp with -gdwarf-4 and b.cpp with -gdwarf-5, results in two object files that describe the same Point with different tags for dimensions:

  • In a.o it’s a DW_TAG_member, making x the second member.
  • In b.o it’s a DW_TAG_variable, making x the first member.

Because a member is identified in part by its position among the DW_TAG_member children of its parent, the descriptions of x were considered different and they would both (incorrectly) end up in the type unit.

A follow-up fix rectified that by leaving static data members out of this numbering, and instead identifying them independently of their tag.