Skip to content

Commit 4237baf

Browse files
committed
[RISCV] Add tune features for postra scheduling direction
The results differ on different platforms so it is really hard to determine a common default value. Several tune features for postra scheduling direction are 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 66dc2cf commit 4237baf

File tree

9 files changed

+192
-154
lines changed

9 files changed

+192
-154
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/RISCVFeatures.td

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1438,6 +1438,20 @@ def TunePreferWInst
14381438
: SubtargetFeature<"prefer-w-inst", "PreferWInst", "true",
14391439
"Prefer instructions with W suffix">;
14401440

1441+
let SetMaxValue = false in {
1442+
def TuneTopDownPostRASched
1443+
: SubtargetFeature<"topdown-postra-sched", "PostRASchedDirection", "MISchedPostRASched::TopDown",
1444+
"Using TopDown post-ra scheduling">;
1445+
1446+
def TuneBottomUpPostRASched
1447+
: SubtargetFeature<"bottomup-postra-sched", "PostRASchedDirection", "MISchedPostRASched::BottomUp",
1448+
"Using BottomUp post-ra scheduling">;
1449+
1450+
def TuneBidirectionalPostRASched
1451+
: SubtargetFeature<"bidirectional-postra-sched", "PostRASchedDirection", "MISchedPostRASched::Bidirectional",
1452+
"Using Bidirectional post-ra scheduling">;
1453+
}
1454+
14411455
def TuneConditionalCompressedMoveFusion
14421456
: SubtargetFeature<"conditional-cmv-fusion", "HasConditionalCompressedMoveFusion",
14431457
"true", "Enable branch+c.mv fusion">;

llvm/lib/Target/RISCV/RISCVSubtarget.cpp

Lines changed: 14 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"
@@ -212,3 +211,17 @@ void RISCVSubtarget::overrideSchedPolicy(MachineSchedPolicy &Policy,
212211
// register-pressure tracking. This will increase compile time.
213212
Policy.ShouldTrackPressure = true;
214213
}
214+
215+
void RISCVSubtarget::overridePostRASchedPolicy(MachineSchedPolicy &Policy,
216+
unsigned NumRegionInstrs) const {
217+
if (PostRASchedDirection == MISchedPostRASched::TopDown) {
218+
Policy.OnlyTopDown = true;
219+
Policy.OnlyBottomUp = false;
220+
} else if (PostRASchedDirection == MISchedPostRASched::BottomUp) {
221+
Policy.OnlyTopDown = false;
222+
Policy.OnlyBottomUp = true;
223+
} else if (PostRASchedDirection == MISchedPostRASched::Bidirectional) {
224+
Policy.OnlyTopDown = false;
225+
Policy.OnlyBottomUp = false;
226+
}
227+
}

llvm/lib/Target/RISCV/RISCVSubtarget.h

Lines changed: 9 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"
@@ -73,6 +74,11 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
7374

7475
RISCVProcFamilyEnum RISCVProcFamily = Others;
7576

77+
// Do bidirectional scheduling by default since it provides a more balanced
78+
// scheduling leading to better performance. This will increase compile time.
79+
MISchedPostRASched::Direction PostRASchedDirection =
80+
MISchedPostRASched::Bidirectional;
81+
7682
#define GET_SUBTARGETINFO_MACRO(ATTRIBUTE, DEFAULT, GETTER) \
7783
bool ATTRIBUTE = DEFAULT;
7884
#include "RISCVGenSubtargetInfo.inc"
@@ -330,6 +336,9 @@ class RISCVSubtarget : public RISCVGenSubtargetInfo {
330336

331337
void overrideSchedPolicy(MachineSchedPolicy &Policy,
332338
unsigned NumRegionInstrs) const override;
339+
340+
void overridePostRASchedPolicy(MachineSchedPolicy &Policy,
341+
unsigned NumRegionInstrs) const override;
333342
};
334343
} // End llvm namespace
335344

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)