Index: include/polly/CodeGen/BlockGenerators.h =================================================================== --- include/polly/CodeGen/BlockGenerators.h +++ include/polly/CodeGen/BlockGenerators.h @@ -795,8 +795,13 @@ __isl_keep isl_id_to_ast_expr *IdToAstExp); private: - /// A map from old to new blocks in the region. - DenseMap BlockMap; + /// A map from old to the first new block in the region, that was created to + /// model the old basic block. + DenseMap StartBlockMap; + + /// A map from old to the last new block in the region, that was created to + /// model the old basic block. + DenseMap EndBlockMap; /// The "BBMaps" for the whole region (one for each block). DenseMap RegionMaps; Index: lib/CodeGen/BlockGenerators.cpp =================================================================== --- lib/CodeGen/BlockGenerators.cpp +++ lib/CodeGen/BlockGenerators.cpp @@ -1269,7 +1269,7 @@ BasicBlock *BBCopy) { BasicBlock *BBIDom = DT.getNode(BB)->getIDom()->getBlock(); - BasicBlock *BBCopyIDom = BlockMap.lookup(BBIDom); + BasicBlock *BBCopyIDom = StartBlockMap.lookup(BBIDom); if (BBCopyIDom) DT.changeImmediateDominator(BBCopy, BBCopyIDom); @@ -1327,7 +1327,8 @@ "Only region statements can be copied by the region generator"); // Forget all old mappings. - BlockMap.clear(); + StartBlockMap.clear(); + EndBlockMap.clear(); RegionMaps.clear(); IncompletePHINodeMap.clear(); @@ -1349,8 +1350,10 @@ generateScalarLoads(Stmt, LTS, EntryBBMap, IdToAstExp); for (auto PI = pred_begin(EntryBB), PE = pred_end(EntryBB); PI != PE; ++PI) - if (!R->contains(*PI)) - BlockMap[*PI] = EntryBBCopy; + if (!R->contains(*PI)) { + StartBlockMap[*PI] = EntryBBCopy; + EndBlockMap[*PI] = EntryBBCopy; + } // Iterate over all blocks in the region in a breadth-first search. std::deque Blocks; @@ -1384,7 +1387,8 @@ copyBB(Stmt, BB, BBCopy, RegionMap, LTS, IdToAstExp); // In order to remap PHI nodes we store also basic block mappings. - BlockMap[BB] = BBCopy; + StartBlockMap[BB] = BBCopy; + EndBlockMap[BB] = Builder.GetInsertBlock(); // Add values to incomplete PHI nodes waiting for this block to be copied. for (const PHINodePairTy &PHINodePair : IncompletePHINodeMap[BB]) @@ -1405,9 +1409,10 @@ BasicBlock *ExitBBCopy = SplitBlock(Builder.GetInsertBlock(), &*Builder.GetInsertPoint(), &DT, &LI); ExitBBCopy->setName("polly.stmt." + R->getExit()->getName() + ".exit"); - BlockMap[R->getExit()] = ExitBBCopy; + StartBlockMap[R->getExit()] = ExitBBCopy; + EndBlockMap[R->getExit()] = ExitBBCopy; - BasicBlock *ExitDomBBCopy = BlockMap.lookup(findExitDominator(DT, R)); + BasicBlock *ExitDomBBCopy = EndBlockMap.lookup(findExitDominator(DT, R)); assert(ExitDomBBCopy && "Common exit dominator must be within region; at least the entry node " "must match"); @@ -1417,19 +1422,22 @@ // region control flow by hand after all blocks have been copied. for (BasicBlock *BB : SeenBlocks) { - BasicBlock *BBCopy = BlockMap[BB]; + BasicBlock *BBCopyStart = StartBlockMap[BB]; + BasicBlock *BBCopyEnd = EndBlockMap[BB]; TerminatorInst *TI = BB->getTerminator(); if (isa(TI)) { - while (!BBCopy->empty()) - BBCopy->begin()->eraseFromParent(); - new UnreachableInst(BBCopy->getContext(), BBCopy); + while (!BBCopyEnd->empty()) + BBCopyEnd->begin()->eraseFromParent(); + new UnreachableInst(BBCopyEnd->getContext(), BBCopyEnd); continue; } - Instruction *BICopy = BBCopy->getTerminator(); + Instruction *BICopy = BBCopyEnd->getTerminator(); - ValueMapT &RegionMap = RegionMaps[BBCopy]; - RegionMap.insert(BlockMap.begin(), BlockMap.end()); + ValueMapT &RegionMap = RegionMaps[BBCopyStart]; + RegionMap.insert(StartBlockMap.begin(), StartBlockMap.end()); + ValueMapT &RegionMap2 = RegionMaps[BBCopyEnd]; + RegionMap2.insert(StartBlockMap.begin(), StartBlockMap.end()); Builder.SetInsertPoint(BICopy); copyInstScalar(Stmt, TI, RegionMap, LTS); @@ -1443,7 +1451,7 @@ if (L == nullptr || L->getHeader() != BB || !R->contains(L)) continue; - BasicBlock *BBCopy = BlockMap[BB]; + BasicBlock *BBCopy = StartBlockMap[BB]; Value *NullVal = Builder.getInt32(0); PHINode *LoopPHI = PHINode::Create(Builder.getInt32Ty(), 2, "polly.subregion.iv"); @@ -1456,9 +1464,9 @@ if (!R->contains(PredBB)) continue; if (L->contains(PredBB)) - LoopPHI->addIncoming(LoopPHIInc, BlockMap[PredBB]); + LoopPHI->addIncoming(LoopPHIInc, StartBlockMap[PredBB]); else - LoopPHI->addIncoming(NullVal, BlockMap[PredBB]); + LoopPHI->addIncoming(NullVal, StartBlockMap[PredBB]); } for (auto *PredBBCopy : make_range(pred_begin(BBCopy), pred_end(BBCopy))) @@ -1473,7 +1481,8 @@ // Write values visible to other statements. generateScalarStores(Stmt, LTS, ValueMap, IdToAstExp); - BlockMap.clear(); + StartBlockMap.clear(); + EndBlockMap.clear(); RegionMaps.clear(); IncompletePHINodeMap.clear(); } @@ -1493,7 +1502,7 @@ if (OrigPHI->getParent() != SubR->getExit()) { BasicBlock *FormerExit = SubR->getExitingBlock(); if (FormerExit) - NewSubregionExit = BlockMap.lookup(FormerExit); + NewSubregionExit = StartBlockMap.lookup(FormerExit); } PHINode *NewPHI = PHINode::Create(OrigPHI->getType(), Incoming.size(), @@ -1503,7 +1512,7 @@ // Add the incoming values to the PHI. for (auto &Pair : Incoming) { BasicBlock *OrigIncomingBlock = Pair.first; - BasicBlock *NewIncomingBlock = BlockMap.lookup(OrigIncomingBlock); + BasicBlock *NewIncomingBlock = EndBlockMap.lookup(OrigIncomingBlock); Builder.SetInsertPoint(NewIncomingBlock->getTerminator()); assert(RegionMaps.count(NewIncomingBlock)); ValueMapT *LocalBBMap = &RegionMaps[NewIncomingBlock]; @@ -1580,7 +1589,7 @@ LoopToScevMapT <S) { // If the incoming block was not yet copied mark this PHI as incomplete. // Once the block will be copied the incoming value will be added. - BasicBlock *BBCopy = BlockMap[IncomingBB]; + BasicBlock *BBCopy = EndBlockMap[IncomingBB]; if (!BBCopy) { assert(Stmt.contains(IncomingBB) && "Bad incoming block for PHI in non-affine region"); Index: test/Isl/CodeGen/partial_write_in_region.ll =================================================================== --- /dev/null +++ test/Isl/CodeGen/partial_write_in_region.ll @@ -0,0 +1,60 @@ +; RUN: opt %loadPolly -polly-import-jscop -polly-import-jscop-dir=%S \ +; RUN: -polly-import-jscop-postfix=transformed -polly-codegen \ +; RUN: -S < %s | FileCheck %s +; +; void foo(long A[], float B[]) { +; for (long i = 0; i < 1024; i++) +; if (A[i]) +; B[i]++; +; } + +; CHECK: polly.stmt.bb5: ; preds = %polly.stmt.bb2 +; CHECK-NEXT: %scevgep3 = getelementptr float, float* %B, i64 %polly.indvar +; CHECK-NEXT: %tmp7_p_scalar_ = load float, float* %scevgep3, align 4, !alias.scope !3, !noalias !4 +; CHECK-NEXT: %p_tmp8 = fadd float %tmp7_p_scalar_, 1.000000e+00 +; CHECK-NEXT: %8 = icmp sle i64 %polly.indvar, 9 +; CHECK-NEXT: %polly.Stmt_bb2__TO__bb9_MayWrite2.cond = icmp ne i1 %8, false +; CHECK-NEXT: br i1 %polly.Stmt_bb2__TO__bb9_MayWrite2.cond, label %polly.stmt.bb5.Stmt_bb2__TO__bb9_MayWrite2.partial, label %polly.stmt.bb5.cont + +; CHECK: polly.stmt.bb5.Stmt_bb2__TO__bb9_MayWrite2.partial: ; preds = %polly.stmt.bb5 +; CHECK-NEXT: %polly.access.B4 = getelementptr float, float* %B, i64 %polly.indvar +; CHECK-NEXT: store float %p_tmp8, float* %polly.access.B4, align 4, !alias.scope !3, !noalias !4 +; CHECK-NEXT: br label %polly.stmt.bb5.cont + +; CHECK: polly.stmt.bb5.cont: ; preds = %polly.stmt.bb5, %polly.stmt.bb5.Stmt_bb2__TO__bb9_MayWrite2.partial +; CHECK-NEXT: br label %polly.stmt.bb9.exit + +target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128" + +define void @partial_write_in_region(i64* %A, float* %B) { +bb: + br label %bb1 + +bb1: ; preds = %bb10, %bb + %i.0 = phi i64 [ 0, %bb ], [ %tmp11, %bb10 ] + %exitcond = icmp ne i64 %i.0, 1024 + br i1 %exitcond, label %bb2, label %bb12 + +bb2: ; preds = %bb1 + %tmp = getelementptr inbounds i64, i64* %A, i64 %i.0 + %tmp3 = load i64, i64* %tmp, align 8 + %tmp4 = icmp eq i64 %tmp3, 0 + br i1 %tmp4, label %bb9, label %bb5 + +bb5: ; preds = %bb2 + %tmp6 = getelementptr inbounds float, float* %B, i64 %i.0 + %tmp7 = load float, float* %tmp6, align 4 + %tmp8 = fadd float %tmp7, 1.000000e+00 + store float %tmp8, float* %tmp6, align 4 + br label %bb9 + +bb9: ; preds = %bb2, %bb5 + br label %bb10 + +bb10: ; preds = %bb9 + %tmp11 = add nuw nsw i64 %i.0, 1 + br label %bb1 + +bb12: ; preds = %bb1 + ret void +} Index: test/Isl/CodeGen/partial_write_in_region___%bb1---%bb12.jscop =================================================================== --- /dev/null +++ test/Isl/CodeGen/partial_write_in_region___%bb1---%bb12.jscop @@ -0,0 +1,37 @@ +{ + "arrays" : [ + { + "name" : "MemRef_A", + "sizes" : [ "*" ], + "type" : "i64" + }, + { + "name" : "MemRef_B", + "sizes" : [ "*" ], + "type" : "float" + } + ], + "context" : "{ : }", + "name" : "%bb1---%bb12", + "statements" : [ + { + "accesses" : [ + { + "kind" : "read", + "relation" : "{ Stmt_bb2__TO__bb9[i0] -> MemRef_A[i0] }" + }, + { + "kind" : "read", + "relation" : "{ Stmt_bb2__TO__bb9[i0] -> MemRef_B[i0] }" + }, + { + "kind" : "write", + "relation" : "{ Stmt_bb2__TO__bb9[i0] -> MemRef_B[i0] }" + } + ], + "domain" : "{ Stmt_bb2__TO__bb9[i0] : 0 <= i0 <= 1023 }", + "name" : "Stmt_bb2__TO__bb9", + "schedule" : "{ Stmt_bb2__TO__bb9[i0] -> [i0] }" + } + ] +} Index: test/Isl/CodeGen/partial_write_in_region___%bb1---%bb12.jscop.transformed =================================================================== --- /dev/null +++ test/Isl/CodeGen/partial_write_in_region___%bb1---%bb12.jscop.transformed @@ -0,0 +1,37 @@ +{ + "arrays" : [ + { + "name" : "MemRef_A", + "sizes" : [ "*" ], + "type" : "i64" + }, + { + "name" : "MemRef_B", + "sizes" : [ "*" ], + "type" : "float" + } + ], + "context" : "{ : }", + "name" : "%bb1---%bb12", + "statements" : [ + { + "accesses" : [ + { + "kind" : "read", + "relation" : "{ Stmt_bb2__TO__bb9[i0] -> MemRef_A[i0] }" + }, + { + "kind" : "read", + "relation" : "{ Stmt_bb2__TO__bb9[i0] -> MemRef_B[i0] }" + }, + { + "kind" : "write", + "relation" : "{ Stmt_bb2__TO__bb9[i0] -> MemRef_B[i0] : i0 < 10}" + } + ], + "domain" : "{ Stmt_bb2__TO__bb9[i0] : 0 <= i0 <= 1023 }", + "name" : "Stmt_bb2__TO__bb9", + "schedule" : "{ Stmt_bb2__TO__bb9[i0] -> [i0] }" + } + ] +}