[Bf-blender-cvs] [a9f757eda55] nodes_playground: generate function from graph
Jacques Lucke
noreply at git.blender.org
Sun Jan 6 17:25:20 CET 2019
Commit: a9f757eda55031dbc4cca1ca6813778dfbaab9b6
Author: Jacques Lucke
Date: Thu Jan 3 18:31:49 2019 +0100
Branches: nodes_playground
https://developer.blender.org/rBa9f757eda55031dbc4cca1ca6813778dfbaab9b6
generate function from graph
===================================================================
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 d5e3b5aa31b..b4eb317513b 100644
--- a/source/blender/modifiers/intern/node_compiler.cpp
+++ b/source/blender/modifiers/intern/node_compiler.cpp
@@ -2,7 +2,7 @@
#include <sstream>
-namespace NodeCompiler {
+namespace LLVMNodeCompiler {
AnySocket LinkSet::getOriginSocket(AnySocket socket) const
{
@@ -32,12 +32,12 @@ const SocketInfo *AnySocket::info() const
}
}
-const llvm::Type *AnySocket::type() const
+llvm::Type *AnySocket::type() const
{
return this->info()->type;
}
-const std::string &AnySocket::debug_name() const
+std::string AnySocket::debug_name() const
{
return this->info()->debug_name;
}
@@ -49,6 +49,56 @@ std::string SimpleNode::debug_id() const
return ss.str();
}
+llvm::Function *Graph::generateFunction(
+ llvm::Module *module, std::string name,
+ std::vector<AnySocket> &inputs, std::vector<AnySocket> &outputs)
+{
+ llvm::LLVMContext &context = module->getContext();
+
+ std::vector<llvm::Type *> input_types;
+ for (AnySocket socket : inputs) {
+ input_types.push_back(socket.type());
+ }
+
+ std::vector<llvm::Type *> output_types;
+ for (AnySocket socket : outputs) {
+ output_types.push_back(socket.type());
+ }
+
+ llvm::StructType *return_type = llvm::StructType::create(output_types, name + " Output");
+
+ llvm::FunctionType *function_type = llvm::FunctionType::get(
+ return_type, input_types, false);
+
+ llvm::Function *function = llvm::Function::Create(
+ function_type, llvm::GlobalValue::LinkageTypes::ExternalLinkage,
+ name, module);
+
+ llvm::BasicBlock *bb = llvm::BasicBlock::Create(context, "entry", function);
+ llvm::IRBuilder<> builder(context);
+ builder.SetInsertPoint(bb);
+
+ std::vector<llvm::Value *> input_values;
+ for (uint i = 0; i < inputs.size(); i++) {
+ input_values.push_back(function->arg_begin() + i);
+ }
+
+ std::vector<llvm::Value *> output_values;
+ llvm::IRBuilder<> *next_builder;
+ this->generateCode(&builder, inputs, outputs, input_values, &next_builder, output_values);
+
+ llvm::Value *output = llvm::UndefValue::get(return_type);
+ for (uint i = 0; i < outputs.size(); i++) {
+ output = next_builder->CreateInsertValue(output, output_values[i], i);
+ }
+ next_builder->CreateRet(output);
+
+ llvm::verifyFunction(*function, &llvm::outs());
+ llvm::verifyModule(*module, &llvm::outs());
+
+ return function;
+}
+
void Graph::generateCode(
llvm::IRBuilder<> *builder,
std::vector<AnySocket> &inputs, std::vector<AnySocket> &outputs, std::vector<llvm::Value *> &input_values,
@@ -175,4 +225,4 @@ std::string Graph::toDotFormat(std::vector<SimpleNode *> marked_nodes) const
return ss.str();
}
-} /* namespace NodeCompiler */
\ No newline at end of file
+} /* namespace LLVMNodeCompiler */
\ No newline at end of file
diff --git a/source/blender/modifiers/intern/node_compiler.hpp b/source/blender/modifiers/intern/node_compiler.hpp
index c478854395a..54ee0c7e9ed 100644
--- a/source/blender/modifiers/intern/node_compiler.hpp
+++ b/source/blender/modifiers/intern/node_compiler.hpp
@@ -11,7 +11,7 @@
#include "HashSet.hpp"
#include "HashMap.hpp"
-namespace NodeCompiler {
+namespace LLVMNodeCompiler {
struct AnySocket;
struct SocketInfo;
@@ -26,8 +26,8 @@ struct AnySocket {
inline SimpleNode *node() const { return this->_node; }
inline uint index() const { return this->_index; }
- const llvm::Type *type() const;
- const std::string &debug_name() const;
+ llvm::Type *type() const;
+ std::string debug_name() const;
inline static AnySocket NewInput(SimpleNode *node, uint index)
{ return AnySocket(node, false, index); }
@@ -62,8 +62,8 @@ using SocketMap = HashMap<AnySocket, TValue>;
using SocketValueMap = SocketMap<llvm::Value *>;
struct SocketInfo {
- const std::string debug_name;
- const llvm::Type *type;
+ std::string debug_name;
+ llvm::Type *type;
SocketInfo(std::string debug_name, llvm::Type *type)
: debug_name(debug_name), type(type) {}
@@ -109,6 +109,10 @@ struct Graph {
std::vector<SimpleNode *> nodes;
LinkSet links;
+ llvm::Function *generateFunction(
+ llvm::Module *module, std::string name,
+ std::vector<AnySocket> &inputs, std::vector<AnySocket> &outputs);
+
void generateCode(
llvm::IRBuilder<> *builder,
std::vector<AnySocket> &inputs, std::vector<AnySocket> &outputs, std::vector<llvm::Value *> &input_values,
@@ -129,4 +133,4 @@ private:
llvm::IRBuilder<> **r_builder);
};
-} /* namespace NodeCompiler */
+} /* namespace LLVMNodeCompiler */
diff --git a/source/blender/modifiers/intern/node_compiler_testing.cpp b/source/blender/modifiers/intern/node_compiler_testing.cpp
index 63d0af38bf9..1b687ad69ef 100644
--- a/source/blender/modifiers/intern/node_compiler_testing.cpp
+++ b/source/blender/modifiers/intern/node_compiler_testing.cpp
@@ -7,7 +7,7 @@ extern "C" {
#include <iostream>
-namespace NC = NodeCompiler;
+namespace NC = LLVMNodeCompiler;
static void generateCode_AddNode(
std::vector<llvm::Value *> &inputs, llvm::IRBuilder<> *builder,
@@ -94,27 +94,10 @@ void run_tests()
graph.links.links.push_back(NC::Link(add2->Output(0), add3->Input(1)));
llvm::Module *module = new llvm::Module("test", context);
- std::vector<llvm::Type *> arg_types = {};
- llvm::FunctionType *ftype = llvm::FunctionType::get(
- llvm::Type::getInt32Ty(context), arg_types, false);
-
- llvm::Function *func = llvm::Function::Create(
- ftype, llvm::GlobalValue::LinkageTypes::ExternalLinkage, "my_func", module);
-
- llvm::BasicBlock *bb = llvm::BasicBlock::Create(context, "entry", func);
- llvm::IRBuilder<> builder(context);
- builder.SetInsertPoint(bb);
-
- std::vector<NC::AnySocket> inputs = {};
- std::vector<llvm::Value *> input_values = {};
- std::vector<NC::AnySocket> outputs = { add3->Output(0) };
- llvm::IRBuilder<> *next_builder;
- std::vector<llvm::Value *> output_values;
- graph.generateCode(&builder, inputs, outputs, input_values, &next_builder, output_values);
- next_builder->CreateRet(output_values[0]);
-
- llvm::verifyFunction(*func, &llvm::outs());
- llvm::verifyModule(*module, &llvm::outs());
+
+ std::vector<NC::AnySocket> inputs = { };
+ std::vector<NC::AnySocket> outputs = { add3->Output(0), add1->Input(0) };
+ graph.generateFunction(module, "HelloWorld", inputs, outputs);
module->print(llvm::outs(), nullptr);
More information about the Bf-blender-cvs
mailing list