[llvm-branch-commits] [llvm] ba0265a - [OpenMPIRBuilder] Various changes required for tileLoops.
Michael Kruse via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Fri Dec 11 09:42:25 PST 2020
Author: Michael Kruse
Date: 2020-12-11T11:37:45-06:00
New Revision: ba0265a8d86575f4f0d893555c87bdfd944742ce
URL: https://github.com/llvm/llvm-project/commit/ba0265a8d86575f4f0d893555c87bdfd944742ce
DIFF: https://github.com/llvm/llvm-project/commit/ba0265a8d86575f4f0d893555c87bdfd944742ce.diff
LOG: [OpenMPIRBuilder] Various changes required for tileLoops.
Extract some changes not directly related to tileLoops out of D92974:
* Refactor `createLoopSkeleton` out of `createCanonicalLoop`.
* Introduce `ComputeIP` parameter to the `createCanonicalLoop` overload inserts instructions to compute the trip count. Specifying the location is necessary to make these instructions appear before the outermost loop of a loop nest that is tiled.
* Introduce `Name` parameter to `createCanonicalLoop`. This can help better understanding the origin of values of basic blocks with many loops. The default value is "loop" instead of "for" which could be confused with the "for directive" (aka worksharing-loop) and does not apply to Fortran.
* Remove `CanonicalLoopInfo::eraseFromParent` which is currently unused and untested and was added in anticipation to be used by `tileLoops`. `eraseFromParent` has shown to be insufficient when more than a single loop is involved and is replaced by `removeUnusedBlocksFromParent` in D92974.
Reviewed By: SouraVX
Differential Revision: https://reviews.llvm.org/D93088
Added:
Modified:
llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
index fe06c502489d..68df3f2271d9 100644
--- a/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
+++ b/llvm/include/llvm/Frontend/OpenMP/OMPIRBuilder.h
@@ -201,15 +201,19 @@ class OpenMPIRBuilder {
/// convert the logical iteration variable to the loop counter variable in the
/// loop body.
///
- /// \param Loc The insert and source location description.
+ /// \param Loc The insert and source location description. The insert
+ /// location can be between two instructions or the end of a
+ /// degenerate block (e.g. a BB under construction).
/// \param BodyGenCB Callback that will generate the loop body code.
/// \param TripCount Number of iterations the loop body is executed.
+ /// \param Name Base name used to derive BB and instruction names.
///
/// \returns An object representing the created control flow structure which
/// can be used for loop-associated directives.
CanonicalLoopInfo *createCanonicalLoop(const LocationDescription &Loc,
LoopBodyGenCallbackTy BodyGenCB,
- Value *TripCount);
+ Value *TripCount,
+ const Twine &Name = "loop");
/// Generator for the control flow structure of an OpenMP canonical loop.
///
@@ -252,13 +256,20 @@ class OpenMPIRBuilder {
/// and Stop are signed integers.
/// \param InclusiveStop Whether \p Stop itself is a valid value for the loop
/// counter.
+ /// \param ComputeIP Insertion point for instructions computing the trip
+ /// count. Can be used to ensure the trip count is available
+ /// at the outermost loop of a loop nest. If not set,
+ /// defaults to the preheader of the generated loop.
+ /// \param Name Base name used to derive BB and instruction names.
///
/// \returns An object representing the created control flow structure which
/// can be used for loop-associated directives.
CanonicalLoopInfo *createCanonicalLoop(const LocationDescription &Loc,
LoopBodyGenCallbackTy BodyGenCB,
Value *Start, Value *Stop, Value *Step,
- bool IsSigned, bool InclusiveStop);
+ bool IsSigned, bool InclusiveStop,
+ InsertPointTy ComputeIP = {},
+ const Twine &Name = "loop");
/// Modifies the canonical loop to be a statically-scheduled workshare loop.
///
@@ -644,6 +655,28 @@ class OpenMPIRBuilder {
/// \param CriticalName Name of the critical region.
///
Value *getOMPCriticalRegionLock(StringRef CriticalName);
+
+ /// Create the control flow structure of a canonical OpenMP loop.
+ ///
+ /// The emitted loop will be disconnected, i.e. no edge to the loop's
+ /// preheader and no terminator in the AfterBB. The OpenMPIRBuilder's
+ /// IRBuilder location is not preserved.
+ ///
+ /// \param DL DebugLoc used for the instructions in the skeleton.
+ /// \param TripCount Value to be used for the trip count.
+ /// \param F Function in which to insert the BasicBlocks.
+ /// \param PreInsertBefore Where to insert BBs that execute before the body,
+ /// typically the body itself.
+ /// \param PostInsertBefore Where to insert BBs that execute after the body.
+ /// \param Name Base name used to derive BB
+ /// and instruction names.
+ ///
+ /// \returns The CanonicalLoopInfo that represents the emitted loop.
+ CanonicalLoopInfo *createLoopSkeleton(DebugLoc DL, Value *TripCount,
+ Function *F,
+ BasicBlock *PreInsertBefore,
+ BasicBlock *PostInsertBefore,
+ const Twine &Name = {});
};
/// Class to represented the control flow structure of an OpenMP canonical loop.
@@ -693,9 +726,6 @@ class CanonicalLoopInfo {
BasicBlock *Exit;
BasicBlock *After;
- /// Delete this loop if unused.
- void eraseFromParent();
-
public:
/// The preheader ensures that there is only a single edge entering the loop.
/// Code that must be execute before any loop iteration can be emitted here,
@@ -748,6 +778,11 @@ class CanonicalLoopInfo {
return IndVarPHI;
}
+ /// Return the insertion point for user code in the body.
+ OpenMPIRBuilder::InsertPointTy getBodyIP() const {
+ return {Body, Body->begin()};
+ };
+
/// Return the insertion point for user code after the loop.
OpenMPIRBuilder::InsertPointTy getAfterIP() const {
return {After, After->begin()};
diff --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 609184af6ce8..d120eaa17fb7 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -858,44 +858,43 @@ OpenMPIRBuilder::createMaster(const LocationDescription &Loc,
/*Conditional*/ true, /*hasFinalize*/ true);
}
-CanonicalLoopInfo *
-OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc,
- LoopBodyGenCallbackTy BodyGenCB,
- Value *TripCount) {
- BasicBlock *BB = Loc.IP.getBlock();
- BasicBlock *NextBB = BB->getNextNode();
- Function *F = BB->getParent();
+CanonicalLoopInfo *OpenMPIRBuilder::createLoopSkeleton(
+ DebugLoc DL, Value *TripCount, Function *F, BasicBlock *PreInsertBefore,
+ BasicBlock *PostInsertBefore, const Twine &Name) {
+ Module *M = F->getParent();
+ LLVMContext &Ctx = M->getContext();
Type *IndVarTy = TripCount->getType();
// Create the basic block structure.
BasicBlock *Preheader =
- BasicBlock::Create(M.getContext(), "omp_for.preheader", F, NextBB);
+ BasicBlock::Create(Ctx, "omp_" + Name + ".preheader", F, PreInsertBefore);
BasicBlock *Header =
- BasicBlock::Create(M.getContext(), "omp_for.header", F, NextBB);
+ BasicBlock::Create(Ctx, "omp_" + Name + ".header", F, PreInsertBefore);
BasicBlock *Cond =
- BasicBlock::Create(M.getContext(), "omp_for.cond", F, NextBB);
+ BasicBlock::Create(Ctx, "omp_" + Name + ".cond", F, PreInsertBefore);
BasicBlock *Body =
- BasicBlock::Create(M.getContext(), "omp_for.body", F, NextBB);
+ BasicBlock::Create(Ctx, "omp_" + Name + ".body", F, PreInsertBefore);
BasicBlock *Latch =
- BasicBlock::Create(M.getContext(), "omp_for.inc", F, NextBB);
+ BasicBlock::Create(Ctx, "omp_" + Name + ".inc", F, PostInsertBefore);
BasicBlock *Exit =
- BasicBlock::Create(M.getContext(), "omp_for.exit", F, NextBB);
+ BasicBlock::Create(Ctx, "omp_" + Name + ".exit", F, PostInsertBefore);
BasicBlock *After =
- BasicBlock::Create(M.getContext(), "omp_for.after", F, NextBB);
+ BasicBlock::Create(Ctx, "omp_" + Name + ".after", F, PostInsertBefore);
- updateToLocation(Loc);
- Builder.CreateBr(Preheader);
+ // Use specified DebugLoc for new instructions.
+ Builder.SetCurrentDebugLocation(DL);
Builder.SetInsertPoint(Preheader);
Builder.CreateBr(Header);
Builder.SetInsertPoint(Header);
- PHINode *IndVarPHI = Builder.CreatePHI(IndVarTy, 2, "omp_for.iv");
+ PHINode *IndVarPHI = Builder.CreatePHI(IndVarTy, 2, "omp_" + Name + ".iv");
IndVarPHI->addIncoming(ConstantInt::get(IndVarTy, 0), Preheader);
Builder.CreateBr(Cond);
Builder.SetInsertPoint(Cond);
- Value *Cmp = Builder.CreateICmpULT(IndVarPHI, TripCount, "omp_for.cmp");
+ Value *Cmp =
+ Builder.CreateICmpULT(IndVarPHI, TripCount, "omp_" + Name + ".cmp");
Builder.CreateCondBr(Cmp, Body, Exit);
Builder.SetInsertPoint(Body);
@@ -903,16 +902,13 @@ OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc,
Builder.SetInsertPoint(Latch);
Value *Next = Builder.CreateAdd(IndVarPHI, ConstantInt::get(IndVarTy, 1),
- "omp_for.next", /*HasNUW=*/true);
+ "omp_" + Name + ".next", /*HasNUW=*/true);
Builder.CreateBr(Header);
IndVarPHI->addIncoming(Next, Latch);
Builder.SetInsertPoint(Exit);
Builder.CreateBr(After);
- // After all control flow has been created, insert the body user code.
- BodyGenCB(InsertPointTy(Body, Body->begin()), IndVarPHI);
-
// Remember and return the canonical control flow.
LoopInfos.emplace_front();
CanonicalLoopInfo *CL = &LoopInfos.front();
@@ -933,9 +929,43 @@ OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc,
return CL;
}
+CanonicalLoopInfo *
+OpenMPIRBuilder::createCanonicalLoop(const LocationDescription &Loc,
+ LoopBodyGenCallbackTy BodyGenCB,
+ Value *TripCount, const Twine &Name) {
+ BasicBlock *BB = Loc.IP.getBlock();
+ BasicBlock *NextBB = BB->getNextNode();
+
+ CanonicalLoopInfo *CL = createLoopSkeleton(Loc.DL, TripCount, BB->getParent(),
+ NextBB, NextBB, Name);
+ BasicBlock *After = CL->getAfter();
+
+ // If location is not set, don't connect the loop.
+ if (updateToLocation(Loc)) {
+ // Split the loop at the insertion point: Branch to the preheader and move
+ // every following instruction to after the loop (the After BB). Also, the
+ // new successor is the loop's after block.
+ Builder.CreateBr(CL->Preheader);
+ After->getInstList().splice(After->begin(), BB->getInstList(),
+ Builder.GetInsertPoint(), BB->end());
+ After->replaceSuccessorsPhiUsesWith(BB, After);
+ }
+
+ // Emit the body content. We do it after connecting the loop to the CFG to
+ // avoid that the callback encounters degenerate BBs.
+ BodyGenCB(CL->getBodyIP(), CL->getIndVar());
+
+#ifndef NDEBUG
+ CL->assertOK();
+#endif
+ return CL;
+}
+
CanonicalLoopInfo *OpenMPIRBuilder::createCanonicalLoop(
const LocationDescription &Loc, LoopBodyGenCallbackTy BodyGenCB,
- Value *Start, Value *Stop, Value *Step, bool IsSigned, bool InclusiveStop) {
+ Value *Start, Value *Stop, Value *Step, bool IsSigned, bool InclusiveStop,
+ InsertPointTy ComputeIP, const Twine &Name) {
+
// Consider the following
diff iculties (assuming 8-bit signed integers):
// * Adding \p Step to the loop counter which passes \p Stop may overflow:
// DO I = 1, 100, 50
@@ -947,7 +977,9 @@ CanonicalLoopInfo *OpenMPIRBuilder::createCanonicalLoop(
assert(IndVarTy == Stop->getType() && "Stop type mismatch");
assert(IndVarTy == Step->getType() && "Step type mismatch");
- updateToLocation(Loc);
+ LocationDescription ComputeLoc =
+ ComputeIP.isSet() ? LocationDescription(ComputeIP, Loc.DL) : Loc;
+ updateToLocation(ComputeLoc);
ConstantInt *Zero = ConstantInt::get(IndVarTy, 0);
ConstantInt *One = ConstantInt::get(IndVarTy, 1);
@@ -988,7 +1020,8 @@ CanonicalLoopInfo *OpenMPIRBuilder::createCanonicalLoop(
InclusiveStop ? CmpInst::ICMP_ULT : CmpInst::ICMP_ULE, Span, Incr);
CountIfLooping = Builder.CreateSelect(OneCmp, One, CountIfTwo);
}
- Value *TripCount = Builder.CreateSelect(ZeroCmp, Zero, CountIfLooping);
+ Value *TripCount = Builder.CreateSelect(ZeroCmp, Zero, CountIfLooping,
+ "omp_" + Name + ".tripcount");
auto BodyGen = [=](InsertPointTy CodeGenIP, Value *IV) {
Builder.restoreIP(CodeGenIP);
@@ -996,7 +1029,8 @@ CanonicalLoopInfo *OpenMPIRBuilder::createCanonicalLoop(
Value *IndVar = Builder.CreateAdd(Span, Start);
BodyGenCB(Builder.saveIP(), IndVar);
};
- return createCanonicalLoop(Builder.saveIP(), BodyGen, TripCount);
+ LocationDescription LoopLoc = ComputeIP.isSet() ? Loc.IP : Builder.saveIP();
+ return createCanonicalLoop(LoopLoc, BodyGen, TripCount, Name);
}
// Returns an LLVM function to call for initializing loop bounds using OpenMP
@@ -1112,20 +1146,6 @@ CanonicalLoopInfo *OpenMPIRBuilder::createStaticWorkshareLoop(
return CLI;
}
-void CanonicalLoopInfo::eraseFromParent() {
- assert(IsValid && "can only erase previously valid loop cfg");
- IsValid = false;
-
- SmallVector<BasicBlock *, 5> BBsToRemove{Header, Cond, Latch, Exit};
- SmallVector<Instruction *, 16> InstsToRemove;
-
- // Only remove preheader if not re-purposed somewhere else.
- if (Preheader->getNumUses() == 0)
- BBsToRemove.push_back(Preheader);
-
- DeleteDeadBlocks(BBsToRemove);
-}
-
OpenMPIRBuilder::InsertPointTy
OpenMPIRBuilder::createCopyPrivate(const LocationDescription &Loc,
llvm::Value *BufSize, llvm::Value *CpyBuf,
More information about the llvm-branch-commits
mailing list