[llvm-dev] How to make the induction variable local when extracting a loop into a function ?

Maxime Mogé via llvm-dev llvm-dev at lists.llvm.org
Wed Jun 6 08:33:50 PDT 2018


Hi all,

I have a C++ code with the following loop nest
for (int i = 0; i < ni; i++) {
     for (int j = 0; j < nj; j++) {
         A[i] *= j;
     }
}
and I want to extract each of these two loops into a separate function 
with LLVM.
What I would like is an outerloop function doing the loop over i and 
calls to innerloop function, and an innerloop function doing the loop 
over j.

I tried to do this using CodeExtractor, but I encountered an issue : it 
creates functions taking the induction variables i and j as parameters.
define private void @outer_loop(i32* %i, i32* %ni, i32* %j, i32* %nj, 
double** %A)
define private void @inner_loop(i32* %j, i32* %nj, i32* %i, double** %A)

The problem here is that j is passed by address to both functions when I 
expect it to be local to the inner_loop function, or at least be passed 
by value.
i.e. I want the functions to look like this :
define private void @outer_loop(i32* %ni, i32* %nj, double** %A){
newFuncRoot:
   %i = alloca i32
   br label %for.cond
...
}
define private void @inner_loop(i32* %nj, i32* %i, double** %A){
newFuncRoot:
   %j = alloca i32
   br label %for.cond
...
}

This is very problematic for me as I want to execute chunks of the outer 
loop in parallel, and this will lead to errors since I will get a race 
condition on j.

Is there a way to make the induction variable local when extracting a 
Loop into a Function ?

Maxime



More information about the llvm-dev mailing list