Mercurial > hg > graal-compiler
comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/AssumptionsTest.java @ 19291:f4792a544170
Truffle-DSL: implement new assumptions semantics.
author | Christian Humer <christian.humer@gmail.com> |
---|---|
date | Wed, 11 Feb 2015 12:13:44 +0100 |
parents | 9f38d222fa6c |
children |
comparison
equal
deleted
inserted
replaced
19290:bf166845c7d8 | 19291:f4792a544170 |
---|---|
20 * or visit www.oracle.com if you need additional information or have any | 20 * or visit www.oracle.com if you need additional information or have any |
21 * questions. | 21 * questions. |
22 */ | 22 */ |
23 package com.oracle.truffle.api.dsl.test; | 23 package com.oracle.truffle.api.dsl.test; |
24 | 24 |
25 import static com.oracle.truffle.api.dsl.test.TestHelper.*; | |
26 import static org.junit.Assert.*; | |
27 | |
28 import java.util.*; | |
29 | |
25 import org.junit.*; | 30 import org.junit.*; |
26 | 31 |
27 import com.oracle.truffle.api.*; | 32 import com.oracle.truffle.api.*; |
28 import com.oracle.truffle.api.dsl.*; | 33 import com.oracle.truffle.api.dsl.*; |
29 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.DerivedAssumptionNodeFactory; | 34 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.AssumptionArrayTestFactory; |
30 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.DerivedAssumptionRedeclaredNodeFactory; | 35 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.CacheAssumptionTestFactory; |
31 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.MultipleAssumptionsNodeFactory; | 36 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.FieldTestFactory; |
32 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.SingleAssumptionNodeFactory; | 37 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.MethodTestFactory; |
33 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode; | 38 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.NodeFieldTest2Factory; |
39 import com.oracle.truffle.api.dsl.test.AssumptionsTestFactory.StaticFieldTestFactory; | |
34 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; | 40 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; |
35 | 41 |
36 public class AssumptionsTest { | 42 public class AssumptionsTest { |
37 | 43 |
38 @Test | 44 @Test |
39 public void testSingleAssumption() { | 45 public void testField() { |
46 CallTarget root = createCallTarget(FieldTestFactory.getInstance()); | |
47 FieldTest node = getNode(root); | |
48 assertEquals(42, root.call(42)); | |
49 assertEquals(42, root.call(42)); | |
50 node.field.invalidate(); | |
51 try { | |
52 root.call(45); | |
53 fail(); | |
54 } catch (UnsupportedSpecializationException e) { | |
55 } | |
56 } | |
57 | |
58 @NodeChild | |
59 static class FieldTest extends ValueNode { | |
60 | |
61 protected final Assumption field = Truffle.getRuntime().createAssumption(); | |
62 | |
63 @Specialization(assumptions = "field") | |
64 static int do1(int value) { | |
65 return value; | |
66 } | |
67 } | |
68 | |
69 @Test | |
70 public void testNodeField() { | |
40 Assumption assumption = Truffle.getRuntime().createAssumption(); | 71 Assumption assumption = Truffle.getRuntime().createAssumption(); |
41 TestRootNode<?> root = TestHelper.createRoot(SingleAssumptionNodeFactory.getInstance(), assumption); | 72 CallTarget root = createCallTarget(NodeFieldTest2Factory.getInstance(), assumption); |
42 | 73 assertEquals(42, root.call(42)); |
43 Assert.assertEquals(42, TestHelper.executeWith(root)); | 74 assertEquals(42, root.call(42)); |
44 assumption.invalidate(); | 75 assumption.invalidate(); |
45 Assert.assertEquals("42", TestHelper.executeWith(root)); | 76 try { |
46 } | 77 root.call(45); |
47 | 78 fail(); |
48 @NodeAssumptions("assumption") | 79 } catch (UnsupportedSpecializationException e) { |
49 abstract static class SingleAssumptionNode extends ValueNode { | 80 } |
50 | 81 } |
51 @Specialization(assumptions = "assumption") | 82 |
52 int do2() { | 83 @NodeChild |
53 return 42; | 84 @NodeField(name = "field", type = Assumption.class) |
54 } | 85 static class NodeFieldTest2 extends ValueNode { |
55 | 86 |
56 @Fallback | 87 @Specialization(assumptions = "field") |
57 Object doFallBack() { | 88 static int do1(int value) { |
58 return "42"; | 89 return value; |
59 } | 90 } |
60 | 91 } |
61 } | 92 |
62 | 93 @Test |
63 @Test | 94 public void testStaticField() { |
64 public void testMultipleAssumption() { | 95 CallTarget root = createCallTarget(StaticFieldTestFactory.getInstance()); |
65 Assumption assumption1 = Truffle.getRuntime().createAssumption(); | 96 assertEquals(42, root.call(42)); |
66 Assumption assumption2 = Truffle.getRuntime().createAssumption(); | 97 assertEquals(42, root.call(42)); |
67 TestRootNode<?> root = TestHelper.createRoot(MultipleAssumptionsNodeFactory.getInstance(), assumption1, assumption2); | 98 StaticFieldTest.FIELD.invalidate(); |
68 | 99 try { |
69 Assert.assertEquals(42, TestHelper.executeWith(root)); | 100 root.call(45); |
70 assumption2.invalidate(); | 101 fail(); |
71 Assert.assertEquals("41", TestHelper.executeWith(root)); | 102 } catch (UnsupportedSpecializationException e) { |
72 assumption1.invalidate(); | 103 } |
73 Assert.assertEquals("42", TestHelper.executeWith(root)); | 104 } |
74 } | 105 |
75 | 106 @NodeChild |
76 @NodeAssumptions({"assumption1", "assumption2"}) | 107 static class StaticFieldTest extends ValueNode { |
77 abstract static class MultipleAssumptionsNode extends ValueNode { | 108 |
78 | 109 static final Assumption FIELD = Truffle.getRuntime().createAssumption(); |
79 @Specialization(assumptions = {"assumption1", "assumption2"}) | 110 |
80 int doInt() { | 111 @Specialization(assumptions = "FIELD") |
81 return 42; | 112 static int do1(int value) { |
82 } | 113 return value; |
83 | 114 } |
84 @Specialization(assumptions = "assumption1") | 115 } |
85 Object doObject() { | 116 |
86 return "41"; | 117 @Test |
87 } | 118 public void testMethod() { |
88 | 119 CallTarget root = createCallTarget(MethodTestFactory.getInstance()); |
89 @Fallback | 120 MethodTest node = getNode(root); |
90 Object doFallBack() { | 121 assertEquals(42, root.call(42)); |
91 return "42"; | 122 assertEquals(42, root.call(42)); |
92 } | 123 node.hiddenAssumption.invalidate(); |
93 } | 124 try { |
94 | 125 root.call(45); |
95 @Test | 126 fail(); |
96 public void testDerivedAssumption() { | 127 } catch (UnsupportedSpecializationException e) { |
97 Assumption additionalAssumption = Truffle.getRuntime().createAssumption(); | 128 } |
98 Assumption assumption = Truffle.getRuntime().createAssumption(); | 129 } |
99 TestRootNode<?> root = TestHelper.createRoot(DerivedAssumptionNodeFactory.getInstance(), assumption, additionalAssumption); | 130 |
100 | 131 @NodeChild |
101 Assert.assertEquals(42, TestHelper.executeWith(root)); | 132 static class MethodTest extends ValueNode { |
102 assumption.invalidate(); | 133 |
103 Assert.assertEquals(43, TestHelper.executeWith(root)); | 134 private final Assumption hiddenAssumption = Truffle.getRuntime().createAssumption(); |
104 additionalAssumption.invalidate(); | 135 |
105 Assert.assertEquals("42", TestHelper.executeWith(root)); | 136 @Specialization(assumptions = "getAssumption()") |
106 } | 137 static int do1(int value) { |
107 | 138 return value; |
108 @NodeAssumptions({"additionalAssumption"}) | 139 } |
109 abstract static class DerivedAssumptionNode extends SingleAssumptionNode { | 140 |
110 | 141 Assumption getAssumption() { |
111 @Specialization(assumptions = "additionalAssumption") | 142 return hiddenAssumption; |
112 int doIntDerived() { | 143 } |
113 return 43; | 144 } |
114 } | 145 |
115 | 146 @Test |
116 } | 147 public void testCacheAssumption() { |
117 | 148 CallTarget root = createCallTarget(CacheAssumptionTestFactory.getInstance()); |
118 @Test | 149 CacheAssumptionTest node = getNode(root); |
119 public void testDerivedAssumptionRedeclared() { | 150 assertEquals("do1", root.call(42)); |
120 Assumption additionalAssumption = Truffle.getRuntime().createAssumption(); | 151 assertEquals("do1", root.call(43)); |
121 Assumption assumption = Truffle.getRuntime().createAssumption(); | 152 assertEquals("do1", root.call(44)); |
122 TestRootNode<?> root = TestHelper.createRoot(DerivedAssumptionRedeclaredNodeFactory.getInstance(), additionalAssumption, assumption); | 153 node.assumptions.get(42).invalidate(); |
123 | 154 node.assumptions.put(42, null); // clear 42 |
124 Assert.assertEquals(42, TestHelper.executeWith(root)); | 155 node.assumptions.get(43).invalidate(); |
125 assumption.invalidate(); | 156 node.assumptions.get(44).invalidate(); |
126 Assert.assertEquals(43, TestHelper.executeWith(root)); | 157 assertEquals("do1", root.call(42)); // recreates 42 |
127 additionalAssumption.invalidate(); | 158 assertEquals("do2", root.call(43)); // invalid 43 -> remove -> insert do2 |
128 Assert.assertEquals("42", TestHelper.executeWith(root)); | 159 assertEquals("do2", root.call(46)); // no more lines can be created. |
129 } | 160 } |
130 | 161 |
131 @NodeAssumptions({"additionalAssumption", "assumption"}) | 162 @Test |
132 abstract static class DerivedAssumptionRedeclaredNode extends SingleAssumptionNode { | 163 public void testCacheAssumptionLimit() { |
133 | 164 CallTarget root = createCallTarget(CacheAssumptionTestFactory.getInstance()); |
134 @Specialization(assumptions = "additionalAssumption") | 165 assertEquals("do1", root.call(42)); |
135 int doIntDerived() { | 166 assertEquals("do1", root.call(43)); |
136 return 43; | 167 assertEquals("do1", root.call(44)); |
137 } | 168 assertEquals("do2", root.call(45)); |
138 | 169 assertEquals("do1", root.call(43)); |
170 assertEquals("do1", root.call(44)); | |
171 } | |
172 | |
173 @NodeChild | |
174 @SuppressWarnings("unused") | |
175 static class CacheAssumptionTest extends ValueNode { | |
176 | |
177 private final Map<Integer, Assumption> assumptions = new HashMap<>(); | |
178 | |
179 @Specialization(guards = "value == cachedValue", assumptions = "getAssumption(cachedValue)") | |
180 static String do1(int value, @Cached("value") int cachedValue) { | |
181 return "do1"; | |
182 } | |
183 | |
184 @Specialization | |
185 static String do2(int value) { | |
186 return "do2"; | |
187 } | |
188 | |
189 Assumption getAssumption(int value) { | |
190 Assumption assumption = assumptions.get(value); | |
191 if (assumption == null) { | |
192 assumption = Truffle.getRuntime().createAssumption(); | |
193 assumptions.put(value, assumption); | |
194 } | |
195 return assumption; | |
196 } | |
197 } | |
198 | |
199 @Test | |
200 public void testAssumptionArrays() { | |
201 CallTarget root = createCallTarget(AssumptionArrayTestFactory.getInstance()); | |
202 AssumptionArrayTest node = getNode(root); | |
203 | |
204 Assumption a1 = Truffle.getRuntime().createAssumption(); | |
205 Assumption a2 = Truffle.getRuntime().createAssumption(); | |
206 | |
207 node.assumptions = new Assumption[]{a1, a2}; | |
208 | |
209 assertEquals("do1", root.call(42)); | |
210 | |
211 a2.invalidate(); | |
212 | |
213 assertEquals("do2", root.call(42)); | |
214 } | |
215 | |
216 @NodeChild | |
217 @SuppressWarnings("unused") | |
218 static class AssumptionArrayTest extends ValueNode { | |
219 | |
220 Assumption[] assumptions; | |
221 | |
222 @Specialization(assumptions = "assumptions") | |
223 static String do1(int value) { | |
224 return "do1"; | |
225 } | |
226 | |
227 @Specialization | |
228 static String do2(int value) { | |
229 return "do2"; | |
230 } | |
231 | |
232 } | |
233 | |
234 @NodeChild | |
235 static class ErrorIncompatibleReturnType extends ValueNode { | |
236 @ExpectError("Incompatible return type int. Assumptions must be assignable to Assumption or Assumption[].") | |
237 @Specialization(assumptions = "3") | |
238 static int do1(int value) { | |
239 return value; | |
240 } | |
241 } | |
242 | |
243 @NodeChild | |
244 static class ErrorBoundDynamicValue extends ValueNode { | |
245 | |
246 @ExpectError("Assumption expressions must not bind dynamic parameter values.") | |
247 @Specialization(assumptions = "createAssumption(value)") | |
248 static int do1(int value) { | |
249 return value; | |
250 } | |
251 | |
252 Assumption createAssumption(int value) { | |
253 return Truffle.getRuntime().createAssumption(String.valueOf(value)); | |
254 } | |
139 } | 255 } |
140 | 256 |
141 } | 257 } |