add CALL_FINALLY opcode (#116159)
Pull Request resolved: https://github.com/pytorch/pytorch/pull/116159
Approved by: https://github.com/yanboliang
diff --git a/test/dynamo/test_repros.py b/test/dynamo/test_repros.py
index 8bef6a2..8ee979c 100644
--- a/test/dynamo/test_repros.py
+++ b/test/dynamo/test_repros.py
@@ -3659,6 +3659,16 @@
make_fn(None)()
+ def test_call_finally_opcode_python_3_8(self):
+ def fn():
+ try:
+ return torch.zeros(4)
+ finally:
+ return torch.ones(4) # noqa: SIM107, B012
+
+ result = torch.compile(fn, backend="aot_eager")()
+ self.assertEqual(result, torch.ones(4))
+
def test_string_format(self):
s = "temp{i}"
diff --git a/torch/_dynamo/symbolic_convert.py b/torch/_dynamo/symbolic_convert.py
index 48d5621..785e160 100644
--- a/torch/_dynamo/symbolic_convert.py
+++ b/torch/_dynamo/symbolic_convert.py
@@ -1082,15 +1082,32 @@
self.popn(2)
self.push(None)
+ def CALL_FINALLY(self, inst):
+ """
+ pushes the address of the next instruction onto the stack and increments
+ bytecode counter by delta
+ """
+ # Python 3.8 only
+ assert self.next_instruction is not None
+ addr = self.indexof[self.next_instruction]
+ self.push(ConstantVariable.create(addr))
+ self.instruction_pointer = self.indexof[inst.target]
+
def END_FINALLY(self, inst):
+ # Python 3.8 only
+ # https://docs.python.org/3.8/library/dis.html#opcode-END_FINALLY
tos = self.pop()
- assert tos is None
+ if isinstance(tos, ConstantVariable):
+ self.instruction_pointer = tos.as_python_constant()
+ else:
+ pass
def POP_FINALLY(self, inst):
+ # Python 3.8 only
preserve_tos = inst.argval
if preserve_tos:
tos = self.pop()
- assert self.pop() is None
+ _ = self.pop()
if preserve_tos:
self.push(tos)