<div dir="ltr">Hi Alberto:<div><br></div><div>You are right, llvm-tutor. I am happy to share my change with you. I also put the patch file with complete change in this mail.</div><div><br></div><div>``` cpp</div><div><font face="monospace">llvm::PassPluginLibraryInfo getHelloWorldPluginInfo() {<br>  return {LLVM_PLUGIN_API_VERSION, "HelloWorld", LLVM_VERSION_STRING,<br>          [](PassBuilder &PB) {<br>            errs() << "registerPipelineStartEPCallback" << "\n";<br>            PB.registerPipelineStartEPCallback(<br>              [](ModulePassManager &MPM, OptimizationLevel Level) {<br>                 FunctionPassManager FPM;                 <br>                 FPM.addPass(HelloWorld());<br>                 MPM.addPass(createModuleToFunctionPassAdaptor(std::move(FPM)));<br>                   });<br>         }};<br>}</font><br></div><div>```</div><div><br></div><div>``` bash</div><div><font face="monospace">❯ $LLVM_DIR/bin/clang --version<br>clang version 14.0.0 (<a href="https://github.com/llvm/llvm-project.git">https://github.com/llvm/llvm-project.git</a> eb052f6b8f787847827f9cc9ff8e09f954cb0a44)<br>Target: x86_64-unknown-linux-gnu<br>Thread model: posix<br>InstalledDir: /home/dougpuob/petzone/llvm/installed/main/debug//bin<br><br>❯ make<br>Scanning dependencies of target HelloWorld<br>[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/HelloWorld.cpp.o<br>[100%] Linking CXX shared library libHelloWorld.so<br>[100%] Built target HelloWorld<br><br></font></div><div><font face="monospace">❯ cat /home/dougpuob/petzone/llvm/testdata/20211218--symcc/input.c<br>#include <stdio.h><br><br>int main() {<br>        printf("Hello Input !!! \n");<br>        return 0;<br>}⏎                                                                                                                      <br><br>❯ $LLVM_DIR/bin/clang \<br>           -fpass-plugin=/home/dougpuob/petzone/llvm/testdata/20211218--symcc/llvm-tutor.git/build/libHelloWorld.so  \<br>           /home/dougpuob/petzone/llvm/testdata/20211218--symcc/input.c<br>CodeGenOpts.PassPlugins<br>PassPlugin::Load()<br>registerPipelineStartEPCallback<br>registerPipelineStartEPCallback()<br>(llvm-tutor) Hello from: main<br>(llvm-tutor)   number of arguments: 0<br>(llvm-tutor)    Opcode : alloca<br>(llvm-tutor)    Opcode : store<br>(llvm-tutor)    Opcode : call<br>(llvm-tutor)    Opcode : ret</font><br></div><div>```</div><div><br></div><div><div><div dir="ltr" class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><font face="arial, sans-serif">-- Douglas Chen</font><br></div></div></div></div></div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sun, Dec 19, 2021 at 1:14 AM Alberto Barbaro <<a href="mailto:barbaro.alberto@gmail.com">barbaro.alberto@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="auto">Hi Douglas,<div dir="auto">Just be clear, did you modify the helloworld example from llvm-tutor not clang-tutor right?</div><div dir="auto"><br></div><div dir="auto">Would you mind to share the entire code, the clang full command and output? It'd help me to solve a lot of time.</div><div dir="auto"><br></div><div dir="auto">Imo it could be added to the website as a tutorial.</div><div dir="auto"><br></div><div dir="auto">Thanks</div><div dir="auto">Alberto</div><div dir="auto"><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Dec 18, 2021, 17:26 Douglas Chen [陳鍵源] <<a href="mailto:dougpuob@gmail.com" target="_blank">dougpuob@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div dir="ltr">Hi Min:<div><br></div><div>I am interested in the discussion. I modified the code from clang-tutor to register with `PassPipeline::registerPipelineStartEPCallback`. The plugin can be loaded with clang with `-fpass-plugin=<path to plugin>` option. Thank you for your suggestions and help.</div><div><div><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div dir="ltr"><div><br></div><div><font face="arial, sans-serif">-- Douglas Chen</font><br></div></div></div></div></div></div></div><br></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Sat, Dec 18, 2021 at 5:03 PM Min-Yih Hsu via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div><br><div><br><blockquote type="cite"><div>On Dec 18, 2021, at 4:36 PM, Alberto Barbaro <<a href="mailto:barbaro.alberto@gmail.com" rel="noreferrer" target="_blank">barbaro.alberto@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div>Hi,</div><div>I think I got confused. Just clarify: my main goal is to be able to port symcc[1] to llvm-13.</div><div><br></div><div>Since I'm confused I went back to the beginning I have experimented with and HelloWorld [2] LLVM pass. As you can see in the following paragraph all went well:</div><div><br></div><div>root@eaa014e3667a:~/llvm-tutor/build# opt-13 -load-pass-plugin ./libHelloWorld.so -passes=hello-world -disable-output output.ll <br>(llvm-tutor) Hello from: foo<br>(llvm-tutor)   number of arguments: 1<br>(llvm-tutor) Hello from: bar<br>(llvm-tutor)   number of arguments: 2<br>(llvm-tutor) Hello from: fez<br>(llvm-tutor)   number of arguments: 3<br>(llvm-tutor) Hello from: main<br>(llvm-tutor)   number of arguments: 2<br>root@eaa014e3667a:~/llvm-tutor/build#</div><div><br></div><div>At this point I wanted to load directly the plugin via clang using the -fplugin parameter</div></div></div></blockquote><div><br></div><div><span style="color:rgb(0,0,0)">Correct, that is expected, `-fplugin` is for Clang plugin not LLVM pass plugin.</span></div><br><blockquote type="cite"><div><div dir="ltr"><div>but I wasn't able to do it. I tried the following command:</div></div></div></blockquote><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>root@eaa014e3667a:~/llvm-tutor/build# clang -fpass-plugin=/root/llvm-tutor/build/libHelloWorld.so ../inputs/input_for_hello.c -o hello           <br>root@eaa014e3667a:~/llvm-tutor/build# file hello <br>hello: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=ba237b79f2b2bcd362e894657b1e203af914aa9c, for GNU/Linux 3.2.0, not stripped<br>root@eaa014e3667a:~/llvm-tutor/build# <br></div><div><br></div><div>I was expecting the same output as before where the number of parameters were printed.<br></div></div></div></blockquote><div><br></div><div>The HelloWorld example you mentioned is using `llvm::PassBuilder::registerPipelineParsingCallback`. Passes registered in this way are not visible to clang.</div><div>More specifically, if you registered a Pass via `<span style="color:rgb(0,0,0)">registerPipelineParsingCallback</span>`, you need to call `PassBuilder::parsePassPipeline` with a textual pass pipeline description. But clang doesn’t use that function at all. The opt tool is using it (via the `—passes` flag) though.</div><div><br></div><div>Try to use other `PassPipline` registration functions like `PassPipeline::registerPipelineStartEPCallback` or `PassPipeline::registerOptimizerLastEPCallback`.</div><div><br></div><div>Also, when the optimization level is set to -O0 (which is the default one), every IR function is annotated with `optnone`, which prevents a function from being visited by any LLVM Pass. So be sure to add `-Xclang -disable-O0-optnone` flag on clang to turn this off (or using optimization levels other than -O0).</div><div><br></div><div>Best,</div><div>-Min</div><br><blockquote type="cite"><div><div dir="ltr"><div><br></div><div>With some verbosity the interesting part was</div><div><br></div><div> "/usr/lib/llvm-13/bin/clang" -cc1 -triple x86_64-pc-linux-gnu -emit-obj -mrelax-all --mrelax-relocations -disable-free -disable-llvm-verifier -discard-value-names -main-file-name input_for_hello.c -mrelocation-model static -mframe-pointer=all -fmath-errno -fno-rounding-math -mconstructor-aliases -munwind-tables -target-cpu x86-64 -tune-cpu generic -debugger-tuning=gdb -v -fcoverage-compilation-dir=/root/llvm-tutor/build -resource-dir /usr/lib/llvm-13/lib/clang/13.0.1 -internal-isystem /usr/lib/llvm-13/lib/clang/13.0.1/include -internal-isystem /usr/local/include -internal-isystem /usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../x86_64-linux-gnu/include -internal-externc-isystem /usr/include/x86_64-linux-gnu -internal-externc-isystem /include -internal-externc-isystem /usr/include -fdebug-compilation-dir=/root/llvm-tutor/build -ferror-limit 19 -fgnuc-version=4.2.1 -fcolor-diagnostics <b>-fpass-plugin=/root/llvm-tutor/build/libHelloWorld.so</b> -faddrsig -D__GCC_HAVE_DWARF2_CFI_ASM=1 -o /tmp/input_for_hello-c52362.o -x c ../inputs/input_for_hello.c</div><div><br></div><div>symcc loads the plugin in this way:</div><div><br></div><div>➜  build git:(master) ✗ tail -n 8 symcc                                                   <br><br>exec $compiler                                  \<br>     -Xclang -load -Xclang "$pass"              \<br>     "$@"                                       \<br>     -L"$runtime_dir"                           \<br>     -lSymRuntime                               \<br>     -Wl,-rpath,"$runtime_dir"                  \<br>     -Qunused-arguments<br>➜  build git:(master) ✗ <br></div><div><br></div><div><br></div><div><br></div><div>How am I supposed to load the plugin via clang so I can just have the same output? I want to <i>avoid</i> the use of opt.<br></div><div><br></div><div>Thanks a lot for your help</div><div>Alberto<br></div><br><div><br></div><div>[1] <a href="https://github.com/eurecom-s3/symcc" rel="noreferrer" target="_blank">https://github.com/eurecom-s3/symcc</a><br></div><div>[2] <a href="https://github.com/banach-space/llvm-tutor" rel="noreferrer" target="_blank">https://github.com/banach-space/llvm-tutor</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno sab 18 dic 2021 alle ore 00:52 Min-Yih Hsu <<a href="mailto:minyihh@uci.edu" rel="noreferrer" target="_blank">minyihh@uci.edu</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>Both links you provided are for Clang plugin not LLVM plugin. Examples of clang plugins including modifying AST or registering custom `#pragma` directives.<div><br></div><div>To my best understanding your original question was asking about LLVM plugin, which works on LLVM IR.</div><div><br></div><div>Best,</div><div>-Min<br><div><br><blockquote type="cite"><div>On Dec 17, 2021, at 2:56 PM, Alberto Barbaro <<a href="mailto:barbaro.alberto@gmail.com" rel="noreferrer" target="_blank">barbaro.alberto@gmail.com</a>> wrote:</div><br><div><div dir="ltr"><div>Hi Min-Yih,</div><div>thanks for your email. I have searched online on how to build clang plugins and I found [1]. I wanted to use the example that prints all the function names but, despite the fact that I followed the instructions, I could not compile it :)</div><div><br></div><div>I searched again and I found [2] and hopefully In this case I could build and run the basic hello-world example. I then tried to just replace the HelloWorld.cpp with the example that print the functions names but no luck. I compile but I have an error message. The following paragraph shows the entire test:</div><div><br></div><div>➜  build git:(main) ✗ make                                   <br>Scanning dependencies of target HelloWorld<br>[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/HelloWorld.cpp.o<br>[100%] Linking CXX shared library libHelloWorld.so<br>[100%] Built target HelloWorld<br>➜  build git:(main) ✗ clang-13 -cc1 -load ./libHelloWorld.so -plugin hello-world $CLANG_TUTOR_DIR/test/HelloWorld-basic.cpp<br>➜  build git:(main) ✗ $Clang_DIR/bin/clang -cc1 -load ./libHelloWorld.so -plugin hello-world $CLANG_TUTOR_DIR/test/HelloWorld-basic.cpp <br>(clang-tutor)  file: /home/alberto/Desktop/projects/llvm/clang-tutor//test/HelloWorld-basic.cpp<br>(clang-tutor)  count: 3<br>➜  build git:(main) ✗ cd ..    <br>➜  HelloWorld git:(main) ✗ wget <a href="https://raw.githubusercontent.com/llvm/llvm-project/main/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp" rel="noreferrer" target="_blank">https://raw.githubusercontent.com/llvm/llvm-project/main/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp</a><br>--2021-12-17 06:45:54--  <a href="https://raw.githubusercontent.com/llvm/llvm-project/main/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp" rel="noreferrer" target="_blank">https://raw.githubusercontent.com/llvm/llvm-project/main/clang/examples/PrintFunctionNames/PrintFunctionNames.cpp</a><br>Resolving <a href="http://raw.githubusercontent.com/" rel="noreferrer" target="_blank">raw.githubusercontent.com</a> (<a href="http://raw.githubusercontent.com/" rel="noreferrer" target="_blank">raw.githubusercontent.com</a>)... 185.199.110.133, 185.199.109.133, 185.199.108.133, ...<br>Connecting to <a href="http://raw.githubusercontent.com/" rel="noreferrer" target="_blank">raw.githubusercontent.com</a> (<a href="http://raw.githubusercontent.com/" rel="noreferrer" target="_blank">raw.githubusercontent.com</a>)|185.199.110.133|:443... connected.<br>HTTP request sent, awaiting response... 200 OK<br>Length: 4504 (4.4K) [text/plain]<br>Saving to: ‘PrintFunctionNames.cpp’<br><br>PrintFunctionNames.cp 100%[========================>]   4.40K  --.-KB/s    in 0s      <br><br>2021-12-17 06:45:54 (11.3 MB/s) - ‘PrintFunctionNames.cpp’ saved [4504/4504]<br><br>➜  HelloWorld git:(main) ✗ mv HelloWorld.cpp HelloWorld.cpp.backup<br>➜  HelloWorld git:(main) ✗ mv PrintFunctionNames.cpp HelloWorld.cpp        <br>➜  HelloWorld git:(main) ✗ rm -rf build <br>➜  HelloWorld git:(main) ✗ take build<br>➜  build git:(main) ✗ cmake ../                               <br>-- The C compiler identification is GNU 10.2.1<br>-- The CXX compiler identification is GNU 10.2.1<br>-- Detecting C compiler ABI info<br>-- Detecting C compiler ABI info - done<br>-- Check for working C compiler: /usr/bin/cc - skipped<br>-- Detecting C compile features<br>-- Detecting C compile features - done<br>-- Detecting CXX compiler ABI info<br>-- Detecting CXX compiler ABI info - done<br>-- Check for working CXX compiler: /usr/bin/c++ - skipped<br>-- Detecting CXX compile features<br>-- Detecting CXX compile features - done<br>-- Performing Test Terminfo_LINKABLE<br>-- Performing Test Terminfo_LINKABLE - Success<br>-- Found Terminfo: /usr/lib/x86_64-linux-gnu/libtinfo.so  <br>-- Found ZLIB: /usr/local/lib/libz.so (found version "1.2.11") <br>-- Configuring done<br>-- Generating done<br>-- Build files have been written to: /home/alberto/Desktop/progetti/llvm/clang-tutor/HelloWorld/build<br>➜  build git:(main) ✗ make                                    <br>Scanning dependencies of target HelloWorld<br>[ 50%] Building CXX object CMakeFiles/HelloWorld.dir/HelloWorld.cpp.o<br>[100%] Linking CXX shared library libHelloWorld.so<br>[100%] Built target HelloWorld<br>➜  build git:(main) ✗ $Clang_DIR/bin/clang -cc1 -load ./libHelloWorld.so -plugin hello-world $CLANG_TUTOR_DIR/test/HelloWorld-basic.cpp<br>error: unable to find plugin 'hello-world'<br>➜  build git:(main) ✗</div><div><br></div><div>Any idea on how to solve it? I think a little github repo with an example on how to do it as standalone project would be beneficial to others as well.</div><div><br></div><div>Thanks a lot for all</div><div>Alberto<br></div><div><br></div><div>[1] <a href="https://clang.llvm.org/docs/ClangPlugins.html" rel="noreferrer" target="_blank">https://clang.llvm.org/docs/ClangPlugins.html</a><br></div><div>[2] <a href="https://github.com/banach-space/clang-tutor" rel="noreferrer" target="_blank">https://github.com/banach-space/clang-tutor</a></div></div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">Il giorno ven 17 dic 2021 alle ore 01:11 Min-Yih Hsu <<a href="mailto:minyihh@uci.edu" rel="noreferrer" target="_blank">minyihh@uci.edu</a>> ha scritto:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><div>It’s a lot easier to load custom pass plugins into clang with the new PassManager actually:<div>```</div><div>clang -fpass-plugin=<path to plugin> ...</div><div>```</div><div>Note that <path to plugin> needs to be an absolute path.</div><div><br></div><div>-Min<br><div><div><div><br><blockquote type="cite"><div>On Dec 17, 2021, at 6:38 AM, Alberto Barbaro via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a>> wrote:</div><br><div><div dir="auto">Hi all,<div dir="auto">Few days ago I have discovered the symcc[1] project. This project, via an llvm pass, is able to modify the IR code and to inject calls to a backend which allows symbolic execution. I can use it with clang/llvm 11 but not with the version 13. </div><div dir="auto"><br></div><div dir="auto">I was wondering if maybe the new pass manager uses llvm passes in a different way .. so I have created a small pass which injects a call to printf in each function and I'm able to use it via opt. Now my question is: is it possible to run the same pass via clang and just obtain the modified IR code? I'd like to avoid to use opt if not mandatory. Is it possible to do it or the new pass manager forces me to use opt?</div><div dir="auto"><br></div><div dir="auto">How would you fix this situation in symcc?</div><div dir="auto"><br></div><div dir="auto">If someone could tell me how to load a ModulePass in clang-13 would be great.</div><div dir="auto"><br></div><div dir="auto">Thanks a lot</div><div dir="auto">Alberto</div><div dir="auto"><br></div><div dir="auto">[1] <a href="https://github.com/eurecom-s3/symcc" rel="noreferrer noreferrer" target="_blank">https://github.com/eurecom-s3/symcc</a><br><div dir="auto"><br></div></div></div>
_______________________________________________<br>LLVM Developers mailing list<br><a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br><a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br></div></blockquote></div><br></div></div></div></div></blockquote></div>
</div></blockquote></div><br></div></div></blockquote></div>
</div></blockquote></div><br></div>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" rel="noreferrer" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>
</blockquote></div>
</blockquote></div>