[Bf-blender-cvs] [49a0f7f290c] nodes_playground: use own type system

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


Commit: 49a0f7f290cb108f09e6599840a71e20d0ed333e
Author: Jacques Lucke
Date:   Sat Jan 5 11:50:07 2019 +0100
Branches: nodes_playground
https://developer.blender.org/rB49a0f7f290cb108f09e6599840a71e20d0ed333e

use own type system

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

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 c88bd2ecc45..91d78d266c8 100644
--- a/source/blender/modifiers/intern/node_compiler.cpp
+++ b/source/blender/modifiers/intern/node_compiler.cpp
@@ -4,6 +4,15 @@
 
 namespace LLVMNodeCompiler {
 
+llvm::Type *Type::getLLVMType(llvm::LLVMContext &context)
+{
+	if (!this->typePerContext.contains(&context)) {
+		llvm::Type *type = this->createLLVMType(context);
+		this->typePerContext.add(&context, type);
+	}
+	return this->typePerContext.lookup(&context);
+}
+
 AnySocket LinkSet::getOriginSocket(AnySocket socket) const
 {
 	assert(socket.is_input());
@@ -32,7 +41,7 @@ const SocketInfo *AnySocket::info() const
 	}
 }
 
-llvm::Type *AnySocket::type() const
+Type *AnySocket::type() const
 {
 	return this->info()->type;
 }
@@ -59,11 +68,13 @@ DataFlowCallable *DataFlowGraph::generateCallable(
 	std::string debug_name,
 	SocketArraySet &inputs, SocketArraySet &outputs)
 {
+	llvm::LLVMContext *context = new llvm::LLVMContext();
+
+	std::string module_name = debug_name + " Module";
 	std::string function_name = debug_name + " Function";
 
 	llvm::Module *module = this->generateModule(
-		debug_name + " Module", function_name,
-		inputs, outputs);
+		*context, module_name, function_name, inputs, outputs);
 
 	llvm::InitializeNativeTarget();
 	llvm::InitializeNativeTargetAsmPrinter();
@@ -78,11 +89,11 @@ DataFlowCallable *DataFlowGraph::generateCallable(
 }
 
 llvm::Module *DataFlowGraph::generateModule(
+	llvm::LLVMContext &context,
 	std::string module_name, std::string function_name,
 	SocketArraySet &inputs, SocketArraySet &outputs)
 {
 	assert(outputs.size() > 0);
-	llvm::LLVMContext &context = outputs[0].type()->getContext();
 	llvm::Module *module = new llvm::Module(module_name, context);
 	this->generateFunction(module, function_name, inputs, outputs);
 	return module;
@@ -96,12 +107,12 @@ llvm::Function *DataFlowGraph::generateFunction(
 
 	std::vector<llvm::Type *> input_types;
 	for (AnySocket socket : inputs.elements()) {
-		input_types.push_back(socket.type());
+		input_types.push_back(socket.type()->getLLVMType(context));
 	}
 
 	std::vector<llvm::Type *> output_types;
 	for (AnySocket socket : outputs.elements()) {
-		output_types.push_back(socket.type());
+		output_types.push_back(socket.type()->getLLVMType(context));
 	}
 
 	llvm::StructType *return_type = llvm::StructType::create(output_types, name + " Output");
diff --git a/source/blender/modifiers/intern/node_compiler.hpp b/source/blender/modifiers/intern/node_compiler.hpp
index d93aa01eabe..3896201478f 100644
--- a/source/blender/modifiers/intern/node_compiler.hpp
+++ b/source/blender/modifiers/intern/node_compiler.hpp
@@ -17,16 +17,28 @@ struct AnySocket;
 struct SocketInfo;
 struct Node;
 struct Link;
+struct Type;
 struct LinkSet;
 struct DataFlowGraph;
 
+class Type {
+private:
+	HashMap<llvm::LLVMContext *, llvm::Type *> typePerContext;
+
+public:
+	llvm::Type *getLLVMType(llvm::LLVMContext &context);
+	virtual llvm::Type *createLLVMType(llvm::LLVMContext &context) = 0;
+	// virtual llvm::Value *buildCopyIR(llvm::Value *value);
+	// virtual void buildFreeIR(llvm::Value *value);
+};
+
 struct AnySocket {
 	inline bool is_output() const { return this->_is_output; }
 	inline bool is_input() const { return !this->_is_output; }
 	inline Node *node() const { return this->_node; }
 	inline uint index() const { return this->_index; }
 
-	llvm::Type *type() const;
+	Type *type() const;
 	std::string debug_name() const;
 
 	inline static AnySocket NewInput(Node *node, uint index)
@@ -64,9 +76,9 @@ using SocketValueMap = SocketMap<llvm::Value *>;
 
 struct SocketInfo {
 	std::string debug_name;
-	llvm::Type *type;
+	Type *type;
 
-	SocketInfo(std::string debug_name, llvm::Type *type)
+	SocketInfo(std::string debug_name, Type *type)
 		: debug_name(debug_name), type(type) {}
 };
 
@@ -156,6 +168,7 @@ public:
 		SocketArraySet &inputs, SocketArraySet &outputs);
 
 	llvm::Module *generateModule(
+		llvm::LLVMContext &context,
 		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 165fb4821ed..b4e03fdf337 100644
--- a/source/blender/modifiers/intern/node_compiler_testing.cpp
+++ b/source/blender/modifiers/intern/node_compiler_testing.cpp
@@ -9,6 +9,22 @@ extern "C" {
 
 namespace NC = LLVMNodeCompiler;
 
+class IntegerType : public NC::Type {
+private:
+	uint bits;
+
+public:
+	IntegerType(uint bits)
+		: bits(bits) {}
+
+	llvm::Type *createLLVMType(llvm::LLVMContext &context)
+	{
+		return llvm::Type::getIntNTy(context, this->bits);
+	}
+};
+
+auto *type_int32 = new IntegerType(32);
+
 static void generateCode_AddNode(
 	std::vector<llvm::Value *> &inputs, llvm::IRBuilder<> *builder,
 	std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder)
@@ -18,21 +34,21 @@ static void generateCode_AddNode(
 	*r_builder = builder;
 }
 
-static NC::Node *new_add_node(llvm::LLVMContext &context)
+static NC::Node *new_add_node()
 {
 	return NC::Node::FromIRBuilderFunction("Add",
-		{ NC::SocketInfo("A", llvm::Type::getInt32Ty(context)),
-		  NC::SocketInfo("B", llvm::Type::getInt32Ty(context)) },
-		{ NC::SocketInfo("Result", llvm::Type::getInt32Ty(context)) },
+		{ NC::SocketInfo("A", type_int32),
+		  NC::SocketInfo("B", type_int32) },
+		{ NC::SocketInfo("Result", type_int32) },
 		generateCode_AddNode
 	);
 }
 
-static NC::Node *new_int_node(llvm::LLVMContext &context, int value)
+static NC::Node *new_int_node(int value)
 {
 	return NC::Node::FromIRBuilderFunction("Int",
 		{},
-		{ NC::SocketInfo("Value", llvm::Type::getInt32Ty(context)) },
+		{ NC::SocketInfo("Value", type_int32) },
 		[value](
 			std::vector<llvm::Value *> &UNUSED(inputs), llvm::IRBuilder<> *builder,
 			std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder)
@@ -43,11 +59,11 @@ static NC::Node *new_int_node(llvm::LLVMContext &context, int value)
 	);
 }
 
-static NC::Node *new_int_ref_node(llvm::LLVMContext &context, int *value)
+static NC::Node *new_int_ref_node(int *value)
 {
 	return NC::Node::FromIRBuilderFunction("Int Ref",
 		{},
-		{ NC::SocketInfo("Value", llvm::Type::getInt32Ty(context)) },
+		{ NC::SocketInfo("Value", type_int32) },
 		[value](
 			std::vector<llvm::Value *> &UNUSED(inputs), llvm::IRBuilder<> *builder,
 			std::vector<llvm::Value *> &r_outputs, llvm::IRBuilder<> **r_builder)
@@ -66,18 +82,15 @@ extern "C" {
 
 void run_tests()
 {
-	llvm::LLVMContext *_context = new llvm::LLVMContext();
-	llvm::LLVMContext &context = *_context;
-
 	int test_value = 1000;
 
-	auto in1 = new_int_node(context, 1);
-	auto in2 = new_int_ref_node(context, &test_value);
-	auto in3 = new_int_node(context, 10);
+	auto in1 = new_int_node(1);
+	auto in2 = new_int_ref_node(&test_value);
+	auto in3 = new_int_node(10);
 
-	auto add1 = new_add_node(context);
-	auto add2 = new_add_node(context);
-	auto add3 = new_add_node(context);
+	auto add1 = new_add_node();
+	auto add2 = new_add_node();
+	auto add3 = new_add_node();
 
 	NC::DataFlowGraph graph;
 	graph.nodes.push_back(in1);



More information about the Bf-blender-cvs mailing list