[Bf-blender-cvs] [2205d28] object_nodes: Use annotation attributes on module functions to avoid name mangling issues.

Lukas Tönne noreply at git.blender.org
Fri Apr 8 17:30:50 CEST 2016


Commit: 2205d2870d8484573554eb0b317b83a1888de673
Author: Lukas Tönne
Date:   Thu Apr 7 17:54:53 2016 +0200
Branches: object_nodes
https://developer.blender.org/rB2205d2870d8484573554eb0b317b83a1888de673

Use annotation attributes on module functions to avoid name mangling issues.

Names in C++ code tend to get mangled by the clang compiler according to the
respective target ABI. This is unfortunate because it makes it difficult to
map functions to nodes by name. For now we use a function attribute in the module
C++ code to pass on the "true" name of the function and map them internally.

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

M	source/blender/blenvm/llvm/llvm_codegen.cc
M	source/blender/blenvm/llvm/llvm_engine.cc
M	source/blender/blenvm/modules/mod_value.cc
M	source/blender/blenvm/modules/mod_value.h

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

diff --git a/source/blender/blenvm/llvm/llvm_codegen.cc b/source/blender/blenvm/llvm/llvm_codegen.cc
index 2143df5..91d62de 100644
--- a/source/blender/blenvm/llvm/llvm_codegen.cc
+++ b/source/blender/blenvm/llvm/llvm_codegen.cc
@@ -104,14 +104,14 @@ namespace blenvm {
  * rather than storing full TypeDesc in each socket!
  * These functions just provides per-socket type names in the meantime.
  */
-static string dummy_type_name(const InputKey &input)
+inline string dummy_type_name(const InputKey &input)
 {
 	size_t hash = std::hash<const NodeInstance *>()(input.node) ^ std::hash<const NodeInput *>()(input.socket);
 	std::stringstream ss;
 	ss << "InputType" << (unsigned short)hash;
 	return ss.str();
 }
-static string dummy_type_name(const OutputKey &output)
+inline string dummy_type_name(const OutputKey &output)
 {
 	size_t hash = std::hash<const NodeInstance *>()(output.node) ^ std::hash<const NodeOutput *>()(output.socket);
 	std::stringstream ss;
@@ -283,14 +283,12 @@ llvm::CallInst *LLVMCompiler::codegen_node_call(llvm::BasicBlock *block,
 {
 	using namespace llvm;
 	
-	const bool use_struct_return = (node->num_outputs() > 1);
-	
 	IRBuilder<> builder(context());
 	builder.SetInsertPoint(block);
 	
 	/* get evaluation function */
 	const std::string &evalname = node->type->name();
-	Function *evalfunc = module()->getFunction(evalname);
+	Function *evalfunc = llvm_find_external_function(module(), evalname);
 	if (!evalfunc) {
 		printf("Could not find node function '%s'\n", evalname.c_str());
 		return NULL;
diff --git a/source/blender/blenvm/llvm/llvm_engine.cc b/source/blender/blenvm/llvm/llvm_engine.cc
index ac9a6f6..4a20315 100644
--- a/source/blender/blenvm/llvm/llvm_engine.cc
+++ b/source/blender/blenvm/llvm/llvm_engine.cc
@@ -133,7 +133,6 @@ string llvm_get_external_function_name(const llvm::Function *func)
 		return func->getName().str();
 }
 
-#if 0
 /* Based on
  * http://homes.cs.washington.edu/~bholt/posts/llvm-quick-tricks.html
  */
@@ -148,14 +147,13 @@ static void llvm_parse_function_annotations(llvm::Module *mod)
 			ConstantStruct *e = static_cast<ConstantStruct*>(a->getOperand(i));
 			StringRef anno = static_cast<ConstantDataArray*>(static_cast<GlobalVariable*>(e->getOperand(1)->getOperand(0))->getOperand(0))->getAsCString();
 			
-			Function *fn = dynamic_cast<Function*>(e->getOperand(0)->getOperand(0));
-			if (fn) {
+			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 */
 			}
 		}
 	}
 }
-#endif
 
 void llvm_load_module(const string &modfile, const string &modname)
 {
@@ -172,31 +170,27 @@ void llvm_load_module(const string &modfile, const string &modname)
 		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());
+		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());
+//		printf("    %s\n", func.getName().str().c_str());
 		
 //		func.dump();
 //		printf("++++++++++++++++++++++++++++++++++\n");
 	}
-	
-	/* XXX this code was used to identify special node functions,
-	 * similar to the "shader" keyword in OSL.
-	 * Not sure if needed eventually.
-	 */
-//	bjit_parse_function_annotations(mod);
-	mod->setModuleIdentifier(modname);
-	
-	verifyModule(*mod, &outs());
-	
-	theEngine->addModule(mod);
-	theModules[mod->getModuleIdentifier()] = mod;
 }
 
 void llvm_load_all_modules(const string &modpath, bool reload)
diff --git a/source/blender/blenvm/modules/mod_value.cc b/source/blender/blenvm/modules/mod_value.cc
index 1985926..11c43e8 100644
--- a/source/blender/blenvm/modules/mod_value.cc
+++ b/source/blender/blenvm/modules/mod_value.cc
@@ -28,27 +28,27 @@
 #include "mod_math.h"
 #include "mod_value.h"
 
-float VALUE_FLOAT(float value)
+void VALUE_FLOAT(float *result, const float *value)
 {
-	return value;
+	*result = *value;
 }
 
-float3 VALUE_FLOAT3(float3 value)
+void VALUE_FLOAT3(float3 &result, const float3 &value)
 {
-	return value;
+	result = value;
 }
 
-float4 VALUE_FLOAT4(float4 value)
+void VALUE_FLOAT4(float4 &result, const float4 &value)
 {
-	return value;
+	result = value;
 }
 
-int VALUE_INT(int value)
+void VALUE_INT(int &result, const int &value)
 {
-	return value;
+	result = value;
 }
 
-matrix44 VALUE_MATRIX44(matrix44 value)
+void VALUE_MATRIX44(matrix44 &result, const matrix44 &value)
 {
-	return value;
+	result = value;
 }
diff --git a/source/blender/blenvm/modules/mod_value.h b/source/blender/blenvm/modules/mod_value.h
index 8275f10..a0eff6d 100644
--- a/source/blender/blenvm/modules/mod_value.h
+++ b/source/blender/blenvm/modules/mod_value.h
@@ -28,10 +28,10 @@
 #ifndef __MOD_VALUE_H__
 #define __MOD_VALUE_H__
 
-float VALUE_FLOAT(float value);
-float3 VALUE_FLOAT3(float3 value);
-float4 VALUE_FLOAT4(float4 value);
-int VALUE_INT(int value);
-matrix44 VALUE_MATRIX44(matrix44 value);
+__attribute__((annotate("VALUE_FLOAT"))) void VALUE_FLOAT(float *result, const float *value);
+__attribute__((annotate("VALUE_FLOAT3"))) void VALUE_FLOAT3(float3 &result, const float3 &value);
+__attribute__((annotate("VALUE_FLOAT4"))) void VALUE_FLOAT4(float4 &result, const float4 &value);
+__attribute__((annotate("VALUE_INT"))) void VALUE_INT(int &result, const int &value);
+__attribute__((annotate("VALUE_MATRIX44"))) void VALUE_MATRIX44(matrix44 &result, const matrix44 &value);
 
 #endif /* __MOD_VALUE_H__ */




More information about the Bf-blender-cvs mailing list