<div dir="ltr"><div>Hi, <br><br></div>I suggest you have a look at lib/Transforms/Instrumentation/EdgeProfiling.cpp. This pass inserts counters for every edge in the program. you can probably copy many things from there.<br>
<br><div class="gmail_extra"><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
2. I currently create the global variables holding the counter by<br>
calling "module.getOrInsertGlobal(...)". Will this ensure that the<br>
global starts with the value 0, or do I have to do this via other means?<br></blockquote><div><br></div><div>See EdgeProfiler::runOnModule. There, a global is created by calling new GlobalVariable. It is automatically zero-initialized by virtue of being a global variable. The EdgeProfiler pass also inserts a call to llvm_start_edge_profiling at the beginning of the main function. This function calls atexit to register the handler that will process the counters when the application exits.<br>
</div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
3. How can I add code that is called when the program exits?<br></blockquote><div><br></div><div>See llvm_start_edge_profiling in runtime/libprofile/EdgeProfiling.c<br><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
4. Suppose I want to call a function 'foo()' instead of incrementing a<br>
counter, where 'foo()' is some instrumentation code that I would like to<br>
insert rather than something in the target program. *Ideally*, what I'd<br>
like to be able to do is write foo() in C or C++, compile it to LLVM<br>
bytecode, and then somehow pull that into programs that run my pass. Is<br>
there an easy way to do this (i.e. not using an IRBuilder to recreate<br>
the bytecode or something silly like that), or should I just say "if you<br>
want to use my thing, then compile foo.c into your program so the<br>
definition of foo() is available".<br></blockquote><div><br></div><div>You could define your function foo() in a special runtime, and ensure that it gets linked in during the linking step. Again, I'd have a look at how the EdgeProfiler does this.<br>
<br>Cheers,<br>Jonas<br></div></div></div></div>