-
Notifications
You must be signed in to change notification settings - Fork 7
/
manager.cpp
294 lines (236 loc) · 8.68 KB
/
manager.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
#include <manager.hpp>
#include <OIPrinter.hpp>
#include <fstream>
#include <vector>
#include "llvm/IR/Verifier.h"
#include "llvm/Support/raw_ostream.h"
#include "timer.hpp"
#include "llvm/Transforms/Utils/Cloning.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IRReader/IRReader.h"
using namespace dbt;
bool hasAddr(const OIInstList& OIRegion, uint32_t Addr) {
for (auto Pair : OIRegion)
if (Pair[0] == Addr) return true;
return false;
}
llvm::Module* Manager::loadRegionFromFile(std::string Path) {
llvm::SMDiagnostic error;
auto M = llvm::parseIRFile(RegionPath+Path, error, TheContext).release();
if (M)
return M;
else
return nullptr;
}
bool compInst(std::array<uint32_t, 2> A, std::array<uint32_t, 2> B) { return (A[0]<B[0]); }
void Manager::loadRegionsFromFiles() {
std::ifstream infile(RegionPath + "regions.order");
std::string line;
std::cout << "Loading Regions from " << RegionPath << "regions.order\n";
while (std::getline(infile, line)) {
uint32_t Entry = std::stoi(line);
if (!IsToLoadBCFormat) {
std::ifstream infile(RegionPath + "r"+std::to_string(Entry)+".oi");
std::string line;
OIInstList Insts;
while (std::getline(infile, line)) {
std::istringstream iss(line);
uint32_t Addrs, Opcode;
if (!(iss >> Addrs >> Opcode)) { break; }
Insts.push_back({Addrs, Opcode});
}
addOIRegion(Entry, Insts);
} else {
addOIRegion(Entry, {{Entry, 0}});
}
}
// If doing whole compilation, merge all regions in one
if (IsToDoWholeCompilation) {
OIInstList OIAll;
std::set<uint32_t> UniqInsts;
for (auto OIR : OIRegions) {
for (auto I : OIR.second) {
if (UniqInsts.count(I[0]) == 0)
OIAll.push_back({I[0], I[1]});
UniqInsts.insert(I[0]);
}
}
OIRegionsMtx.lock();
std::sort(OIAll.begin(), OIAll.end(), compInst);
OIRegions.clear();
OIRegions[0] = OIAll;
OIRegionsKey.insert(OIRegionsKey.begin(), 0);
NumOfOIRegions = 1;
cv.notify_all();
OIRegionsMtx.unlock();
}
}
static unsigned int ModuleId = 0;
void Manager::runPipeline() {
if (!IRE) {
llvm::InitializeNativeTarget();
llvm::InitializeNativeTargetAsmPrinter();
llvm::InitializeNativeTargetAsmParser();
IRJIT = llvm::make_unique<llvm::orc::IRJIT>();
}
PerfMapFile = new std::ofstream("/tmp/perf-"+std::to_string(getpid())+".map");
IRE = llvm::make_unique<IREmitter>();
IRO = llvm::make_unique<IROpt>();
while (isRunning) {
uint32_t EntryAddress;
OIInstList OIRegion;
std::unique_lock<std::mutex> lk(NR);
cv.wait(lk, [&]{ return getNumOfOIRegions() != 0; });
OIRegionsMtx.lock_shared();
EntryAddress = OIRegionsKey.front();
OIRegion = OIRegions[EntryAddress];
OIRegionsMtx.unlock_shared();
llvm::Module* Module = nullptr;
unsigned Size = 1;
unsigned OSize = 1;
if (OIRegion.size() == 0)
continue;
if (IsToLoadRegions && IsToLoadBCFormat)
Module = loadRegionFromFile("r"+std::to_string(EntryAddress)+".bc");
std::vector<uint32_t> EntryAddresses = {EntryAddress};
if (Module == nullptr) {
if (!isRunning) return;
CompiledOIRegionsMtx.lock();
CompiledOIRegions[EntryAddress] = OIRegion;
CompiledOIRegionsMtx.unlock();
if (VerboseOutput)
std::cerr << "Trying to compile: " << std::hex << EntryAddress << "...";
if (VerboseOutput) {
std::cerr << "---------------------- Printing OIRegion (OpenISA instr.) --------------------" << std::endl;
for (auto Pair : OIRegion)
std::cerr << std::hex << Pair[0] << ":\t" << dbt::OIPrinter::getString(OIDecoder::decode(Pair[1])) << "\n";
std::cerr << "\n" << std::endl;
}
OICompiled += OIRegion.size();
Module = new llvm::Module(std::to_string(++ModuleId), TheContext);
if (IsToDoWholeCompilation) {
OIRegionsKey.erase(OIRegionsKey.begin());
EntryAddresses = OIRegionsKey;
}
if (VerboseOutput)
std::cerr << "Generating IR for: " << std::hex << EntryAddress << "...";
IRE->generateRegionIR(EntryAddresses, OIRegion, DataMemOffset, TheMachine, IRJIT->getTargetMachine(),
NativeRegions, Module);
if (VerboseOutput)
std::cerr << "OK" << std::endl;
for (auto& F : *Module)
for (auto& BB : F)
Size += BB.size();
if (!isRunning) return;
if (OptMode != OptPolitic::Custom)
IRO->optimizeIRFunction(Module, IROpt::OptLevel::Basic, EntryAddress, 1, TheMachine.getBinPath());
else if (CustomOpts->count(EntryAddress) != 0)
IRO->customOptimizeIRFunction(Module, (*CustomOpts)[EntryAddress]);
if (VerboseOutput)
Module->print(llvm::errs(), nullptr);
for (auto& F : *Module)
for (auto& BB : F)
OSize += BB.size();
}
// Remove a region if the first instruction is a return <- can cause infinity loops
llvm::Function* LLVMRegion = Module->getFunction("r"+std::to_string(EntryAddress));
if (LLVMRegion == nullptr) {
std::cerr << "Module->getFunction has returned empty!\n";
exit(1);
}
auto Inst = LLVMRegion->getEntryBlock().getFirstNonPHI();
bool IsRet = Inst->getOpcode() == llvm::Instruction::Ret;
bool RetLoop = true;
if (IsRet) {
auto RetInst = llvm::dyn_cast<llvm::ReturnInst>(Inst);
if (llvm::isa<llvm::ConstantInt>(RetInst->getReturnValue()))
if (!llvm::dyn_cast<llvm::ConstantInt>(RetInst->getReturnValue())->equalsInt(EntryAddress))
RetLoop = false;
}
if (!IsRet || !RetLoop) {
if (!isRunning) return;
IRRegions[EntryAddress] = llvm::CloneModule(*Module).release();
IRRegionsKey.push_back(EntryAddress);
NativeRegionsMtx.lock();
IRJIT->addModule(std::unique_ptr<llvm::Module>(Module));
if (VerboseOutput)
llvm::errs() << ".. we've compiled (" << (float) OSize/Size << ")\n";
CompiledRegions += 1;
LLVMCompiled += OSize;
AvgOptCodeSize += (float) OSize/Size;
auto Addr = IRJIT->findSymbol("r"+std::to_string(EntryAddress)).getAddress();
*PerfMapFile << std::hex << "0x" << *Addr << std::dec <<" " << IREmitter::getAssemblySize((const void*) *Addr) << " r" << EntryAddress << ".oi\n";
PerfMapFile->flush();
if (Addr) {
for (auto EA : EntryAddresses)
if (EA < NATIVE_REGION_SIZE) {
NativeRegions[EA] = static_cast<intptr_t>(*Addr);
} else {
std::cerr << EntryAddress << " is out of NativeRegion entries range!\n";
}
} else {
std::cerr << EntryAddress << " was not successfully compiled!\n";
}
NativeRegionsMtx.unlock();
if (VerboseOutput) {
std::cerr << "Disassembly of Region: " << EntryAddress << ":" << std::endl;
std::ostringstream buffer;
size_t t = IREmitter::disassemble((const void*) *Addr, buffer);
std::cerr << buffer.str().c_str() << std::endl;
buffer.clear();
buffer.str("");
std::cerr << "Dumping Region: " << EntryAddress << ":" << std::endl;
IREmitter::regionDump((const void*) *Addr, buffer, t);
std::cerr << buffer.str().c_str() << std::endl;
}
} else if (VerboseOutput) {
std::cerr << "Giving up " << std::hex << EntryAddress << " compilation as it starts with a return!\n";
delete Module;
}
OIRegionsMtx.lock();
OIRegions.erase(EntryAddress);
NumOfOIRegions -= 1;
OIRegionsKey.erase(OIRegionsKey.begin());
OIRegionsMtx.unlock();
if (IsToDoWholeCompilation) {
isFinished = true;
return;
}
}
PerfMapFile->close();
isFinished = true;
}
bool Manager::addOIRegion(uint32_t EntryAddress, OIInstList OIRegion) {
if (!isRegionEntry(EntryAddress) && OIRegions.count(EntryAddress) == 0) {
OIRegionsMtx.lock();
OIRegionsKey.push_back(EntryAddress);
OIRegions[EntryAddress] = OIRegion;
OIRegionsMtx.unlock();
NumOfOIRegions += 1;
cv.notify_all();
return true;
}
return false;
}
int32_t Manager::jumpToRegion(uint32_t EntryAddress) {
uint32_t JumpTo = EntryAddress;
int32_t* RegPtr = TheMachine.getRegisterPtr();
uint32_t* MemPtr = TheMachine.getMemoryPtr();
while (isNativeRegionEntry(JumpTo)) {
JumpTo = ((uint32_t (*)(int32_t*, uint32_t*, uint32_t)) NativeRegions[JumpTo])(RegPtr, MemPtr, EntryAddress);
}
return JumpTo;
}
void Manager::reset() {
while (NumOfOIRegions != 0);
OIRegionsKey.clear();
OIRegions.clear();
CompiledOIRegions.clear();
TouchedEntries.clear();
NumOfOIRegions = 0;
for (auto P : IRRegions)
delete P.second;
IRRegions.clear();
for (unsigned I = 0; I < NATIVE_REGION_SIZE; I++)
NativeRegions[I] = 0;
}