forked from djsutherland/dswp-plus
/
DSWP_5.cpp
334 lines (281 loc) · 8.48 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
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
// 5th step - inserting thread synchronization
#include "DSWP.h"
using namespace llvm;
using namespace std;
void DSWP::insertSynchronization(Loop *L) {
cout << "inserting sychronization" << endl << endl;
int channel = 0;
for (unsigned int i = 0; i < allEdges.size(); i++) {
const Edge &e = allEdges[i];
int utr = getInstAssigned(e.u);
for (int vtr = utr + 1; vtr < MAX_THREAD; vtr++) {
// Legal produce/consume dependences only go *upwards* in thread
// counts. If it goes the other way, fuhgeddaboutit.
Value *vu = instMap[utr][e.u];
Value *vv = instMap[vtr][e.v];
if (vu == NULL || vv == NULL)
continue;
Instruction *nu = dyn_cast<Instruction>(vu);
Instruction *nv = dyn_cast<Instruction>(vv);
if (nu == NULL || nv == NULL)
continue;
cout << "SYN: channel " << channel << ": "
<< dname[e.u]
<< " -> " << dname[e.v]
<< " [" << e.dtype << "]" << endl;
insertProduce(nu, nv, e.dtype, channel, utr, vtr);
insertConsume(nu, nv, e.dtype, channel, utr, vtr);
channel++;
}
}
}
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
// cast the value to something that the communication guy likes
CastInst *cast;
if (u->getType()->isIntegerTy()) {
cast = new ZExtInst(u, eleType, u->getName().str() + "_64");
} else if (u->getType()->isFloatingPointTy()) {
if (u->getType()->isFloatTy()) {
cout << "WARNING: float sucks?";
}
cast = new BitCastInst(u, eleType, u->getName().str() + "_64");
} else if (u->getType()->isPointerTy()){
cast = new PtrToIntInst(u, eleType, u->getName().str() + "_64");
} else {
error("what's the hell type");
}
cast->insertBefore(insPos);
args.push_back(cast);
/* TODO: for true memory dependences, need to send anything or just sync?
} 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->getName().str() + "_ptr");
cast->insertBefore(insPos);
args.push_back(cast);
*/
} else { // others
// just send a dummy value for synchronization
args.push_back(Constant::getNullValue(Type::getInt64Ty(*context)));
}
// channel ID
args.push_back(ConstantInt::get(Type::getInt32Ty(*context), channel));
// make the actual call
CallInst *call = CallInst::Create(fun, args, "", insPos);
}
void DSWP::insertConsume(Instruction *u, Instruction *v, DType dtype,
int channel, int uthread, int vthread) {
Instruction *oldu = dyn_cast<Instruction>(newToOld[u]);
Instruction *insPos = placeEquivalents[vthread][oldu];
if (insPos == NULL) {
insPos = dyn_cast<Instruction>(instMap[vthread][oldu]);
if (insPos == NULL) {
error("can't insert nowhere");
}
}
// call sync_consume(channel)
Function *fun = module->getFunction("sync_consume");
vector<Value *> args;
args.push_back(ConstantInt::get(Type::getInt32Ty(*context), channel));
CallInst *call = CallInst::Create(fun, args, "c" + itoa(channel), insPos);
if (dtype == REG) {
CastInst *cast;
string name = call->getName().str() + "_val";
if (u->getType()->isIntegerTy()) {
cast = new TruncInst(call, u->getType(), name);
}
else if (u->getType()->isFloatingPointTy()) {
if (u->getType()->isFloatTy())
error("cannot deal with double");
cast = new BitCastInst(call, u->getType(), name);
}
else if (u->getType()->isPointerTy()){
cast = new IntToPtrInst(call, u->getType(), name);
} else {
error("what's the hell type");
}
cast->insertBefore(insPos);
// replace the uses
for (Instruction::use_iterator ui = oldu->use_begin(),
ue = oldu->use_end();
ui != ue; ++ui) {
Instruction *user = dyn_cast<Instruction>(*ui);
if (user == NULL) {
error("used by a non-instruction?");
}
// make sure it's in the same function...
if (user->getParent()->getParent() != v->getParent()->getParent()) {
continue;
}
// call replaceUses so that it handles phi nodes
map<Value *, Value *> reps;
reps[oldu] = cast;
replaceUses(user, reps);
}
} /* TODO: need to handle true memory dependences more than just syncing?
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->getName().str() + "_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(), ue = v->use_end();
ui != ue; 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 {
// nothing to do
}
}
// TODO: clean up redundant synchronization
// especially due to control dependences...
void DSWP::cleanup(Loop *L, LPPassManager &LPM) {
// Move some instructions that may not have been inserted in the right
// place, delete the old loop, and clean up our aux data structures for this
// loop.
/*
* move the produce instructions, which have been inserted after the branch,
* in front of it
*/
for (int i = 0; i < MAX_THREAD; i++) {
for (Function::iterator bi = allFunc[i]->begin(),
be = allFunc[i]->end();
bi != be; ++bi) {
BasicBlock *bb = bi;
TerminatorInst *term = NULL;
for (BasicBlock::iterator ii = bb->begin(), ie = bb->end();
ii != ie; ++ii) {
Instruction *inst = ii;
if (isa<TerminatorInst>(inst)) {
term = dyn_cast<TerminatorInst>(inst);
break;
}
}
if (term == NULL) {
error("term cannot be null");
}
while (true) {
Instruction *last = &bb->getInstList().back();
if (isa<TerminatorInst>(last))
break;
last->moveBefore(term);
}
}
}
/*
* move the phi nodes to the top of the block
*/
for (int i = 0; i < MAX_THREAD; i++) {
for (Function::iterator bi = allFunc[i]->begin(),
be = allFunc[i]->end();
bi != be; ++bi) {
BasicBlock *bb = bi;
Instruction *first_nonphi = bb->getFirstNonPHI();
BasicBlock::iterator ii = bb->begin(), ie = bb->end();
// advance the iterator up to one past first_nonphi
while (&(*ii) != first_nonphi) { ++ii; }
++ii;
// move any phi nodes after the first nonphi to before it
for (BasicBlock::iterator i_next; ii != ie; ii = i_next) {
i_next = ii;
++i_next;
Instruction *inst = ii;
if (isa<PHINode>(inst)) {
inst->moveBefore(first_nonphi);
}
}
}
}
cout << "begin to delete loop" << endl;
for (Loop::block_iterator bi = L->block_begin(), be = L->block_end();
bi != be; ++bi) {
BasicBlock *BB = *bi;
for (BasicBlock::iterator ii = BB->begin(), i_next, ie = BB->end();
ii != ie; ii = i_next) {
i_next = ii;
++i_next;
Instruction &inst = *ii;
inst.replaceAllUsesWith(UndefValue::get(inst.getType()));
inst.eraseFromParent();
}
}
// Delete the basic blocks only afterwards
// so that backwards branch instructions don't break
for (Loop::block_iterator bi = L->block_begin(), be = L->block_end();
bi != be; ++bi) {
BasicBlock *BB = *bi;
BB->eraseFromParent();
}
LPM.deleteLoopFromQueue(L);
}
void DSWP::clear() {
module = NULL;
func = NULL;
header = NULL;
predecessor = NULL;
exit = NULL;
context = NULL;
eleType = NULL;
replaceBlock = NULL;
argStructTy = NULL;
sccNum = 0;
pdg.clear();
rev.clear();
scc_dependents.clear();
scc_parents.clear();
dag_added.clear();
allEdges.clear();
InstInSCC.clear();
idom.clear();
postidom.clear();
sccId.clear();
used.clear();
list.clear();
assigned.clear();
for (int i = 0; i < MAX_THREAD; i++) {
part[i].clear();
instMap[i].clear();
placeEquivalents[i].clear();
}
sccLatency.clear();
newToOld.clear();
newInstAssigned.clear();
allFunc.clear();
livein.clear();
defin.clear();
liveout.clear();
dname.clear();
}