I am wondering whether I can get the variable name of loop trip count in LLVM?
For example,
int NUM;
NUM=atoi(argv[i]);
for (int i=0; i<NUM; i++)
{
...
}
How can I get the corresponding variable name for "NUM"? Then, I can
instrument something in the source code to record the loop trip count
for a given input data set.
BasicBlock* b = L->getHeader();
returns the basicblock of the loop header, but I don't know how to
extract "NUM" from basicblock b.
Cheers,
Zheng
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
If you need to instrument the source, you'd probably be better off
doing this with clang purely at the source level; by the time LLVM
loop analyzers can tell "NUM" is the trip count, the name is likely to
be lost due to optimization.
-Eli
Could you tell me how to extract the variable name?
The reason I want to do it at the IR level is because I have more
information at this stage, such as, constant propagation and back
edges.
Cheers,
Zheng
On 5 April 2010 21:51, Eli Friedman <eli.fr...@gmail.com> wrote:
> On Mon, Apr 5, 2010 at 1:19 PM, Zheng Wang <jason...@gmail.com> wrote:
>> Hello,
>>
>> I am wondering whether I can get the variable name of loop trip count in LLVM?
>>
>> For example,
>>
>> int NUM;
>>
>> NUM=atoi(argv[i]);
>>
>> for (int i=0; i<NUM; i++)
>> {
>> ...
>> }
>>
>> How can I get the corresponding variable name for "NUM"? Then, I can
>> instrument something in the source code to record the loop trip count
>> for a given input data set.
>>
>> BasicBlock* b = L->getHeader();
>>
>> returns the basicblock of the loop header, but I don't know how to
>> extract "NUM" from basicblock b.
>
> If you need to instrument the source, you'd probably be better off
> doing this with clang purely at the source level; by the time LLVM
> loop analyzers can tell "NUM" is the trip count, the name is likely to
> be lost due to optimization.
>
> -Eli
>
--
Best regards,
WANG Zheng
I guess once I am able to get *V* (which probably is a pointer to a
Value object), then, I can instrument some code at the IR level to
dump V. As long as I maintain V at this pass stage, I should be able
to dump the loop trip count. This is true, isn't it?
Basically, what I am going to do is to add a function call before the
loop body, such as:
dumpInfo(FILE_NAME, LINE_NUMBER, VALUE_TYPE, Value)
...
for (int i=0; i<num; i++)
of course, this may have some side effects. If the trip count is a
function call, and I need to rewrite the loop header to avoid calling
the function twice. For example, if the loop header is:
for (int i=0; i<foo(a,b,c); i++)
then I should rewrite it as:
tmp_var = foo(a,b,c);
dumpInfo(...)
for (int i=0; i<tmp_var; i++)
{
...
tmp_var=foo(a,b,c);
}
This looks quite complicated!
Does anyone has any ideal about how easy to insert some basic blocks
and to rewrite the loop header in LLVM?
Cheers,
Zheng
On 6 April 2010 17:27, Zakk <zakk...@gmail.com> wrote:
> Hi, you should run some loop transformation passes(loop rotate, loop
> simplify, -indvar), and then get TripCount.
> loopinfo.h
> /// getTripCount - Return a loop-invariant LLVM value indicating the number
> of
> /// times the loop will be executed. Note that this means that the backedge
> /// of the loop executes N-1 times. If the trip-count cannot be determined,
> /// this returns null.
>
> inline Value *getTripCount() const {
> Canonical loops will end with a 'cmp ne I, V', where I is the incremented
> canonical induction variable and V is the trip count of the loop.
>
> 2010/4/6 Zheng Wang <jason...@gmail.com>
> --
> Best regards,
> Kuan-Hsu
>
> P
>
--
Best regards,
WANG Zheng
------------------------------------------------------
#include <stdio.h>
int getV(int i)
{
return i * 2;
}
int main()
{
int num = 10;
int sum=0;
int i;
for (i=0; i<num; i++)
{
sum += getV(i);
}
return 0;
}
--------------------------------------------------------
2. Then, I translated it to LLVM bitcode by:
llvm-gcc -emit-llvm -c test.c
3. Then, I performed some optimization passes as:
opt -loop-rotate -loops -loopvr -indvars -loopsimplify -f < test.o > n.o
4. I tried to dump the loop trip count by my own pass (cfginst):
opt -load lib/libLLVMCFGInst.so -cfginst < n.o
-----------------------------------------------
But I got the following messages:
Function getV doesn't have loops
Function main has 1 loops
loop_trip_count0 0
could not get the trip count
-------------------------------
My pass looks like:
----------------------------------
virtual bool runOnFunction(Function &F) {
LoopInfo &LI = getAnalysis<LoopInfo>();
CFGInstCounter++;
std::string fname = F.getName();
EscapeString(fname);
cerr << "Function " << fname;
if (LI.empty())
cout << " doesn't have loops" << endl;
else
cout << " has " << LI.end() -
LI.begin() << " loops" << endl;
int ii = 0;
for (LoopInfo::iterator i = LI.begin(), e =
LI.end(); i != e; ++i) {
Loop *TLL = *i;
cout << "loop_trip_count is " <<
TLL->getSmallConstantTripCount();
Value* v = TLL->getTripCount();
if (v != 0)
{
cout << "value=" << *v << endl;
}
else
{
cout << "\n could not get the
trip count" << endl;
}
}
----------------------------
Anything wrong?
You're likely to get bad results if you don't run mem2reg.
-Eli
On 04/05/2010 10:51 PM, Eli Friedman wrote:
> On Mon, Apr 5, 2010 at 1:19 PM, Zheng Wang<jason...@gmail.com> wrote:
>> Hello,
>>
>> I am wondering whether I can get the variable name of loop trip count in LLVM?
>>
>> For example,
>>
>> int NUM;
>>
>> NUM=atoi(argv[i]);
>>
>> for (int i=0; i<NUM; i++)
>> {
>> ...
>> }
>>
>> How can I get the corresponding variable name for "NUM"? Then, I can
>> instrument something in the source code to record the loop trip count
>> for a given input data set.
>>
>> BasicBlock* b = L->getHeader();
>>
>> returns the basicblock of the loop header, but I don't know how to
>> extract "NUM" from basicblock b.
>
> If you need to instrument the source, you'd probably be better off
> doing this with clang purely at the source level; by the time LLVM
> loop analyzers can tell "NUM" is the trip count, the name is likely to
> be lost due to optimization.
You can also use the instrumentation pass
(-insert-optimal-edge-instrumenation) to instrument all your code, this
adds about 10% runtime overhead.
Also with some tweaking the instrumentation pass could be talked into
instrumenting only loop headers so that only the loop counts get
recorded but for a code with a whole lot of loops this would be almost
equivalent to do the optimal instrumentation. (For a loop with only one
block in the body only one counter gets added anyway...)
Andi
Thanks! But I could not find -insert-optimal-edge-instrumenation pass
from "opt -help". Did you mean -insert-edge-profiling?
Cheers,
Zheng
--
Best regards,
WANG Zheng
_______________________________________________
On 07.04.2010, at 16:15, Zheng Wang wrote:
> [...] But I could not find -insert-optimal-edge-instrumenation pass
> from "opt -help". Did you mean -insert-edge-profiling?
Yeah, I meant "-insert-optimal-edge-profiling", sorry.
Andi