Movatterモバイル変換


[0]ホーム

URL:


LLVM 20.0.0git
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_THREADSAFEMODULE_H
14#define LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULE_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
24namespacellvm {
25namespaceorc {
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.
29classThreadSafeContext {
30private:
31structState {
32 State(std::unique_ptr<LLVMContext> Ctx) : Ctx(std::move(Ctx)) {}
33
34 std::unique_ptr<LLVMContext> Ctx;
35 std::recursive_mutexMutex;
36 };
37
38public:
39// RAII based lock for ThreadSafeContext.
40class[[nodiscard]]Lock {
41public:
42Lock(std::shared_ptr<State> S) : S(std::move(S)), L(this->S->Mutex) {}
43
44private:
45 std::shared_ptr<State> S;
46 std::unique_lock<std::recursive_mutex> L;
47 };
48
49 /// Construct a null context.
50ThreadSafeContext() =default;
51
52 /// Construct a ThreadSafeContext from the given LLVMContext.
53ThreadSafeContext(std::unique_ptr<LLVMContext> NewCtx)
54 : S(std::make_shared<State>(std::move(NewCtx))) {
55assert(S->Ctx !=nullptr &&
56"Can not construct a ThreadSafeContext from a nullptr");
57 }
58
59 /// Returns a pointer to the LLVMContext that was used to construct this
60 /// instance, or null if the instance was default constructed.
61LLVMContext *getContext() {return S ? S->Ctx.get() :nullptr; }
62
63 /// Returns a pointer to the LLVMContext that was used to construct this
64 /// instance, or null if the instance was default constructed.
65constLLVMContext *getContext() const{return S ? S->Ctx.get() :nullptr; }
66
67LockgetLock() const{
68assert(S &&"Can not lock an empty ThreadSafeContext");
69returnLock(S);
70 }
71
72private:
73 std::shared_ptr<State> S;
74};
75
76/// An LLVM Module together with a shared ThreadSafeContext.
77classThreadSafeModule {
78public:
79 /// Default construct a ThreadSafeModule. This results in a null module and
80 /// null context.
81ThreadSafeModule() =default;
82
83ThreadSafeModule(ThreadSafeModule &&Other) =default;
84
85ThreadSafeModule &operator=(ThreadSafeModule &&Other) {
86// We have to explicitly define this move operator to copy the fields in
87// reverse order (i.e. module first) to ensure the dependencies are
88// protected: The old module that is being overwritten must be destroyed
89// *before* the context that it depends on.
90// We also need to lock the context to make sure the module tear-down
91// does not overlap any other work on the context.
92if (M) {
93auto L = TSCtx.getLock();
94 M =nullptr;
95 }
96 M = std::move(Other.M);
97 TSCtx = std::move(Other.TSCtx);
98return *this;
99 }
100
101 /// Construct a ThreadSafeModule from a unique_ptr<Module> and a
102 /// unique_ptr<LLVMContext>. This creates a new ThreadSafeContext from the
103 /// given context.
104ThreadSafeModule(std::unique_ptr<Module> M, std::unique_ptr<LLVMContext> Ctx)
105 : M(std::move(M)), TSCtx(std::move(Ctx)) {}
106
107 /// Construct a ThreadSafeModule from a unique_ptr<Module> and an
108 /// existing ThreadSafeContext.
109ThreadSafeModule(std::unique_ptr<Module> M,ThreadSafeContext TSCtx)
110 : M(std::move(M)), TSCtx(std::move(TSCtx)) {}
111
112~ThreadSafeModule() {
113// We need to lock the context while we destruct the module.
114if (M) {
115auto L = TSCtx.getLock();
116 M =nullptr;
117 }
118 }
119
120 /// Boolean conversion: This ThreadSafeModule will evaluate to true if it
121 /// wraps a non-null module.
122explicitoperatorbool() const{
123if (M) {
124assert(TSCtx.getContext() &&
125"Non-null module must have non-null context");
126returntrue;
127 }
128returnfalse;
129 }
130
131 /// Locks the associated ThreadSafeContext and calls the given function
132 /// on the contained Module.
133template <typename Func>decltype(auto)withModuleDo(Func &&F) {
134assert(M &&"Can not call on null module");
135auto Lock = TSCtx.getLock();
136returnF(*M);
137 }
138
139 /// Locks the associated ThreadSafeContext and calls the given function
140 /// on the contained Module.
141template <typename Func>decltype(auto)withModuleDo(Func &&F)const {
142assert(M &&"Can not call on null module");
143auto Lock = TSCtx.getLock();
144returnF(*M);
145 }
146
147 /// Locks the associated ThreadSafeContext and calls the given function,
148 /// passing the contained std::unique_ptr<Module>. The given function should
149 /// consume the Module.
150template <typename Func>decltype(auto)consumingModuleDo(Func &&F) {
151auto Lock = TSCtx.getLock();
152returnF(std::move(M));
153 }
154
155 /// Get a raw pointer to the contained module without locking the context.
156Module *getModuleUnlocked() {return M.get(); }
157
158 /// Get a raw pointer to the contained module without locking the context.
159constModule *getModuleUnlocked() const{return M.get(); }
160
161 /// Returns the context for this ThreadSafeModule.
162ThreadSafeContextgetContext() const{return TSCtx; }
163
164private:
165 std::unique_ptr<Module> M;
166ThreadSafeContext TSCtx;
167};
168
169usingGVPredicate = std::function<bool(constGlobalValue &)>;
170usingGVModifier = std::function<void(GlobalValue &)>;
171
172/// Clones the given module on to a new context.
173ThreadSafeModule
174cloneToNewContext(constThreadSafeModule &TSMW,
175GVPredicate ShouldCloneDef =GVPredicate(),
176GVModifier UpdateClonedDefSource =GVModifier());
177
178}// End namespace orc
179}// End namespace llvm
180
181#endif// LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULE_H
Compiler.h
Module.h
Module.h This file contains the declarations for the Module class.
LLVMContext.h
F
#define F(x, y, z)
Definition:MD5.cpp:55
assert
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool
llvm::GlobalValue
Definition:GlobalValue.h:48
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition:LLVMContext.h:67
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition:Module.h:65
llvm::orc::ThreadSafeContext::Lock
Definition:ThreadSafeModule.h:40
llvm::orc::ThreadSafeContext::Lock::Lock
Lock(std::shared_ptr< State > S)
Definition:ThreadSafeModule.h:42
llvm::orc::ThreadSafeContext
An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...
Definition:ThreadSafeModule.h:29
llvm::orc::ThreadSafeContext::ThreadSafeContext
ThreadSafeContext()=default
Construct a null context.
llvm::orc::ThreadSafeContext::getLock
Lock getLock() const
Definition:ThreadSafeModule.h:67
llvm::orc::ThreadSafeContext::getContext
LLVMContext * getContext()
Returns a pointer to the LLVMContext that was used to construct this instance, or null if the instanc...
Definition:ThreadSafeModule.h:61
llvm::orc::ThreadSafeContext::ThreadSafeContext
ThreadSafeContext(std::unique_ptr< LLVMContext > NewCtx)
Construct a ThreadSafeContext from the given LLVMContext.
Definition:ThreadSafeModule.h:53
llvm::orc::ThreadSafeContext::getContext
const LLVMContext * getContext() const
Returns a pointer to the LLVMContext that was used to construct this instance, or null if the instanc...
Definition:ThreadSafeModule.h:65
llvm::orc::ThreadSafeModule
An LLVM Module together with a shared ThreadSafeContext.
Definition:ThreadSafeModule.h:77
llvm::orc::ThreadSafeModule::getContext
ThreadSafeContext getContext() const
Returns the context for this ThreadSafeModule.
Definition:ThreadSafeModule.h:162
llvm::orc::ThreadSafeModule::consumingModuleDo
decltype(auto) consumingModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function, passing the contained std::uniqu...
Definition:ThreadSafeModule.h:150
llvm::orc::ThreadSafeModule::operator=
ThreadSafeModule & operator=(ThreadSafeModule &&Other)
Definition:ThreadSafeModule.h:85
llvm::orc::ThreadSafeModule::~ThreadSafeModule
~ThreadSafeModule()
Definition:ThreadSafeModule.h:112
llvm::orc::ThreadSafeModule::ThreadSafeModule
ThreadSafeModule()=default
Default construct a ThreadSafeModule.
llvm::orc::ThreadSafeModule::getModuleUnlocked
Module * getModuleUnlocked()
Get a raw pointer to the contained module without locking the context.
Definition:ThreadSafeModule.h:156
llvm::orc::ThreadSafeModule::ThreadSafeModule
ThreadSafeModule(std::unique_ptr< Module > M, std::unique_ptr< LLVMContext > Ctx)
Construct a ThreadSafeModule from a unique_ptr<Module> and a unique_ptr<LLVMContext>.
Definition:ThreadSafeModule.h:104
llvm::orc::ThreadSafeModule::withModuleDo
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
Definition:ThreadSafeModule.h:133
llvm::orc::ThreadSafeModule::ThreadSafeModule
ThreadSafeModule(ThreadSafeModule &&Other)=default
llvm::orc::ThreadSafeModule::withModuleDo
decltype(auto) withModuleDo(Func &&F) const
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
Definition:ThreadSafeModule.h:141
llvm::orc::ThreadSafeModule::ThreadSafeModule
ThreadSafeModule(std::unique_ptr< Module > M, ThreadSafeContext TSCtx)
Construct a ThreadSafeModule from a unique_ptr<Module> and an existing ThreadSafeContext.
Definition:ThreadSafeModule.h:109
llvm::orc::ThreadSafeModule::getModuleUnlocked
const Module * getModuleUnlocked() const
Get a raw pointer to the contained module without locking the context.
Definition:ThreadSafeModule.h:159
llvm::sys::SmartMutex< false >
llvm::orc::GVPredicate
std::function< bool(const GlobalValue &)> GVPredicate
Definition:ThreadSafeModule.h:169
llvm::orc::GVModifier
std::function< void(GlobalValue &)> GVModifier
Definition:ThreadSafeModule.h:170
llvm::orc::cloneToNewContext
ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
Definition:ThreadSafeModule.cpp:18
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition:AddressRanges.h:18
llvm::IRMemLocation::Other
@ Other
Any other memory.
llvm::move
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition:STLExtras.h:1873
std
Implement std::hash so that hash_code can be used in STL containers.
Definition:BitVector.h:858

Generated on Sun Jul 20 2025 07:21:12 for LLVM by doxygen 1.9.6
[8]ページ先頭

©2009-2025 Movatter.jp