[llvm-commits] [RFC] Adding PassManagerBuilder to the C API

Rafael Ávila de Espíndola rafael.espindola at gmail.com
Sat Jul 30 19:16:45 PDT 2011


I would like to add PassManagerBuilder bindings to the C API so that a
FE not written in C++ can use it.

The normal way to do this is to have a C++ function marked extern C that
wraps and unwraps the types as needed and calls the C++ API. This
doesn't work for PassManagerBuilder because the populate* functions
depend on all the standard passes, so calling them from any of the
existing libraries would create a cycle (that is why they are defined
inline).

My first idea was to add a LLVMDefaults library and move the
PassManagerBuilder there (or at least the populate* methods could become
out of line functions in there).

In a quick chat on IRC Chris suggested an alternative: flip the bindings
upside down. Implement the pupulate* functions as inline C and have the
C++ methods call them.

The attached patch is a partial implementation of this idea. The
advantage of not having an extra library should be clear. This approach
also forces us to have C bindings to any default pass, but the patch has
also some disadvantages

*) Since the populate* functions are still inline, there is no symbol a
binding to another language can call. A small stub .c file is still
needed for each language. Something like

void MyLangLLVMPopulateModulePassManager(PassManagerBase &MPM) {
  LLVMPassManagerBuilderPopulateModulePassManager(MPM);
}

*) Since the C++ PassManagerBuilder.h uses the C one, we get a bit of
namespace pollution. For example, the enum ExtensionPointTy moves to the
global scope.

If people agree this is the best way, I will finish up the patch and
send it for review. If you have a better idea, let me know :-)

Cheers,
Rafael
-------------- next part --------------
A non-text attachment was scrubbed...
Name: llvm-c.patch
Type: text/x-patch
Size: 35860 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20110730/16035f36/attachment.bin>


More information about the llvm-commits mailing list