This repository was archived by the owner on Dec 16, 2022. It is now read-only.
forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathllvm-simdloop.cpp
174 lines (153 loc) · 6.45 KB
/
llvm-simdloop.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
#define DEBUG_TYPE "lower_simd_loop"
#undef DEBUG
// This file defines two entry points:
// global function annotateSimdLoop: mark a loop as a SIMD loop.
// createLowerSimdLoopPass: construct LLVM for lowering a marked loop later.
#include "llvm/Analysis/LoopPass.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Metadata.h"
#include "llvm/Support/Debug.h"
#include <cstdio>
namespace llvm {
// simd loop
static unsigned simd_loop_mdkind = 0;
static MDNode* simd_loop_md = NULL;
/// Mark loop as a SIMD loop. Return false if loop cannot be marked.
/// incr should be the basic block that increments the loop counter.
bool annotateSimdLoop(BasicBlock* incr) {
DEBUG(dbgs() << "LSL: annotating simd_loop\n");
// Lazy initialization
if (!simd_loop_mdkind) {
simd_loop_mdkind = getGlobalContext().getMDKindID("simd_loop");
simd_loop_md = MDNode::get(getGlobalContext(), ArrayRef<Value*>());
}
// Ideally, the decoration would go on the block itself, but LLVM 3.3 does not
// support putting metadata on blocks. So instead, put the decoration on the last
// Add instruction, which (somewhat riskily) is assumed to be the loop increment.
for (BasicBlock::reverse_iterator ri = incr->rbegin(); ri!=incr->rend(); ++ri) {
Instruction& i = *ri;
unsigned op = i.getOpcode();
if (op==Instruction::Add) {
if (i.getType()->isIntegerTy()) {
DEBUG(dbgs() << "LSL: setting simd_loop metadata\n");
i.setMetadata(simd_loop_mdkind, simd_loop_md);
return true;
} else {
return false;
}
}
}
return false;
}
/// Pass that lowers a loop marked by annotateSimdLoop.
/// This pass should run after reduction variables have been converted to phi nodes,
/// otherwise floating-point reductions might not be recognized as such and
/// prevent SIMDization.
struct LowerSIMDLoop: public LoopPass {
static char ID;
LowerSIMDLoop() : LoopPass(ID) {}
private:
/*override*/ bool runOnLoop(Loop *, LPPassManager &LPM);
/// Check if loop has "simd_loop" annotation.
/// If present, the annotation is an MDNode attached to an instruction in the loop's latch.
bool hasSIMDLoopMetadata( Loop *L) const;
/// If Phi is part of a reduction cycle of FAdd or FMul, mark the ops as permitting reassociation/commuting.
void enableUnsafeAlgebraIfReduction(PHINode* Phi, Loop* L) const;
};
bool LowerSIMDLoop::hasSIMDLoopMetadata(Loop *L) const {
// Note: If a loop has 0 or multiple latch blocks, it's probably not a simd_loop anyway.
if (BasicBlock* latch = L->getLoopLatch())
for (BasicBlock::iterator II = latch->begin(), EE = latch->end(); II!=EE; ++II)
if (II->getMetadata(simd_loop_mdkind))
return true;
return false;
}
void LowerSIMDLoop::enableUnsafeAlgebraIfReduction(PHINode* Phi, Loop* L) const {
typedef SmallVector<Instruction*, 8> chainVector;
chainVector chain;
Instruction *J;
unsigned opcode = 0;
for (Instruction *I = Phi; ; I=J) {
J = NULL;
// Find the user of instruction I that is within loop L.
for (Value::use_iterator UI = I->use_begin(), UE = I->use_end(); UI != UE; ++UI) {
Instruction *U = cast<Instruction>(*UI);
if (L->contains(U)) {
if (J) {
DEBUG(dbgs() << "LSL: not a reduction var because op has two internal uses: " << *I << "\n");
return;
}
J = U;
}
}
if (!J) {
DEBUG(dbgs() << "LSL: chain prematurely terminated at " << *I << "\n");
return;
}
if (J==Phi) {
// Found the entire chain.
break;
}
if (opcode) {
// Check that arithmetic op matches prior arithmetic ops in the chain.
if (J->getOpcode()!=opcode) {
DEBUG(dbgs() << "LSL: chain broke at " << *J << " because of wrong opcode\n");
return;
}
} else {
// First arithmetic op in the chain.
opcode = J->getOpcode();
if (opcode!=Instruction::FAdd && opcode!=Instruction::FMul) {
DEBUG(dbgs() << "LSL: first arithmetic op in chain is uninteresting" << *J << "\n");
return;
}
}
chain.push_back(J);
}
for (chainVector::const_iterator K=chain.begin(); K!=chain.end(); ++K) {
DEBUG(dbgs() << "LSL: marking " << **K << "\n");
(*K)->setHasUnsafeAlgebra(true);
}
}
bool LowerSIMDLoop::runOnLoop(Loop *L, LPPassManager &LPM) {
if (!simd_loop_mdkind)
return false; // Fast rejection test.
if (!hasSIMDLoopMetadata(L))
return false;
DEBUG(dbgs() << "LSL: simd_loop found\n");
#if defined(LLVM_VERSION_MAJOR) && LLVM_VERSION_MAJOR == 3 && LLVM_VERSION_MINOR >= 4
MDNode* n = L->getLoopID();
if( !n ) {
// Loop does not have a LoopID yet, so give it one.
n = MDNode::get(getGlobalContext(), ArrayRef<Value*>(NULL));
n->replaceOperandWith(0,n);
L->setLoopID(n);
}
#else
MDNode* n = MDNode::get(getGlobalContext(), ArrayRef<Value*>());
L->getLoopLatch()->getTerminator()->setMetadata("llvm.loop.parallel", n);
#endif
MDNode* m = MDNode::get(getGlobalContext(), ArrayRef<Value*>(n));
// Mark memory references so that Loop::isAnnotatedParallel will return true for this loop.
for(Loop::block_iterator BBI = L->block_begin(), E=L->block_end(); BBI!=E; ++BBI)
for (BasicBlock::iterator I = (*BBI)->begin(), EE = (*BBI)->end(); I!=EE; ++I)
if (I->mayReadOrWriteMemory())
I->setMetadata("llvm.mem.parallel_loop_access", m);
assert(L->isAnnotatedParallel());
// Mark floating-point reductions as okay to reassociate/commute.
BasicBlock* Lh = L->getHeader();
DEBUG(dbgs() << "LSL: loop header: " << *Lh << "\n");
for (BasicBlock::iterator I = Lh->begin(), E = Lh->end(); I!=E; ++I)
if (PHINode *Phi = dyn_cast<PHINode>(I))
enableUnsafeAlgebraIfReduction(Phi,L);
return true;
}
char LowerSIMDLoop::ID = 0;
static RegisterPass<LowerSIMDLoop> X("LowerSIMDLoop", "LowerSIMDLoop Pass",
false /* Only looks at CFG */,
false /* Analysis Pass */);
Pass* createLowerSimdLoopPass() {
return new LowerSIMDLoop();
}
} // namespace llvm