#include <llvm/Support/CommandLine.h>
#include <llvm/Support/Host.h>
#include <llvm/Support/PrettyStackTrace.h>
-
+#include <llvm/ExecutionEngine/ObjectCache.h>
#include <llvm/Support/TargetSelect.h>
#if LLVM_VERSION_MAJOR < 11
}
};
+class LPObjectCache : public llvm::ObjectCache {
+private:
+ bool has_object;
+ struct lp_cached_code *cache_out;
+public:
+ LPObjectCache(struct lp_cached_code *cache) {
+ cache_out = cache;
+ has_object = false;
+ }
+
+ ~LPObjectCache() {
+ }
+ void notifyObjectCompiled(const llvm::Module *M, llvm::MemoryBufferRef Obj) {
+ const std::string ModuleID = M->getModuleIdentifier();
+ if (has_object)
+ fprintf(stderr, "CACHE ALREADY HAS MODULE OBJECT\n");
+ has_object = true;
+ cache_out->data_size = Obj.getBufferSize();
+ cache_out->data = malloc(cache_out->data_size);
+ memcpy(cache_out->data, Obj.getBufferStart(), cache_out->data_size);
+ }
+
+ virtual std::unique_ptr<llvm::MemoryBuffer> getObject(const llvm::Module *M) {
+ if (cache_out->data_size) {
+ return llvm::MemoryBuffer::getMemBuffer(llvm::StringRef((const char *)cache_out->data, cache_out->data_size));
+ }
+ return NULL;
+ }
+
+};
/**
* Same as LLVMCreateJITCompilerForModule, but:
ExecutionEngine *JIT;
JIT = builder.create();
+
+ if (cache_out) {
+ LPObjectCache *objcache = new LPObjectCache(cache_out);
+ JIT->setObjectCache(objcache);
+ cache_out->jit_obj_cache = (void *)objcache;
+ }
+
#if LLVM_USE_INTEL_JITEVENTS
JITEventListener *JEL = JITEventListener::createIntelJITEventListener();
JIT->RegisterJITEventListener(JEL);
delete reinterpret_cast<BaseMemoryManager*>(memorymgr);
}
+extern "C" void
+lp_free_objcache(void *objcache_ptr)
+{
+ LPObjectCache *objcache = (LPObjectCache *)objcache_ptr;
+ delete objcache;
+}
+
extern "C" LLVMValueRef
lp_get_called_value(LLVMValueRef call)
{