-
Notifications
You must be signed in to change notification settings - Fork 17
/
OptSchedMachineWrapper.cpp
298 lines (255 loc) · 10.5 KB
/
OptSchedMachineWrapper.cpp
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
298
/*******************************************************************************
Description: A wrapper that convert an LLVM target to an OptSched MachineModel.
*******************************************************************************/
#include "OptSchedMachineWrapper.h"
#include "opt-sched/Scheduler/config.h"
#include "opt-sched/Scheduler/logger.h"
#include "opt-sched/Scheduler/machine_model.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/ScheduleDAGInstrs.h"
#include "llvm/CodeGen/TargetInstrInfo.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/CodeGen/TargetRegisterInfo.h"
#include "llvm/MC/MCInstrItineraries.h"
#include "llvm/Support/TargetRegistry.h"
#include "llvm/Target/TargetMachine.h"
#include <memory>
#define DEBUG_TYPE "optsched-machine-model"
using namespace llvm;
using namespace llvm::opt_sched;
namespace {
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
LLVM_DUMP_METHOD
void dumpInstType(InstTypeInfo &instType, MachineModel *mm) {
Logger::Info("Adding new instruction type.\n \
Name: %s\n \
Is Context Dependent: %s\n \
IssueType: %s\n \
Latency: %d\n \
Is Pipelined %s\n \
Supported: %s\n \
Blocks Cycle: %s\n",
instType.name.c_str(), instType.isCntxtDep ? "True" : "False",
mm->GetIssueTypeNameByCode(instType.issuType), instType.ltncy,
instType.pipelined ? "True" : "False",
instType.sprtd ? "True" : "False",
instType.blksCycle ? "True" : "False");
}
#endif
std::unique_ptr<MachineModelGenerator>
createCortexA7MMGenerator(const llvm::ScheduleDAGInstrs *dag,
MachineModel *mm) {
return make_unique<CortexA7MMGenerator>(dag, mm);
}
std::unique_ptr<MachineModelGenerator>
createCortexA53MMGenerator(const llvm::ScheduleDAGInstrs *dag,
MachineModel *mm) {
return make_unique<CortexA53MMGenerator>(dag, mm);
}
} // end anonymous namespace
OptSchedMachineModel::OptSchedMachineModel(const char *configFile)
: MachineModel(configFile), shouldGenerateMM(false), MMGen(nullptr) {}
void OptSchedMachineModel::convertMachineModel(
const ScheduleDAGInstrs &Dag, const RegisterClassInfo *RegClassInfo) {
const TargetMachine &Target = Dag.TM;
const TargetSchedModel *LLVMSchedModel = Dag.getSchedModel();
const TargetSubtargetInfo *SubtargetInfo = LLVMSchedModel->getSubtargetInfo();
std::string ExactCPU = SubtargetInfo->getCPU().str();
mdlName_ = Target.getTarget().getName();
LLVM_DEBUG(dbgs() << "Machine model: " << mdlName_.c_str() << '\n');
// Should we try to generate a machine model using LLVM itineraries.
shouldGenerateMM =
SchedulerOptions::getInstance().GetBool("GENERATE_MACHINE_MODEL", false);
if (shouldGenerateMM) {
if (mdlName_ == "ARM-Cortex-A7")
MMGen = createCortexA7MMGenerator(&Dag, this);
else if (ExactCPU == "cortex-a53")
MMGen = createCortexA53MMGenerator(&Dag, this);
else
llvm::report_fatal_error(
"Could not find machine model generator for target " + mdlName_,
false);
}
InstTypeInfo instType;
instType.name = "Default";
instType.isCntxtDep = false;
instType.issuType = 0;
instType.ltncy = 1;
instType.pipelined = true;
instType.sprtd = true;
instType.blksCycle = false;
instTypes_.push_back(instType);
instType.name = "artificial";
instType.isCntxtDep = false;
instType.issuType = 0;
instType.ltncy = 1;
instType.pipelined = true;
instType.sprtd = true;
instType.blksCycle = false;
instTypes_.push_back(instType);
// Clear The registerTypes list to read registers limits from the LLVM machine
// model
registerTypes_.clear();
if (mdlName_ == "amdgcn") {
RegTypeInfo SGPR32;
SGPR32.name = "SGPR32";
SGPR32.count = 80;
registerTypes_.push_back(SGPR32);
RegTypeInfo VGPR32;
VGPR32.name = "VGPR32";
VGPR32.count = 24;
registerTypes_.push_back(VGPR32);
} else {
const auto *TRI = Dag.TRI;
for (unsigned pSet = 0; pSet < TRI->getNumRegPressureSets(); ++pSet) {
RegTypeInfo regType;
regType.name = TRI->getRegPressureSetName(pSet);
int pressureLimit = RegClassInfo->getRegPressureSetLimit(pSet);
// set registers with 0 limit to 1 to support flags and special cases
if (pressureLimit > 0)
regType.count = pressureLimit;
else
regType.count = 1;
registerTypes_.push_back(regType);
}
}
// initialize other MM non-instruction specific info if appropriate
if (shouldGenerateMM && MMGen->generatesAllData())
MMGen->generateProcessorData(&mdlName_, &issueRate_);
}
CortexA7MMGenerator::CortexA7MMGenerator(const llvm::ScheduleDAGInstrs *dag,
MachineModel *mm)
: DAG(dag), MM(mm) {
IID = dag->getSchedModel()->getInstrItineraries();
}
bool CortexA7MMGenerator::isMIPipelined(const MachineInstr *inst,
unsigned idx) const {
const InstrStage *IS = IID->beginStage(idx);
const InstrStage *E = IID->endStage(idx);
for (; IS != E; ++IS)
if (IS->getCycles() > 1)
// Instruction contains a non-pipelined stage
return false;
// All stages can be pipelined
return true;
}
IssueType CortexA7MMGenerator::generateIssueType(const InstrStage *E) const {
IssueType type = INVALID_ISSUE_TYPE;
// Get functional units for the last stage in the itinerary
const unsigned units = E->getUnits();
if (units & NLSPipe)
type = MM->GetIssueTypeByName("NLSPipe");
else if (units & NPipe)
type = MM->GetIssueTypeByName("NPipe");
else if (units & LSPipe)
type = MM->GetIssueTypeByName("LSPipe");
else if (units & Pipe0 || units & Pipe1)
type = MM->GetIssueTypeByName("ALUPipe");
else
type = MM->getDefaultIssueType();
assert(type != INVALID_ISSUE_TYPE && "Could not find issue type for "
"instruction, is the correct machine "
"model file loaded?");
return type;
}
InstType CortexA7MMGenerator::generateInstrType(const MachineInstr *instr) {
// Search in the machine model for an instType with this OpCode
const std::string instrName = DAG->TII->getName(instr->getOpcode());
const InstType InstType = MM->GetInstTypeByName(instrName);
// If the machine model does not have instType with this OpCode name,
// generate a type for the instruction.
if (InstType != INVALID_INST_TYPE)
return InstType;
else {
LLVM_DEBUG(dbgs() << "Generating instr type for " << instrName.c_str());
SUnit *SU = DAG->getSUnit(const_cast<MachineInstr *>(instr));
const MCInstrDesc *instDesc = DAG->getInstrDesc(SU);
unsigned IDX = instDesc->getSchedClass();
if (!IID || IID->isEmpty() || IID->isEndMarker(IDX))
return MM->getDefaultInstType();
// Create the new instruction type
InstTypeInfo InstTypeI;
InstTypeI.name = instrName;
const InstrStage *E = IID->endStage(IDX);
InstTypeI.issuType = generateIssueType(--E);
InstTypeI.isCntxtDep = false;
InstTypeI.ltncy = 1; // Assume using "rough" llvm latencies
InstTypeI.pipelined = isMIPipelined(instr, IDX);
InstTypeI.sprtd = true;
InstTypeI.blksCycle = false; // TODO: Find a more precise value for this.
LLVM_DEBUG(dumpInstType(InstTypeI, MM));
// Add the new instruction type
MM->AddInstType(InstTypeI);
return MM->GetInstTypeByName(instrName);
}
}
void CortexA53MMGenerator::generateProcessorData(std::string *mdlName_,
int *issueRate_) {
const TargetSchedModel *LLVMSchedModel = DAG->getSchedModel();
const TargetSubtargetInfo *SubtargetInfo = LLVMSchedModel->getSubtargetInfo();
std::string ExactCPU = SubtargetInfo->getCPU().str();
*mdlName_ = ExactCPU;
*issueRate_ = LLVMSchedModel->getIssueWidth();
unsigned FUTypesCount = LLVMSchedModel->getNumProcResourceKinds();
ResourceIdToIssueType.resize(FUTypesCount);
for (unsigned FUIdx = 1; FUIdx < FUTypesCount; FUIdx++) {
const MCProcResourceDesc *FUResource =
LLVMSchedModel->getProcResource(FUIdx);
ResourceIdToIssueType[FUIdx] = FUResource->Name;
IssueTypeInfo ITI{FUResource->Name, static_cast<int>(FUResource->NumUnits)};
MM->addIssueType(ITI);
}
}
InstType
CortexA53MMGenerator::generateInstrType(const llvm::MachineInstr *instr) {
// Search in the machine model for an instType with this OpCode
const std::string InstrName = DAG->TII->getName(instr->getOpcode());
const InstType InstrType = MM->GetInstTypeByName(InstrName);
// If the machine model does not have instType with this OpCode name,
// generate a type for the instruction.
if (InstrType != INVALID_INST_TYPE)
return InstrType;
else {
const TargetSchedModel *LLVMSchedModel = DAG->getSchedModel();
const MCSchedClassDesc *MCDesc = LLVMSchedModel->resolveSchedClass(instr);
InstTypeInfo InstTypeI;
InstTypeI.name = InstrName;
// Most of the A53's instructions only use one FU (one uses 3 FUs and some
// use 0 FUs) we use the default issue type for instructions which use no FU
// If we were to see the 3 FU instruction then we assign it its first issue
// type (but I think the 3 FU inst is a type of branch isntruction that we
// never see)
const MCWriteProcResEntry *ResEntry =
LLVMSchedModel->getWriteProcResBegin(MCDesc);
if (ResEntry != LLVMSchedModel->getWriteProcResEnd(MCDesc)) {
std::string IssueTypeName =
ResourceIdToIssueType[ResEntry->ProcResourceIdx];
InstTypeI.issuType = MM->GetIssueTypeByName(IssueTypeName.c_str());
InstTypeI.pipelined = (ResEntry->Cycles == 1);
} else {
InstTypeI.issuType = MM->getDefaultIssueType();
InstTypeI.pipelined = true;
}
InstTypeI.ltncy = 1; // This field is never used
InstTypeI.isCntxtDep = false;
InstTypeI.sprtd = true;
InstTypeI.blksCycle = false;
Logger::Info(
"Adding new instruction type.\n"
"Name: %s\n"
"Is Context Dependent: %s\n"
"IssueType: %s (%d)\n"
"Latency: %d\n"
"Is Pipelined: %s\n"
"Supported: %s\n"
"Blocks Cycle: %s\n",
InstTypeI.name.c_str(), InstTypeI.isCntxtDep ? "True" : "False",
MM->GetIssueTypeNameByCode(InstTypeI.issuType), InstTypeI.issuType,
InstTypeI.ltncy, InstTypeI.pipelined ? "True" : "False",
InstTypeI.sprtd ? "True" : "False",
InstTypeI.blksCycle ? "True" : "False");
// Add the new instruction type
MM->AddInstType(InstTypeI);
return MM->GetInstTypeByName(InstrName);
}
}