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…)
}
(B)
{
llvm::LLVMContext &globa_context = llvm::getGlobalContext();
// codes using only globa_context (get Int32ty() or StructType::create or etc…)
}
Q2) What situation do I need to create multiple LLVMContexts?
I don’t know the situation used multiple LLVMContexts.
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.
These questions are really the same question. LLVMContext is an object that contains (in theory) all of the global state associated with a use of LLVM. Most LLVM functions are not thread safe, so you create an LLVMContext for each thread and, if you want to be able to free some memory, for each separate use of LLVM APIs (e.g. create a module and JIT it in one context, then clean up all of the memory except for the generated code).
The global context object exists to help migrate legacy code that predated the introduction of LLVMContext. I thought it had been removed already, but if not then it probably will be eventually. You should not use it in new code.
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: rG03b42e41bf4b
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.
I’m using LLVM 3.8 in C++. I appreciate that you told me thatgetGlobalContext() had been removed.
Context is owning a Module.
I missunderstood that a module has a context, becase a module is created by such as the following code
{
std::unique_ptrllvm::Module module = llvm::make_uniquellvm::Module(“my-module”, context);
}
If you work with multiple modules and you intend to link them together, they need to be in the same context.
I’ll create a common context to link modules together.