Skip to content

Commit 84f7fb6

Browse files
authored
[MachineScheduler] Add option to control reordering for store/load clustering (#75338)
Reordering based on the sort order of the MemOpInfo array was disabled in <https://reviews.llvm.org/D72706>. However, it's not clear this is desirable for al targets. It also makes it more difficult to compare the incremental benefit of enabling load clustering in the selectiondag scheduler as well was the machinescheduler, as the sdag scheduler does seem to allow this reordering. This patch adds a parameter that can control the behaviour on a per-target basis. Split out from #73789.
1 parent 57d517c commit 84f7fb6

File tree

4 files changed

+33
-17
lines changed

4 files changed

+33
-17
lines changed

llvm/include/llvm/CodeGen/MachineScheduler.h

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,13 +1347,19 @@ ScheduleDAGMILive *createGenericSchedLive(MachineSchedContext *C);
13471347
/// Create a generic scheduler with no vreg liveness or DAG mutation passes.
13481348
ScheduleDAGMI *createGenericSchedPostRA(MachineSchedContext *C);
13491349

1350+
/// If ReorderWhileClustering is set to true, no attempt will be made to
1351+
/// reduce reordering due to store clustering.
13501352
std::unique_ptr<ScheduleDAGMutation>
13511353
createLoadClusterDAGMutation(const TargetInstrInfo *TII,
1352-
const TargetRegisterInfo *TRI);
1354+
const TargetRegisterInfo *TRI,
1355+
bool ReorderWhileClustering = false);
13531356

1357+
/// If ReorderWhileClustering is set to true, no attempt will be made to
1358+
/// reduce reordering due to store clustering.
13541359
std::unique_ptr<ScheduleDAGMutation>
13551360
createStoreClusterDAGMutation(const TargetInstrInfo *TII,
1356-
const TargetRegisterInfo *TRI);
1361+
const TargetRegisterInfo *TRI,
1362+
bool ReorderWhileClustering = false);
13571363

13581364
std::unique_ptr<ScheduleDAGMutation>
13591365
createCopyConstrainDAGMutation(const TargetInstrInfo *TII,

llvm/lib/CodeGen/MachineScheduler.cpp

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,11 +1743,14 @@ class BaseMemOpClusterMutation : public ScheduleDAGMutation {
17431743
const TargetInstrInfo *TII;
17441744
const TargetRegisterInfo *TRI;
17451745
bool IsLoad;
1746+
bool ReorderWhileClustering;
17461747

17471748
public:
17481749
BaseMemOpClusterMutation(const TargetInstrInfo *tii,
1749-
const TargetRegisterInfo *tri, bool IsLoad)
1750-
: TII(tii), TRI(tri), IsLoad(IsLoad) {}
1750+
const TargetRegisterInfo *tri, bool IsLoad,
1751+
bool ReorderWhileClustering)
1752+
: TII(tii), TRI(tri), IsLoad(IsLoad),
1753+
ReorderWhileClustering(ReorderWhileClustering) {}
17511754

17521755
void apply(ScheduleDAGInstrs *DAGInstrs) override;
17531756

@@ -1763,14 +1766,16 @@ class BaseMemOpClusterMutation : public ScheduleDAGMutation {
17631766
class StoreClusterMutation : public BaseMemOpClusterMutation {
17641767
public:
17651768
StoreClusterMutation(const TargetInstrInfo *tii,
1766-
const TargetRegisterInfo *tri)
1767-
: BaseMemOpClusterMutation(tii, tri, false) {}
1769+
const TargetRegisterInfo *tri,
1770+
bool ReorderWhileClustering)
1771+
: BaseMemOpClusterMutation(tii, tri, false, ReorderWhileClustering) {}
17681772
};
17691773

17701774
class LoadClusterMutation : public BaseMemOpClusterMutation {
17711775
public:
1772-
LoadClusterMutation(const TargetInstrInfo *tii, const TargetRegisterInfo *tri)
1773-
: BaseMemOpClusterMutation(tii, tri, true) {}
1776+
LoadClusterMutation(const TargetInstrInfo *tii, const TargetRegisterInfo *tri,
1777+
bool ReorderWhileClustering)
1778+
: BaseMemOpClusterMutation(tii, tri, true, ReorderWhileClustering) {}
17741779
};
17751780

17761781
} // end anonymous namespace
@@ -1779,15 +1784,19 @@ namespace llvm {
17791784

17801785
std::unique_ptr<ScheduleDAGMutation>
17811786
createLoadClusterDAGMutation(const TargetInstrInfo *TII,
1782-
const TargetRegisterInfo *TRI) {
1783-
return EnableMemOpCluster ? std::make_unique<LoadClusterMutation>(TII, TRI)
1787+
const TargetRegisterInfo *TRI,
1788+
bool ReorderWhileClustering) {
1789+
return EnableMemOpCluster ? std::make_unique<LoadClusterMutation>(
1790+
TII, TRI, ReorderWhileClustering)
17841791
: nullptr;
17851792
}
17861793

17871794
std::unique_ptr<ScheduleDAGMutation>
17881795
createStoreClusterDAGMutation(const TargetInstrInfo *TII,
1789-
const TargetRegisterInfo *TRI) {
1790-
return EnableMemOpCluster ? std::make_unique<StoreClusterMutation>(TII, TRI)
1796+
const TargetRegisterInfo *TRI,
1797+
bool ReorderWhileClustering) {
1798+
return EnableMemOpCluster ? std::make_unique<StoreClusterMutation>(
1799+
TII, TRI, ReorderWhileClustering)
17911800
: nullptr;
17921801
}
17931802

@@ -1840,7 +1849,7 @@ void BaseMemOpClusterMutation::clusterNeighboringMemOps(
18401849

18411850
SUnit *SUa = MemOpa.SU;
18421851
SUnit *SUb = MemOpb.SU;
1843-
if (SUa->NodeNum > SUb->NodeNum)
1852+
if (!ReorderWhileClustering && SUa->NodeNum > SUb->NodeNum)
18441853
std::swap(SUa, SUb);
18451854

18461855
// FIXME: Is this check really required?

llvm/lib/Target/RISCV/RISCVTargetMachine.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,8 @@ class RISCVPassConfig : public TargetPassConfig {
348348
ScheduleDAGMILive *DAG = nullptr;
349349
if (EnableMISchedLoadClustering) {
350350
DAG = createGenericSchedLive(C);
351-
DAG->addMutation(createLoadClusterDAGMutation(DAG->TII, DAG->TRI));
351+
DAG->addMutation(createLoadClusterDAGMutation(
352+
DAG->TII, DAG->TRI, /*ReorderWhileClustering=*/true));
352353
}
353354
if (ST.hasMacroFusion()) {
354355
DAG = DAG ? DAG : createGenericSchedLive(C);

llvm/test/CodeGen/RISCV/misched-load-clustering.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ define i32 @load_clustering_1(ptr nocapture %p) {
2323
; LDCLUSTER: ********** MI Scheduling **********
2424
; LDCLUSTER-LABEL: load_clustering_1:%bb.0
2525
; LDCLUSTER: *** Final schedule for %bb.0 ***
26-
; LDCLUSTER: SU(5): %6:gpr = LW %0:gpr, 16
27-
; LDCLUSTER: SU(1): %1:gpr = LW %0:gpr, 12
28-
; LDCLUSTER: SU(2): %2:gpr = LW %0:gpr, 8
2926
; LDCLUSTER: SU(4): %4:gpr = LW %0:gpr, 4
27+
; LDCLUSTER: SU(2): %2:gpr = LW %0:gpr, 8
28+
; LDCLUSTER: SU(1): %1:gpr = LW %0:gpr, 12
29+
; LDCLUSTER: SU(5): %6:gpr = LW %0:gpr, 16
3030
entry:
3131
%arrayidx0 = getelementptr inbounds i32, ptr %p, i32 3
3232
%val0 = load i32, i32* %arrayidx0

0 commit comments

Comments
 (0)