-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_util_helper.h
335 lines (300 loc) · 11.3 KB
/
test_util_helper.h
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
335
#pragma once
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/IR/Module.h"
#include "llvm/IRReader/IRReader.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/Operator.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ExecutionEngine/Orc/LLJIT.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/IR/Mangler.h"
#include "llvm/Support/DynamicLibrary.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/Target/TargetMachine.h"
#include "llvm/ExecutionEngine/ExecutionEngine.h"
#include "llvm/ExecutionEngine/Orc/ExecutionUtils.h"
#include "llvm/Bitcode/BitcodeWriter.h"
#include "llvm/IR/DebugInfo.h"
#include "pochivm/common.h"
#include "gtest/gtest.h"
#include "pochivm/codegen_context.hpp"
#include "pochivm/pochivm.h"
// Whether we are in update-expected-output mode
//
extern bool g_is_update_expected_mode;
// a bunch of utility helpers for tests
// return if string 'a' starts with 'b'
//
inline bool WARN_UNUSED StartsWith(const char* a, const char* b)
{
size_t la = strlen(a);
size_t lb = strlen(b);
CHECK(la >= lb);
return strncmp(a, b, lb) == 0;
}
namespace {
const char* const x_expected_output_dir = "test_expected_output";
inline std::string GetExpectedOutputFileName(const std::string& suffix)
{
const char* namePart1 = ::testing::UnitTest::GetInstance()->current_test_info()->test_case_name();
const char* namePart2 = ::testing::UnitTest::GetInstance()->current_test_info()->name();
return std::string(x_expected_output_dir) + std::string("/") +
std::string(namePart1) + std::string(".") + std::string(namePart2) +
(suffix == "" ? "" : std::string(".") + suffix) + ".expected";
}
inline void CreateExpectedOutputFolderIfNeeded()
{
struct stat st;
if (stat(x_expected_output_dir, &st) == -1)
{
int ret = mkdir(x_expected_output_dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
if (ret != 0)
{
printf("Creating expected output file directory failed with errno=%d (%s)\n", errno, strerror(errno));
ReleaseAssert(false);
}
}
ReleaseAssert(stat(x_expected_output_dir, &st) == 0);
}
inline size_t GetFileSize(const char *filename) {
struct stat st;
if (stat(filename, &st) == 0)
{
return static_cast<size_t>(st.st_size);
}
else
{
printf("Failed to access expected output file %s, errno=%d (%s)\n", filename, errno, strerror(errno));
ReleaseAssert(false);
}
}
inline void TrimTrailingWhitespaces(std::string& s, const char* t = " \t\n\r\f\v")
{
size_t pos = s.find_last_not_of(t);
if (pos == std::string::npos)
{
s = "";
}
else
{
s.erase(pos + 1);
}
}
inline void NO_RETURN DumpExpectedAndActualAndFail(const std::string& actual, const std::string& expected)
{
printf("!!! TEST FAILED !!!\n");
printf("=============== Expected Output ===============\n");
for (size_t i = 0; i < expected.length(); i++)
{
printf("%c", expected[i]);
}
printf("\n");
printf("================ Actual Output ================\n");
for (size_t i = 0; i < actual.length(); i++)
{
printf("%c", actual[i]);
}
printf("\n");
printf("===============================================\n");
ReleaseAssert(false);
}
} // anonymous namespace
// Check the output against the expected output file of this test
//
inline void AssertIsExpectedOutput(std::string out, std::string suffix = "")
{
if (g_is_update_expected_mode)
{
CreateExpectedOutputFolderIfNeeded();
std::string filename = GetExpectedOutputFileName(suffix);
FILE* pFile = fopen(filename.c_str(), "w");
if (pFile == nullptr)
{
printf("Failed to open file %s for write, errno=%d (%s)\n", filename.c_str(), errno, strerror(errno));
ReleaseAssert(false);
}
ReleaseAssert(fwrite(out.data(), sizeof(char), out.length(), pFile) == out.length());
fclose(pFile);
printf("[UPDATE_EXPECTED_OUTPUT] Succesfully updated expected output file.\n");
}
else
{
std::string filename = GetExpectedOutputFileName(suffix);
FILE* pFile = fopen(filename.c_str(), "r");
if (pFile == nullptr)
{
printf("!!! TEST CONFIGURATION ISSUE !!!\n");
printf("=============== Expected Output ===============\n");
printf("[ERROR] Failed to open expected output file %s, errno=%d (%s)\n", filename.c_str(), errno, strerror(errno));
printf("================ Actual Output ================\n");
for (size_t i = 0; i < out.length(); i++)
{
printf("%c", out[i]);
}
printf("\n");
printf("===============================================\n");
ReleaseAssert(false);
}
Auto(fclose(pFile));
size_t fileSize = GetFileSize(filename.c_str());
std::string expected(fileSize, ' ');
ReleaseAssert(fread(expected.data(), sizeof(char), fileSize, pFile) == fileSize);
TrimTrailingWhitespaces(expected);
TrimTrailingWhitespaces(out);
if (expected != out)
{
DumpExpectedAndActualAndFail(out, expected);
}
}
}
// This class has been so rotten.. Don't use it for anything but test purpose
// WARNING: especially do not use this class for perf benchmark.
// It does not set llvm::CodeGenOpt::Level correctly, so the assembly instructions emitted by LLVM is very poor quality.
//
class SimpleJIT
{
public:
SimpleJIT()
: m_jit(nullptr)
, m_astModule(nullptr)
, m_allowResolveSymbolInHostProcess(false)
{ }
// JIT the given module. Transfers ownership of the llvm module.
// The previous JIT'd module, if exists, is thrown away
//
void SetModule(PochiVM::AstModule* module)
{
llvm::ExitOnError exitOnErr;
std::unique_ptr<llvm::orc::LLJIT>&& jit = GetJIT();
llvm::orc::ThreadSafeModule M = module->GetThreadSafeModule();
exitOnErr(jit->addIRModule(std::move(M)));
m_jit.reset(jit.release());
m_astModule = module;
}
void SetNonAstModule(std::unique_ptr<llvm::orc::ThreadSafeModule> module)
{
llvm::ExitOnError exitOnErr;
std::unique_ptr<llvm::orc::LLJIT> jit = GetJIT();
exitOnErr(jit->addIRModule(std::move(*module.release())));
m_jit.reset(jit.release());
m_astModule = nullptr;
}
std::unique_ptr<llvm::orc::LLJIT> GetJIT()
{
llvm::ExitOnError exitOnErr;
std::unique_ptr<llvm::orc::LLJIT> jit = exitOnErr(llvm::orc::LLJITBuilder().create());
if (m_allowResolveSymbolInHostProcess)
{
char Prefix = llvm::EngineBuilder().selectTarget()->createDataLayout().getGlobalPrefix();
std::unique_ptr<llvm::orc::DynamicLibrarySearchGenerator> R =
exitOnErr(llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(Prefix));
ReleaseAssert(R != nullptr);
jit->getMainJITDylib().addGenerator(std::move(R));
}
return jit;
}
void SetAllowResolveSymbolInHostProcess(bool value)
{
m_allowResolveSymbolInHostProcess = value;
}
// Get a callable to the function name
// FnPrototype may be a C-style pointer or std::function object.
// Fires an assert if the function prototype does not match the actual type of the function
//
template<typename FnPrototype>
FnPrototype GetFunction(const std::string& fnName)
{
ReleaseAssert(m_jit != nullptr && m_astModule != nullptr);
ReleaseAssert(m_astModule->CheckFunctionExistsAndPrototypeMatches<FnPrototype>(fnName));
llvm::ExitOnError exitOnErr;
auto sym = exitOnErr(m_jit->lookup(fnName));
return PochiVM::AstTypeHelper::function_addr_to_callable<FnPrototype>::get(
reinterpret_cast<void*>(sym.getAddress()));
}
template<typename FnPrototype>
FnPrototype GetFunctionNonAst(const std::string& fnName)
{
ReleaseAssert(m_jit != nullptr);
llvm::ExitOnError exitOnErr;
auto sym = exitOnErr(m_jit->lookup(fnName));
return PochiVM::AstTypeHelper::function_addr_to_callable<FnPrototype>::get(
reinterpret_cast<void*>(sym.getAddress()));
}
std::unique_ptr<llvm::orc::LLJIT> m_jit;
PochiVM::AstModule* m_astModule;
bool m_allowResolveSymbolInHostProcess;
};
class TestJitHelper
{
public:
TestJitHelper() {}
void Init(int optLevel)
{
using namespace PochiVM;
TestAssert(0 <= optLevel && optLevel <= 3);
if (optLevel > 0)
{
thread_pochiVMContext->m_curModule->OptimizeIR(optLevel);
}
llvm::ExitOnError exitOnErr;
llvm::orc::JITTargetMachineBuilder jtmb((llvm::Triple(llvm::sys::getProcessTriple())));
// The other part of this project are compiled using default options
// "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87"
// For fair comparison, only enable the same set of CPU features used
// TODO: consider enable avx2
//
jtmb.getFeatures().AddFeature("cx8");
jtmb.getFeatures().AddFeature("fxsr");
jtmb.getFeatures().AddFeature("mmx");
jtmb.getFeatures().AddFeature("sse");
jtmb.getFeatures().AddFeature("sse2");
jtmb.getFeatures().AddFeature("x87");
jtmb.setCPU("x86-64");
if (optLevel == 0)
{
jtmb.setCodeGenOptLevel(llvm::CodeGenOpt::None);
}
else if (optLevel == 1)
{
jtmb.setCodeGenOptLevel(llvm::CodeGenOpt::Less);
}
else if (optLevel == 2)
{
jtmb.setCodeGenOptLevel(llvm::CodeGenOpt::Default);
}
else if (optLevel == 3)
{
jtmb.setCodeGenOptLevel(llvm::CodeGenOpt::Aggressive);
}
// We must use CodeModel::Medium, otherwise address of functions/data symbols would break down
//
jtmb.setCodeModel(llvm::CodeModel::Medium);
m_jit = exitOnErr(llvm::orc::LLJITBuilder().setJITTargetMachineBuilder(jtmb).create());
{
char Prefix = llvm::EngineBuilder().selectTarget()->createDataLayout().getGlobalPrefix();
std::unique_ptr<llvm::orc::DynamicLibrarySearchGenerator> R =
exitOnErr(llvm::orc::DynamicLibrarySearchGenerator::GetForCurrentProcess(Prefix));
ReleaseAssert(R != nullptr);
m_jit->getMainJITDylib().addGenerator(std::move(R));
}
llvm::orc::ThreadSafeModule M = thread_pochiVMContext->m_curModule->GetThreadSafeModule();
exitOnErr(m_jit->addIRModule(std::move(M)));
m_astModule = thread_pochiVMContext->m_curModule;
}
template<typename FnPrototype>
FnPrototype GetFunction(const std::string& fnName)
{
ReleaseAssert(m_jit != nullptr && m_astModule != nullptr);
ReleaseAssert(m_astModule->CheckFunctionExistsAndPrototypeMatches<FnPrototype>(fnName));
llvm::ExitOnError exitOnErr;
auto sym = exitOnErr(m_jit->lookup(fnName));
return PochiVM::AstTypeHelper::function_addr_to_callable<FnPrototype>::get(
reinterpret_cast<void*>(sym.getAddress()));
}
std::unique_ptr<llvm::orc::LLJIT> m_jit;
PochiVM::AstModule* m_astModule;
};