Skip to content

Commit

Permalink
Merge pull request #2 from arvid220u/principledjumps
Browse files Browse the repository at this point in the history
Make instrumentation much more robust
  • Loading branch information
arvid220u authored Aug 19, 2021
2 parents 2c43af9 + c5e3344 commit 4579f80
Show file tree
Hide file tree
Showing 4 changed files with 228 additions and 135 deletions.
6 changes: 5 additions & 1 deletion engine/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,14 @@ If you're curious, this is how the `run.py` script works. Study the source code

## Bytecode Instrumentation

Helpful resource: https://towardsdatascience.com/understanding-python-bytecode-e7edaae8734d

The engine counts the number of bytecodes used by code. It does this by inserting a function call, `__increment__()`, in between every single bytecode (each function call requires 3 bytecodes, so this increases the code size by 4x).

Python bytecode post-3.6 consists of two bytes each. The first byte corresponds to the instruction, and the second byte to the argument. In case the argument needs to be bigger than one byte, additional `EXTENDED_ARG` are inserted before, containing the higher bits of the argument. At most 3 of them are allowed per instruction.

The bytecodes operate on a stack machine. That is, most operations are performed on the top x stack elements. Here is a list of all bytecode instructions: https://docs.python.org/3/library/dis.html#python-bytecode-instructions.

The bytecode instrumentation is not perfect, because many of the bytecodes are not constant time. For example, `BUILD_STRING(i)` concatenates `i` strings, which should probably cost `i` bytecodes and not 1 bytecode. But meh, seems relatively benign.
The bytecode instrumentation is not perfect, because many of the bytecodes are not constant time. For example, `BUILD_STRING(i)` concatenates `i` strings, which should probably cost `i` bytecodes and not 1 bytecode. But meh, seems relatively benign.

Note: `is_jump_target` is never set on the extended args, but always on the root instruction. However, the jump target is the extended args instruction.
13 changes: 12 additions & 1 deletion engine/malthusia/engine/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,13 @@
import logging
import os

from .game import Game, BasicViewer, GameConstants
from .container import CodeContainer
from .container import CodeContainer

logger = logging.getLogger(__name__)
logger.setLevel(level=os.environ.get("LOGLEVEL", logging.WARNING))
ch = logging.StreamHandler()
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
ch.setLevel(level=logging.DEBUG)
ch.setFormatter(formatter)
logger.addHandler(ch)
66 changes: 45 additions & 21 deletions engine/malthusia/engine/container/instruction.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,37 +2,61 @@
from types import SimpleNamespace

class Instruction(SimpleNamespace):
def __init__(self, instruction, in_dict=None):
if in_dict is not None:
super().__init__(**in_dict)
def __init__(self, instruction, original: bool):
vals = {a: b for a,b in zip(dis.Instruction._fields, instruction)}
vals["orig_offset"] = vals["offset"]
vals["original"] = original
assert((original and vals["offset"] is not None) or (not original and vals["offset"] is None))
vals["orig_jump_target_offset"] = None
super().__init__(**vals)

def calculate_orig_jump_target_offset(self):
assert(self.offset is not None)
assert(self.original)
assert(self.is_jumper())
assert(self.offset == self.orig_offset)
if self.is_rel_jumper():
self.orig_jump_target_offset = self.orig_offset + self.arg + 2
elif self.is_abs_jumper():
self.orig_jump_target_offset = self.arg
else:
super().__init__(**{a:b for a,b in zip(dis.Instruction._fields+('jump_to', 'was_there', 'extra_extended_args'), instruction + (None, True, 0))})
assert(False)

def is_jumper(self):
return self.is_abs_jumper() or self.is_rel_jumper()
return self.is_rel_jumper() or self.is_abs_jumper()

def is_rel_jumper(self):
return self.opcode in dis.hasjrel

def is_abs_jumper(self):
return self.opcode in dis.hasjabs

def is_extended_arg(self):
return self.opcode == dis.opmap["EXTENDED_ARG"]

@classmethod
def ExtendedArgs(self, value):
return Instruction(None, in_dict={
'opcode':144, 'opname':'EXTENDED_ARGS', 'arg':value,
'argval':value, 'argrepr':value, 'offset':None,
'starts_line':None, 'is_jump_target':False, 'was_there': False,
'extra_extended_args': 0,
})

def calculate_offset(self, instructions):
# Return the offset (rel or abs) to self.jump_to in instructions
target_loc = 2 * instructions.index(self.jump_to) - 2 * self.jump_to.extra_extended_args
def ExtendedArgs(self):
return Instruction(dis.Instruction(
opcode=dis.opmap["EXTENDED_ARG"],
opname='EXTENDED_ARG',
arg=1,
argval=1,
argrepr="",
offset=None,
starts_line=None,
is_jump_target=False
), original=False)

def calculate_jump_arg(self, orig_to_curr_offset):
assert(self.is_jumper())
assert(self.orig_jump_target_offset is not None)
assert(self.orig_jump_target_offset in orig_to_curr_offset)

target_offset = orig_to_curr_offset[self.orig_jump_target_offset]

if self.is_abs_jumper():
return target_loc

self_loc = 2 * instructions.index(self)

return target_loc - self_loc - 2
return target_offset
elif self.is_rel_jumper():
return target_offset - self.offset - 2
else:
assert(False)
Loading

0 comments on commit 4579f80

Please sign in to comment.