1 //===----------- ThreadSafeModule.h -- Layer interfaces ---------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Thread safe wrappers and utilities for Module and LLVMContext.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULEWRAPPER_H
15 #define LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULEWRAPPER_H
17 #include "llvm/IR/LLVMContext.h"
18 #include "llvm/IR/Module.h"
19 #include "llvm/Support/Compiler.h"
28 /// An LLVMContext together with an associated mutex that can be used to lock
29 /// the context to prevent concurrent access by other threads.
30 class ThreadSafeContext {
33 State(std::unique_ptr<LLVMContext> Ctx) : Ctx(std::move(Ctx)) {}
35 std::unique_ptr<LLVMContext> Ctx;
36 std::recursive_mutex Mutex;
40 // RAII based lock for ThreadSafeContext.
41 class LLVM_NODISCARD Lock {
43 using UnderlyingLock = std::lock_guard<std::recursive_mutex>;
46 Lock(std::shared_ptr<State> S)
48 L(llvm::make_unique<UnderlyingLock>(this->S->Mutex)) {}
51 std::shared_ptr<State> S;
52 std::unique_ptr<UnderlyingLock> L;
55 /// Construct a null context.
56 ThreadSafeContext() = default;
58 /// Construct a ThreadSafeContext from the given LLVMContext.
59 ThreadSafeContext(std::unique_ptr<LLVMContext> NewCtx)
60 : S(std::make_shared<State>(std::move(NewCtx))) {
61 assert(S->Ctx != nullptr &&
62 "Can not construct a ThreadSafeContext from a nullptr");
65 /// Returns a pointer to the LLVMContext that was used to construct this
66 /// instance, or null if the instance was default constructed.
67 LLVMContext *getContext() { return S ? S->Ctx.get() : nullptr; }
69 /// Returns a pointer to the LLVMContext that was used to construct this
70 /// instance, or null if the instance was default constructed.
71 const LLVMContext *getContext() const { return S ? S->Ctx.get() : nullptr; }
74 assert(S && "Can not lock an empty ThreadSafeContext");
79 std::shared_ptr<State> S;
82 /// An LLVM Module together with a shared ThreadSafeContext.
83 class ThreadSafeModule {
85 /// Default construct a ThreadSafeModule. This results in a null module and
87 ThreadSafeModule() = default;
89 ThreadSafeModule(ThreadSafeModule &&Other) = default;
91 ThreadSafeModule &operator=(ThreadSafeModule &&Other) {
92 // We have to explicitly define this move operator to copy the fields in
93 // reverse order (i.e. module first) to ensure the dependencies are
94 // protected: The old module that is being overwritten must be destroyed
95 // *before* the context that it depends on.
96 // We also need to lock the context to make sure the module tear-down
97 // does not overlap any other work on the context.
99 auto L = getContextLock();
102 M = std::move(Other.M);
103 TSCtx = std::move(Other.TSCtx);
107 /// Construct a ThreadSafeModule from a unique_ptr<Module> and a
108 /// unique_ptr<LLVMContext>. This creates a new ThreadSafeContext from the
110 ThreadSafeModule(std::unique_ptr<Module> M, std::unique_ptr<LLVMContext> Ctx)
111 : M(std::move(M)), TSCtx(std::move(Ctx)) {}
113 /// Construct a ThreadSafeModule from a unique_ptr<Module> and an
114 /// existing ThreadSafeContext.
115 ThreadSafeModule(std::unique_ptr<Module> M, ThreadSafeContext TSCtx)
116 : M(std::move(M)), TSCtx(std::move(TSCtx)) {}
118 ~ThreadSafeModule() {
119 // We need to lock the context while we destruct the module.
121 auto L = getContextLock();
126 /// Get the module wrapped by this ThreadSafeModule.
127 Module *getModule() { return M.get(); }
129 /// Get the module wrapped by this ThreadSafeModule.
130 const Module *getModule() const { return M.get(); }
132 /// Take out a lock on the ThreadSafeContext for this module.
133 ThreadSafeContext::Lock getContextLock() { return TSCtx.getLock(); }
135 /// Boolean conversion: This ThreadSafeModule will evaluate to true if it
136 /// wraps a non-null module.
137 explicit operator bool() {
139 assert(TSCtx.getContext() &&
140 "Non-null module must have non-null context");
147 std::unique_ptr<Module> M;
148 ThreadSafeContext TSCtx;
151 using GVPredicate = std::function<bool(const GlobalValue &)>;
152 using GVModifier = std::function<void(GlobalValue &)>;
154 /// Clones the given module on to a new context.
156 cloneToNewContext(ThreadSafeModule &TSMW,
157 GVPredicate ShouldCloneDef = GVPredicate(),
158 GVModifier UpdateClonedDefSource = GVModifier());
160 } // End namespace orc
161 } // End namespace llvm
163 #endif // LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULEWRAPPER_H