Skip to content

[Cherry-pick] Patches related to dwarf linker and AccelTable #8106

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 89 additions & 34 deletions llvm/include/llvm/CodeGen/AccelTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/STLFunctionalExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/CodeGen/DIE.h"
Expand All @@ -25,6 +24,7 @@
#include "llvm/Support/DJB.h"
#include "llvm/Support/Debug.h"
#include <cstdint>
#include <variant>
#include <vector>

/// \file
Expand Down Expand Up @@ -103,8 +103,9 @@
namespace llvm {

class AsmPrinter;
class DwarfCompileUnit;
class DwarfUnit;
class DwarfDebug;
class DwarfTypeUnit;
class MCSymbol;
class raw_ostream;

Expand Down Expand Up @@ -196,6 +197,9 @@ template <typename DataT> class AccelTable : public AccelTableBase {

template <typename... Types>
void addName(DwarfStringPoolEntryRef Name, Types &&... Args);
void clear() { Entries.clear(); }
void addEntries(AccelTable<DataT> &Table);
const StringEntries getEntries() const { return Entries; }
};

template <typename AccelTableDataT>
Expand Down Expand Up @@ -249,46 +253,93 @@ class AppleAccelTableData : public AccelTableData {
/// emitDWARF5AccelTable function.
class DWARF5AccelTableData : public AccelTableData {
public:
struct AttributeEncoding {
dwarf::Index Index;
dwarf::Form Form;
};

static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }

DWARF5AccelTableData(const DIE &Die) : Die(Die) {}
DWARF5AccelTableData(const DIE &Die, const uint32_t UnitID,
const bool IsTU = false);
DWARF5AccelTableData(const uint64_t DieOffset, const unsigned DieTag,
const unsigned UnitID, const bool IsTU = false)
: OffsetVal(DieOffset), DieTag(DieTag), UnitID(UnitID), IsTU(IsTU) {}

#ifndef NDEBUG
void print(raw_ostream &OS) const override;
#endif

const DIE &getDie() const { return Die; }
uint64_t getDieOffset() const { return Die.getOffset(); }
unsigned getDieTag() const { return Die.getTag(); }
uint64_t getDieOffset() const {
assert(isNormalized() && "Accessing DIE Offset before normalizing.");
return std::get<uint64_t>(OffsetVal);
}
unsigned getDieTag() const { return DieTag; }
unsigned getUnitID() const { return UnitID; }
bool isTU() const { return IsTU; }
void normalizeDIEToOffset() {
assert(!isNormalized() && "Accessing offset after normalizing.");
OffsetVal = std::get<const DIE *>(OffsetVal)->getOffset();
}
bool isNormalized() const {
return std::holds_alternative<uint64_t>(OffsetVal);
}

protected:
const DIE &Die;
std::variant<const DIE *, uint64_t> OffsetVal;
uint32_t DieTag : 16;
uint32_t UnitID : 15;
uint32_t IsTU : 1;

uint64_t order() const override { return Die.getOffset(); }
uint64_t order() const override { return getDieOffset(); }
};

class DWARF5AccelTableStaticData : public AccelTableData {
public:
static uint32_t hash(StringRef Name) { return caseFoldingDjbHash(Name); }

DWARF5AccelTableStaticData(uint64_t DieOffset, unsigned DieTag,
unsigned CUIndex)
: DieOffset(DieOffset), DieTag(DieTag), CUIndex(CUIndex) {}

#ifndef NDEBUG
void print(raw_ostream &OS) const override;
#endif

uint64_t getDieOffset() const { return DieOffset; }
unsigned getDieTag() const { return DieTag; }
unsigned getCUIndex() const { return CUIndex; }
struct TypeUnitMetaInfo {
// Symbol for start of the TU section or signature if this is SplitDwarf.
std::variant<MCSymbol *, uint64_t> LabelOrSignature;
// Unique ID of Type Unit.
unsigned UniqueID;
};
using TUVectorTy = SmallVector<TypeUnitMetaInfo, 1>;
class DWARF5AccelTable : public AccelTable<DWARF5AccelTableData> {
// Symbols to start of all the TU sections that were generated.
TUVectorTy TUSymbolsOrHashes;

protected:
uint64_t DieOffset;
unsigned DieTag;
unsigned CUIndex;
public:
struct UnitIndexAndEncoding {
unsigned Index;
DWARF5AccelTableData::AttributeEncoding Encoding;
};
/// Returns type units that were constructed.
const TUVectorTy &getTypeUnitsSymbols() { return TUSymbolsOrHashes; }
/// Add a type unit start symbol.
void addTypeUnitSymbol(DwarfTypeUnit &U);
/// Add a type unit Signature.
void addTypeUnitSignature(DwarfTypeUnit &U);
/// Convert DIE entries to explicit offset.
/// Needs to be called after DIE offsets are computed.
void convertDieToOffset() {
for (auto &Entry : Entries) {
for (AccelTableData *Value : Entry.second.Values) {
DWARF5AccelTableData *Data = static_cast<DWARF5AccelTableData *>(Value);
// For TU we normalize as each Unit is emitted.
// So when this is invoked after CU construction we will be in mixed
// state.
if (!Data->isNormalized())
Data->normalizeDIEToOffset();
}
}
}

uint64_t order() const override { return DieOffset; }
void addTypeEntries(DWARF5AccelTable &Table) {
for (auto &Entry : Table.getEntries()) {
for (AccelTableData *Value : Entry.second.Values) {
DWARF5AccelTableData *Data = static_cast<DWARF5AccelTableData *>(Value);
addName(Entry.second.Name, Data->getDieOffset(), Data->getDieTag(),
Data->getUnitID(), true);
}
}
}
};

void emitAppleAccelTableImpl(AsmPrinter *Asm, AccelTableBase &Contents,
Expand All @@ -305,16 +356,20 @@ void emitAppleAccelTable(AsmPrinter *Asm, AccelTable<DataT> &Contents,
emitAppleAccelTableImpl(Asm, Contents, Prefix, SecBegin, DataT::Atoms);
}

void emitDWARF5AccelTable(AsmPrinter *Asm,
AccelTable<DWARF5AccelTableData> &Contents,
void emitDWARF5AccelTable(AsmPrinter *Asm, DWARF5AccelTable &Contents,
const DwarfDebug &DD,
ArrayRef<std::unique_ptr<DwarfCompileUnit>> CUs);

/// Emit a DWARFv5 Accelerator Table consisting of entries in the specified
/// AccelTable. The \p CUs contains either symbols keeping offsets to the
/// start of compilation unit, either offsets to the start of compilation
/// unit themselves.
void emitDWARF5AccelTable(
AsmPrinter *Asm, AccelTable<DWARF5AccelTableStaticData> &Contents,
ArrayRef<MCSymbol *> CUs,
llvm::function_ref<unsigned(const DWARF5AccelTableStaticData &)>
getCUIndexForEntry);
AsmPrinter *Asm, DWARF5AccelTable &Contents,
ArrayRef<std::variant<MCSymbol *, uint64_t>> CUs,
llvm::function_ref<std::optional<DWARF5AccelTable::UnitIndexAndEncoding>(
const DWARF5AccelTableData &)>
getIndexForEntry);

/// Accelerator table data implementation for simple Apple accelerator tables
/// with just a DIE reference.
Expand Down
5 changes: 2 additions & 3 deletions llvm/include/llvm/DWARFLinker/DWARFLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,7 @@ class DwarfEmitter {
virtual void emitLineStrings(const NonRelocatableStringpool &Pool) = 0;

/// Emit DWARF debug names.
virtual void
emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) = 0;
virtual void emitDebugNames(DWARF5AccelTable &Table) = 0;

/// Emit Apple namespaces accelerator table.
virtual void
Expand Down Expand Up @@ -896,7 +895,7 @@ class DWARFLinker {
uint32_t LastCIEOffset = 0;

/// Apple accelerator tables.
AccelTable<DWARF5AccelTableStaticData> DebugNames;
DWARF5AccelTable DebugNames;
AccelTable<AppleAccelTableStaticOffsetData> AppleNames;
AccelTable<AppleAccelTableStaticOffsetData> AppleNamespaces;
AccelTable<AppleAccelTableStaticOffsetData> AppleObjc;
Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/DWARFLinker/DWARFStreamer.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ class DwarfStreamer : public DwarfEmitter {
StringRef Bytes) override;

/// Emit DWARF debug names.
void emitDebugNames(AccelTable<DWARF5AccelTableStaticData> &Table) override;
void emitDebugNames(DWARF5AccelTable &Table) override;

/// Emit Apple namespaces accelerator table.
void emitAppleNamespaces(
Expand Down
3 changes: 0 additions & 3 deletions llvm/include/llvm/DWARFLinkerParallel/DWARFLinker.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,6 @@ class ExtraDwarfEmitter {
/// Emit section named SecName with data SecData.
virtual void emitSectionContents(StringRef SecData, StringRef SecName) = 0;

/// Emit temporarily symbol named \p SymName inside section \p SecName.
virtual MCSymbol *emitTempSym(StringRef SecName, StringRef SymName) = 0;

/// Emit the swift_ast section stored in \p Buffer.
virtual void emitSwiftAST(StringRef Buffer) = 0;

Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Analysis/LoopAccessAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/ADT/SmallSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/Analysis/AliasAnalysis.h"
#include "llvm/Analysis/AliasSetTracker.h"
#include "llvm/Analysis/LoopAnalysisManager.h"
Expand Down
1 change: 0 additions & 1 deletion llvm/lib/Bitcode/Reader/MetadataLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/Twine.h"
#include "llvm/ADT/ilist_iterator.h"
#include "llvm/ADT/iterator_range.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/Bitcode/BitcodeReader.h"
#include "llvm/Bitcode/LLVMBitCodes.h"
Expand Down
Loading