Trouble with ORCv2 Tutorial

Hi folks,

I'm working on migrating a JIT compiler from the old ORCv1 JIT APIs to
the newer ORCv2 ones and am having some trouble getting the code from
chapter 1 of the "Building a JIT" [1] tutorial working properly. I
have previously walked through the "My First Langauge" [2] tutorial
and that went smoothly using the provided JIT class, but using the one
from the JIT tutorial is giving me trouble.

Kaleidoscope builds fine using Clang and LLVM version 9, but fails on
an expression like this:

fun foo(c) c;

Read function definition:
define double @foo(double %c) {
  ret double %c

kaleidoscope: kaleidoscope_baseline.cpp:980: void HandleDefinition():
Assertion `add_q && "HandleDefinition: Error adding a module."'

I'm using the exact source code from the tutorials to reproduce the
problem with some tiny changes to the front end to deal with some
slight API changes introduced in the JIT tutorial where some functions
return Expected. For example, the specific place that this is failing:

            fprintf(stderr, "\n");
- TheJIT->addModule(std::move(TheModule));
+ auto add_q = TheJIT->addModule(std::move(TheModule));
+ assert(add_q && "HandleDefinition: Error adding a module.");

I've looked at a previous thread discussing moving from ORCv1 to ORCv2
[3] and watched the "Updating ORC JIT for Concurrency” talk by L.
Hames & B. Loggins, but am still having a little trouble getting my
head around everything without walking through a complete tutorial.
I'd appreciate any guidance that you folks have on the migration as
well as some help with this specific issue. I'm happy to share more
about the source code and build settings if that is helpful.

Thanks in advance,



Hi Zeke,
Bool conversion of Error returns True for Failure States and False for Success States.

assert(add_q && “HandleDefinition: Error adding a module.”); - You’re essentially checking against success state to assert the condition. Plus, Error have many handling APIs dealing with success and failure states, you can use them instead of assert.
That will trigger a runtime error if Error is not handled correctly with the reason.

Try this:

+assert(!add_q && “Handle Definition : Error Adding a Module”);

Please let me know, if you ran any more problems with ORCV2.


Hi Praveen,

Thanks for pointing that out :slight_smile: That's my mistake.

I might have been misidentifying the issue with the assert statement.
With those fixes made function calls seem to all call the first
function called in the REPL. For example:

def fib(n) if (n < 2) then n else fib(n - 1) + fib(n - 2);



Evaluated to 102334155.000000

fib(10); # This should not have the same result as the earlier call.

Evaluated to 102334155.000000

Both of those calls evaluate fib(40) despite the second one being a
call to fib(10). The same goes for further calls to functions. If I
subsequently define another function in the same repl session and call
it, it uses the results of the first function call in that repl

def bar(x) x + 1;



Evaluated to 102334155.000000

I suspect that this has to do with the fact that in ORCv2 we can no
longer remove modules meaning that the code in
HandleTopLevelExpression can't wrap up by removing the most recent
module it added. The tutorial does lookups by creating a function with
no arguments names "__anon_expr", calling it, then removing it AFIK
and because ORCv2 doesn't allow for the removal part as of yet, my
hunch is that the issue is there.

Any thoughts from anyone?

If this does end up being an issue with how the tutorial looks right
now and not me making a mistake I'll happily volunteer to update the
tutorials as needed :slight_smile:


Hi Zeke,

Thanks for pointing this out.

You’re right: the issue is that ORCv2 doesn’t support code removal (yet). To work around that we need to rename the anonymous expression each time (e.g. __anon_expr.1, __anon_expr.2, …). Sounds like we’re not doing that at the moment. I’ll try to get it fixed up shortly.


HI Lang,

Thanks a bunch for the reply. That worked great for on my end and is a
pretty tiny change.

Really appreciate all the work you've done on this new JIT stuff and
putting together those great tutorials.


Hi Zeke,

Thanks a bunch for the reply. That worked great for on my end and is a
pretty tiny change.

Great to hear! Since you’ve already done the hard work, would you be willing to share your patch? I can review and apply it to the mainline.


Hi Lang,

I'd love to. I'll put that together this weekend :slight_smile: