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

via llvm-dev llvm-dev at lists.llvm.org
Tue Feb 21 22:01:23 PST 2017


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>


More information about the llvm-dev mailing list