LLVM  9.0.0svn
ThreadSafeModule.h
Go to the documentation of this file.
1 //===----------- ThreadSafeModule.h -- Layer interfaces ---------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Thread safe wrappers and utilities for Module and LLVMContext.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULEWRAPPER_H
14 #define LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULEWRAPPER_H
15 
16 #include "llvm/IR/LLVMContext.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/Compiler.h"
19 
20 #include <functional>
21 #include <memory>
22 #include <mutex>
23 
24 namespace llvm {
25 namespace orc {
26 
27 /// An LLVMContext together with an associated mutex that can be used to lock
28 /// the context to prevent concurrent access by other threads.
30 private:
31  struct State {
32  State(std::unique_ptr<LLVMContext> Ctx) : Ctx(std::move(Ctx)) {}
33 
34  std::unique_ptr<LLVMContext> Ctx;
35  std::recursive_mutex Mutex;
36  };
37 
38 public:
39  // RAII based lock for ThreadSafeContext.
41  private:
42  using UnderlyingLock = std::lock_guard<std::recursive_mutex>;
43 
44  public:
45  Lock(std::shared_ptr<State> S)
46  : S(std::move(S)),
47  L(llvm::make_unique<UnderlyingLock>(this->S->Mutex)) {}
48 
49  private:
50  std::shared_ptr<State> S;
51  std::unique_ptr<UnderlyingLock> L;
52  };
53 
54  /// Construct a null context.
55  ThreadSafeContext() = default;
56 
57  /// Construct a ThreadSafeContext from the given LLVMContext.
58  ThreadSafeContext(std::unique_ptr<LLVMContext> NewCtx)
59  : S(std::make_shared<State>(std::move(NewCtx))) {
60  assert(S->Ctx != nullptr &&
61  "Can not construct a ThreadSafeContext from a nullptr");
62  }
63 
64  /// Returns a pointer to the LLVMContext that was used to construct this
65  /// instance, or null if the instance was default constructed.
66  LLVMContext *getContext() { return S ? S->Ctx.get() : nullptr; }
67 
68  /// Returns a pointer to the LLVMContext that was used to construct this
69  /// instance, or null if the instance was default constructed.
70  const LLVMContext *getContext() const { return S ? S->Ctx.get() : nullptr; }
71 
73  assert(S && "Can not lock an empty ThreadSafeContext");
74  return Lock(S);
75  }
76 
77 private:
78  std::shared_ptr<State> S;
79 };
80 
81 /// An LLVM Module together with a shared ThreadSafeContext.
83 public:
84  /// Default construct a ThreadSafeModule. This results in a null module and
85  /// null context.
86  ThreadSafeModule() = default;
87 
89 
91  // We have to explicitly define this move operator to copy the fields in
92  // reverse order (i.e. module first) to ensure the dependencies are
93  // protected: The old module that is being overwritten must be destroyed
94  // *before* the context that it depends on.
95  // We also need to lock the context to make sure the module tear-down
96  // does not overlap any other work on the context.
97  if (M) {
98  auto L = getContextLock();
99  M = nullptr;
100  }
101  M = std::move(Other.M);
102  TSCtx = std::move(Other.TSCtx);
103  return *this;
104  }
105 
106  /// Construct a ThreadSafeModule from a unique_ptr<Module> and a
107  /// unique_ptr<LLVMContext>. This creates a new ThreadSafeContext from the
108  /// given context.
109  ThreadSafeModule(std::unique_ptr<Module> M, std::unique_ptr<LLVMContext> Ctx)
110  : M(std::move(M)), TSCtx(std::move(Ctx)) {}
111 
112  /// Construct a ThreadSafeModule from a unique_ptr<Module> and an
113  /// existing ThreadSafeContext.
114  ThreadSafeModule(std::unique_ptr<Module> M, ThreadSafeContext TSCtx)
115  : M(std::move(M)), TSCtx(std::move(TSCtx)) {}
116 
118  // We need to lock the context while we destruct the module.
119  if (M) {
120  auto L = getContextLock();
121  M = nullptr;
122  }
123  }
124 
125  /// Get the module wrapped by this ThreadSafeModule.
126  Module *getModule() { return M.get(); }
127 
128  /// Get the module wrapped by this ThreadSafeModule.
129  const Module *getModule() const { return M.get(); }
130 
131  /// Take out a lock on the ThreadSafeContext for this module.
132  ThreadSafeContext::Lock getContextLock() { return TSCtx.getLock(); }
133 
134  /// Boolean conversion: This ThreadSafeModule will evaluate to true if it
135  /// wraps a non-null module.
136  explicit operator bool() {
137  if (M) {
138  assert(TSCtx.getContext() &&
139  "Non-null module must have non-null context");
140  return true;
141  }
142  return false;
143  }
144 
145 private:
146  std::unique_ptr<Module> M;
147  ThreadSafeContext TSCtx;
148 };
149 
150 using GVPredicate = std::function<bool(const GlobalValue &)>;
151 using GVModifier = std::function<void(GlobalValue &)>;
152 
153 /// Clones the given module on to a new context.
156  GVPredicate ShouldCloneDef = GVPredicate(),
157  GVModifier UpdateClonedDefSource = GVModifier());
158 
159 } // End namespace orc
160 } // End namespace llvm
161 
162 #endif // LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULEWRAPPER_H
This class represents lattice values for constants.
Definition: AllocatorList.h:23
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:65
An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...
static sys::Mutex Lock
ThreadSafeContext(std::unique_ptr< LLVMContext > NewCtx)
Construct a ThreadSafeContext from the given LLVMContext.
std::enable_if<!std::is_array< T >::value, std::unique_ptr< T > >::type make_unique(Args &&... args)
Constructs a new T() with the given args and returns a unique_ptr<T> which owns the object...
Definition: STLExtras.h:1393
std::function< bool(const GlobalValue &)> GVPredicate
SmartMutex< false > Mutex
Mutex - A standard, always enforced mutex.
Definition: Mutex.h:138
Definition: BitVector.h:937
ELFYAML::ELF_STO Other
Definition: ELFYAML.cpp:849
const LLVMContext * getContext() const
Returns a pointer to the LLVMContext that was used to construct this instance, or null if the instanc...
Module * getModule()
Get the module wrapped by this ThreadSafeModule.
ThreadSafeModule(std::unique_ptr< Module > M, ThreadSafeContext TSCtx)
Construct a ThreadSafeModule from a unique_ptr<Module> and an existing ThreadSafeContext.
const Module * getModule() const
Get the module wrapped by this ThreadSafeModule.
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:64
LLVMContext * getContext()
Returns a pointer to the LLVMContext that was used to construct this instance, or null if the instanc...
An LLVM Module together with a shared ThreadSafeContext.
Module.h This file contains the declarations for the Module class.
ThreadSafeContext::Lock getContextLock()
Take out a lock on the ThreadSafeContext for this module.
ThreadSafeModule(std::unique_ptr< Module > M, std::unique_ptr< LLVMContext > Ctx)
Construct a ThreadSafeModule from a unique_ptr<Module> and a unique_ptr<LLVMContext>.
ThreadSafeModule cloneToNewContext(ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
ThreadSafeModule & operator=(ThreadSafeModule &&Other)
#define LLVM_NODISCARD
LLVM_NODISCARD - Warn if a type or return value is discarded.
Definition: Compiler.h:128
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
std::function< void(GlobalValue &)> GVModifier
Lock(std::shared_ptr< State > S)
ThreadSafeContext()=default
Construct a null context.