tags:

views:

1121

answers:

4

Hello,

I am new with LLVM and i just played around with it for a couple of days.

I generated a bc file with the online compiler on llvm.org and i would like to know if it was possible to load this bc file from a c or c++ program, execute the IR in the bc file with the llvm jit (programmatically in the c program) and get the result back.

Is it possible ? and how ?

Thanks you !

A: 

From the command line, you can use the LLVM program lli to run a bc file. If the file is in LLVM assembly language, you'll have to run llvm-as on it first to create a binary bitcode file.

It is easy to do this from C. I'd recommend you look at the extensive LLVM documentation: http://llvm.org/docs

The LLVM irc channel, which has a link on that page, is full of very knowledgeable people that are willing to answer questions.

Sorry for the indirect answer. I use LLVM extensively, but I do direct code generation not just in time compliation.

Richard Pennington
+2  A: 

This should (more or less) work using LLVM 2.6. It looks like there are some more helper functions in SVN to create a lazy ModuleProvider on top of a bitcode file. I haven't tried compiling it though, just glued together some bits from one of my JIT applications.

#include <string>
#include <memory>

#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ModuleProvider.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/JIT.h>

using namespace std;
using namespace llvm;

int main()
{
    InitializeNativeTarget();
    llvm_start_multithreaded();
    LLVMContext context;

    string error;
    auto_ptr<MemoryBuffer> buffer(MemoryBuffer::getFile("bitcode.bc"));
    auto_ptr<Module> module(ParseBitcodeFile(buffer.get(), context, &error));
    auto_ptr<ModuleProvider> mp(new ExistingModuleProvider(module));
    module.release();

    auto_ptr<ExecutionEngine> ee(ExecutionEngine::createJIT(mp.get(), &error));
    mp.release();

    Function* func = ee->getFunction("foo");

    typedef void (*PFN)();
    PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
    pfn();
}
Nathan Howell
+2  A: 

Here's some working code based on Nathan Howell's:

#include <string>
#include <memory>
#include <iostream>

#include <llvm/LLVMContext.h>
#include <llvm/Target/TargetSelect.h>
#include <llvm/Bitcode/ReaderWriter.h>
#include <llvm/ExecutionEngine/ExecutionEngine.h>
#include <llvm/ModuleProvider.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/ExecutionEngine/JIT.h>

using namespace std;
using namespace llvm;

int main()
{
    InitializeNativeTarget();
    llvm_start_multithreaded();
    LLVMContext context;
    string error;
    Module *m = ParseBitcodeFile(MemoryBuffer::getFile("tst.bc"), context, &error);
    ExecutionEngine *ee = ExecutionEngine::create(m);

    Function* func = ee->FindFunctionNamed("main");

    typedef void (*PFN)();
    PFN pfn = reinterpret_cast<PFN>(ee->getPointerToFunction(func));
    pfn();
    delete ee;
}

One oddity was that without the final include, ee is NULL. Bizarre.

To generate my tst.bc, I used http://llvm.org/demo/index.cgi and the llvm-as command-line tool.

Sacha Varma
Doh, #including <JIT.h> is required to force the linker to pull in the JIT, otherwise it would be discarded. I'll update my sample.
Nathan Howell
Is there c api to do this?
Ariel
A: 

Could someone post Makefile's source to compile example posted above? I've a problem with the linker when compiling that code, i don't know what to link...

Void-995