Skip to content

[ModuloSchedule] Implement modulo variable expansion for pipelining #65609

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
Jun 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions llvm/include/llvm/CodeGen/ModuloSchedule.h
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,78 @@ class PeelingModuloScheduleExpander {
std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo> LoopInfo;
};

/// Expand the kernel using modulo variable expansion algorithm (MVE).
/// It unrolls the kernel enough to avoid overlap of register lifetime.
class ModuloScheduleExpanderMVE {
private:
using ValueMapTy = DenseMap<unsigned, unsigned>;
using MBBVectorTy = SmallVectorImpl<MachineBasicBlock *>;
using InstrMapTy = DenseMap<MachineInstr *, MachineInstr *>;

ModuloSchedule &Schedule;
MachineFunction &MF;
const TargetSubtargetInfo &ST;
MachineRegisterInfo &MRI;
const TargetInstrInfo *TII = nullptr;
LiveIntervals &LIS;

MachineBasicBlock *OrigKernel = nullptr;
MachineBasicBlock *OrigPreheader = nullptr;
MachineBasicBlock *OrigExit = nullptr;
MachineBasicBlock *Check = nullptr;
MachineBasicBlock *Prolog = nullptr;
MachineBasicBlock *NewKernel = nullptr;
MachineBasicBlock *Epilog = nullptr;
MachineBasicBlock *NewPreheader = nullptr;
MachineBasicBlock *NewExit = nullptr;
std::unique_ptr<TargetInstrInfo::PipelinerLoopInfo> LoopInfo;

/// The number of unroll required to avoid overlap of live ranges.
/// NumUnroll = 1 means no unrolling.
int NumUnroll;

void calcNumUnroll();
void generatePipelinedLoop();
void generateProlog(SmallVectorImpl<ValueMapTy> &VRMap);
void generatePhi(MachineInstr *OrigMI, int UnrollNum,
SmallVectorImpl<ValueMapTy> &PrologVRMap,
SmallVectorImpl<ValueMapTy> &KernelVRMap,
SmallVectorImpl<ValueMapTy> &PhiVRMap);
void generateKernel(SmallVectorImpl<ValueMapTy> &PrologVRMap,
SmallVectorImpl<ValueMapTy> &KernelVRMap,
InstrMapTy &LastStage0Insts);
void generateEpilog(SmallVectorImpl<ValueMapTy> &KernelVRMap,
SmallVectorImpl<ValueMapTy> &EpilogVRMap,
InstrMapTy &LastStage0Insts);
void mergeRegUsesAfterPipeline(Register OrigReg, Register NewReg);

MachineInstr *cloneInstr(MachineInstr *OldMI);

void updateInstrDef(MachineInstr *NewMI, ValueMapTy &VRMap, bool LastDef);

void generateKernelPhi(Register OrigLoopVal, Register NewLoopVal,
unsigned UnrollNum,
SmallVectorImpl<ValueMapTy> &VRMapProlog,
SmallVectorImpl<ValueMapTy> &VRMapPhi);
void updateInstrUse(MachineInstr *MI, int StageNum, int PhaseNum,
SmallVectorImpl<ValueMapTy> &CurVRMap,
SmallVectorImpl<ValueMapTy> *PrevVRMap);

void insertCondBranch(MachineBasicBlock &MBB, int RequiredTC,
InstrMapTy &LastStage0Insts,
MachineBasicBlock &GreaterThan,
MachineBasicBlock &Otherwise);

public:
ModuloScheduleExpanderMVE(MachineFunction &MF, ModuloSchedule &S,
LiveIntervals &LIS)
: Schedule(S), MF(MF), ST(MF.getSubtarget()), MRI(MF.getRegInfo()),
TII(ST.getInstrInfo()), LIS(LIS) {}

void expand();
static bool canApply(MachineLoop &L);
};

/// Expander that simply annotates each scheduled instruction with a post-instr
/// symbol that can be consumed by the ModuloScheduleTest pass.
///
Expand Down
24 changes: 24 additions & 0 deletions llvm/include/llvm/CodeGen/TargetInstrInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,26 @@ class TargetInstrInfo : public MCInstrInfo {
createTripCountGreaterCondition(int TC, MachineBasicBlock &MBB,
SmallVectorImpl<MachineOperand> &Cond) = 0;

/// Create a condition to determine if the remaining trip count for a phase
/// is greater than TC. Some instructions such as comparisons may be
/// inserted at the bottom of MBB. All instructions expanded for the
/// phase must be inserted in MBB before calling this function.
/// LastStage0Insts is the map from the original instructions scheduled at
/// stage#0 to the expanded instructions for the last iteration of the
/// kernel. LastStage0Insts is intended to obtain the instruction that
/// refers the latest loop counter value.
///
/// MBB can also be a predecessor of the prologue block. Then
/// LastStage0Insts must be empty and the compared value is the initial
/// value of the trip count.
virtual void createRemainingIterationsGreaterCondition(
int TC, MachineBasicBlock &MBB, SmallVectorImpl<MachineOperand> &Cond,
DenseMap<MachineInstr *, MachineInstr *> &LastStage0Insts) {
llvm_unreachable(
"Target didn't implement "
"PipelinerLoopInfo::createRemainingIterationsGreaterCondition!");
}

/// Modify the loop such that the trip count is
/// OriginalTC + TripCountAdjust.
virtual void adjustTripCount(int TripCountAdjust) = 0;
Expand All @@ -780,6 +800,10 @@ class TargetInstrInfo : public MCInstrInfo {
/// Once this function is called, no other functions on this object are
/// valid; the loop has been removed.
virtual void disposed() = 0;

/// Return true if the target can expand pipelined schedule with modulo
/// variable expansion.
virtual bool isMVEExpanderSupported() { return false; }
};

/// Analyze loop L, which must be a single-basic-block loop, and if the
Expand Down
9 changes: 9 additions & 0 deletions llvm/lib/CodeGen/MachinePipeliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,10 @@ static cl::opt<int>
cl::desc("Margin representing the unused percentage of "
"the register pressure limit"));

static cl::opt<bool>
MVECodeGen("pipeliner-mve-cg", cl::Hidden, cl::init(false),
cl::desc("Use the MVE code generator for software pipelining"));

namespace llvm {

// A command line option to enable the CopyToPhi DAG mutation.
Expand Down Expand Up @@ -677,6 +681,11 @@ void SwingSchedulerDAG::schedule() {
if (ExperimentalCodeGen && NewInstrChanges.empty()) {
PeelingModuloScheduleExpander MSE(MF, MS, &LIS);
MSE.expand();
} else if (MVECodeGen && NewInstrChanges.empty() &&
LoopPipelinerInfo->isMVEExpanderSupported() &&
ModuloScheduleExpanderMVE::canApply(Loop)) {
ModuloScheduleExpanderMVE MSE(MF, MS, LIS);
MSE.expand();
} else {
ModuloScheduleExpander MSE(MF, MS, LIS, std::move(NewInstrChanges));
MSE.expand();
Expand Down
Loading
Loading