[pypy-commit] pypy default: merge faster-nested-scopes: make nested scopes be a bit better supported by the JIT.
cfbolz
noreply at buildbot.pypy.org
Thu Sep 1 15:34:01 CEST 2011
Author: Carl Friedrich Bolz <cfbolz at gmx.de>
Branch:
Changeset: r46983:2641481ac49e
Date: 2011-09-01 15:30 +0200
http://bitbucket.org/pypy/pypy/changeset/2641481ac49e/
Log: merge faster-nested-scopes: make nested scopes be a bit better
supported by the JIT.
- the cells on the frame are now part of the virtualizable.
- the constructor of frame get a reference to the outer scope, which
often makes reading of the inherited cells constant-foldable
diff --git a/pypy/interpreter/baseobjspace.py b/pypy/interpreter/baseobjspace.py
--- a/pypy/interpreter/baseobjspace.py
+++ b/pypy/interpreter/baseobjspace.py
@@ -626,9 +626,9 @@
self.default_compiler = compiler
return compiler
- def createframe(self, code, w_globals, closure=None):
+ def createframe(self, code, w_globals, outer_func=None):
"Create an empty PyFrame suitable for this code object."
- return self.FrameClass(self, code, w_globals, closure)
+ return self.FrameClass(self, code, w_globals, outer_func)
def allocate_lock(self):
"""Return an interp-level Lock object if threads are enabled,
diff --git a/pypy/interpreter/function.py b/pypy/interpreter/function.py
--- a/pypy/interpreter/function.py
+++ b/pypy/interpreter/function.py
@@ -30,7 +30,7 @@
can_change_code = True
_immutable_fields_ = ['code?',
'w_func_globals?',
- 'closure?',
+ 'closure?[*]',
'defs_w?[*]',
'name?']
@@ -96,7 +96,7 @@
assert isinstance(code, PyCode)
if nargs < 5:
new_frame = self.space.createframe(code, self.w_func_globals,
- self.closure)
+ self)
for i in funccallunrolling:
if i < nargs:
new_frame.locals_stack_w[i] = args_w[i]
@@ -156,7 +156,7 @@
def _flat_pycall(self, code, nargs, frame):
# code is a PyCode
new_frame = self.space.createframe(code, self.w_func_globals,
- self.closure)
+ self)
for i in xrange(nargs):
w_arg = frame.peekvalue(nargs-1-i)
new_frame.locals_stack_w[i] = w_arg
@@ -167,7 +167,7 @@
def _flat_pycall_defaults(self, code, nargs, frame, defs_to_load):
# code is a PyCode
new_frame = self.space.createframe(code, self.w_func_globals,
- self.closure)
+ self)
for i in xrange(nargs):
w_arg = frame.peekvalue(nargs-1-i)
new_frame.locals_stack_w[i] = w_arg
diff --git a/pypy/interpreter/nestedscope.py b/pypy/interpreter/nestedscope.py
--- a/pypy/interpreter/nestedscope.py
+++ b/pypy/interpreter/nestedscope.py
@@ -8,7 +8,7 @@
class Cell(Wrappable):
"A simple container for a wrapped value."
-
+
def __init__(self, w_value=None):
self.w_value = w_value
@@ -90,32 +90,33 @@
# variables coming from a parent function in which i'm nested
# 'closure' is a list of Cell instances: the received free vars.
- cells = None
-
@jit.unroll_safe
- def initialize_frame_scopes(self, closure, code):
- super_initialize_frame_scopes(self, closure, code)
+ def initialize_frame_scopes(self, outer_func, code):
+ super_initialize_frame_scopes(self, outer_func, code)
ncellvars = len(code.co_cellvars)
nfreevars = len(code.co_freevars)
if not nfreevars:
if not ncellvars:
+ self.cells = []
return # no self.cells needed - fast path
- if closure is None:
- closure = []
- elif closure is None:
+ elif outer_func is None:
space = self.space
raise OperationError(space.w_TypeError,
space.wrap("directly executed code object "
"may not contain free variables"))
- if len(closure) != nfreevars:
+ if outer_func and outer_func.closure:
+ closure_size = len(outer_func.closure)
+ else:
+ closure_size = 0
+ if closure_size != nfreevars:
raise ValueError("code object received a closure with "
"an unexpected number of free variables")
self.cells = [None] * (ncellvars + nfreevars)
for i in range(ncellvars):
self.cells[i] = Cell()
for i in range(nfreevars):
- self.cells[i + ncellvars] = closure[i]
-
+ self.cells[i + ncellvars] = outer_func.closure[i]
+
def _getcells(self):
return self.cells
diff --git a/pypy/interpreter/pycode.py b/pypy/interpreter/pycode.py
--- a/pypy/interpreter/pycode.py
+++ b/pypy/interpreter/pycode.py
@@ -198,7 +198,7 @@
def funcrun(self, func, args):
frame = self.space.createframe(self, func.w_func_globals,
- func.closure)
+ func)
sig = self._signature
# speed hack
fresh_frame = jit.hint(frame, access_directly=True,
@@ -211,7 +211,7 @@
def funcrun_obj(self, func, w_obj, args):
frame = self.space.createframe(self, func.w_func_globals,
- func.closure)
+ func)
sig = self._signature
# speed hack
fresh_frame = jit.hint(frame, access_directly=True,
diff --git a/pypy/interpreter/pyframe.py b/pypy/interpreter/pyframe.py
--- a/pypy/interpreter/pyframe.py
+++ b/pypy/interpreter/pyframe.py
@@ -51,7 +51,7 @@
is_being_profiled = False
escaped = False # see mark_as_escaped()
- def __init__(self, space, code, w_globals, closure):
+ def __init__(self, space, code, w_globals, outer_func):
if not we_are_translated():
assert type(self) in (space.FrameClass, CPythonFrame), (
"use space.FrameClass(), not directly PyFrame()")
@@ -70,7 +70,7 @@
self.builtin = space.builtin.pick_builtin(w_globals)
# regular functions always have CO_OPTIMIZED and CO_NEWLOCALS.
# class bodies only have CO_NEWLOCALS.
- self.initialize_frame_scopes(closure, code)
+ self.initialize_frame_scopes(outer_func, code)
self.f_lineno = code.co_firstlineno
def mark_as_escaped(self):
@@ -117,8 +117,8 @@
return self.builtin
else:
return self.space.builtin
-
- def initialize_frame_scopes(self, closure, code):
+
+ def initialize_frame_scopes(self, outer_func, code):
# regular functions always have CO_OPTIMIZED and CO_NEWLOCALS.
# class bodies only have CO_NEWLOCALS.
# CO_NEWLOCALS: make a locals dict unless optimized is also set
@@ -385,7 +385,11 @@
# do not use the instance's __init__ but the base's, because we set
# everything like cells from here
- PyFrame.__init__(self, space, pycode, w_globals, closure)
+ # XXX hack
+ from pypy.interpreter.function import Function
+ outer_func = Function(space, None, closure=closure,
+ forcename="fake")
+ PyFrame.__init__(self, space, pycode, w_globals, outer_func)
f_back = space.interp_w(PyFrame, w_f_back, can_be_None=True)
new_frame.f_backref = jit.non_virtual_ref(f_back)
diff --git a/pypy/module/__builtin__/functional.py b/pypy/module/__builtin__/functional.py
--- a/pypy/module/__builtin__/functional.py
+++ b/pypy/module/__builtin__/functional.py
@@ -292,7 +292,7 @@
raise
break
new_frame = space.createframe(code, w_func.w_func_globals,
- w_func.closure)
+ w_func)
new_frame.locals_stack_w[0] = w_item
w_res = new_frame.run()
result_w.append(w_res)
diff --git a/pypy/module/cpyext/frameobject.py b/pypy/module/cpyext/frameobject.py
--- a/pypy/module/cpyext/frameobject.py
+++ b/pypy/module/cpyext/frameobject.py
@@ -57,7 +57,7 @@
code = space.interp_w(PyCode, w_code)
w_globals = from_ref(space, py_frame.c_f_globals)
- frame = space.FrameClass(space, code, w_globals, closure=None)
+ frame = space.FrameClass(space, code, w_globals, outer_func=None)
frame.f_lineno = py_frame.c_f_lineno
w_obj = space.wrap(frame)
track_reference(space, py_obj, w_obj)
diff --git a/pypy/module/pypyjit/interp_jit.py b/pypy/module/pypyjit/interp_jit.py
--- a/pypy/module/pypyjit/interp_jit.py
+++ b/pypy/module/pypyjit/interp_jit.py
@@ -21,6 +21,7 @@
PyFrame._virtualizable2_ = ['last_instr', 'pycode',
'valuestackdepth', 'locals_stack_w[*]',
+ 'cells[*]',
'last_exception',
'lastblock',
'is_being_profiled',
diff --git a/pypy/module/pypyjit/test_pypy_c/test_call.py b/pypy/module/pypyjit/test_pypy_c/test_call.py
--- a/pypy/module/pypyjit/test_pypy_c/test_call.py
+++ b/pypy/module/pypyjit/test_pypy_c/test_call.py
@@ -174,7 +174,7 @@
guard_no_overflow(descr=...)
i18 = force_token()
--TICK--
- jump(p0, p1, p2, p3, p4, i8, p7, i17, p8, i9, i17, p10, p11, p12, descr=<Loop0>)
+ jump(..., descr=<Loop0>)
""")
def test_default_and_kw(self):
@@ -396,3 +396,70 @@
--TICK--
jump(..., descr=<Loop0>)
""")
+
+ def test_global_closure_has_constant_cells(self):
+ log = self.run("""
+ def make_adder(n):
+ def add(x):
+ return x + n
+ return add
+ add5 = make_adder(5)
+ def main():
+ i = 0
+ while i < 5000:
+ i = add5(i) # ID: call
+ """, [])
+ loop, = log.loops_by_id('call', is_entry_bridge=True)
+ assert loop.match("""
+ guard_value(i6, 1, descr=...)
+ guard_nonnull_class(p8, ConstClass(W_IntObject), descr=...)
+ guard_value(i4, 0, descr=...)
+ guard_value(p3, ConstPtr(ptr14), descr=...)
+ i15 = getfield_gc_pure(p8, descr=<SignedFieldDescr pypy.objspace.std.intobject.W_IntObject.inst_intval 8>)
+ i17 = int_lt(i15, 5000)
+ guard_true(i17, descr=...)
+ p18 = getfield_gc(p0, descr=<GcPtrFieldDescr pypy.interpreter.eval.Frame.inst_w_globals 8>)
+ guard_value(p18, ConstPtr(ptr19), descr=...)
+ p20 = getfield_gc(p18, descr=<GcPtrFieldDescr pypy.objspace.std.dictmultiobject.W_DictMultiObject.inst_strategy 12>)
+ guard_value(p20, ConstPtr(ptr21), descr=...)
+ guard_not_invalidated(descr=...)
+ # most importantly, there is no getarrayitem_gc here
+ p23 = call(ConstClass(getexecutioncontext), descr=<GcPtrCallDescr>)
+ p24 = getfield_gc(p23, descr=<GcPtrFieldDescr pypy.interpreter.executioncontext.ExecutionContext.inst_topframeref 36>)
+ i25 = force_token()
+ p26 = getfield_gc(p23, descr=<GcPtrFieldDescr pypy.interpreter.executioncontext.ExecutionContext.inst_w_tracefunc 44>)
+ guard_isnull(p26, descr=...)
+ i27 = getfield_gc(p23, descr=<NonGcPtrFieldDescr pypy.interpreter.executioncontext.ExecutionContext.inst_profilefunc 24>)
+ i28 = int_is_zero(i27)
+ guard_true(i28, descr=...)
+ p30 = getfield_gc(ConstPtr(ptr29), descr=<GcPtrFieldDescr pypy.interpreter.nestedscope.Cell.inst_w_value 8>)
+ guard_nonnull_class(p30, ConstClass(W_IntObject), descr=...)
+ i32 = getfield_gc_pure(p30, descr=<SignedFieldDescr pypy.objspace.std.intobject.W_IntObject.inst_intval 8>)
+ i33 = int_add_ovf(i15, i32)
+ guard_no_overflow(descr=...)
+ --TICK--
+ jump(p0, p1, p2, p5, i33, i32, p23, p30, p24, descr=<Loop0>)
+ """)
+
+ def test_local_closure_is_virtual(self):
+ log = self.run("""
+ def main():
+ i = 0
+ while i < 5000:
+ def add():
+ return i + 1
+ i = add() # ID: call
+ """, [])
+ loop, = log.loops_by_id('call')
+ assert loop.match("""
+ i8 = getfield_gc_pure(p6, descr=<SignedFieldDescr pypy.objspace.std.intobject.W_IntObject.inst_intval 8>)
+ i10 = int_lt(i8, 5000)
+ guard_true(i10, descr=...)
+ i11 = force_token()
+ i13 = int_add(i8, 1)
+ --TICK--
+ p22 = new_with_vtable(ConstClass(W_IntObject))
+ setfield_gc(p22, i13, descr=<SignedFieldDescr pypy.objspace.std.intobject.W_IntObject.inst_intval 8>)
+ setfield_gc(p4, p22, descr=<GcPtrFieldDescr pypy.interpreter.nestedscope.Cell.inst_w_value 8>)
+ jump(p0, p1, p2, p3, p4, p7, p22, p7, descr=<Loop0>)
+ """)
diff --git a/pypy/objspace/flow/flowcontext.py b/pypy/objspace/flow/flowcontext.py
--- a/pypy/objspace/flow/flowcontext.py
+++ b/pypy/objspace/flow/flowcontext.py
@@ -184,7 +184,7 @@
class FlowExecutionContext(ExecutionContext):
- def __init__(self, space, code, globals, constargs={}, closure=None,
+ def __init__(self, space, code, globals, constargs={}, outer_func=None,
name=None):
ExecutionContext.__init__(self, space)
self.code = code
@@ -193,11 +193,11 @@
self.crnt_offset = -1
self.crnt_frame = None
- if closure is None:
+ if outer_func and outer_func.closure:
+ self.closure = [nestedscope.Cell(Constant(value))
+ for value in outer_func.closure]
+ else:
self.closure = None
- else:
- self.closure = [nestedscope.Cell(Constant(value))
- for value in closure]
frame = self.create_frame()
formalargcount = code.getformalargcount()
arg_list = [Variable() for i in range(formalargcount)]
@@ -216,7 +216,7 @@
# while ignoring any operation like the creation of the locals dict
self.recorder = []
frame = FlowSpaceFrame(self.space, self.code,
- self.w_globals, self.closure)
+ self.w_globals, self)
frame.last_instr = 0
return frame
diff --git a/pypy/objspace/flow/objspace.py b/pypy/objspace/flow/objspace.py
--- a/pypy/objspace/flow/objspace.py
+++ b/pypy/objspace/flow/objspace.py
@@ -252,9 +252,9 @@
raise TypeError("%r is a generator" % (func,))
code = PyCode._from_code(self, code)
if func.func_closure is None:
- closure = None
+ cl = None
else:
- closure = [extract_cell_content(c) for c in func.func_closure]
+ cl = [extract_cell_content(c) for c in func.func_closure]
# CallableFactory.pycall may add class_ to functions that are methods
name = func.func_name
class_ = getattr(func, 'class_', None)
@@ -262,8 +262,10 @@
name = '%s.%s' % (class_.__name__, name)
for c in "<>&!":
name = name.replace(c, '_')
+ class outerfunc: # hack
+ closure = cl
ec = flowcontext.FlowExecutionContext(self, code, func.func_globals,
- constargs, closure, name)
+ constargs, outerfunc, name)
graph = ec.graph
graph.func = func
# attach a signature and defaults to the graph
diff --git a/pypy/objspace/std/fake.py b/pypy/objspace/std/fake.py
--- a/pypy/objspace/std/fake.py
+++ b/pypy/objspace/std/fake.py
@@ -142,7 +142,7 @@
def funcrun(self, func, args):
frame = func.space.createframe(self, func.w_func_globals,
- func.closure)
+ func)
sig = self.signature()
scope_w = args.parse_obj(None, func.name, sig, func.defs_w)
frame.setfastscope(scope_w)
diff --git a/pypy/objspace/std/objspace.py b/pypy/objspace/std/objspace.py
--- a/pypy/objspace/std/objspace.py
+++ b/pypy/objspace/std/objspace.py
@@ -129,12 +129,12 @@
ec._py_repr = None
return ec
- def createframe(self, code, w_globals, closure=None):
+ def createframe(self, code, w_globals, outer_func=None):
from pypy.objspace.std.fake import CPythonFakeCode, CPythonFakeFrame
if not we_are_translated() and isinstance(code, CPythonFakeCode):
return CPythonFakeFrame(self, code, w_globals)
else:
- return ObjSpace.createframe(self, code, w_globals, closure)
+ return ObjSpace.createframe(self, code, w_globals, outer_func)
def gettypefor(self, cls):
return self.gettypeobject(cls.typedef)
More information about the pypy-commit
mailing list