[RFC] Deprecate C API functions using the global context

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.

The implementation for the change can be found here: [llvm-c] Deprecate functions working on the global context by nikic · Pull Request #163979 · llvm/llvm-project · GitHub

6 Likes

Bumping this thread as there have been no responses. Does anyone have objections to this change?

I think this is fine as long as users have a reasonable way to transition. How old are the replacement APIs?

Deprecating now sounds good to me.

It looks like the context-based APIs were introduced in 2009 (LLVM 2.6).

On the Julia wrapper LLVM.jl we moved away from these last year so fine for us.

1 Like

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.

@nikic Hi, I am facing a build error. I bisected and found this commit.

 🠞 build.fast (main) $ ninja -v
[1/1887] cd /home/user/build.fast && /usr/bin/cmake -E make_directory /home/user/build.fast/./lib/ocaml/llvm
[2/1887] cd /home/user/build.fast/bindings/ocaml/llvm && /home/user/.opam/default/bin/ocamlfind ocamlc -c /home/user/build.fast/bindings/ocaml/llvm/llvm_ocaml.c -ccopt "-I/home/user/llvm-project/llvm/bindings/ocaml/llvm/ -D_GNU_SOURCE -D_GLIBCXX_USE_CXX11_ABI=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/user/build.fast/include -I/home/user/llvm-project/llvm/include  -DNDEBUG "
FAILED: bindings/ocaml/llvm/llvm_ocaml.o /home/user/build.fast/bindings/ocaml/llvm/llvm_ocaml.o
cd /home/user/build.fast/bindings/ocaml/llvm && /home/user/.opam/default/bin/ocamlfind ocamlc -c /home/user/build.fast/bindings/ocaml/llvm/llvm_ocaml.c -ccopt "-I/home/user/llvm-project/llvm/bindings/ocaml/llvm/ -D_GNU_SOURCE -D_GLIBCXX_USE_CXX11_ABI=1 -D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS -I/home/user/build.fast/include -I/home/user/llvm-project/llvm/include  -DNDEBUG "
/home/user/build.fast/bindings/ocaml/llvm/llvm_ocaml.c: In function ‘llvm_global_context’:
/home/user/build.fast/bindings/ocaml/llvm/llvm_ocaml.c:243:17: error: implicit declaration of function ‘getGlobalContextForCAPI’ [-Wimplicit-function-declaration]
  243 |   return to_val(getGlobalContextForCAPI());
      |                 ^~~~~~~~~~~~~~~~~~~~~~~
/home/user/build.fast/bindings/ocaml/llvm/llvm_ocaml.c:243:17: error: passing argument 1 of ‘to_val’ makes pointer from integer without a cast [-Wint-conversion]
  243 |   return to_val(getGlobalContextForCAPI());
      |                 ^~~~~~~~~~~~~~~~~~~~~~~~~
      |                 |
      |                 int
/home/user/build.fast/bindings/ocaml/llvm/llvm_ocaml.c:46:20: note: expected ‘void *’ but argument is of type ‘int’
   46 | value to_val(void *ptr) {
      |              ~~~~~~^~~

This doesn’t get resolved, and is in LLVMContext.h. Including this header directly doesn’t work either.

CMake command:

cmake -G Ninja \
    -DCMAKE_C_COMPILER=/usr/bin/clang \
    -DCMAKE_CXX_COMPILER=/usr/bin/clang++ \
    -DLLVM_TARGETS_TO_BUILD="X86" \
    -DLLVM_BUILD_EXAMPLES=OFF \
    -DCMAKE_BUILD_TYPE=Release \
    /path/to/llvm-project/llvm

Thanks for the report, should be fixed by [OCaml] Fix build · llvm/llvm-project@c66eb25 · GitHub. I didn’t not have OCaml bindings enabled locally, and it looks like we don’t have buildbots for it either?

1 Like

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?

They’re enabled by default, but require OCaml and certain OCaml packages to be installed.

I usually intentionally uninstall ocaml-ctypes to disable the bindings, because they seem to always get broken on rebuilds. ([OCaml] Try to fix library dependencies by nikic · Pull Request #171470 · llvm/llvm-project · GitHub might fix that.)