`PassRegistry` corruption with multiple users


I have a scenario as follows: my main application uses LLVM to do some codegen/JIT execution using MCJIT. Everything works fine currently, linking against libLLVM.so. However, now I want to add some functionality that depends on another lib2.so dynamic library. This new library also uses LLVM to do some of its own codegening, which is orthogonal to my application’s needs. I load lib2.so at runtime as a plugin (using dlopen()).

However, occasionally after loading lib2.so my application crashes during LLVM codegen. I debugged the issue and figured the reason is likely the global PassRegistry being corrupted due to the concurrent uses (the crash I get is related to a pass not being initialized, even though using a PassRegistrationListener I can see that it was initialized/scheduled earlier normally).

I saw on the documentation that the PassRegistry is indeed not thread-safe. However, I could not find any documentation/examples on how to keep local instances of a PassRegistry and how to have my ExecutionEngine use this separate instance to avoid these concurrent access issues. Any tips/guidelines on best practice to handle this scenario?

Thanks in advance for any help!