I have been trying to invoke my out-of-tree LLVM Function pass using clang (opt is not an option. Works fine with opt btw):
clang -std=c99 -m64 -c -o file.o -DSPEC -DNDEBUG -Ispec_qsort -DSPEC_AUTO_SUPPRESS_OPENMP -g -march=native -fno-unsafe-math-optimizations -Xclang -load -Xclang path/to/MyPass.so path/to/my_lib.a -fno-strict-aliasing -fgnu89-inline -DSPEC_LP64 file.c
But I get the following error:
error in backend: Trying to construct TargetPassConfig without a target machine. Scheduling a CodeGen pass without a target triple set?
The pass works fine if I remove the code related to TargetPassConfig.
I register the pass with clang by doing the following:
static void registerClangPass ( const PassManagerBuilder &,
legacy :: PassManagerBase & PM )
{ PM . add ( new MyPass ()); }
static RegisterStandardPasses RegisterClangPass
( PassManagerBuilder :: EP_ModuleOptimizerEarly , registerClangPass );
static RegisterStandardPasses RegisterClangPass0
( PassManagerBuilder :: EP_EnabledOnOptLevel0 , registerClangPass );
To get a rough idea how my pass looks like, please take a look at:
https://github.com/llvm-mirror/llvm/blob/master/lib/CodeGen/SafeStack.cpp
which also has dependency on TargetPassConfig. The following line seems to be the problem:
TM = &getAnalysis<TargetPassConfig>().getTM<TargetMachine>();
I am not sure how I can invoke/populate TargetMachine before my pass runs. But as mentioned above, the pass works fine with opt. Therefore, I am reluctant to change the pass logic.
Any pointers would be appreciated.