-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.cpp
More file actions
123 lines (102 loc) · 3.49 KB
/
Copy pathcommon.cpp
File metadata and controls
123 lines (102 loc) · 3.49 KB
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
#include "common.h"
#include <cassert>
#include <cstdio>
#include <string>
#include <unordered_map>
using namespace std;
void parse(FILE* dfg_file,
FILE* op_file,
FILE* constr_file,
DFG* dfg,
vector<Op*>& ops,
std::vector<Constr*>& constrs,
double& clock_period) {
int n_mem, n_input, n_stmts;
int n_ops;
int n_constrs;
fscanf(dfg_file, "%d%d%d", &n_mem, &n_input, &n_stmts);
fscanf(op_file, "%d%lf", &n_ops, &clock_period);
fscanf(constr_file, "%d", &n_constrs);
ops.resize(n_ops);
unordered_map<string, tuple<Op*, int>> op_mapping;
for (int i = 0; i < n_ops; ++i) {
char name[128];
fscanf(op_file, "%s", name);
string name_str(name);
int has_result, n_operand, latency, limit;
double delay;
fscanf(op_file, "%d%lf%d%d", &n_operand, &delay, &latency, &limit);
ops[i] = new Op{latency, limit, i, delay, name_str};
op_mapping[name_str] = {ops[i], n_operand};
}
constrs.resize(n_constrs);
for (int i = 0; i < n_constrs; ++i) {
int op_0, op_1, difference;
fscanf(constr_file, "%d%d%d", &op_0, &op_1, &difference);
constrs[i] = new Constr{op_0, op_1, difference};
}
dfg->num_memory = n_mem;
for (int i = 0; i < n_stmts; ++i) {
char name[128];
fscanf(dfg_file, "%s", name);
string name_str(name);
assert(op_mapping.find(name_str) != op_mapping.end());
int n_operands = get<1>(op_mapping[name_str]);
Op* op = get<0>(op_mapping[name_str]);
vector<int> operands;
for (int j = 0; j < n_operands; ++j) {
int opr;
fscanf(dfg_file, "%d", &opr);
if (opr == -1 || (n_mem < opr && opr <= n_input + n_mem)) {
continue;
}
// In the data structure, we number variables starting from 0
if (opr > n_input + n_mem) {
operands.push_back(opr - n_input - n_mem - 1);
} else {
operands.push_back(opr - 1);
}
}
if (name_str == "load" || name_str == "store") {
dfg->stmts.push_back(new MemStmt);
((MemStmt*)dfg->stmts.back())->arr_idx = operands[0];
operands.erase(operands.begin());
} else {
dfg->stmts.push_back(new Stmt);
}
vector<Stmt*> ins;
for (int i : operands) {
ins.push_back(dfg->stmts[i]);
}
dfg->stmts.back()->ins = std::move(ins);
dfg->stmts.back()->idx = i;
dfg->stmts.back()->op = op;
dfg->stmts.back()->start_cycle = -1;
}
}
void get_deps_and_uses(DFG* dfg, vec2d<int>& deps, vec2d<int>& uses) {
int n = dfg->stmts.size();
deps = vec2d<int>(n);
uses = vec2d<int>(n);
for (int i = 0; i < n; ++i) {
for (auto in : dfg->stmts[i]->ins) {
uses[in->idx].push_back(i);
deps[i].push_back(in->idx);
}
}
for (int i = 0; i < n; ++i) {
for (int j = i + 1; j < n; ++j) {
if (!dfg->stmts[i]->is_mem_stmt() || !dfg->stmts[j]->is_mem_stmt()) {
continue;
}
if (dfg->stmts[i]->get_arr_idx() != dfg->stmts[j]->get_arr_idx()) {
continue;
}
if (dfg->stmts[i]->op->name == "load" && dfg->stmts[j]->op->name == "load") {
continue;
}
uses[i].push_back(j);
deps[j].push_back(i);
}
}
}