Skip to content

Commit

Permalink
Move memory manager out of debuginfo.cpp
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Jun 7, 2016
1 parent b396b4d commit 3c8eed9
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 73 deletions.
17 changes: 5 additions & 12 deletions src/codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@
#include "llvm/ExecutionEngine/ObjectMemoryBuffer.h"
#elif defined(USE_MCJIT)
#include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/ExecutionEngine/SectionMemoryManager.h>
#include <llvm/ADT/DenseMapInfo.h>
#include <llvm/Object/ObjectFile.h>
#else
Expand Down Expand Up @@ -335,15 +334,11 @@ static GlobalVariable *jlRTLD_DEFAULT_var;
static GlobalVariable *jlexe_var;
static GlobalVariable *jldll_var;
#if defined(_CPU_X86_64_) && !defined(USE_MCJIT)
extern JITMemoryManager *createJITMemoryManagerWin();
JITMemoryManager *createJITMemoryManagerWin();
#endif
#endif //_OS_WINDOWS_
#if defined(_OS_DARWIN_) && defined(LLVM37) && defined(LLVM_SHLIB)
#define CUSTOM_MEMORY_MANAGER createRTDyldMemoryManagerOSX
extern RTDyldMemoryManager *createRTDyldMemoryManagerOSX();
#elif defined(_OS_LINUX_) && defined(LLVM37) && defined(JL_UNW_HAS_FORMAT_IP)
#define CUSTOM_MEMORY_MANAGER createRTDyldMemoryManagerUnix
extern RTDyldMemoryManager *createRTDyldMemoryManagerUnix();
#ifdef USE_MCJIT
RTDyldMemoryManager *createRTDyldMemoryManager();
#endif

static Function *jltls_states_func;
Expand Down Expand Up @@ -5731,10 +5726,8 @@ extern "C" void jl_init_codegen(void)
eb .setEngineKind(EngineKind::JIT)
#if defined(_OS_WINDOWS_) && defined(_CPU_X86_64_) && !defined(USE_MCJIT)
.setJITMemoryManager(createJITMemoryManagerWin())
#elif defined(CUSTOM_MEMORY_MANAGER)
.setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>{CUSTOM_MEMORY_MANAGER()})
#elif defined(USE_ORCMCJIT) // ORCJIT forgets to create one if one isn't created for it
.setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>{new SectionMemoryManager()})
#elif defined(USE_MCJIT)
.setMCJITMemoryManager(std::unique_ptr<RTDyldMemoryManager>{createRTDyldMemoryManager()})
#endif
.setTargetOptions(options)
#if (defined(_OS_LINUX_) && defined(_CPU_X86_64_))
Expand Down
61 changes: 12 additions & 49 deletions src/debuginfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1341,28 +1341,11 @@ static void processFDEs(const char *EHFrameAddr, size_t EHFrameSize, callback f)
* ourselves to ensure the right one gets picked.
*/

#include "llvm/ExecutionEngine/SectionMemoryManager.h"
class RTDyldMemoryManagerOSX : public SectionMemoryManager
{
RTDyldMemoryManagerOSX(const RTDyldMemoryManagerOSX&) = delete;
void operator=(const RTDyldMemoryManagerOSX&) = delete;

public:
RTDyldMemoryManagerOSX() {};
~RTDyldMemoryManagerOSX() override {};
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
};

static void (*libc_register_frame)(void*) = NULL;
static void (*libc_deregister_frame)(void*) = NULL;

// This implementation handles frame registration for local targets.
// Memory managers for remote targets should re-implement this function
// and use the LoadAddr parameter.
void RTDyldMemoryManagerOSX::registerEHFrames(uint8_t *Addr,
uint64_t LoadAddr,
size_t Size)
void register_eh_frames(uint8_t *Addr, size_t Size)
{
// On OS X OS X __register_frame takes a single FDE as an argument.
// See http://lists.cs.uiuc.edu/pipermail/llvmdev/2013-April/061768.html
Expand All @@ -1376,9 +1359,7 @@ void RTDyldMemoryManagerOSX::registerEHFrames(uint8_t *Addr,
});
}

void RTDyldMemoryManagerOSX::deregisterEHFrames(uint8_t *Addr,
uint64_t LoadAddr,
size_t Size)
void deregister_eh_frames(uint8_t *Addr, size_t Size)
{
processFDEs((char*)Addr, Size, [](const char *Entry) {
if (!libc_deregister_frame) {
Expand All @@ -1390,27 +1371,8 @@ void RTDyldMemoryManagerOSX::deregisterEHFrames(uint8_t *Addr,
});
}

RTDyldMemoryManager* createRTDyldMemoryManagerOSX()
{
return new RTDyldMemoryManagerOSX();
}

#endif

#if defined(_OS_LINUX_) && defined(LLVM37) && defined(JL_UNW_HAS_FORMAT_IP)
#elif defined(_OS_LINUX_) && defined(LLVM37) && defined(JL_UNW_HAS_FORMAT_IP)
#include <type_traits>
#include "llvm/ExecutionEngine/SectionMemoryManager.h"
class RTDyldMemoryManagerUnix : public SectionMemoryManager
{
RTDyldMemoryManagerUnix(const RTDyldMemoryManagerUnix&) = delete;
void operator=(const RTDyldMemoryManagerUnix&) = delete;

public:
RTDyldMemoryManagerUnix() {};
~RTDyldMemoryManagerUnix() override {};
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
};

struct unw_table_entry
{
Expand Down Expand Up @@ -1592,9 +1554,7 @@ static DW_EH_PE parseCIE(const uint8_t *Addr, const uint8_t *End)
return DW_EH_PE_absptr;
}

void RTDyldMemoryManagerUnix::registerEHFrames(uint8_t *Addr,
uint64_t LoadAddr,
size_t Size)
void register_eh_frames(uint8_t *Addr, size_t Size)
{
#ifndef _CPU_ARM_
// System unwinder
Expand Down Expand Up @@ -1725,9 +1685,7 @@ void RTDyldMemoryManagerUnix::registerEHFrames(uint8_t *Addr,
_U_dyn_register(di);
}

void RTDyldMemoryManagerUnix::deregisterEHFrames(uint8_t *Addr,
uint64_t LoadAddr,
size_t Size)
void deregister_eh_frames(uint8_t *Addr, size_t Size)
{
#ifndef _CPU_ARM_
__deregister_frame(Addr);
Expand All @@ -1737,9 +1695,14 @@ void RTDyldMemoryManagerUnix::deregisterEHFrames(uint8_t *Addr,
// data structures).
}

RTDyldMemoryManager* createRTDyldMemoryManagerUnix()
#else

void register_eh_frames(uint8_t *Addr, size_t Size)
{
}

void deregister_eh_frames(uint8_t *Addr, size_t Size)
{
return new RTDyldMemoryManagerUnix();
}

#endif
Expand Down
51 changes: 39 additions & 12 deletions src/jitlayers.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
// This file is a part of Julia. License is MIT: http://julialang.org/license

#include <llvm/ExecutionEngine/SectionMemoryManager.h>

// Except for parts of this file which were copied from LLVM, under the UIUC license (marked below).

// this defines the set of optimization passes defined for Julia at various optimization levels
Expand Down Expand Up @@ -129,6 +131,42 @@ static void addOptimizationPasses(T *PM)
//PM->add(createCFGSimplificationPass()); // Merge & remove BBs
}

#ifdef USE_MCJIT
#ifdef LLVM37
class RTDyldMemoryManagerJL : public SectionMemoryManager {
RTDyldMemoryManagerJL(const RTDyldMemoryManagerJL&) = delete;
void operator=(const RTDyldMemoryManagerJL&) = delete;

public:
RTDyldMemoryManagerJL() {};
~RTDyldMemoryManagerJL() override {};
void registerEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
void deregisterEHFrames(uint8_t *Addr, uint64_t LoadAddr, size_t Size) override;
};

void RTDyldMemoryManagerJL::registerEHFrames(uint8_t *Addr,
uint64_t LoadAddr,
size_t Size)
{
register_eh_frames(Addr, Size);
}

void RTDyldMemoryManagerJL::deregisterEHFrames(uint8_t *Addr,
uint64_t LoadAddr,
size_t Size)
{
deregister_eh_frames(Addr, Size);
}
#else
typedef SectionMemoryManager RTDyldMemoryManagerJL;
#endif

RTDyldMemoryManager* createRTDyldMemoryManager()
{
return new RTDyldMemoryManagerJL();
}
#endif

#ifdef USE_ORCJIT

// ------------------------ TEMPORARILY COPIED FROM LLVM -----------------
Expand Down Expand Up @@ -190,17 +228,6 @@ void NotifyDebugger(jit_code_entry *JITCodeEntry)
}
// ------------------------ END OF TEMPORARY COPY FROM LLVM -----------------

#if defined(_OS_DARWIN_) && defined(LLVM37) && defined(LLVM_SHLIB)
#define CUSTOM_MEMORY_MANAGER createRTDyldMemoryManagerOSX
extern RTDyldMemoryManager *createRTDyldMemoryManagerOSX();
#elif defined(_OS_LINUX_) && defined(LLVM37) && defined(JL_UNW_HAS_FORMAT_IP)
#define CUSTOM_MEMORY_MANAGER createRTDyldMemoryManagerUnix
extern RTDyldMemoryManager *createRTDyldMemoryManagerUnix();
#endif
#ifndef CUSTOM_MEMORY_MANAGER
#define CUSTOM_MEMORY_MANAGER() new SectionMemoryManager
#endif

#ifdef _OS_LINUX_
// Resolve non-lock free atomic functions in the libatomic library.
// This is the library that provides support for c11/c++11 atomic operations.
Expand Down Expand Up @@ -322,7 +349,7 @@ class JuliaOJIT {
: TM(TM),
DL(TM.createDataLayout()),
ObjStream(ObjBufferSV),
MemMgr(CUSTOM_MEMORY_MANAGER()),
MemMgr(createRTDyldMemoryManager()),
ObjectLayer(DebugObjectRegistrar(*this)),
CompileLayer(
ObjectLayer,
Expand Down
3 changes: 3 additions & 0 deletions src/julia_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,9 @@ jl_value_t *jl_lookup_match(jl_value_t *a, jl_value_t *b, jl_svec_t **penv, jl_s

unsigned jl_special_vector_alignment(size_t nfields, jl_value_t *field_type);

void register_eh_frames(uint8_t *Addr, size_t Size);
void deregister_eh_frames(uint8_t *Addr, size_t Size);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit 3c8eed9

Please sign in to comment.