Can I write LLVM pass using add_llvm_library, not add_llvm_loadable_module?

I’d write new transform pass related to some function optimization, and contribute it into LLVM. But Writing an LLVM Pass was described only using as dynamic LLVM loadable modules (.so extensions).

I want to write LLVM pass using add_llvm_library, not add_llvm_loadable_module in CMakeLists.txt. Can I do it? If yes, how?

Yes you can put a pass in a static or shared library rather than a module. It largely depends on how you want to have it consumed. Using a loadable module is the least intrusive way to create an out-of-tree pass, so that is how the pass tutorial is written. If you use a static archive you’ll need to modify the build system to link your pass into all the tools you wish to use it from. There is very little technical difference between a shared library and a module, but the build system treats them differently because they have different impact on the dependency graph because a runtime loaded module does not need to exist in order to link binaries that depend on it.

-Chris

Polly (polly.llvm.org) creates both, a loadable module, and if built
in the tools/polly directory, by default also linked to opt/clang. You
might look into Polly's source for how it is done (primarily
lib/CMakeLists.txt and lib/Polly.cpp)

Michael

You probably don’t want to add a new directory? In this case, the easiest would be to take an existing file and implement by analogy.

/lib/Transforms/Scalar/ConstantProp.cpp is a good example. The relevant lines are 53-62. Note that createConstantPropagation function is called outside – grep it and add a call to your own pass creator. Also, you should add your file to relevant CMakeLists.txt.

Yours,

Andrey