/
0006-BEC-RISCV-Add-reliability-aware-post-ra-instruction-.patch
298 lines (287 loc) · 10.5 KB
/
0006-BEC-RISCV-Add-reliability-aware-post-ra-instruction-.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
From 397239a994fa76e4ed8de9b4b7e1904961b8acbb Mon Sep 17 00:00:00 2001
From: "yousun.ko@yonsei.ac.kr" <yousun.ko@yonsei.ac.kr>
Date: Sat, 18 Nov 2023 22:02:37 +0900
Subject: [PATCH 6/7] [BEC][RISCV] Add reliability-aware post-ra instruction
scheduler for RISC-V
---
llvm/lib/Target/RISCV/CMakeLists.txt | 1 +
.../Target/RISCV/RISCVMachineScheduler.cpp | 128 ++++++++++++++++++
llvm/lib/Target/RISCV/RISCVMachineScheduler.h | 38 ++++++
llvm/lib/Target/RISCV/RISCVSubtarget.h | 1 +
llvm/lib/Target/RISCV/RISCVTargetMachine.cpp | 25 +++-
5 files changed, 192 insertions(+), 1 deletion(-)
create mode 100644 llvm/lib/Target/RISCV/RISCVMachineScheduler.cpp
create mode 100644 llvm/lib/Target/RISCV/RISCVMachineScheduler.h
diff --git a/llvm/lib/Target/RISCV/CMakeLists.txt b/llvm/lib/Target/RISCV/CMakeLists.txt
index 89d0851e21ff..7310cccee840 100644
--- a/llvm/lib/Target/RISCV/CMakeLists.txt
+++ b/llvm/lib/Target/RISCV/CMakeLists.txt
@@ -49,6 +49,7 @@ add_llvm_target(RISCVCodeGen
RISCVKnownBitsInfo.cpp
RISCVPostRAFaultIndexCoalescer.cpp
+ RISCVMachineScheduler.cpp
LINK_COMPONENTS
Analysis
diff --git a/llvm/lib/Target/RISCV/RISCVMachineScheduler.cpp b/llvm/lib/Target/RISCV/RISCVMachineScheduler.cpp
new file mode 100644
index 000000000000..43623f9eac51
--- /dev/null
+++ b/llvm/lib/Target/RISCV/RISCVMachineScheduler.cpp
@@ -0,0 +1,128 @@
+//===-- RISCVMachineScheduler.cpp - MI Scheduler for RISC-V ---------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#include "RISCVMachineScheduler.h"
+#include "MCTargetDesc/RISCVMCTargetDesc.h"
+
+using namespace llvm;
+
+bool RISCVPostRASchedStrategy::tryCandidate(SchedCandidate &Cand,
+ SchedCandidate &TryCand) {
+ // From llvm/lib/CodeGen/MachineScheduler.cpp:PostGenericScheduler::tryCandidate
+ if ((Cand.SU && Cand.SU->isInstr()) && (TryCand.SU && TryCand.SU->isInstr())) {
+ MachineInstr* CandMI = Cand.SU->getInstr();
+ MachineInstr* TryCandMI = TryCand.SU->getInstr();
+ // Number of bits that are live out after this MI
+ unsigned bitsCand = 0;
+ unsigned bitsTryCand = 0;
+
+ // Compute for Cand
+ for (MachineOperand &MOP : CandMI->operands()) {
+ if (CandMI->RIMap.find(&MOP) != CandMI->RIMap.end()) {
+ for (unsigned i = 0, e = CandMI->RIMap[&MOP].size(); i < e; i++) {
+ if (CandMI->RIMap[&MOP][i].second != 0) {
+ bitsCand++;
+ }
+ }
+ }
+ }
+
+ // Compute for TryCand
+ for (MachineOperand &MOP : TryCandMI->operands()) {
+ if (TryCandMI->RIMap.find(&MOP) != TryCandMI->RIMap.end()) {
+ for (unsigned i = 0, e = TryCandMI->RIMap[&MOP].size(); i < e; i++) {
+ if (TryCandMI->RIMap[&MOP][i].second != 0) {
+ bitsTryCand++;
+ }
+ }
+ }
+ }
+
+ // Return true if TryCand is better
+ // select to minimize the by live out bits (best)
+ if (bitsCand > bitsTryCand) {
+ TryCand.Reason = NodeOrder;
+ return true;
+ } else {
+ return false;
+ }
+
+ /*
+ // select to maximze the live out bits (worst)
+ if (bitsCand < bitsTryCand) {
+ TryCand.Reason = NodeOrder;
+ return true;
+ } else {
+ return false;
+ }
+ */
+ }
+
+ // Initialize the candidate if needed.
+ if (!Cand.isValid()) {
+ TryCand.Reason = NodeOrder;
+ return true;
+ }
+
+ // Prioritize instructions that read unbuffered resources by stall cycles.
+ if (tryLess(Top.getLatencyStallCycles(TryCand.SU),
+ Top.getLatencyStallCycles(Cand.SU), TryCand, Cand, Stall))
+ return TryCand.Reason != NoCand;
+
+ // Keep clustered nodes together.
+ if (tryGreater(TryCand.SU == DAG->getNextClusterSucc(),
+ Cand.SU == DAG->getNextClusterSucc(), TryCand, Cand, Cluster))
+ return TryCand.Reason != NoCand;
+
+ // Avoid critical resource consumption and balance the schedule.
+ if (tryLess(TryCand.ResDelta.CritResources, Cand.ResDelta.CritResources,
+ TryCand, Cand, ResourceReduce))
+ return TryCand.Reason != NoCand;
+ if (tryGreater(TryCand.ResDelta.DemandedResources,
+ Cand.ResDelta.DemandedResources, TryCand, Cand,
+ ResourceDemand))
+ return TryCand.Reason != NoCand;
+
+ // Avoid serializing long latency dependence chains.
+ if (Cand.Policy.ReduceLatency && tryLatency(TryCand, Cand, Top)) {
+ return TryCand.Reason != NoCand;
+ }
+
+ // Fall through to original instruction order.
+ if (TryCand.SU->NodeNum < Cand.SU->NodeNum)
+ TryCand.Reason = NodeOrder;
+
+ // PostGenericScheduler::tryCandidate end
+
+ // Add powerpc post ra specific heuristic only when TryCand isn't selected or
+ // selected as node order.
+ if (TryCand.Reason != NodeOrder && TryCand.Reason != NoCand)
+ return true;
+
+ return TryCand.Reason != NoCand;
+}
+
+void RISCVPostRASchedStrategy::enterMBB(MachineBasicBlock *MBB) {
+ // Custom PPC PostRA specific behavior here.
+ PostGenericScheduler::enterMBB(MBB);
+}
+
+void RISCVPostRASchedStrategy::leaveMBB() {
+ // Custom PPC PostRA specific behavior here.
+ PostGenericScheduler::leaveMBB();
+}
+
+void RISCVPostRASchedStrategy::initialize(ScheduleDAGMI *Dag) {
+ // Custom PPC PostRA specific initialization here.
+ PostGenericScheduler::initialize(Dag);
+}
+
+SUnit *RISCVPostRASchedStrategy::pickNode(bool &IsTopNode) {
+ // Custom PPC PostRA specific scheduling here.
+ return PostGenericScheduler::pickNode(IsTopNode);
+}
diff --git a/llvm/lib/Target/RISCV/RISCVMachineScheduler.h b/llvm/lib/Target/RISCV/RISCVMachineScheduler.h
new file mode 100644
index 000000000000..94c7d3de3529
--- /dev/null
+++ b/llvm/lib/Target/RISCV/RISCVMachineScheduler.h
@@ -0,0 +1,38 @@
+//===--- RISCVMachineScheduler.h - MI scheduler for RISC-V ----------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Custom RISCV MI scheduler.
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIB_TARGET_RISCV_RISCVMACHINESCHEDULER_H
+#define LLVM_LIB_TARGET_RISCV_RISCVMACHINESCHEDULER_H
+
+#include "llvm/CodeGen/MachineScheduler.h"
+
+namespace llvm {
+
+/// A MachineSchedStrategy implementation for RISCV post RA scheduling.
+class RISCVPostRASchedStrategy : public PostGenericScheduler {
+public:
+ RISCVPostRASchedStrategy(const MachineSchedContext *C) :
+ PostGenericScheduler(C) {}
+
+protected:
+ void initialize(ScheduleDAGMI *Dag) override;
+ SUnit *pickNode(bool &IsTopNode) override;
+ void enterMBB(MachineBasicBlock *MBB) override;
+ void leaveMBB() override;
+
+ bool tryCandidate(SchedCandidate &Cand, SchedCandidate &TryCand) override;
+ bool biasAddiCandidate(SchedCandidate &Cand, SchedCandidate &TryCand) const;
+};
+
+} // end namespace llvm
+
+#endif // LLVM_LIB_TARGET_RISCV_RISCVMACHINESCHEDULER_H
diff --git a/llvm/lib/Target/RISCV/RISCVSubtarget.h b/llvm/lib/Target/RISCV/RISCVSubtarget.h
index 230e2e371d77..8c253b1fdda5 100644
--- a/llvm/lib/Target/RISCV/RISCVSubtarget.h
+++ b/llvm/lib/Target/RISCV/RISCVSubtarget.h
@@ -99,6 +99,7 @@ public:
return &TSInfo;
}
bool enableMachineScheduler() const override { return true; }
+ bool enablePostRAScheduler() const override { return true; }
/// Returns RISCV processor family.
/// Avoid this function! CPU specifics should be kept local to this class
diff --git a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
index 1417b08d7b6a..a898528451fe 100644
--- a/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
+++ b/llvm/lib/Target/RISCV/RISCVTargetMachine.cpp
@@ -14,6 +14,7 @@
#include "MCTargetDesc/RISCVBaseInfo.h"
#include "RISCV.h"
#include "RISCVMachineFunctionInfo.h"
+#include "RISCVMachineScheduler.h"
#include "RISCVMacroFusion.h"
#include "RISCVTargetObjectFile.h"
#include "RISCVTargetTransformInfo.h"
@@ -24,6 +25,7 @@
#include "llvm/CodeGen/GlobalISel/InstructionSelect.h"
#include "llvm/CodeGen/GlobalISel/Legalizer.h"
#include "llvm/CodeGen/GlobalISel/RegBankSelect.h"
+#include "llvm/CodeGen/MachineScheduler.h"
#include "llvm/CodeGen/MIRParser/MIParser.h"
#include "llvm/CodeGen/MIRYamlMapping.h"
#include "llvm/CodeGen/Passes.h"
@@ -98,6 +100,13 @@ static Reloc::Model getEffectiveRelocModel(const Triple &TT,
return RM.value_or(Reloc::Static);
}
+static ScheduleDAGInstrs *createRISCVPostMachineScheduler(
+ MachineSchedContext *C) {
+ ScheduleDAGMI *DAG =
+ new ScheduleDAGMI(C, std::make_unique<RISCVPostRASchedStrategy>(C), true);
+ return DAG;
+}
+
RISCVTargetMachine::RISCVTargetMachine(const Target &T, const Triple &TT,
StringRef CPU, StringRef FS,
const TargetOptions &Options,
@@ -222,7 +231,10 @@ namespace {
class RISCVPassConfig : public TargetPassConfig {
public:
RISCVPassConfig(RISCVTargetMachine &TM, PassManagerBase &PM)
- : TargetPassConfig(TM, PM) {}
+ : TargetPassConfig(TM, PM) {
+ if (TM.getOptLevel() != CodeGenOpt::None)
+ substitutePass(&PostRASchedulerID, &PostMachineSchedulerID);
+ }
RISCVTargetMachine &getRISCVTargetMachine() const {
return getTM<RISCVTargetMachine>();
@@ -241,12 +253,18 @@ public:
ScheduleDAGInstrs *
createPostMachineScheduler(MachineSchedContext *C) const override {
+ if (true /* enable soft-error reliability-aware post-ra scheduling */) {
+ return createRISCVPostMachineScheduler(C);
+ }
+
const RISCVSubtarget &ST = C->MF->getSubtarget<RISCVSubtarget>();
if (ST.hasMacroFusion()) {
ScheduleDAGMI *DAG = createGenericSchedPostRA(C);
DAG->addMutation(createRISCVMacroFusionDAGMutation());
return DAG;
}
+
+
return nullptr;
}
@@ -367,6 +385,11 @@ void RISCVPassConfig::addPostRegAlloc() {
addPass(createRISCVRedundantCopyEliminationPass());
}
+static MachineSchedRegistry
+RISCVPostRASchedRegistry("riscv-postra",
+ "Run RISCV Post-RA scheduler",
+ createRISCVPostMachineScheduler);
+
yaml::MachineFunctionInfo *
RISCVTargetMachine::createDefaultFuncInfoYAML() const {
return new yaml::RISCVMachineFunctionInfo();
--
2.25.1