使用llvm执行循环展开,在分割块时失败

时间:2014-11-24 21:25:54

标签: llvm unroll

我正在为一些基本的循环转换实践做llvm。 我想要转换的目标循环如下:

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;
}

此代码的IR具有嵌套循环结构,如:

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

当我想展开内部循环时,对于.cond1,for.body3和for.inc这三个部分。我首先在指令分支中拆分for.body3,然后想在它们之间插入新的展开块:

  %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

但是当我在通行证中给出推荐时,我得到了错误。

我的指示:

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

错误:

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

任何熟悉llvm的人都可以告诉我这是什么问题吗?或者我是否有其他方法来拆分块并插入展开块?

HasInsideLoopSuccs正在以下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补充:这是我的通行证。

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;

};

1 个答案:

答案 0 :(得分:0)

你的内部for循环是:

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

你的意思是增量为:

j = j + 1

避免无限循环?