-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoopBarriers.cpp
160 lines (137 loc) · 5.31 KB
/
LoopBarriers.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
//#include "config.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/Transforms/Utils/BasicBlockUtils.h"
#include <iostream>
#include "LoopBarriers.h"
#include "barrier_inst.h"
//#include "Workgroup.h"
namespace SpmdKernel {
using namespace llvm;
namespace Coarsening {
char LoopBarriers::ID = 0;
namespace {
static
RegisterPass<LoopBarriers> X("loop-barriers",
"Add needed barriers to loops");
}
void
LoopBarriers::getAnalysisUsage(AnalysisUsage &AU) const
{
AU.addRequired<DominatorTree>();
AU.addPreserved<DominatorTree>();
}
bool
LoopBarriers::runOnLoop(Loop *L, LPPassManager &LPM)
{
//if (!Workgroup::isKernelToProcess(*L->getHeader()->getParent()))
// return false;
DT = &getAnalysis<DominatorTree>();
bool changed = ProcessLoop(L, LPM);
DT->verifyAnalysis();
return changed;
}
bool
LoopBarriers::ProcessLoop(Loop *L, LPPassManager &LPM)
{
bool isBLoop = false;
bool changed = false;
for (Loop::block_iterator i = L->block_begin(), e = L->block_end();
i != e && !isBLoop; ++i) {
for (BasicBlock::iterator j = (*i)->begin(), e = (*i)->end();
j != e; ++j) {
if (isa<BarrierInst>(j)) {
isBLoop = true;
break;
}
}
}
LLVMContext &LC = getGlobalContext();
IntegerType * IntTy = IntegerType::get(LC, 32);
Value *Args = ConstantInt::get(IntTy, 0);
for (Loop::block_iterator i = L->block_begin(), e = L->block_end();
i != e && isBLoop; ++i) {
for (BasicBlock::iterator j = (*i)->begin(), e = (*i)->end();
j != e; ++j) {
if (isa<BarrierInst>(j)) {
BasicBlock *preheader = L->getLoopPreheader();
assert((preheader != NULL) && "Non-canonicalized loop found!\n");
Instruction *PhdrBarrierInst =
BarrierInst::createBarrier(Args, preheader->getTerminator());
MDNode* PhdrAuxBarrierInfo =
MDNode::get(LC, MDString::get(LC, "auxiliary phdr barrier"));
PhdrBarrierInst->setMetadata("aux.phdr.barrier", PhdrAuxBarrierInfo);
preheader->setName(preheader->getName() + ".loopbarrier");
BasicBlock *header = L->getHeader();
if (header->getFirstNonPHI() != &header->front()) {
Instruction *HdrBarrierInst =
BarrierInst::createBarrier(Args, header->getFirstNonPHI());
MDNode* HdrAuxBarrierInfo =
MDNode::get(LC, MDString::get(LC, "auxiliary phihdr barrier"));
HdrBarrierInst->setMetadata("aux.phihdr.barrier", HdrAuxBarrierInfo);
header->setName(header->getName() + ".phibarrier");
}
/*
SmallVector<BasicBlock*, 8> ExitingBlocks;
L->getExitingBlocks(ExitingBlocks);
*/
BasicBlock *brexit = L->getExitingBlock();
if (brexit != NULL) {
Instruction *ExitingBarrierInst =
BarrierInst::createBarrier(Args, brexit->getTerminator());
MDNode* ExitingAuxBarrierInfo =
MDNode::get(LC, MDString::get(LC, "auxiliary exiting barrier"));
ExitingBarrierInst->setMetadata("aux.exiting.barrier",
ExitingAuxBarrierInfo);
brexit->setName(brexit->getName() + ".brexitbarrier");
}
BasicBlock *latch = L->getLoopLatch();
if (latch != NULL && brexit != latch) {
Instruction *LatchBarrierInst =
BarrierInst::createBarrier(Args, latch->getTerminator());
MDNode* LatchAuxBarrierInfo =
MDNode::get(LC, MDString::get(LC, "auxiliary latch barrier"));
LatchBarrierInst->setMetadata("aux.latch.barrier",
LatchAuxBarrierInfo);
latch->setName(latch->getName() + ".latchbarrier");
return changed;
}
BasicBlock *Header = L->getHeader();
typedef GraphTraits<Inverse<BasicBlock *> > InvBlockTraits;
InvBlockTraits::ChildIteratorType PI = InvBlockTraits::child_begin(Header);
InvBlockTraits::ChildIteratorType PE = InvBlockTraits::child_end(Header);
BasicBlock *Latch = NULL;
for (; PI != PE; ++PI) {
InvBlockTraits::NodeType *N = *PI;
if (L->contains(N)) {
Latch = N;
if (DT->dominates(j->getParent(), Latch)) {
BarrierInst::createBarrier(Args, Latch->getTerminator());
Latch->setName(Latch->getName() + ".latchbarrier");
}
}
}
return true;
}
}
}
BasicBlock *preheader = L->getLoopPreheader();
assert((preheader != NULL) && "Non-canonicalized loop found!\n");
TerminatorInst *t = preheader->getTerminator();
Instruction *prev = NULL;
if (&preheader->front() != t) {
// If t is not the first/only instruction in the block, get the previous
// instruction.
prev = t->getPrevNode();
}
if (prev && isa<BarrierInst>(prev)) {
BasicBlock *new_b = SplitBlock(preheader, t, this);
new_b->setName(preheader->getName() + ".postbarrier_dummy");
return true;
}
return changed;
}
}
}