Debian Codegraph @

rpython/jit/metainterp/virtualref.py

raw symbols SCIP
1 from rpython.rtyper.rmodel import inputconst, log
2 from rpython.rtyper.lltypesystem import lltype, llmemory
3 from rpython.rtyper import rclass
4 from rpython.jit.metainterp import history
5 from rpython.jit.metainterp.support import ptr2int
6 from rpython.jit.metainterp.virtualizable import (
7 TOKEN_NONE, TOKEN_TRACING_RESCALL)
8 from rpython.jit.codewriter import heaptracker
9 from rpython.rlib.jit import InvalidVirtualRef
10
11 class VirtualRefInfo:
12
13 def __init__(self, warmrunnerdesc):
14 self.warmrunnerdesc = warmrunnerdesc
15 self.cpu = warmrunnerdesc.cpu
16 # we make the low-level type of an RPython class directly
17 self.JIT_VIRTUAL_REF = lltype.GcStruct('JitVirtualRef',
18 ('super', rclass.OBJECT),
19 ('virtual_token', llmemory.GCREF),
20 ('forced', rclass.OBJECTPTR))
21 self.jit_virtual_ref_vtable = lltype.malloc(rclass.OBJECT_VTABLE,
22 zero=True, flavor='raw',
23 immortal=True)
24 if hasattr(self.cpu, 'gc_ll_descr'):
25 heaptracker.setup_cache_gcstruct2vtable(self.cpu.gc_ll_descr)
26 self.cpu.gc_ll_descr._cache_gcstruct2vtable[self.JIT_VIRTUAL_REF] = self.jit_virtual_ref_vtable
27 #
28 # record the type JIT_VIRTUAL_REF explicitly in the rtyper, too
29 if hasattr(self.warmrunnerdesc, 'rtyper'): # <-- for tests
30 self.warmrunnerdesc.rtyper.set_type_for_typeptr(
31 self.jit_virtual_ref_vtable, self.JIT_VIRTUAL_REF)
32 self.descr = self.cpu.sizeof(self.JIT_VIRTUAL_REF,
33 vtable=self.jit_virtual_ref_vtable)
34 self.jit_virtual_ref_vtable.name = rclass.alloc_array_name(
35 'jit_virtual_ref')
36 # build some constants
37 adr = ptr2int(self.jit_virtual_ref_vtable)
38 self.jit_virtual_ref_const_class = history.ConstInt(adr)
39 fielddescrof = self.cpu.fielddescrof
40 self.descr_virtual_token = fielddescrof(self.JIT_VIRTUAL_REF,
41 'virtual_token')
42 self.descr_forced = fielddescrof(self.JIT_VIRTUAL_REF, 'forced')
43
44 def _freeze_(self):
45 return True
46
47 def replace_force_virtual_with_call(self, graphs):
48 # similar to rvirtualizable.replace_force_virtualizable_with_call().
49 c_force_virtual_ptr = None
50 c_is_virtual_ptr = None
51 force_virtual_count = 0
52 for graph in graphs:
53 for block in graph.iterblocks():
54 for op in block.operations:
55 if op.opname == 'jit_force_virtual':
56 # first compute c_funcptr, but only if there is any
57 # 'jit_force_virtual' around
58 if c_force_virtual_ptr is None:
59 c_force_virtual_ptr = self.get_force_virtual_fnptr()
60 #
61 op.opname = 'direct_call'
62 op.args = [c_force_virtual_ptr, op.args[0]]
63 force_virtual_count += 1
64 #
65 if op.opname == 'jit_is_virtual':
66 if c_is_virtual_ptr is None:
67 c_is_virtual_ptr = self.get_is_virtual_fnptr()
68 #
69 op.opname = 'direct_call'
70 op.args = [c_is_virtual_ptr, op.args[0]]
71 #
72 if c_force_virtual_ptr is not None:
73 log("replaced %d 'jit_force_virtual' with %r" % (force_virtual_count,
74 c_force_virtual_ptr.value))
75
76 # ____________________________________________________________
77
78 # The 'virtual_token' field has the same meaning as the 'vable_token' field
79 # of a virtualizable. It is equal to:
80 # * TOKEN_NONE when tracing, except as described below;
81 # * TOKEN_TRACING_RESCALL during tracing when we do a residual call;
82 # * the JITFRAME (set by FORCE_TOKEN) when running the assembler;
83 # * TOKEN_NONE after the virtual is forced, if it is forced at all.
84
85 def virtual_ref_during_tracing(self, real_object):
86 assert real_object
87 vref = lltype.malloc(self.JIT_VIRTUAL_REF)
88 p = lltype.cast_pointer(rclass.OBJECTPTR, vref)
89 p.typeptr = self.jit_virtual_ref_vtable
90 vref.virtual_token = TOKEN_NONE
91 vref.forced = lltype.cast_opaque_ptr(rclass.OBJECTPTR, real_object)
92 return lltype.cast_opaque_ptr(llmemory.GCREF, vref)
93
94 def is_virtual_ref(self, gcref):
95 if not gcref:
96 return False
97 inst = lltype.cast_opaque_ptr(rclass.OBJECTPTR, gcref)
98 return inst.typeptr == self.jit_virtual_ref_vtable
99
100 def tracing_before_residual_call(self, gcref):
101 if not self.is_virtual_ref(gcref):
102 return
103 vref = lltype.cast_opaque_ptr(lltype.Ptr(self.JIT_VIRTUAL_REF), gcref)
104 assert vref.virtual_token == TOKEN_NONE
105 vref.virtual_token = TOKEN_TRACING_RESCALL
106
107 def tracing_after_residual_call(self, gcref):
108 if not self.is_virtual_ref(gcref):
109 return False
110 vref = lltype.cast_opaque_ptr(lltype.Ptr(self.JIT_VIRTUAL_REF), gcref)
111 assert vref.forced
112 if vref.virtual_token != TOKEN_NONE:
113 # not modified by the residual call; assert that it is still
114 # set to TOKEN_TRACING_RESCALL and clear it.
115 assert vref.virtual_token == TOKEN_TRACING_RESCALL
116 vref.virtual_token = TOKEN_NONE
117 return False
118 else:
119 # marker "modified during residual call" set.
120 return True
121
122 def continue_tracing(self, gcref, real_object):
123 if not self.is_virtual_ref(gcref):
124 return
125 assert real_object
126 vref = lltype.cast_opaque_ptr(lltype.Ptr(self.JIT_VIRTUAL_REF), gcref)
127 assert vref.virtual_token != TOKEN_TRACING_RESCALL
128 vref.virtual_token = TOKEN_NONE
129 vref.forced = lltype.cast_opaque_ptr(rclass.OBJECTPTR, real_object)
130
131 # ____________________________________________________________
132
133 def get_force_virtual_fnptr(self):
134 #
135 def force_virtual_if_necessary(inst):
136 if not inst or inst.typeptr != self.jit_virtual_ref_vtable:
137 return inst # common, fast case
138 return self.force_virtual(inst)
139 #
140 FUNC = lltype.FuncType([rclass.OBJECTPTR], rclass.OBJECTPTR)
141 funcptr = self.warmrunnerdesc.helper_func(
142 lltype.Ptr(FUNC),
143 force_virtual_if_necessary)
144 return inputconst(lltype.typeOf(funcptr), funcptr)
145
146 def get_is_virtual_fnptr(self):
147 #
148 def is_virtual(inst):
149 if not inst:
150 return False
151 return inst.typeptr == self.jit_virtual_ref_vtable
152 #
153 FUNC = lltype.FuncType([rclass.OBJECTPTR], lltype.Bool)
154 funcptr = self.warmrunnerdesc.helper_func(lltype.Ptr(FUNC), is_virtual)
155 return inputconst(lltype.typeOf(funcptr), funcptr)
156
157 def force_virtual(self, inst):
158 vref = lltype.cast_pointer(lltype.Ptr(self.JIT_VIRTUAL_REF), inst)
159 token = vref.virtual_token
160 if token != TOKEN_NONE:
161 if token == TOKEN_TRACING_RESCALL:
162 # The "virtual" is not a virtual at all during tracing.
163 # We only need to reset virtual_token to TOKEN_NONE
164 # as a marker for the tracing, to tell it that this
165 # "virtual" escapes.
166 assert vref.forced
167 vref.virtual_token = TOKEN_NONE
168 else:
169 assert not vref.forced
170 from rpython.jit.metainterp.compile import ResumeGuardForcedDescr
171 ResumeGuardForcedDescr.force_now(self.cpu, token)
172 assert vref.virtual_token == TOKEN_NONE
173 assert vref.forced
174 elif not vref.forced:
175 # token == TOKEN_NONE and the vref was not forced: it's invalid
176 raise InvalidVirtualRef
177 return vref.forced