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
|
From: Siu Kwan Lam <1929845+sklam@users.noreply.github.com>
Origin: https://github.com/numba/numba/pull/8545
Date: Wed, 28 Sep 2022 17:53:28 -0500
Subject: Support JUMP_IS_NONE
---
numba/core/byteflow.py | 6 ++++++
numba/core/interpreter.py | 27 +++++++++++++++++++++++++++
numba/tests/test_withlifting.py | 8 ++++----
3 files changed, 37 insertions(+), 4 deletions(-)
diff --git a/numba/core/byteflow.py b/numba/core/byteflow.py
index 8bd9dd1..cc93845 100644
--- a/numba/core/byteflow.py
+++ b/numba/core/byteflow.py
@@ -678,6 +678,12 @@ class TraceRunner(object):
op_JUMP_IF_FALSE_OR_POP = _op_JUMP_IF_OR_POP
op_JUMP_IF_TRUE_OR_POP = _op_JUMP_IF_OR_POP
+ def op_POP_JUMP_FORWARD_IF_NONE(self, state, inst):
+ self._op_POP_JUMP_IF(state, inst)
+
+ def op_POP_JUMP_FORWARD_IF_NOT_NONE(self, state, inst):
+ self._op_POP_JUMP_IF(state, inst)
+
def op_POP_JUMP_FORWARD_IF_FALSE(self, state, inst):
self._op_POP_JUMP_IF(state, inst)
diff --git a/numba/core/interpreter.py b/numba/core/interpreter.py
index ec590b7..c0e74a4 100644
--- a/numba/core/interpreter.py
+++ b/numba/core/interpreter.py
@@ -2787,6 +2787,33 @@ class Interpreter(object):
def op_JUMP_IF_TRUE(self, inst, pred):
self._op_JUMP_IF(inst, pred=pred, iftrue=True)
+ def _jump_if_none(self, inst, pred, iftrue):
+ brs = {
+ True: inst.get_jump_target(),
+ False: inst.next,
+ }
+ truebr = brs[iftrue]
+ falsebr = brs[not iftrue]
+
+ op = BINOPS_TO_OPERATORS["is"]
+
+ lhs = self.store(value=ir.Const(None, loc=self.loc),
+ name="${inst.offset}constnone")
+ rhs = self.get(pred)
+ isnone = ir.Expr.binop(op, lhs=lhs, rhs=rhs, loc=self.loc)
+
+ pname = "$%spred" % (inst.offset)
+ predicate = self.store(value=isnone, name=pname)
+ bra = ir.Branch(cond=predicate, truebr=truebr, falsebr=falsebr,
+ loc=self.loc)
+ self.current_block.append(bra)
+
+ def op_POP_JUMP_FORWARD_IF_NONE(self, inst, pred):
+ self._jump_if_none(inst, pred, True)
+
+ def op_POP_JUMP_FORWARD_IF_NOT_NONE(self, inst, pred):
+ self._jump_if_none(inst, pred, False)
+
def op_POP_JUMP_FORWARD_IF_FALSE(self, inst, pred):
self._op_JUMP_IF(inst, pred=pred, iftrue=False)
diff --git a/numba/tests/test_withlifting.py b/numba/tests/test_withlifting.py
index f969081..e72d2d3 100644
--- a/numba/tests/test_withlifting.py
+++ b/numba/tests/test_withlifting.py
@@ -172,13 +172,10 @@ gv_type = types.intp
class TestWithFinding(TestCase):
def check_num_of_with(self, func, expect_count):
the_ir = get_func_ir(func)
- the_ir.dump()
ct = len(find_setupwiths(the_ir)[0])
self.assertEqual(ct, expect_count)
def test_lift1(self):
- import dis
- dis.dis(lift1)
self.check_num_of_with(lift1, expect_count=1)
def test_lift2(self):
@@ -1186,7 +1183,6 @@ class TestLiftObjCaching(MemoryLeak, TestCase):
class TestBogusContext(BaseTestWithLifting):
def test_undefined_global(self):
the_ir = get_func_ir(lift_undefiend)
-
with self.assertRaises(errors.CompilerError) as raises:
with_lifting(
the_ir, self.typingctx, self.targetctx, self.flags, locals={},
@@ -1197,6 +1193,8 @@ class TestBogusContext(BaseTestWithLifting):
)
def test_invalid(self):
+ import dis
+ dis.dis(lift_invalid)
the_ir = get_func_ir(lift_invalid)
with self.assertRaises(errors.CompilerError) as raises:
@@ -1214,6 +1212,8 @@ class TestBogusContext(BaseTestWithLifting):
with open('') as f:
pass
+ import dis
+ dis.dis(foo)
with self.assertRaises(errors.UnsupportedError) as raises:
foo()
|