| 1 |
import sys |
| 2 |
import os |
| 3 |
from hashlib import md5 |
| 4 |
|
| 5 |
import z3 |
| 6 |
|
| 7 |
from rpython.jit.metainterp.optimizeopt.test.test_z3intbound import ( |
| 8 |
make_z3_intbounds_instance, |
| 9 |
) |
| 10 |
from rpython.jit.metainterp.optimizeopt.intutils import IntBound |
| 11 |
from rpython.jit.metainterp.ruleopt import parse |
| 12 |
from rpython.rlib.rarithmetic import LONG_BIT, intmask, r_uint |
| 13 |
|
| 14 |
from rpython.config.translationoption import CACHE_DIR |
| 15 |
from rpython.tool.gcc_cache import try_atomic_write |
| 16 |
|
| 17 |
MAXINT = sys.maxint |
| 18 |
MININT = -sys.maxint - 1 |
| 19 |
|
| 20 |
class ProofProblem(Exception): |
| 21 |
pass |
| 22 |
|
| 23 |
class CouldNotProve(ProofProblem): |
| 24 |
def __init__(self, rule, cond, model, lhs, rhs, prover): |
| 25 |
self.rule = rule |
| 26 |
self.cond = cond |
| 27 |
self.model = model |
| 28 |
self.rhs = rhs |
| 29 |
self.lhs = lhs |
| 30 |
self.prover = prover |
| 31 |
|
| 32 |
def format(self): |
| 33 |
rule = self.rule |
| 34 |
res = ["Could not prove correctness of rule '%s'" % self.rule.name] |
| 35 |
if self.rule.sourcepos: |
| 36 |
res.append("in line %s" % (self.rule.sourcepos.lineno, )) |
| 37 |
prover = self.prover |
| 38 |
model = prover.solver.model() |
| 39 |
detail = [] |
| 40 |
res.append("counterexample given by Z3:") |
| 41 |
res.append("counterexample values:") |
| 42 |
for name, bound in prover.name_to_intbound.iteritems(): |
| 43 |
if name in prover.glue_conditions_added: |
| 44 |
realbound = IntBound(model.evaluate(bound.lower).as_signed_long(), |
| 45 |
model.evaluate(bound.upper).as_signed_long(), |
| 46 |
r_uint(model.evaluate(bound.tmask).as_signed_long()), |
| 47 |
r_uint(model.evaluate(bound.tvalue).as_signed_long()),) |
| 48 |
detail.append("bounds for %s: %s" % (name, realbound)) |
| 49 |
res.append("%s: %s" % (name, model[prover.name_to_z3[name]].as_signed_long())) |
| 50 |
res.append("operation %s with Z3 formula %s" % (rule.pattern, self.lhs)) |
| 51 |
res.append("has counterexample result vale: %s" % (model.evaluate(self.lhs).as_signed_long(), )) |
| 52 |
res.append("BUT") |
| 53 |
res.append("target expression: %s with Z3 formula %s" % (rule.target, self.rhs)) |
| 54 |
res.append("has counterexample value: %s" % (model.evaluate(self.rhs).as_signed_long(), )) |
| 55 |
res.extend(detail) |
| 56 |
return "\n".join(res) |
| 57 |
|
| 58 |
class RuleCannotApply(ProofProblem): |
| 59 |
def __init__(self, rule, cond, prover): |
| 60 |
self.rule = rule |
| 61 |
self.cond = cond |
| 62 |
self.prover = prover |
| 63 |
|
| 64 |
def format(self): |
| 65 |
rule = self.rule |
| 66 |
res = ["Rule '%s' cannot ever apply" % self.rule.name] |
| 67 |
if self.rule.sourcepos: |
| 68 |
res.append("in line %s" % (self.rule.sourcepos.lineno, )) |
| 69 |
prover = self.prover |
| 70 |
res.append("Z3 did not manage to find values for variables %s such that the following condition becomes True:" % ", ".join(prover.name_to_z3)) |
| 71 |
res.append(str(self.cond)) |
| 72 |
return "\n".join(res) |
| 73 |
|
| 74 |
TRUEBV = z3.BitVecVal(1, LONG_BIT) |
| 75 |
FALSEBV = z3.BitVecVal(0, LONG_BIT) |
| 76 |
|
| 77 |
|
| 78 |
def z3_cond(z3expr): |
| 79 |
return z3.If(z3expr, TRUEBV, FALSEBV) |
| 80 |
|
| 81 |
|
| 82 |
def z3_bool_expression(opname, arg0, arg1=None): |
| 83 |
expr = None |
| 84 |
valid = True |
| 85 |
if opname == "int_eq": |
| 86 |
expr = arg0 == arg1 |
| 87 |
elif opname == "int_ne": |
| 88 |
expr = arg0 != arg1 |
| 89 |
elif opname == "int_lt": |
| 90 |
expr = arg0 < arg1 |
| 91 |
elif opname == "int_le": |
| 92 |
expr = arg0 <= arg1 |
| 93 |
elif opname == "int_gt": |
| 94 |
expr = arg0 > arg1 |
| 95 |
elif opname == "int_ge": |
| 96 |
expr = arg0 >= arg1 |
| 97 |
elif opname == "uint_lt": |
| 98 |
expr = z3.ULT(arg0, arg1) |
| 99 |
elif opname == "uint_le": |
| 100 |
expr = z3.ULE(arg0, arg1) |
| 101 |
elif opname == "uint_gt": |
| 102 |
expr = z3.UGT(arg0, arg1) |
| 103 |
elif opname == "uint_ge": |
| 104 |
expr = z3.UGE(arg0, arg1) |
| 105 |
elif opname == "int_is_true": |
| 106 |
expr = arg0 != FALSEBV |
| 107 |
elif opname == "int_is_zero": |
| 108 |
expr = arg0 == FALSEBV |
| 109 |
else: |
| 110 |
assert 0 |
| 111 |
return expr, valid |
| 112 |
|
| 113 |
|
| 114 |
def z3_expression(opname, arg0, arg1=None): |
| 115 |
expr = None |
| 116 |
valid = True |
| 117 |
if opname == "int_add": |
| 118 |
expr = arg0 + arg1 |
| 119 |
elif opname == "int_sub": |
| 120 |
expr = arg0 - arg1 |
| 121 |
elif opname == "int_mul": |
| 122 |
expr = arg0 * arg1 |
| 123 |
elif opname == "int_and": |
| 124 |
expr = arg0 & arg1 |
| 125 |
elif opname == "int_or": |
| 126 |
expr = arg0 | arg1 |
| 127 |
elif opname == "int_xor": |
| 128 |
expr = arg0 ^ arg1 |
| 129 |
elif opname == "int_lshift": |
| 130 |
expr = arg0 << arg1 |
| 131 |
valid = z3.And(arg1 >= 0, arg1 < LONG_BIT) |
| 132 |
elif opname == "int_rshift": |
| 133 |
expr = arg0 >> arg1 |
| 134 |
valid = z3.And(arg1 >= 0, arg1 < LONG_BIT) |
| 135 |
elif opname == "uint_rshift": |
| 136 |
expr = z3.LShR(arg0, arg1) |
| 137 |
valid = z3.And(arg1 >= 0, arg1 < LONG_BIT) |
| 138 |
elif opname == "uint_mul_high": |
| 139 |
|
| 140 |
|
| 141 |
zarg0 = z3.ZeroExt(LONG_BIT, arg0) |
| 142 |
zarg1 = z3.ZeroExt(LONG_BIT, arg1) |
| 143 |
expr = z3.Extract(LONG_BIT * 2 - 1, LONG_BIT, zarg0 * zarg1) |
| 144 |
elif opname == "int_neg": |
| 145 |
expr = -arg0 |
| 146 |
elif opname == "int_invert": |
| 147 |
expr = ~arg0 |
| 148 |
elif opname == "int_force_ge_zero": |
| 149 |
expr = z3.If(arg0 < 0, 0, arg0) |
| 150 |
else: |
| 151 |
expr, valid = z3_bool_expression(opname, arg0, arg1) |
| 152 |
return z3_cond(expr), valid |
| 153 |
return expr, valid |
| 154 |
|
| 155 |
|
| 156 |
def z3_and(*args): |
| 157 |
args = [arg for arg in args if arg is not True] |
| 158 |
if args: |
| 159 |
if len(args) == 1: |
| 160 |
return args[0] |
| 161 |
return z3.And(*args) |
| 162 |
return True |
| 163 |
|
| 164 |
|
| 165 |
def z3_implies(a, b): |
| 166 |
if a is True: |
| 167 |
return b |
| 168 |
return z3.Implies(a, b) |
| 169 |
|
| 170 |
|
| 171 |
def popcount64(w): |
| 172 |
w -= (w >> 1) & 0x5555555555555555 |
| 173 |
w = (w & 0x3333333333333333) + ((w >> 2) & 0x3333333333333333) |
| 174 |
w = (w + (w >> 4)) & 0x0F0F0F0F0F0F0F0F |
| 175 |
return ((w * 0x0101010101010101) >> 56) & 0xFF |
| 176 |
|
| 177 |
|
| 178 |
def highest_bit(x): |
| 179 |
x |= x >> 1 |
| 180 |
x |= x >> 2 |
| 181 |
x |= x >> 4 |
| 182 |
x |= x >> 8 |
| 183 |
x |= x >> 16 |
| 184 |
x |= x >> 32 |
| 185 |
return popcount64(x) - 1 |
| 186 |
|
| 187 |
|
| 188 |
def z3_highest_bit(x): |
| 189 |
x |= z3.LShR(x, 1) |
| 190 |
x |= z3.LShR(x, 2) |
| 191 |
x |= z3.LShR(x, 4) |
| 192 |
x |= z3.LShR(x, 8) |
| 193 |
x |= z3.LShR(x, 16) |
| 194 |
x |= z3.LShR(x, 32) |
| 195 |
return popcount64(x) - 1 |
| 196 |
|
| 197 |
def z3_min(a, b): |
| 198 |
return z3.If(a <= b, a, b) |
| 199 |
|
| 200 |
class Prover(parse.Visitor): |
| 201 |
def __init__(self): |
| 202 |
self.solver = z3.Optimize() |
| 203 |
self.name_to_z3 = {} |
| 204 |
self.name_to_intbound = {} |
| 205 |
self.glue_conditions_added = set() |
| 206 |
self.glue_conditions = [] |
| 207 |
|
| 208 |
def prove(self, cond): |
| 209 |
z3res = self.solver.check(z3.Not(cond)) |
| 210 |
if z3res == z3.unsat: |
| 211 |
return True |
| 212 |
elif z3res == z3.unknown: |
| 213 |
return False |
| 214 |
elif z3res == z3.sat: |
| 215 |
global model |
| 216 |
model = self.solver.model() |
| 217 |
return False |
| 218 |
|
| 219 |
def _convert_var(self, name): |
| 220 |
def newvar(name, suffix=""): |
| 221 |
if suffix: |
| 222 |
name += "_" + suffix |
| 223 |
res = z3.BitVec(name, LONG_BIT) |
| 224 |
self.name_to_z3[name] = res |
| 225 |
return res |
| 226 |
|
| 227 |
if name in self.name_to_z3: |
| 228 |
return self.name_to_z3[name] |
| 229 |
res = newvar(name) |
| 230 |
self.solver.minimize(res) |
| 231 |
b = make_z3_intbounds_instance(name, res) |
| 232 |
self.name_to_intbound[name] = b |
| 233 |
return res |
| 234 |
|
| 235 |
def _convert_intbound(self, name): |
| 236 |
b = self.name_to_intbound[name] |
| 237 |
if name not in self.glue_conditions_added: |
| 238 |
self.glue_conditions.append(b.z3_formula()) |
| 239 |
self.glue_conditions_added.add(name) |
| 240 |
return b |
| 241 |
|
| 242 |
def _convert_attr( |
| 243 |
self, |
| 244 |
varname, |
| 245 |
attrname, |
| 246 |
): |
| 247 |
b = self._convert_intbound(varname) |
| 248 |
if attrname == 'ones': |
| 249 |
return b.tvalue |
| 250 |
if attrname == 'zeros': |
| 251 |
return ~(b.tvalue | b.tmask) |
| 252 |
return getattr(b, attrname) |
| 253 |
|
| 254 |
def visit_PatternOp(self, pattern): |
| 255 |
args = [self.visit(arg) for arg in pattern.args] |
| 256 |
res, valid = z3_expression(pattern.opname, *[arg[0] for arg in args]) |
| 257 |
return res, z3_and(valid, *[arg[1] for arg in args]) |
| 258 |
|
| 259 |
def visit_PatternVar(self, pattern): |
| 260 |
return self._convert_var(pattern.name), True |
| 261 |
|
| 262 |
def visit_PatternConst(self, pattern): |
| 263 |
if pattern.const == "LONG_BIT": |
| 264 |
return z3.BitVecVal(LONG_BIT, LONG_BIT) |
| 265 |
elif pattern.const == "MININT": |
| 266 |
return z3.BitVecVal(MININT, LONG_BIT), True |
| 267 |
elif pattern.const == "MAXINT": |
| 268 |
return z3.BitVecVal(MAXINT, LONG_BIT), True |
| 269 |
res = z3.BitVecVal(pattern.const, LONG_BIT) |
| 270 |
return res, True |
| 271 |
|
| 272 |
def visit_ShortcutOr(self, expr, targettype=int): |
| 273 |
assert targettype is bool |
| 274 |
left, leftvalid = self.visit(expr.left, bool) |
| 275 |
right, rightvalid = self.visit(expr.right, bool) |
| 276 |
res = z3.If(left, left, right) |
| 277 |
return res, z3_and(leftvalid, rightvalid) |
| 278 |
|
| 279 |
def visit_ShortcutAnd(self, expr, targettype=int): |
| 280 |
assert targettype is bool |
| 281 |
left, leftvalid = self.visit(expr.left, bool) |
| 282 |
right, rightvalid = self.visit(expr.right, bool) |
| 283 |
res = z3.If(left, right, left) |
| 284 |
return res, z3_and(leftvalid, rightvalid) |
| 285 |
|
| 286 |
def visit_BinOp(self, expr, targettype=int): |
| 287 |
left, leftvalid = self.visit(expr.left, int) |
| 288 |
right, rightvalid = self.visit(expr.right, int) |
| 289 |
if targettype is int: |
| 290 |
res, valid = z3_expression(expr.opname, left, right) |
| 291 |
else: |
| 292 |
assert targettype is bool |
| 293 |
res, valid = z3_bool_expression(expr.opname, left, right) |
| 294 |
return res, z3_and(leftvalid, rightvalid, valid) |
| 295 |
|
| 296 |
def visit_UnaryOp(self, expr, targettype=int): |
| 297 |
assert targettype is int |
| 298 |
left, leftvalid = self.visit(expr.left, targettype) |
| 299 |
res, valid = z3_expression(expr.opname, left) |
| 300 |
return res, z3_and(leftvalid, valid) |
| 301 |
|
| 302 |
def visit_Name(self, expr, targettype=int): |
| 303 |
if expr.name == "LONG_BIT": |
| 304 |
return 64, True |
| 305 |
if expr.name == "MAXINT": |
| 306 |
return MAXINT, True |
| 307 |
if expr.name == "MININT": |
| 308 |
import pdb;pdb.set_trace() |
| 309 |
return MININT, True |
| 310 |
if targettype is int: |
| 311 |
var = self._convert_var(expr.name) |
| 312 |
return var, True |
| 313 |
if targettype is IntBound: |
| 314 |
b = self._convert_intbound(expr.name) |
| 315 |
return b, True |
| 316 |
import pdb |
| 317 |
|
| 318 |
pdb.set_trace() |
| 319 |
|
| 320 |
def visit_Number(self, expr, targettype=int): |
| 321 |
assert targettype is int |
| 322 |
res = z3.BitVecVal(expr.value, LONG_BIT) |
| 323 |
return res, True |
| 324 |
|
| 325 |
def visit_Attribute(self, expr, targettype=int): |
| 326 |
res = self._convert_attr(expr.varname, expr.attrname) |
| 327 |
return res, True |
| 328 |
|
| 329 |
def visit_MethodCall(self, expr, targettype=int): |
| 330 |
res, resvalid = self.visit(expr.value, IntBound) |
| 331 |
assert isinstance(res, IntBound) |
| 332 |
args = [ |
| 333 |
self.visit(arg, arg.typ) for arg in expr.args |
| 334 |
] |
| 335 |
methargs = [arg[0] for arg in args] |
| 336 |
return getattr(res, expr.methname)(*methargs), z3_and( |
| 337 |
resvalid, *[arg[1] for arg in args] |
| 338 |
) |
| 339 |
|
| 340 |
def visit_FuncCall(self, expr, targettype=int): |
| 341 |
targettypes = [int] * len(expr.args) |
| 342 |
args = [ |
| 343 |
self.visit(arg, typ) for arg, typ in zip(expr.args, targettypes) |
| 344 |
] |
| 345 |
func = globals()["z3_" + expr.funcname] |
| 346 |
funcargs = [arg[0] for arg in args] |
| 347 |
return func(*funcargs), z3_and(*[arg[1] for arg in args]) |
| 348 |
|
| 349 |
def must_be_sat(self, rule, lhs, *conditions): |
| 350 |
def _find_index_to_remove(): |
| 351 |
for removeindex in range(len(conditions)): |
| 352 |
if self.solver.check(z3_and(lhs == somevar, *(conditions[:removeindex] + conditions[removeindex + 1:]))) == z3.unsat: |
| 353 |
return removeindex |
| 354 |
return -1 |
| 355 |
todo = list(conditions) |
| 356 |
conditions = [] |
| 357 |
while todo: |
| 358 |
c = todo.pop() |
| 359 |
if c is True: |
| 360 |
continue |
| 361 |
if c.decl().name() == 'and': |
| 362 |
todo.extend(c.children()) |
| 363 |
else: |
| 364 |
conditions.append(c) |
| 365 |
|
| 366 |
somevar = z3.BitVec('check_not_empty', LONG_BIT) |
| 367 |
conditions.append(lhs == somevar) |
| 368 |
cond = z3_and(*conditions) |
| 369 |
if self.solver.check(cond) != z3.sat: |
| 370 |
|
| 371 |
while 1: |
| 372 |
removeindex = _find_index_to_remove() |
| 373 |
if removeindex >= 0: |
| 374 |
del conditions[removeindex] |
| 375 |
cond = z3_and(*conditions) |
| 376 |
else: |
| 377 |
break |
| 378 |
raise RuleCannotApply(rule, cond, self) |
| 379 |
|
| 380 |
def check_rule(self, rule): |
| 381 |
import time |
| 382 |
t1 = time.time() |
| 383 |
print("checking %s" % rule) |
| 384 |
lhs, lhsvalid = self.visit(rule.pattern) |
| 385 |
self.must_be_sat(rule, lhs, lhsvalid) |
| 386 |
rhs, rhsvalid = self.visit(rule.target) |
| 387 |
implies_left = [lhsvalid] |
| 388 |
implies_right = [rhsvalid, rhs == lhs] |
| 389 |
for el in rule.elements: |
| 390 |
if isinstance(el, parse.Compute): |
| 391 |
expr, exprvalid = self.visit(el.expr, int) |
| 392 |
if el.expr.typ is not IntBound: |
| 393 |
implies_left.append(self._convert_var(el.name) == expr) |
| 394 |
implies_right.append(exprvalid) |
| 395 |
else: |
| 396 |
self.name_to_intbound[el.name] = expr |
| 397 |
self.glue_conditions_added.add(el.name) |
| 398 |
continue |
| 399 |
if isinstance(el, parse.Check): |
| 400 |
expr, _ = self.visit(el.expr, bool) |
| 401 |
implies_left.append(expr) |
| 402 |
continue |
| 403 |
assert 0, "unreachable" |
| 404 |
implies_left.extend(self.glue_conditions) |
| 405 |
self.must_be_sat(rule, lhs, lhsvalid, *implies_left) |
| 406 |
condition = z3_implies(z3_and(*implies_left), z3_and(*implies_right)) |
| 407 |
print(condition) |
| 408 |
if not self.prove(condition): |
| 409 |
raise CouldNotProve(rule, condition, model, lhs, rhs, self) |
| 410 |
t2 = time.time() |
| 411 |
print("took %s seconds" % (t2 - t1)) |
| 412 |
|
| 413 |
|
| 414 |
def prove_source(s, force=False): |
| 415 |
lines = s.splitlines() |
| 416 |
ast = parse.parse(s) |
| 417 |
for rule in ast.rules: |
| 418 |
if rule.cantproof: |
| 419 |
print "SKIPPING PROOF!", rule.name |
| 420 |
continue |
| 421 |
cachename = None |
| 422 |
if not force: |
| 423 |
start_lineno = rule.sourcepos.lineno - 1 |
| 424 |
end_lineno = rule.endsourcepos.lineno |
| 425 |
rule_lines = lines[start_lineno:end_lineno] |
| 426 |
|
| 427 |
h = md5("\n".join(rule_lines)) |
| 428 |
cachename = os.path.join( |
| 429 |
CACHE_DIR, "jit_dsl_rule_%s" % (h.hexdigest(), )) |
| 430 |
try: |
| 431 |
with open(cachename, 'rb') as f: |
| 432 |
f.read() |
| 433 |
print "reusing previous proof", rule.name |
| 434 |
continue |
| 435 |
except IOError: |
| 436 |
pass |
| 437 |
p = Prover() |
| 438 |
p.check_rule(rule) |
| 439 |
if cachename is not None: |
| 440 |
try_atomic_write(cachename, "\n".join(rule_lines)) |
| 441 |
return ast |