Skip to content

Commit 0c3751f

Browse files
committed
compress DebugObject to save memory
Account for debug info memory as well, since this info is usually about 10x the size of the data that was being tracked, although this compression appears to reduce that by at least half.
1 parent 492ae63 commit 0c3751f

File tree

5 files changed

+55
-22
lines changed

5 files changed

+55
-22
lines changed

src/debug-registry.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ class JITDebugInfoRegistry
100100
private:
101101

102102
struct LazyObjectInfo {
103-
std::unique_ptr<MemoryBuffer> data;
103+
SmallVector<uint8_t, 0> data;
104+
size_t uncompressedsize;
104105
std::unique_ptr<const llvm::object::ObjectFile> object;
105106
std::unique_ptr<llvm::DIContext> context;
106107
LazyObjectInfo() = delete;

src/debuginfo.cpp

+31-8
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <llvm/DebugInfo/DWARF/DWARFContext.h>
88
#include <llvm/Object/SymbolSize.h>
99
#include <llvm/Support/MemoryBuffer.h>
10+
#include <llvm/Support/MemoryBufferRef.h>
1011
#include <llvm/IR/Function.h>
1112
#include <llvm/ADT/StringRef.h>
1213
#include <llvm/ADT/StringMap.h>
@@ -335,7 +336,10 @@ void JITDebugInfoRegistry::registerJITObject(const object::ObjectFile &Object,
335336
#endif // defined(_OS_X86_64_)
336337
#endif // defined(_OS_WINDOWS_)
337338

338-
auto ObjectCopy = new LazyObjectInfo{MemoryBuffer::getMemBufferCopy(Object.getData())}; // intentionally leaked so that we don't need to ref-count it
339+
SmallVector<uint8_t, 0> packed;
340+
compression::zlib::compress(ArrayRef<uint8_t>((uint8_t*)Object.getData().data(), Object.getData().size()), packed, compression::zlib::DefaultCompression);
341+
jl_jit_add_bytes(packed.size());
342+
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)
339343
auto symbols = object::computeSymbolSizes(Object);
340344
for (const auto &sym_size : symbols) {
341345
const object::SymbolRef &sym_iter = sym_size.first;
@@ -1211,13 +1215,32 @@ int jl_DI_for_fptr(uint64_t fptr, uint64_t *symsize, int64_t *slide,
12111215
if (fit != objmap.end() && fptr < fit->first + fit->second.SectionSize) {
12121216
*slide = fit->second.slide;
12131217
auto lazyobject = fit->second.object;
1214-
if (!lazyobject->object)
1215-
lazyobject->object = cantFail(object::ObjectFile::createObjectFile(lazyobject->data->getMemBufferRef()));
1216-
*Section = *std::next(lazyobject->object->section_begin(), fit->second.SectionIndex);
1217-
if (context) {
1218-
if (lazyobject->context == nullptr)
1219-
lazyobject->context = DWARFContext::create(*lazyobject->object);
1220-
*context = lazyobject->context.get();
1218+
if (!lazyobject->object && !lazyobject->data.empty()) {
1219+
if (lazyobject->uncompressedsize) {
1220+
SmallVector<uint8_t, 0> unpacked;
1221+
Error E = compression::zlib::decompress(lazyobject->data, unpacked, lazyobject->uncompressedsize);
1222+
if (E)
1223+
lazyobject->data.clear();
1224+
else
1225+
lazyobject->data = std::move(unpacked);
1226+
jl_jit_add_bytes(lazyobject->data.size() - lazyobject->uncompressedsize);
1227+
lazyobject->uncompressedsize = 0;
1228+
}
1229+
if (!lazyobject->data.empty()) {
1230+
auto obj = object::ObjectFile::createObjectFile(MemoryBufferRef(StringRef((const char*)lazyobject->data.data(), lazyobject->data.size()), "jit.o"));
1231+
if (obj)
1232+
lazyobject->object = std::move(*obj);
1233+
else
1234+
lazyobject->data.clear();
1235+
}
1236+
}
1237+
if (lazyobject->object) {
1238+
*Section = *std::next(lazyobject->object->section_begin(), fit->second.SectionIndex);
1239+
if (context) {
1240+
if (lazyobject->context == nullptr)
1241+
lazyobject->context = DWARFContext::create(*lazyobject->object);
1242+
*context = lazyobject->context.get();
1243+
}
12211244
}
12221245
found = 1;
12231246
}

src/debuginfo.h

+1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// This file is a part of Julia. License is MIT: https://julialang.org/license
22

33
// Declarations for debuginfo.cpp
4+
void jl_jit_add_bytes(size_t bytes) JL_NOTSAFEPOINT;
45

56
int jl_DI_for_fptr(uint64_t fptr, uint64_t *symsize, int64_t *slide,
67
llvm::object::SectionRef *Section, llvm::DIContext **context) JL_NOTSAFEPOINT;

src/jitlayers.cpp

+19-12
Original file line numberDiff line numberDiff line change
@@ -781,12 +781,12 @@ class JLDebuginfoPlugin : public ObjectLinkingLayer::Plugin {
781781

782782
class JLMemoryUsagePlugin : public ObjectLinkingLayer::Plugin {
783783
private:
784-
std::atomic<size_t> &total_size;
784+
std::atomic<size_t> &jit_bytes_size;
785785

786786
public:
787787

788-
JLMemoryUsagePlugin(std::atomic<size_t> &total_size)
789-
: total_size(total_size) {}
788+
JLMemoryUsagePlugin(std::atomic<size_t> &jit_bytes_size)
789+
: jit_bytes_size(jit_bytes_size) {}
790790

791791
Error notifyFailed(orc::MaterializationResponsibility &MR) override {
792792
return Error::success();
@@ -835,7 +835,7 @@ class JLMemoryUsagePlugin : public ObjectLinkingLayer::Plugin {
835835
}
836836
(void) code_size;
837837
(void) data_size;
838-
this->total_size.fetch_add(graph_size, std::memory_order_relaxed);
838+
this->jit_bytes_size.fetch_add(graph_size, std::memory_order_relaxed);
839839
jl_timing_counter_inc(JL_TIMING_COUNTER_JITSize, graph_size);
840840
jl_timing_counter_inc(JL_TIMING_COUNTER_JITCodeSize, code_size);
841841
jl_timing_counter_inc(JL_TIMING_COUNTER_JITDataSize, data_size);
@@ -1579,7 +1579,7 @@ JuliaOJIT::JuliaOJIT()
15791579
ES, std::move(ehRegistrar)));
15801580

15811581
ObjectLayer.addPlugin(std::make_unique<JLDebuginfoPlugin>());
1582-
ObjectLayer.addPlugin(std::make_unique<JLMemoryUsagePlugin>(total_size));
1582+
ObjectLayer.addPlugin(std::make_unique<JLMemoryUsagePlugin>(jit_bytes_size));
15831583
#else
15841584
ObjectLayer.setNotifyLoaded(
15851585
[this](orc::MaterializationResponsibility &MR,
@@ -1991,19 +1991,20 @@ std::string JuliaOJIT::getMangledName(const GlobalValue *GV)
19911991
return getMangledName(GV->getName());
19921992
}
19931993

1994-
#ifdef JL_USE_JITLINK
19951994
size_t JuliaOJIT::getTotalBytes() const
19961995
{
1997-
return total_size.load(std::memory_order_relaxed);
1996+
auto bytes = jit_bytes_size.load(std::memory_order_relaxed);
1997+
#ifndef JL_USE_JITLINK
1998+
size_t getRTDyldMemoryManagerTotalBytes(RTDyldMemoryManager *mm) JL_NOTSAFEPOINT;
1999+
bytes += getRTDyldMemoryManagerTotalBytes(MemMgr.get());
2000+
#endif
2001+
return bytes;
19982002
}
1999-
#else
2000-
size_t getRTDyldMemoryManagerTotalBytes(RTDyldMemoryManager *mm) JL_NOTSAFEPOINT;
20012003

2002-
size_t JuliaOJIT::getTotalBytes() const
2004+
void JuliaOJIT::addBytes(size_t bytes)
20032005
{
2004-
return getRTDyldMemoryManagerTotalBytes(MemMgr.get());
2006+
jit_bytes_size.fetch_add(bytes, std::memory_order_relaxed);
20052007
}
2006-
#endif
20072008

20082009
void JuliaOJIT::printTimers()
20092010
{
@@ -2288,3 +2289,9 @@ size_t jl_jit_total_bytes_impl(void)
22882289
{
22892290
return jl_ExecutionEngine->getTotalBytes();
22902291
}
2292+
2293+
// API for adding bytes to record being owned by the JIT
2294+
void jl_jit_add_bytes(size_t bytes)
2295+
{
2296+
jl_ExecutionEngine->addBytes(bytes);
2297+
}

src/jitlayers.h

+2-1
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,7 @@ class JuliaOJIT {
559559
TargetIRAnalysis getTargetIRAnalysis() const JL_NOTSAFEPOINT;
560560

561561
size_t getTotalBytes() const JL_NOTSAFEPOINT;
562+
void addBytes(size_t bytes) JL_NOTSAFEPOINT;
562563
void printTimers() JL_NOTSAFEPOINT;
563564

564565
jl_locked_stream &get_dump_emitted_mi_name_stream() JL_NOTSAFEPOINT {
@@ -605,10 +606,10 @@ class JuliaOJIT {
605606

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

609+
std::atomic<size_t> jit_bytes_size{0};
608610
#ifndef JL_USE_JITLINK
609611
const std::shared_ptr<RTDyldMemoryManager> MemMgr;
610612
#else
611-
std::atomic<size_t> total_size{0};
612613
const std::unique_ptr<jitlink::JITLinkMemoryManager> MemMgr;
613614
#endif
614615
ObjLayerT ObjectLayer;

0 commit comments

Comments
 (0)