Skip to content

Commit 79cc2ea

Browse files
committed
Small Refactor of MachineModel to facilitate tests
By allowing MachineModel to be constructed by a SpecsBuffer rather than a file, we can easily get a MachineModel in any tests that have one as a dependency.
1 parent 6a3dbe7 commit 79cc2ea

File tree

8 files changed

+100
-26
lines changed

8 files changed

+100
-26
lines changed

include/opt-sched/Scheduler/buffers.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ class InputBuffer {
9696
class SpecsBuffer : public InputBuffer {
9797
public:
9898
SpecsBuffer();
99+
explicit SpecsBuffer(char *buf, long size);
99100
void ReadSpec(const char *const title, char *value);
100101
void readLine(char *value, int maxPieceCnt);
101102
void readLstElmnt(char *value);

include/opt-sched/Scheduler/machine_model.h

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,14 @@ Last Update: Mar. 2011
1010
#ifndef OPTSCHED_BASIC_MACHINE_MODEL_H
1111
#define OPTSCHED_BASIC_MACHINE_MODEL_H
1212

13-
// For class ostream.
14-
#include <iostream>
15-
// For class string.
16-
#include <string>
17-
// For class vector.
1813
#include "opt-sched/Scheduler/defines.h"
14+
#include <string>
1915
#include <vector>
2016

2117
namespace llvm {
2218
namespace opt_sched {
2319

24-
using std::string;
25-
using std::vector;
20+
class SpecsBuffer;
2621

2722
// The possible types of dependence between two machine instructions.
2823
enum DependenceType {
@@ -55,7 +50,7 @@ const int MAX_ISSUTYPE_CNT = 20;
5550
// A description of an instruction type.
5651
struct InstTypeInfo {
5752
// The name of the instruction type.
58-
string name;
53+
std::string name;
5954
// Whether instructions of this type can be scheduled only in a particular
6055
// context.
6156
bool isCntxtDep;
@@ -77,7 +72,7 @@ struct InstTypeInfo {
7772
// A description of a issue type/FU.
7873
struct IssueTypeInfo {
7974
// The name of the issue type.
80-
string name;
75+
std::string name;
8176
// How many slots of this issue type the machine has per cycle.
8277
int slotsCount;
8378
};
@@ -86,12 +81,13 @@ struct IssueTypeInfo {
8681
class MachineModel {
8782
public:
8883
// Loads a machine model description from a file.
89-
MachineModel(const string &modelFile);
84+
MachineModel(const std::string &modelFile);
85+
MachineModel(SpecsBuffer &buf);
9086
// A no-op virtual destructor to allow proper subclassing.
9187
virtual ~MachineModel() {}
9288

9389
// Returns the name of the machine model.
94-
const string &GetModelName() const;
90+
const std::string &GetModelName() const;
9591
// Returns the number of instruction types.
9692
int GetInstTypeCnt() const;
9793
// Returns the number of issue types (pipelines).
@@ -104,7 +100,7 @@ class MachineModel {
104100
// Returns the number of registers of a given type.
105101
int GetPhysRegCnt(int16_t regType) const;
106102
// Returns the name of a given register type.
107-
const string &GetRegTypeName(int16_t regType) const;
103+
const std::string &GetRegTypeName(int16_t regType) const;
108104
// Returns the register type given its name.
109105
int16_t GetRegTypeByName(const char *const regTypeName) const;
110106
// Returns the number of issue slots for a given issue type.
@@ -126,8 +122,8 @@ class MachineModel {
126122
// Returns the instruction type given the name of the instruction as well
127123
// as the name of the previous instruction (used for context-dependent
128124
// instructions).
129-
InstType GetInstTypeByName(const string &typeName,
130-
const string &prevName = "") const;
125+
InstType GetInstTypeByName(const std::string &typeName,
126+
const std::string &prevName = "") const;
131127
// Return the default instruction type
132128
InstType getDefaultInstType() const;
133129
// Return the default issue type
@@ -161,9 +157,9 @@ class MachineModel {
161157
issueTypes_[0].slotsCount == 1 && !includesUnpipelined_;
162158
}
163159
// Add a new instruction type.
164-
void AddInstType(InstTypeInfo &instTypeInfo);
160+
void AddInstType(InstTypeInfo instTypeInfo);
165161
// Add a new issue type.
166-
void addIssueType(IssueTypeInfo &IssueTypeInfo);
162+
void addIssueType(IssueTypeInfo IssueTypeInfo);
167163

168164
protected:
169165
// Creates an uninitialized machine model. For use by subclasses.
@@ -172,13 +168,13 @@ class MachineModel {
172168
// A description of a register type.
173169
struct RegTypeInfo {
174170
// The name of the register.
175-
string name;
171+
std::string name;
176172
// How many register of this type the machine has.
177173
int count;
178174
};
179175

180176
// The name of the machine model.
181-
string mdlName_;
177+
std::string mdlName_;
182178
// The machine's issue rate. I.e. the total number of issue slots for all
183179
// issue types.
184180
int issueRate_;
@@ -188,11 +184,11 @@ class MachineModel {
188184
bool includesUnpipelined_ = false;
189185

190186
// A vector of instruction type descriptions.
191-
vector<InstTypeInfo> instTypes_;
187+
std::vector<InstTypeInfo> instTypes_;
192188
// A vector of register types with their names and counts.
193-
vector<RegTypeInfo> registerTypes_;
189+
std::vector<RegTypeInfo> registerTypes_;
194190
// A vector of issue types with their names and slot counts.
195-
vector<IssueTypeInfo> issueTypes_;
191+
std::vector<IssueTypeInfo> issueTypes_;
196192
};
197193

198194
} // namespace opt_sched

lib/Scheduler/buffers.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,3 +580,6 @@ FUNC_RESULT SpecsBuffer::checkTitle(const char *const title) {
580580
}
581581

582582
SpecsBuffer::SpecsBuffer() { nxtLineType = NXT_DATA; }
583+
SpecsBuffer::SpecsBuffer(char *buf, long size) : SpecsBuffer() {
584+
SetBuf(buf, size);
585+
}

lib/Scheduler/machine_model.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,16 @@
1111

1212
using namespace llvm::opt_sched;
1313

14-
MachineModel::MachineModel(const string &modelFile) {
15-
SpecsBuffer buf;
16-
char buffer[MAX_NAMESIZE];
14+
using std::string;
15+
using std::vector;
1716

17+
MachineModel::MachineModel(const std::string &modelFile) {
18+
SpecsBuffer buf;
1819
buf.Load(modelFile.c_str());
20+
}
21+
22+
MachineModel::MachineModel(SpecsBuffer &buf) {
23+
char buffer[MAX_NAMESIZE];
1924

2025
buf.ReadSpec("MODEL_NAME:", buffer);
2126
mdlName_ = buffer;
@@ -197,15 +202,15 @@ bool MachineModel::IsFloat(InstType instTypeCode) const {
197202
return instTypes_[instTypeCode].name[0] == 'f';
198203
}
199204

200-
void MachineModel::AddInstType(InstTypeInfo &instTypeInfo) {
205+
void MachineModel::AddInstType(InstTypeInfo instTypeInfo) {
201206
// If this new instruction type is unpipelined notify the model
202207
if (!instTypeInfo.pipelined)
203208
includesUnpipelined_ = true;
204209

205210
instTypes_.push_back(std::move(instTypeInfo));
206211
}
207212

208-
void MachineModel::addIssueType(IssueTypeInfo &IssueTypeInfo) {
213+
void MachineModel::addIssueType(IssueTypeInfo IssueTypeInfo) {
209214
issueTypes_.push_back(std::move(IssueTypeInfo));
210215
}
211216

lib/Scheduler/ready_list.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include "opt-sched/Scheduler/logger.h"
44
#include "opt-sched/Scheduler/utilities.h"
55

6+
#include <iostream>
7+
68
using namespace llvm::opt_sched;
79

810
ReadyList::ReadyList(DataDepGraph *dataDepGraph, SchedPriorities prirts) {

unittests/Basic/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ add_optsched_unittest(OptSchedBasicTests
44
LinkedListTest.cpp
55
LoggerTest.cpp
66
UtilitiesTest.cpp
7+
simple_machine_model_test.cpp
78
)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#ifndef OPTSCHED_SIMPLE_MACHINE_MODEL_H
2+
#define OPTSCHED_SIMPLE_MACHINE_MODEL_H
3+
4+
#include <string.h> // strdup is in the C header, but not the C++ header
5+
6+
#include "opt-sched/Scheduler/buffers.h"
7+
#include "opt-sched/Scheduler/machine_model.h"
8+
9+
inline llvm::opt_sched::MachineModel simpleMachineModel() {
10+
static constexpr const char SimpleModel[] = R"(
11+
MODEL_NAME: Simple
12+
13+
# The limit on the total number of instructions that can be issued in one cycle
14+
ISSUE_RATE: 1
15+
16+
# Each instruction must have an issue type, i.e. a function unit that the instruction uses.
17+
ISSUE_TYPE_COUNT: 1
18+
19+
# Default issue type for LLVM instructions.
20+
Default 1
21+
22+
DEP_LATENCY_ANTI: 0
23+
DEP_LATENCY_OUTPUT: 1
24+
DEP_LATENCY_OTHER: 1
25+
26+
# This will not be used. Reg type info will be taken from the compiler.
27+
REG_TYPE_COUNT: 2
28+
I 1
29+
F 1
30+
31+
# Set this to the total number of instructions
32+
INST_TYPE_COUNT: 2
33+
34+
INST_TYPE: artificial
35+
ISSUE_TYPE: Default
36+
LATENCY: 0
37+
PIPELINED: YES
38+
BLOCKS_CYCLE: NO
39+
SUPPORTED: NO
40+
41+
INST_TYPE: Inst
42+
ISSUE_TYPE: Default
43+
LATENCY: 1
44+
PIPELINED: YES
45+
BLOCKS_CYCLE: NO
46+
SUPPORTED: YES
47+
)";
48+
49+
llvm::opt_sched::SpecsBuffer Buf(strdup(SimpleModel), sizeof(SimpleModel));
50+
llvm::opt_sched::MachineModel Model(Buf);
51+
return Model;
52+
}
53+
54+
#endif
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "simple_machine_model.h"
2+
3+
#include "gtest/gtest.h"
4+
5+
using llvm::opt_sched::MachineModel;
6+
7+
namespace {
8+
TEST(SimpleMachineModel, CanBeLoaded) {
9+
MachineModel Model = simpleMachineModel();
10+
EXPECT_EQ(1, Model.GetIssueRate());
11+
}
12+
} // namespace

0 commit comments

Comments
 (0)