Calling functions defined in .o files in LLVM code executed in lli

Hi,

This is probably an easy question, but I could not find the answer to it.

I'm working on a LLVM back-end for the EHC compiler (a Haskell
compiler). Currently there is a C back-end and I would like to reuse the
primitive library functions from this back-end.

So I have compiled C sources in a .o file and I would like to call these
code and link the bitcode file and the .o file with llvm-ld it works
only if I generate native code. If I omit the -native flag to llvm-ld
and execute the generated shell script a.out I get the following error:

glimi@brodie:~$ llvm-ld ../../prim.o TestExist.bc && ./a.out

ERROR: Program used external function 'primPatternMatchFailure' which
could not be resolved!
lli((anonymous namespace)::PrintStackTrace()+0x15)[0x852c55]
lli((anonymous namespace)::SignalHandler(int)+0x216)[0x852fe6]
/lib/libc.so.6[0x2ada87ac87d0]
/lib/libc.so.6(gsignal+0x35)[0x2ada87ac8765]
/lib/libc.so.6(abort+0x110)[0x2ada87aca1c0]
lli(llvm::JIT::getPointerToNamedFunction(std::basic_string<char,
std::char_traits<char>, std::allocator<char> > const&)+0xde)[0x5c473e]
lli(llvm::JIT::getPointerToFunction(llvm::Function*)+0xf3)[0x5c5183]
lli((anonymous
namespace)::JITResolver::getFunctionStub(llvm::Function*)+0x27f)[0x5c7c6f]
lli((anonymous
namespace)::JITEmitter::getPointerToGlobal(llvm::GlobalValue*, void*,
bool)+0x192)[0x5c7f02]
lli((anonymous
namespace)::JITEmitter::finishFunction(llvm::MachineFunction&)+0x38d)[0x5c82bd]
lli[0x4af284]
lli(llvm::MachineFunctionPass::runOnFunction(llvm::Function&)+0x2c)[0x4f9bbc]
lli(llvm::FPPassManager::runOnFunction(llvm::Function&)+0x1ef)[0x805eef]
lli(llvm::FunctionPassManagerImpl::run(llvm::Function&)+0x72)[0x8063d2]
lli(llvm::FunctionPassManager::run(llvm::Function&)+0x3e)[0x80651e]
lli(llvm::JIT::runJITOnFunction(llvm::Function*)+0x48)[0x5c4848]
lli(llvm::JIT::getPointerToFunction(llvm::Function*)+0x159)[0x5c51e9]
lli((anonymous
namespace)::JITResolver::JITCompilerFn(void*)+0x2df)[0x5c774f]
lli(X86CompilationCallback2+0x43)[0x4ec053]
lli[0x4ebfaa]
Aborted (core dumped)

Is there a way to link the functions declared in the .o file in the
bitcode without compiling the C sources with llvm-gcc?

Regards,

John van Schie

Hi,

This is probably an easy question, but I could not find the answer to it.

I'm working on a LLVM back-end for the EHC compiler (a Haskell
compiler). Currently there is a C back-end and I would like to reuse the
primitive library functions from this back-end.

Ok.

So I have compiled C sources in a .o file and I would like to call these
from my LLVM code. If I declare these primitive functions in the LLVM
code and link the bitcode file and the .o file with llvm-ld it works
only if I generate native code. If I omit the -native flag to llvm-ld
and execute the generated shell script a.out I get the following error:

glimi@brodie:~$ llvm-ld ../../prim.o TestExist.bc && ./a.out

This fails because "a.out" is a shell script that is invoking the JIT.

Is there a way to link the functions declared in the .o file in the
bitcode without compiling the C sources with llvm-gcc?

Yes, certainly. If you want the JIT to work, you need to link your .o files together into a .so/.dylib file, then use the 'lli -load foo.dylib' option to load it. If you want to natively link your application together, just toss your .o files on the native link line.

-Chris

Assuming the run-time library is in C, is there a reason why you cannot also compile that to LLVM? You could get significantly better optimization via LTO that way.

--Vikram
http://www.cs.uiuc.edu/~vadve
http://llvm.org

Chris Lattner wrote:

Yes, certainly. If you want the JIT to work, you need to link your .o files together into a .so/.dylib file, then use the 'lli -load foo.dylib' option to load it. If you want to natively link your application together, just toss your .o files on the native link line.

Thanks! I was a bit confused by the --help description of the load option. I guessed that it was an option to load an optimization that could be applied by the JIT compiler.

-- John

Vikram S. Adve wrote:

Assuming the run-time library is in C, is there a reason why you cannot also compile that to LLVM? You could get significantly better optimization via LTO that way.

--Vikram
Vikram S. Adve
http://llvm.org
  

Vikram,

Eventually I am aiming to do just this, but this means that the build process will become depended on llvm-gcc (or I have to smarten up the build process). The other people working on EHC do not have llvm-gcc on their machines, so I'll have to discuss it first before I break the build.
Thanks for your remark!

-- John