<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body style="word-wrap: break-word; -webkit-nbsp-mode: space; line-break: after-white-space;" class="">Hi Philip,<div class=""><br class=""></div><div class="">Thanks for your answer. It was very helpful.</div><div class="">I still have a problem when I integrate my pass directly into clang rather than opt.</div><div class=""><br class=""></div><div class=""><blockquote type="cite" class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">The articles are  correct in how to do pass registration. In particular, [this](<a href="https://www.cs.cornell.edu/~asampson/blog/clangpass.html" class="">https://www.cs.cornell.edu/~asampson/blog/clangpass.html</a>) 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.</div></div></div></blockquote></div><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class=""><br class=""></div><div class="">As you mentioned, I followed the above link and run the following command (TracePass.so is my pass)</div><div class=""><br class=""></div><div class="">$ clang -Xclang -load -Xclang ../../build/TracePass.so myprint.c</div><div class=""><br class=""></div><div class="">It results in segmentation fault below.</div><div class="">It seems that this issue (<a href="https://github.com/sampsyo/llvm-pass-skeleton/issues/7" class="">https://github.com/sampsyo/llvm-pass-skeleton/issues/7</a>) 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.</div><div class="">Here is my code (<a href="https://github.com/KihongHeo/llvm-trace/blob/pass/src/Trace.cpp" class="">https://github.com/KihongHeo/llvm-trace/blob/pass/src/Trace.cpp</a>).</div><div class=""><br class=""></div><div class="">It would be appreciated if you can give some pointers of manuals or references.</div><div class=""><br class=""></div><div class="">Thanks,</div><div class="">Kihong</div><div class=""><br class=""></div><div class=""><br class=""></div><div class=""><br class=""></div><div class="">Error message:</div><div class=""><br class=""></div><div class=""><div class="">Module: Stack dump:</div><div class="">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</div><div class="">-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</div><div class="">1.      <eof> parser at end of file</div><div class="">2.      Per-function optimization</div><div class="">3.      Running pass 'Unnamed pass: implement Pass::getPassName()' on function '@myprintf'</div><div class=""> #0 0x00007fc41281d4ef llvm::sys::PrintStackTrace(llvm::raw_ostream&) (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0x9714ef)</div><div class=""> #1 0x00007fc41281b940 llvm::sys::RunSignalHandlers() (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0x96f940)</div><div class=""> #2 0x00007fc41281d8f8 (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0x9718f8)</div><div class=""> #3 0x00007fc415b04890 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x12890)</div><div class=""> #4 0x00007fc4102e01dc llvm::ilist_node_base<false>::getNext() const /usr/lib/llvm-8/include/llvm/ADT/ilist_node_base.h:30:45</div><div class=""> #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</div><div class=""> #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</div><div class=""> #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</div><div class=""> #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</div><div class=""> #9 0x00007fc4102e0027 llvm::simple_ilist<llvm::Function>::size() const /usr/lib/llvm-8/include/llvm/ADT/simple_ilist.h:136:5</div><div class="">#10 0x00007fc4102dfcc9 llvm::Module::size() const /usr/lib/llvm-8/include/llvm/IR/Module.h:603:43</div><div class="">#11 0x00007fc4102df47c (anonymous namespace)::TracePass::runOnModule(llvm::Module&) /home/khheo/project/llvm-trace/src/Trace.cpp:70:31</div><div class="">#12 0x00007fc4129105c0 llvm::FPPassManager::runOnFunction(llvm::Function&) (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0xa645c0)</div><div class="">#13 0x00007fc41290fcfe llvm::legacy::FunctionPassManagerImpl::run(llvm::Function&) (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0xa63cfe)</div><div class="">#14 0x00007fc41290fc90 llvm::legacy::FunctionPassManager::run(llvm::Function&) (/usr/lib/x86_64-linux-gnu/libLLVM-8.so.1+0xa63c90)</div><div class="">#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_delete<llvm::raw_pwrite_stream> >) (/usr/lib/llvm-8/bin/clang+0x6db762)</div><div class="">#16 0x0000000000d5390f (/usr/lib/llvm-8/bin/clang+0xd5390f)</div><div class="">#17 0x00000000011d9c93 clang::ParseAST(clang::Sema&, bool, bool) (/usr/lib/llvm-8/bin/clang+0x11d9c93)</div><div class="">#18 0x0000000000ada39f clang::FrontendAction::Execute() (/usr/lib/llvm-8/bin/clang+0xada39f)</div><div class="">#19 0x0000000000a98ec8 clang::CompilerInstance::ExecuteAction(clang::FrontendAction&) (/usr/lib/llvm-8/bin/clang+0xa98ec8)</div><div class="">#20 0x0000000000b5fe76 clang::ExecuteCompilerInvocation(clang::CompilerInstance*) (/usr/lib/llvm-8/bin/clang+0xb5fe76)</div><div class="">#21 0x00000000006a2dec cc1_main(llvm::ArrayRef<char const*>, char const*, void*) (/usr/lib/llvm-8/bin/clang+0x6a2dec)</div><div class="">#22 0x00000000006a1336 main (/usr/lib/llvm-8/bin/clang+0x6a1336)</div><div class="">#23 0x00007fc41119db97 __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:344:0</div><div class="">#24 0x000000000069ea5a _start (/usr/lib/llvm-8/bin/clang+0x69ea5a)</div><div class="">clang: error: unable to execute command: Segmentation fault (core dumped)</div></div></div></div></div><div class=""><div><br class=""><blockquote type="cite" class=""><div class="">On May 27, 2019, at 3:33 AM, Philip Pfaffe <<a href="mailto:philip.pfaffe@gmail.com" class="">philip.pfaffe@gmail.com</a>> wrote:</div><br class="Apple-interchange-newline"><div class=""><div dir="ltr" class=""><div class="gmail_quote"><div class="">Hi Kihong, </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class=""><div class="">1) Is building a custom LLVM pass out-of-source not recommended?</div></div></blockquote><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class=""><div class="">The official document only contains instructions about in-source build (<a href="http://llvm.org/docs/WritingAnLLVMPass.html" target="_blank" class="">http://llvm.org/docs/WritingAnLLVMPass.html</a>).</div></div></blockquote><div class="">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.</div><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class=""><div class=""></div><div class="">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:</div></div></blockquote><div class="">Did you build your plugin against LLVM >= 4? </div><div class=""> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class=""><div class="">3) I tried to invoke custom LLVM passes from clang rather than opt. I followed instructions from several articles like</div><div class=""><a href="https://www.cs.cornell.edu/~asampson/blog/llvm.html" target="_blank" class="">https://www.cs.cornell.edu/~asampson/blog/llvm.html</a></div><div class=""><a href="https://medium.com/@mshockwave/writing-llvm-pass-in-2018-part-iii-d44cd0c2c354" target="_blank" class="">https://medium.com/@mshockwave/writing-llvm-pass-in-2018-part-iii-d44cd0c2c354</a></div><div class="">and registered the pass to clang’s workflow with legacy pass manger. But it raises segmentation fault errors.</div><div class="">What is a recommended way to register custom passes to clang directly?</div></div></blockquote><div class="">The articles are  correct in how to do pass registration. In particular, [this](<a href="https://www.cs.cornell.edu/~asampson/blog/clangpass.html" class="">https://www.cs.cornell.edu/~asampson/blog/clangpass.html</a>) 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.</div><div class=""><br class=""></div><div class="">Cheers,</div><div class="">Philip</div><div class=""><br class=""></div><div class=""><br class=""></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div style="overflow-wrap: break-word;" class=""><div class=""><br class=""></div><div class="">Thanks in advance.</div><div class=""><br class=""></div><div class="">Best,</div><div class="">Kihong</div></div>_______________________________________________<br class="">
LLVM Developers mailing list<br class="">
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" class="">llvm-dev@lists.llvm.org</a><br class="">
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank" class="">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br class="">
</blockquote></div></div>
</div></blockquote></div><br class=""></div></body></html>