Skip to content

Commit 354a42c

Browse files
committed
fixup! [ModuloSchedule] Implement modulo variable expansion for pipelining
1 parent 85175fa commit 354a42c

File tree

6 files changed

+54
-34
lines changed

6 files changed

+54
-34
lines changed

llvm/include/llvm/CodeGen/TargetInstrInfo.h

Lines changed: 14 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -748,19 +748,18 @@ class TargetInstrInfo : public MCInstrInfo {
748748
createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
749749
SmallVectorImpl<MachineOperand> &Cond) = 0;
750750

751-
/// Create a condtion to determine if the remaining trip count represented
752-
/// by the loop counter CounterReg is greater than TC. Some instructions
753-
/// such as comparisons may be inserted at the bottom of MBB. CounterReg
754-
/// must be accessible there.
751+
/// Create a condition to determine if the remaining trip count for a phase
752+
/// is greater than TC. Some instructions such as comparisons may be
753+
/// inserted at the bottom of MBB. The all instructions expanded for the
754+
/// phase must be inserted in MBB before calling this function. RegMap is
755+
/// the map from the original registers to the expanded registers for the
756+
/// phase.
755757
///
756-
/// The definition of the return value is the same as for the variant above.
757-
virtual std::optional<bool>
758-
createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
759-
SmallVectorImpl<MachineOperand> &Cond,
760-
Register CounterReg) {
761-
llvm_unreachable(
762-
"Target didn't implement createTripCountGreaterCondition");
763-
}
758+
/// MBB can also be a predecessor of the prologue block. Then RegMap must be
759+
/// empty and the compared value is the initial value of the trip count.
760+
virtual void createRemainingIterationsGreaterCondition(
761+
int TC, MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &Cond,
762+
DenseMap<unsigned, unsigned> RegMap) = 0;
764763

765764
/// Modify the loop such that the trip count is
766765
/// OriginalTC + TripCountAdjust.
@@ -776,15 +775,9 @@ class TargetInstrInfo : public MCInstrInfo {
776775
/// valid; the loop has been removed.
777776
virtual void disposed() = 0;
778777

779-
/// Return the initial value of the loop counter.
780-
virtual Register getCounterInitReg() {
781-
llvm_unreachable("Target didn't implement getCounterInitReg");
782-
}
783-
784-
/// Return the updated value of the loop counter in the original loop.
785-
virtual Register getCounterUpdatedReg() {
786-
llvm_unreachable("Target didn't implement getCounterUpdatedReg");
787-
}
778+
/// Return true if the target can expand pipelined schedule with modulo
779+
/// variable expansion.
780+
virtual bool isMVEExpanderSupported() = 0;
788781
};
789782

790783
/// Analyze loop L, which must be a single-basic-block loop, and if the

llvm/lib/CodeGen/MachinePipeliner.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -663,9 +663,9 @@ void SwingSchedulerDAG::schedule() {
663663
if (ExperimentalCodeGen && NewInstrChanges.empty()) {
664664
PeelingModuloScheduleExpander MSE(MF, MS, &LIS);
665665
MSE.expand();
666-
}
667-
if (MVECodeGen && NewInstrChanges.empty() &&
668-
ModuloScheduleExpanderMVE::canApply(Loop)) {
666+
} else if (MVECodeGen && NewInstrChanges.empty() &&
667+
LoopPipelinerInfo->isMVEExpanderSupported() &&
668+
ModuloScheduleExpanderMVE::canApply(Loop)) {
669669
ModuloScheduleExpanderMVE MSE(MF, MS, LIS);
670670
MSE.expand();
671671
} else {

llvm/lib/CodeGen/ModuloSchedule.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,9 +2262,8 @@ void ModuloScheduleExpanderMVE::generatePipelinedLoop() {
22622262
Epilog->addSuccessor(NewExit);
22632263

22642264
SmallVector<MachineOperand, 4> Cond;
2265-
LoopInfo->createTripCountGreaterCondition(
2266-
Schedule.getNumStages() + NumUnroll - 2, *Check, Cond,
2267-
LoopInfo->getCounterInitReg());
2265+
LoopInfo->createRemainingIterationsGreaterCondition(
2266+
Schedule.getNumStages() + NumUnroll - 2, *Check, Cond, ValueMapTy());
22682267
TII->insertBranch(*Check, Prolog, NewPreheader, Cond, DebugLoc());
22692268

22702269
// VRMaps map (prolog/kernel/epilog phase#, original register#) to new
@@ -2552,9 +2551,8 @@ void ModuloScheduleExpanderMVE::generateKernel(
25522551

25532552
// If remaining trip count is greater than NumUnroll-1, loop continues
25542553
SmallVector<MachineOperand, 4> Cond;
2555-
LoopInfo->createTripCountGreaterCondition(
2556-
NumUnroll - 1, *NewKernel, Cond,
2557-
KernelVRMap[NumUnroll - 1][LoopInfo->getCounterUpdatedReg()]);
2554+
LoopInfo->createRemainingIterationsGreaterCondition(
2555+
NumUnroll - 1, *NewKernel, Cond, KernelVRMap[NumUnroll - 1]);
25582556
TII->insertBranch(*NewKernel, NewKernel, Epilog, Cond, DebugLoc());
25592557

25602558
LLVM_DEBUG({
@@ -2591,11 +2589,13 @@ void ModuloScheduleExpanderMVE::generateEpilog(
25912589
updateInstrUse(MI, StageNum, EpilogNum, EpilogVRMap, &KernelVRMap);
25922590
}
25932591

2594-
// If there are remaining iterations, they are executed in the original loop
2592+
// If there are remaining iterations, they are executed in the original loop.
2593+
// Instructions related to loop control, such as loop counter comparison,
2594+
// are indicated by shouldIgnoreForPipelining() and are assumed to be placed
2595+
// in stage 0. Thus, the map is for the last one in the kernel.
25952596
SmallVector<MachineOperand, 4> Cond;
2596-
LoopInfo->createTripCountGreaterCondition(
2597-
0, *Epilog, Cond,
2598-
KernelVRMap[NumUnroll - 1][LoopInfo->getCounterUpdatedReg()]);
2597+
LoopInfo->createRemainingIterationsGreaterCondition(
2598+
0, *Epilog, Cond, KernelVRMap[NumUnroll - 1]);
25992599
TII->insertBranch(*Epilog, NewPreheader, NewExit, Cond, DebugLoc());
26002600

26012601
LLVM_DEBUG({

llvm/lib/Target/ARM/ARMBaseInstrInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6841,11 +6841,20 @@ class ARMPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
68416841
llvm_unreachable("Unknown EndLoop");
68426842
}
68436843

6844+
void createRemainingIterationsGreaterCondition(
6845+
int TC, MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &Cond,
6846+
DenseMap<unsigned, unsigned> RegMap) override {
6847+
llvm_unreachable(
6848+
"Target didn't implement createRemainingIterationsGreaterCondition");
6849+
}
6850+
68446851
void setPreheader(MachineBasicBlock *NewPreheader) override {}
68456852

68466853
void adjustTripCount(int TripCountAdjust) override {}
68476854

68486855
void disposed() override {}
6856+
6857+
bool isMVEExpanderSupported() override { return false; }
68496858
};
68506859

68516860
void ARMPipelinerLoopInfo::bumpCrossIterationPressure(RegPressureTracker &RPT,

llvm/lib/Target/Hexagon/HexagonInstrInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -770,6 +770,13 @@ class HexagonPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
770770
return TripCount > TC;
771771
}
772772

773+
void createRemainingIterationsGreaterCondition(
774+
int TC, MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &Cond,
775+
DenseMap<unsigned, unsigned> RegMap) override {
776+
llvm_unreachable(
777+
"Target didn't implement createRemainingIterationsGreaterCondition");
778+
}
779+
773780
void setPreheader(MachineBasicBlock *NewPreheader) override {
774781
NewPreheader->splice(NewPreheader->getFirstTerminator(), Loop->getParent(),
775782
Loop);
@@ -798,6 +805,8 @@ class HexagonPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
798805
}
799806

800807
void disposed() override { Loop->eraseFromParent(); }
808+
809+
bool isMVEExpanderSupported() override { return false; }
801810
};
802811
} // namespace
803812

llvm/lib/Target/PowerPC/PPCInstrInfo.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5574,6 +5574,13 @@ class PPCPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
55745574
return TripCount > TC;
55755575
}
55765576

5577+
void createRemainingIterationsGreaterCondition(
5578+
int TC, MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &Cond,
5579+
DenseMap<unsigned, unsigned> RegMap) override {
5580+
llvm_unreachable(
5581+
"Target didn't implement createRemainingIterationsGreaterCondition");
5582+
}
5583+
55775584
void setPreheader(MachineBasicBlock *NewPreheader) override {
55785585
// Do nothing. We want the LOOP setup instruction to stay in the *old*
55795586
// preheader, so we can use BDZ in the prologs to adapt the loop trip count.
@@ -5598,6 +5605,8 @@ class PPCPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
55985605
// Ensure the loop setup instruction is deleted too.
55995606
LoopCount->eraseFromParent();
56005607
}
5608+
5609+
bool isMVEExpanderSupported() override { return false; }
56015610
};
56025611
} // namespace
56035612

0 commit comments

Comments
 (0)