Hi,
I have a question with respect to relating the alloca’s of local variables in the LLVM bit-
code to their actual declarations in the source code. Consider for instance the following
declaration of a variable called ‘digest’ within a loop (iteration local variable):
int main()
{
…
for (i=0; i < n; i++)
{
unsigned char digest[16];
…
}
}
The LLVM bit-code looks as follows:
define i32 @main(i32 %argc, i8** %argv) noreturn nounwind {
entry:
…
%digest = alloca [16 x i8], align 1 ; <[16 x i8]*> [#uses=2]
…
for.cond:
…
}
What I would like to is to transform the code above, for certain selective loops,
to pre-allocate n copies of digest, using alloca’s or malloc/free before the loop.
I would like modify clang to detect and annotate variables like digest as being local
to a loop/compound statement, which can then be used by some pass in LLVM’s opt to
find the corresponding alloca and apply the transformation above. Right now, one crude
way of doing this would be to add calls to annotation functions at places where the
variables are declared, by detecting VarDecl instances in clang. The later pass in opt can then
parse the annotation function and figure out the nearest enclosing loop to do the pre-allocation
transformation. Is there is better/easier way to do this ?
thanks,
Prakash
Hi Prakash,
Hi,
I have a question with respect to relating the alloca's of local variables
in the LLVM bit-
code to their actual declarations in the source code. Consider for instance
the following
declaration of a variable called 'digest' within a loop (iteration local
variable):
int main()
{
...
for (i=0; i < n; i++)
{
unsigned char digest[16];
...
}
}
The LLVM bit-code looks as follows:
define i32 @main(i32 %argc, i8** %argv) noreturn nounwind {
entry:
...
%digest = alloca [16 x i8], align 1 ; <[16 x i8]*> [#uses=2]
...
for.cond:
...
}
What I would like to is to transform the code above, for certain selective
loops,
to pre-allocate n copies of digest, using alloca's or malloc/free before
the loop.
I would like modify clang to detect and annotate variables like digest as
being local
to a loop/compound statement, which can then be used by some pass in LLVM's
opt to
find the corresponding alloca and apply the transformation above. Right now,
one crude
way of doing this would be to add calls to annotation functions at places
where the
variables are declared, by detecting VarDecl instances in clang. The later
pass in opt can then
parse the annotation function and figure out the nearest enclosing loop to
do the pre-allocation
transformation. Is there is better/easier way to do this ?
In general, this seems like something you should do solely on the
basis of the LLVM IR, not at the Clang AST level. The backend already
has lots of infrastructure for dealing with loops, although I'm not
personally familiar with it. You might try mailing llvm-dev instead.
- Daniel