-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathtest_sanity_llvm_optimization.cpp
87 lines (73 loc) · 2.71 KB
/
test_sanity_llvm_optimization.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
#include "gtest/gtest.h"
#include "pochivm.h"
#include "codegen_context.hpp"
#include "test_util_helper.h"
using namespace PochiVM;
TEST(Sanity, LLVMOptimizationPassEffective)
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
// Sanity check that LLVM function-level and module-level optimization are working.
// Specifically, we want to see expression gets simplified, and function gets inlined
//
using FnPrototype = int(*)(int);
{
auto [fn, a] = NewFunction<FnPrototype>("a_plus_10", "a");
fn.SetBody(
Increment(a),
Increment(a),
Increment(a),
Increment(a),
Increment(a),
Increment(a),
Increment(a),
Increment(a),
Increment(a),
Increment(a),
Return(a)
);
}
{
auto [fn, a] = NewFunction<FnPrototype>("a_plus_50", "a");
fn.SetBody(
Assign(a, Call<FnPrototype>("a_plus_10", a)),
Assign(a, Call<FnPrototype>("a_plus_10", a)),
Assign(a, Call<FnPrototype>("a_plus_10", a)),
Assign(a, Call<FnPrototype>("a_plus_10", a)),
Assign(a, Call<FnPrototype>("a_plus_10", a)),
Return(a)
);
}
ReleaseAssert(thread_pochiVMContext->m_curModule->Validate());
ReleaseAssert(!thread_errorContext->HasError());
thread_pochiVMContext->m_curModule->EmitIR();
thread_pochiVMContext->m_curModule->OptimizeIR(2 /*optLevel*/);
std::string _dst;
llvm::raw_string_ostream rso(_dst /*target*/);
thread_pochiVMContext->m_curModule->GetBuiltLLVMModule()->print(rso, nullptr);
std::string& dump = rso.str();
AssertIsExpectedOutput(dump);
SimpleJIT jit;
jit.SetModule(thread_pochiVMContext->m_curModule);
{
FnPrototype jitFn = jit.GetFunction<FnPrototype>("a_plus_10");
ReleaseAssert(jitFn(233) == 233 + 10);
}
{
FnPrototype jitFn = jit.GetFunction<FnPrototype>("a_plus_50");
ReleaseAssert(jitFn(233) == 233 + 50);
}
thread_pochiVMContext->m_curModule->PrepareForFastInterp();
{
FastInterpFunction<FnPrototype> interpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("a_plus_10");
ReleaseAssert(interpFn(233) == 233 + 10);
}
{
FastInterpFunction<FnPrototype> interpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("a_plus_50");
ReleaseAssert(interpFn(233) == 233 + 50);
}
}