LLVM: include/llvm/ExecutionEngine/Orc/ThreadSafeModule.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13#ifndef LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULE_H

14#define LLVM_EXECUTIONENGINE_ORC_THREADSAFEMODULE_H

15

19

20#include

21#include

22#include

23

24namespace llvm {

25namespace orc {

26

27

28

30private:

31 struct State {

32 State(std::unique_ptr Ctx) : Ctx(std::move(Ctx)) {}

33

34 std::unique_ptr Ctx;

35 std::recursive_mutex Mutex;

36 };

37

38public:

39

41

42

44 : S(std::make_shared(std::move(NewCtx))) {

45 assert(S->Ctx != nullptr &&

46 "Can not construct a ThreadSafeContext from a nullptr");

47 }

48

49 template decltype(auto) withContextDo(Func &&F) {

50 if (auto TmpS = S) {

51 std::lock_guardstd::recursive\_mutex Lock(TmpS->Mutex);

52 return F(TmpS->Ctx.get());

53 } else

55 }

56

57 template decltype(auto) withContextDo(Func &&F) const {

58 if (auto TmpS = S) {

59 std::lock_guardstd::recursive\_mutex Lock(TmpS->Mutex);

60 return F(const_cast<const LLVMContext *>(TmpS->Ctx.get()));

61 } else

63 }

64

65private:

66 std::shared_ptr S;

67};

68

69

71public:

72

73

75

77

79

80

81

82

83

84

85 TSCtx.withContextDo([this](LLVMContext *Ctx) { M = nullptr; });

86 M = std::move(Other.M);

87 TSCtx = std::move(Other.TSCtx);

88 return *this;

89 }

90

91

92

93

96

97

98

101

103

104 TSCtx.withContextDo([this](LLVMContext *Ctx) { M = nullptr; });

105 }

106

107

108

109 explicit operator bool() const { return !!M; }

110

111

112

113 template decltype(auto) withModuleDo(Func &&F) {

114 return TSCtx.withContextDo([&](LLVMContext *) {

115 assert(M && "Can not call on null module");

116 return F(*M);

117 });

118 }

119

120

121

122 template decltype(auto) withModuleDo(Func &&F) const {

123 return TSCtx.withContextDo([&](const LLVMContext *) {

124 assert(M && "Can not call on null module");

125 return F(*M);

126 });

127 }

128

129

130

131

133 return TSCtx.withContextDo([&](LLVMContext *) {

134 assert(M && "Can not call on null module");

135 return F(std::move(M));

136 });

137 }

138

139

141

142

144

145

147

148private:

149 std::unique_ptr M;

151};

152

155

156

161

162

163

164

169

170

174

175}

176}

177

178#endif

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

Module.h This file contains the declarations for the Module class.

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.

An LLVMContext together with an associated mutex that can be used to lock the context to prevent conc...

Definition ThreadSafeModule.h:29

ThreadSafeContext()=default

Construct a null context.

ThreadSafeContext(std::unique_ptr< LLVMContext > NewCtx)

Construct a ThreadSafeContext from the given LLVMContext.

Definition ThreadSafeModule.h:43

decltype(auto) withContextDo(Func &&F)

Definition ThreadSafeModule.h:49

decltype(auto) withContextDo(Func &&F) const

Definition ThreadSafeModule.h:57

An LLVM Module together with a shared ThreadSafeContext.

Definition ThreadSafeModule.h:70

ThreadSafeContext getContext() const

Returns the context for this ThreadSafeModule.

Definition ThreadSafeModule.h:146

decltype(auto) consumingModuleDo(Func &&F)

Locks the associated ThreadSafeContext and calls the given function, passing the contained std::uniqu...

Definition ThreadSafeModule.h:132

ThreadSafeModule & operator=(ThreadSafeModule &&Other)

Definition ThreadSafeModule.h:78

~ThreadSafeModule()

Definition ThreadSafeModule.h:102

ThreadSafeModule()=default

Default construct a ThreadSafeModule.

Module * getModuleUnlocked()

Get a raw pointer to the contained module without locking the context.

Definition ThreadSafeModule.h:140

ThreadSafeModule(std::unique_ptr< Module > M, std::unique_ptr< LLVMContext > Ctx)

Construct a ThreadSafeModule from a unique_ptr and a unique_ptr.

Definition ThreadSafeModule.h:94

decltype(auto) withModuleDo(Func &&F)

Locks the associated ThreadSafeContext and calls the given function on the contained Module.

Definition ThreadSafeModule.h:113

ThreadSafeModule(ThreadSafeModule &&Other)=default

decltype(auto) withModuleDo(Func &&F) const

Locks the associated ThreadSafeContext and calls the given function on the contained Module.

Definition ThreadSafeModule.h:122

ThreadSafeModule(std::unique_ptr< Module > M, ThreadSafeContext TSCtx)

Construct a ThreadSafeModule from a unique_ptr and an existing ThreadSafeContext.

Definition ThreadSafeModule.h:99

const Module * getModuleUnlocked() const

Get a raw pointer to the contained module without locking the context.

Definition ThreadSafeModule.h:143

std::function< bool(const GlobalValue &)> GVPredicate

Definition ThreadSafeModule.h:153

std::function< void(GlobalValue &)> GVModifier

Definition ThreadSafeModule.h:154

LLVM_ABI ThreadSafeModule cloneToContext(const ThreadSafeModule &TSMW, ThreadSafeContext TSCtx, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())

Clones the given module onto the given context.

LLVM_ABI ThreadSafeModule cloneExternalModuleToContext(const Module &M, ThreadSafeContext TSCtx, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())

Clone the given module onto the given context.

LLVM_ABI ThreadSafeModule cloneToNewContext(const ThreadSafeModule &TSMW, GVPredicate ShouldCloneDef=GVPredicate(), GVModifier UpdateClonedDefSource=GVModifier())

Clones the given module on to a new context.

SmartMutex< false > Mutex

Mutex - A standard, always enforced mutex.

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.