-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcodegen.cpp
352 lines (278 loc) · 11.3 KB
/
codegen.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
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#include <z3++.h>
#include <vector>
#include <chrono>
#include <random>
#include "isa.h"
// ====================================================================================================================
// ====================================================================================================================
std::mt19937 prng;
std::uniform_int_distribution<ValueType> rndDist(-INT_MAX - 1, INT_MAX);
// ====================================================================================================================
// ====================================================================================================================
ValueType TargetFunc(const ValueType x)
{
// return x >= 0 ? x : 1 - x;
return x >= 0 ? x : -x;
}
// ====================================================================================================================
// ====================================================================================================================
using expr_vector_array = std::vector<z3::expr_vector>;
class CodeGenContext
{
public:
CodeGenContext(z3::context& _ctx, const int _numInputs, const int _numChains, const int _numSteps, const ISASubset& _isa)
: ctx(_ctx)
, solver(_ctx)
, numInputs(_numInputs)
, numChains(_numChains)
, numInstr(_numSteps)
, opCode(_ctx)
, regX(_ctx)
, regY(_ctx)
, imm32(_ctx)
, isa(_isa)
{
}
z3::context& ctx;
z3::solver solver;
int numInputs = 0;
int numChains = 0;
int numInstr = 0;
z3::expr_vector opCode;
z3::expr_vector regX;
z3::expr_vector regY;
z3::expr_vector imm32;
expr_vector_array R;
ISASubset isa;
};
// ====================================================================================================================
// ====================================================================================================================
void CreateConstants(CodeGenContext& codeGen)
{
for (int c = 0; c < codeGen.numChains; c++)
{
z3::expr_vector chainR(codeGen.ctx);
for (int idx = 0; idx < codeGen.numInstr; idx++)
{
char name[16];
sprintf(name, "R%d_c%d", idx, c);
chainR.push_back(codeGen.ctx.bv_const(name, 32));
}
codeGen.R.push_back(chainR);
}
for (int idx = 0; idx < codeGen.numInstr; idx++)
{
char name[16];
sprintf(name, "opCode_s%d", idx);
codeGen.opCode.push_back(codeGen.ctx.int_const(name));
sprintf(name, "regX_s%d", idx);
codeGen.regX.push_back(codeGen.ctx.int_const(name));
sprintf(name, "regY_s%d", idx);
codeGen.regY.push_back(codeGen.ctx.int_const(name));
sprintf(name, "imm32_s%d", idx);
codeGen.imm32.push_back(codeGen.ctx.bv_const(name, 32));
}
}
// ====================================================================================================================
// ====================================================================================================================
void AddConstraints(CodeGenContext& codeGen)
{
codeGen.solver = z3::solver(codeGen.ctx);
for (int idx = codeGen.numInputs; idx < codeGen.numInstr; idx++)
{
codeGen.solver.add(codeGen.opCode[idx] >= 0);
codeGen.solver.add(codeGen.opCode[idx] < codeGen.isa.size());
codeGen.solver.add(codeGen.regX[idx] >= 0);
codeGen.solver.add(codeGen.regX[idx] < idx);
codeGen.solver.add(codeGen.regY[idx] >= 0);
codeGen.solver.add(codeGen.regY[idx] < idx);
z3::expr_vector shiftConstraints(codeGen.ctx);
shiftConstraints.push_back(codeGen.imm32[idx] > 0);
shiftConstraints.push_back(codeGen.imm32[idx] <= 31);
z3::expr andShiftConstriants = z3::mk_and(shiftConstraints);
for (const int shiftOpCode: codeGen.isa.opCodesForKindMask(Instruction::Kind_Shift))
{
codeGen.solver.add(z3::to_expr(codeGen.ctx, z3::implies(codeGen.opCode[idx] == shiftOpCode, andShiftConstriants)));
}
}
}
// ====================================================================================================================
// ====================================================================================================================
z3::expr SelectOperand(CodeGenContext& codeGen, z3::expr_vector& R, const z3::expr& regIdx, const int instructionIdx)
{
z3::expr cond = codeGen.ctx.bv_val(0, 32);
for (int i = instructionIdx - 1; i >= 0; i--)
{
cond = z3::to_expr(codeGen.ctx, z3::ite(regIdx == i, R[i], cond));
}
return cond;
}
// ====================================================================================================================
// ====================================================================================================================
void AddPerChainConstraints(CodeGenContext& codeGen)
{
for (int c = 0; c < codeGen.numChains; c++)
{
auto& chainR = codeGen.R[c];
const ValueType x = rndDist(prng);
const ValueType out = TargetFunc(x);
codeGen.solver.add(chainR[0] == codeGen.ctx.bv_val(x, 32));
codeGen.solver.add(chainR[codeGen.numInstr - 1] == codeGen.ctx.bv_val(out, 32));
for (int idx = codeGen.numInputs; idx < codeGen.numInstr; idx++)
{
const auto& op = codeGen.opCode[idx];
const auto& x = SelectOperand(codeGen, chainR, codeGen.regX[idx], idx);
const auto& y = op != codeGen.isa.opCodeForName("set") ?
SelectOperand(codeGen, chainR, codeGen.regY[idx], idx) :
codeGen.ctx.bv_val(0, 32);
const auto& imm = codeGen.imm32[idx];
auto opers = SimOperands(codeGen.ctx, x, y, imm);
z3::expr cond = codeGen.ctx.bv_val(0, 32);
for (int opcodeIdx = codeGen.isa.size() - 1; opcodeIdx >= 0; opcodeIdx--)
{
cond = z3::to_expr(codeGen.ctx, z3::ite(op == opcodeIdx, codeGen.isa.simulateOp(opcodeIdx, opers), cond));
}
codeGen.solver.add(chainR[idx] == cond);
}
}
}
// ====================================================================================================================
// ====================================================================================================================
EvalOperands CreateEvalOperands(CodeGenContext& codeGen, const int instructionIdx, int& opcodeIdx)
{
const auto model = codeGen.solver.get_model();
const auto op = model.eval(codeGen.opCode[instructionIdx]).get_numeral_int();
const auto x = model.eval(codeGen.regX[instructionIdx]).get_numeral_int();
const auto y = model.eval(codeGen.regY[instructionIdx]).get_numeral_int();
// imm32 is not always retreivable so need to check first
const auto imm_ = model.eval(codeGen.imm32[instructionIdx]);
const auto imm = imm_.is_numeral() ? imm_.get_numeral_uint() : 0;
opcodeIdx = op;
return EvalOperands(codeGen.ctx, x, y, imm);
}
// ====================================================================================================================
// ====================================================================================================================
void PrintModel(CodeGenContext& codeGen, int numInputs)
{
printf("Generated code:\n");
for (int i = 0; i < numInputs; i++)
{
printf(" r%d = <input>\n", i);
}
for (int instrIdx = numInputs; instrIdx < codeGen.numInstr; instrIdx++)
{
int opcodeIdx = -1;
const auto opers = CreateEvalOperands(codeGen, instrIdx, opcodeIdx);
codeGen.isa.formatOp(opcodeIdx, instrIdx, opers);
}
printf("\n");
}
// ====================================================================================================================
// ====================================================================================================================
std::vector<ValueType> Evaluate(CodeGenContext& codeGen, const std::vector<ValueType>& initValues)
{
std::vector<ValueType> evalState(codeGen.numInstr);
for (int i = 0; i < initValues.size(); i++)
{
evalState[i] = initValues[i];
}
for (int instrIdx = codeGen.numInputs; instrIdx < codeGen.numInstr; instrIdx++)
{
int opcodeIdx = -1;
auto opers = CreateEvalOperands(codeGen, instrIdx, opcodeIdx);
// if we're evaluation we want x & y to have the values of the registers
opers.x = evalState[opers.x];
opers.y = evalState[opers.y];
evalState[instrIdx] = codeGen.isa.evaluateOp(opcodeIdx, opers);
}
return evalState;
}
// ====================================================================================================================
// ====================================================================================================================
z3::check_result Solve(CodeGenContext& codeGen)
{
const auto start = std::chrono::high_resolution_clock::now();
const auto res = codeGen.solver.check();
const auto end = std::chrono::high_resolution_clock::now();
const auto delta = end - start;
const auto delta_ms = std::chrono::duration_cast<std::chrono::milliseconds>(delta);
printf(" solver.check() call completed: %d ms\n", delta_ms);
return res;
}
// ====================================================================================================================
// ====================================================================================================================
static bool EvaluateModel(CodeGenContext& codeGen)
{
int numPassed = 0;
const ValueType x = rndDist(prng);
const ValueType y = rndDist(prng);
const ValueType expected = TargetFunc(x);
const auto stateValues = Evaluate(codeGen, {x, y});
const ValueType actual = stateValues.back();
return expected == actual;
}
// ====================================================================================================================
// ====================================================================================================================
bool FindSolution(const int numInstructions, const ISASubset& isa)
{
z3::context ctx;
const int numChains = 10;
const int numInputs = 1;
CodeGenContext codeGen(ctx, numInputs, numChains, numInstructions, isa);
CreateConstants(codeGen);
AddConstraints(codeGen);
AddPerChainConstraints(codeGen);
const auto res = Solve(codeGen);
if (res != z3::sat)
{
printf(" unsatifiable\n");
return false;
}
printf(" satisified!\n\n");
PrintModel(codeGen, numInputs);
const int NUM_TESTS = 10000;
int numPassed = 0;
printf("Testing with random values...\n");
for (int i = 0; i < NUM_TESTS; i++)
{
if (!EvaluateModel(codeGen))
{
break;
}
numPassed++;
}
printf(" %d / %d passed\n\n", numPassed, NUM_TESTS);
return true;
}
// ====================================================================================================================
// ====================================================================================================================
int main(int argc, char** argv)
{
// Don't need to always use the full ISA
ISASubset isa;
isa.addOpcode(ISA_OpCodeForName("set"));
// isa.addOpcode(ISA_OpCodeForName("add"));
isa.addOpcode(ISA_OpCodeForName("sub"));
isa.addOpcode(ISA_OpCodeForName("xor"));
// isa.addOpcode(ISA_OpCodeForName("shr"));
isa.addOpcode(ISA_OpCodeForName("gt"));
const int minInstructions = 2;
const int maxInstructions = 8;
for (int i = 2; i < maxInstructions; i++)
{
try
{
printf("Try with %d instructions...\n", i);
if (FindSolution(i, isa))
{
break;
}
}
catch (z3::exception& e)
{
std::cout << e.msg() << std::endl;
}
}
}
// ====================================================================================================================
// ====================================================================================================================