[llvm-dev] LLVMContext: Threads and Ownership.

Lang Hames via llvm-dev llvm-dev at lists.llvm.org
Sat Sep 15 21:30:03 PDT 2018


Actually, looking at the destructors for LLVMContext and Module I do not
think the current ownership scheme makes sense, so this might be a good
opportunity to re-think it.

Right now an LLVMContext owns a list of modules (see
LLVMContextImpl::OwnedModules) that it destroys when its destructor is
called.  Modules remove themselves from this list if they are destructed
before the context:

Module::~Module() {
  Context.removeModule(this);
  ...

LLVMContextImpl::~LLVMContextImpl() {
  // NOTE: We need to delete the contents of OwnedModules, but Module's dtor
  // will call LLVMContextImpl::removeModule, thus invalidating iterators
into
  // the container. Avoid iterators during this operation:
  while (!OwnedModules.empty())
    delete *OwnedModules.begin();
    ...

This makes it unsafe to hold a unique_ptr to a Module: If any Module is
still alive when its context goes out of scope it will be double freed,
first by the LLVMContextImpl destructor and then again by the unique ptr.
Idiomatic scoping means that we tend not to see this in practice  (Module
takes an LLVMContext reference, meaning we always declare the context
first, so it goes out of scope last), but makes the context ownership
redundant: the modules are always freed first via their unique_ptr's.

I don't think it makes sense for LLVMContext to own Modules. I think that
Modules should share ownership of their LLVMContext via a shared_ptr.

Thoughts?

Cheers,
Lang.

On Sat, Sep 15, 2018 at 4:14 PM Lang Hames <lhames at gmail.com> wrote:

> Hi All,
>
> ORC's new concurrent compilation model generates some interesting lifetime
> and thread safety questions around LLVMContext: We need multiple
> LLVMContexts (one per module in the simplest case, but at least one per
> thread), and the lifetime of each context depends on the execution path of
> the JIT'd code. We would like to deallocate contexts once all modules
> associated with them have been compiled, but there is no safe or easy way
> to check that condition at the moment as LLVMContext does not expose how
> many modules are associated with it.
>
> One way to fix this would be to add a mutex to LLVMContext, and expose
> this and the module count. Then in the IR-compiling layer of the JIT we
> could have something like:
>
> // Compile finished, time to deallocate the module.
> // Explicit deletes used for clarity, we would use unique_ptrs in practice.
> auto &Ctx = Mod->getContext();
> delete Mod;
> std::lock_guard<std::mutex> Lock(Ctx->getMutex());
> if (Ctx.getNumModules())
>   delete Ctx;
>
> Another option would be to invert the ownership model and say that each
> Module shares ownership of its LLVMContext. That way LLVMContexts would be
> automatically deallocated when the last module using them is destructed
> (providing no other shared_ptrs to the context are held elsewhere).
>
> There are other possible approaches (e.g. side tables for the mutex and
> module count) but before I spent too much time on it I wanted to see
> whether anyone else has encountered these issues or has opinions on
> solutions.
>
> Cheers,
> Lang.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180915/c52a91cd/attachment.html>


More information about the llvm-dev mailing list