PageRenderTime 45ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/pypy/rlib/jit.py

https://bitbucket.org/pypy/pypy/
Python | 873 lines | 868 code | 5 blank | 0 comment | 1 complexity | 316dc28c59e4066838080d876d6ccfa6 MD5 | raw file
Possible License(s): AGPL-3.0, BSD-3-Clause, Apache-2.0
  1. import sys
  2. import py
  3. from pypy.rlib.nonconst import NonConstant
  4. from pypy.rlib.objectmodel import CDefinedIntSymbolic, keepalive_until_here, specialize
  5. from pypy.rlib.unroll import unrolling_iterable
  6. from pypy.rpython.extregistry import ExtRegistryEntry
  7. DEBUG_ELIDABLE_FUNCTIONS = False
  8. def elidable(func):
  9. """ Decorate a function as "trace-elidable". Usually this means simply that
  10. the function is constant-foldable, i.e. is pure and has no side-effects.
  11. In some situations it is ok to use this decorator if the function *has*
  12. side effects, as long as these side-effects are idempotent. A typical
  13. example for this would be a cache.
  14. To be totally precise:
  15. (1) the result of the call should not change if the arguments are
  16. the same (same numbers or same pointers)
  17. (2) it's fine to remove the call completely if we can guess the result
  18. according to rule 1
  19. (3) the function call can be moved around by optimizer,
  20. but only so it'll be called earlier and not later.
  21. Most importantly it doesn't mean that an elidable function has no observable
  22. side effect, but those side effects are idempotent (ie caching).
  23. If a particular call to this function ends up raising an exception, then it
  24. is handled like a normal function call (this decorator is ignored).
  25. """
  26. if DEBUG_ELIDABLE_FUNCTIONS:
  27. cache = {}
  28. oldfunc = func
  29. def func(*args):
  30. result = oldfunc(*args) # if it raises, no caching
  31. try:
  32. oldresult = cache.setdefault(args, result)
  33. except TypeError:
  34. pass # unhashable args
  35. else:
  36. assert oldresult == result
  37. return result
  38. func._elidable_function_ = True
  39. return func
  40. def purefunction(*args, **kwargs):
  41. import warnings
  42. warnings.warn("purefunction is deprecated, use elidable instead", DeprecationWarning)
  43. return elidable(*args, **kwargs)
  44. def hint(x, **kwds):
  45. """ Hint for the JIT
  46. possible arguments are:
  47. * promote - promote the argument from a variable into a constant
  48. * promote_string - same, but promote string by *value*
  49. * access_directly - directly access a virtualizable, as a structure
  50. and don't treat it as a virtualizable
  51. * fresh_virtualizable - means that virtualizable was just allocated.
  52. Useful in say Frame.__init__ when we do want
  53. to store things directly on it. Has to come with
  54. access_directly=True
  55. """
  56. return x
  57. @specialize.argtype(0)
  58. def promote(x):
  59. return hint(x, promote=True)
  60. def promote_string(x):
  61. return hint(x, promote_string=True)
  62. def dont_look_inside(func):
  63. """ Make sure the JIT does not trace inside decorated function
  64. (it becomes a call instead)
  65. """
  66. func._jit_look_inside_ = False
  67. return func
  68. def unroll_safe(func):
  69. """ JIT can safely unroll loops in this function and this will
  70. not lead to code explosion
  71. """
  72. func._jit_unroll_safe_ = True
  73. return func
  74. def loop_invariant(func):
  75. """ Describes a function with no argument that returns an object that
  76. is always the same in a loop.
  77. Use it only if you know what you're doing.
  78. """
  79. dont_look_inside(func)
  80. func._jit_loop_invariant_ = True
  81. return func
  82. def _get_args(func):
  83. import inspect
  84. args, varargs, varkw, defaults = inspect.getargspec(func)
  85. args = ["v%s" % (i, ) for i in range(len(args))]
  86. assert varargs is None and varkw is None
  87. assert not defaults
  88. return args
  89. def elidable_promote(promote_args='all'):
  90. """ A decorator that promotes all arguments and then calls the supplied
  91. function
  92. """
  93. def decorator(func):
  94. elidable(func)
  95. args = _get_args(func)
  96. argstring = ", ".join(args)
  97. code = ["def f(%s):\n" % (argstring, )]
  98. if promote_args != 'all':
  99. args = [('v%d' % int(i)) for i in promote_args.split(",")]
  100. for arg in args:
  101. code.append(" %s = hint(%s, promote=True)\n" % (arg, arg))
  102. code.append(" return func(%s)\n" % (argstring, ))
  103. d = {"func": func, "hint": hint}
  104. exec py.code.Source("\n".join(code)).compile() in d
  105. result = d["f"]
  106. result.func_name = func.func_name + "_promote"
  107. return result
  108. return decorator
  109. def purefunction_promote(*args, **kwargs):
  110. import warnings
  111. warnings.warn("purefunction_promote is deprecated, use elidable_promote instead", DeprecationWarning)
  112. return elidable_promote(*args, **kwargs)
  113. def look_inside_iff(predicate):
  114. """
  115. look inside (including unrolling loops) the target function, if and only if
  116. predicate(*args) returns True
  117. """
  118. def inner(func):
  119. func = unroll_safe(func)
  120. # When we return the new function, it might be specialized in some
  121. # way. We "propogate" this specialization by using
  122. # specialize:call_location on relevant functions.
  123. for thing in [func, predicate]:
  124. thing._annspecialcase_ = "specialize:call_location"
  125. args = _get_args(func)
  126. d = {
  127. "dont_look_inside": dont_look_inside,
  128. "predicate": predicate,
  129. "func": func,
  130. "we_are_jitted": we_are_jitted,
  131. }
  132. exec py.code.Source("""
  133. @dont_look_inside
  134. def trampoline(%(arguments)s):
  135. return func(%(arguments)s)
  136. if hasattr(func, "oopspec"):
  137. # XXX: This seems like it should be here, but it causes errors.
  138. # trampoline.oopspec = func.oopspec
  139. del func.oopspec
  140. trampoline.__name__ = func.__name__ + "_trampoline"
  141. trampoline._annspecialcase_ = "specialize:call_location"
  142. def f(%(arguments)s):
  143. if not we_are_jitted() or predicate(%(arguments)s):
  144. return func(%(arguments)s)
  145. else:
  146. return trampoline(%(arguments)s)
  147. f.__name__ = func.__name__ + "_look_inside_iff"
  148. """ % {"arguments": ", ".join(args)}).compile() in d
  149. return d["f"]
  150. return inner
  151. def oopspec(spec):
  152. def decorator(func):
  153. func.oopspec = spec
  154. return func
  155. return decorator
  156. @oopspec("jit.isconstant(value)")
  157. def isconstant(value):
  158. """
  159. While tracing, returns whether or not the value is currently known to be
  160. constant. This is not perfect, values can become constant later. Mostly for
  161. use with @look_inside_iff.
  162. This is for advanced usage only.
  163. """
  164. return NonConstant(False)
  165. isconstant._annspecialcase_ = "specialize:call_location"
  166. @oopspec("jit.isvirtual(value)")
  167. def isvirtual(value):
  168. """
  169. Returns if this value is virtual, while tracing, it's relatively
  170. conservative and will miss some cases.
  171. This is for advanced usage only.
  172. """
  173. return NonConstant(False)
  174. isvirtual._annspecialcase_ = "specialize:call_location"
  175. class Entry(ExtRegistryEntry):
  176. _about_ = hint
  177. def compute_result_annotation(self, s_x, **kwds_s):
  178. from pypy.annotation import model as annmodel
  179. s_x = annmodel.not_const(s_x)
  180. access_directly = 's_access_directly' in kwds_s
  181. fresh_virtualizable = 's_fresh_virtualizable' in kwds_s
  182. if access_directly or fresh_virtualizable:
  183. assert access_directly, "lone fresh_virtualizable hint"
  184. if isinstance(s_x, annmodel.SomeInstance):
  185. from pypy.objspace.flow.model import Constant
  186. classdesc = s_x.classdef.classdesc
  187. virtualizable = classdesc.read_attribute('_virtualizable2_',
  188. Constant(None)).value
  189. if virtualizable is not None:
  190. flags = s_x.flags.copy()
  191. flags['access_directly'] = True
  192. if fresh_virtualizable:
  193. flags['fresh_virtualizable'] = True
  194. s_x = annmodel.SomeInstance(s_x.classdef,
  195. s_x.can_be_None,
  196. flags)
  197. return s_x
  198. def specialize_call(self, hop, **kwds_i):
  199. from pypy.rpython.lltypesystem import lltype
  200. hints = {}
  201. for key, index in kwds_i.items():
  202. s_value = hop.args_s[index]
  203. if not s_value.is_constant():
  204. from pypy.rpython.error import TyperError
  205. raise TyperError("hint %r is not constant" % (key,))
  206. assert key.startswith('i_')
  207. hints[key[2:]] = s_value.const
  208. v = hop.inputarg(hop.args_r[0], arg=0)
  209. c_hint = hop.inputconst(lltype.Void, hints)
  210. hop.exception_cannot_occur()
  211. return hop.genop('hint', [v, c_hint], resulttype=v.concretetype)
  212. def we_are_jitted():
  213. """ Considered as true during tracing and blackholing,
  214. so its consquences are reflected into jitted code """
  215. return False
  216. _we_are_jitted = CDefinedIntSymbolic('0 /* we are not jitted here */',
  217. default=0)
  218. class Entry(ExtRegistryEntry):
  219. _about_ = we_are_jitted
  220. def compute_result_annotation(self):
  221. from pypy.annotation import model as annmodel
  222. return annmodel.SomeInteger(nonneg=True)
  223. def specialize_call(self, hop):
  224. from pypy.rpython.lltypesystem import lltype
  225. hop.exception_cannot_occur()
  226. return hop.inputconst(lltype.Signed, _we_are_jitted)
  227. def current_trace_length():
  228. """During JIT tracing, returns the current trace length (as a constant).
  229. If not tracing, returns -1."""
  230. if NonConstant(False):
  231. return 73
  232. return -1
  233. current_trace_length.oopspec = 'jit.current_trace_length()'
  234. def jit_debug(string, arg1=-sys.maxint-1, arg2=-sys.maxint-1,
  235. arg3=-sys.maxint-1, arg4=-sys.maxint-1):
  236. """When JITted, cause an extra operation JIT_DEBUG to appear in
  237. the graphs. Should not be left after debugging."""
  238. keepalive_until_here(string) # otherwise the whole function call is removed
  239. jit_debug.oopspec = 'jit.debug(string, arg1, arg2, arg3, arg4)'
  240. def assert_green(value):
  241. """Very strong assert: checks that 'value' is a green
  242. (a JIT compile-time constant)."""
  243. keepalive_until_here(value)
  244. assert_green._annspecialcase_ = 'specialize:argtype(0)'
  245. assert_green.oopspec = 'jit.assert_green(value)'
  246. class AssertGreenFailed(Exception):
  247. pass
  248. # ____________________________________________________________
  249. # VRefs
  250. def virtual_ref(x):
  251. """Creates a 'vref' object that contains a reference to 'x'. Calls
  252. to virtual_ref/virtual_ref_finish must be properly nested. The idea
  253. is that the object 'x' is supposed to be JITted as a virtual between
  254. the calls to virtual_ref and virtual_ref_finish, but the 'vref'
  255. object can escape at any point in time. If at runtime it is
  256. dereferenced (by the call syntax 'vref()'), it returns 'x', which is
  257. then forced."""
  258. return DirectJitVRef(x)
  259. virtual_ref.oopspec = 'virtual_ref(x)'
  260. def virtual_ref_finish(vref, x):
  261. """See docstring in virtual_ref(x)"""
  262. keepalive_until_here(x) # otherwise the whole function call is removed
  263. _virtual_ref_finish(vref, x)
  264. virtual_ref_finish.oopspec = 'virtual_ref_finish(x)'
  265. def non_virtual_ref(x):
  266. """Creates a 'vref' that just returns x when called; nothing more special.
  267. Used for None or for frames outside JIT scope."""
  268. return DirectVRef(x)
  269. class InvalidVirtualRef(Exception):
  270. """
  271. Raised if we try to call a non-forced virtualref after the call to
  272. virtual_ref_finish
  273. """
  274. # ---------- implementation-specific ----------
  275. class DirectVRef(object):
  276. def __init__(self, x):
  277. self._x = x
  278. self._state = 'non-forced'
  279. def __call__(self):
  280. if self._state == 'non-forced':
  281. self._state = 'forced'
  282. elif self._state == 'invalid':
  283. raise InvalidVirtualRef
  284. return self._x
  285. @property
  286. def virtual(self):
  287. """A property that is True if the vref contains a virtual that would
  288. be forced by the '()' operator."""
  289. return self._state == 'non-forced'
  290. def _finish(self):
  291. if self._state == 'non-forced':
  292. self._state = 'invalid'
  293. class DirectJitVRef(DirectVRef):
  294. def __init__(self, x):
  295. assert x is not None, "virtual_ref(None) is not allowed"
  296. DirectVRef.__init__(self, x)
  297. def _virtual_ref_finish(vref, x):
  298. assert vref._x is x, "Invalid call to virtual_ref_finish"
  299. vref._finish()
  300. class Entry(ExtRegistryEntry):
  301. _about_ = (non_virtual_ref, DirectJitVRef)
  302. def compute_result_annotation(self, s_obj):
  303. from pypy.rlib import _jit_vref
  304. return _jit_vref.SomeVRef(s_obj)
  305. def specialize_call(self, hop):
  306. return hop.r_result.specialize_call(hop)
  307. class Entry(ExtRegistryEntry):
  308. _type_ = DirectVRef
  309. def compute_annotation(self):
  310. from pypy.rlib import _jit_vref
  311. assert isinstance(self.instance, DirectVRef)
  312. s_obj = self.bookkeeper.immutablevalue(self.instance())
  313. return _jit_vref.SomeVRef(s_obj)
  314. class Entry(ExtRegistryEntry):
  315. _about_ = _virtual_ref_finish
  316. def compute_result_annotation(self, s_vref, s_obj):
  317. pass
  318. def specialize_call(self, hop):
  319. pass
  320. vref_None = non_virtual_ref(None)
  321. # ____________________________________________________________
  322. # User interface for the warmspot JIT policy
  323. class JitHintError(Exception):
  324. """Inconsistency in the JIT hints."""
  325. PARAMETER_DOCS = {
  326. 'threshold': 'number of times a loop has to run for it to become hot',
  327. 'function_threshold': 'number of times a function must run for it to become traced from start',
  328. 'trace_eagerness': 'number of times a guard has to fail before we start compiling a bridge',
  329. 'trace_limit': 'number of recorded operations before we abort tracing with ABORT_TOO_LONG',
  330. 'inlining': 'inline python functions or not (1/0)',
  331. 'loop_longevity': 'a parameter controlling how long loops will be kept before being freed, an estimate',
  332. 'retrace_limit': 'how many times we can try retracing before giving up',
  333. 'max_retrace_guards': 'number of extra guards a retrace can cause',
  334. 'max_unroll_loops': 'number of extra unrollings a loop can cause',
  335. 'enable_opts': 'optimizations to enable or all, INTERNAL USE ONLY'
  336. }
  337. PARAMETERS = {'threshold': 1039, # just above 1024, prime
  338. 'function_threshold': 1619, # slightly more than one above, also prime
  339. 'trace_eagerness': 200,
  340. 'trace_limit': 6000,
  341. 'inlining': 1,
  342. 'loop_longevity': 1000,
  343. 'retrace_limit': 5,
  344. 'max_retrace_guards': 15,
  345. 'max_unroll_loops': 4,
  346. 'enable_opts': 'all',
  347. }
  348. unroll_parameters = unrolling_iterable(PARAMETERS.items())
  349. DEFAULT = object()
  350. # ____________________________________________________________
  351. class JitDriver(object):
  352. """Base class to declare fine-grained user control on the JIT. So
  353. far, there must be a singleton instance of JitDriver. This style
  354. will allow us (later) to support a single RPython program with
  355. several independent JITting interpreters in it.
  356. """
  357. active = True # if set to False, this JitDriver is ignored
  358. virtualizables = []
  359. name = 'jitdriver'
  360. def __init__(self, greens=None, reds=None, virtualizables=None,
  361. get_jitcell_at=None, set_jitcell_at=None,
  362. get_printable_location=None, confirm_enter_jit=None,
  363. can_never_inline=None, should_unroll_one_iteration=None,
  364. name='jitdriver'):
  365. if greens is not None:
  366. self.greens = greens
  367. self.name = name
  368. if reds is not None:
  369. self.reds = reds
  370. if not hasattr(self, 'greens') or not hasattr(self, 'reds'):
  371. raise AttributeError("no 'greens' or 'reds' supplied")
  372. if virtualizables is not None:
  373. self.virtualizables = virtualizables
  374. for v in self.virtualizables:
  375. assert v in self.reds
  376. self._alllivevars = dict.fromkeys(
  377. [name for name in self.greens + self.reds if '.' not in name])
  378. self._make_extregistryentries()
  379. self.get_jitcell_at = get_jitcell_at
  380. self.set_jitcell_at = set_jitcell_at
  381. self.get_printable_location = get_printable_location
  382. self.confirm_enter_jit = confirm_enter_jit
  383. self.can_never_inline = can_never_inline
  384. self.should_unroll_one_iteration = should_unroll_one_iteration
  385. def _freeze_(self):
  386. return True
  387. def jit_merge_point(_self, **livevars):
  388. # special-cased by ExtRegistryEntry
  389. assert dict.fromkeys(livevars) == _self._alllivevars
  390. def can_enter_jit(_self, **livevars):
  391. # special-cased by ExtRegistryEntry
  392. assert dict.fromkeys(livevars) == _self._alllivevars
  393. def loop_header(self):
  394. # special-cased by ExtRegistryEntry
  395. pass
  396. def _make_extregistryentries(self):
  397. # workaround: we cannot declare ExtRegistryEntries for functions
  398. # used as methods of a frozen object, but we can attach the
  399. # bound methods back to 'self' and make ExtRegistryEntries
  400. # specifically for them.
  401. self.jit_merge_point = self.jit_merge_point
  402. self.can_enter_jit = self.can_enter_jit
  403. self.loop_header = self.loop_header
  404. class Entry(ExtEnterLeaveMarker):
  405. _about_ = (self.jit_merge_point, self.can_enter_jit)
  406. class Entry(ExtLoopHeader):
  407. _about_ = self.loop_header
  408. def _set_param(driver, name, value):
  409. # special-cased by ExtRegistryEntry
  410. # (internal, must receive a constant 'name')
  411. # if value is DEFAULT, sets the default value.
  412. assert name in PARAMETERS
  413. @specialize.arg(0, 1)
  414. def set_param(driver, name, value):
  415. """Set one of the tunable JIT parameter. Driver can be None, then all
  416. drivers have this set """
  417. _set_param(driver, name, value)
  418. @specialize.arg(0, 1)
  419. def set_param_to_default(driver, name):
  420. """Reset one of the tunable JIT parameters to its default value."""
  421. _set_param(driver, name, DEFAULT)
  422. def set_user_param(driver, text):
  423. """Set the tunable JIT parameters from a user-supplied string
  424. following the format 'param=value,param=value', or 'off' to
  425. disable the JIT. For programmatic setting of parameters, use
  426. directly JitDriver.set_param().
  427. """
  428. if text == 'off':
  429. set_param(driver, 'threshold', -1)
  430. set_param(driver, 'function_threshold', -1)
  431. return
  432. if text == 'default':
  433. for name1, _ in unroll_parameters:
  434. set_param_to_default(driver, name1)
  435. return
  436. for s in text.split(','):
  437. s = s.strip(' ')
  438. parts = s.split('=')
  439. if len(parts) != 2:
  440. raise ValueError
  441. name = parts[0]
  442. value = parts[1]
  443. if name == 'enable_opts':
  444. set_param(driver, 'enable_opts', value)
  445. else:
  446. for name1, _ in unroll_parameters:
  447. if name1 == name and name1 != 'enable_opts':
  448. try:
  449. set_param(driver, name1, int(value))
  450. except ValueError:
  451. raise
  452. break
  453. else:
  454. raise ValueError
  455. set_user_param._annspecialcase_ = 'specialize:arg(0)'
  456. # ____________________________________________________________
  457. #
  458. # Annotation and rtyping of some of the JitDriver methods
  459. class BaseJitCell(object):
  460. __slots__ = ()
  461. class ExtEnterLeaveMarker(ExtRegistryEntry):
  462. # Replace a call to myjitdriver.jit_merge_point(**livevars)
  463. # with an operation jit_marker('jit_merge_point', myjitdriver, livevars...)
  464. # Also works with can_enter_jit.
  465. def compute_result_annotation(self, **kwds_s):
  466. from pypy.annotation import model as annmodel
  467. if self.instance.__name__ == 'jit_merge_point':
  468. self.annotate_hooks(**kwds_s)
  469. driver = self.instance.im_self
  470. keys = kwds_s.keys()
  471. keys.sort()
  472. expected = ['s_' + name for name in driver.greens + driver.reds
  473. if '.' not in name]
  474. expected.sort()
  475. if keys != expected:
  476. raise JitHintError("%s expects the following keyword "
  477. "arguments: %s" % (self.instance,
  478. expected))
  479. try:
  480. cache = self.bookkeeper._jit_annotation_cache[driver]
  481. except AttributeError:
  482. cache = {}
  483. self.bookkeeper._jit_annotation_cache = {driver: cache}
  484. except KeyError:
  485. cache = {}
  486. self.bookkeeper._jit_annotation_cache[driver] = cache
  487. for key, s_value in kwds_s.items():
  488. s_previous = cache.get(key, annmodel.s_ImpossibleValue)
  489. s_value = annmodel.unionof(s_previous, s_value)
  490. if annmodel.isdegenerated(s_value):
  491. raise JitHintError("mixing incompatible types in argument %s"
  492. " of jit_merge_point/can_enter_jit" %
  493. key[2:])
  494. cache[key] = s_value
  495. # add the attribute _dont_reach_me_in_del_ (see pypy.rpython.rclass)
  496. try:
  497. graph = self.bookkeeper.position_key[0]
  498. graph.func._dont_reach_me_in_del_ = True
  499. except (TypeError, AttributeError):
  500. pass
  501. return annmodel.s_None
  502. def annotate_hooks(self, **kwds_s):
  503. driver = self.instance.im_self
  504. s_jitcell = self.bookkeeper.valueoftype(BaseJitCell)
  505. h = self.annotate_hook
  506. h(driver.get_jitcell_at, driver.greens, **kwds_s)
  507. h(driver.set_jitcell_at, driver.greens, [s_jitcell], **kwds_s)
  508. h(driver.get_printable_location, driver.greens, **kwds_s)
  509. def annotate_hook(self, func, variables, args_s=[], **kwds_s):
  510. if func is None:
  511. return
  512. bk = self.bookkeeper
  513. s_func = bk.immutablevalue(func)
  514. uniquekey = 'jitdriver.%s' % func.func_name
  515. args_s = args_s[:]
  516. for name in variables:
  517. if '.' not in name:
  518. s_arg = kwds_s['s_' + name]
  519. else:
  520. objname, fieldname = name.split('.')
  521. s_instance = kwds_s['s_' + objname]
  522. attrdef = s_instance.classdef.find_attribute(fieldname)
  523. position = self.bookkeeper.position_key
  524. attrdef.read_locations[position] = True
  525. s_arg = attrdef.getvalue()
  526. assert s_arg is not None
  527. args_s.append(s_arg)
  528. bk.emulate_pbc_call(uniquekey, s_func, args_s)
  529. def get_getfield_op(self, rtyper):
  530. if rtyper.type_system.name == 'ootypesystem':
  531. return 'oogetfield'
  532. else:
  533. return 'getfield'
  534. def specialize_call(self, hop, **kwds_i):
  535. # XXX to be complete, this could also check that the concretetype
  536. # of the variables are the same for each of the calls.
  537. from pypy.rpython.lltypesystem import lltype
  538. driver = self.instance.im_self
  539. greens_v = []
  540. reds_v = []
  541. for name in driver.greens:
  542. if '.' not in name:
  543. i = kwds_i['i_' + name]
  544. r_green = hop.args_r[i]
  545. v_green = hop.inputarg(r_green, arg=i)
  546. else:
  547. objname, fieldname = name.split('.') # see test_green_field
  548. assert objname in driver.reds
  549. i = kwds_i['i_' + objname]
  550. s_red = hop.args_s[i]
  551. r_red = hop.args_r[i]
  552. while True:
  553. try:
  554. mangled_name, r_field = r_red._get_field(fieldname)
  555. break
  556. except KeyError:
  557. pass
  558. assert r_red.rbase is not None, (
  559. "field %r not found in %r" % (name,
  560. r_red.lowleveltype.TO))
  561. r_red = r_red.rbase
  562. if hop.rtyper.type_system.name == 'ootypesystem':
  563. GTYPE = r_red.lowleveltype
  564. else:
  565. GTYPE = r_red.lowleveltype.TO
  566. assert GTYPE._immutable_field(mangled_name), (
  567. "field %r must be declared as immutable" % name)
  568. if not hasattr(driver, 'll_greenfields'):
  569. driver.ll_greenfields = {}
  570. driver.ll_greenfields[name] = GTYPE, mangled_name
  571. #
  572. v_red = hop.inputarg(r_red, arg=i)
  573. c_llname = hop.inputconst(lltype.Void, mangled_name)
  574. getfield_op = self.get_getfield_op(hop.rtyper)
  575. v_green = hop.genop(getfield_op, [v_red, c_llname],
  576. resulttype=r_field)
  577. s_green = s_red.classdef.about_attribute(fieldname)
  578. assert s_green is not None
  579. hop.rtyper.annotator.setbinding(v_green, s_green)
  580. greens_v.append(v_green)
  581. for name in driver.reds:
  582. i = kwds_i['i_' + name]
  583. r_red = hop.args_r[i]
  584. v_red = hop.inputarg(r_red, arg=i)
  585. reds_v.append(v_red)
  586. hop.exception_cannot_occur()
  587. vlist = [hop.inputconst(lltype.Void, self.instance.__name__),
  588. hop.inputconst(lltype.Void, driver)]
  589. vlist.extend(greens_v)
  590. vlist.extend(reds_v)
  591. return hop.genop('jit_marker', vlist,
  592. resulttype=lltype.Void)
  593. class ExtLoopHeader(ExtRegistryEntry):
  594. # Replace a call to myjitdriver.loop_header()
  595. # with an operation jit_marker('loop_header', myjitdriver).
  596. def compute_result_annotation(self, **kwds_s):
  597. from pypy.annotation import model as annmodel
  598. return annmodel.s_None
  599. def specialize_call(self, hop):
  600. from pypy.rpython.lltypesystem import lltype
  601. driver = self.instance.im_self
  602. hop.exception_cannot_occur()
  603. vlist = [hop.inputconst(lltype.Void, 'loop_header'),
  604. hop.inputconst(lltype.Void, driver)]
  605. return hop.genop('jit_marker', vlist,
  606. resulttype=lltype.Void)
  607. class ExtSetParam(ExtRegistryEntry):
  608. _about_ = _set_param
  609. def compute_result_annotation(self, s_driver, s_name, s_value):
  610. from pypy.annotation import model as annmodel
  611. assert s_name.is_constant()
  612. if not self.bookkeeper.immutablevalue(DEFAULT).contains(s_value):
  613. if s_name.const == 'enable_opts':
  614. assert annmodel.SomeString(can_be_None=True).contains(s_value)
  615. else:
  616. assert annmodel.SomeInteger().contains(s_value)
  617. return annmodel.s_None
  618. def specialize_call(self, hop):
  619. from pypy.rpython.lltypesystem import lltype
  620. from pypy.rpython.lltypesystem.rstr import string_repr
  621. from pypy.objspace.flow.model import Constant
  622. hop.exception_cannot_occur()
  623. driver = hop.inputarg(lltype.Void, arg=0)
  624. name = hop.args_s[1].const
  625. if name == 'enable_opts':
  626. repr = string_repr
  627. else:
  628. repr = lltype.Signed
  629. if (isinstance(hop.args_v[2], Constant) and
  630. hop.args_v[2].value is DEFAULT):
  631. value = PARAMETERS[name]
  632. v_value = hop.inputconst(repr, value)
  633. else:
  634. v_value = hop.inputarg(repr, arg=2)
  635. vlist = [hop.inputconst(lltype.Void, "set_param"),
  636. driver,
  637. hop.inputconst(lltype.Void, name),
  638. v_value]
  639. return hop.genop('jit_marker', vlist,
  640. resulttype=lltype.Void)
  641. class AsmInfo(object):
  642. """ An addition to JitDebugInfo concerning assembler. Attributes:
  643. ops_offset - dict of offsets of operations or None
  644. asmaddr - (int) raw address of assembler block
  645. asmlen - assembler block length
  646. """
  647. def __init__(self, ops_offset, asmaddr, asmlen):
  648. self.ops_offset = ops_offset
  649. self.asmaddr = asmaddr
  650. self.asmlen = asmlen
  651. class JitDebugInfo(object):
  652. """ An object representing debug info. Attributes meanings:
  653. greenkey - a list of green boxes or None for bridge
  654. logger - an instance of jit.metainterp.logger.LogOperations
  655. type - either 'loop', 'entry bridge' or 'bridge'
  656. looptoken - description of a loop
  657. fail_descr_no - number of failing descr for bridges, -1 otherwise
  658. asminfo - extra assembler information
  659. """
  660. asminfo = None
  661. def __init__(self, jitdriver_sd, logger, looptoken, operations, type,
  662. greenkey=None, fail_descr_no=-1):
  663. self.jitdriver_sd = jitdriver_sd
  664. self.logger = logger
  665. self.looptoken = looptoken
  666. self.operations = operations
  667. self.type = type
  668. if type == 'bridge':
  669. assert fail_descr_no != -1
  670. else:
  671. assert greenkey is not None
  672. self.greenkey = greenkey
  673. self.fail_descr_no = fail_descr_no
  674. def get_jitdriver(self):
  675. """ Return where the jitdriver on which the jitting started
  676. """
  677. return self.jitdriver_sd.jitdriver
  678. def get_greenkey_repr(self):
  679. """ Return the string repr of a greenkey
  680. """
  681. return self.jitdriver_sd.warmstate.get_location_str(self.greenkey)
  682. class JitHookInterface(object):
  683. """ This is the main connector between the JIT and the interpreter.
  684. Several methods on this class will be invoked at various stages
  685. of JIT running like JIT loops compiled, aborts etc.
  686. An instance of this class will be available as policy.jithookiface.
  687. """
  688. def on_abort(self, reason, jitdriver, greenkey, greenkey_repr):
  689. """ A hook called each time a loop is aborted with jitdriver and
  690. greenkey where it started, reason is a string why it got aborted
  691. """
  692. #def before_optimize(self, debug_info):
  693. # """ A hook called before optimizer is run, called with instance of
  694. # JitDebugInfo. Overwrite for custom behavior
  695. # """
  696. # DISABLED
  697. def before_compile(self, debug_info):
  698. """ A hook called after a loop is optimized, before compiling assembler,
  699. called with JitDebugInfo instance. Overwrite for custom behavior
  700. """
  701. def after_compile(self, debug_info):
  702. """ A hook called after a loop has compiled assembler,
  703. called with JitDebugInfo instance. Overwrite for custom behavior
  704. """
  705. #def before_optimize_bridge(self, debug_info):
  706. # operations, fail_descr_no):
  707. # """ A hook called before a bridge is optimized.
  708. # Called with JitDebugInfo instance, overwrite for
  709. # custom behavior
  710. # """
  711. # DISABLED
  712. def before_compile_bridge(self, debug_info):
  713. """ A hook called before a bridge is compiled, but after optimizations
  714. are performed. Called with instance of debug_info, overwrite for
  715. custom behavior
  716. """
  717. def after_compile_bridge(self, debug_info):
  718. """ A hook called after a bridge is compiled, called with JitDebugInfo
  719. instance, overwrite for custom behavior
  720. """
  721. def get_stats(self):
  722. """ Returns various statistics
  723. """
  724. raise NotImplementedError
  725. def record_known_class(value, cls):
  726. """
  727. Assure the JIT that value is an instance of cls. This is not a precise
  728. class check, unlike a guard_class.
  729. """
  730. assert isinstance(value, cls)
  731. class Entry(ExtRegistryEntry):
  732. _about_ = record_known_class
  733. def compute_result_annotation(self, s_inst, s_cls):
  734. from pypy.annotation import model as annmodel
  735. assert s_cls.is_constant()
  736. assert not s_inst.can_be_none()
  737. assert isinstance(s_inst, annmodel.SomeInstance)
  738. def specialize_call(self, hop):
  739. from pypy.rpython.lltypesystem import rclass, lltype
  740. classrepr = rclass.get_type_repr(hop.rtyper)
  741. hop.exception_cannot_occur()
  742. v_inst = hop.inputarg(hop.args_r[0], arg=0)
  743. v_cls = hop.inputarg(classrepr, arg=1)
  744. return hop.genop('jit_record_known_class', [v_inst, v_cls],
  745. resulttype=lltype.Void)