Skip to content

Commit b9d203d

Browse files
committed
fixup! [ModuloSchedule] Implement modulo variable expansion for pipelining
1 parent 545b894 commit b9d203d

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
@@ -765,19 +765,18 @@ class TargetInstrInfo : public MCInstrInfo {
765765
createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
766766
SmallVectorImpl<MachineOperand> &Cond) = 0;
767767

768-
/// Create a condtion to determine if the remaining trip count represented
769-
/// by the loop counter CounterReg is greater than TC. Some instructions
770-
/// such as comparisons may be inserted at the bottom of MBB. CounterReg
771-
/// must be accessible there.
768+
/// Create a condition to determine if the remaining trip count for a phase
769+
/// is greater than TC. Some instructions such as comparisons may be
770+
/// inserted at the bottom of MBB. The all instructions expanded for the
771+
/// phase must be inserted in MBB before calling this function. RegMap is
772+
/// the map from the original registers to the expanded registers for the
773+
/// phase.
772774
///
773-
/// The definition of the return value is the same as for the variant above.
774-
virtual std::optional<bool>
775-
createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
776-
SmallVectorImpl<MachineOperand> &Cond,
777-
Register CounterReg) {
778-
llvm_unreachable(
779-
"Target didn't implement createTripCountGreaterCondition");
780-
}
775+
/// MBB can also be a predecessor of the prologue block. Then RegMap must be
776+
/// empty and the compared value is the initial value of the trip count.
777+
virtual void createRemainingIterationsGreaterCondition(
778+
int TC, MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &Cond,
779+
DenseMap<unsigned, unsigned> RegMap) = 0;
781780

782781
/// Modify the loop such that the trip count is
783782
/// OriginalTC + TripCountAdjust.
@@ -793,15 +792,9 @@ class TargetInstrInfo : public MCInstrInfo {
793792
/// valid; the loop has been removed.
794793
virtual void disposed() = 0;
795794

796-
/// Return the initial value of the loop counter.
797-
virtual Register getCounterInitReg() {
798-
llvm_unreachable("Target didn't implement getCounterInitReg");
799-
}
800-
801-
/// Return the updated value of the loop counter in the original loop.
802-
virtual Register getCounterUpdatedReg() {
803-
llvm_unreachable("Target didn't implement getCounterUpdatedReg");
804-
}
795+
/// Return true if the target can expand pipelined schedule with modulo
796+
/// variable expansion.
797+
virtual bool isMVEExpanderSupported() = 0;
805798
};
806799

807800
/// 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
@@ -681,9 +681,9 @@ void SwingSchedulerDAG::schedule() {
681681
if (ExperimentalCodeGen && NewInstrChanges.empty()) {
682682
PeelingModuloScheduleExpander MSE(MF, MS, &LIS);
683683
MSE.expand();
684-
}
685-
if (MVECodeGen && NewInstrChanges.empty() &&
686-
ModuloScheduleExpanderMVE::canApply(Loop)) {
684+
} else if (MVECodeGen && NewInstrChanges.empty() &&
685+
LoopPipelinerInfo->isMVEExpanderSupported() &&
686+
ModuloScheduleExpanderMVE::canApply(Loop)) {
687687
ModuloScheduleExpanderMVE MSE(MF, MS, LIS);
688688
MSE.expand();
689689
} 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
@@ -6823,11 +6823,20 @@ class ARMPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
68236823
llvm_unreachable("Unknown EndLoop");
68246824
}
68256825

6826+
void createRemainingIterationsGreaterCondition(
6827+
int TC, MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &Cond,
6828+
DenseMap<unsigned, unsigned> RegMap) override {
6829+
llvm_unreachable(
6830+
"Target didn't implement createRemainingIterationsGreaterCondition");
6831+
}
6832+
68266833
void setPreheader(MachineBasicBlock *NewPreheader) override {}
68276834

68286835
void adjustTripCount(int TripCountAdjust) override {}
68296836

68306837
void disposed() override {}
6838+
6839+
bool isMVEExpanderSupported() override { return false; }
68316840
};
68326841

68336842
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
@@ -5439,6 +5439,13 @@ class PPCPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
54395439
return TripCount > TC;
54405440
}
54415441

5442+
void createRemainingIterationsGreaterCondition(
5443+
int TC, MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &Cond,
5444+
DenseMap<unsigned, unsigned> RegMap) override {
5445+
llvm_unreachable(
5446+
"Target didn't implement createRemainingIterationsGreaterCondition");
5447+
}
5448+
54425449
void setPreheader(MachineBasicBlock *NewPreheader) override {
54435450
// Do nothing. We want the LOOP setup instruction to stay in the *old*
54445451
// preheader, so we can use BDZ in the prologs to adapt the loop trip count.
@@ -5463,6 +5470,8 @@ class PPCPipelinerLoopInfo : public TargetInstrInfo::PipelinerLoopInfo {
54635470
// Ensure the loop setup instruction is deleted too.
54645471
LoopCount->eraseFromParent();
54655472
}
5473+
5474+
bool isMVEExpanderSupported() override { return false; }
54665475
};
54675476
} // namespace
54685477

0 commit comments

Comments
 (0)