-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathtest_sanity_logical_op.cpp
More file actions
337 lines (292 loc) · 12 KB
/
test_sanity_logical_op.cpp
File metadata and controls
337 lines (292 loc) · 12 KB
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
336
337
#include "gtest/gtest.h"
#include "pochivm.h"
#include "test_util_helper.h"
using namespace PochiVM;
namespace {
bool SideEffect(bool r, int32_t* dst, uint64_t offset)
{
*dst += 1;
dst[offset] = *dst;
return r;
}
using SimulationFn = std::function<bool(int32_t*, bool*)>;
std::tuple<Value<bool>, SimulationFn>
Build(Value<int32_t*> dst, Value<bool>* vars, int numVars, uint64_t& cnt)
{
using SideEffectFn = bool(*)(bool, int32_t*, uint64_t);
cnt++;
uint64_t ord = cnt;
int chance = 49; // on average 100 clauses
if (ord != 1 && (rand() % 100 >= chance || ord > 500))
{
int dice = rand() % numVars;
Value<bool> ret = Call<SideEffectFn>("f", vars[dice], dst, Literal<uint64_t>(ord));
SimulationFn fn = [dice, ord](int32_t* _dst, bool* _vars) -> bool {
return SideEffect(_vars[dice], _dst, ord);
};
return std::make_tuple(ret, fn);
}
else
{
auto [lhs, _fnLhs] = Build(dst, vars, numVars, cnt);
auto [rhs, _fnRhs] = Build(dst, vars, numVars, cnt);
bool dice1 = (rand() % 1000 < 500);
bool dice2 = (rand() % 1000 < 500);
bool dice3 = (rand() % 1000 < 500);
Value<bool> lhs2 = dice1 ? (lhs) : (!lhs);
Value<bool> rhs2 = dice2 ? (rhs) : (!rhs);
Value<bool> param = dice3 ? (lhs2 && rhs2) : (lhs2 || rhs2);
SimulationFn fnLhs = _fnLhs;
SimulationFn fnRhs = _fnRhs;
SimulationFn fn = [dice1, dice2, dice3, fnLhs, fnRhs, ord](int32_t* _dst, bool* _vars) -> bool {
bool l = fnLhs(_dst, _vars);
if (!dice1) { l = !l; }
if ((dice3 && l) || (!dice3 && !l))
{
bool r = fnRhs(_dst, _vars);
if (!dice2) { r = !r; }
l = r;
}
return SideEffect(l, _dst, ord);
};
Value<bool> ret = Call<SideEffectFn>("f", param, dst, Literal<uint64_t>(ord));
return std::make_tuple(ret, fn);
}
}
const static int x_numVars = 10;
using GeneratedFn = bool(*)(int32_t*, bool, bool, bool, bool, bool, bool, bool, bool, bool, bool) noexcept;
void CheckOnce()
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
using SideEffectFn = bool(*)(bool, int32_t*, uint64_t) noexcept;
{
auto [fn, r, dst, offset] = NewFunction<SideEffectFn>("f", "r", "dst", "offset");
fn.SetBody(
Assign(*dst, *dst + Literal<int32_t>(1)),
Assign(*ReinterpretCast<int32_t*>(ReinterpretCast<uint64_t>(dst) + offset * Literal<uint64_t>(4)), *dst),
Return(r)
);
}
SimulationFn simFn;
uint64_t cnt = 0;
{
auto [fn, dst, a0, a1, a2, a3, a4, a5, a6, a7, a8, a9] = NewFunction<GeneratedFn>("testFn");
Value<bool> arr[x_numVars] = {a0, a1, a2, a3, a4, a5, a6, a7, a8, a9};
auto [r, _simFn] = Build(dst, arr, x_numVars, cnt);
fn.SetBody(Return(r));
simFn = _simFn;
}
ReleaseAssert(thread_pochiVMContext->m_curModule->Validate());
thread_pochiVMContext->m_curModule->PrepareForDebugInterp();
thread_pochiVMContext->m_curModule->PrepareForFastInterp();
thread_pochiVMContext->m_curModule->EmitIR();
thread_pochiVMContext->m_curModule->OptimizeIRIfNotDebugMode(2 /*optLevel*/);
auto interpFn = thread_pochiVMContext->m_curModule->
GetDebugInterpGeneratedFunction<GeneratedFn>("testFn");
FastInterpFunction<GeneratedFn> fastinterpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<GeneratedFn>("testFn");
SimpleJIT jit;
jit.SetModule(thread_pochiVMContext->m_curModule);
GeneratedFn jitFn = jit.GetFunction<GeneratedFn>("testFn");
int32_t* d1 = new int32_t[cnt + 1];
Auto(delete [] d1);
int32_t* d2 = new int32_t[cnt + 1];
Auto(delete [] d2);
int32_t* d3 = new int32_t[cnt + 1];
Auto(delete [] d3);
int32_t* d4 = new int32_t[cnt + 1];
Auto(delete [] d4);
auto CompareResults = [&simFn, &interpFn, &jitFn, &fastinterpFn, d1, d2, d3, d4, cnt](bool* input)
{
memset(d1, 0, sizeof(int32_t) * (cnt + 1));
memset(d2, 0, sizeof(int32_t) * (cnt + 1));
memset(d3, 0, sizeof(int32_t) * (cnt + 1));
memset(d4, 0, sizeof(int32_t) * (cnt + 1));
bool r1 = simFn(d1, input);
bool r2 = interpFn(d2, input[0], input[1], input[2], input[3], input[4], input[5], input[6], input[7], input[8], input[9]);
bool r3 = jitFn(d3, input[0], input[1], input[2], input[3], input[4], input[5], input[6], input[7], input[8], input[9]);
bool r4 = fastinterpFn(d4, input[0], input[1], input[2], input[3], input[4], input[5], input[6], input[7], input[8], input[9]);
ReleaseAssert(r1 == r2);
ReleaseAssert(r1 == r3);
ReleaseAssert(r1 == r4);
for (uint64_t i = 0; i <= cnt; i++)
{
ReleaseAssert(d1[i] == d2[i]);
ReleaseAssert(d1[i] == d3[i]);
ReleaseAssert(d1[i] == d4[i]);
}
};
for (uint64_t bitMask = 0; bitMask < (1ULL << x_numVars); bitMask++)
{
bool input[x_numVars];
uint64_t x = bitMask;
for (int i = 0; i < x_numVars; i++)
{
input[i] = (x % 2 == 1);
x /= 2;
}
CompareResults(input);
}
}
} // anonymous namespace
TEST(SanityIrCodeDump, LogicalOp)
{
AutoThreadPochiVMContext apv;
AutoThreadErrorContext arc;
AutoThreadLLVMCodegenContext alc;
thread_pochiVMContext->m_curModule = new AstModule("test");
using FnPrototype = bool(*)(int32_t*, bool, bool);
using SideEffectFn = bool(*)(bool, int32_t*, uint64_t);
{
auto [fn, r, dst, offset] = NewFunction<SideEffectFn>("f", "r", "dst", "offset");
fn.SetBody(
Assign(*dst, *dst + Literal<int32_t>(1)),
Assign(*ReinterpretCast<int32_t*>(ReinterpretCast<uint64_t>(dst) + offset * Literal<uint64_t>(4)), *dst),
Return(r)
);
}
{
auto [fn, out, a, b] = NewFunction<FnPrototype>("logical_and", "out", "a", "b");
fn.SetBody(
Return(Call<SideEffectFn>("f", a, out, Literal<uint64_t>(1)) &&
Call<SideEffectFn>("f", b, out, Literal<uint64_t>(2)))
);
}
{
auto [fn, out, a, b] = NewFunction<FnPrototype>("logical_or", "out", "a", "b");
fn.SetBody(
Return(Call<SideEffectFn>("f", a, out, Literal<uint64_t>(1)) ||
Call<SideEffectFn>("f", b, out, Literal<uint64_t>(2)))
);
}
{
auto [fn, out, a, b] = NewFunction<FnPrototype>("logical_fn_3", "out", "a", "b");
fn.SetBody(
Return(!Call<SideEffectFn>("f", a, out, Literal<uint64_t>(1)) ||
Call<SideEffectFn>("f", b, out, Literal<uint64_t>(2)))
);
}
{
auto [fn, out, a, b] = NewFunction<FnPrototype>("logical_fn_4", "out", "a", "b");
fn.SetBody(
Return((!Call<SideEffectFn>("f", a, out, Literal<uint64_t>(1)) &&
Call<SideEffectFn>("f", b, out, Literal<uint64_t>(2))) ||
Call<SideEffectFn>("f", a || !b, out, Literal<uint64_t>(3)))
);
}
{
auto [fn, out, a, b] = NewFunction<FnPrototype>("logical_fn_5", "out", "a", "b");
fn.SetBody(
Return(Call<SideEffectFn>("f",
Call<SideEffectFn>("f", a, out, Literal<uint64_t>(1)) ||
Call<SideEffectFn>("f", b, out, Literal<uint64_t>(2)),
out, Literal<uint64_t>(3)) &&
!Call<SideEffectFn>("f",
Call<SideEffectFn>("f", a, out, Literal<uint64_t>(4)) &&
Call<SideEffectFn>("f", b, out, Literal<uint64_t>(5)),
out, Literal<uint64_t>(6))
)
);
}
ReleaseAssert(thread_pochiVMContext->m_curModule->Validate());
ReleaseAssert(!thread_errorContext->HasError());
thread_pochiVMContext->m_curModule->EmitIR();
{
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);
}
thread_pochiVMContext->m_curModule->OptimizeIRIfNotDebugMode(2 /*optLevel*/);
SideEffectFn f = [](bool r, int32_t* dst, uint64_t offset) -> bool {
*dst += 1;
dst[offset] = *dst;
return r;
};
auto checkOne = [](const std::function<std::remove_pointer_t<FnPrototype>>& fn,
const std::function<std::remove_pointer_t<FnPrototype>>& gold,
bool a, bool b)
{
const int sz = 7;
int32_t d1[sz]; memset(d1, 0, sizeof(int32_t) * sz);
int32_t d2[sz]; memset(d2, 0, sizeof(int32_t) * sz);
bool r1 = fn(d1, a, b);
bool r2 = gold(d2, a, b);
ReleaseAssert(r1 == r2);
for (int i = 0; i < sz; i++)
{
ReleaseAssert(d1[i] == d2[i]);
}
};
auto check = [checkOne](const std::function<std::remove_pointer_t<FnPrototype>>& fn,
const std::function<std::remove_pointer_t<FnPrototype>>& gold) {
checkOne(fn, gold, false, false);
checkOne(fn, gold, false, true);
checkOne(fn, gold, true, false);
checkOne(fn, gold, true, true);
};
SimpleJIT jit;
jit.SetModule(thread_pochiVMContext->m_curModule);
thread_pochiVMContext->m_curModule->PrepareForFastInterp();
{
FnPrototype jitFn = jit.GetFunction<FnPrototype>("logical_and");
FastInterpFunction<FnPrototype> interpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("logical_and");
auto gold = [f](int32_t* dst, bool a, bool b) ->bool {
return f(a, dst, 1) && f(b, dst, 2);
};
check(jitFn, gold);
check(interpFn, gold);
}
{
FnPrototype jitFn = jit.GetFunction<FnPrototype>("logical_or");
FastInterpFunction<FnPrototype> interpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("logical_or");
auto gold = [f](int32_t* dst, bool a, bool b) ->bool {
return f(a, dst, 1) || f(b, dst, 2);
};
check(jitFn, gold);
check(interpFn, gold);
}
{
FnPrototype jitFn = jit.GetFunction<FnPrototype>("logical_fn_3");
FastInterpFunction<FnPrototype> interpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("logical_fn_3");
auto gold = [f](int32_t* dst, bool a, bool b) ->bool {
return !f(a, dst, 1) || f(b, dst, 2);
};
check(jitFn, gold);
check(interpFn, gold);
}
{
FnPrototype jitFn = jit.GetFunction<FnPrototype>("logical_fn_4");
FastInterpFunction<FnPrototype> interpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("logical_fn_4");
auto gold = [f](int32_t* dst, bool a, bool b) ->bool {
return (!f(a, dst, 1) && f(b, dst, 2)) || (f(a || !b, dst, 3));
};
check(jitFn, gold);
check(interpFn, gold);
}
{
FnPrototype jitFn = jit.GetFunction<FnPrototype>("logical_fn_5");
FastInterpFunction<FnPrototype> interpFn = thread_pochiVMContext->m_curModule->
GetFastInterpGeneratedFunction<FnPrototype>("logical_fn_5");
auto gold = [f](int32_t* dst, bool a, bool b) ->bool {
return f(f(a, dst, 1) || f(b, dst, 2), dst, 3) && !f(f(a, dst, 4) && f(b, dst, 5), dst, 6);
};
check(jitFn, gold);
check(interpFn, gold);
}
}
TEST(Sanity, LogicalOp)
{
for (int i = 0; i < 100; i++)
{
CheckOnce();
}
}