llvm pass

Hi list,

I have several questions about LLVM pass.

  1. Is building a custom LLVM pass out-of-source not recommended?
    The official document only contains instructions about in-source build (http://llvm.org/docs/WritingAnLLVMPass.html).

  2. opt (ver >= 4) with custom pass libraries does not work as before. When I have a simple custom LLVM pass called “helloworld", loading the library works well with 3.9 but opt-4.0 cannot not find the pass:

$ opt-3.9 -load …/build/hello/LLVMHelloWorld.so -helloworld hello.bc -S > /dev/null
Hello: main

$ opt-4.0 -load …/build/hello/LLVMHelloWorld.so -helloworld hello.bc -S > /dev/null
opt-4.0: Unknown command line argument ‘-helloworld’. Try: ‘opt-4.0 -help’
opt-4.0: Did you mean '-loops’?

  1. I tried to invoke custom LLVM passes from clang rather than opt. I followed instructions from several articles like
    https://www.cs.cornell.edu/~asampson/blog/llvm.html
    https://medium.com/@mshockwave/writing-llvm-pass-in-2018-part-iii-d44cd0c2c354
    and registered the pass to clang’s workflow with legacy pass manger. But it raises segmentation fault errors.
    What is a recommended way to register custom passes to clang directly?

Thanks in advance.

Best,
Kihong

  1. Is building a custom LLVM pass out-of-source not recommended?

https://reviews.llvm.org/D61446

to make it easier to add out-of tree passes and register them in clang/opt/bugpoint

Hi Kihong,

  1. Is building a custom LLVM pass out-of-source not recommended?

The official document only contains instructions about in-source build (http://llvm.org/docs/WritingAnLLVMPass.html).

Out-of-tree passes are perfectly fine, but not well documented. All you need to do is find_package() the llvm you want to build against and include its AddLLVM module. Then effectively all the details discussed in the how-to you linked work exactly the same.

  1. opt (ver >= 4) with custom pass libraries does not work as before. When I have a simple custom LLVM pass called “helloworld", loading the library works well with 3.9 but opt-4.0 cannot not find the pass:

Did you build your plugin against LLVM >= 4?

  1. I tried to invoke custom LLVM passes from clang rather than opt. I followed instructions from several articles like
    https://www.cs.cornell.edu/~asampson/blog/llvm.html
    https://medium.com/@mshockwave/writing-llvm-pass-in-2018-part-iii-d44cd0c2c354
    and registered the pass to clang’s workflow with legacy pass manger. But it raises segmentation fault errors.
    What is a recommended way to register custom passes to clang directly?

The articles are correct in how to do pass registration. In particular, this is how you do it. Hard to guess where you bugs are without having seen the code. Again, is the clang version the same as the LLVM version you build your plugin against.

Cheers,
Philip

Hi Philip,

Thanks for your answer. It was very helpful.
I still have a problem when I integrate my pass directly into clang rather than opt.

The articles are correct in how to do pass registration. In particular, this is how you do it. Hard to guess where you bugs are without having seen the code. Again, is the clang version the same as the LLVM version you build your plugin against.

As you mentioned, I followed the above link and run the following command (TracePass.so is my pass)

$ clang -Xclang -load -Xclang …/…/build/TracePass.so myprint.c

It results in segmentation fault below.
It seems that this issue (https://github.com/sampsyo/llvm-pass-skeleton/issues/7) is about the same problem. I am curious about the standard way to register custom passes in the latest compiler. I am using Clang 8.
Here is my code (https://github.com/KihongHeo/llvm-trace/blob/pass/src/Trace.cpp).

It would be appreciated if you can give some pointers of manuals or references.

Thanks,
Kihong

Error message:

Module: Stack dump:
0. Program arguments: /usr/lib/llvm-8/bin/clang -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all -disable-free -disable-llvm-verifier -discard-value-names -main-file-name myprint.c -mrelocation
-model static -mthread-model posix -mdisable-fp-elim -fmath-errno -masm-verbose -mconstructor-aliases -munwind-tables -fuse-init-array -target-cpu x86-64 -dwarf-column-info -debugger-tuning=gdb -resource-dir /usr/lib/llvm-8/lib/clang/8.0.1 -internal-isystem /usr/local/include -internal-isystem /usr/lib/llvm-8/lib/clang/8.0.1/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir /home/khheo/project/llvm-trace/test/test0 -ferror-limit 19 -fmessage-length 204 -fobjc-runtime=gcc -fdiagnostics-show-option -fcolor-diagnostics -load …/…/build/TracePass.so -o /tmp/myprint-cd1bfe.o -x c myprint.c -faddrsig

  1. parser at end of file
  2. Per-function optimization
  3. Running pass ‘Unnamed pass: implement Pass::getPassName()’ on function ‘@myprintf
    #0 0x00007fc41281d4ef llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0x9714ef)
    #1 0x00007fc41281b940 llvm::sys::RunSignalHandlers() (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0x96f940)
    #2 0x00007fc41281d8f8 (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0x9718f8)
    #3 0x00007fc415b04890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890)
    #4 0x00007fc4102e01dc llvm::ilist_node_base::getNext() const /usr/lib/llvm-8/include/llvm/ADT/ilist_node_base.h:30:45
    #5 0x00007fc4102e01c5 llvm::ilist_node_impl<llvm::ilist_detail::node_options<llvm::Function, false, false, void> >::getNext() const /usr/lib/llvm-8/include/llvm/ADT/ilist_node.h:75:5
    #6 0x00007fc4102e019f llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true>::operator++() /usr/lib/llvm-8/include/llvm/ADT/ilist_iterator.h:159:57
    #7 0x00007fc4102e011b std::iterator_traits<llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true> >::difference_type std::__distance<llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true> >(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true>, std::input_iterator_tag) /usr/bin/…/lib/gcc/x86_64-linux-gnu/7.4.0/…/…/…/…/include/c++/7.4.0/bits/stl_iterator_base_funcs.h:90:4
    #8 0x00007fc4102e0066 std::iterator_traits<llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true> >::difference_type std::distance<llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true> >(llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true>, llvm::ilist_iterator<llvm::ilist_detail::node_options<llvm::Function, false, false, void>, false, true>) /usr/bin/…/lib/gcc/x86_64-linux-gnu/7.4.0/…/…/…/…/include/c++/7.4.0/bits/stl_iterator_base_funcs.h:141:7
    #9 0x00007fc4102e0027 llvm::simple_ilistllvm::Function::size() const /usr/lib/llvm-8/include/llvm/ADT/simple_ilist.h:136:5
    #10 0x00007fc4102dfcc9 llvm::Module::size() const /usr/lib/llvm-8/include/llvm/IR/Module.h:603:43
    #11 0x00007fc4102df47c (anonymous namespace)::TracePass::runOnModule(llvm::Module&) /home/khheo/project/llvm-trace/src/Trace.cpp:70:31
    #12 0x00007fc4129105c0 llvm::FPPassManager::runOnFunction(llvm::Function&) (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0xa645c0)
    #13 0x00007fc41290fcfe llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&) (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0xa63cfe)
    #14 0x00007fc41290fc90 llvm::legacy::FunctionPassManager::run(llvm::Function&) (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0xa63c90)
    #15 0x00000000006db762 clang::EmitBackendOutput(clang::DiagnosticsEngine&, clang::HeaderSearchOptions const&, clang::CodeGenOptions const&, clang::TargetOptions const&, clang::LangOptions const&, llvm::DataLayout const&, llvm::Module*, clang::BackendAction, std::unique_ptr<llvm::raw_pwrite_stream, std::default_deletellvm::raw_pwrite_stream >) (/usr/lib/llvm-8/bin/clang+0x6db762)
    #16 0x0000000000d5390f (/usr/lib/llvm-8/bin/clang+0xd5390f)
    #17 0x00000000011d9c93 clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-8/bin/clang+0x11d9c93)
    #18 0x0000000000ada39f clang::FrontendAction::Execute() (/usr/lib/llvm-8/bin/clang+0xada39f)
    #19 0x0000000000a98ec8 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-8/bin/clang+0xa98ec8)
    #20 0x0000000000b5fe76 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-8/bin/clang+0xb5fe76)
    #21 0x00000000006a2dec cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-8/bin/clang+0x6a2dec)
    #22 0x00000000006a1336 main (/usr/lib/llvm-8/bin/clang+0x6a1336)
    #23 0x00007fc41119db97 __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/…/csu/libc-start.c:344:0
    #24 0x000000000069ea5a _start (/usr/lib/llvm-8/bin/clang+0x69ea5a)
    clang: error: unable to execute command: Segmentation fault (core dumped)