Skip to content
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

compress jit debuginfo for easy memory savings #55180

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion src/debug-registry.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,8 @@ class JITDebugInfoRegistry
private:

struct LazyObjectInfo {
std::unique_ptr<MemoryBuffer> data;
SmallVector<uint8_t, 0> data;
size_t uncompressedsize;
std::unique_ptr<const llvm::object::ObjectFile> object;
std::unique_ptr<llvm::DIContext> context;
LazyObjectInfo() = delete;
Expand Down
43 changes: 35 additions & 8 deletions src/debuginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <llvm/DebugInfo/DWARF/DWARFContext.h>
#include <llvm/Object/SymbolSize.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/MemoryBufferRef.h>
#include <llvm/IR/Function.h>
#include <llvm/ADT/StringRef.h>
#include <llvm/ADT/StringMap.h>
Expand Down Expand Up @@ -335,8 +336,12 @@ void JITDebugInfoRegistry::registerJITObject(const object::ObjectFile &Object,
#endif // defined(_OS_X86_64_)
#endif // defined(_OS_WINDOWS_)

auto ObjectCopy = new LazyObjectInfo{MemoryBuffer::getMemBufferCopy(Object.getData())}; // intentionally leaked so that we don't need to ref-count it
SmallVector<uint8_t, 0> packed;
compression::zlib::compress(ArrayRef<uint8_t>((uint8_t*)Object.getData().data(), Object.getData().size()), packed, compression::zlib::DefaultCompression);
jl_jit_add_bytes(packed.size());
auto ObjectCopy = new LazyObjectInfo{packed, Object.getData().size()}; // intentionally leaked so that we don't need to ref-count it, intentionally copied so that we exact-size the allocation (since no shrink_to_fit function)
vtjnash marked this conversation as resolved.
Show resolved Hide resolved
auto symbols = object::computeSymbolSizes(Object);
bool hassection = false;
for (const auto &sym_size : symbols) {
const object::SymbolRef &sym_iter = sym_size.first;
object::SymbolRef::Type SymbolType = cantFail(sym_iter.getType());
Expand Down Expand Up @@ -385,6 +390,7 @@ void JITDebugInfoRegistry::registerJITObject(const object::ObjectFile &Object,
jl_profile_atomic([&]() JL_NOTSAFEPOINT {
if (mi)
linfomap[Addr] = std::make_pair(Size, mi);
hassection = true;
objectmap.insert(std::pair{SectionLoadAddr, SectionInfo{
ObjectCopy,
(size_t)SectionSize,
Expand All @@ -393,6 +399,8 @@ void JITDebugInfoRegistry::registerJITObject(const object::ObjectFile &Object,
}});
});
}
if (!hassection) // clang-sa demands that we do this to fool cplusplus.NewDeleteLeaks
delete ObjectCopy;
}

void jl_register_jit_object(const object::ObjectFile &Object,
Expand Down Expand Up @@ -1211,13 +1219,32 @@ int jl_DI_for_fptr(uint64_t fptr, uint64_t *symsize, int64_t *slide,
if (fit != objmap.end() && fptr < fit->first + fit->second.SectionSize) {
*slide = fit->second.slide;
auto lazyobject = fit->second.object;
if (!lazyobject->object)
lazyobject->object = cantFail(object::ObjectFile::createObjectFile(lazyobject->data->getMemBufferRef()));
*Section = *std::next(lazyobject->object->section_begin(), fit->second.SectionIndex);
if (context) {
if (lazyobject->context == nullptr)
lazyobject->context = DWARFContext::create(*lazyobject->object);
*context = lazyobject->context.get();
if (!lazyobject->object && !lazyobject->data.empty()) {
if (lazyobject->uncompressedsize) {
SmallVector<uint8_t, 0> unpacked;
Error E = compression::zlib::decompress(lazyobject->data, unpacked, lazyobject->uncompressedsize);
if (E)
lazyobject->data.clear();
else
lazyobject->data = std::move(unpacked);
jl_jit_add_bytes(lazyobject->data.size() - lazyobject->uncompressedsize);
lazyobject->uncompressedsize = 0;
}
if (!lazyobject->data.empty()) {
auto obj = object::ObjectFile::createObjectFile(MemoryBufferRef(StringRef((const char*)lazyobject->data.data(), lazyobject->data.size()), "jit.o"));
if (obj)
lazyobject->object = std::move(*obj);
else
lazyobject->data.clear();
}
}
if (lazyobject->object) {
*Section = *std::next(lazyobject->object->section_begin(), fit->second.SectionIndex);
if (context) {
if (lazyobject->context == nullptr)
lazyobject->context = DWARFContext::create(*lazyobject->object);
*context = lazyobject->context.get();
}
}
found = 1;
}
Expand Down
1 change: 1 addition & 0 deletions src/debuginfo.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// This file is a part of Julia. License is MIT: https://julialang.org/license

// Declarations for debuginfo.cpp
void jl_jit_add_bytes(size_t bytes) JL_NOTSAFEPOINT;

int jl_DI_for_fptr(uint64_t fptr, uint64_t *symsize, int64_t *slide,
llvm::object::SectionRef *Section, llvm::DIContext **context) JL_NOTSAFEPOINT;
Expand Down
31 changes: 19 additions & 12 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -798,12 +798,12 @@ class JLDebuginfoPlugin : public ObjectLinkingLayer::Plugin {

class JLMemoryUsagePlugin : public ObjectLinkingLayer::Plugin {
private:
std::atomic<size_t> &total_size;
std::atomic<size_t> &jit_bytes_size;

public:

JLMemoryUsagePlugin(std::atomic<size_t> &total_size)
: total_size(total_size) {}
JLMemoryUsagePlugin(std::atomic<size_t> &jit_bytes_size)
: jit_bytes_size(jit_bytes_size) {}

Error notifyFailed(orc::MaterializationResponsibility &MR) override {
return Error::success();
Expand Down Expand Up @@ -852,7 +852,7 @@ class JLMemoryUsagePlugin : public ObjectLinkingLayer::Plugin {
}
(void) code_size;
(void) data_size;
this->total_size.fetch_add(graph_size, std::memory_order_relaxed);
this->jit_bytes_size.fetch_add(graph_size, std::memory_order_relaxed);
jl_timing_counter_inc(JL_TIMING_COUNTER_JITSize, graph_size);
jl_timing_counter_inc(JL_TIMING_COUNTER_JITCodeSize, code_size);
jl_timing_counter_inc(JL_TIMING_COUNTER_JITDataSize, data_size);
Expand Down Expand Up @@ -1598,7 +1598,7 @@ JuliaOJIT::JuliaOJIT()
ES, std::move(ehRegistrar)));

ObjectLayer.addPlugin(std::make_unique<JLDebuginfoPlugin>());
ObjectLayer.addPlugin(std::make_unique<JLMemoryUsagePlugin>(total_size));
ObjectLayer.addPlugin(std::make_unique<JLMemoryUsagePlugin>(jit_bytes_size));
#else
ObjectLayer.setNotifyLoaded(
[this](orc::MaterializationResponsibility &MR,
Expand Down Expand Up @@ -2010,19 +2010,20 @@ std::string JuliaOJIT::getMangledName(const GlobalValue *GV)
return getMangledName(GV->getName());
}

#ifdef JL_USE_JITLINK
size_t JuliaOJIT::getTotalBytes() const
{
return total_size.load(std::memory_order_relaxed);
auto bytes = jit_bytes_size.load(std::memory_order_relaxed);
#ifndef JL_USE_JITLINK
size_t getRTDyldMemoryManagerTotalBytes(RTDyldMemoryManager *mm) JL_NOTSAFEPOINT;
bytes += getRTDyldMemoryManagerTotalBytes(MemMgr.get());
#endif
return bytes;
}
#else
size_t getRTDyldMemoryManagerTotalBytes(RTDyldMemoryManager *mm) JL_NOTSAFEPOINT;

size_t JuliaOJIT::getTotalBytes() const
void JuliaOJIT::addBytes(size_t bytes)
{
return getRTDyldMemoryManagerTotalBytes(MemMgr.get());
jit_bytes_size.fetch_add(bytes, std::memory_order_relaxed);
}
#endif

void JuliaOJIT::printTimers()
{
Expand Down Expand Up @@ -2307,3 +2308,9 @@ size_t jl_jit_total_bytes_impl(void)
{
return jl_ExecutionEngine->getTotalBytes();
}

// API for adding bytes to record being owned by the JIT
void jl_jit_add_bytes(size_t bytes)
{
jl_ExecutionEngine->addBytes(bytes);
}
3 changes: 2 additions & 1 deletion src/jitlayers.h
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,7 @@ class JuliaOJIT {
TargetIRAnalysis getTargetIRAnalysis() const JL_NOTSAFEPOINT;

size_t getTotalBytes() const JL_NOTSAFEPOINT;
void addBytes(size_t bytes) JL_NOTSAFEPOINT;
void printTimers() JL_NOTSAFEPOINT;

jl_locked_stream &get_dump_emitted_mi_name_stream() JL_NOTSAFEPOINT {
Expand Down Expand Up @@ -605,10 +606,10 @@ class JuliaOJIT {

ResourcePool<orc::ThreadSafeContext, 0, std::queue<orc::ThreadSafeContext>> ContextPool;

std::atomic<size_t> jit_bytes_size{0};
#ifndef JL_USE_JITLINK
const std::shared_ptr<RTDyldMemoryManager> MemMgr;
#else
std::atomic<size_t> total_size{0};
const std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr;
#endif
ObjLayerT ObjectLayer;
Expand Down