<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.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="gmail-Apple-tab-span" style="white-space:pre"> </span>printf("Hello World\n");</div><div> <span class="gmail-Apple-tab-span" style="white-space:pre"> </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: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"="false" "no-infs-fp-math"="false" "no-nans-fp-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+fxsr,+mmx,+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">http://releases.llvm.org/3.8.0/docs/WritingAnLLVMPass.html#quick-start-writing-hello-world</a></div><div> [2]: <a href="http://releases.llvm.org/3.8.0/docs/WritingAnLLVMPass.html#running-a-pass-with-opt">http://releases.llvm.org/3.8.0/docs/WritingAnLLVMPass.html#running-a-pass-with-opt</a></div></div><div><br></div>-- <br><div class="gmail_signature"><div dir="ltr"><span><div><div dir="ltr"><p>Thanks & Regards,</p>
<div>Dipanjan</div></div></div></span></div></div>
</div>