Linking LLVM code with LLVM

Hi all,

Sorry if this is obvious.
I'm trying to link LLVM-generated code with LLVM itself, and don't know how to do it. Just as a little test, I've tried compiling the example HowToUseJIT.cpp using llvm-g++, but I get tons of errors.

Is there an easy way of doing this? Has anyone tried it before?

Thanks,

Marc Ordinas i Llopis | Tragnarion Studios

What sort of errors do you get? Are you passing the right -I options?

-Chris

Excuse the long post,

Chris Lattner wrote:

Sorry if this is obvious.
I'm trying to link LLVM-generated code with LLVM itself, and don't know how to do it. Just as a little test, I've tried compiling the example HowToUseJIT.cpp using llvm-g++, but I get tons of errors.

Is there an easy way of doing this? Has anyone tried it before?

What sort of errors do you get? Are you passing the right -I options?

Sorry, I didn't mean I had problems compiling HowToUseJIT.cpp with llvm-g++. Using the right -I options, I get a correct HowToUseJIT.o bytecode file. The problem is when I try to link it to LLVM to produce a executable.

To try things out, I created a project based on the sample and included the HowToUseJIT code. This compiles perfectly. Next I tried changing the CXX and CC variables in the makefile to use llvm-g++. This got me

gccld: Unknown command line argument '--rpath'. Try: 'gccld --help'

Then I tried the last command produced by the makefile without the --rpath option, but the makefile always removes the produced .o in .libs, even when RM is set to 'touch'. I haven't found any way to keep the intermediate files, by the way.

Next thing, doing it all by hand, using the same command line as produced by the makefile but removing --rpath and using HowToUseJIT.cpp as input instead of the .o file, gets me

gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMInterpreter.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMJIT.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMCodeGen.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMExecutionEngine.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMX86.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMSelectionDAG.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMScalarOpts.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMBCReader.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMCore.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/build/Debug/lib/LLVMbzip2.o' ignored.
gccld: warning: File of unknown type '/home/marc/code/llvm/tests/sample/Debug/bin' ignored.

and the linker goes into an infinite loop, never returning to the command line.

Finally, I tried first generating the .o (bytecode) file, and then linking got me the same result.

So, I'm really stuck here. Is there a way of linking LLVM-produced code with LLVM? Has anyone tried it before?

Thanks,

Marc Ordinas i Llopis | Tragnarion Studios

I guess I don't really understand what your goal is. Currently our support for transparently mixing native and llvm .o files (what you're trying to do) is pretty bad.

If you really want to do this (and I'm still not sure at all why), you would want to compile one part of the program (either the part compiled with llvm-g++ or the part compiled with g++) to a native .so file, then compile the other part to a native application that links to it. You're not going to have much luck linking mixed bytecode/native .o files.

Sorry!

-Chris

Chris Lattner wrote:

Finally, I tried first generating the .o (bytecode) file, and then linking got me the same result.

So, I'm really stuck here. Is there a way of linking LLVM-produced code with LLVM? Has anyone tried it before?

I guess I don't really understand what your goal is.

The goal is, simply, to be able to generate code (with the JIT) that uses LLVM to generate more code. Most of today's VM runtimes include ways of creating code at run time (.NET's reflection package, for example).

Currently our support for transparently mixing native and llvm .o files (what you're trying to do) is pretty bad.

If you really want to do this (and I'm still not sure at all why), you would want to compile one part of the program (either the part compiled with llvm-g++ or the part compiled with g++) to a native .so file, then compile the other part to a native application that links to it. You're not going to have much luck linking mixed bytecode/native .o files.

I got the JIT libraries compiled inside a native .so and tried to run HowToUseJIT.bc, but it didn't work. Then, from the output I realized I had been looking at the problem in the wrong way. It's not possible to load the JIT from JITted code because there's already a JITter working. Loading another one messes up the passes registry and the targets.

I think a way of doing this would be to make a version of lli that somehow passes the execution engine handle into the JITted module. I'll try this and tell you how it went.

Regards,

Marc Ordinas i Llopis | Tragnarion Studios

PS: By the way, I found what I think are two glitches in the Makefiles. First, LDFLAGS must be set before including Makefile.common to have any effect, instead of after as the docs say. And second, USEDLIBS and LIBS are ignored when building shared libraries.