AsmPrinter: Create a unified .debug_loc stream
authorDuncan P. N. Exon Smith <dexonsmith@apple.com>
Fri, 17 Apr 2015 21:34:47 +0000 (21:34 +0000)
committerDuncan P. N. Exon Smith <dexonsmith@apple.com>
Fri, 17 Apr 2015 21:34:47 +0000 (21:34 +0000)
commit364a3005f2b62f81d0bcf81fa48eb78ce0024dde
tree29b1aac29e04ad3044c80fed770a0db2caabb49e
parent2bbbd8b543fea8b3218c27b9cbd0416062fbe423
AsmPrinter: Create a unified .debug_loc stream

This commit removes `DebugLocList` and replaces it with
`DebugLocStream`.

  - `DebugLocEntry` no longer contains its byte/comment streams.
  - The `DebugLocEntry` list for a variable/inlined-at pair is allocated
    on the stack, and released right after `DebugLocEntry::finalize()`
    (possible because of the refactoring in r231023).  Now, only one
    list is in memory at a time now.
  - There's a single unified stream for the `.debug_loc` section that
    persists, stored in the new `DebugLocStream` data structure.

The last point is important: this collapses the nested `SmallVector<>`s
from `DebugLocList` into unified streams.  We previously had something
like the following:

    vec<tuple<Label, CU,
              vec<tuple<BeginSym, EndSym,
                        vec<Value>,
                        vec<char>,
                        vec<string>>>>>

A `SmallVector` can avoid allocations, but is statically fairly large
for a vector: three pointers plus the size of the small storage, which
is the number of elements in small mode times the element size).
Nesting these is expensive, since an inner vector's size contributes to
the element size of an outer one.  (Nesting any vector is expensive...)

In the old data structure, the outer vector's *element* size was 632B,
excluding allocation costs for when the middle and inner vectors
exceeded their small sizes.  312B of this was for the "three" pointers
in the vector-tree beneath it.  If you assume 1M functions with an
average of 10 variable/inlined-at pairs each (in an LTO scenario),
that's almost 6GB (besides inner allocations), with almost 3GB for the
"three" pointers.

This came up in a heap profile a little while ago of a `clang -flto -g`
bootstrap, with `DwarfDebug::collectVariableInfo()` using something like
10-15% of the total memory.

With this commit, we have:

    tuple<vec<tuple<Label, CU, Offset>>,
          vec<tuple<BeginSym, EndSym, Offset, Offset>>,
          vec<char>,
          vec<string>>

The offsets are used to create `ArrayRef` slices of adjacent
`SmallVector`s.  This reduces the number of vectors to four (unrelated
to the number of variable/inlined-at pairs), and caps the number of
allocations at the same number.

Besides saving memory and limiting allocations, this is NFC.

I don't know my way around this code very well yet, but I wonder if we
could go further: why stream to a side-table, instead of directly to the
output stream?

llvm-svn: 235229
llvm/lib/CodeGen/AsmPrinter/DIE.cpp
llvm/lib/CodeGen/AsmPrinter/DIEHash.cpp
llvm/lib/CodeGen/AsmPrinter/DebugLocEntry.h
llvm/lib/CodeGen/AsmPrinter/DebugLocList.h [deleted file]
llvm/lib/CodeGen/AsmPrinter/DebugLocStream.h [new file with mode: 0644]
llvm/lib/CodeGen/AsmPrinter/DwarfCompileUnit.cpp
llvm/lib/CodeGen/AsmPrinter/DwarfDebug.cpp
llvm/lib/CodeGen/AsmPrinter/DwarfDebug.h