[LLVMdev] Get the loop trip count variable

679 views
Skip to first unread message

Zheng Wang

unread,
Apr 5, 2010, 4:19:11 PM4/5/10
to llv...@cs.uiuc.edu
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.

Cheers,
Zheng
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Eli Friedman

unread,
Apr 5, 2010, 4:51:31 PM4/5/10
to Zheng Wang, llv...@cs.uiuc.edu
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

Zheng Wang

unread,
Apr 6, 2010, 6:15:27 AM4/6/10
to Eli Friedman, llv...@cs.uiuc.edu
Hi 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

Jeffrey Yasskin

unread,
Apr 6, 2010, 11:49:48 AM4/6/10
to Zheng Wang, llv...@cs.uiuc.edu
You'll have to look at the debug information, if it's still present.
See http://llvm.org/docs/SourceLevelDebugging.html. This is probably
not what you want to do though. The loop trip count may not even be a
simple variable. You might be able to transform the IR to record the
trip count somewhere, but the source is harder.

Zheng Wang

unread,
Apr 6, 2010, 12:47:23 PM4/6/10
to Zakk, llv...@cs.uiuc.edu
Thanks a lot for your guys' help!!!

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

Zheng Wang

unread,
Apr 6, 2010, 1:47:25 PM4/6/10
to Zakk, llv...@cs.uiuc.edu
Sorry, I could not the the loop trip count with getTripCount(). I used
a simple program as a test case:

------------------------------------------------------
#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?

Eli Friedman

unread,
Apr 6, 2010, 3:41:35 PM4/6/10
to Zheng Wang, llv...@cs.uiuc.edu
On Tue, Apr 6, 2010 at 10:47 AM, Zheng Wang <jason...@gmail.com> wrote:
> Sorry, I could not the the loop trip count with getTripCount(). I used
> a simple program as a test case:
>
> ------------------------------------------------------
> #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

You're likely to get bad results if you don't run mem2reg.

-Eli

Andreas Neustifter

unread,
Apr 7, 2010, 7:29:38 AM4/7/10
to Eli Friedman, llv...@cs.uiuc.edu
Hi,

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

Zheng Wang

unread,
Apr 7, 2010, 10:15:45 AM4/7/10
to Andreas Neustifter, llv...@cs.uiuc.edu
Hello Andreas,

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

_______________________________________________

Andreas Neustifter

unread,
Apr 7, 2010, 10:44:07 AM4/7/10
to Zheng Wang, llvmdev@cs.uiuc.edu Mailing List
Hi!

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

Reply all
Reply to author
Forward
0 new messages