<div dir="ltr"><br><div>This example is taken straight out of official documentation, yet it does not quite work. Did anyone else face such issue with LLVM function pass? Basically, the pass does not get triggered for external library function calls, e.g. malloc(), puts(); however  behaves correctly for user defined ones.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On 15 April 2017 at 15:42, Dipanjan Das <span dir="ltr"><<a href="mailto:mail.dipanjan.das@gmail.com" target="_blank">mail.dipanjan.das@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br clear="all"><div><div>I am learning to write LLVM pass by trying to reproduce [hello world][1] example. The pass `hello.cpp` looks like:</div><div><br></div><div>    #include "llvm/Pass.h"</div><div>    #include "llvm/IR/Function.h"</div><div>    #include "llvm/Support/raw_ostream.h"</div><div>    </div><div>    using namespace llvm;</div><div>    </div><div>    namespace {</div><div>      struct Hello : public FunctionPass {</div><div>        static char ID;</div><div>        Hello() : FunctionPass(ID) {}</div><div>    </div><div>        bool runOnFunction(Function &F) override {</div><div>          errs() << "Hello: ";</div><div>          errs().write_escaped(F.<wbr>getName()) << '\n';</div><div>          return false;</div><div>        }</div><div>      };</div><div>    }</div><div>    </div><div>    char Hello::ID = 0;9</div><div>    static RegisterPass<Hello> X("hello", "Hello World Pass", false, false);</div><div><br></div><div>The sample program `world.c` looks like:</div><div><br></div><div>    #include <stdio.h></div><div>    </div><div>    int main() {</div><div>    <span class="m_-3197094866711786445gmail-Apple-tab-span" style="white-space:pre-wrap">        </span>printf("Hello World\n");</div><div>    <span class="m_-3197094866711786445gmail-Apple-tab-span" style="white-space:pre-wrap">    </span>return 0;</div><div>    }</div><div><br></div><div>The program is compiled using the following command line: `clang world.c -c -emit-llvm -O3 -o world.bc`</div><div><br></div><div>The bitcode produced by `llvm-dis` looks like:</div><div><br></div><div>    ; ModuleID = 'src/hello.bc'</div><div>    target datalayout = "e-m:e-i64:64-f80:128-n8:16:<wbr>32:64-S128"</div><div>    target triple = "x86_64-unknown-linux-gnu"</div><div>    </div><div>    @str = private unnamed_addr constant [12 x i8] c"Hello World\00"</div><div>    </div><div>    ; Function Attrs: nounwind uwtable</div><div>    define i32 @main() #0 {</div><div>      %puts = tail call i32 @puts(i8* getelementptr inbounds ([12 x i8], [12 x i8]* @str, i64 0, i64 0))</div><div>      ret i32 0</div><div>    }</div><div>    </div><div>    ; Function Attrs: nounwind</div><div>    declare i32 @puts(i8* nocapture) #1</div><div>    </div><div>    attributes #0 = { nounwind uwtable "disable-tail-calls"="false" "less-precise-fpmad"="false" "no-frame-pointer-elim"="<wbr>false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"=<wbr>"8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,<wbr>+sse,+sse2" "unsafe-fp-math"="false" "use-soft-float"="false" }</div><div>    attributes #1 = { nounwind }</div><div>    </div><div>    !llvm.ident = !{!0}</div><div>    </div><div>    !0 = !{!"clang version 3.8.0 (tags/RELEASE_380/final)"}</div><div><br></div><div>When I run the pass on the bitcode: `opt -load hello/libhello.so -hello src/world.bc > /dev/null`, the output is:</div><div><br></div><div>    Hello: main</div><div><br></div><div>However, the [tutorial][2] claims that the output should have been:</div><div><br></div><div>    Hello: __main</div><div>    Hello: puts</div><div>    Hello: main</div><div><br></div><div>Why does my pass not get triggered for the first two functions? </div><div><br></div><div>  [1]: <a href="http://releases.llvm.org/3.8.0/docs/WritingAnLLVMPass.html#quick-start-writing-hello-world" target="_blank">http://releases.llvm.org/3.8.<wbr>0/docs/WritingAnLLVMPass.html#<wbr>quick-start-writing-hello-<wbr>world</a></div><div>  [2]: <a href="http://releases.llvm.org/3.8.0/docs/WritingAnLLVMPass.html#running-a-pass-with-opt" target="_blank">http://releases.llvm.org/3.8.<wbr>0/docs/WritingAnLLVMPass.html#<wbr>running-a-pass-with-opt</a></div></div><span class="HOEnZb"><font color="#888888"><div><br></div>-- <br><div class="m_-3197094866711786445gmail_signature"><div dir="ltr"><span><div><div dir="ltr"><p>Thanks & Regards,</p>
<div>Dipanjan</div></div></div></span></div></div>
</font></span></div>
</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><span><div><div dir="ltr"><p>Thanks & Regards,</p>
<div>Dipanjan</div></div></div></span></div></div>
</div>