1. Carl Friedrich Bolz
  2. Pyrolog

Commits

Carl Friedrich Bolz  committed ffed743

perform mode specialization, taking the shape of the unbound variables in the
query into account.

  • Participants
  • Parent commits a462ec9
  • Branches compress-terms2

Comments (0)

Files changed (3)

File prolog/interpreter/continuation.py

View file
 # ___________________________________________________________________
 # JIT stuff
 
-def get_printable_location(rule):
+def get_printable_location(mode, rule, shape):
     if rule:
         s = rule.signature.string()
     else:
         s = "No rule"
+    if shape:
+        s = "%s %s" % (s, shape.str())
+    s = "%s %s" % (s, mode)
     return s
 
 def get_jitcell_at(where, rule):
 callsig = Signature.getsignature(":-", 1)
 
 jitdriver = jit.JitDriver(
-        greens=["rule"],
+        greens=["mode", "rule", "shape"],
         reds=["scont", "fcont", "heap"],
         get_printable_location=get_printable_location,
         #get_jitcell_at=get_jitcell_at,
 
 
 def driver(scont, fcont, heap):
+    from prolog.interpreter import shape as shapemod
     rule = None
+    shape = None
+    mode = 0
     while not scont.is_done():
         #view(scont=scont, fcont=fcont, heap=heap)
+        rule, shape, mode = scont.get_greens(rule, shape, mode)
         if isinstance(scont, RuleContinuation) and scont._rule.body is not None:
-            rule = scont._rule
-            jitdriver.can_enter_jit(rule=rule, scont=scont, fcont=fcont,
-                                    heap=heap)
+            jitdriver.can_enter_jit(rule=rule, shape=shape, scont=scont, fcont=fcont,
+                                    heap=heap, mode=mode)
         try:
-            jitdriver.jit_merge_point(rule=rule, scont=scont, fcont=fcont,
-                                      heap=heap)
+            jitdriver.jit_merge_point(rule=rule, shape=shape, scont=scont, fcont=fcont,
+                                      heap=heap, mode=mode)
             oldscont = scont
             scont, fcont, heap  = scont.activate(fcont, heap)
             assert heap is not None
     def find_end_of_cut(self):
         return self.nextcont.find_end_of_cut()
 
+    def get_greens(self, oldrule, oldshape, oldmode):
+        return oldrule, oldshape, oldmode
+
     _dot = _dot
 
 class ContinuationWithModule(Continuation):
             cont = nextcont
         return cont, fcont, heap
 
+    def get_greens(self, oldrule, oldshape, oldmode):
+        from prolog.interpreter import shape
+        query = self.query
+        if isinstance(query, shape.ShapedCallableBase):
+            shape = query.get_shape()
+            mode = query.get_mode()
+        else:
+            shape = oldshape
+            mode = oldmode
+        return self._rule, shape, mode
+
+
+    def get_shape(self, oldshape):
+        return oldshape
+
     def __repr__(self):
         return "<RuleContinuation rule=%r query=%r>" % (self._rule, self.query)
 

File prolog/interpreter/shape.py

View file
     def get_path(self, index):
         raise NotImplementedError("abstract base class")
 
+    def str(self):
+        return ""
+
+    def __repr__(self):
+        return self.str()
+
 INEFFICIENT = Shape()
 SEEN_ONCE = Shape()
 
     def replace(self, i, shape):
         assert 0, "cannot happen"
 
-    def __repr__(self):
-        return "%s(%r)" % (self.__class__.__name__, self.w_obj)
+    def str(self):
+        return "WrapShape(%s)" % (self.w_obj, )
 
 class InStorageShape(Shape):
 
         assert i == 0
         return shape
 
-    def __repr__(self):
-        return self.__class__.__name__ + "()"
+    def str(self):
+        return "InStorageShape()"
 
 InStorageShape._singleton = InStorageShape()
 
         assert isinstance(newshape, SharingShape)
         return newshape
 
-    def __repr__(self):
-        return "%s(%r, %r)" % (self.__class__.__name__, self.signature, self.children)
+    def str(self):
+        return "SharingShape(%s, [%s])" % (self.signature.string(), ", ".join([child.str() for child in self.children]))
 
     def _dot(self, seen):
         if self in seen:
     def new(self, shape, storage):
         raise NotImplementedError("abstract base class")
 
+    @jit.unroll_safe
+    def get_mode(self):
+        from pypy.rlib.rarithmetic import intmask
+        mode = 0x345678
+        for i in range(self.size_storage()):
+            child = self.get_storage(i)
+            y = 0
+            if isinstance(child, term.VarInTerm):
+                parent = child.parent
+                shape = parent.get_shape()
+                indicator = child.indicator
+                if isinstance(indicator, term.VarInTermIndex):
+                    y = objectmodel.compute_identity_hash(shape)
+                else:
+                    y = 0
+            mode = intmask((1000003 * mode) ^ y)
+        return mode
+
 class ShapedCallableMixin:
     TYPE_STANDARD_ORDER = term.Term.TYPE_STANDARD_ORDER
     _mixin_ = True

File prolog/interpreter/test/test_continuation.py

View file
         def activate(self, fcont, heap):
             all.append((X.dereference(heap).name(), Y.dereference(heap).name()))
             raise error.UnificationFailed
+        def get_greens(self, a, b, c):
+            return a, b, c
     e.add_rule(Callable.build("f", [Callable.build("x")]), True)
     e.add_rule(Callable.build("f", [Callable.build("y")]), True)
     e.add_rule(Callable.build("g", [Callable.build("a")]), True)