Hey Xiaoming, Thanks for those source codes. Can you please explain what this llvm-memory-profiling.patch does ?<br><br>Thanks,<br>Himanshu<br><br><div class="gmail_quote">On Wed, Sep 28, 2011 at 6:49 AM, xiaoming gu <span dir="ltr"><<a href="mailto:xiaoming.gu@gmail.com">xiaoming.gu@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Hi, Himanshu. I once wrote an LLVM IR-based memory profiling pass. Basically, I followed the code for EdgeProfiling. The source code is enclosed here, which worked with LLVM 2.8. Hope it is helpful. <div>
<br></div><div> MemoryProfiling.cpp---the instrumentation pass, which inserts profiling function calls into the original program </div>
<div> MemoryProfiling.c---the profiling library containing the profiling calls</div><div> llvm-memory-profiling.patch---the other modifications<br> notes.txt---some information collected when I was working on this profiling pass</div>
<div><br></div><font color="#888888"><div>Xiaoming</div></font><div><div></div><div class="h5"><div><br><div class="gmail_quote">On Tue, Sep 27, 2011 at 7:13 PM, Himanshu Shekhar <span dir="ltr"><<a href="mailto:imhimanshu91@gmail.com" target="_blank">imhimanshu91@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 class="gmail_quote">Hey John,<br>Thank you for the detailed reply.<br>I tried to figure out myself which IR should I use for my purpose ( Clang's Abstract Syntax Tree (AST) or
LLVM's SSA Intermediate Representation (IR). ), but couldn't really figure out which one to use.<br>Here is what I m trying to do.<br>Given any C/C++ program (like the one given below), I am trying to insert calls to some function, before and after <b>every instruction that reads/writes to/from memory</b>. For example consider the below C++ program ( Account.cpp)<br>
/***********************************************************/<br><br>#include <stdio.h><br><br>class Account {<br> int balance;<br> <br>public:<br> Account(int b)<br> {<br> balance = b; <br> }<br> ~Account(){ }<br>
<br> int read() {<br> int r; <br> r = balance; <br> return r;<br> }<br><br> void deposit(int n) { <br> balance = balance + n; <br> }<br><br> void withdraw(int n) {<br> int r = read(); <br>
balance = r - n; <br> }<br>};<br><br>int main (){ <br> Account* a = new Account(10); <br> a->deposit(1);<br> a->withdraw(2); <br> delete a; <br>}<br><br>/***********************************************************/<br>
So after the instrumentation my program should look like :<br><br>/***********************************************************/<br><br>#include <stdio.h><br><br>class Account {<br> int balance;<br> <br>public:<br>
Account(int b)<br>
{<br> balance = b; <br> }<br> ~Account(){ }<br><br> int read() {<br> int r; <br> foo();<br> r = balance;<br> foo(); <br> return r;<br> }<br><br> void deposit(int n) { <br> foo(); <br>
balance = balance + n;<br> foo(); <br> }<br><br> void withdraw(int n) {<br> foo();<br> int r = read();<br> foo();<br> foo(); <br> balance = r - n;<br> foo(); <br> }<br>
};<br>
<br>int main (){ <br> Account* a = new Account(10); <br> a->deposit(1);<br>
a->withdraw(2); <br> delete a; <br>}<br><br>/***********************************************************/<br>where <b>foo() </b>may be any function like get the current system time or increment a counter .. so on. I understand that to insert function like above I will have to first get the IR and then run an instrumentation pass on the IR which will insert such calls into the IR, but I don't really know how to achieve it. Please suggest me with examples how to go about it.<br>
Also I understand that once I compile the program into the IR, it would be really difficult to get 1:1 mapping between my original program and the instrumented IR. So, is it possible to reflect the changes made in the IR ( because of instrumentation ) into the original program.<br>
<br>In order to get started with LLVM pass and how to make one on my own, I looked at an example of a pass that adds run-time checks to LLVM IR loads
and stores, the SAFECode's load/store instrumentation pass
(<a href="http://llvm.org/viewvc/llvm-project/safecode/trunk/include/safecode/LoadStoreChecks.h?view=markup" target="_blank">http://llvm.org/viewvc/llvm-project/safecode/trunk/include/safecode/LoadStoreChecks.h?view=markup</a>
and
<a href="http://llvm.org/viewvc/llvm-project/safecode/trunk/lib/InsertPoolChecks/LoadStoreChecks.cpp?view=markup" target="_blank">http://llvm.org/viewvc/llvm-project/safecode/trunk/lib/InsertPoolChecks/LoadStoreChecks.cpp?view=markup</a>). But I couldn't figure out how to run this pass. Please give me steps how to run this pass on some program say the above Account.cpp.<br>
<br>Thanks,<br><font color="#888888">Himanshu</font><div><div></div><div><div><div></div><div><br><br><br><br><div class="gmail_quote">On Fri, Sep 23, 2011 at 11:13 PM, John Criswell <span dir="ltr"><<a href="mailto:criswell@illinois.edu" target="_blank">criswell@illinois.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div bgcolor="#FFFFFF" text="#000000"><div>
On 9/23/11 12:24 PM, Himanshu Shekhar wrote:
<blockquote type="cite">
I just read that LLVM project could be used to do static analysis
on C/C++ codes using the analyzer Clang which the front end of
LLVM. I wanted to know if it is possible to extract all the
accesses to memory(variables, local as well as global) in the
source code using LLVM.
<br>
</blockquote>
<br></div>
When doing analysis with Clang and LLVM, you first must make a
choice about which IR to use: Clang's Abstract Syntax Tree (AST) or
LLVM's SSA Intermediate Representation (IR). Clang takes source
code and converts it into an AST; it later takes the AST and
converts it to LLVM IR. LLVM then performs mid-level compiler
analysis and optimization on code in LLVM IR form and then
translates from LLVM IR to native code.<br>
<br>
Clang ASTs will give you much higher level information than LLVM
IR. On the other hand, LLVM IR is probably easier to work with and
is programming language agnostic.<br>
<br>
You might want to read about the LLVM Language Reference Manual
(<a href="http://llvm.org/docs/LangRef.html" target="_blank">http://llvm.org/docs/LangRef.html</a>) to get a feel of whether it is
suitable for your analysis. There may be a similar document for
Clang, but I'm not familiar with it since I haven't worked with
Clang ASTs myself.<div><br>
<br>
<blockquote type="cite">Is there any inbuilt library present in LLVM which I
could use to extract this information.
If not please suggest me how to write functions to do the
same.(existing source code, reference, tutorial, example...)
<br>
</blockquote>
<br></div>
It is easy to write an LLVM pass that plugs into the opt tool that
searches for explicit accesses to memory. The LLVM load and store
instructions access memory (similar to how loads and stores are used
to access memory in a RISC instruction set). That said, it is not
clear whether this is what you want to do. Some source-level
variables are translated into one or more SSA virtual registers, so
you'll never see a load or store to them (as they may never exist in
memory but only in registers). Additionally, some loads and stores
to memory are not visible at the LLVM IR level. For example, loads
and stores to stack spill slots are not visible at the LLVM IR level
because they're only created during code generation (and
technically, they're generated in a third IR called Machine
Instructions that is used specifically for code generation).<div><br>
<br>
<br>
<blockquote type="cite">Of what i studied is, I need to first convert the
source code into LLVM IR and then make an instrumenting pass which
would go over this bitcode file and insert calls to do the
analysis, but don't know exactly how to do it.<br clear="all">
</blockquote>
<br></div>
The first thing you need to do is figure out which representation of
the program (Clang ASTs, LLVM IR, LLVM's code generation IR) is the
best for solving your particular problem. If you want, you can
provide more details on what you're trying to do; people on the list
can then provide feedback on which representation is most suitable
for what you want to do.<br>
<br>
If you decide to work with LLVM IR, I then recommend reading the
"How to Write an LLVM Pass" document
(<a href="http://llvm.org/docs/WritingAnLLVMPass.html" target="_blank">http://llvm.org/docs/WritingAnLLVMPass.html</a>) as well as the
Programmer's Guide (<a href="http://llvm.org/docs/ProgrammersManual.html" target="_blank">http://llvm.org/docs/ProgrammersManual.html</a>).
Doxygen is also valuable (<a href="http://llvm.org/doxygen/" target="_blank">http://llvm.org/doxygen/</a>).<br>
<br>
For an example of a pass that adds run-time checks to LLVM IR loads
and stores, look at SAFECode's load/store instrumentation pass
(<a href="http://llvm.org/viewvc/llvm-project/safecode/trunk/include/safecode/LoadStoreChecks.h?view=markup" target="_blank">http://llvm.org/viewvc/llvm-project/safecode/trunk/include/safecode/LoadStoreChecks.h?view=markup</a>
and
<a href="http://llvm.org/viewvc/llvm-project/safecode/trunk/lib/InsertPoolChecks/LoadStoreChecks.cpp?view=markup" target="_blank">http://llvm.org/viewvc/llvm-project/safecode/trunk/lib/InsertPoolChecks/LoadStoreChecks.cpp?view=markup</a>).
It's about as simple as an instrumentation pass gets.<br>
<br>
-- John T.<br>
<blockquote type="cite"><div><br>
Please suggest me how to go about it .<br>
thanks <br>
himanshu<br>
-- <br>
<br>
<br>
<br>
<fieldset></fieldset>
<br>
</div><pre>_______________________________________________
LLVM Developers mailing list
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a>
</pre>
</blockquote>
<br>
</div>
</blockquote></div><br><br clear="all"><br></div></div><font color="#888888"></font><br></div></div></div><span style="font-family:arial,helvetica,sans-serif"></span><br>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:LLVMdev@cs.uiuc.edu" target="_blank">LLVMdev@cs.uiuc.edu</a> <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
<a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
<br></blockquote></div><br></div>
</div></div></blockquote></div><br><br clear="all"><br><br>