1//===----------- ThreadSafeModule.h -- Layer interfaces ---------*- C++ -*-===// 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 7//===----------------------------------------------------------------------===// 9// Thread safe wrappers and utilities for Module and LLVMContext. 11//===----------------------------------------------------------------------===// 13#ifndef LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULE_H 14#define LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULE_H 27/// An LLVMContext together with an associated mutex that can be used to lock 28/// the context to prevent concurrent access by other threads. 32 State(std::unique_ptr<LLVMContext> Ctx) : Ctx(std::move(Ctx)) {}
34 std::unique_ptr<LLVMContext> Ctx;
35 std::recursive_mutex
Mutex;
39// RAII based lock for ThreadSafeContext. 45 std::shared_ptr<State> S;
46 std::unique_lock<std::recursive_mutex> L;
49 /// Construct a null context. 52 /// Construct a ThreadSafeContext from the given LLVMContext. 54 : S(
std::make_shared<State>(
std::
move(NewCtx))) {
56"Can not construct a ThreadSafeContext from a nullptr");
59 /// Returns a pointer to the LLVMContext that was used to construct this 60 /// instance, or null if the instance was default constructed. 63 /// Returns a pointer to the LLVMContext that was used to construct this 64 /// instance, or null if the instance was default constructed. 68assert(S &&
"Can not lock an empty ThreadSafeContext");
73 std::shared_ptr<State> S;
76/// An LLVM Module together with a shared ThreadSafeContext. 79 /// Default construct a ThreadSafeModule. This results in a null module and 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. 96 M = std::move(
Other.M);
97 TSCtx = std::move(
Other.TSCtx);
101 /// Construct a ThreadSafeModule from a unique_ptr<Module> and a 102 /// unique_ptr<LLVMContext>. This creates a new ThreadSafeContext from the 107 /// Construct a ThreadSafeModule from a unique_ptr<Module> and an 108 /// existing ThreadSafeContext. 113// We need to lock the context while we destruct the module. 120 /// Boolean conversion: This ThreadSafeModule will evaluate to true if it 121 /// wraps a non-null module. 125"Non-null module must have non-null context");
131 /// Locks the associated ThreadSafeContext and calls the given function 132 /// on the contained Module. 134assert(M &&
"Can not call on null module");
139 /// Locks the associated ThreadSafeContext and calls the given function 140 /// on the contained Module. 142assert(M &&
"Can not call on null module");
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. 152returnF(std::move(M));
155 /// Get a raw pointer to the contained module without locking the context. 158 /// Get a raw pointer to the contained module without locking the context. 161 /// Returns the context for this ThreadSafeModule. 165 std::unique_ptr<Module> M;
172/// Clones the given module on to a new context. 179}
// End namespace llvm 181#endif// LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULE_H Module.h This file contains the declarations for the Module class.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This is an important class for using LLVM in a threaded context.
A Module instance is used to store all the information related to an LLVM module.
Lock(std::shared_ptr< State > S)
An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...
ThreadSafeContext()=default
Construct a null context.
LLVMContext * getContext()
Returns a pointer to the LLVMContext that was used to construct this instance, or null if the instanc...
ThreadSafeContext(std::unique_ptr< LLVMContext > NewCtx)
Construct a ThreadSafeContext from the given LLVMContext.
const LLVMContext * getContext() const
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.
ThreadSafeContext getContext() const
Returns the context for this ThreadSafeModule.
decltype(auto) consumingModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function, passing the contained std::uniqu...
ThreadSafeModule & operator=(ThreadSafeModule &&Other)
ThreadSafeModule()=default
Default construct a ThreadSafeModule.
Module * getModuleUnlocked()
Get a raw pointer to the contained module without locking the context.
ThreadSafeModule(std::unique_ptr< Module > M, std::unique_ptr< LLVMContext > Ctx)
Construct a ThreadSafeModule from a unique_ptr<Module> and a unique_ptr<LLVMContext>.
decltype(auto) withModuleDo(Func &&F)
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
ThreadSafeModule(ThreadSafeModule &&Other)=default
decltype(auto) withModuleDo(Func &&F) const
Locks the associated ThreadSafeContext and calls the given function on the contained Module.
ThreadSafeModule(std::unique_ptr< Module > M, ThreadSafeContext TSCtx)
Construct a ThreadSafeModule from a unique_ptr<Module> and an existing ThreadSafeContext.
const Module * getModuleUnlocked() const
Get a raw pointer to the contained module without locking the context.
std::function< bool(const GlobalValue &)> GVPredicate
std::function< void(GlobalValue &)> GVModifier
ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())
Clones the given module on to a new context.
This is an optimization pass for GlobalISel generic memory operations.
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Implement std::hash so that hash_code can be used in STL containers.