[LLVMdev] llvm jit acting at runtime, like libgccjit ?

135 views
Skip to first unread message

deco...@yandex.com

unread,
Jul 9, 2015, 9:51:53 AM7/9/15
to llv...@cs.uiuc.edu
Hello,

i mainly code in c/c++ on linux.

I would like to know if I can generate code at runtime using llvm, like libgccjit -> https://gcc.gnu.org/onlinedocs/jit/


My needs are : create functions, conditions, loops at runtime. libgccjit does the job pretty well but I would like to test llvm too (licence issue).

Is it possible ?
Is there a resource that helps in that regard ? Kaleidoscope doesn't seems to be what I expect.


Thanks
Bye


-- 
Jog

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

James Molloy

unread,
Jul 9, 2015, 10:49:22 AM7/9/15
to deco...@yandex.com, llv...@cs.uiuc.edu
Hi,

Yes, LLVM can do this and has been able to long before libgccjit. Kaleidoscope gives an introduction - what exactly is it about Kaleidoscope that differs from your expectations?

Cheers,

James

deco...@yandex.com

unread,
Jul 9, 2015, 10:50:53 AM7/9/15
to James Molloy, llv...@cs.uiuc.edu
Thanks James,
 
Kaleidoscope seems to differ in the sense that I cannot really understand how to create, say, a loop. It all looks like very complicated (http://llvm.org/docs/tutorial/LangImpl3.html)
 
llvm is new to me.
 
it is pretty basic, but the tutorial page of the libgccjit is very helpful in that regard (how to create a function, a condition, a loop..)
 
I will keep doing my homework, but a bit of help is very welcome :)
 
-- 
Jog
 

James Molloy

unread,
Jul 9, 2015, 10:59:23 AM7/9/15
to deco...@yandex.com, llv...@cs.uiuc.edu
Hi,

Once you have a Module and Function created, which Kaleidoscope can show you how to do, the important thing is to understand what the LLVM IR you want to create will look like. You can do this by writing a trivial function with a loop in Clang and running:

clang -O0 -emit-llvm -S -o - my-trivial-program.c

Clang, like any frontend, produces deliberately poor code and expects LLVM to clean it up - if you want more cleaned up code use -O2.

The IR reference is here: http://llvm.org/docs/LangRef.html
The IRBuilder reference, which is the object you use to build up some IR programatically, is here: http://llvm.org/docs/doxygen/html/classllvm_1_1IRBuilder.html (and http://llvm.org/docs/doxygen/html/classllvm_1_1IRBuilderBase.html )

To create a simple loop you will need to create at least two blocks: the loop header, and loop body. The header will perform your exit test and conditionally exit the loop. The body will do all your work and then increment/decrement the loop counter.

Best of luck,

James

Dibyendu Majumdar

unread,
Jul 9, 2015, 11:30:49 AM7/9/15
to James Molloy, llv...@cs.uiuc.edu
On 9 July 2015 at 15:53, James Molloy <ja...@jamesmolloy.co.uk> wrote:

> You can do this by writing a trivial function with a
> loop in Clang and running:
>
> clang -O0 -emit-llvm -S -o - my-trivial-program.c
>
> Clang, like any frontend, produces deliberately poor code and expects LLVM
> to clean it up - if you want more cleaned up code use -O2.

I find it is more useful to use this:

clang -cc1 -O1 -disable-llvm-optzns -S -emit-llvm prog.c


-O0 produces some extra rubbish that is just noise.
-O1 and above can get rid of the code altogether - so using the
disable optimizations flag appears to help.


Regards
Dibyendu
Reply all
Reply to author
Forward
0 new messages