The LLVM C API has a number of APIs that exist in two variants: One that works on a “global context” and one taking an explicit context argument.
The use of the global context APIs is discouraged, and we do not add any new APIs using the global context.
One of the most common mistakes when using the C API is to accidentally mix APIs using the global context and those using an explicit context. This results in seemingly nonsensical errors or silent misbehavior because types from multiple contexts are mixed. I regularly see confusion related to such issues on Discord.
I think at this point, these APIs are doing more damage than good. (They do make it marginally simpler to use the C API for very basic usage.)
I’d like to formally deprecate the functions working on the global context (with LLVM_ATTRIBUTE_C_DEPRECATED) and remove them after a few releases.
One of the issues with llvm-c is that is supposed to be the ABI stable interface for LLVM. However, I think that it is still very reasonable to at least deprecate the APIs using the global context. That would allow existing users to migrate away without breaking any compatibility.
Yes, in favor! I’ve had many subtle problems in the past caused by accidentally using the global context. I’ve even considered removing them in the Go bindings I maintain ( GitHub - tinygo-org/go-llvm: Go bindings to LLVM. Used as part of TinyGo. ). Removing them in LLVM means those bindings need a small update but that’s well worth it IMHO.
Thanks for the feedback everyone! Sounds like there is a consensus to deprecate these APIs, so I’ve merged the PR.
The ABI stability promise for the C API is that we never change the ABI of an existing function. We only add or remove functions. An LLVM update can result in a linker error, but should not result in silent misbehavior because of a signature change.
Of course, we do try to avoid removals unless necessary, e.g. due to underlying IR changes. See for example all the removed functions for creating no longer supported constant expressions.
I do want to remove these global context APIs at some point, as unfortunately deprecations are of limited utility for the C API – as it is mostly used as an FFI API, header deprecations are often not visible (possibly linker deprecations would be an alternative here). But actual removal will have to be a separate RFC.
Thanks for the fix. I am not sure about the buildbot. I used the above cmake command to build. I thought OCaml bindings are being enabled by default. Is that not the case?