[LLVMdev] Function calls only being JIT'd once by Kaleidoscope with MCJIT?

Charlie Turner charlesturner7c5 at gmail.com
Fri Dec 26 09:09:36 PST 2014

Hi all,

Starting from Chapter 4 of the Kaleidoscope tutorial (where the JIT
support is added), there's some strange behaviour,

ready> def foo(x y) x+y;
ready> Read function definition:
define double @foo(double %x, double %y) {
  %addtmp = fadd double %x, %y
  ret double %addtmp

ready> foo(1, 2);
ready> Evaluated to 3.000000
ready> foo(3, 4);
ready> Evaluated to 3.000000

You can see that foo(3, 4) is not being computed correctly. Well
actually, it appears to not be getting compiled. It seems like the
Kaleidoscope tutorial has slipped a bit with the recent JIT changes in

I have attached a patch which gets the above working. I found a blog
post about getting Kaleidoscope working with MCJIT
which most of the code comes from. Unfortunately that didn't
completely work, it was using a deprecated API
"RTDyldMemoryManager::getPointerToNamedFunction" which was failing to
find function symbols for reasons I didn't have time to track down. I
changed it to use the recommended getSymbolAddress, and now it works.

If the attached seems reasonable, there's more to do,

  * Updating the Kaleidoscope tutorial to explain all this. Andy
Kaylor's blog post [1] is good place to start. There are other methods
of using MCJIT in Kaleidoscope explained on that blog, but the
attached method is perhaps the most straightforward one.
  * Update all future chapter code to fix this bug.
  * Updating the Kaleidoscope/MCJIT example code. This code doesn't
get built when you request examples to be built, so it has suffered
bit rot (doesn't compile at the moment, I have local patches for this
but can submit those in the future). Should we be building this code
more regularly?
  * .... Bonus: Add unit tests to Kaleidoscope so it doesn't slip like
this in the future?

Am I on the right track with these fixes? If so I'll try and get to
the points above, or at least raise a PR for it. Maybe in the meantime
a note should be added to the tutorial that this is a known bug?

Thanks for your time!
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 0001-Function-calls-not-getting-compiled-in-Kaleidoscope-.patch
Type: text/x-diff
Size: 13378 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20141226/afefbe41/attachment.patch>

More information about the llvm-dev mailing list