[Bf-blender-cvs] [9032c2a] object_nodes: Generalization of the function cache feature to support both bvm and llvm backends.

Lukas Tönne noreply at git.blender.org
Mon Apr 4 17:35:19 CEST 2016


Commit: 9032c2ae961dcb633cb832fba680647f2c0ffae0
Author: Lukas Tönne
Date:   Fri Apr 1 09:23:35 2016 +0200
Branches: object_nodes
https://developer.blender.org/rB9032c2ae961dcb633cb832fba680647f2c0ffae0

Generalization of the function cache feature to support both bvm and llvm backends.

This code is not very elegant atm, but it does the job. Have to see how internal code
management of LLVM (modules) plays into this, the function cache then could become
redundant.

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

M	source/blender/blenvm/BVM_api.h
M	source/blender/blenvm/CMakeLists.txt
M	source/blender/blenvm/compile/CMakeLists.txt
M	source/blender/blenvm/compile/bvm_function.cc
M	source/blender/blenvm/compile/bvm_function.h
M	source/blender/blenvm/intern/bvm_api.cc
A	source/blender/blenvm/intern/function.cc
A	source/blender/blenvm/intern/function.h
M	source/blender/blenvm/intern/function_cache.cc
M	source/blender/blenvm/intern/function_cache.h
M	source/blender/blenvm/llvm/CMakeLists.txt
M	source/blender/blenvm/llvm/llvm_function.h

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

diff --git a/source/blender/blenvm/BVM_api.h b/source/blender/blenvm/BVM_api.h
index 24218a5..3363e29 100644
--- a/source/blender/blenvm/BVM_api.h
+++ b/source/blender/blenvm/BVM_api.h
@@ -118,11 +118,17 @@ typedef enum BVMDebugMode {
 void BVM_function_bvm_free(struct BVMFunction *fn);
 
 struct BVMFunction *BVM_function_bvm_cache_acquire(void *key);
-void BVM_function_bvm_cache_release(struct BVMFunction *_fn);
-void BVM_function_bvm_cache_set(void *key, struct BVMFunction *_fn);
+void BVM_function_bvm_cache_release(struct BVMFunction *fn);
+void BVM_function_bvm_cache_set(void *key, struct BVMFunction *fn);
 void BVM_function_bvm_cache_remove(void *key);
 void BVM_function_bvm_cache_clear(void);
 
+struct BVMFunction *BVM_function_llvm_cache_acquire(void *key);
+void BVM_function_llvm_cache_release(struct BVMFunction *fn);
+void BVM_function_llvm_cache_set(void *key, struct BVMFunction *fn);
+void BVM_function_llvm_cache_remove(void *key);
+void BVM_function_llvm_cache_clear(void);
+
 /* ------------------------------------------------------------------------- */
 
 struct Object;
diff --git a/source/blender/blenvm/CMakeLists.txt b/source/blender/blenvm/CMakeLists.txt
index 570e118..60bbf90 100644
--- a/source/blender/blenvm/CMakeLists.txt
+++ b/source/blender/blenvm/CMakeLists.txt
@@ -35,6 +35,8 @@ set(INC
 	.
 	bvm
 	compile
+	llvm
+	intern
 	util
 	../blenkernel
 	../blenlib
@@ -50,6 +52,8 @@ set(INC_SYS
 
 set(SRC
 	intern/bvm_api.cc
+	intern/function.cc
+	intern/function.h
 	intern/function_cache.cc
 	intern/function_cache.h
 
diff --git a/source/blender/blenvm/compile/CMakeLists.txt b/source/blender/blenvm/compile/CMakeLists.txt
index 02246cd..5e10574 100644
--- a/source/blender/blenvm/compile/CMakeLists.txt
+++ b/source/blender/blenvm/compile/CMakeLists.txt
@@ -27,6 +27,7 @@ set(INC
 	.
 	..
 	../bvm
+	../intern
 	../util
 	../../blenkernel
 	../../blenlib
diff --git a/source/blender/blenvm/compile/bvm_function.cc b/source/blender/blenvm/compile/bvm_function.cc
index 68dc240..ba0c282 100644
--- a/source/blender/blenvm/compile/bvm_function.cc
+++ b/source/blender/blenvm/compile/bvm_function.cc
@@ -34,11 +34,7 @@
 
 namespace blenvm {
 
-mutex FunctionBVM::users_mutex = mutex();
-spin_lock FunctionBVM::users_lock = spin_lock(FunctionBVM::users_mutex);
-
-FunctionBVM::FunctionBVM() :
-    m_users(0)
+FunctionBVM::FunctionBVM()
 {
 }
 
@@ -46,29 +42,6 @@ FunctionBVM::~FunctionBVM()
 {
 }
 
-void FunctionBVM::retain(FunctionBVM *fn)
-{
-	if (fn) {
-		users_lock.lock();
-		++fn->m_users;
-		users_lock.unlock();
-	}
-}
-
-void FunctionBVM::release(FunctionBVM **fn)
-{
-	if (*fn) {
-		users_lock.lock();
-		assert((*fn)->m_users > 0);
-		--(*fn)->m_users;
-		if ((*fn)->m_users == 0) {
-			delete *fn;
-			*fn = NULL;
-		}
-		users_lock.unlock();
-	}
-}
-
 size_t FunctionBVM::num_arguments() const
 {
 	return m_arguments.size();
diff --git a/source/blender/blenvm/compile/bvm_function.h b/source/blender/blenvm/compile/bvm_function.h
index 8b3259d..d022408 100644
--- a/source/blender/blenvm/compile/bvm_function.h
+++ b/source/blender/blenvm/compile/bvm_function.h
@@ -37,13 +37,13 @@
 
 #include "MEM_guardedalloc.h"
 
+#include "function.h"
 #include "typedesc.h"
 
 #include "bvm_instruction_list.h"
 
 #include "util_opcode.h"
 #include "util_string.h"
-#include "util_thread.h"
 
 namespace blenvm {
 
@@ -64,15 +64,12 @@ struct Argument {
 	MEM_CXX_CLASS_ALLOC_FUNCS("BVM:ReturnValue")
 };
 
-struct FunctionBVM : public InstructionList {
+struct FunctionBVM : public Function, public InstructionList {
 	typedef std::vector<Argument> ArgumentList;
 	
 	FunctionBVM();
 	~FunctionBVM();
 	
-	static void retain(FunctionBVM *fn);
-	static void release(FunctionBVM **fn);
-	
 	size_t num_return_values() const;
 	const Argument &return_value(size_t index) const;
 	const Argument &return_value(const string &name) const;
@@ -89,12 +86,7 @@ private:
 	ArgumentList m_arguments;
 	ArgumentList m_return_values;
 	
-	int m_users;
-	
-	static mutex users_mutex;
-	static spin_lock users_lock;
-	
-	MEM_CXX_CLASS_ALLOC_FUNCS("BVM:Function")
+	MEM_CXX_CLASS_ALLOC_FUNCS("BVM:FunctionBVM")
 };
 
 } /* namespace blenvm */
diff --git a/source/blender/blenvm/intern/bvm_api.cc b/source/blender/blenvm/intern/bvm_api.cc
index f86f985..44dd173 100644
--- a/source/blender/blenvm/intern/bvm_api.cc
+++ b/source/blender/blenvm/intern/bvm_api.cc
@@ -64,6 +64,7 @@ extern "C" {
 
 #ifdef WITH_LLVM
 #include "llvm/llvm_engine.h"
+#include "llvm/llvm_function.h"
 #endif
 
 #include "util_debug.h"
@@ -319,11 +320,11 @@ void BVM_context_free(struct BVMEvalContext *ctx)
 
 /* ------------------------------------------------------------------------- */
 
-BLI_INLINE blenvm::FunctionBVM *_FUNC(struct BVMFunction *fn)
+BLI_INLINE blenvm::FunctionBVM *_FUNC_BVM(struct BVMFunction *fn)
 { return (blenvm::FunctionBVM *)fn; }
 
 void BVM_function_bvm_free(struct BVMFunction *fn)
-{ delete _FUNC(fn); }
+{ delete _FUNC_BVM(fn); }
 
 struct BVMFunction *BVM_function_bvm_cache_acquire(void *key)
 {
@@ -332,12 +333,12 @@ struct BVMFunction *BVM_function_bvm_cache_acquire(void *key)
 
 void BVM_function_bvm_cache_release(BVMFunction *fn)
 {
-	blenvm::function_bvm_cache_release(_FUNC(fn));
+	blenvm::function_bvm_cache_release(_FUNC_BVM(fn));
 }
 
 void BVM_function_bvm_cache_set(void *key, BVMFunction *fn)
 {
-	blenvm::function_bvm_cache_set(key, _FUNC(fn));
+	blenvm::function_bvm_cache_set(key, _FUNC_BVM(fn));
 }
 
 void BVM_function_bvm_cache_remove(void *key)
@@ -350,6 +351,42 @@ void BVM_function_bvm_cache_clear(void)
 	blenvm::function_bvm_cache_clear();
 }
 
+#ifdef WITH_LLVM
+BLI_INLINE blenvm::FunctionLLVM *_FUNC_LLVM(struct BVMFunction *fn)
+{ return (blenvm::FunctionLLVM *)fn; }
+
+struct BVMFunction *BVM_function_llvm_cache_acquire(void *key)
+{
+	return (BVMFunction *)blenvm::function_llvm_cache_acquire(key);
+}
+
+void BVM_function_llvm_cache_release(BVMFunction *fn)
+{
+	blenvm::function_llvm_cache_release(_FUNC_LLVM(fn));
+}
+
+void BVM_function_llvm_cache_set(void *key, BVMFunction *fn)
+{
+	blenvm::function_llvm_cache_set(key, _FUNC_LLVM(fn));
+}
+
+void BVM_function_llvm_cache_remove(void *key)
+{
+	blenvm::function_llvm_cache_remove(key);
+}
+
+void BVM_function_llvm_cache_clear(void)
+{
+	blenvm::function_llvm_cache_clear();
+}
+#else
+struct BVMFunction *BVM_function_llvm_cache_acquire(void */*key*/) { return NULL; }
+void BVM_function_llvm_cache_release(BVMFunction */*fn*/) {}
+void BVM_function_llvm_cache_set(void */*key*/, BVMFunction */*fn*/) {}
+void BVM_function_llvm_cache_remove(void */*key*/) {}
+void BVM_function_llvm_cache_clear(void) {}
+#endif
+
 /* ------------------------------------------------------------------------- */
 
 static void parse_py_nodes(bNodeTree *btree, blenvm::NodeGraph *graph)
@@ -435,7 +472,7 @@ void BVM_eval_forcefield(struct BVMEvalGlobals *globals, struct BVMEvalContext *
 	const void *args[] = { &object_ptr, point->loc, point->vel };
 	void *results[] = { force, impulse };
 	
-	_FUNC(fn)->eval(_CTX(ctx), _GLOBALS(globals), args, results);
+	_FUNC_BVM(fn)->eval(_CTX(ctx), _GLOBALS(globals), args, results);
 }
 
 /* ------------------------------------------------------------------------- */
@@ -979,7 +1016,7 @@ void BVM_eval_texture(struct BVMEvalContext *ctx, struct BVMFunction *fn,
 	const void *args[] = { coord, dxt, dyt, &cfra, &osatex };
 	void *results[] = { &color.x, &normal.x };
 	
-	_FUNC(fn)->eval(_CTX(ctx), &globals, args, results);
+	_FUNC_BVM(fn)->eval(_CTX(ctx), &globals, args, results);
 	
 	target->tr = color.x;
 	target->tg = color.y;
@@ -1063,7 +1100,7 @@ struct DerivedMesh *BVM_eval_modifier(struct BVMEvalGlobals *globals,
 	const void *args[] = { &object_ptr, &base_mesh_ptr };
 	void *results[] = { &result };
 	
-	_FUNC(fn)->eval(_CTX(ctx), _GLOBALS(globals), args, results);
+	_FUNC_BVM(fn)->eval(_CTX(ctx), _GLOBALS(globals), args, results);
 	
 	DerivedMesh *dm = result.get();
 	/* destroy the pointer variable */
@@ -1138,7 +1175,7 @@ void BVM_eval_dupli(struct BVMEvalGlobals *globals,
 	duplis_ptr result;
 	void *results[] = { &result };
 	
-	_FUNC(fn)->eval(_CTX(ctx), _GLOBALS(globals), args, results);
+	_FUNC_BVM(fn)->eval(_CTX(ctx), _GLOBALS(globals), args, results);
 	
 	DupliList *duplis = result.get();
 	if (duplis) {
diff --git a/source/blender/blenvm/intern/function_cache.h b/source/blender/blenvm/intern/function.cc
similarity index 63%
copy from source/blender/blenvm/intern/function_cache.h
copy to source/blender/blenvm/intern/function.cc
index 956a2d2..8165fcc 100644
--- a/source/blender/blenvm/intern/function_cache.h
+++ b/source/blender/blenvm/intern/function.cc
@@ -25,27 +25,48 @@
  * ***** END GPL LICENSE BLOCK *****
  */
 
-#ifndef __FUNCTION_CACHE_H__
-#define __FUNCTION_CACHE_H__
-
-/** \file function_cache.h
+/** \file blender/blenvm/intern/function.cc
  *  \ingroup blenvm
  */
 
-#include "util_map.h"
+#include <assert.h>
+
+#include "function.h"
 
 namespace blenvm {
 
-struct FunctionBVM;
+mutex Function::users_mutex = mutex();
+spin_lock Function::users_lock = spin_lock(Function::users_mutex);
 
-typedef unordered_map<void*, FunctionBVM*> FunctionBVMCache;
+Function::Function() :
+    m_users(0)
+{
+}
 
-FunctionBVM *function_bvm_cache_acquire(void *key);
-void function_bvm_cache_release(FunctionBVM *fn);
-void function_bvm_cache_set(void *key, FunctionBVM *fn);
-void function_bvm_cache_remove(void *key);
-void function_bvm_cache_clear(void);
+Function::~Function()
+{
+}
 
-} /* namespace blenvm */
+void Function::retain(Function *fn)
+{
+	if (fn) {
+		users_lock.lock();
+		++fn->m_users;
+		users_lock.unlock();
+	}
+}
 
-#endif /* __FUNCTION_CACHE_H__ */
+bool Function::release(Function *fn)
+{
+	bool released = false;
+	if (fn) {
+		users_lock.lock();
+		assert(fn->m_users > 0);
+		--fn->m_users;
+		released = (fn->m_users == 0);
+		users_lock.unlock();
+	}
+	return released;
+}
+
+} /* namespace blenvm */
diff --git a/source/blender/blenvm/intern/function_cache.h b/source/blender/blenvm/intern/function.h
similarity index 67%
copy from source/blender/blenvm/intern/function_cache.h
copy to source/blender/blenvm/intern/function.h
index 956a2d2..fc72f83 100644
--- a/source/blender/blenvm/intern/function_c

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list