Skip to content

Commit

Permalink
[docs][NewPM] Move pass plugin documentation into existing new PM docs
Browse files Browse the repository at this point in the history
Reviewed By: awarzynski, asbirlea

Differential Revision: https://reviews.llvm.org/D136626
  • Loading branch information
aeubanks committed Nov 2, 2022
1 parent 5b30fc2 commit 76b04c2
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 45 deletions.
5 changes: 5 additions & 0 deletions llvm/docs/NewPassManager.rst
Expand Up @@ -179,6 +179,11 @@ sanitizer) passes to various parts of the pipeline.
``AMDGPUTargetMachine::registerPassBuilderCallbacks()`` is an example of a
backend adding passes to various parts of the pipeline.

Pass plugins can also add passes into default pipelines. Different tools have
different ways of loading dynamic pass plugins. For example, ``opt
-load-pass-plugin=path/to/plugin.so`` loads a pass plugin into ``opt``. For
information on writing a pass plugin, see :doc:`WritingAnLLVMNewPMPass`.

Using Analyses
==============

Expand Down
55 changes: 55 additions & 0 deletions llvm/docs/WritingAnLLVMNewPMPass.rst
Expand Up @@ -232,3 +232,58 @@ function. Required passes will still be run on ``optnone`` functions.

For more implementation details, see
``PassInstrumentation::runBeforePass()``.

Registering passes as plugins
-----------------------------

LLVM provides a mechanism to register pass plugins within various tools like
``clang`` or ``opt``. A pass plugin can add passes to default optimization
pipelines or to be manually run via tools like ``opt``. For more information,
see :doc:`NewPassManager`.

Create a CMake project at the root of the repo alongside
other projects. This project must contain the following minimal
``CMakeLists.txt``:

.. code-block:: cmake
add_llvm_pass_plugin(MyPassName source.cpp)
See the definition of ``add_llvm_pass_plugin`` for more CMake details.

The pass must provide at least one of two entry points for the new pass manager,
one for static registration and one for dynamically loaded plugins:

- ``llvm::PassPluginLibraryInfo get##Name##PluginInfo();``
- ``extern "C" ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() LLVM_ATTRIBUTE_WEAK;``

Pass plugins are compiled and linked dynamically by default. Setting
``LLVM_${NAME}_LINK_INTO_TOOLS`` to ``ON`` turns the project into a statically
linked extension.

For an in-tree example, see ``llvm/examples/Bye/``.

To make ``PassBuilder`` aware of statically linked pass plugins:

.. code-block:: c++

// Declare plugin extension function declarations.
#define HANDLE_EXTENSION(Ext) llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
#include "llvm/Support/Extension.def"

...

// Register plugin extensions in PassBuilder.
#define HANDLE_EXTENSION(Ext) get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
#include "llvm/Support/Extension.def"

To make ``PassBuilder`` aware of dynamically linked pass plugins:

.. code-block:: c++

// Load plugin dynamically.
auto Plugin = PassPlugin::Load(PathToPlugin);
if (!Plugin)
report_error();
// Register plugin extensions in PassBuilder.
Plugin.registerPassBuilderCallbacks(PB);
45 changes: 0 additions & 45 deletions llvm/docs/WritingAnLLVMPass.rst
Expand Up @@ -1183,51 +1183,6 @@ implement ``releaseMemory`` to, well, release the memory allocated to maintain
this internal state. This method is called after the ``run*`` method for the
class, before the next call of ``run*`` in your pass.

Building pass plugins
=====================

As an alternative to using ``PLUGIN_TOOL``, LLVM provides a mechanism to
automatically register pass plugins within ``clang``, ``opt`` and ``bugpoint``.
One first needs to create an independent project and add it to either ``tools/``
or, using the MonoRepo layout, at the root of the repo alongside other projects.
This project must contain the following minimal ``CMakeLists.txt``:

.. code-block:: cmake
add_llvm_pass_plugin(Name source0.cpp)
The pass must provide two entry points for the new pass manager, one for static
registration and one for dynamically loaded plugins:

- ``llvm::PassPluginLibraryInfo get##Name##PluginInfo();``
- ``extern "C" ::llvm::PassPluginLibraryInfo llvmGetPassPluginInfo() LLVM_ATTRIBUTE_WEAK;``

Pass plugins are compiled and link dynamically by default, but it's
possible to set the following variables to change this behavior:

- ``LLVM_${NAME}_LINK_INTO_TOOLS``, when set to ``ON``, turns the project into
a statically linked extension


When building a tool that uses the new pass manager, one can use the following snippet to
include statically linked pass plugins:

.. code-block:: c++

// fetch the declaration
#define HANDLE_EXTENSION(Ext) llvm::PassPluginLibraryInfo get##Ext##PluginInfo();
#include "llvm/Support/Extension.def"

[...]

// use them, PB is an llvm::PassBuilder instance
#define HANDLE_EXTENSION(Ext) get##Ext##PluginInfo().RegisterPassBuilderCallbacks(PB);
#include "llvm/Support/Extension.def"





Registering dynamically loaded passes
=====================================

Expand Down

0 comments on commit 76b04c2

Please sign in to comment.