How to use - an lto pass


so far I am using standard llvm and writing "standard" llvm pass
modules, e.g. with RegisterAFLPass(PassManagerBuilder::EP_OptimizerLast,

In llvm-9 there is now the new flag to run the module at link time. I
get a test module compiled but it does not seem to run.

Example module (based on an llvm example):

#include "llvm/Pass.h"
#include "llvm/IR/Function.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/IR/LegacyPassManager.h"
#include "llvm/Transforms/IPO/PassManagerBuilder.h"
using namespace llvm;

namespace {
struct Hello : public FunctionPass {
  static char ID;
  Hello() : FunctionPass(ID) {}

  bool runOnFunction(Function &F) override {
    errs() << "Hello: ";
    errs().write_escaped(F.getName()) << '\n';
    return false;
}; // end of struct Hello
} // end of anonymous namespace

char Hello::ID = 0;
static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);

static RegisterStandardPasses Y(
    (const PassManagerBuilder &Builder,
       legacy::PassManagerBase &PM) { PM.add(new Hello()); });

Compiled with:
clang++-9 `llvm-config --cxxflags` -Wl,-znodelete -fno-rtti -fpic -O3
-funroll-loops -Wall -D_FORTIFY_SOURCE=2 -g -I ../include/
-DVERSION=\"++2.58e\" -Wno-variadic-macros -DLLVMInsTrim_EXPORTS
-fno-rtti -fPIC -std=gnu++11 -shared -o `llvm-config

And when I try to run it:
clang -Xclang -load -Xclang ./ -Qunused-arguments -g -O3 -flto -o
foo foo.c

I dont see any of the "Hello: <functioname>" outputs.
If I set EP_OptimizerLast instead it works, but that at compile time of
each .c file and that is not what I want to do in my project.

How do I run it correctly?


I have not tried the extension points or loading an LTO pass dynamically myself, but adding the author of the patch that added the new EPs (D61738) for insights. Not sure how they interact with dynamically loaded LTO passes.