Skip to content
This repository has been archived by the owner on Feb 5, 2019. It is now read-only.

Commit

Permalink
Replace Execution Engine's mutex with std::recursive_mutex.
Browse files Browse the repository at this point in the history
This change has a bit of a trickle down effect due to the fact that
there are a number of derived implementations of ExecutionEngine,
and that the mutex is not tightly encapsulated so is used by other
classes directly.

Reviewed by: rnk

Differential Revision: http://reviews.llvm.org/D4196

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@211214 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Zachary Turner committed Jun 18, 2014
1 parent b279154 commit 1f502bd
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 71 deletions.
7 changes: 3 additions & 4 deletions include/llvm/ExecutionEngine/ExecutionEngine.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@
#include "llvm/IR/ValueMap.h"
#include "llvm/MC/MCCodeGenInfo.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include <map>
#include <mutex>
#include <string>
#include <vector>

Expand All @@ -42,7 +42,6 @@ class JITEventListener;
class JITMemoryManager;
class MachineCodeInfo;
class Module;
class MutexGuard;
class ObjectCache;
class RTDyldMemoryManager;
class Triple;
Expand All @@ -59,7 +58,7 @@ class ExecutionEngineState {
public:
struct AddressMapConfig : public ValueMapConfig<const GlobalValue*> {
typedef ExecutionEngineState *ExtraData;
static sys::Mutex *getMutex(ExecutionEngineState *EES);
static std::recursive_mutex *getMutex(ExecutionEngineState *EES);
static void onDelete(ExecutionEngineState *EES, const GlobalValue *Old);
static void onRAUW(ExecutionEngineState *, const GlobalValue *,
const GlobalValue *);
Expand Down Expand Up @@ -164,7 +163,7 @@ class ExecutionEngine {
/// lock - This lock protects the ExecutionEngine, MCJIT, JIT, JITResolver and
/// JITEmitter classes. It must be held while changing the internal state of
/// any of those classes.
sys::Mutex lock;
std::recursive_mutex lock;

//===--------------------------------------------------------------------===//
// ExecutionEngine Startup
Expand Down
12 changes: 6 additions & 6 deletions include/llvm/IR/ValueMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@

#include "llvm/ADT/DenseMap.h"
#include "llvm/IR/ValueHandle.h"
#include "llvm/Support/Mutex.h"
#include "llvm/Support/type_traits.h"
#include <iterator>
#include <mutex>

namespace llvm {

Expand All @@ -45,7 +45,7 @@ class ValueMapConstIterator;
/// This class defines the default behavior for configurable aspects of
/// ValueMap<>. User Configs should inherit from this class to be as compatible
/// as possible with future versions of ValueMap.
template<typename KeyT, typename MutexT = sys::Mutex>
template<typename KeyT, typename MutexT = std::recursive_mutex>
struct ValueMapConfig {
typedef MutexT mutex_type;

Expand Down Expand Up @@ -216,11 +216,11 @@ class ValueMapCallbackVH : public CallbackVH {
ValueMapCallbackVH Copy(*this);
typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
if (M)
M->acquire();
M->lock();
Config::onDelete(Copy.Map->Data, Copy.Unwrap()); // May destroy *this.
Copy.Map->Map.erase(Copy); // Definitely destroys *this.
if (M)
M->release();
M->unlock();
}
void allUsesReplacedWith(Value *new_key) override {
assert(isa<KeySansPointerT>(new_key) &&
Expand All @@ -229,7 +229,7 @@ class ValueMapCallbackVH : public CallbackVH {
ValueMapCallbackVH Copy(*this);
typename Config::mutex_type *M = Config::getMutex(Copy.Map->Data);
if (M)
M->acquire();
M->lock();

KeyT typed_new_key = cast<KeySansPointerT>(new_key);
// Can destroy *this:
Expand All @@ -245,7 +245,7 @@ class ValueMapCallbackVH : public CallbackVH {
}
}
if (M)
M->release();
M->unlock();
}
};

Expand Down
16 changes: 8 additions & 8 deletions lib/ExecutionEngine/ExecutionEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void *ExecutionEngineState::RemoveMapping(const GlobalValue *ToUnmap) {
}

void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

DEBUG(dbgs() << "JIT: Map \'" << GV->getName()
<< "\' to [" << Addr << "]\n";);
Expand All @@ -184,14 +184,14 @@ void ExecutionEngine::addGlobalMapping(const GlobalValue *GV, void *Addr) {
}

void ExecutionEngine::clearAllGlobalMappings() {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

EEState.getGlobalAddressMap().clear();
EEState.getGlobalAddressReverseMap().clear();
}

void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI)
EEState.RemoveMapping(FI);
Expand All @@ -201,7 +201,7 @@ void ExecutionEngine::clearGlobalMappingsFromModule(Module *M) {
}

void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

ExecutionEngineState::GlobalAddressMapTy &Map =
EEState.getGlobalAddressMap();
Expand All @@ -228,15 +228,15 @@ void *ExecutionEngine::updateGlobalMapping(const GlobalValue *GV, void *Addr) {
}

void *ExecutionEngine::getPointerToGlobalIfAvailable(const GlobalValue *GV) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

ExecutionEngineState::GlobalAddressMapTy::iterator I =
EEState.getGlobalAddressMap().find(GV);
return I != EEState.getGlobalAddressMap().end() ? I->second : nullptr;
}

const GlobalValue *ExecutionEngine::getGlobalValueAtAddress(void *Addr) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

// If we haven't computed the reverse mapping yet, do so first.
if (EEState.getGlobalAddressReverseMap().empty()) {
Expand Down Expand Up @@ -555,7 +555,7 @@ void *ExecutionEngine::getPointerToGlobal(const GlobalValue *GV) {
if (Function *F = const_cast<Function*>(dyn_cast<Function>(GV)))
return getPointerToFunction(F);

MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
if (void *P = EEState.getGlobalAddressMap()[GV])
return P;

Expand Down Expand Up @@ -1346,7 +1346,7 @@ ExecutionEngineState::ExecutionEngineState(ExecutionEngine &EE)
: EE(EE), GlobalAddressMap(this) {
}

sys::Mutex *
std::recursive_mutex *
ExecutionEngineState::AddressMapConfig::getMutex(ExecutionEngineState *EES) {
return &EES->EE.lock;
}
Expand Down
38 changes: 19 additions & 19 deletions lib/ExecutionEngine/JIT/JIT.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ namespace {
/// bugpoint or gdb users to search for a function by name without any context.
class JitPool {
SmallPtrSet<JIT*, 1> JITs; // Optimize for process containing just 1 JIT.
mutable sys::Mutex Lock;
mutable std::recursive_mutex Lock;
public:
void Add(JIT *jit) {
MutexGuard guard(Lock);
std::lock_guard<std::recursive_mutex> guard(Lock);
JITs.insert(jit);
}
void Remove(JIT *jit) {
MutexGuard guard(Lock);
std::lock_guard<std::recursive_mutex> guard(Lock);
JITs.erase(jit);
}
void *getPointerToNamedFunction(const char *Name) const {
MutexGuard guard(Lock);
std::lock_guard<std::recursive_mutex> guard(Lock);
assert(JITs.size() != 0 && "No Jit registered");
//search function in every instance of JIT
for (SmallPtrSet<JIT*, 1>::const_iterator Jit = JITs.begin(),
Expand Down Expand Up @@ -150,7 +150,7 @@ JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
AllJits->Add(this);

// Add target data
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
FunctionPassManager &PM = jitstate->getPM();
M->setDataLayout(TM.getDataLayout());
PM.add(new DataLayoutPass(M));
Expand All @@ -177,7 +177,7 @@ JIT::~JIT() {
/// addModule - Add a new Module to the JIT. If we previously removed the last
/// Module, we need re-initialize jitstate with a valid Module.
void JIT::addModule(Module *M) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

if (Modules.empty()) {
assert(!jitstate && "jitstate should be NULL if Modules vector is empty!");
Expand Down Expand Up @@ -206,7 +206,7 @@ void JIT::addModule(Module *M) {
bool JIT::removeModule(Module *M) {
bool result = ExecutionEngine::removeModule(M);

MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

if (jitstate && jitstate->getModule() == M) {
delete jitstate;
Expand Down Expand Up @@ -408,13 +408,13 @@ GenericValue JIT::runFunction(Function *F,
void JIT::RegisterJITEventListener(JITEventListener *L) {
if (!L)
return;
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
EventListeners.push_back(L);
}
void JIT::UnregisterJITEventListener(JITEventListener *L) {
if (!L)
return;
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
std::vector<JITEventListener*>::reverse_iterator I=
std::find(EventListeners.rbegin(), EventListeners.rend(), L);
if (I != EventListeners.rend()) {
Expand All @@ -426,14 +426,14 @@ void JIT::NotifyFunctionEmitted(
const Function &F,
void *Code, size_t Size,
const JITEvent_EmittedFunctionDetails &Details) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
EventListeners[I]->NotifyFunctionEmitted(F, Code, Size, Details);
}
}

void JIT::NotifyFreeingMachineCode(void *OldPtr) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
for (unsigned I = 0, S = EventListeners.size(); I < S; ++I) {
EventListeners[I]->NotifyFreeingMachineCode(OldPtr);
}
Expand All @@ -444,7 +444,7 @@ void JIT::NotifyFreeingMachineCode(void *OldPtr) {
/// GlobalAddress[F] with the address of F's machine code.
///
void JIT::runJITOnFunction(Function *F, MachineCodeInfo *MCI) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

class MCIListener : public JITEventListener {
MachineCodeInfo *const MCI;
Expand Down Expand Up @@ -505,7 +505,7 @@ void *JIT::getPointerToFunction(Function *F) {
if (void *Addr = getPointerToGlobalIfAvailable(F))
return Addr; // Check if function already code gen'd

MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

// Now that this thread owns the lock, make sure we read in the function if it
// exists in this Module.
Expand Down Expand Up @@ -534,7 +534,7 @@ void *JIT::getPointerToFunction(Function *F) {
}

void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

BasicBlockAddressMapTy::iterator I =
getBasicBlockAddressMap().find(BB);
Expand All @@ -546,7 +546,7 @@ void JIT::addPointerToBasicBlock(const BasicBlock *BB, void *Addr) {
}

void JIT::clearPointerToBasicBlock(const BasicBlock *BB) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
getBasicBlockAddressMap().erase(BB);
}

Expand All @@ -555,7 +555,7 @@ void *JIT::getPointerToBasicBlock(BasicBlock *BB) {
(void)getPointerToFunction(BB->getParent());

// resolve basic block address
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

BasicBlockAddressMapTy::iterator I =
getBasicBlockAddressMap().find(BB);
Expand Down Expand Up @@ -592,7 +592,7 @@ void *JIT::getPointerToNamedFunction(const std::string &Name,
/// variable, possibly emitting it to memory if needed. This is used by the
/// Emitter.
void *JIT::getOrEmitGlobalVariable(const GlobalVariable *GV) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);

void *Ptr = getPointerToGlobalIfAvailable(GV);
if (Ptr) return Ptr;
Expand Down Expand Up @@ -666,7 +666,7 @@ char* JIT::getMemoryForGV(const GlobalVariable* GV) {
size_t S = getDataLayout()->getTypeAllocSize(GlobalType);
size_t A = getDataLayout()->getPreferredAlignment(GV);
if (GV->isThreadLocal()) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
Ptr = TJI.allocateThreadLocalMemory(S);
} else if (TJI.allocateSeparateGVMemory()) {
if (A <= 8) {
Expand All @@ -687,7 +687,7 @@ char* JIT::getMemoryForGV(const GlobalVariable* GV) {
}

void JIT::addPendingFunction(Function *F) {
MutexGuard locked(lock);
std::lock_guard<std::recursive_mutex> locked(lock);
jitstate->getPendingFunctions().push_back(F);
}

Expand Down
Loading

0 comments on commit 1f502bd

Please sign in to comment.