Skip to content

Commit 0bfa0bc

Browse files
authored
[ORC] Replace ThreadSafeContext::getContext with withContextDo. (#146819)
This removes ThreadSafeContext::Lock, ThreadSafeContext::getLock, and ThreadSafeContext::getContext, and replaces them with a ThreadSafeContext::withContextDo method (and const override). The new method can be used to access an existing ThreadSafeContext-wrapped LLVMContext in a safe way: ThreadSafeContext TSCtx = ... ; TSCtx.withContextDo([](LLVMContext *Ctx) { // this closure has exclusive access to Ctx. }); The new API enforces correct locking, whereas the old APIs relied on manual locking (which almost no in-tree code preformed, relying instead on incidental exclusive access to the ThreadSafeContext).
1 parent 9234d07 commit 0bfa0bc

File tree

18 files changed

+151
-164
lines changed

18 files changed

+151
-164
lines changed

clang/lib/Interpreter/Interpreter.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -373,8 +373,11 @@ Interpreter::Interpreter(std::unique_ptr<CompilerInstance> Instance,
373373
auto LLVMCtx = std::make_unique<llvm::LLVMContext>();
374374
TSCtx = std::make_unique<llvm::orc::ThreadSafeContext>(std::move(LLVMCtx));
375375

376-
Act = std::make_unique<IncrementalAction>(*CI, *TSCtx->getContext(), ErrOut,
377-
*this, std::move(Consumer));
376+
Act = TSCtx->withContextDo([&](llvm::LLVMContext *Ctx) {
377+
return std::make_unique<IncrementalAction>(*CI, *Ctx, ErrOut, *this,
378+
std::move(Consumer));
379+
});
380+
378381
if (ErrOut)
379382
return;
380383
CI->ExecuteAction(*Act);
@@ -495,10 +498,10 @@ Interpreter::createWithCUDA(std::unique_ptr<CompilerInstance> CI,
495498
std::unique_ptr<Interpreter> Interp = std::move(*InterpOrErr);
496499

497500
llvm::Error Err = llvm::Error::success();
498-
llvm::LLVMContext &LLVMCtx = *Interp->TSCtx->getContext();
499501

500-
auto DeviceAct =
501-
std::make_unique<IncrementalAction>(*DCI, LLVMCtx, Err, *Interp);
502+
auto DeviceAct = Interp->TSCtx->withContextDo([&](llvm::LLVMContext *Ctx) {
503+
return std::make_unique<IncrementalAction>(*DCI, *Ctx, Err, *Interp);
504+
});
502505

503506
if (Err)
504507
return std::move(Err);

llvm/examples/OrcV2Examples/LLJITWithThinLTOSummaries/LLJITWithThinLTOSummaries.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,9 @@ Expected<ThreadSafeModule> loadModule(StringRef Path,
169169

170170
MemoryBufferRef BitcodeBufferRef = (**BitcodeBuffer).getMemBufferRef();
171171
Expected<std::unique_ptr<Module>> M =
172-
parseBitcodeFile(BitcodeBufferRef, *TSCtx.getContext());
172+
TSCtx.withContextDo([&](LLVMContext *Ctx) {
173+
return parseBitcodeFile(BitcodeBufferRef, *Ctx);
174+
});
173175
if (!M)
174176
return M.takeError();
175177

llvm/examples/OrcV2Examples/OrcV2CBindingsBasicUsage/OrcV2CBindingsBasicUsage.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ int handleError(LLVMErrorRef Err) {
2222
}
2323

2424
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
25-
// Create a new ThreadSafeContext and underlying LLVMContext.
26-
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
27-
28-
// Get a reference to the underlying LLVMContext.
29-
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
25+
// Create an LLVMContext.
26+
LLVMContextRef Ctx = LLVMContextCreate();
3027

3128
// Create a new LLVM module.
3229
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
@@ -57,6 +54,9 @@ LLVMOrcThreadSafeModuleRef createDemoModule(void) {
5754
// - Free the builder.
5855
LLVMDisposeBuilder(Builder);
5956

57+
// Create a new ThreadSafeContext to hold the context.
58+
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
59+
6060
// Our demo module is now complete. Wrap it and our ThreadSafeContext in a
6161
// ThreadSafeModule.
6262
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);

llvm/examples/OrcV2Examples/OrcV2CBindingsDumpObjects/OrcV2CBindingsDumpObjects.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ int handleError(LLVMErrorRef Err) {
3131
}
3232

3333
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
34-
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
35-
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
34+
LLVMContextRef Ctx = LLVMContextCreate();
3635
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
3736
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
3837
LLVMTypeRef SumFunctionType =
@@ -45,6 +44,8 @@ LLVMOrcThreadSafeModuleRef createDemoModule(void) {
4544
LLVMValueRef SumArg1 = LLVMGetParam(SumFunction, 1);
4645
LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
4746
LLVMBuildRet(Builder, Result);
47+
LLVMOrcThreadSafeContextRef TSCtx =
48+
LLVMOrcCreateNewThreadSafeContextFromLLVMContext(Ctx);
4849
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);
4950
LLVMOrcDisposeThreadSafeContext(TSCtx);
5051
return TSM;

llvm/examples/OrcV2Examples/OrcV2CBindingsIRTransforms/OrcV2CBindingsIRTransforms.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ int handleError(LLVMErrorRef Err) {
3232
}
3333

3434
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
35-
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
36-
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
35+
LLVMContextRef Ctx = LLVMContextCreate();
3736
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
3837
LLVMTypeRef ParamTypes[] = {LLVMInt32Type(), LLVMInt32Type()};
3938
LLVMTypeRef SumFunctionType =
@@ -47,6 +46,7 @@ LLVMOrcThreadSafeModuleRef createDemoModule(void) {
4746
LLVMValueRef Result = LLVMBuildAdd(Builder, SumArg0, SumArg1, "result");
4847
LLVMBuildRet(Builder, Result);
4948
LLVMDisposeBuilder(Builder);
49+
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
5050
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);
5151
LLVMOrcDisposeThreadSafeContext(TSCtx);
5252
return TSM;

llvm/examples/OrcV2Examples/OrcV2CBindingsLazy/OrcV2CBindingsLazy.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,9 @@ const char MainMod[] =
6767
LLVMErrorRef parseExampleModule(const char *Source, size_t Len,
6868
const char *Name,
6969
LLVMOrcThreadSafeModuleRef *TSM) {
70-
// Create a new ThreadSafeContext and underlying LLVMContext.
71-
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
7270

73-
// Get a reference to the underlying LLVMContext.
74-
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
71+
// Create an LLVMContext for the Module.
72+
LLVMContextRef Ctx = LLVMContextCreate();
7573

7674
// Wrap Source in a MemoryBuffer
7775
LLVMMemoryBufferRef MB =
@@ -85,6 +83,10 @@ LLVMErrorRef parseExampleModule(const char *Source, size_t Len,
8583
// TODO: LLVMDisposeMessage(ErrMsg);
8684
}
8785

86+
// Create a new ThreadSafeContext to hold the context.
87+
LLVMOrcThreadSafeContextRef TSCtx =
88+
LLVMOrcCreateNewThreadSafeContextFromLLVMContext(Ctx);
89+
8890
// Our module is now complete. Wrap it and our ThreadSafeContext in a
8991
// ThreadSafeModule.
9092
*TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);

llvm/examples/OrcV2Examples/OrcV2CBindingsMCJITLikeMemoryManager/OrcV2CBindingsMCJITLikeMemoryManager.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,8 @@ int handleError(LLVMErrorRef Err) {
150150
}
151151

152152
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
153-
// Create a new ThreadSafeContext and underlying LLVMContext.
154-
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
155-
156-
// Get a reference to the underlying LLVMContext.
157-
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
153+
// Create an LLVMContext.
154+
LLVMContextRef Ctx = LLVMContextCreate();
158155

159156
// Create a new LLVM module.
160157
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
@@ -182,6 +179,10 @@ LLVMOrcThreadSafeModuleRef createDemoModule(void) {
182179
// - Build the return instruction.
183180
LLVMBuildRet(Builder, Result);
184181

182+
// Create a new ThreadSafeContext to hold the context.
183+
LLVMOrcThreadSafeContextRef TSCtx =
184+
LLVMOrcCreateNewThreadSafeContextFromLLVMContext(Ctx);
185+
185186
// Our demo module is now complete. Wrap it and our ThreadSafeContext in a
186187
// ThreadSafeModule.
187188
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);

llvm/examples/OrcV2Examples/OrcV2CBindingsRemovableCode/OrcV2CBindingsRemovableCode.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,8 @@ int handleError(LLVMErrorRef Err) {
2222
}
2323

2424
LLVMOrcThreadSafeModuleRef createDemoModule(void) {
25-
// Create a new ThreadSafeContext and underlying LLVMContext.
26-
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
27-
28-
// Get a reference to the underlying LLVMContext.
29-
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
25+
// Create an LLVMContext.
26+
LLVMContextRef Ctx = LLVMContextCreate();
3027

3128
// Create a new LLVM module.
3229
LLVMModuleRef M = LLVMModuleCreateWithNameInContext("demo", Ctx);
@@ -57,6 +54,10 @@ LLVMOrcThreadSafeModuleRef createDemoModule(void) {
5754
// - Free the builder.
5855
LLVMDisposeBuilder(Builder);
5956

57+
// Create a new ThreadSafeContext to hold the context.
58+
LLVMOrcThreadSafeContextRef TSCtx =
59+
LLVMOrcCreateNewThreadSafeContextFromLLVMContext(Ctx);
60+
6061
// Our demo module is now complete. Wrap it and our ThreadSafeContext in a
6162
// ThreadSafeModule.
6263
LLVMOrcThreadSafeModuleRef TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);

llvm/examples/OrcV2Examples/OrcV2CBindingsVeryLazy/OrcV2CBindingsVeryLazy.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,8 @@ LLVMErrorRef applyDataLayout(void *Ctx, LLVMModuleRef M) {
7474
LLVMErrorRef parseExampleModule(const char *Source, size_t Len,
7575
const char *Name,
7676
LLVMOrcThreadSafeModuleRef *TSM) {
77-
// Create a new ThreadSafeContext and underlying LLVMContext.
78-
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
79-
80-
// Get a reference to the underlying LLVMContext.
81-
LLVMContextRef Ctx = LLVMOrcThreadSafeContextGetContext(TSCtx);
77+
// Create an LLVMContext.
78+
LLVMContextRef Ctx = LLVMContextCreate();
8279

8380
// Wrap Source in a MemoryBuffer
8481
LLVMMemoryBufferRef MB =
@@ -93,6 +90,9 @@ LLVMErrorRef parseExampleModule(const char *Source, size_t Len,
9390
return Err;
9491
}
9592

93+
// Create a new ThreadSafeContext to hold the context.
94+
LLVMOrcThreadSafeContextRef TSCtx = LLVMOrcCreateNewThreadSafeContext();
95+
9696
// Our module is now complete. Wrap it and our ThreadSafeContext in a
9797
// ThreadSafeModule.
9898
*TSM = LLVMOrcCreateNewThreadSafeModule(M, TSCtx);

llvm/include/llvm-c/Orc.h

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1062,20 +1062,32 @@ LLVMErrorRef LLVMOrcCreateStaticLibrarySearchGeneratorForPath(
10621062
const char *FileName);
10631063

10641064
/**
1065-
* Create a ThreadSafeContext containing a new LLVMContext.
1065+
* Create a ThreadSafeContextRef containing a new LLVMContext.
10661066
*
10671067
* Ownership of the underlying ThreadSafeContext data is shared: Clients
1068-
* can and should dispose of their ThreadSafeContext as soon as they no longer
1069-
* need to refer to it directly. Other references (e.g. from ThreadSafeModules)
1070-
* will keep the data alive as long as it is needed.
1068+
* can and should dispose of their ThreadSafeContextRef as soon as they no
1069+
* longer need to refer to it directly. Other references (e.g. from
1070+
* ThreadSafeModules) will keep the underlying data alive as long as it is
1071+
* needed.
10711072
*/
10721073
LLVMOrcThreadSafeContextRef LLVMOrcCreateNewThreadSafeContext(void);
10731074

10741075
/**
1075-
* Get a reference to the wrapped LLVMContext.
1076+
* Create a ThreadSafeContextRef from a given LLVMContext, which must not be
1077+
* associated with any existing ThreadSafeContext.
1078+
*
1079+
* The underlying ThreadSafeContext will take ownership of the LLVMContext
1080+
* object, so clients should not free the LLVMContext passed to this
1081+
* function.
1082+
*
1083+
* Ownership of the underlying ThreadSafeContext data is shared: Clients
1084+
* can and should dispose of their ThreadSafeContextRef as soon as they no
1085+
* longer need to refer to it directly. Other references (e.g. from
1086+
* ThreadSafeModules) will keep the underlying data alive as long as it is
1087+
* needed.
10761088
*/
1077-
LLVMContextRef
1078-
LLVMOrcThreadSafeContextGetContext(LLVMOrcThreadSafeContextRef TSCtx);
1089+
LLVMOrcThreadSafeContextRef
1090+
LLVMOrcCreateNewThreadSafeContextFromLLVMContext(LLVMContextRef Ctx);
10791091

10801092
/**
10811093
* Dispose of a ThreadSafeContext.

0 commit comments

Comments
 (0)