[Bf-blender-cvs] [cfdb20c9dcc] nodes_playground: generate actual machine code

Jacques Lucke noreply at git.blender.org
Sun Jan 6 17:25:30 CET 2019


Commit: cfdb20c9dcce963b261056c26505a3f70d31e5cf
Author: Jacques Lucke
Date:   Fri Jan 4 11:15:43 2019 +0100
Branches: nodes_playground
https://developer.blender.org/rBcfdb20c9dcce963b261056c26505a3f70d31e5cf

generate actual machine code

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

M	source/blender/modifiers/intern/node_compiler.cpp
M	source/blender/modifiers/intern/node_compiler.hpp
M	source/blender/modifiers/intern/node_compiler_testing.cpp

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

diff --git a/source/blender/modifiers/intern/node_compiler.cpp b/source/blender/modifiers/intern/node_compiler.cpp
index a5eb57f664e..c88bd2ecc45 100644
--- a/source/blender/modifiers/intern/node_compiler.cpp
+++ b/source/blender/modifiers/intern/node_compiler.cpp
@@ -49,6 +49,34 @@ std::string Node::debug_id() const
 	return ss.str();
 }
 
+void DataFlowCallable::printCode()
+{
+	this->module->print(llvm::outs(), nullptr);
+}
+
+
+DataFlowCallable *DataFlowGraph::generateCallable(
+	std::string debug_name,
+	SocketArraySet &inputs, SocketArraySet &outputs)
+{
+	std::string function_name = debug_name + " Function";
+
+	llvm::Module *module = this->generateModule(
+		debug_name + " Module", function_name,
+		inputs, outputs);
+
+	llvm::InitializeNativeTarget();
+	llvm::InitializeNativeTargetAsmPrinter();
+	llvm::InitializeNativeTargetAsmParser();
+
+	llvm::ExecutionEngine *ee = llvm::EngineBuilder(std::unique_ptr<llvm::Module>(module)).create();
+	ee->finalizeObject();
+	ee->generateCodeForModule(module);
+
+	DataFlowCallable *callable = new DataFlowCallable(module, ee, function_name);
+	return callable;
+}
+
 llvm::Module *DataFlowGraph::generateModule(
 	std::string module_name, std::string function_name,
 	SocketArraySet &inputs, SocketArraySet &outputs)
diff --git a/source/blender/modifiers/intern/node_compiler.hpp b/source/blender/modifiers/intern/node_compiler.hpp
index dc2766dcc82..d93aa01eabe 100644
--- a/source/blender/modifiers/intern/node_compiler.hpp
+++ b/source/blender/modifiers/intern/node_compiler.hpp
@@ -129,17 +129,32 @@ struct LinkSet {
 	AnySocket getOriginSocket(AnySocket socket) const;
 };
 
-struct AnySocketHash {
-	size_t operator()(const AnySocket &socket) const
+class DataFlowCallable {
+	void *function_pointer;
+	llvm::Module *module;
+	llvm::ExecutionEngine *ee;
+public:
+	DataFlowCallable(llvm::Module *module, llvm::ExecutionEngine *ee, std::string function_name)
+		: module(module), ee(ee)
 	{
-		return (size_t)socket.node() ^ (size_t)socket.is_input() ^ (size_t)socket.index();
+		this->function_pointer = (void *)this->ee->getFunctionAddress(function_name);
 	}
+
+	inline void *getFunctionPointer()
+	{ return this->function_pointer; }
+
+	void printCode();
 };
 
-struct DataFlowGraph {
+class DataFlowGraph {
+public:
 	std::vector<Node *> nodes;
 	LinkSet links;
 
+	DataFlowCallable *generateCallable(
+		std::string debug_name,
+		SocketArraySet &inputs, SocketArraySet &outputs);
+
 	llvm::Module *generateModule(
 		std::string module_name, std::string function_name,
 		SocketArraySet &inputs, SocketArraySet &outputs);
diff --git a/source/blender/modifiers/intern/node_compiler_testing.cpp b/source/blender/modifiers/intern/node_compiler_testing.cpp
index 6960d9c834c..165fb4821ed 100644
--- a/source/blender/modifiers/intern/node_compiler_testing.cpp
+++ b/source/blender/modifiers/intern/node_compiler_testing.cpp
@@ -95,11 +95,13 @@ void run_tests()
 	graph.links.links.push_back(NC::Link(add2->Output(0), add3->Input(1)));
 
 
-	NC::SocketArraySet inputs = { in1->Output(0), in2->Output(0) };
-	NC::SocketArraySet outputs = { add3->Output(0), add1->Input(0) };
-	llvm::Module *module = graph.generateModule("MyModule", "MyFunction", inputs, outputs);
+	NC::SocketArraySet inputs = { in1->Output(0), in2->Output(0), in3->Output(0) };
+	NC::SocketArraySet outputs = { add3->Output(0) };
+	NC::DataFlowCallable *callable = graph.generateCallable("Hello", inputs, outputs);
 
-	module->print(llvm::outs(), nullptr);
+	callable->printCode();
+	int result = ((int (*)(int, int, int))callable->getFunctionPointer())(10, 25, 100);
+	std::cout << result << std::endl;
 
 	// NC::SocketSet inputs = { add1->Input(0), add1->Input(1), add2->Input(1) };
 	// NC::SocketSet outputs = { add3->Output(0) };



More information about the Bf-blender-cvs mailing list