我正在为一些基本的循环转换练习做 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:
当我想展开 for.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;
};