Debian Codegraph @

rpython/doc/jit/virtualizable.rst

raw
1
2 Virtualizables
3 ==============
4
5 **Note:** this document does not have a proper introduction as to how
6 to understand the basics. We should write some. If you happen to be here
7 and you're missing context, feel free to pester us on IRC.
8
9 Problem description
10 -------------------
11
12 The JIT is very good at making sure some objects are never allocated if they
13 don't escape from the trace. Such objects are called ``virtuals``. However,
14 if we're dealing with frames, virtuals are often not good enough. Frames
15 can escape and they can also be allocated already at the moment we enter the
16 JIT. In such cases we need some extra object that can still be optimized away,
17 despite existing on the heap.
18
19 Solution
20 --------
21
22 We introduce virtualizables. They're objects that exist on the heap, but their
23 fields are not always in sync with whatever happens in the assembler. One
24 example is that virtualizable fields can store virtual objects without
25 forcing them. This is very useful for frames. Declaring an object to be
26 virtualizable works like this:
27
28 .. code-block:: python
29
30 class Frame(object):
31 _virtualizable_ = ['locals[*]', 'stackdepth']
32
33
34 And we use them in ``JitDriver`` like this:
35
36 .. code-block:: python
37
38 jitdriver = JitDriver(greens=[], reds=['frame'], virtualizables=['frame'])
39
40
41 This declaration means that ``stackdepth`` is a virtualizable **field**, while
42 ``locals`` is a virtualizable **array** (a list stored on a virtualizable).
43 There are various rules about using virtualizables, especially using
44 virtualizable arrays that can be very confusing. Those will usually end
45 up with a compile-time error (as opposed to strange behavior). The rules are:
46
47 * A virtualizable array must be a fixed-size list. After it is
48 initialized (e.g. in ``Frame.__init__``) you cannot resize it at all.
49 You cannot assign a different list to the field, or even pass around the
50 list. You can only access ``frame.array[index]`` directly.
51
52 * Each array access must be with a known positive index that cannot raise
53 an ``IndexError``.
54 Using ``index = jit.hint(index, promote=True)`` might be useful
55 to get a constant-number access. This is only safe if the index is actually
56 constant or changing rarely within the context of the user's code.
57
58 * If you initialize a new virtualizable in the JIT, it has to be done like this
59 (for example if we're in ``Frame.__init__``):
60
61 .. code-block:: python
62
63 self = hint(self, access_directly=True, fresh_virtualizable=True)
64
65 that way you can populate the fields directly.
66
67 * If you use virtualizable outside of the JIT – it's very expensive and
68 sometimes aborts tracing. Consider it carefully as to how do it only for
69 debugging purposes and not every time (e.g. ``sys._getframe`` call).
70
71 * If you have something equivalent of a Python generator, where the
72 virtualizable survives for longer, you want to force it before returning.
73 It's better to do it that way than by an external call some time later.
74 It's done using ``jit.hint(frame, force_virtualizable=True)``
75
76 * Your interpreter should have a local variable similar to ``frame``
77 above. It must not be modified as long as it runs its
78 ``jit_merge_point`` loop, and in the loop it must be passed directly
79 to the ``jit_merge_point()`` and ``can_enter_jit()`` calls. The JIT
80 generator is known to produce buggy code if you fetch the
81 virtualizable from somewhere every iteration, instead of reusing the
82 same unmodified local variable.