LLVM Discussion Forums

[RFC] Explicit "_MLIR_CAPI_EXPORTED" macro

I would like to be able to build a shared library/DLL that only exports the C-API and hides everything else (either as part of LLVM/MLIR itself or as part of a larger project). I believe the relatively normal way to do this for this kind of project is something like:

  • On Windows:
    • Apply declspec(dllexport) to functions if building a library
    • Apply declspec(dllimport) to functions if depending on a library
  • On Others:
    • Apply __attribute__((__visibility__("default"))) to functions
    • Build the project with -fvisibility=hidden

There are many things and permutations that can be done if such annotations are applied correctly, and as a first step, we should decide on the macro and use it appropriately.

Libcxx has more complicated needs, but the basic structure and knobs from libcxx/include/__config provides the right recipe. I propose adding a _MLIR_CAPI_EXPORTED macro to a new mlir-c/Config.h and defining it roughly like the libcxx macro is defined (above). We can elaborate on the knobs and additional build setup to fully leverage it later.

Is the request here 1) add such a macro, and 2) add only to functions that we want exported? (e.g., would it be a fairly local change to CAPI or extend beyond that?). Or question about preferred way to address this here?

It is just #1 and then applying it to all functions in the CAPI. I see it as scoped just to the CAPI. Just raising as an RFC vs a patch since it is a design change and we have a somewhat higher bar on stability of the CAPI.