<div dir="ltr">Hi Folks,<br><br>some update on the « compiler extension » topic.<br>The main patch landed last week and received a bunch of update meanwhile. It's almost complete, so let me describe it some more,<div>based on a thread [0] on <a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>.<br><br># What's this patch all about?<br><br>Proposing a common infrastructure to write LLVM pass plugins linked dynamically (a.ka. MODULE in the cmake terminology) or statically</div><div>(just a STATIC library) and have them work gracefully within opt/clang/bugpoint.<br>Polly already had several hooks to provide this behavior. At such, this patch is a generalization of the approach.<br><br># What problem does it solve?<br><br>Dynamically loaded plugins are an easy way to jump into LLVM, but they are not natural to use for the end-users: no one wants to type</div><div><br> clang -Xclang -load -Xclang /clang/install/path/.../MyPlugin.so my_source.c on a regular basis.</div><div><br>The next step is generally to write a regular LLVM pass, register it in clang / opt pipelines, which involves a few modifications to the original tree.<br>With this patch, one can write a top-level project, say StuffyDoll, that's separated from llvm-project source, and use the cmake </div><div><br></div><div> -DCMAKE_ENABLE_PROJECTS=clang;StuffyDoll</div><div><br></div><div>to have it being compiled alongside LLVM, then use the</div><div> </div><div> -DLLVM_STUFFYDOLL_LINK_INTO_TOOLS=[ON|OFF]</div><div><br></div><div>option to switch between the module or static build. When using the static build, the appropriate clang/opt/bugpoint hooks are automatically installed,</div><div>which means *no* modification outside of StuffyDoll.<br><br># How should I use it?<br><br>Probably through reading the in-tree example [1], after reading the documentation [2] :-)<br><br># Is it working for all platforms?<br><br>It's 100% functional on Linux, waiting for <a href="https://reviews.llvm.org/D72493">https://reviews.llvm.org/D72493</a> to be accepted[3] to fully work on OSX, and it only works for static builds on Windows.<br><br>[0] <a href="http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20200106/300726.html">http://lists.llvm.org/pipermail/cfe-commits/Week-of-Mon-20200106/300726.html</a><br>[1] <a href="https://github.com/llvm/llvm-project/tree/master/llvm/examples/Bye">https://github.com/llvm/llvm-project/tree/master/llvm/examples/Bye</a><br>[2] <a href="http://llvm.org/docs/WritingAnLLVMPass.html#building-pass-plugins">http://llvm.org/docs/WritingAnLLVMPass.html#building-pass-plugins</a><br>[3] If you've read so far, you might be interested enough to participate in the review ;-)</div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Fri, Sep 20, 2019 at 3:56 PM Serge Guelton <<a href="mailto:sguelton@redhat.com" target="_blank">sguelton@redhat.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Hi folks,<br>
<br>
I've been working for a few months on a proposal to make it easier to develop<br>
out-of-tree passes, and have them linked either statically or dynamically,<br>
within LLVM. This includes automatic integration within clang, opt and bugpoint.<br>
<br>
The goal is to lower the bar for people who develop out-of tree passes: they can<br>
maintain their code base in a third-party repo, pick it at config time and build<br>
it statically or dynamically.<br>
<br>
As a side-effect, this provides a generalization of the polly-specific code<br>
that spread at several locations of the code base (and removes most of the<br>
explicit mention of Polly itself, without removing the functionality, obviously).<br>
<br>
Both legacy and new pass managers are supported.<br>
<br>
>From the review's documentation:<br>
<br>
<br>
LLVM provides a mechanism to automatically register pass plugins within<br>
``clang``, ``opt`` and ``bugpoint``. One first needs to create an independent<br>
project and add it to either ``tools/`` or, using the MonoRepo layout, at the<br>
root of the repo alongside other projects. This project must contain the<br>
following minimal ``CMakeLists.txt``:<br>
<br>
.. code-block:: cmake<br>
<br>
add_llvm_pass_plugin(Name source0.cpp)<br>
<br>
The pass must provide two entry points for the new pass manager, one for static<br>
registration and one for dynamically loaded plugins:<br>
<br>
- ``llvm::PassPluginLibraryInfo get##Name##PluginInfo();``<br>
- ``extern "C" ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() LLVM_ATTRIBUTE_WEAK;``<br>
<br>
<br>
Pass plugins are compiled and link dynamically by default, but it's<br>
possible to set the following variables to change this behavior:<br>
<br>
- ``LLVM_${NAME}_LINK_INTO_TOOLS``, when sets to ``ON``, turns the project into<br>
a statically linked extension<br>
<br>
The review also contains an example of pass plugin, `llvm/examples/Bye/Bye.cpp`.<br>
<br>
The associated review is available at <a href="https://reviews.llvm.org/D61446" rel="noreferrer" target="_blank">https://reviews.llvm.org/D61446</a>, Michael<br>
Kruse has already done a lot of review, I think it's time to gather more<br>
feedback, so if you're interested... jump in!<br>
</blockquote></div>