Skip to content

[MISched] Use SchedRegion in overrideSchedPolicy and overridePostRASchedPolicy #149297

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 5 commits into from
Jul 22, 2025
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
18 changes: 17 additions & 1 deletion llvm/include/llvm/CodeGen/MachineScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
//
// void <SubTarget>Subtarget::
// overrideSchedPolicy(MachineSchedPolicy &Policy,
// unsigned NumRegionInstrs) const {
// const SchedRegion &Region) const {
// Policy.<Flag> = true;
// }
//
Expand Down Expand Up @@ -218,6 +218,22 @@ struct MachineSchedPolicy {
MachineSchedPolicy() = default;
};

/// A region of an MBB for scheduling.
struct SchedRegion {
/// RegionBegin is the first instruction in the scheduling region, and
/// RegionEnd is either MBB->end() or the scheduling boundary after the
/// last instruction in the scheduling region. These iterators cannot refer
/// to instructions outside of the identified scheduling region because
/// those may be reordered before scheduling this region.
MachineBasicBlock::iterator RegionBegin;
MachineBasicBlock::iterator RegionEnd;
unsigned NumRegionInstrs;

SchedRegion(MachineBasicBlock::iterator B, MachineBasicBlock::iterator E,
unsigned N)
: RegionBegin(B), RegionEnd(E), NumRegionInstrs(N) {}
};

/// MachineSchedStrategy - Interface to the scheduling algorithm used by
/// ScheduleDAGMI.
///
Expand Down
5 changes: 3 additions & 2 deletions llvm/include/llvm/CodeGen/TargetSubtargetInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class TargetRegisterClass;
class TargetRegisterInfo;
class TargetSchedModel;
class Triple;
struct SchedRegion;

//===----------------------------------------------------------------------===//
///
Expand Down Expand Up @@ -231,7 +232,7 @@ class LLVM_ABI TargetSubtargetInfo : public MCSubtargetInfo {
/// scheduling heuristics (no custom MachineSchedStrategy) to make
/// changes to the generic scheduling policy.
virtual void overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const {}
const SchedRegion &Region) const {}

/// Override generic post-ra scheduling policy within a region.
///
Expand All @@ -241,7 +242,7 @@ class LLVM_ABI TargetSubtargetInfo : public MCSubtargetInfo {
/// Note that some options like tracking register pressure won't take effect
/// in post-ra scheduling.
virtual void overridePostRASchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const {}
const SchedRegion &Region) const {}

// Perform target-specific adjustments to the latency of a schedule
// dependency.
Expand Down
24 changes: 4 additions & 20 deletions llvm/lib/CodeGen/MachineScheduler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,24 +771,6 @@ static bool isSchedBoundary(MachineBasicBlock::iterator MI,
MI->isFakeUse();
}

/// A region of an MBB for scheduling.
namespace {
struct SchedRegion {
/// RegionBegin is the first instruction in the scheduling region, and
/// RegionEnd is either MBB->end() or the scheduling boundary after the
/// last instruction in the scheduling region. These iterators cannot refer
/// to instructions outside of the identified scheduling region because
/// those may be reordered before scheduling this region.
MachineBasicBlock::iterator RegionBegin;
MachineBasicBlock::iterator RegionEnd;
unsigned NumRegionInstrs;

SchedRegion(MachineBasicBlock::iterator B, MachineBasicBlock::iterator E,
unsigned N) :
RegionBegin(B), RegionEnd(E), NumRegionInstrs(N) {}
};
} // end anonymous namespace

using MBBRegionsVector = SmallVector<SchedRegion, 16>;

static void
Expand Down Expand Up @@ -3725,7 +3707,8 @@ void GenericScheduler::initPolicy(MachineBasicBlock::iterator Begin,
RegionPolicy.OnlyBottomUp = true;

// Allow the subtarget to override default policy.
MF.getSubtarget().overrideSchedPolicy(RegionPolicy, NumRegionInstrs);
SchedRegion Region(Begin, End, NumRegionInstrs);
MF.getSubtarget().overrideSchedPolicy(RegionPolicy, Region);

// After subtarget overrides, apply command line options.
if (!EnableRegPressure) {
Expand Down Expand Up @@ -4338,7 +4321,8 @@ void PostGenericScheduler::initPolicy(MachineBasicBlock::iterator Begin,
RegionPolicy.OnlyBottomUp = false;

// Allow the subtarget to override default policy.
MF.getSubtarget().overridePostRASchedPolicy(RegionPolicy, NumRegionInstrs);
SchedRegion Region(Begin, End, NumRegionInstrs);
MF.getSubtarget().overridePostRASchedPolicy(RegionPolicy, Region);

// After subtarget overrides, apply command line options.
if (PostRADirection == MISched::TopDown) {
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AArch64/AArch64Subtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -534,7 +534,7 @@ unsigned AArch64Subtarget::classifyGlobalFunctionReference(
}

void AArch64Subtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const {
const SchedRegion &Region) const {
// LNT run (at least on Cyclone) showed reasonably significant gains for
// bi-directional scheduling. 253.perlbmk.
Policy.OnlyTopDown = false;
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/AArch64/AArch64Subtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,8 @@ class AArch64Subtarget final : public AArch64GenSubtargetInfo {
}

void overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const override;
const SchedRegion &Region) const override;

void adjustSchedDependency(SUnit *Def, int DefOpIdx, SUnit *Use, int UseOpIdx,
SDep &Dep,
const TargetSchedModel *SchedModel) const override;
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/GCNSubtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ bool GCNSubtarget::zeroesHigh16BitsOfDest(unsigned Opcode) const {
}

void GCNSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const {
const SchedRegion &Region) const {
// Track register pressure so the scheduler can try to decrease
// pressure once register usage is above the threshold defined by
// SIRegisterInfo::getRegPressureSetLimit()
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/AMDGPU/GCNSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ class GCNSubtarget final : public AMDGPUGenSubtargetInfo,
}

void overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const override;
const SchedRegion &Region) const override;

void mirFileLoaded(MachineFunction &MF) const override;

Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/PowerPC/PPCSubtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void PPCSubtarget::getCriticalPathRCs(RegClassVector &CriticalPathRCs) const {
}

void PPCSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const {
const SchedRegion &Region) const {
// The GenericScheduler that we use defaults to scheduling bottom up only.
// We want to schedule from both the top and the bottom and so we set
// OnlyBottomUp to false.
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/PowerPC/PPCSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,8 @@ class PPCSubtarget : public PPCGenSubtargetInfo {
void getCriticalPathRCs(RegClassVector &CriticalPathRCs) const override;

void overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const override;
const SchedRegion &Region) const override;

bool useAA() const override;

bool enableSubRegLiveness() const override;
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/RISCV/RISCVSubtarget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ unsigned RISCVSubtarget::getMinimumJumpTableEntries() const {
}

void RISCVSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const {
const SchedRegion &Region) const {
// Do bidirectional scheduling since it provides a more balanced scheduling
// leading to better performance. This will increase compile time.
Policy.OnlyTopDown = false;
Expand All @@ -231,8 +231,8 @@ void RISCVSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
Policy.ShouldTrackPressure = true;
}

void RISCVSubtarget::overridePostRASchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const {
void RISCVSubtarget::overridePostRASchedPolicy(
MachineSchedPolicy &Policy, const SchedRegion &Region) const {
MISched::Direction PostRASchedDirection = getPostRASchedDirection();
if (PostRASchedDirection == MISched::TopDown) {
Policy.OnlyTopDown = true;
Expand Down
6 changes: 3 additions & 3 deletions llvm/lib/Target/RISCV/RISCVSubtarget.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,11 +395,11 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
}

void overrideSchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const override;
const SchedRegion &Region) const override;

void overridePostRASchedPolicy(MachineSchedPolicy &Policy,
unsigned NumRegionInstrs) const override;
const SchedRegion &Region) const override;
};
} // End llvm namespace
} // namespace llvm

#endif
Loading