-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDSWP_5.cpp
257 lines (211 loc) · 7.5 KB
/
DSWP_5.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
// 5th step - inserting thread synchronization
#include "DSWP.h"
using namespace llvm;
using namespace std;
void DSWP::insertSynchronization(Loop *L) {
//this->insertSynDependecy(L);
cout << "inserting sychronization" << endl;
int channel = 0;
for (unsigned int i = 0; i < allEdges.size(); i++) {
Edge e = allEdges[i];
// int t1 = assigned[sccId[e.u]];
// int t2 = assigned[sccId[e.v]];
// if (t1 == t2) //they are in same thread, therefore no need to syn
// continue;
for (int utr = 0; utr < MAX_THREAD; utr++) {
for (int vtr = 0; vtr < MAX_THREAD; vtr++) {
if (utr == vtr)
continue;
if (getInstAssigned(e.u) != utr)
continue;
if (instMap[utr][e.u] == NULL || instMap[vtr][e.v] == NULL)
continue;
Instruction * nu = dyn_cast<Instruction>(instMap[utr][e.u]);
Instruction * nv = dyn_cast<Instruction>(instMap[vtr][e.v]);
if (nu == NULL || nv == NULL)
continue;
insertProduce(nu, nv, e.dtype, channel, utr, vtr);
insertConsume(nu, nv, e.dtype, channel, utr, vtr);
channel++;
}
}
// if (isa<TerminatorInst>(e.v)) { //so e.v is a branch that could be copy into many thread
// //vector<Value*> termList = termMap[e.v];
// for (unsigned j = 0; j < MAX_THREAD; j++) {
// Value *vv = instMap[j][e.v];
// if (vv == NULL)
// continue;
// //insertProduce(dyn_cast<Instruction>(oldToNew[e.u]), dyn_cast<Instruction>(oldToNew[e.v]), e.dtype, channel);
// //insertConsume(dyn_cast<Instruction>(oldToNew[e.u]), dyn_cast<Instruction>(vv), e.dtype, channel);
// channel++;
// }
// } else {
//
// //should get thread num first!!
// int uthr = this->getInstAssigned(e.u);
// int vthr = this->getInstAssigned(e.v);
//// e.u->dump();
//// e.v->dump();
//// Value *tu = oldToNew[e.u];
//// tu->dump();
//// Instruction *nu = dyn_cast<Instruction>(tu);
// //Instruction *nv = dyn_cast<Instruction>(oldToNew[e.u]);
//
// insertProduce(dyn_cast<Instruction>(instMap[uthr][e.u]), dyn_cast<Instruction>(instMap[vthr][e.v]), e.dtype, channel, uthr, vthr);
// insertConsume(dyn_cast<Instruction>(instMap[uthr][e.u]), dyn_cast<Instruction>(instMap[vthr][e.v]), e.dtype, channel, uthr, vthr);
// channel++;
// }
}
//cout << channel << endl;
// // remaining functions are auxiliary
// for (unsigned i = 1; i < this->allFunc.size(); i++) {
// Function *curr = this->allFunc[i];
// // check each instruction and insert flows
// }
}
void DSWP::insertSynDependecy(Loop *L) {
MemoryDependenceAnalysis &mda = getAnalysis<MemoryDependenceAnalysis>();
for (Loop::block_iterator bi = L->getBlocks().begin(); bi != L->getBlocks().end(); bi++) {
BasicBlock *BB = *bi;
for (BasicBlock::iterator ii = BB->begin(); ii != BB->end(); ii++) {
Instruction *inst = &(*ii);
MemDepResult mdr = mda.getDependency(inst);
if (mdr.isDef()) {
Instruction *dep = mdr.getInst();
if (isa<LoadInst> (inst)) {
if (isa<LoadInst> (dep)) {
addEdge(dep, inst, DSYN); //READ AFTER READ
}
}
}
}
}
}
void DSWP::insertProduce(Instruction * u, Instruction *v, DType dtype, int channel, int uthread, int vthread) {
Function *fun = module->getFunction("sync_produce");
vector<Value*> args;
Instruction *insPos = u->getNextNode();
if (insPos == NULL) {
error("here cannot be null");
}
//if (isa<BranchInst>(u)) {
// error("I don't know how do deal with it");
// return;
//}
if (dtype == REG) { //register dep
CastInst *cast;
if (u->getType()->isIntegerTy()) {
cast = new SExtInst(u, eleType, u->getNameStr() + "_64");
}
else if (u->getType()->isFloatingPointTy()) {
if (u->getType()->isFloatTy()) {
error("float sucks");
}
cast = new BitCastInst(u, eleType, u->getNameStr() + "_64");
}
else if (u->getType()->isPointerTy()){
cast = new PtrToIntInst(u, eleType, u->getNameStr() + "_64"); //cast value
} else {
error("what's the hell type");
}
cast->insertBefore(insPos);
args.push_back(cast); //push the value
}
else if (dtype == DTRUE) { //true dep
error("check mem dep!!");
StoreInst *store = dyn_cast<StoreInst>(u);
if (store == NULL) {
error("not true dependency!");
}
BitCastInst *cast = new BitCastInst(store->getOperand(0), Type::getInt8PtrTy(*context), u->getNameStr() + "_ptr");
cast->insertBefore(insPos);
args.push_back(cast); //push the value //insert call
} else { //others
args.push_back(Constant::getNullValue( Type::getInt64Ty(*context) ) ); //just a dummy value
}
args.push_back(ConstantInt::get(Type::getInt32Ty(*context), channel));
CallInst *call = CallInst::Create(fun, args.begin(), args.end()); //call it
// call->setName("p" + channel);
call->insertBefore(insPos); //insert call
}
void DSWP::insertConsume(Instruction * u, Instruction *v, DType dtype, int channel, int uthread, int vthread) {
// int uthread = this->getNewInstAssigned(u);
// int vthread = this->getNewInstAssigned(v);
Value *oldu = newToOld[u];
Function *fun = module->getFunction("sync_consume");
vector<Value*> args;
args.push_back(ConstantInt::get(Type::getInt32Ty(*context), channel));
CallInst *call = CallInst::Create(fun, args.begin(), args.end(), "c" + itoa(channel));
call->insertBefore(v);
if (dtype == REG) {
CastInst *cast;
if (u->getType()->isIntegerTy()) {
cast = new TruncInst(call, u->getType(), call->getNameStr() + "_val");
}
else if (u->getType()->isFloatingPointTy()) {
if (u->getType()->isFloatTy())
error("cannot deal with double");
cast = new BitCastInst(call, u->getType(), call->getNameStr() + "_val"); //cast value
}
else if (u->getType()->isPointerTy()){
cast = new IntToPtrInst(call, u->getType(), call->getNameStr() + "_val"); //cast value
} else {
error("what's the hell type");
}
cast->insertBefore(v);
// for (unsigned i = 0; i < v->getNumOperands(); i++) {
// Value *op = v->getOperand(i);
// int opthread = this->getNewInstAssigned(op);
// if (op == u) {
// v->setOperand(i, cast);
// }
// }
//cout << "replace reg" << endl;
//TODO perhaps also need to replace other elements after this User
for (Instruction::use_iterator ui = oldu->use_begin(); ui != oldu->use_end(); ui++) { // this is diff from next block for DTRUE
Instruction *user = dyn_cast<Instruction>(*ui);
if (user == NULL) {
error("how could it be NULL");
}
// int userthread = this->getNewInstAssigned(user);
if (user->getParent()->getParent() != v->getParent()->getParent()) {
continue;
}
//user->dump();
for (unsigned i = 0; i < user->getNumOperands(); i++) {
Value * op = user->getOperand(i);
if (op == oldu) {
user->setOperand(i, cast);
}
}
//cast->dump();
//user->dump();
//cout <<"replace operand" << endl;
}
} else if (dtype == DTRUE) { //READ after WRITE
error("check mem dep!!");
if (!isa<LoadInst>(v)) {
error("not true dependency");
}
BitCastInst *cast = new BitCastInst(call, v->getType(), call->getNameStr() + "_ptr");
cast->insertBefore(v);
//replace the v with 'cast' in v's thread: (other thread with be dealed using dependence)
for (Instruction::use_iterator ui = v->use_begin(); ui != v->use_end(); ui++) {
Instruction *user = dyn_cast<Instruction>(*ui);
if (user == NULL) {
error("how could it be NULL");
}
// int userthread = this->getNewInstAssigned(user);
if (user->getParent()->getParent() != v->getParent()->getParent()) {
continue;
}
for (unsigned i = 0; i < user->getNumOperands(); i++) {
Value * op = user->getOperand(i);
if (op == v) {
user->setOperand(i, cast);
}
}
}
} else {
}
}