changeset 22348:dbbcd8eb5dae

Truffle DSL: fix algorithm to find guard dependencies on caches
author Benoit Daloze <benoit.daloze@jku.at>
date Thu, 05 Nov 2015 17:05:48 +0100
parents 6546703e0c76
children 522155ade3bf
files truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/CachedTest.java truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/SpecializationData.java
diffstat 2 files changed, 35 insertions(+), 4 deletions(-) [+]
line wrap: on
line diff
--- a/truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/CachedTest.java	Thu Nov 05 16:11:50 2015 +0100
+++ b/truffle/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/CachedTest.java	Thu Nov 05 17:05:48 2015 +0100
@@ -42,6 +42,7 @@
 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheFieldFactory;
 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheMethodFactory;
 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCacheNodeFieldFactory;
+import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestCachesOrderFactory;
 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestGuardWithCachedAndDynamicParameterFactory;
 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestGuardWithJustCachedParameterFactory;
 import com.oracle.truffle.api.dsl.test.CachedTestFactory.TestMultipleCachesFactory;
@@ -309,6 +310,35 @@
     }
 
     @NodeChild
+    static class TestCachesOrder extends ValueNode {
+
+        @Specialization(guards = "boundByGuard != 0")
+        static int do1(int value, //
+                        @Cached("get(value)") int intermediateValue, //
+                        @Cached("transform(intermediateValue)") int boundByGuard, //
+                        @Cached("new()") Object notBoundByGuards) {
+            return intermediateValue;
+        }
+
+        protected int get(int i) {
+            return i * 2;
+        }
+
+        protected int transform(int i) {
+            return i * 3;
+        }
+
+    }
+
+    @Test
+    public void testCachesOrder() {
+        CallTarget root = createCallTarget(TestCachesOrderFactory.getInstance());
+        assertEquals(42, root.call(21));
+        assertEquals(42, root.call(22));
+        assertEquals(42, root.call(23));
+    }
+
+    @NodeChild
     static class CachedError1 extends ValueNode {
         @Specialization
         static int do1(int value, @ExpectError("Incompatible return type int. The expression type must be equal to the parameter type double.")//
--- a/truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/SpecializationData.java	Thu Nov 05 16:11:50 2015 +0100
+++ b/truffle/com.oracle.truffle.dsl.processor/src/com/oracle/truffle/dsl/processor/model/SpecializationData.java	Thu Nov 05 17:05:48 2015 +0100
@@ -72,20 +72,21 @@
     }
 
     public boolean isCacheBoundByGuard(CacheExpression cacheExpression) {
+        VariableElement cachedVariable = cacheExpression.getParameter().getVariableElement();
+
         for (GuardExpression expression : getGuards()) {
-            if (expression.getExpression().findBoundVariableElements().contains(cacheExpression.getParameter().getVariableElement())) {
+            if (expression.getExpression().findBoundVariableElements().contains(cachedVariable)) {
                 return true;
             }
         }
 
-        // check all next binding caches if they are bound by guard
-        Set<VariableElement> boundVariables = cacheExpression.getExpression().findBoundVariableElements();
+        // check all next binding caches if they are bound to a guard and use this cache variable
         boolean found = false;
         for (CacheExpression expression : getCaches()) {
             if (cacheExpression == expression) {
                 found = true;
             } else if (found) {
-                if (boundVariables.contains(expression.getParameter().getVariableElement())) {
+                if (expression.getExpression().findBoundVariableElements().contains(cachedVariable)) {
                     if (isCacheBoundByGuard(expression)) {
                         return true;
                     }