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 `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. Sincerely, Ryo -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170221/19ca2a2c/attachment.html>
David Chisnall via llvm-dev
2017-Feb-21 14:30 UTC
[llvm-dev] What is the proper usage of LLVMContext?
On 21 Feb 2017, at 14:15, via llvm-dev <llvm-dev at lists.llvm.org> wrote:> > Q1) What is the difference between the following (A)`my_context` and (B)`global_context`?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. David
Mehdi Amini via llvm-dev
2017-Feb-22 05:19 UTC
[llvm-dev] What is the proper usage of LLVMContext?
> 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
Thank you very much for your helpful answer. I'm using LLVM 3.8 in C++. I appreciate that you told me that`getGlobalContext()` 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_ptr<llvm::Module> module = llvm::make_unique<llvm::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. Sincerely, Ryo 2017年2月22日(水) 14:19 Mehdi Amini <mehdi.amini at apple.com>:> > > 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 > >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20170222/f46e10d3/attachment.html>