Jan Rehders
2007-Jun-07 15:38 UTC
[LLVMdev] How to call native functions from bytecode run in JIT?
Hello, can anyone help me calling native functions from LLVM-Bytecode functions run in the JIT? I have a program which creates an LLVM execution engine and adds modules and functions to it on the fly. I need to call some native functions of my program from bytecode functions which causes some troubles as it appears not to be documented. My test scenario works like the following: I have a simple function "int get5()" which I want to call from a bytecode function "int callget5()" which is generated at runtime. I register the native function "get5" using addGlobalMapping in the execution engine. I then try to call it using runFunction. This works under OS X, but fails in Linux giving something like "tried to call unknown function int* () get5". Calling module->getFunction("foo5") does not return NULL, though. Some relevant code snippets: // the native function int get5() { return 5; } // registering it std::vector<const Type*> emptyArgList; FunctionType* get5Type = FunctionType::get(Type::Int32Ty, emptyArgList, false); Function* get5Function = new Function(get5Type, GlobalValue::ExternalLinkage, "get5", m); EE->addGlobalMapping( get5Function, (void*)get5 ); // verifying it's existance Function* func = m->getFunction("get5"); if( func == NULL ) { std::cout << "registered native function not found\n"; exit(-1); } // calling it EE->runFunction(func, noargs).IntVal.getLimitedValue() As said, it will fail calling the function in Linux (but works in OS X). Version is LLVM 2.0, OS X 10.4.9, several linux distros How can I fix this? When I try to construct a function which will call "get5" it will fail on OS X, too. I have attached the whole .cpp file containing the complete code. It's adapted from the JIT example in the LLVM source distribution. I compile it using g++ -c codegen1.cpp -o codegen1.o -D__STDC_LIMIT_MACROS g++ -o codegen1 -L${LLVM_LIB_DIR} codegen1.o ${LLVM_LIB_DIR}/ LLVMPowerPC.o -lLLVMSelectionDAG ${LLVM_LIB_DIR}/LLVMInterpreter.o $ {LLVM_LIB_DIR}/LLVMJIT.o -lLLVMCodeGen -lLLVMScalarOpts - lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis ${LLVM_LIB_DIR}/ LLVMExecutionEngine.o -lLLVMTarget -lLLVMCore -lLLVMSupport - lLLVMbzip2 -lLLVMSystem -lpthread -ldl greetings, Jan Rehders -------------- next part -------------- A non-text attachment was scrubbed... Name: codegen1.cpp Type: application/octet-stream Size: 5240 bytes Desc: not available URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20070607/653399de/attachment.obj>
Evan Cheng
2007-Jun-10 04:08 UTC
[LLVMdev] How to call native functions from bytecode run in JIT?
Are you able make calls to well known external functions such as printf? As far as I known, this capability is well tested on x86 / Linux. I am wondering if there is some name mangling issue? Evan On Jun 7, 2007, at 8:38 AM, Jan Rehders wrote:> Hello, > > can anyone help me calling native functions from LLVM-Bytecode > functions run in the JIT? > > I have a program which creates an LLVM execution engine and adds > modules and functions > to it on the fly. I need to call some native functions of my > program from bytecode functions which causes some troubles as it > appears not to be documented. My test scenario works like the > following: > > I have a simple function "int get5()" which I want to call from a > bytecode function "int callget5()" which is generated at runtime. I > register the native function "get5" using addGlobalMapping in the > execution engine. I then try to call it using runFunction. This > works under OS X, but fails in Linux giving something like "tried > to call unknown function int* () get5". Calling module->getFunction > ("foo5") does not return NULL, though. Some relevant code snippets: > > // the native function > > int get5() { return 5; } > > // registering it > > std::vector<const Type*> emptyArgList; > FunctionType* get5Type = FunctionType::get(Type::Int32Ty, > emptyArgList, false); > Function* get5Function = new Function(get5Type, > GlobalValue::ExternalLinkage, "get5", m); > EE->addGlobalMapping( get5Function, (void*)get5 ); > > // verifying it's existance > > Function* func = m->getFunction("get5"); > if( func == NULL ) { > std::cout << "registered native function not found\n"; > exit(-1); > } > > // calling it > > EE->runFunction(func, noargs).IntVal.getLimitedValue() > > As said, it will fail calling the function in Linux (but works in > OS X). Version is LLVM 2.0, OS X 10.4.9, several linux distros > > How can I fix this? > > When I try to construct a function which will call "get5" it will > fail on OS X, too. I have attached the whole .cpp file containing > the complete code. It's adapted from the JIT example in the LLVM > source distribution. I compile it using > > g++ -c codegen1.cpp -o codegen1.o -D__STDC_LIMIT_MACROS > g++ -o codegen1 -L${LLVM_LIB_DIR} codegen1.o ${LLVM_LIB_DIR}/ > LLVMPowerPC.o -lLLVMSelectionDAG ${LLVM_LIB_DIR}/LLVMInterpreter.o $ > {LLVM_LIB_DIR}/LLVMJIT.o -lLLVMCodeGen -lLLVMScalarOpts - > lLLVMTransformUtils -lLLVMipa -lLLVMAnalysis ${LLVM_LIB_DIR}/ > LLVMExecutionEngine.o -lLLVMTarget -lLLVMCore -lLLVMSupport - > lLLVMbzip2 -lLLVMSystem -lpthread -ldl > > greetings, > Jan Rehders > > <codegen1.cpp> > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
Jan Rehders
2007-Jun-11 15:23 UTC
[LLVMdev] How to call native functions from bytecode run in JIT?
> Are you able make calls to well known external functions such as > printf? As far as I known, this capability is well tested on x86 / > Linux.Calling printf works at least on OS X (I'm waiting for a reply whether this works on linux). If I call my native function it fails with PPCJITInfo.cpp:382: failed assertion `ResultPtr >= -(1 << 23) && ResultPtr < (1 << 23) && "Relocation out of range!"'> I am wondering if there is some name mangling issue?Not that I'm aware of. It appears to find the function using module- >getFunction("get5") Is there really nobody here who knows how to properly register native functions of the application to the execution engine?
Possibly Parallel Threads
- [LLVMdev] How to call native functions from bytecode run in JIT?
- [LLVMdev] How to call native functions from bytecode run in JIT?
- [LLVMdev] How to call native functions from bytecode run in JIT?
- [LLVMdev] How to call native functions from bytecode run in JIT?
- [LLVMdev] How to call native functions from bytecode run in JIT?