[Bf-blender-cvs] [24b7839] object_nodes: Disable IR module loading for blenvm by default (with a compile option).

Lukas Tönne noreply at git.blender.org
Tue May 10 11:25:08 CEST 2016


Commit: 24b78398cf1c4435a0d707737bf7a2f75b110b38
Author: Lukas Tönne
Date:   Mon May 9 07:23:12 2016 +0200
Branches: object_nodes
https://developer.blender.org/rB24b78398cf1c4435a0d707737bf7a2f75b110b38

Disable IR module loading for blenvm by default (with a compile option).

Base modules for nodes will instead be declared as external functions with regular
linkage through C code. This means some potential for low-level optimization might be lost,
but this shouldn't be a big problem as long as node functions don't get too complex
(which means they should be split up in the first place).

IR module loading is not the real issue, but generating the IR code from C/C++ code
in a generic way is not so easy. In the future we may actually generate most code for
basic operators and types directly inside blenvm and only use precompiled modules
for more complex functions.

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

M	CMakeLists.txt
M	source/blender/blenvm/llvm/CMakeLists.txt
M	source/blender/blenvm/llvm/llvm_codegen.cc
M	source/blender/blenvm/llvm/llvm_engine.cc
M	source/blender/blenvm/llvm/llvm_engine.h
A	source/blender/blenvm/llvm/llvm_modules.cc
A	source/blender/blenvm/llvm/llvm_modules.h

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

diff --git a/CMakeLists.txt b/CMakeLists.txt
index 6fc4ca2..4a4d11e 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -229,6 +229,8 @@ option(WITH_GAMEENGINE    "Enable Game Engine" ${_init_GAMEENGINE})
 option(WITH_PLAYER        "Build Player" OFF)
 option(WITH_OPENCOLORIO   "Enable OpenColorIO color management" ${_init_OPENCOLORIO})
 
+option(WITH_BLENVM_IRMODULES   "Enable LLVM IR module compilation" OFF)
+
 # Compositor
 option(WITH_COMPOSITOR         "Enable the tile based nodal compositor" ON)
 
diff --git a/source/blender/blenvm/llvm/CMakeLists.txt b/source/blender/blenvm/llvm/CMakeLists.txt
index cef5817..5d16683 100644
--- a/source/blender/blenvm/llvm/CMakeLists.txt
+++ b/source/blender/blenvm/llvm/CMakeLists.txt
@@ -49,6 +49,8 @@ set(SRC
 	llvm_function.cc
 	llvm_function.h
 	llvm_headers.h
+	llvm_modules.cc
+	llvm_modules.h
 )
 
 set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ${RTTI_DISABLE_FLAGS}")
@@ -60,33 +62,6 @@ blender_add_lib(bf_blenvm_llvm "${SRC}" "${INC}" "${INC_SYS}")
 
 # Modules
 
-# XXX TODO
-set(LLVM_IR_COMPILER "clang-3.5")
-set(LLVM_IR_INSTALL_PATH "scripts/llvm")
-set(llvm_irs)
-
-macro(BLENVM_LLVM_MODULE_ADD src)
-	get_filename_component(llvm_name ${src} NAME_WE)
-	set(llvm_ir "${llvm_name}.ll")
-
-	add_custom_command(
-		OUTPUT ${llvm_ir}
-		COMMAND ${LLVM_IR_COMPILER}
-				-S
-				${CMAKE_CURRENT_SOURCE_DIR}/${src}
-				-emit-llvm
-				-std=c++0x
-				-o ${CMAKE_CURRENT_BINARY_DIR}/${llvm_ir}
-				-DBLENVM_RUNTIME
-				-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS
-				-I${CMAKE_CURRENT_SOURCE_DIR}/../../blenlib
-		DEPENDS ${LLVM_SRC} ${LLVM_HEADERS})
-
-	delayed_install("${CMAKE_CURRENT_BINARY_DIR}" "${llvm_ir}" ${LLVM_IR_INSTALL_PATH}/modules/)
-	list(APPEND llvm_irs ${llvm_ir})
-endmacro()
-
-
 set(LLVM_SRC
 	../modules/modules.cc
 )
@@ -96,12 +71,45 @@ set(LLVM_HEADERS
 	../modules/mod_value.h
 )
 
-foreach(src ${LLVM_SRC})
-	# Compile LLVM IR code
-	BLENVM_LLVM_MODULE_ADD(${src})
-endforeach()
-
-add_custom_target(bf_blenvm_llvm_modules ALL DEPENDS ${llvm_irs})
-add_dependencies(bf_blenvm_llvm bf_blenvm_llvm_modules)
 
-delayed_install(${CMAKE_CURRENT_SOURCE_DIR} "${LLVM_HEADERS}" ${LLVM_IR_INSTALL_PATH})
+if(WITH_BLENVM_IRMODULES)
+	# XXX TODO
+	set(LLVM_IR_COMPILER "clang-3.5")
+	set(LLVM_IR_INSTALL_PATH "scripts/llvm")
+	set(llvm_irs)
+
+	macro(BLENVM_LLVM_MODULE_ADD src)
+		get_filename_component(llvm_name ${src} NAME_WE)
+		set(llvm_ir "${llvm_name}.ll")
+
+		add_custom_command(
+			OUTPUT ${llvm_ir}
+			COMMAND ${LLVM_IR_COMPILER}
+					-S
+					${CMAKE_CURRENT_SOURCE_DIR}/${src}
+					-emit-llvm
+					-std=c++0x
+					-o ${CMAKE_CURRENT_BINARY_DIR}/${llvm_ir}
+					-DBLENVM_RUNTIME
+					-D__STDC_CONSTANT_MACROS -D__STDC_LIMIT_MACROS
+					-I${CMAKE_CURRENT_SOURCE_DIR}/../../blenlib
+			DEPENDS ${LLVM_SRC} ${LLVM_HEADERS})
+	
+		delayed_install("${CMAKE_CURRENT_BINARY_DIR}" "${llvm_ir}" ${LLVM_IR_INSTALL_PATH}/modules/)
+		list(APPEND llvm_irs ${llvm_ir})
+	endmacro()
+
+	add_definitions(-DWITH_BLENVM_IRMODULES)
+
+	foreach(src ${LLVM_SRC})
+		# Compile LLVM IR code
+		BLENVM_LLVM_MODULE_ADD(${src})
+	endforeach()
+
+	add_custom_target(bf_blenvm_llvm_modules ALL DEPENDS ${llvm_irs})
+	add_dependencies(bf_blenvm_llvm bf_blenvm_llvm_modules)
+
+	delayed_install(${CMAKE_CURRENT_SOURCE_DIR} "${LLVM_HEADERS}" ${LLVM_IR_INSTALL_PATH})
+else()
+
+endif()
diff --git a/source/blender/blenvm/llvm/llvm_codegen.cc b/source/blender/blenvm/llvm/llvm_codegen.cc
index 6cf3849..8e568c6 100644
--- a/source/blender/blenvm/llvm/llvm_codegen.cc
+++ b/source/blender/blenvm/llvm/llvm_codegen.cc
@@ -39,6 +39,7 @@
 #include "llvm_engine.h"
 #include "llvm_function.h"
 #include "llvm_headers.h"
+#include "llvm_modules.h"
 
 /* call signature convention in llvm modules:
  * BYVALUE:   Pass struct types directly into (inlined) functions,
diff --git a/source/blender/blenvm/llvm/llvm_engine.cc b/source/blender/blenvm/llvm/llvm_engine.cc
index 4a20315..d463a14 100644
--- a/source/blender/blenvm/llvm/llvm_engine.cc
+++ b/source/blender/blenvm/llvm/llvm_engine.cc
@@ -29,29 +29,18 @@
  *  \ingroup llvm
  */
 
-#include <map>
-
 extern "C" {
-#include "BLI_fileops.h"
-#include "BLI_fileops_types.h"
-#include "BLI_path_util.h"
 #include "BLI_utildefines.h"
-
-#include "BKE_appdir.h"
 }
 
 #include "llvm_engine.h"
 #include "llvm_headers.h"
+#include "llvm_modules.h"
 
 namespace blenvm {
 
-typedef std::map<string, llvm::Module*> ModuleMap;
-
 static llvm::ExecutionEngine *theEngine = NULL;
 static llvm::Module *theModule = NULL;
-static ModuleMap theModules;
-
-//static ModuleMap theModules;
 
 static llvm::ExecutionEngine *create_execution_engine()
 {
@@ -105,155 +94,4 @@ llvm::ExecutionEngine *llvm_execution_engine()
 	return theEngine;
 }
 
-bool llvm_function_is_external(const llvm::Function *func)
-{
-	return func->hasFnAttribute("name");
-}
-
-llvm::Function *llvm_find_external_function(llvm::Module *mod, const string &name)
-{
-	using namespace llvm;
-	
-	for (Module::FunctionListType::iterator it = mod->getFunctionList().begin(); it != mod->getFunctionList().end(); ++it) {
-		Function *func = &(*it);
-		if (func->hasFnAttribute("name")) {
-			std::string value = func->getFnAttribute("name").getValueAsString();
-			if (value == name)
-				return func;
-		}
-	}
-	return NULL;
-}
-
-string llvm_get_external_function_name(const llvm::Function *func)
-{
-	if (func->hasFnAttribute("name"))
-		return func->getFnAttribute("name").getValueAsString().str();
-	else
-		return func->getName().str();
-}
-
-/* Based on
- * http://homes.cs.washington.edu/~bholt/posts/llvm-quick-tricks.html
- */
-static void llvm_parse_function_annotations(llvm::Module *mod)
-{
-	using namespace llvm;
-	
-	GlobalVariable *global_annos = mod->getNamedGlobal("llvm.global.annotations");
-	if (global_annos) {
-		ConstantArray *a = static_cast<ConstantArray*>(global_annos->getOperand(0));
-		for (int i = 0; i < a->getNumOperands(); i++) {
-			ConstantStruct *e = static_cast<ConstantStruct*>(a->getOperand(i));
-			StringRef anno = static_cast<ConstantDataArray*>(static_cast<GlobalVariable*>(e->getOperand(1)->getOperand(0))->getOperand(0))->getAsCString();
-			
-			if (e->getOperand(0)->getOperand(0)->getValueID() == Value::FunctionVal) {
-				Function *fn = static_cast<Function*>(e->getOperand(0)->getOperand(0));
-				fn->addFnAttr("name", anno); /* add function annotation */
-			}
-		}
-	}
-}
-
-void llvm_load_module(const string &modfile, const string &modname)
-{
-	using namespace llvm;
-	
-	printf("Loading module '%s'\n", modfile.c_str());
-	LLVMContext &llvmctx = getGlobalContext();
-	SMDiagnostic err;
-	
-	Module *mod = getLazyIRFileModule(modfile, err, llvmctx);
-//	Module *mod = ParseIRFile(modfile, err, llvmctx);
-	if (!mod) {
-		err.print(modfile.c_str(), errs());
-		return;
-	}
-	
-	llvm_parse_function_annotations(mod);
-	mod->setModuleIdentifier(modname);
-	
-	verifyModule(*mod, &outs());
-	
-	theEngine->addModule(mod);
-	theModules[mod->getModuleIdentifier()] = mod;
-	
-	printf("Module Functions for '%s'\n", mod->getModuleIdentifier().c_str());
-	for (Module::FunctionListType::const_iterator it = mod->getFunctionList().begin(); it != mod->getFunctionList().end(); ++it) {
-		const Function &func = *it;
-		
-		if (!llvm_function_is_external(&func))
-			continue;
-		printf("    %s\n", llvm_get_external_function_name(&func).c_str());
-		
-//		printf("    %s\n", func.getName().str().c_str());
-		
-//		func.dump();
-//		printf("++++++++++++++++++++++++++++++++++\n");
-	}
-}
-
-void llvm_load_all_modules(const string &modpath, bool reload)
-{
-	using namespace llvm;
-	
-	string path = modpath;
-	if (path.empty())
-		path = string(BKE_appdir_folder_id(BLENDER_SYSTEM_SCRIPTS, "llvm/modules/"));
-	if (path.empty())
-		return;
-	
-	if (reload) {
-		llvm_unload_all_modules();
-	}
-	
-	struct direntry *dir;
-	int totfile = BLI_filelist_dir_contents(path.c_str(), &dir);
-	for (int i = 0; i < totfile; ++i) {
-		if ((dir[i].type & S_IFREG)) {
-			const char *filename = dir[i].relname;
-			const char *filepath = dir[i].path;
-			
-			if (BLI_testextensie(filename, ".ll")) {
-				/* found a potential llvm IR module, try parsing it */
-				llvm_load_module(filepath, filename);
-			}
-		}
-	}
-	BLI_filelist_free(dir, totfile);
-}
-
-void llvm_unload_all_modules()
-{
-	using namespace llvm;
-	
-	// TODO
-	theModules.clear();
-}
-
-/* links the module to all other modules in the module map */
-void llvm_link_module_full(llvm::Module *mod)
-{
-	using namespace llvm;
-	
-	for (ModuleMap::const_iterator it = theModules.begin(); it != theModules.end(); ++it) {
-		Module *lmod = it->second;
-		std::string error;
-		Linker::LinkModules(mod, lmod, Linker::LinkerMode::PreserveSource, &error);
-	}
-	
-//	printf("Linked Module Functions for '%s'\n", mod->getModuleIdentifier().c_str());
-//	for (Module::FunctionListType::const_iterator it = mod->getFunctionList().begin(); it != mod->getFunctionList().end(); ++it) {
-//		printf("    %s\n", it->getName().str().c_str());
-//	}
-	
-	verifyModule(*mod, &outs());
-	
-//	PassManager *pm = create_pass_manager();
-//	pm->run(*mod);
-//	delete pm;
-	
-	theEngine->finalizeObject();
-}
-
 } /* namespace llvm */
diff --git a/source/blender/blenvm/llvm/llvm_engine.h b/source/blender/blenvm/llvm/llvm_engine.h
index 095f76b..fdbeb69 100644
--- a/source/blender/blenvm/llvm/llvm_engine.h
+++ b/source/blender/blenvm/llvm/llvm_engine.h
@@ -36,8 +36,6 @@
 
 namespace llvm {
 class ExecutionEngine;
-class Function;
-class Module;
 }
 
 namespace blenvm {
@@ -47,16 +45,6 @@ void llvm_free();
 
 llvm::ExecutionEngine *llvm_execution_engine();
 
-bool llvm_function_is_external(const llvm::Function *func);
-llvm::Function *llvm_find_external_function(llvm::Module *mod, const string &name);
-string llvm_get_external_function_name(const llvm::Function *func);
-
-void llvm_load_module(const string &modfile, const string &modname);
-void llvm_load_all_modules(const string &modpath, bool reload);
-void llvm_unload_all_modules();
-
-void llvm_link_module_full(llvm::Module *mod);
-
 } /* namespace blenvm */
 
 #endif /* __LLVM_ENGINE_H__ */
diff --git a/source/blender/blenvm/llvm/llvm_engine.cc b/source/blender/blenvm/llvm/llvm_modules.cc
similarity index 76%
copy from so

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list