Reputation: 31
I am new to the LLVM environment. I am trying to comprehend the concept of Context. What does it mean "Context of a code"? Why the framework needs it? From various information on the web, it seems that its state information for a given code. Still could not visualize what it does? and why it is important?
Upvotes: 2
Views: 1001
Reputation: 1342
The Context exists to allow a single program to use two libraries (say, a graphics library and a 3d-audio library) both of which use LLVM under the hood, to have their LLVM's not interfere with one another. The rule is that you may never move LLVM objects between two contexts. All modules, type and constants created with one context stay in that context forever. (The verifier will check that all the IR in a module belongs to the same context, just in case.)
This non-interference can also be useful for thread-safety, when you want to have two threads doing things with LLVM at the same time. You can't combine the contexts later, but you could generate assembly code text and concatenate that.
There is also a global object for convenience, but only the main() program should use that, lest we have two instances of LLVM interfere again.
Upvotes: 4