[LLVMdev] generating ELF shared object, C code, from a module like Fibionnaci

Reid Spencer rspencer at reidspencer.com
Wed May 16 10:19:21 PDT 2007

On Wed, 2007-05-16 at 15:09 +0200, Basile STARYNKEVITCH wrote:
> Hello
> Apparently the file llvm/examples/Fibonacci/fibonacci.cpp generate JIT code
> without explicitly doing any pass (but I suppose that internally, compiler
> passes are running!) 

Very few. There are passes that run in order to do the code generation.
But, none of the optimization passes are run. 

> - is there a dissymetry between JIT machine code
> generation in memory, and C or ELF shared object generation in files? 

Generation of native assembly, ELF and JIT are very similar except for
the final emission stage where the code is actually produced. The C
Backend is completely different, it shares nothing with the other code

> I
> thought that all these shared a lot of LLVM infrastructure and happen
> similarily!

They do, but the optimization passes are at a level above code
generation. They operate on the generic mid-level IR. Consequently code
generated by any back end can have the same optimizations applied.

The only thing missing is that fibonacci example just isn't doing the
optimization part. It could be enhanced to do that, however.
> Also, the llvm/tools/llc/llcp.ccp file explicitly need passes and a pass
> manager to generate .so and .c files

That's right.

> I would like to understand precisely how to patch the fibionnaci example to
> generate an ELF shared object or a C file, in addition of the JIT-ed code in
> memory. May it should become a FAQ.

I suggest you study llc and lib/CodeGen and lib/Target more carefully.
AFAIK the ELF writer is not working on all platforms (I think it only
works on one) and its being enhanced contemporaneously.

> I also don't understand what is the LLVM meaning of inter-module linking...
> To suggest an example, one could add code to fibionacci which load a bitcode
> from a file, JIT it, and uses a printing routine there to print fib(10).

Code in LLVM is bundled into a "Module" concept (somewhat like a
translation unit). inter-module linking is simply linking together those
modules (i.e. multiple bitcode files).

> Actually, I miss a document or wiki explaining how to use (without extending
> it) LLVM to generate code (in variety of ways, including C, ELF shared
> object, JIT-ed code in memory) from within an application. Just a simple
> example like fibionnaci is great!
> My dream would be some tutorial documentation with working actual code
> snippets... in the spirit of e.g.
> http://www.gnu.org/software/lightning/manual/html_node/Fibonacci.html which
> documents an example similar to llvm/examples/Fibonacci/fibonacci.cpp I
> would like the same for LLVM, emitting JIT code, ELF shared library, C
> file.... Maybe such a documentation exist (hopefully up to date for
> LLVM2.0!) but I did not found it yet! Perhaps just linking the example
> source files from the web site could be a good idea...

I don't think such a tutorial exists.

> The doxygen documentation is very good, but does not separate enough what an
> LLVM user (some developer using LLVM in his own program) should see and what
> an LLVM developer (someone enhancing LLVM) should in addition know.


> I find LLVM great, but quite hard to grasp.

There is extensive documentation at http://llvm.org/docs/


> Regards.

More information about the llvm-dev mailing list