================
@@ -6666,18 +6651,47 @@ OpenMPIRBuilder::tileLoops(DebugLoc DL, 
ArrayRef<CanonicalLoopInfo *> Loops,
     Value *Shift =
         Builder.CreateAdd(Scale, TileLoop->getIndVar(), {}, /*HasNUW=*/true);
     OrigIndVar->replaceAllUsesWith(Shift);
+    ShiftValues.push_back(Shift);
+  }
+
+  // Build a validity predicate: for each tiled dimension, check that the
+  // computed original index is within the original trip count. This guards
+  // against executing out-of-bounds iterations in the remainder tile.
+  Value *Pred = nullptr;
+  for (int i = 0; i < NumLoops; ++i) {
+    Value *DimPred = Builder.CreateICmpULT(ShiftValues[i], OrigTripCounts[i],
+                                           "omp_tile" + Twine(i) + 
".inbounds");
+    Pred = Pred ? Builder.CreateAnd(Pred, DimPred) : DimPred;
   }
 
+  // Insert the conditional guard: split the body flow so that out-of-bounds
+  // iterations skip directly to a merge block before the tile latch.
+  BasicBlock *TileBodyBB = Builder.GetInsertBlock();
+  Instruction *BodyTerm = TileBodyBB->getTerminator();
+  BasicBlock *BodySucc = cast<UncondBrInst>(BodyTerm)->getSuccessor(0);
+  BasicBlock *TileLatch = Result.back()->getLatch();
+
+  BasicBlock *MergeBB =
+      BasicBlock::Create(F->getContext(), "omp_tile.body.merge", F, TileLatch);
+  Builder.SetInsertPoint(MergeBB);
+  Builder.CreateBr(TileLatch);
+
+  // Redirect the body chain's exit from the tile latch to the merge block.
+  for (BasicBlock *PredBB : 
llvm::make_early_inc_range(predecessors(TileLatch)))
+    if (PredBB != MergeBB)
+      PredBB->getTerminator()->replaceUsesOfWith(TileLatch, MergeBB);
+
+  // Replace the tile body's unconditional branch with a conditional one.
+  BodyTerm->eraseFromParent();
+  Builder.SetInsertPoint(TileBodyBB);
+  Builder.CreateCondBr(Pred, BodySucc, MergeBB);
+
   // Remove unused parts of the original loops.
   removeUnusedBlocksFromParent(OldControlBBs);
 
   for (CanonicalLoopInfo *L : Loops)
     L->invalidate();
 
-#ifndef NDEBUG
-  for (CanonicalLoopInfo *GenL : Result)
-    GenL->assertOK();
-#endif
----------------
Meinersbur wrote:

This is here for a reason

https://github.com/llvm/llvm-project/pull/191114
_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to