Skip to content

Commit c3841ef

Browse files
committed
[RISCV] Add tune info for postra scheduling direction
The results differ on different platforms so it is really hard to determine a common default value. Tune info for postra scheduling direction is added and CPUs can set their own preferable postra scheduling direction. We set the default value to `bidirectional` as it may be the most balanced direction.
1 parent 920495c commit c3841ef

File tree

10 files changed

+223
-184
lines changed

10 files changed

+223
-184
lines changed

llvm/include/llvm/CodeGen/MachineScheduler.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,14 @@ class TargetInstrInfo;
123123
class TargetPassConfig;
124124
class TargetRegisterInfo;
125125

126+
namespace MISchedPostRASched {
127+
enum Direction {
128+
TopDown,
129+
BottomUp,
130+
Bidirectional,
131+
};
132+
} // end namespace MISchedPostRASched
133+
126134
/// MachineSchedContext provides enough context from the MachineScheduler pass
127135
/// for the target to instantiate a scheduler.
128136
struct MachineSchedContext {

llvm/lib/CodeGen/MachineScheduler.cpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,7 @@ cl::opt<bool> ForceTopDown("misched-topdown", cl::Hidden,
8181
cl::desc("Force top-down list scheduling"));
8282
cl::opt<bool> ForceBottomUp("misched-bottomup", cl::Hidden,
8383
cl::desc("Force bottom-up list scheduling"));
84-
namespace MISchedPostRASched {
85-
enum Direction {
86-
TopDown,
87-
BottomUp,
88-
Bidirectional,
89-
};
90-
} // end namespace MISchedPostRASched
84+
9185
cl::opt<MISchedPostRASched::Direction> PostRADirection(
9286
"misched-postra-direction", cl::Hidden,
9387
cl::desc("Post reg-alloc list scheduling direction"),

llvm/lib/Target/RISCV/RISCVProcessors.td

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@
1010
// RISC-V processors supported.
1111
//===----------------------------------------------------------------------===//
1212

13+
// Predefined scheduling direction.
14+
defvar TopDown = [{ MISchedPostRASched::TopDown }];
15+
defvar BottomUp = [{ MISchedPostRASched::BottomUp }];
16+
defvar Bidirectional = [{ MISchedPostRASched::Bidirectional }];
17+
1318
class RISCVTuneInfo {
1419
bits<8> PrefFunctionAlignment = 1;
1520
bits<8> PrefLoopAlignment = 1;
@@ -37,6 +42,11 @@ class RISCVTuneInfo {
3742

3843
bits<32> MaxLoadsPerMemcmpOptSize = 4;
3944
bits<32> MaxLoadsPerMemcmp = 8;
45+
46+
// The direction of PostRA scheduling.
47+
// Do bidirectional scheduling by default since it provides a more balanced
48+
// scheduling leading to better performance. This will increase compile time.
49+
code PostRASchedDirection = Bidirectional;
4050
}
4151

4252
def RISCVTuneInfoTable : GenericTable {
@@ -49,7 +59,8 @@ def RISCVTuneInfoTable : GenericTable {
4959
"MaxStoresPerMemset", "MaxGluedStoresPerMemcpy",
5060
"MaxStoresPerMemcpyOptSize", "MaxStoresPerMemcpy",
5161
"MaxStoresPerMemmoveOptSize", "MaxStoresPerMemmove",
52-
"MaxLoadsPerMemcmpOptSize", "MaxLoadsPerMemcmp"];
62+
"MaxLoadsPerMemcmpOptSize", "MaxLoadsPerMemcmp",
63+
"PostRASchedDirection"];
5364
}
5465

5566
def getRISCVTuneInfo : SearchIndex {

llvm/lib/Target/RISCV/RISCVSubtarget.cpp

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
#include "RISCV.h"
1717
#include "RISCVFrameLowering.h"
1818
#include "RISCVTargetMachine.h"
19-
#include "llvm/CodeGen/MachineScheduler.h"
2019
#include "llvm/CodeGen/MacroFusion.h"
2120
#include "llvm/CodeGen/ScheduleDAGMutation.h"
2221
#include "llvm/MC/TargetRegistry.h"
@@ -211,3 +210,19 @@ void RISCVSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
211210
// register-pressure tracking. This will increase compile time.
212211
Policy.ShouldTrackPressure = true;
213212
}
213+
214+
void RISCVSubtarget::overridePostRASchedPolicy(MachineSchedPolicy &Policy,
215+
unsigned NumRegionInstrs) const {
216+
MISchedPostRASched::Direction PostRASchedDirection =
217+
getPostRASchedDirection();
218+
if (PostRASchedDirection == MISchedPostRASched::TopDown) {
219+
Policy.OnlyTopDown = true;
220+
Policy.OnlyBottomUp = false;
221+
} else if (PostRASchedDirection == MISchedPostRASched::BottomUp) {
222+
Policy.OnlyTopDown = false;
223+
Policy.OnlyBottomUp = true;
224+
} else if (PostRASchedDirection == MISchedPostRASched::Bidirectional) {
225+
Policy.OnlyTopDown = false;
226+
Policy.OnlyBottomUp = false;
227+
}
228+
}

llvm/lib/Target/RISCV/RISCVSubtarget.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#include "llvm/CodeGen/GlobalISel/CallLowering.h"
2222
#include "llvm/CodeGen/GlobalISel/InstructionSelector.h"
2323
#include "llvm/CodeGen/GlobalISel/LegalizerInfo.h"
24+
#include "llvm/CodeGen/MachineScheduler.h"
2425
#include "llvm/CodeGen/SelectionDAGTargetInfo.h"
2526
#include "llvm/CodeGen/TargetSubtargetInfo.h"
2627
#include "llvm/IR/DataLayout.h"
@@ -66,6 +67,9 @@ struct RISCVTuneInfo {
6667

6768
unsigned MaxLoadsPerMemcmpOptSize;
6869
unsigned MaxLoadsPerMemcmp;
70+
71+
// The direction of PostRA scheduling.
72+
MISchedPostRASched::Direction PostRASchedDirection;
6973
};
7074

7175
#define GET_RISCVTuneInfoTable_DECL
@@ -362,8 +366,15 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
362366
: TuneInfo->MaxLoadsPerMemcmp;
363367
}
364368

369+
MISchedPostRASched::Direction getPostRASchedDirection() const {
370+
return TuneInfo->PostRASchedDirection;
371+
}
372+
365373
void overrideSchedPolicy(MachineSchedPolicy &Policy,
366374
unsigned NumRegionInstrs) const override;
375+
376+
void overridePostRASchedPolicy(MachineSchedPolicy &Policy,
377+
unsigned NumRegionInstrs) const override;
367378
};
368379
} // End llvm namespace
369380

llvm/test/CodeGen/RISCV/machine-combiner-strategies.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,8 @@ define i32 @test_local_strategy(i32 %a0, i32 %a1, i32 %a2, i32 %a3, i32 %a4, i32
5050
; CHECK_LOCAL_SIFIVE_U74-NEXT: # %bb.1: # %b2
5151
; CHECK_LOCAL_SIFIVE_U74-NEXT: ret
5252
; CHECK_LOCAL_SIFIVE_U74-NEXT: .LBB0_2: # %b1
53-
; CHECK_LOCAL_SIFIVE_U74-NEXT: add a3, a3, a4
5453
; CHECK_LOCAL_SIFIVE_U74-NEXT: add a0, a0, a5
54+
; CHECK_LOCAL_SIFIVE_U74-NEXT: add a3, a3, a4
5555
; CHECK_LOCAL_SIFIVE_U74-NEXT: addw a0, a0, a3
5656
; CHECK_LOCAL_SIFIVE_U74-NEXT: ret
5757
;

0 commit comments

Comments
 (0)