Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ORC] Add DWARFContext generation from LinkGraphs, use in perf support.
This patch adds line numbers to perf jitdump records emitted by the PerfSupportPlugin, by parsing and using a DWARFContext from preserved debug sections. To avoid making the OrcJIT library depend on DebugInfoDWARF this patch introduces a new OrcDebugging library. Reviewed By: lhames Differential Revision: https://reviews.llvm.org/D146391
- Loading branch information
1 parent
2f3b7d3
commit 7ddf7d8
Showing
8 changed files
with
246 additions
and
29 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
llvm/include/llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
//===--- DebugInfoSupport.h ---- Utils for debug info support ---*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Utilities to preserve and parse debug info from LinkGraphs. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#ifndef LLVM_EXECUTIONENGINE_ORC_DEBUGINFOSUPPORT_H | ||
#define LLVM_EXECUTIONENGINE_ORC_DEBUGINFOSUPPORT_H | ||
|
||
#include "llvm/ExecutionEngine/Orc/Core.h" | ||
#include "llvm/ExecutionEngine/Orc/ObjectLinkingLayer.h" | ||
|
||
#include "llvm/DebugInfo/DWARF/DWARFContext.h" | ||
|
||
namespace llvm { | ||
|
||
namespace orc { | ||
|
||
Error preserveDebugSections(jitlink::LinkGraph &G); | ||
// The backing stringmap is also returned, for memory liftime management. | ||
Expected<std::pair<std::unique_ptr<DWARFContext>, | ||
StringMap<std::unique_ptr<MemoryBuffer>>>> | ||
createDWARFContext(jitlink::LinkGraph &G); | ||
|
||
// Thin wrapper around preserveDebugSections to be used as a standalone plugin. | ||
class DebugInfoPreservationPlugin : public ObjectLinkingLayer::Plugin { | ||
public: | ||
void modifyPassConfig(MaterializationResponsibility &MR, | ||
jitlink::LinkGraph &LG, | ||
jitlink::PassConfiguration &PassConfig) override { | ||
PassConfig.PrePrunePasses.push_back(preserveDebugSections); | ||
} | ||
|
||
Error notifyRemovingResources(JITDylib &JD, ResourceKey K) override { | ||
// Do nothing. | ||
return Error::success(); | ||
} | ||
Error notifyFailed(MaterializationResponsibility &MR) override { | ||
// Do nothing. | ||
return Error::success(); | ||
} | ||
void notifyTransferringResources(JITDylib &JD, ResourceKey DstKey, | ||
ResourceKey SrcKey) override { | ||
// Do nothing. | ||
} | ||
|
||
static Expected<std::unique_ptr<DebugInfoPreservationPlugin>> Create() { | ||
return std::make_unique<DebugInfoPreservationPlugin>(); | ||
} | ||
}; | ||
|
||
} // namespace orc | ||
|
||
} // namespace llvm | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
if( CMAKE_HOST_UNIX AND HAVE_LIBRT ) | ||
set(rt_lib rt) | ||
endif() | ||
|
||
add_llvm_component_library(LLVMOrcDebugging | ||
DebugInfoSupport.cpp | ||
PerfSupportPlugin.cpp | ||
|
||
ADDITIONAL_HEADER_DIRS | ||
${LLVM_MAIN_INCLUDE_DIR}/llvm/ExecutionEngine/Orc/Debugging/ | ||
|
||
LINK_LIBS | ||
${LLVM_PTHREAD_LIB} | ||
${rt_lib} | ||
|
||
LINK_COMPONENTS | ||
DebugInfoDWARF | ||
OrcJIT | ||
OrcShared | ||
Support | ||
TargetParser | ||
) |
120 changes: 120 additions & 0 deletions
120
llvm/lib/ExecutionEngine/Orc/Debugging/DebugInfoSupport.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
//===--- DebugInfoSupport.cpp -- Utils for debug info support ---*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// Utilities to preserve and parse debug info from LinkGraphs. | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "llvm/ExecutionEngine/Orc/Debugging/DebugInfoSupport.h" | ||
|
||
#include "llvm/Support/SmallVectorMemoryBuffer.h" | ||
|
||
#define DEBUG_TYPE "orc" | ||
|
||
using namespace llvm; | ||
using namespace llvm::orc; | ||
using namespace llvm::jitlink; | ||
|
||
namespace { | ||
static DenseSet<StringRef> DWARFSectionNames = { | ||
#define HANDLE_DWARF_SECTION(ENUM_NAME, ELF_NAME, CMDLINE_NAME, OPTION) \ | ||
StringRef(ELF_NAME), | ||
#include "llvm/BinaryFormat/Dwarf.def" | ||
#undef HANDLE_DWARF_SECTION | ||
}; | ||
|
||
// We might be able to drop relocations to symbols that do end up | ||
// being pruned by the linker, but for now we just preserve all | ||
static void preserveDWARFSection(LinkGraph &G, Section &Sec) { | ||
DenseMap<Block *, Symbol *> Preserved; | ||
for (auto Sym : Sec.symbols()) { | ||
if (Sym->isLive()) | ||
Preserved[&Sym->getBlock()] = Sym; | ||
else if (!Preserved.count(&Sym->getBlock())) | ||
Preserved[&Sym->getBlock()] = Sym; | ||
} | ||
for (auto Block : Sec.blocks()) { | ||
auto &PSym = Preserved[Block]; | ||
if (!PSym) | ||
PSym = &G.addAnonymousSymbol(*Block, 0, 0, false, true); | ||
else if (!PSym->isLive()) | ||
PSym->setLive(true); | ||
} | ||
} | ||
|
||
static SmallVector<char, 0> getSectionData(Section &Sec) { | ||
SmallVector<char, 0> SecData; | ||
SmallVector<Block *, 8> SecBlocks(Sec.blocks().begin(), Sec.blocks().end()); | ||
std::sort(SecBlocks.begin(), SecBlocks.end(), [](Block *LHS, Block *RHS) { | ||
return LHS->getAddress() < RHS->getAddress(); | ||
}); | ||
// Convert back to what object file would have, one blob of section content | ||
// Assumes all zerofill | ||
// TODO handle alignment? | ||
// TODO handle alignment offset? | ||
for (auto *Block : SecBlocks) { | ||
if (Block->isZeroFill()) | ||
SecData.resize(SecData.size() + Block->getSize(), 0); | ||
else | ||
SecData.append(Block->getContent().begin(), Block->getContent().end()); | ||
} | ||
return SecData; | ||
} | ||
|
||
static void dumpDWARFContext(DWARFContext &DC) { | ||
auto options = llvm::DIDumpOptions(); | ||
options.DumpType &= ~DIDT_UUID; | ||
options.DumpType &= ~(1 << DIDT_ID_DebugFrame); | ||
LLVM_DEBUG(DC.dump(dbgs(), options)); | ||
} | ||
|
||
} // namespace | ||
|
||
Error llvm::orc::preserveDebugSections(LinkGraph &G) { | ||
if (!G.getTargetTriple().isOSBinFormatELF()) { | ||
return make_error<StringError>( | ||
"preserveDebugSections only supports ELF LinkGraphs!", | ||
inconvertibleErrorCode()); | ||
} | ||
for (auto &Sec : G.sections()) { | ||
if (DWARFSectionNames.count(Sec.getName())) { | ||
LLVM_DEBUG(dbgs() << "Preserving DWARF section " << Sec.getName() | ||
<< "\n"); | ||
preserveDWARFSection(G, Sec); | ||
} | ||
} | ||
return Error::success(); | ||
} | ||
|
||
Expected<std::pair<std::unique_ptr<DWARFContext>, | ||
StringMap<std::unique_ptr<MemoryBuffer>>>> | ||
llvm::orc::createDWARFContext(LinkGraph &G) { | ||
if (!G.getTargetTriple().isOSBinFormatELF()) { | ||
return make_error<StringError>( | ||
"createDWARFContext only supports ELF LinkGraphs!", | ||
inconvertibleErrorCode()); | ||
} | ||
StringMap<std::unique_ptr<MemoryBuffer>> DWARFSectionData; | ||
for (auto &Sec : G.sections()) { | ||
if (DWARFSectionNames.count(Sec.getName())) { | ||
auto SecData = getSectionData(Sec); | ||
auto Name = Sec.getName(); | ||
// DWARFContext expects the section name to not start with a dot | ||
if (Name.startswith(".")) | ||
Name = Name.drop_front(); | ||
LLVM_DEBUG(dbgs() << "Creating DWARFContext section " << Name | ||
<< " with size " << SecData.size() << "\n"); | ||
DWARFSectionData[Name] = | ||
std::make_unique<SmallVectorMemoryBuffer>(std::move(SecData)); | ||
} | ||
} | ||
auto Ctx = DWARFContext::create(DWARFSectionData, G.getPointerSize(), | ||
G.getEndianness() == support::little); | ||
dumpDWARFContext(*Ctx); | ||
return std::make_pair(std::move(Ctx), std::move(DWARFSectionData)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ set(LLVM_LINK_COMPONENTS | |
JITLink | ||
MC | ||
Object | ||
OrcDebugging | ||
OrcJIT | ||
OrcShared | ||
OrcTargetProcess | ||
|
Oops, something went wrong.