1
0
mirror of https://github.com/RPCS3/llvm-mirror.git synced 2024-10-22 20:43:44 +02:00
llvm-mirror/lib/CodeGen/AsmPrinter/DebugLocStream.h
Duncan P. N. Exon Smith cfe0f9cca6 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
2015-04-17 21:34:47 +00:00

130 lines
4.1 KiB
C++

//===--- lib/CodeGen/DebugLocStream.h - DWARF debug_loc stream --*- C++ -*-===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
#define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCSTREAM_H
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/SmallVector.h"
#include "ByteStreamer.h"
namespace llvm {
class DwarfCompileUnit;
class MCSymbol;
/// \brief Byte stream of .debug_loc entries.
///
/// Stores a unified stream of .debug_loc entries. There's \a List for each
/// variable/inlined-at pair, and an \a Entry for each \a DebugLocEntry.
///
/// FIXME: Why do we have comments even when it's an object stream?
/// FIXME: Do we need all these temp symbols?
/// FIXME: Why not output directly to the output stream?
class DebugLocStream {
public:
struct List {
DwarfCompileUnit *CU;
MCSymbol *Label;
size_t EntryOffset;
List(DwarfCompileUnit *CU, MCSymbol *Label, size_t EntryOffset)
: CU(CU), Label(Label), EntryOffset(EntryOffset) {}
};
struct Entry {
const MCSymbol *BeginSym;
const MCSymbol *EndSym;
size_t ByteOffset;
size_t CommentOffset;
Entry(const MCSymbol *BeginSym, const MCSymbol *EndSym, size_t ByteOffset,
size_t CommentOffset)
: BeginSym(BeginSym), EndSym(EndSym), ByteOffset(ByteOffset),
CommentOffset(CommentOffset) {}
};
private:
SmallVector<List, 4> Lists;
SmallVector<Entry, 32> Entries;
SmallString<256> DWARFBytes;
SmallVector<std::string, 32> Comments;
public:
size_t getNumLists() const { return Lists.size(); }
const List &getList(size_t LI) const { return Lists[LI]; }
ArrayRef<List> getLists() const { return Lists; }
/// \brief Start a new .debug_loc entry list.
///
/// Start a new .debug_loc entry list. Return the new list's index so it can
/// be retrieved later via \a getList().
///
/// Until the next call, \a startEntry() will add entries to this list.
size_t startList(DwarfCompileUnit *CU, MCSymbol *Label) {
size_t LI = Lists.size();
Lists.emplace_back(CU, Label, Entries.size());
return LI;
}
/// \brief Start a new .debug_loc entry.
///
/// Until the next call, bytes added to the stream will be added to this
/// entry.
void startEntry(const MCSymbol *BeginSym, const MCSymbol *EndSym) {
Entries.emplace_back(BeginSym, EndSym, DWARFBytes.size(), Comments.size());
}
BufferByteStreamer getStreamer() {
return BufferByteStreamer(DWARFBytes, Comments);
}
ArrayRef<Entry> getEntries(const List &L) const {
size_t LI = getIndex(L);
return makeArrayRef(Entries)
.slice(Lists[LI].EntryOffset, getNumEntries(LI));
}
ArrayRef<char> getBytes(const Entry &E) const {
size_t EI = getIndex(E);
return makeArrayRef(DWARFBytes.begin(), DWARFBytes.end())
.slice(Entries[EI].ByteOffset, getNumBytes(EI));
}
ArrayRef<std::string> getComments(const Entry &E) const {
size_t EI = getIndex(E);
return makeArrayRef(Comments)
.slice(Entries[EI].CommentOffset, getNumComments(EI));
}
private:
size_t getIndex(const List &L) const {
assert(&Lists.front() <= &L && &L <= &Lists.back() &&
"Expected valid list");
return &L - &Lists.front();
}
size_t getIndex(const Entry &E) const {
assert(&Entries.front() <= &E && &E <= &Entries.back() &&
"Expected valid entry");
return &E - &Entries.front();
}
size_t getNumEntries(size_t LI) const {
if (LI + 1 == Lists.size())
return Entries.size() - Lists[LI].EntryOffset;
return Lists[LI + 1].EntryOffset - Lists[LI].EntryOffset;
}
size_t getNumBytes(size_t EI) const {
if (EI + 1 == Entries.size())
return DWARFBytes.size() - Entries[EI].ByteOffset;
return Entries[EI + 1].ByteOffset - Entries[EI].ByteOffset;
}
size_t getNumComments(size_t EI) const {
if (EI + 1 == Entries.size())
return Comments.size() - Entries[EI].CommentOffset;
return Entries[EI + 1].CommentOffset - Entries[EI].CommentOffset;
}
};
}
#endif