1

I am doing llvm for some basic loop transformation practice. The target loop I want to transform is as following:

int main ()
{
    int j=0,i=0;
    int x[100][5000] = {0};  

    for (i = 0; i < 100; i = i+1){
        for (j = 0; j < 5000; j = j+1){
            x[i][j] = 2 * x[i][j];
        }       
    }
   return 0;
}

The IR of this code has nested loop structure goes like:

entry:
 ....
for.cond:
 ....
for.body:
 ....
for.cond1:
 ....
for.body3: 
 ....
for.inc:
 ....
for.end:

When I want to unroll the inner loop which is for.cond1, for.body3 and for.inc these three parts. I first split the for.body3 at the instruction branch, and then want to insert new unrolled block between them:

  %3 = load i32* %j, align 4
  %idxprom = sext i32 %3 to i64
  %4 = load i32* %i, align 4
  %idxprom4 = sext i32 %4 to i64
  %arrayidx = getelementptr inbounds [5000 x [100 x i32]]* %x, i32 0, i64 %idxprom4
  %arrayidx5 = getelementptr inbounds [100 x i32]* %arrayidx, i32 0, i64 %idxprom
  %5 = load i32* %arrayidx5, align 4
  %mul = mul nsw i32 2, %5
  %6 = load i32* %j, align 4
  %idxprom6 = sext i32 %6 to i64
  %7 = load i32* %i, align 4
  %idxprom7 = sext i32 %7 to i64
  %arrayidx8 = getelementptr inbounds [5000 x [100 x i32]]* %x, i32 0, i64 %idxprom7
  %arrayidx9 = getelementptr inbounds [100 x i32]* %arrayidx8, i32 0, i64 %idxprom6
  store i32 %mul, i32* %arrayidx9, align 4

  // insert new blocks here

  br label %for.inc

But when I give the insrtuction in my pass, I got error.

my instruction:

BasicBlock *bb_new = bb->splitBasicBlock(inst_br);

the error:

Assertion (`HasInsideLoopSuccs && "Loop block has no in-loop successors!)

Can anyone familiar with llvm tell me what is the problem? Or do I have other ways to split the block and insert unroll ones?

The HasInsideLoopSuccs is being set at the following LoopInfoImpl.h

// Check the individual blocks.
for ( ; BI != BE; ++BI) {
BlockT *BB = *BI;
bool HasInsideLoopSuccs = false;
bool HasInsideLoopPreds = false;
SmallVector<BlockT *, 2> OutsideLoopPreds;

typedef GraphTraits<BlockT*> BlockTraits;
for (typename BlockTraits::ChildIteratorType SI =
       BlockTraits::child_begin(BB), SE = BlockTraits::child_end(BB);
     SI != SE; ++SI)
  if (contains(*SI)) {
    HasInsideLoopSuccs = true;
    break;
  }

11/26 add: This is my pass.

class IndependentUnroll : public llvm::LoopPass
{
    public:

    virtual void unroll(llvm::Loop *L){

        for (Loop::block_iterator block = L->block_begin(); block !=
        L->block_end(); block++) {
            BasicBlock *bb = *block;
            /* Handle loop body.  */
            if (string(bb->getName()).find("for.body3") !=string::npos) {
                Instruction *inst = &bb->back();
                    BasicBlock *new_bb = bb->splitBasicBlock(inst);
                    /*Then the code get crashed!*/
            }
        }
    }

IndependentUnroll() : llvm::LoopPass(IndependentUnroll::ID) { }

virtual bool runOnLoop(llvm::Loop *L, llvm::LPPassManager &LPM) {

    if (L->getLoopDepth() == 1){
        unroll(L);
    }
}
static char ID;

};
Chang May
  • 165
  • 1
  • 10

1 Answers1

0

Your inner for-loop is:

for (j = 0; j < 5000; j = i+1) {

Do you mean for the increment to be:

j = j + 1

To avoid the infinite loop?

ScottyP
  • 144
  • 4