[LLVMdev] Suggestion for VM porting to LLVM

0 views
Skip to first unread message

Gabriele Farina

unread,
Apr 4, 2009, 4:20:21 AM4/4/09
to LLVM Developers Mailing List
Hi guys,

first of all let me say "hello" to everyone. Its the firt message for
me in this list and I'm pretty happy that I'll be able to finally
start working with LLVM.

I've a question for you all if you can help me: a few years ago I've
implemented a simple VM for a language we're using in my company. The
VM JIT compiles a simple bytecode and executes it. It works quite
well, better then some well know VMs. My idea now is to try to migrate
the VM to LLVM to see if I can gain something in terms of speed and
extensibility.
I'm totally new to LLVM, but as far as I understood thare might be two
ways to add LLVM to my project:

- generate IR from my complier;
- build a VM that converts at runtime my bytecode to IR and executes it;

I largely prefer the second option because it would be great if I can
continue to use to old programs without having to recompile them.

Which approach do you suggest ? are there any alternatives ?

Gabriele
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Mike Stump

unread,
Apr 4, 2009, 1:23:23 PM4/4/09
to LLVM Developers Mailing List
On Apr 4, 2009, at 1:20 AM, Gabriele Farina wrote:
> Which approach do you suggest ? are there any alternatives ?

My take, do the second one first, gain some experience and have some
fun. You can use this to double check the performance and
suitability. But mid-term, I'd say, do both. This allows you to
compare the performance of each solution against the other, compile
time, compile time memory pressure, run time and run time memory
usage. You're then in a better position to decide which path suits
what usage styles you have. Long term, you can then see if it makes
sense to trim a solution out.

Jon Harrop

unread,
Apr 4, 2009, 1:46:14 PM4/4/09
to LLVM Developers Mailing List
On Saturday 04 April 2009 09:20:21 Gabriele Farina wrote:
> - generate IR from my complier;
> - build a VM that converts at runtime my bytecode to IR and executes it;
>
> I largely prefer the second option because it would be great if I can
> continue to use to old programs without having to recompile them.
>
> Which approach do you suggest ? are there any alternatives ?

I've done something similar with HLVM, albeit for a new VM that uses a new
representation. I found the combination of OCaml and LLVM to work extremely
well. OCaml makes it very easy to manipulate programs and its LLVM bindings
make it very easy to JIT compile and execute native code. My entire VM
(including GC) is only 1kLOC.

--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e

Gabriele Farina

unread,
Apr 5, 2009, 3:34:21 AM4/5/09
to LLVM Developers Mailing List
Hi Mike,

thanks for the suggestion. Do you know if there are any articles
around that explains how to use LLVM to build a VM that should work
like mine ?
I've read something around (mostly source code) but a good article/doc
will be perfect

Gabriele

Il giorno 04/apr/09, alle ore 19:23, Mike Stump ha scritto:

Gabriele Farina

unread,
Apr 5, 2009, 3:31:37 AM4/5/09
to LLVM Developers Mailing List
Hi Jon,

I've read your articles about HLVM, and that was one of the reasons
that convinced me to try out LLVM. Actually my VM has been implemented
in C++, but as long as it is not extremely complex, I might port it to
OCaml that sounds more compact for this kind of programs. Did you find
any significant performance loss when using OCaml over C++ ? Runtime
performance is quite important in my situation.

Gabriele


Il giorno 04/apr/09, alle ore 19:46, Jon Harrop ha scritto:

Jon Harrop

unread,
Apr 5, 2009, 3:53:38 AM4/5/09
to LLVM Developers Mailing List
On Sunday 05 April 2009 08:34:21 Gabriele Farina wrote:
> Hi Mike,
>
> thanks for the suggestion. Do you know if there are any articles
> around that explains how to use LLVM to build a VM that should work
> like mine ?
> I've read something around (mostly source code) but a good article/doc
> will be perfect

There are a series of articles in the OCaml Journal describing the
construction of a VM using LLVM:

http://ocamlnews.blogspot.com/2009/03/building-virtual-machine-with-llvm-part.html

The VM uses expression trees rather than bytecode. However, there is another
article in the same journal describing the construction of a bytecode
compiler using LLVM:

http://ocamlnews.blogspot.com/2008/09/writing-bytecode-compiler-using-llvm.html

--
Dr Jon Harrop, Flying Frog Consultancy Ltd.
http://www.ffconsultancy.com/?e

Jon Harrop

unread,
Apr 5, 2009, 3:56:31 AM4/5/09
to LLVM Developers Mailing List
On Sunday 05 April 2009 08:31:37 Gabriele Farina wrote:
> Hi Jon,
>
> I've read your articles about HLVM, and that was one of the reasons
> that convinced me to try out LLVM. Actually my VM has been implemented
> in C++, but as long as it is not extremely complex, I might port it to
> OCaml that sounds more compact for this kind of programs. Did you find
> any significant performance loss when using OCaml over C++ ? Runtime
> performance is quite important in my situation.

I have not benchmarked it because I have no C++ alternative but OCaml is
generally several times faster than C++ at symbolic processing (like
compilers).

The main disadvantage of using OCaml is that the bindings are incomplete.
However, they are almost complete and you can easily augment them with
anything that you need. See the "llvm.cpp" and "llvm_stubs.c" files in HLVM,
for example. I also had to work around some bugs in the LLVM bindings when
building HLVM but I'll happily talk you through it.

I'm sure you'll have something suitably spectacular in a short time. :-)

Anton Korobeynikov

unread,
Apr 5, 2009, 5:36:21 AM4/5/09
to LLVM Developers Mailing List
Hello, Gabriele

> thanks for the suggestion. Do you know if there are any articles
> around that explains how to use LLVM to build a VM that should work
> like mine ?
> I've read something around (mostly source code) but a good article/doc
> will be perfect

Have you read the 'Kaleidoscope' tutorial?

--
With best regards, Anton Korobeynikov
Faculty of Mathematics and Mechanics, Saint Petersburg State University

Gabriele Farina

unread,
Apr 5, 2009, 6:15:08 AM4/5/09
to LLVM Developers Mailing List
Hi,

Isn't it intended to explain how to build a compiler for a custom
language that targets LLVM-IR ? Is it useful also to understand how to
build a VM that is meant to execute custom bytecode (converting it to
LLVM-IR previously) ?

Thanks,
Gabriele

Il giorno 05/apr/09, alle ore 11:36, Anton Korobeynikov ha scritto:

someguy

unread,
Apr 7, 2009, 2:22:44 AM4/7/09
to LLVM Developers Mailing List
Gabrielle,

The way I see it, its pretty much the same thing... Conversion to LLVM-IR of a custom bytecode is the same as conversion to LLVM-IR of a custom language.

The syntax of the 'custom language' just happens to be binary bytecode.

Gabriele Farina

unread,
Apr 7, 2009, 3:47:35 PM4/7/09
to LLVM Developers Mailing List
Well, you are right :)

In fact I've started porting the VM in the spare time and it is working fine. I'm still having some issues to understand the garbage collector, but I'll delve more into it as soon as the other features will be complete

Gabriele

Il giorno 07/apr/09, alle ore 08:22, someguy ha scritto:

someguy

unread,
Apr 8, 2009, 5:42:04 AM4/8/09
to LLVM Developers Mailing List
Good luck.

Be sure to document anything you think might be useful on the wiki!

Reply all
Reply to author
Forward
0 new messages