[llvm-dev] How to use - an lto pass

Marc via llvm-dev llvm-dev at lists.llvm.org
Tue Jan 7 06:37:05 PST 2020


Hello,

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

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 test.cc (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(
    PassManagerBuilder::EP_FullLinkTimeOptimizationLast,
    [](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 test.cc -o test.so `llvm-config
--ldflags`

And when I try to run it:
clang -Xclang -load -Xclang ./test.so -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?
Thanks!

Regards,
Marc

-- 
Marc Heuse
www.mh-sec.de

PGP: AF3D 1D4C D810 F0BB 977D  3807 C7EE D0A0 6BE9 F573


More information about the llvm-dev mailing list