[llvm-dev] What is the proper usage of LLVMContext?

Mehdi Amini via llvm-dev llvm-dev at lists.llvm.org
Tue Feb 21 21:19:34 PST 2017


> On Feb 21, 2017, at 6:15 AM, via llvm-dev <llvm-dev at lists.llvm.org> wrote:
> 
> Hi, I'm Ryo Ota. I have two questions about `llvm::LLVMContext`.
> 
> Q1) What is the difference between the following (A)`my_context` and (B)`global_context`?
> Do I have to create a LLVMContext by myself? Or use `getGlobalContext()`? 
> Could you tell me what situation needs a LLVMContext which is created by myself such as (A)?
> 
> (A)
> {
> 	llvm::LLVMContext my_context;
> 	// codes using only my_context (get Int32ty() or StructType::create or etc...)
> }

This is fine.

> 
> (B)
> {
> 	llvm::LLVMContext &globa_context = llvm::getGlobalContext();
> 	// codes using only globa_context (get Int32ty() or StructType::create or etc...)
> }
> 

This does not exist anymore, getGlobalContext() was removed in 3.9: https://reviews.llvm.org/rL266379 


> 
> Q2) What situation do I need to create multiple `LLVMContext`s?
> I don't know the situation used multiple `LLVMContext`s.
> 
> For example,
> {
> 	{
> 		llvm::LLVMContext ctx1;
> 		// some code
> 	}
> 
> 	{
> 		llvm::LLVMContext ctx2;
> 		// some code
> 	}
> 
> 	{
> 		llvm::LLVMContext ctx3;
> 		// some code
> 	}
> }
> 
> or 
> 
> {
> 	llvm::LLVMContext ctxs[] = {....}
> 	// some code
> }
> 
> I'd like to know the appropriate usage of LLVMContext. Thak you very much for reading.


Context is owning a Module. If you work with multiple modules and you intend to link them together, they need to be in the same context. Otherwise you can use a new context for every module.

As David mentioned: LLVM is thread-safe at the context level, which means that you cannot have multiple threads processing two modules that are in the same context.


Hope this helps.

— 
Mehdi



More information about the llvm-dev mailing list