[Bf-blender-cvs] [1456a030f42] functions: initial IR for vector addition

Jacques Lucke noreply at git.blender.org
Fri Apr 5 19:31:16 CEST 2019


Commit: 1456a030f42161a374875227ab83f9b177625e86
Author: Jacques Lucke
Date:   Fri Apr 5 19:31:11 2019 +0200
Branches: functions
https://developer.blender.org/rB1456a030f42161a374875227ab83f9b177625e86

initial IR for vector addition

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

M	source/blender/functions/backends/llvm/builder.hpp
M	source/blender/functions/functions/vectors.cpp

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

diff --git a/source/blender/functions/backends/llvm/builder.hpp b/source/blender/functions/backends/llvm/builder.hpp
index ca3cfcb698b..bd259e4dcc0 100644
--- a/source/blender/functions/backends/llvm/builder.hpp
+++ b/source/blender/functions/backends/llvm/builder.hpp
@@ -217,6 +217,41 @@ namespace FN {
 				this->getModule(), llvm::Intrinsic::sin, value->getType());
 			return m_builder.CreateCall(function, value);
 		}
+
+		llvm::Value *CreateStructToVector(llvm::Value *value)
+		{
+			llvm::Type *struct_type = value->getType();
+			BLI_assert(struct_type->isStructTy());
+			uint length = struct_type->getStructNumElements();
+
+			llvm::Type *base_type = struct_type->getStructElementType(0);
+			llvm::Type *vector_type = llvm::VectorType::get(
+				base_type, length);
+
+			llvm::Value *output = llvm::UndefValue::get(vector_type);
+			for (uint i = 0; i < length; i++) {
+				output = m_builder.CreateInsertElement(output, m_builder.CreateExtractValue(value, i), i);
+			}
+			return output;
+		}
+
+		llvm::Value *CreateVectorToStruct(llvm::Value *value)
+		{
+			llvm::Type *vector_type = value->getType();
+			BLI_assert(vector_type->isVectorTy());
+			uint length = vector_type->getVectorNumElements();
+
+			llvm::Type *base_type = vector_type->getVectorElementType();
+			LLVMTypes types(length);
+			types.fill(base_type);
+			llvm::Type *struct_type = this->getStructType(types);
+
+			llvm::Value *output = llvm::UndefValue::get(struct_type);
+			for (uint i = 0; i < length; i++) {
+				output = m_builder.CreateInsertValue(output, m_builder.CreateExtractElement(value, i), i);
+			}
+			return output;
+		}
 	};
 
 } /* namespace FN */
\ No newline at end of file
diff --git a/source/blender/functions/functions/vectors.cpp b/source/blender/functions/functions/vectors.cpp
index d3a224dadfd..fd32604aa2d 100644
--- a/source/blender/functions/functions/vectors.cpp
+++ b/source/blender/functions/functions/vectors.cpp
@@ -112,10 +112,25 @@ namespace FN { namespace Functions {
 		}
 	};
 
+	class AddVectorsGen : public LLVMBuildIRBody {
+		void build_ir(
+			CodeBuilder &builder,
+			CodeInterface &interface,
+			const BuildIRSettings &UNUSED(settings)) const override
+		{
+			llvm::Value *a = builder.CreateStructToVector(interface.get_input(0));
+			llvm::Value *b = builder.CreateStructToVector(interface.get_input(1));
+			llvm::Value *result = builder.CreateFAdd(a, b);
+			result = builder.CreateVectorToStruct(result);
+			interface.set_output(0, result);
+		}
+	};
+
 	LAZY_INIT_REF__NO_ARG(SharedFunction, add_vectors)
 	{
 		auto fn = get_math_function__two_inputs("Add Vectors");
 		fn->add_body(new AddVectors());
+		fn->add_body(new AddVectorsGen());
 		return fn;
 	}



More information about the Bf-blender-cvs mailing list