1. Wubin Qu
  2. pypy

Commits

Armin Rigo  committed 11a7dcf

Detect potential aliases and revert objects from the 'R' category
to the 'O' category.

  • Participants
  • Parent commits 329b69f
  • Branches stm-thread-2

Comments (0)

Files changed (3)

File pypy/translator/backendopt/writeanalyze.py

View file
  • Ignore whitespace
             if graphinfo is None or not graphinfo.is_fresh_malloc(op.args[0]):
                 return frozenset([
                     ("struct", op.args[0].concretetype, op.args[1].value)])
-        elif op.opname == "setarrayitem":
+        elif op.opname == ("setarrayitem", "setinteriorfield"):
             if graphinfo is None or not graphinfo.is_fresh_malloc(op.args[0]):
                 return self._array_result(op.args[0].concretetype)
+        elif op.opname == "gc_store":
+            return top_set    # xxx conservative
         return empty_set
 
     def _array_result(self, TYPE):

File pypy/translator/stm/test/test_transform2.py

View file
  • Ignore whitespace
 
 
 class BaseTestTransform(object):
-    prebuilt = ()
 
     def build_state(self):
         self.writemode = set()
             return p._category
         if not p:
             return 'N'
-        if p in self.prebuilt:
-            return 'G'
+        if p._solid:
+            return 'G'     # allocated with immortal=True
         raise AssertionError("unknown category on %r" % (p,))
 
     def interpret(self, fn, args):
         x1.foo = 42
         x2 = lltype.malloc(X, immortal=True)
         x2.foo = 81
-        self.prebuilt = [x1, x2]
 
         def f1(n):
             if n > 1:
         X = lltype.GcStruct('X', ('foo', lltype.Signed))
         x1 = lltype.malloc(X, immortal=True)
         x1.foo = 42
-        self.prebuilt = [x1]
 
         def f1(n):
             x1.foo = n
         x2 = lltype.malloc(X, immortal=True)
         x2.foo = 81
         x2.bar = -1
-        self.prebuilt = [x1, x2]
 
         def f1(n):
             if n > 1:
         self.interpret(f1, [4])
         assert len(self.writemode) == 1
         assert self.barriers == []
+
+    def test_write_may_alias(self):
+        X = lltype.GcStruct('X', ('foo', lltype.Signed))
+        def f1(p, q):
+            x1 = p.foo
+            q.foo = 7
+            x2 = p.foo
+            return x1 * x2
+
+        x = lltype.malloc(X, immortal=True); x.foo = 6
+        y = lltype.malloc(X, immortal=True)
+        res = self.interpret(f1, [x, y])
+        assert res == 36
+        assert self.barriers == ['P2R', 'P2W', 'O2R']
+        res = self.interpret(f1, [x, x])
+        assert res == 42
+        assert self.barriers == ['P2R', 'P2W', 'O2R']
+
+    def test_write_cannot_alias(self):
+        X = lltype.GcStruct('X', ('foo', lltype.Signed))
+        Y = lltype.GcStruct('Y', ('foo', lltype.Signed))
+        def f1(p, q):
+            x1 = p.foo
+            q.foo = 7
+            x2 = p.foo
+            return x1 * x2
+
+        x = lltype.malloc(X, immortal=True); x.foo = 6
+        y = lltype.malloc(Y, immortal=True)
+        res = self.interpret(f1, [x, y])
+        assert res == 36
+        assert self.barriers == ['P2R', 'P2W']

File pypy/translator/stm/transform2.py

View file
  • Ignore whitespace
 from pypy.objspace.flow.model import checkgraph
 from pypy.translator.unsimplify import varoftype
 from pypy.rpython.lltypesystem import lltype
+from pypy.translator.backendopt.writeanalyze import WriteAnalyzer, top_set
 
 
 
 
     def __init__(self, translator):
         self.translator = translator
+        self.write_analyzer = WriteAnalyzer(translator)
 
     def transform(self):
         assert not hasattr(self.translator, 'stm_transformation_applied')
         self.start_log()
         for graph in self.translator.graphs:
-            pre_insert_stm_barrier(self.translator, graph)
+            pre_insert_stm_barrier(self, graph)
         self.translator.stm_transformation_applied = True
         self.print_logs()
 
         return False
     raise AssertionError(op)
 
-def pre_insert_stm_barrier(translator, graph):
+
+def pre_insert_stm_barrier(stmtransformer, graph):
+    graphinfo = stmtransformer.write_analyzer.compute_graph_info(graph)
+
     for block in graph.iterblocks():
         if block.operations == ():
             continue
                         newoperations.append(newop)
                         renamings[op.args[0]] = w
                         category[w] = to
-                elif op.opname in MALLOCS:
-                    category[op.result] = 'W'
                 newop = SpaceOperation(op.opname,
                                        [renamings.get(v, v) for v in op.args],
                                        op.result)
                 newoperations.append(newop)
+                #
+                effectinfo = stmtransformer.write_analyzer.analyze(
+                    op, graphinfo=graphinfo)
+                if effectinfo:
+                    if effectinfo is top_set:
+                        category.clear()
+                    else:
+                        types = set([entry[1] for entry in effectinfo])
+                        for v in category.keys():
+                            if v.concretetype in types and category[v] == 'R':
+                                category[v] = 'O'
+                #
+                if op.opname in MALLOCS:
+                    category[op.result] = 'W'
+
             block.operations = newoperations