-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarrierTailReplication.cpp
More file actions
282 lines (236 loc) · 7.1 KB
/
BarrierTailReplication.cpp
File metadata and controls
282 lines (236 loc) · 7.1 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
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
//#include "config.h"
#include "BarrierTailReplication.h"
#include "barrier_inst.h"
//#include "Workgroup.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/IR/InstrTypes.h"
#include "llvm/IR/Instructions.h"
#include <iostream>
#include <algorithm>
using namespace llvm;
namespace SpmdKernel {
using namespace llvm;
namespace Coarsening {
char BarrierTailReplication::ID = 0;
namespace {
static
RegisterPass<BarrierTailReplication> X("barriertails",
"Barrier tail replication pass");
}
void
BarrierTailReplication::getAnalysisUsage(AnalysisUsage &AU) const
{
AU.addRequired<DominatorTree>();
AU.addPreserved<DominatorTree>();
AU.addRequired<LoopInfo>();
AU.addPreserved<LoopInfo>();
}
bool
BarrierTailReplication::runOnFunction(Function &F)
{
//if (!Workgroup::isKernelToProcess(F))
// return false;
DT = &getAnalysis<DominatorTree>();
LI = &getAnalysis<LoopInfo>();
bool changed = ProcessFunction(F);
DT->verifyAnalysis();
LI->verifyAnalysis();
for (Function::iterator i = F.begin(), e = F.end();
i != e; ++i)
{
llvm::BasicBlock *bb = i;
changed |= CleanupPHIs(bb);
}
return changed;
}
bool
BarrierTailReplication::ProcessFunction(Function &F)
{
BasicBlockSet processed_bbs;
return FindBarriersDFS(&F.getEntryBlock(), processed_bbs);
}
bool
BarrierTailReplication::FindBarriersDFS(BasicBlock *bb,
BasicBlockSet &processed_bbs)
{
bool changed = false;
if (processed_bbs.count(bb) != 0)
return changed;
processed_bbs.insert(bb);
bool HasBarrier = BarrierInst::hasBarrier(bb);
if (HasBarrier) {
BasicBlockSet processed_bbs_rjs;
changed = ReplicateJoinedSubgraphs(bb, bb, processed_bbs_rjs);
}
TerminatorInst *t = bb->getTerminator();
for (unsigned i = 0, e = t->getNumSuccessors(); i != e; ++i)
changed |= FindBarriersDFS(t->getSuccessor(i), processed_bbs);
return changed;
}
bool
BarrierTailReplication::ReplicateJoinedSubgraphs(BasicBlock *dominator,
BasicBlock *subgraph_entry,
BasicBlockSet &processed_bbs)
{
bool changed = false;
assert(DT->dominates(dominator, subgraph_entry));
Function *f = dominator->getParent();
TerminatorInst *t = subgraph_entry->getTerminator();
for (int i = 0, e = t->getNumSuccessors(); i != e; ++i) {
BasicBlock *b = t->getSuccessor(i);
if (processed_bbs.count(b) != 0)
{
continue;
}
const bool isBackedge = DT->dominates(b, subgraph_entry);
if (isBackedge) {
continue;
}
if (DT->dominates(dominator, b))
{
changed |= ReplicateJoinedSubgraphs(dominator, b, processed_bbs);
}
else
{
BasicBlock *replicated_subgraph_entry =
ReplicateSubgraph(b, f);
t->setSuccessor(i, replicated_subgraph_entry);
changed = true;
}
if (changed)
{
DT->runOnFunction(*f);
}
}
processed_bbs.insert(subgraph_entry);
return changed;
}
bool
BarrierTailReplication::CleanupPHIs(llvm::BasicBlock *BB)
{
bool changed = false;
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); BI != BE; )
{
PHINode *PN = dyn_cast<PHINode>(BI);
if (PN == NULL) break;
bool PHIRemoved = false;
for (unsigned i = 0, e = PN->getNumIncomingValues(); i < e; ++i)
{
bool isSuccessor = false;
for (unsigned s = 0,
se = PN->getIncomingBlock(i)->getTerminator()->getNumSuccessors();
s < se; ++s) {
if (PN->getIncomingBlock(i)->getTerminator()->getSuccessor(s) == BB)
{
isSuccessor = true;
break;
}
}
if (!isSuccessor)
{
PN->removeIncomingValue(i, true);
changed = true;
e--;
if (e == 0)
{
PHIRemoved = true;
break;
}
i = 0;
continue;
}
}
if (PHIRemoved)
BI = BB->begin();
else
BI++;
}
return changed;
}
BasicBlock *
BarrierTailReplication::ReplicateSubgraph(BasicBlock *entry,
Function *f)
{
BasicBlockVector subgraph;
FindSubgraph(subgraph, entry);
BasicBlockVector v;
ValueToValueMapTy m;
ReplicateBasicBlocks(v, m, subgraph, f);
UpdateReferences(v, m);
return cast<BasicBlock>(m[entry]);
}
void
BarrierTailReplication::FindSubgraph(BasicBlockVector &subgraph,
BasicBlock *entry)
{
if (std::count(subgraph.begin(), subgraph.end(), entry) > 0)
return;
subgraph.push_back(entry);
const TerminatorInst *t = entry->getTerminator();
for (unsigned i = 0, e = t->getNumSuccessors(); i != e; ++i) {
BasicBlock *successor = t->getSuccessor(i);
const bool isBackedge = DT->dominates(successor, entry);
if (isBackedge) continue;
FindSubgraph(subgraph, successor);
}
}
void
BarrierTailReplication::ReplicateBasicBlocks(BasicBlockVector &new_graph,
ValueToValueMapTy &reference_map,
BasicBlockVector &graph,
Function *f)
{
for (BasicBlockVector::const_iterator i = graph.begin(),
e = graph.end();
i != e; ++i) {
BasicBlock *b = *i;
BasicBlock *new_b = BasicBlock::Create(b->getContext(),
b->getName() + ".btr",
f);
reference_map.insert(std::make_pair(b, new_b));
new_graph.push_back(new_b);
for (BasicBlock::iterator i2 = b->begin(), e2 = b->end();
i2 != e2; ++i2) {
Instruction *i = i2->clone();
reference_map.insert(std::make_pair(i2, i));
new_b->getInstList().push_back(i);
}
TerminatorInst *t = new_b->getTerminator();
for (unsigned i = 0, e = t->getNumSuccessors(); i != e; ++i) {
BasicBlock *successor = t->getSuccessor(i);
if (std::count(graph.begin(), graph.end(), successor) == 0) {
for (BasicBlock::iterator i = successor->begin(), e = successor->end();
i != e; ++i) {
PHINode *phi = dyn_cast<PHINode>(i);
if (phi == NULL)
break; // All PHINodes already checked.
Value *v = phi->getIncomingValueForBlock(b);
Value *new_v = reference_map[v];
if (new_v == NULL) {
new_v = v;
}
phi->addIncoming(new_v, new_b);
}
}
}
}
}
void
BarrierTailReplication::UpdateReferences(const BasicBlockVector &graph,
ValueToValueMapTy &reference_map)
{
for (BasicBlockVector::const_iterator i = graph.begin(),
e = graph.end();
i != e; ++i) {
BasicBlock *b = *i;
for (BasicBlock::iterator i2 = b->begin(), e2 = b->end();
i2 != e2; ++i2) {
Instruction *i = i2;
RemapInstruction(i, reference_map,
RF_IgnoreMissingEntries | RF_NoModuleLevelChanges);
}
}
}
}
}