-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Maps, Unrolling, and Dynamic Frame Size #147
base: main
Are you sure you want to change the base?
Conversation
…nch from a script file) - add codeclimate
…ment map transitions for Fixed pointer objects with some inline fields, assume all objects we pass by have a strategy and a class, simplify code and fix tests to honor that"" This reverts commit 78bc8f5.
…the latest strategy in a loop
This reverts commit d134857.
…ays get the latest strategy in a loop" This reverts commit c599ed5.
…rors this exposed in primitives
…POV for squeak is the squeak size
…ldn't happen after we're jitted, but we abort tracing in that case anyway
@@ -127,42 +127,42 @@ | |||
SO_JIT_HOOK_RCVR = 59 # really selectorTrap | |||
|
|||
constant_objects_in_special_object_table = { | |||
"nil": (SO_NIL, "POINTERS"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We use maps for W_FixedPointersObjects that have inline fields. These objects are prebuilt constants - we need to create them to be the right kind of empty PBCs.
w_class=self.getreceiverclass(s_context), | ||
blockmethod=self.getblockmethod(s_context), | ||
s_context=s_context) | ||
if jump_back_pc == old_pc: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This allows us to manually unroll a variable amount of times. At least one time is useful in Squeak, because of the way loops are compiled by the Squeak compiler (see also the jittests for unrolling)
@@ -66,6 +66,8 @@ def _usage(argv): | |||
Execution: | |||
-r|--run <code> - Code will be compiled and executed in | |||
headless mode, result printed. | |||
-rr <code> - Code will be compiled and executed in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a bit of a hack: with the dynamic frame size calculation, we must not JIT a method that we don't know the frame size beforehand, so doits are never jitted. -rr just calls the passed code twice, so the second time we can jit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this is for benchmark purposes? maybe add (used for benchmarks)
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is actually for the jittests, but I'll add a comment
# Do the interrupt-check at the end of a loop, don't | ||
# interrupt loops midway. | ||
self.jitted_check_for_interrupt(s_context) | ||
if not s_context.has_overflow_stack(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When we're in a loop where the initially allocated frame size was smaller than what we actually used in the loop, we'll have an overflow stack. The next time we enter this method the larger frame size will be allocated and no overflow stack will be needed, but we don't want to jit a loop that uses the overflow stack only for the one time we have it.
@@ -79,6 +81,9 @@ def _usage(argv): | |||
synthetic high-prio Process. | |||
-u|--stop-ui - Only with -m or -r. Try to stop UI-process at | |||
startup. Can help benchmarking. | |||
--run-file - Run the .st file supplied as first argument. No |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit of a hackish cmdline argument to support squeak-style invocation ./rsqueak --run-file Image run.st
but side-stepping the entire image startup. This will not work with many files, but with some (like e.g. scripts that just start and then exit, or benchmarking scripts)
@@ -103,8 +103,8 @@ def invariant(self): | |||
|
|||
def class_shadow(self, space): | |||
"""Return internal representation of Squeak class.""" | |||
w_class = jit.promote(self.getclass(space)) | |||
return w_class.as_class_get_shadow(space) | |||
w_class = self.getclass(space) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getclass promotes directly now
w_class = jit.promote(self.getclass(space)) | ||
return w_class.as_class_get_shadow(space) | ||
w_class = self.getclass(space) | ||
return jit.promote(w_class.as_class_get_shadow(space)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to promote this as early as possible, I think, it is used in sends
@@ -273,7 +262,6 @@ class W_AbstractObjectWithClassReference(W_AbstractObjectWithIdentityHash): | |||
"""Objects with arbitrary class (ie not CompiledMethod, SmallInteger or | |||
Float).""" | |||
_attrs_ = ['w_class'] | |||
_immutable_fields_ = ['w_class?'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DSA algorithm uses changeClass: on LargeInteger<->ByteArray, so this really isn't quasi-immutable in that loop. I'm hoping this doesn't hurt too much
@jit.elidable_promote() | ||
def compute_frame_size(self): | ||
def squeak_frame_size(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We now report the frame size as viewed by Squeak differently to the one that we discovered while running (stored in _frame_size
).
assert pc >= 0 and pc < len(self.bytes) | ||
return self.bytes[pc] | ||
try: | ||
return self.bytes[pc] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was getting crashes with some methods, I figured if we go out of bounds, rather than crashing, we can just do a return.
if literals and len(literals) > 1: | ||
w_symbol = literals[-2] | ||
if isinstance(w_symbol, W_BytesObject): | ||
self.lookup_selector = "".join(w_symbol.getbytes()) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Works for at least some methods during fill in
def frame_size(self): | ||
return self._frame_size | ||
|
||
def update_frame_size(self, size): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Frame sizes will only grow. This isn't perfect, because some loops might not require the whole frame, but this way we'll never end up with some assembler that tries to read more frame than what's there. I'm not certain this might be too conservative, though
from rpython.rlib.rstrategies import rstrategies as rstrat | ||
|
||
|
||
class W_PointersObject(W_AbstractObjectWithIdentityHash): | ||
"""Common object.""" | ||
_attrs_ = ['strategy', '_storage'] | ||
# TODO -- is it viable to have these as pseudo-immutable? | ||
# Measurably increases performance, since they do change rarely. | ||
_immutable_attrs_ = ['strategy?', '_storage?'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since we have map transitions and changeClass
in benchmarks, these are not really quasi-immutable. I hope it doesn't hurt performance too much in other places.
|
||
def has_class(self): | ||
return self.getclass(None) is not None | ||
def elidable_strategy(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since strategies are no longer quasi-immutable, we try to make the reads go away anyway if we have a constant self
rsqueakvm/model/pointers.py
Outdated
# getattrs) | ||
_NUMBER_OF_INT_FIELDS = 3 | ||
_NUMBER_OF_INLINE_FIELDS = 2 | ||
class W_FixedPointersObject(W_PointersObject): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right now the number of inline fields here is truly random, i.e., determined by a fair dice roll. Not sure if we want to tweak that or have multiple FixedPointersObjects or what.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe still add your reasoning for the initial values with a comment.
@@ -220,15 +220,16 @@ def func(interp, s_frame, argcount): | |||
w_selector = s_frame.pop() | |||
w_rcvr = s_frame.top() # rcvr is removed in _sendSelector | |||
return s_frame._sendSelector( | |||
w_selector, argcount - 1, interp, w_rcvr, | |||
jit.promote(w_selector), argcount - 1, interp, w_rcvr, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we promote the selector we're performing to elide the lookup
@@ -311,8 +321,7 @@ def store_stackpointer(self, size): | |||
assert size >= 0, "trying to store negative stackpointer" | |||
self.pop_n(depth - size) | |||
else: | |||
for i in range(depth, size): | |||
self.push(self.space.w_nil) | |||
self.store_stack_ptr(size) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's already something on the stack (probably nil), it's fine to leak stuff also, there is not guarantee that the stack is nil-ed
stacksize = self.stacksize() # no temps | ||
return stacksize | ||
|
||
def init_temps_and_stack(self): | ||
self = fresh_virtualizable(self) | ||
stacksize = self.full_stacksize() | ||
self._temps_and_stack = [None] * stacksize | ||
self._temps_and_stack = [self.space.w_nil] * stacksize |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With the dynamic frame size, we're probably only allocating as much as necessary anyway, so this helps us again remove the guard in stack_get
to check for None
return False | ||
|
||
@jit.unroll_safe | ||
def update_stacksize(self, index0): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We cache on the compiled method how large the largest frame was that we needed for this method
""") | ||
|
||
@py.test.mark.skipif("'Not ready'") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
😀
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First pass, still have to look at the new Map classes...
rsqueakvm/interpreter.py
Outdated
_immutable_fields_ = ["max_squeak_unroll_count", "squeak_unroll_trace_limit"] | ||
def __init__(self): | ||
self.max_squeak_unroll_count = 2 | ||
self.squeak_unroll_trace_limit = 32000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add a comment to explain the two values?
@@ -66,6 +66,8 @@ def _usage(argv): | |||
Execution: | |||
-r|--run <code> - Code will be compiled and executed in | |||
headless mode, result printed. | |||
-rr <code> - Code will be compiled and executed in |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm assuming this is for benchmark purposes? maybe add (used for benchmarks)
.
rsqueakvm/model/compiled_methods.py
Outdated
j = 1 | ||
for i in self.bytes: | ||
j = 0 | ||
while j < len(self.bytes): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why the while
loop? for j in range(len(self.bytes)):
?
rsqueakvm/model/compiled_methods.py
Outdated
@@ -380,26 +408,32 @@ def as_string(self, markBytecode=0): | |||
|
|||
def guess_containing_classname(self): | |||
w_class = self.compiled_in() | |||
if w_class and w_class.has_space(): | |||
if w_class: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe start explicitly with if w_class is None:
and switch the return statements.
rsqueakvm/model/pointers.py
Outdated
# getattrs) | ||
_NUMBER_OF_INT_FIELDS = 3 | ||
_NUMBER_OF_INLINE_FIELDS = 2 | ||
class W_FixedPointersObject(W_PointersObject): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe still add your reasoning for the initial values with a comment.
@@ -97,7 +97,7 @@ def func(interp, s_frame, argcount, w_method): | |||
|
|||
@expose_primitive(SNAPSHOT, clean_stack=False, no_result=True) | |||
def func(interp, s_frame, argcount): | |||
s_frame.pop_n(argcount) | |||
s_frame.pop_n(argcount + 1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just checked, it's the only occurrence of pop_n(argcount)
in rsqueakvm.*
.
@@ -356,6 +356,11 @@ def get_bytes_of(self, chunk): | |||
bytes.append(chr((each >> 24) & 0xff)) | |||
return bytes | |||
|
|||
def isfixed(self, g_object): | |||
return (self.space.use_maps.is_set() and | |||
g_object.format == 1 and |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have any consts for the different formats?
rsqueakvm/storage_contexts.py
Outdated
self.extra_data = ExtraContextAttributes() | ||
return self.extra_data | ||
extra_data = self.extra_data_or_blockmethod | ||
if extra_data is None or not isinstance(extra_data, ExtraContextAttributes): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put the easier case first: if isinstance(extra_data, ExtraContextAttributes): return...
rsqueakvm/storage_contexts.py
Outdated
w_bm = self.extra_data_or_blockmethod | ||
if isinstance(w_bm, ExtraContextAttributes): | ||
w_bm = w_bm.blockmethod | ||
if w_bm is None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if isinstance(w_bm, W_CompiledMethod): return w_bm
, otherwise return None
. -> avoids assert
.
setfield_gc(ConstPtr(ptr83), i82, descr=<FieldS rsqueakvm.interpreter.Interpreter.inst_interrupt_check_counter 24>) | ||
i85 = int_le(i82, 0) | ||
guard_false(i85, descr=<Guard0x4a52360>) | ||
jump(p0, p1, i2, p4, p6, p7, p9, p12, p14, i80, p22, p24, p26, p28, p30, p32, p34, p36, p38, p40, p42, p47, i82, descr=TargetToken(83088416)) | ||
""") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can I haz more traces to read? 😄
…/dynamic_frame_size
…er for annotating rvmprof
There is a lot of stuff that has accumulated in this branch. I'm adding comments to explain the different bits. Reviews appreciated.