Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit f166ea7

Browse files
committed
[UnJ] Add debug messages for why loops are not unrolled. NFC
Adds some cleaned up debug messages from back when I was writing this. Hopefully useful to others (and myself) as to why unroll and jam is not transforming as expected. Differential Revision: https://reviews.llvm.org/D50062 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@338676 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 6e63f1c commit f166ea7

File tree

1 file changed

+55
-18
lines changed

1 file changed

+55
-18
lines changed

lib/Transforms/Utils/LoopUnrollAndJam.cpp

Lines changed: 55 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ llvm::UnrollAndJamLoop(Loop *L, unsigned Count, unsigned TripCount,
181181

182182
// Don't enter the unroll code if there is nothing to do.
183183
if (TripCount == 0 && Count < 2) {
184-
LLVM_DEBUG(dbgs() << "Won't unroll; almost nothing to do\n");
184+
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; almost nothing to do\n");
185185
return LoopUnrollResult::Unmodified;
186186
}
187187

@@ -619,16 +619,28 @@ static bool checkDependencies(SmallVector<Value *, 4> &Earlier,
619619
if (auto D = DI.depends(Src, Dst, true)) {
620620
assert(D->isOrdered() && "Expected an output, flow or anti dep.");
621621

622-
if (D->isConfused())
622+
if (D->isConfused()) {
623+
LLVM_DEBUG(dbgs() << " Confused dependency between:\n"
624+
<< " " << *Src << "\n"
625+
<< " " << *Dst << "\n");
623626
return false;
627+
}
624628
if (!InnerLoop) {
625-
if (D->getDirection(LoopDepth) & Dependence::DVEntry::GT)
629+
if (D->getDirection(LoopDepth) & Dependence::DVEntry::GT) {
630+
LLVM_DEBUG(dbgs() << " > dependency between:\n"
631+
<< " " << *Src << "\n"
632+
<< " " << *Dst << "\n");
626633
return false;
634+
}
627635
} else {
628636
assert(LoopDepth + 1 <= D->getLevels());
629637
if (D->getDirection(LoopDepth) & Dependence::DVEntry::GT &&
630-
D->getDirection(LoopDepth + 1) & Dependence::DVEntry::LT)
638+
D->getDirection(LoopDepth + 1) & Dependence::DVEntry::LT) {
639+
LLVM_DEBUG(dbgs() << " < > dependency between:\n"
640+
<< " " << *Src << "\n"
641+
<< " " << *Dst << "\n");
631642
return false;
643+
}
632644
}
633645
}
634646
}
@@ -716,38 +728,58 @@ bool llvm::isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
716728
if (SubLoopLatch != SubLoopExit)
717729
return false;
718730

719-
if (Header->hasAddressTaken() || SubLoopHeader->hasAddressTaken())
731+
if (Header->hasAddressTaken() || SubLoopHeader->hasAddressTaken()) {
732+
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Address taken\n");
720733
return false;
734+
}
721735

722736
// Split blocks into Fore/SubLoop/Aft based on dominators
723737
BasicBlockSet SubLoopBlocks;
724738
BasicBlockSet ForeBlocks;
725739
BasicBlockSet AftBlocks;
726740
if (!partitionOuterLoopBlocks(L, SubLoop, ForeBlocks, SubLoopBlocks,
727-
AftBlocks, &DT))
741+
AftBlocks, &DT)) {
742+
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Incompatible loop layout\n");
728743
return false;
744+
}
729745

730746
// Aft blocks may need to move instructions to fore blocks, which becomes more
731747
// difficult if there are multiple (potentially conditionally executed)
732748
// blocks. For now we just exclude loops with multiple aft blocks.
733-
if (AftBlocks.size() != 1)
749+
if (AftBlocks.size() != 1) {
750+
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Can't currently handle "
751+
"multiple blocks after the loop\n");
734752
return false;
753+
}
735754

736-
// Check inner loop IV is consistent between all iterations
737-
const SCEV *SubLoopBECountSC = SE.getExitCount(SubLoop, SubLoopLatch);
738-
if (isa<SCEVCouldNotCompute>(SubLoopBECountSC) ||
739-
!SubLoopBECountSC->getType()->isIntegerTy())
740-
return false;
741-
ScalarEvolution::LoopDisposition LD =
742-
SE.getLoopDisposition(SubLoopBECountSC, L);
743-
if (LD != ScalarEvolution::LoopInvariant)
755+
// Check inner loop backedge count is consistent on all iterations of the
756+
// outer loop
757+
auto CheckInnerLoopIterationCountInvariant = [](Loop *SubLoop, Loop *OuterL,
758+
ScalarEvolution &SE) {
759+
BasicBlock *SubLoopLatch = SubLoop->getLoopLatch();
760+
const SCEV *SubLoopBECountSC = SE.getExitCount(SubLoop, SubLoopLatch);
761+
if (isa<SCEVCouldNotCompute>(SubLoopBECountSC) ||
762+
!SubLoopBECountSC->getType()->isIntegerTy())
763+
return false;
764+
ScalarEvolution::LoopDisposition LD =
765+
SE.getLoopDisposition(SubLoopBECountSC, OuterL);
766+
if (LD != ScalarEvolution::LoopInvariant)
767+
return false;
768+
return true;
769+
};
770+
if (!CheckInnerLoopIterationCountInvariant(SubLoop, L, SE)) {
771+
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Inner loop iteration count is "
772+
"not consistent on each iteration\n");
744773
return false;
774+
}
745775

746776
// Check the loop safety info for exceptions.
747777
LoopSafetyInfo LSI;
748778
computeLoopSafetyInfo(&LSI, L);
749-
if (LSI.MayThrow)
779+
if (LSI.MayThrow) {
780+
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; Something may throw\n");
750781
return false;
782+
}
751783

752784
// We've ruled out the easy stuff and now need to check that there are no
753785
// interdependencies which may prevent us from moving the:
@@ -772,14 +804,19 @@ bool llvm::isSafeToUnrollAndJam(Loop *L, ScalarEvolution &SE, DominatorTree &DT,
772804
}
773805
// Keep going
774806
return true;
775-
}))
807+
})) {
808+
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; can't move required "
809+
"instructions after subloop to before it\n");
776810
return false;
811+
}
777812

778813
// Check for memory dependencies which prohibit the unrolling we are doing.
779814
// Because of the way we are unrolling Fore/Sub/Aft blocks, we need to check
780815
// there are no dependencies between Fore-Sub, Fore-Aft, Sub-Aft and Sub-Sub.
781-
if (!checkDependencies(L, ForeBlocks, SubLoopBlocks, AftBlocks, DI))
816+
if (!checkDependencies(L, ForeBlocks, SubLoopBlocks, AftBlocks, DI)) {
817+
LLVM_DEBUG(dbgs() << "Won't unroll-and-jam; failed dependency check\n");
782818
return false;
819+
}
783820

784821
return true;
785822
}

0 commit comments

Comments
 (0)