Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mono LLVM and LLVM-IR

Tags:

c#

mono

llvm

I am playing with Mono LLVM (http://www.mono-project.com/Mono:Runtime:Documentation:LLVM) and it seems like they are using LLVM here as a JIT, not really as a code generator. But according to the docs, they go through LLVM-IR before generating native code :

  1. first, normal mono JIT IR is generated from the IL code
  2. the IR is transformed to SSA form
  3. the IR is converted to the LLVM IR
  4. the LLVM IR is compiled by LLVM into native code

So, I am wondering if there any way to catch this 3rd phase and use the IR to feed my LLVM backend. That would allow me to get C# to LLVM-IR front end for free.

Thanks for any help!

like image 672
Thibault Imbert Avatar asked Jun 15 '11 06:06

Thibault Imbert


People also ask

What is LLVM IR?

LLVM IR is a low-level intermediate representation used by the LLVM compiler framework. You can think of LLVM IR as a platform-independent assembly language with an infinite number of function local registers.

Does Mono use LLVM?

Mono has an option to use LLVM for JIT compilation.

Is LLVM backwards compatible?

The rules are intended as a balance between convenience for llvm users and not imposing a big burden on llvm developers: The textual format is not backwards compatible.

Is LLVM IR portable?

LLVM is designed around a language-independent intermediate representation (IR) that serves as a portable, high-level assembly language that can be optimized with a variety of transformations over multiple passes.


1 Answers

Mono's LLVM support works by attempting to load libmono-llvm.so from the appropriate $libdir, e.g. on OSX /usr/bin/mono will load /Library/Frameworks/Mono.framework/Libraries/libmono-llvm.dylib. See the LLVM loading code in mono/mini/mini-llvm.h, specifically mono_llvm_load() and try_llvm_load().

So it looks like if you name your LLVM backend mono-llvm, it will be used when you launch mono as mono --llvm. That said, mono_llvm_load() appears to require particular exports from the mono-llvm library, such as mono_llvm_init and mono_llvm_cleanup, so I'm sure that some additional work will be needed to your LLVM backend in order to allow Mono to use it, and I'm not sure where to find this out (other than grepping for "LLVM" in Mono's sources...).

This also means that the LLVM backend may not be reusable from a prebuilt mono; that is, you may need to build your own mono in order to use your LLVM backend, as a prebuild mono will be providing a mono-llvm backend that may not be pluggable. (I have no idea how pluggable or not pluggable the LLVM backend is.)

like image 53
jonp Avatar answered Sep 28 '22 05:09

jonp