Two ExecutionEngines from one Module

Hello everybody,

I'm pretty new to LLVM programming, so if this answer is pretty simple,
I apologize in advance. We are working with the released LLVM 1.9.

Say you have a class called CompiledProgram which is designed to contain
our Module and (potentially) our ExecutionEngine(s). In particular, I
would like to have an ExecutionEngine for running our program natively
and another for running via the interpreter. It is the ability to have
both flavors of execution that is causing me grief. Given a Module
pointer, module, here's the scenario:

  CompiledProgram* program = new CompiledProgram(module);

Inside our CompiledProgram object, we might want to do this:

  this->interpretedEE =
llvm::ExecutionEngine::create(this->module,true);

Ownership of the module will transfer to the interpretedEE at this
point. Fair enough because we've got this all buttoned up in a class
and we can handle the various orders of takedown.

Later, I'd like to make a native one:

  this->nativeEE = llvm::ExecutionEngine::create(???,false);

and at this point I'm in a seeming catch-22. I can't use the same
module (I believe) to construct the second ExecutionEngine since
ownership of the module is already in the first one. Zooming around the
class hierarchy didn't seem to yield a good way to do a deep copy on a
Module, which I could have done before constructing any given
ExecutionEngine and thus sidestep the ownership issue. There doesn't
seem to be a way to release a module from an ExecutionEngine once it's
there, which would also give me a solution.

I figure it's time to ask and say hi to my fellow LLVMers. Do you have
any advice on how to handle this situation?

Thanks,
Chuck.

and at this point I'm in a seeming catch-22. I can't use the same
module (I believe) to construct the second ExecutionEngine since
ownership of the module is already in the first one.

Right, the LLVM ExecutionEngine stuff has been designed with the assumption you'd only need one :slight_smile:

Zooming around the
class hierarchy didn't seem to yield a good way to do a deep copy on a
Module, which I could have done before constructing any given
ExecutionEngine and thus sidestep the ownership issue.

If copying the module is acceptable, check out llvm/Transforms/Utils/Cloning.h

CloneModule(M);

Which does a deep copy of the module.

-Chris