From 9a4de1d261e03b23b9b2c295561af8d419c11314 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Sat, 2 Sep 2023 04:54:35 +0000 Subject: [PATCH 1/3] fix reset_offset --- .../instruction_utils/instruction_utils.py | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/sot/opcode_translator/instruction_utils/instruction_utils.py b/sot/opcode_translator/instruction_utils/instruction_utils.py index 1b6ee34b4..ec70610e2 100644 --- a/sot/opcode_translator/instruction_utils/instruction_utils.py +++ b/sot/opcode_translator/instruction_utils/instruction_utils.py @@ -143,11 +143,19 @@ def reset_offset(instructions: list[Instruction]) -> None: Returns: None """ + from ..executor.pycode_generator import get_instruction_size + + if sys.version_info >= (3, 11): + current_offset = 0 + for instr in instructions: + instr.offset = current_offset + current_offset += get_instruction_size(instr) + return for idx, instr in enumerate(instructions): instr.offset = idx * 2 -def relocate_jump_target(instuctions: list[Instruction]) -> None: +def relocate_jump_target(instructions: list[Instruction]) -> None: """ If a jump instruction is found, this function will adjust the jump targets based on the presence of EXTENDED_ARG instructions. If an EXTENDED_ARG instruction exists for the jump target, use its offset as the new target. @@ -159,23 +167,28 @@ def relocate_jump_target(instuctions: list[Instruction]) -> None: None """ extended_arg = [] - for instr in instuctions: + for instr in instructions: if instr.opname == "EXTENDED_ARG": extended_arg.append(instr) continue if instr.opname in ALL_JUMP: + assert instr.jump_to is not None + assert instr.offset is not None # if jump target has extended_arg, should jump to the first extended_arg opcode jump_target = ( instr.jump_to.offset if instr.jump_to.first_ex_arg is None else instr.jump_to.first_ex_arg.offset ) + assert jump_target is not None if instr.opname in REL_JUMP: new_arg = jump_target - instr.offset - 2 elif instr.opname in ABS_JUMP: new_arg = jump_target + else: + raise ValueError(f"Unknown jump type: {instr.opname}") if sys.version_info >= (3, 10): new_arg //= 2 @@ -192,7 +205,6 @@ def relocate_jump_target(instuctions: list[Instruction]) -> None: extended_arg[0].arg += new_arg << 8 else: instr.arg = new_arg - extended_arg.clear() From 2536dbfd028a9d23bf52c090ae92ed01b24f5823 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Sat, 2 Sep 2023 04:56:09 +0000 Subject: [PATCH 2/3] enable test_11_jumps.py --- tests/test_11_jumps.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_11_jumps.py b/tests/test_11_jumps.py index a31807704..74c09b5b7 100644 --- a/tests/test_11_jumps.py +++ b/tests/test_11_jumps.py @@ -1,6 +1,5 @@ from __future__ import annotations -import sys import unittest from test_case_base import TestCaseBase @@ -86,9 +85,6 @@ def test_simple(self): self.assert_results(pop_jump_if_not_none, None, a) self.assert_results(pop_jump_if_not_none, True, a) - @unittest.skipIf( - sys.version_info >= (3, 11), "Python 3.11+ not support breakgraph." - ) def test_breakgraph(self): self.assert_results(pop_jump_if_false, true_tensor, a) self.assert_results(jump_if_false_or_pop, true_tensor, a) From 3f8b314bc9bc3f5fa15b84f678cc199ecd1ce413 Mon Sep 17 00:00:00 2001 From: Zhan Rongrui <2742392377@qq.com> Date: Sat, 2 Sep 2023 05:03:23 +0000 Subject: [PATCH 3/3] improve --- .../instruction_utils/instruction_utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sot/opcode_translator/instruction_utils/instruction_utils.py b/sot/opcode_translator/instruction_utils/instruction_utils.py index ec70610e2..6e1a391ca 100644 --- a/sot/opcode_translator/instruction_utils/instruction_utils.py +++ b/sot/opcode_translator/instruction_utils/instruction_utils.py @@ -5,7 +5,7 @@ import sys from typing import TYPE_CHECKING, Any -from .opcode_info import ABS_JUMP, ALL_JUMP, REL_JUMP +from .opcode_info import ALL_JUMP, REL_JUMP if TYPE_CHECKING: import types @@ -185,10 +185,9 @@ def relocate_jump_target(instructions: list[Instruction]) -> None: if instr.opname in REL_JUMP: new_arg = jump_target - instr.offset - 2 - elif instr.opname in ABS_JUMP: + else: # instr.opname in ABS_JUMP new_arg = jump_target - else: - raise ValueError(f"Unknown jump type: {instr.opname}") + if sys.version_info >= (3, 10): new_arg //= 2