[Bf-blender-cvs] [bdd9bff] object_nodes: Replace function signature for dual values with a flattened sequence of basic types.

Lukas Tönne noreply at git.blender.org
Sun May 29 11:30:16 CEST 2016


Commit: bdd9bff0b3ab638d5f475fa1754495292ed790d6
Author: Lukas Tönne
Date:   Sun May 29 11:29:02 2016 +0200
Branches: object_nodes
https://developer.blender.org/rBbdd9bff0b3ab638d5f475fa1754495292ed790d6

Replace function signature for dual values with a flattened sequence of basic types.

This avoids the allocation of aggregate struct space for duals and allows better
optimizations.

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

M	source/blender/blenvm/llvm/llvm_compiler.cc
M	source/blender/blenvm/llvm/llvm_compiler.h
M	source/blender/blenvm/llvm/llvm_compiler_dual.cc
M	source/blender/blenvm/llvm/llvm_compiler_simple.cc

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

diff --git a/source/blender/blenvm/llvm/llvm_compiler.cc b/source/blender/blenvm/llvm/llvm_compiler.cc
index 98712bd..1537a3d 100644
--- a/source/blender/blenvm/llvm/llvm_compiler.cc
+++ b/source/blender/blenvm/llvm/llvm_compiler.cc
@@ -124,17 +124,18 @@ llvm::BasicBlock *LLVMCompilerBase::codegen_function_body_expression(const NodeG
 	int num_inputs = graph.inputs.size();
 	int num_outputs = graph.outputs.size();
 	
-	Argument *retarg = func->getArgumentList().begin();
 	{
-		Function::ArgumentListType::iterator it = retarg;
+		Function::ArgumentListType::iterator it = func->arg_begin();
 		for (int i = 0; i < num_outputs; ++i) {
-			++it; /* skip output arguments */
+			/* skip output arguments */
+			++it;
 		}
 		for (int i = 0; i < num_inputs; ++i) {
 			const NodeGraph::Input &input = graph.inputs[i];
-			Argument *arg = &(*it++);
+			Argument *arg = it++;
 			
-			map_argument(block, input.key, arg);
+			if (input.key)
+				map_argument(block, input.key, arg);
 		}
 	}
 	
@@ -149,10 +150,10 @@ llvm::BasicBlock *LLVMCompilerBase::codegen_function_body_expression(const NodeG
 	}
 	
 	{
-		Function::ArgumentListType::iterator it = func->getArgumentList().begin();
+		Function::ArgumentListType::iterator it = func->arg_begin();
 		for (int i = 0; i < num_outputs; ++i) {
 			const NodeGraph::Output &output = graph.outputs[i];
-			Argument *arg = &(*it++);
+			Argument *arg = it++;
 			
 			store_return_value(block, output.key, arg);
 		}
@@ -172,16 +173,12 @@ llvm::Function *LLVMCompilerBase::codegen_node_function(const string &name, cons
 	std::vector<llvm::Type*> input_types, output_types;
 	for (int i = 0; i < graph.inputs.size(); ++i) {
 		const NodeGraph::Input &input = graph.inputs[i];
-		const TypeSpec *typespec = input.typedesc.get_typespec();
-		Type *type = get_value_type(typespec, false);
-		if (use_argument_pointer(typespec, false))
-			type = type->getPointerTo();
+		Type *type = get_argument_type(input.typedesc.get_typespec());
 		input_types.push_back(type);
 	}
 	for (int i = 0; i < graph.outputs.size(); ++i) {
 		const NodeGraph::Output &output = graph.outputs[i];
-		const TypeSpec *typespec = output.typedesc.get_typespec();
-		Type *type = get_value_type(typespec, false);
+		Type *type = get_return_type(output.typedesc.get_typespec());
 		output_types.push_back(type);
 	}
 	FunctionType *functype = get_node_function_type(input_types, output_types);
@@ -340,37 +337,19 @@ llvm::Function *LLVMCompilerBase::declare_node_function(llvm::Module *mod, const
 	std::vector<Type *> input_types, output_types;
 	for (int i = 0; i < nodetype->num_inputs(); ++i) {
 		const NodeInput *input = nodetype->find_input(i);
-		const TypeSpec *typespec = input->typedesc.get_typespec();
 		bool is_constant = (input->value_type == INPUT_CONSTANT);
 		
-		Type *type = get_value_type(typespec, is_constant);
-		if (type == NULL)
-			break;
-		if (use_argument_pointer(typespec, is_constant))
-			type = type->getPointerTo();
-		input_types.push_back(type);
+		append_input_types(input_types, input->typedesc.get_typespec(), is_constant);
 	}
 	for (int i = 0; i < nodetype->num_outputs(); ++i) {
 		const NodeOutput *output = nodetype->find_output(i);
-		const TypeSpec *typespec = output->typedesc.get_typespec();
-		Type *type = get_value_type(typespec, false);
-		if (type == NULL)
-			break;
-		output_types.push_back(type);
-	}
-	if (input_types.size() != nodetype->num_inputs() ||
-	    output_types.size() != nodetype->num_outputs()) {
-		/* some arguments could not be handled */
-		return NULL;
+		
+		append_output_types(output_types, output->typedesc.get_typespec());
 	}
 	
 	FunctionType *functype = get_node_function_type(input_types, output_types);
 	
 	Function *func = Function::Create(functype, Function::ExternalLinkage, nodetype->name(), mod);
-	
-//	printf("Declared function for node type '%s':\n", nodetype->name().c_str());
-//	func->dump();
-	
 	return func;
 }
 
diff --git a/source/blender/blenvm/llvm/llvm_compiler.h b/source/blender/blenvm/llvm/llvm_compiler.h
index 6f58369..993c94c 100644
--- a/source/blender/blenvm/llvm/llvm_compiler.h
+++ b/source/blender/blenvm/llvm/llvm_compiler.h
@@ -106,8 +106,11 @@ protected:
 	virtual void map_argument(llvm::BasicBlock *block, const OutputKey &output, llvm::Argument *arg) = 0;
 	virtual void store_return_value(llvm::BasicBlock *block, const OutputKey &output, llvm::Value *arg) = 0;
 	
-	virtual llvm::Type *get_value_type(const TypeSpec *spec, bool is_constant) = 0;
-	virtual bool use_argument_pointer(const TypeSpec *typespec, bool is_constant) const = 0;
+	virtual llvm::Type *get_argument_type(const TypeSpec *spec) const = 0;
+	virtual llvm::Type *get_return_type(const TypeSpec *spec) const = 0;
+	virtual void append_input_types(std::vector<llvm::Type*> &params,
+	                                const TypeSpec *spec, bool is_constant) const = 0;
+	virtual void append_output_types(std::vector<llvm::Type*> &params, const TypeSpec *spec) const = 0;
 	
 	llvm::Function *declare_node_function(llvm::Module *mod, const NodeType *nodetype);
 	virtual void define_nodes_module() = 0;
@@ -134,11 +137,15 @@ struct LLVMSimpleCompilerImpl : public LLVMCompilerBase {
 	void map_argument(llvm::BasicBlock *block, const OutputKey &output, llvm::Argument *arg);
 	void store_return_value(llvm::BasicBlock *block, const OutputKey &output, llvm::Value *arg);
 	
-	llvm::Type *get_value_type(const TypeSpec *spec, bool is_constant);
+	llvm::Type *get_argument_type(const TypeSpec *spec) const;
+	llvm::Type *get_return_type(const TypeSpec *spec) const;
+	void append_input_types(std::vector<llvm::Type*> &params,
+	                        const TypeSpec *spec, bool is_constant) const;
+	void append_output_types(std::vector<llvm::Type*> &params, const TypeSpec *spec) const;
 	
 	llvm::Module *get_nodes_module() const { return m_nodes_module; }
 	
-	bool use_argument_pointer(const TypeSpec *typespec, bool is_constant) const;
+	bool use_argument_pointer(const TypeSpec *typespec) const;
 	
 	bool set_node_function_impl(OpCode op, const NodeType *nodetype, llvm::Function *func);
 	void define_nodes_module();
@@ -149,7 +156,8 @@ private:
 };
 
 struct LLVMTextureCompiler : public LLVMCompilerBase {
-	typedef std::map<ConstOutputKey, llvm::Value*> OutputValueMap;
+	typedef Dual2<llvm::Value*> DualValue;
+	typedef std::map<ConstOutputKey, DualValue> OutputValueMap;
 	
 	void node_graph_begin();
 	void node_graph_end();
@@ -165,19 +173,24 @@ struct LLVMTextureCompiler : public LLVMCompilerBase {
 	void map_argument(llvm::BasicBlock *block, const OutputKey &output, llvm::Argument *arg);
 	void store_return_value(llvm::BasicBlock *block, const OutputKey &output, llvm::Value *arg);
 	
-	llvm::Type *get_value_type(const TypeSpec *spec, bool is_constant);
-	llvm::Function *declare_elementary_node_function(
-	        llvm::Module *mod, const NodeType *nodetype, const string &name,
-	        bool with_derivatives);
+	llvm::Type *get_argument_type(const TypeSpec *spec) const;
+	llvm::Type *get_return_type(const TypeSpec *spec) const;
+	void append_input_types(std::vector<llvm::Type*> &params,
+	                        const TypeSpec *spec, bool is_constant) const;
+	void append_output_types(std::vector<llvm::Type*> &params, const TypeSpec *spec) const;
 	
 	llvm::Module *get_nodes_module() const { return m_nodes_module; }
 	
-	bool use_argument_pointer(const TypeSpec *typespec, bool is_constant) const;
+	bool use_argument_pointer(const TypeSpec *typespec, bool use_dual) const;
 	bool use_elementary_argument_pointer(const TypeSpec *typespec) const;
 	
 	void define_node_function(llvm::Module *mod, OpCode op, const string &nodetype_name);
 	void define_nodes_module();
 	
+	llvm::Function *declare_elementary_node_function(
+	        llvm::Module *mod, const NodeType *nodetype, const string &name,
+	        bool with_derivatives);
+	
 	bool set_node_function_impl(OpCode op, const NodeType *nodetype,
 	                            llvm::Function *value_func, llvm::Function * dual_func);
 	void define_elementary_functions(llvm::Module *mod, OpCode op, const NodeType *nodetype);
diff --git a/source/blender/blenvm/llvm/llvm_compiler_dual.cc b/source/blender/blenvm/llvm/llvm_compiler_dual.cc
index c3a94cc..1919017 100644
--- a/source/blender/blenvm/llvm/llvm_compiler_dual.cc
+++ b/source/blender/blenvm/llvm/llvm_compiler_dual.cc
@@ -73,10 +73,12 @@ void LLVMTextureCompiler::alloc_node_value(llvm::BasicBlock *block, const ConstO
 	builder.SetInsertPoint(block);
 	
 	const TypeSpec *typespec = output.socket->typedesc.get_typespec();
-	Type *type = get_value_type(typespec, false);
+	Type *type = bvm_get_llvm_type(context(), typespec, false);
 	BLI_assert(type != NULL);
 	
-	Value *value = builder.CreateAlloca(type);
+	DualValue value(builder.CreateAlloca(type),
+	                builder.CreateAlloca(type),
+	                builder.CreateAlloca(type));
 	
 	/* use as node output values */
 	bool ok = m_output_values.insert(OutputValueMap::value_type(output, value)).second;
@@ -88,7 +90,7 @@ void LLVMTextureCompiler::copy_node_value(const ConstOutputKey &from, const Cons
 {
 	using namespace llvm;
 	
-	Value *value = m_output_values.at(from);
+	DualValue value = m_output_values.at(from);
 	bool ok = m_output_values.insert(OutputValueMap::value_type(to, value)).second;
 	BLI_assert(ok && "Value for node output already defined!");
 	UNUSED_VARS(ok);
@@ -96,7 +98,10 @@ void LLVMTextureCompiler::copy_node_value(const ConstOutputKey &from, const Cons
 
 void LLVMTextureCompiler::append_output_arguments(std::vector<llvm::Value*> &args, const ConstOutputKey &output)
 {
-	args.push_back(m_output_values.at(output));
+	DualValue val = m_output_values.at(output);
+	args.push_back(val.value());
+	args.push_back(val.dx());
+	args.push_back(val.dy());
 }
 
 void LLVMTextureCompiler::append_input_value(llvm::BasicBlock *block, std::vector<llvm::Value*> &args,
@@ -107,16 +112,17 @@ void LLVMTextureCompiler::append_input_value(llvm::BasicBlock *block, std::vecto
 	IRBuilder<> builder(context());
 	builder.SetInsertPoint(block);
 	
-	Value *pvalue = m_output_values.at(link);
-	Value *value;
+	DualValue ptr = m_output_values.at(link);
 	if (use_argument_pointer(typespec, false)) {
-	

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list