Commit: 8a8b7d40ac9ce737820626053408b2f1ba8138ad
Author: Jacques Lucke
Date:   Sun Mar 3 20:41:54 2019 +0100
Branches: functions
https://developer.blender.org/rB8a8b7d40ac9ce737820626053408b2f1ba8138ad

deduplicate compilation code

===================================================================

M       source/blender/functions/CMakeLists.txt
A       source/blender/functions/backends/llvm/compile.cpp
A       source/blender/functions/backends/llvm/compile.hpp
M       source/blender/functions/backends/llvm/compiled_body.cpp
M       source/blender/functions/backends/llvm/compiled_body.hpp
M       source/blender/functions/backends/llvm/to_tuple_call.cpp
M       source/blender/functions/c_wrapper.cpp

===================================================================

diff --git a/source/blender/functions/CMakeLists.txt 
b/source/blender/functions/CMakeLists.txt
index cdc051a7bbc..d9adf6899ce 100644
--- a/source/blender/functions/CMakeLists.txt
+++ b/source/blender/functions/CMakeLists.txt
@@ -62,6 +62,8 @@ set(SRC
        backends/llvm/from_tuple_call.cpp
        backends/llvm/compiled_body.hpp
        backends/llvm/compiled_body.cpp
+       backends/llvm/compile.hpp
+       backends/llvm/compile.cpp
        backends/llvm/ir_utils.hpp
        backends/llvm/ir_utils.cpp
 
diff --git a/source/blender/functions/backends/llvm/compile.cpp 
b/source/blender/functions/backends/llvm/compile.cpp
new file mode 100644
index 00000000000..eb334128666
--- /dev/null
+++ b/source/blender/functions/backends/llvm/compile.cpp
@@ -0,0 +1,37 @@
+#include "compile.hpp"
+#include "BLI_utildefines.h"
+
+#include <llvm/IR/Verifier.h>
+#include <llvm/ExecutionEngine/ExecutionEngine.h>
+
+namespace FN {
+
+       CompiledLLVM::~CompiledLLVM()
+       {
+               delete m_engine;
+       }
+
+       std::unique_ptr<CompiledLLVM>
+       CompiledLLVM::FromIR(
+               llvm::Module *module,
+               llvm::Function *main_function)
+       {
+               BLI_assert(!llvm::verifyModule(*module, &llvm::outs()));
+
+               llvm::ExecutionEngine *ee = llvm::EngineBuilder(
+                       std::unique_ptr<llvm::Module>(module)).create();
+               ee->finalizeObject();
+               ee->generateCodeForModule(module);
+
+               module->print(llvm::outs(), nullptr);
+
+               uint64_t function_ptr = ee->getFunctionAddress(
+                       main_function->getName().str());
+
+               auto result = std::unique_ptr<CompiledLLVM>(new CompiledLLVM());
+               result->m_engine = ee;
+               result->m_func_ptr = (void *)function_ptr;
+               return result;
+       }
+
+} /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/backends/llvm/compile.hpp 
b/source/blender/functions/backends/llvm/compile.hpp
new file mode 100644
index 00000000000..55eef05c801
--- /dev/null
+++ b/source/blender/functions/backends/llvm/compile.hpp
@@ -0,0 +1,32 @@
+#pragma once
+
+#include <memory>
+
+namespace llvm {
+       class ExecutionEngine;
+       class Module;
+       class Function;
+}
+
+namespace FN {
+
+       class CompiledLLVM {
+       private:
+               llvm::ExecutionEngine *m_engine;
+               void *m_func_ptr;
+
+               CompiledLLVM() = default;
+       public:
+               ~CompiledLLVM();
+
+               static std::unique_ptr<CompiledLLVM> FromIR(
+                       llvm::Module *module,
+                       llvm::Function *main_function);
+
+               void *function_ptr() const
+               {
+                       return m_func_ptr;
+               }
+       };
+
+} /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/backends/llvm/compiled_body.cpp 
b/source/blender/functions/backends/llvm/compiled_body.cpp
index e3d23e8d676..e3a01c213c8 100644
--- a/source/blender/functions/backends/llvm/compiled_body.cpp
+++ b/source/blender/functions/backends/llvm/compiled_body.cpp
@@ -17,33 +17,6 @@ namespace FN {
                delete v;
        }
 
-       CompiledLLVMBody::~CompiledLLVMBody()
-       {
-               delete m_engine;
-       }
-
-       CompiledLLVMBody *CompiledLLVMBody::FromIR(
-               llvm::Module *module,
-               llvm::Function *main_func)
-       {
-               BLI_assert(!llvm::verifyModule(*module, &llvm::outs()));
-
-               llvm::ExecutionEngine *ee = llvm::EngineBuilder(
-                       std::unique_ptr<llvm::Module>(module)).create();
-               ee->finalizeObject();
-               ee->generateCodeForModule(module);
-
-               module->print(llvm::outs(), nullptr);
-
-               uint64_t function_ptr = ee->getFunctionAddress(
-                       main_func->getName().str());
-
-               auto body = new CompiledLLVMBody();
-               body->m_engine = ee;
-               body->m_func_ptr = (void *)function_ptr;
-               return body;
-       }
-
        static CompiledLLVMBody *compile_llvm_body(
                SharedFunction &fn,
                llvm::LLVMContext &context)
@@ -87,8 +60,8 @@ namespace FN {
                }
                builder.CreateRet(return_value);
 
-               return CompiledLLVMBody::FromIR(
-                       module, function);
+               auto compiled = CompiledLLVM::FromIR(module, function);
+               return new CompiledLLVMBody(std::move(compiled));
        }
 
        bool try_ensure_CompiledLLVMBody(
diff --git a/source/blender/functions/backends/llvm/compiled_body.hpp 
b/source/blender/functions/backends/llvm/compiled_body.hpp
index 479da64489f..c35e8556cf9 100644
--- a/source/blender/functions/backends/llvm/compiled_body.hpp
+++ b/source/blender/functions/backends/llvm/compiled_body.hpp
@@ -1,6 +1,7 @@
 #pragma once
 
 #include "FN_core.hpp"
+#include "compile.hpp"
 
 namespace llvm {
        class ExecutionEngine;
@@ -13,8 +14,7 @@ namespace FN {
 
        class CompiledLLVMBody : public FunctionBody {
        private:
-               void *m_func_ptr;
-               llvm::ExecutionEngine *m_engine;
+               std::unique_ptr<CompiledLLVM> m_compiled;
 
                CompiledLLVMBody() = default;
 
@@ -22,15 +22,12 @@ namespace FN {
                static const char *identifier_in_composition();
                static void free_self(void *value);
 
-               static CompiledLLVMBody *FromIR(
-                       llvm::Module *module,
-                       llvm::Function *main_func);
-
-               ~CompiledLLVMBody();
+               CompiledLLVMBody(std::unique_ptr<CompiledLLVM> compiled)
+                       : m_compiled(std::move(compiled)) {}
 
                void *function_ptr()
                {
-                       return m_func_ptr;
+                       return m_compiled->function_ptr();
                }
        };
 
diff --git a/source/blender/functions/backends/llvm/to_tuple_call.cpp 
b/source/blender/functions/backends/llvm/to_tuple_call.cpp
index db01077ee8f..5138f3cbd30 100644
--- a/source/blender/functions/backends/llvm/to_tuple_call.cpp
+++ b/source/blender/functions/backends/llvm/to_tuple_call.cpp
@@ -89,11 +89,15 @@ namespace FN {
 
        class LLVMTupleCall : public TupleCallBody {
        private:
+               std::unique_ptr<CompiledLLVM> m_compiled;
                LLVMCallFN m_call;
 
        public:
-               LLVMTupleCall(LLVMCallFN call)
-                       : m_call(call) {}
+               LLVMTupleCall(std::unique_ptr<CompiledLLVM> compiled)
+                       : m_compiled(std::move(compiled))
+               {
+                       m_call = (LLVMCallFN)m_compiled->function_ptr();
+               }
 
                void call(const Tuple &fn_in, Tuple &fn_out) const override
                {
@@ -117,20 +121,8 @@ namespace FN {
                llvm::Module *module = new llvm::Module(fn->name(), context);
                llvm::Function *function = insert_tuple_call_function(fn, 
build_ir, module);
 
-               module->print(llvm::outs(), nullptr);
-
-               llvm::verifyFunction(*function, &llvm::outs());
-               llvm::verifyModule(*module, &llvm::outs());
-
-               llvm::ExecutionEngine *ee = llvm::EngineBuilder(
-                       std::unique_ptr<llvm::Module>(module)).create();
-               ee->finalizeObject();
-               ee->generateCodeForModule(module);
-
-               uint64_t fn_ptr = ee->getFunctionAddress(
-                       function->getName().str());
-
-               return new LLVMTupleCall((LLVMCallFN)fn_ptr);
+               auto compiled = CompiledLLVM::FromIR(module, function);
+               return new LLVMTupleCall(std::move(compiled));
        }
 
        static TupleCallBody *build_from_compiled(
diff --git a/source/blender/functions/c_wrapper.cpp 
b/source/blender/functions/c_wrapper.cpp
index d3dcd2c3b9a..cf2fe988199 100644
--- a/source/blender/functions/c_wrapper.cpp
+++ b/source/blender/functions/c_wrapper.cpp
@@ -26,7 +26,7 @@ static void playground()
        SharedFunction fn = Functions::add_floats();
 
        llvm::LLVMContext *context = new llvm::LLVMContext();
-       try_ensure_CompiledLLVMBody(fn, *context);
+       //try_ensure_CompiledLLVMBody(fn, *context);
        try_ensure_TupleCallBody(fn, *context);
 
        Tuple fn_in(fn->signature().input_types());

_______________________________________________
Bf-blender-cvs mailing list
[email protected]
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to