comparison graal/com.oracle.truffle.api.dsl.test/src/com/oracle/truffle/api/dsl/test/SpecializationGroupingTest.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 08aa0372dad4
children
comparison
equal deleted inserted replaced
19290:bf166845c7d8 19291:f4792a544170
26 26
27 import com.oracle.truffle.api.*; 27 import com.oracle.truffle.api.*;
28 import com.oracle.truffle.api.dsl.*; 28 import com.oracle.truffle.api.dsl.*;
29 import com.oracle.truffle.api.dsl.test.SpecializationGroupingTestFactory.TestElseConnectionBug1Factory; 29 import com.oracle.truffle.api.dsl.test.SpecializationGroupingTestFactory.TestElseConnectionBug1Factory;
30 import com.oracle.truffle.api.dsl.test.SpecializationGroupingTestFactory.TestElseConnectionBug2Factory; 30 import com.oracle.truffle.api.dsl.test.SpecializationGroupingTestFactory.TestElseConnectionBug2Factory;
31 import com.oracle.truffle.api.dsl.test.SpecializationGroupingTestFactory.TestGroupingFactory;
32 import com.oracle.truffle.api.dsl.test.TypeSystemTest.SimpleTypes;
33 import com.oracle.truffle.api.dsl.test.TypeSystemTest.TestRootNode;
34 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode; 31 import com.oracle.truffle.api.dsl.test.TypeSystemTest.ValueNode;
35 import com.oracle.truffle.api.frame.*; 32 import com.oracle.truffle.api.frame.*;
36 import com.oracle.truffle.api.nodes.*; 33 import com.oracle.truffle.api.nodes.*;
37 34
38 /** 35 /**
39 * Tests execution counts of guards. While we do not make guarantees for guard invocation except for 36 * Tests execution counts of guards. While we do not make guarantees for guard invocation except for
40 * their execution order our implementation reduces the calls to guards as much as possible for the 37 * their execution order our implementation reduces the calls to guards as much as possible for the
41 * generic case. 38 * generic case.
42 */ 39 */
43 public class SpecializationGroupingTest { 40 public class SpecializationGroupingTest {
44
45 @Test
46 public void testGrouping() {
47 MockAssumption a1 = new MockAssumption(true);
48 MockAssumption a3 = new MockAssumption(true);
49
50 TestRootNode<TestGrouping> root = TestHelper.createRoot(TestGroupingFactory.getInstance(), a1, a3);
51
52 SimpleTypes.intCast = 0;
53 SimpleTypes.intCheck = 0;
54 TestGrouping.true1 = 0;
55 TestGrouping.false1 = 0;
56 TestGrouping.true2 = 0;
57 TestGrouping.false2 = 0;
58 TestGrouping.true3 = 0;
59
60 Assert.assertEquals(42, TestHelper.executeWith(root, 21, 21));
61 Assert.assertEquals(5, TestGrouping.true1);
62 Assert.assertEquals(0, TestGrouping.false1);
63 Assert.assertEquals(5, TestGrouping.true2);
64 Assert.assertEquals(5, TestGrouping.false2);
65 Assert.assertEquals(5, TestGrouping.true3);
66 Assert.assertEquals(10, SimpleTypes.intCheck);
67 Assert.assertEquals(8, SimpleTypes.intCast);
68 Assert.assertEquals(4, a1.checked);
69 Assert.assertEquals(4, a3.checked);
70
71 Assert.assertEquals(42, TestHelper.executeWith(root, 21, 21));
72 Assert.assertEquals(6, TestGrouping.true1);
73 Assert.assertEquals(0, TestGrouping.false1);
74 Assert.assertEquals(6, TestGrouping.true2);
75 Assert.assertEquals(6, TestGrouping.false2);
76 Assert.assertEquals(6, TestGrouping.true3);
77
78 Assert.assertEquals(5, a1.checked);
79 Assert.assertEquals(5, a3.checked);
80 Assert.assertEquals(10, SimpleTypes.intCheck);
81 Assert.assertEquals(8, SimpleTypes.intCast);
82
83 }
84
85 @SuppressWarnings("unused")
86 @NodeChildren({@NodeChild, @NodeChild})
87 @NodeAssumptions({"a1", "a3"})
88 public abstract static class TestGrouping extends ValueNode {
89
90 private static int true1;
91 private static int false1;
92 private static int true2;
93 private static int false2;
94 private static int true3;
95
96 protected boolean true1(int value) {
97 true1++;
98 return true;
99 }
100
101 protected boolean false1(int value, int value2) {
102 false1++;
103 return false;
104 }
105
106 protected boolean true2(int value) {
107 true2++;
108 return true;
109 }
110
111 protected boolean false2(int value) {
112 false2++;
113 return false;
114 }
115
116 protected boolean true3(int value) {
117 true3++;
118 return true;
119 }
120
121 @Specialization
122 public int fail(int value1, String value2) {
123 throw new AssertionError();
124 }
125
126 @Specialization(guards = {"true1(value1)", "true2(value1)", "!false2(value1)", "true3(value1)"}, assumptions = {"a1", "a3"}, rewriteOn = RuntimeException.class)
127 public int throwRewrite(int value1, int value2) {
128 throw new RuntimeException();
129 }
130
131 @Specialization(guards = {"true1(value1)", "true2(value1)", "!false2(value1)", "true3(value1)"}, contains = "throwRewrite", assumptions = {"a1", "a3"})
132 public int success(int value1, int value2) {
133 return value1 + value2;
134 }
135
136 @Specialization(guards = {"true1(value1)", "true2(value1)", "!false2(value1)", "!true3(value1)"}, assumptions = {"a1", "a3"})
137 public int fail5(int value1, int value2) {
138 throw new AssertionError();
139 }
140
141 @Specialization(guards = {"true1(value1)", "true2(value1)", "false2(value1)"}, assumptions = {"a1", "a3"})
142 public int fail4(int value1, int value2) {
143 throw new AssertionError();
144 }
145
146 @Specialization(guards = {"true1(value1)", "true2(value1)"}, assumptions = {"a1", "a3"})
147 public int fail2break(int value1, int value2) {
148 throw new AssertionError();
149 }
150
151 @Specialization(guards = {"true1(value1)", "false1(value1, value2)"})
152 public int fail1(int value1, int value2) {
153 throw new AssertionError();
154 }
155
156 }
157 41
158 @Test 42 @Test
159 public void testElseConnectionBug1() { 43 public void testElseConnectionBug1() {
160 CallTarget target = TestHelper.createCallTarget(TestElseConnectionBug1Factory.create(new GenericInt())); 44 CallTarget target = TestHelper.createCallTarget(TestElseConnectionBug1Factory.create(new GenericInt()));
161 Assert.assertEquals(42, target.call()); 45 Assert.assertEquals(42, target.call());
230 boolean guard1(int value) { 114 boolean guard1(int value) {
231 return false; 115 return false;
232 } 116 }
233 } 117 }
234 118
235 private static class MockAssumption implements Assumption {
236
237 int checked;
238
239 private final boolean valid;
240
241 public MockAssumption(boolean valid) {
242 this.valid = valid;
243 }
244
245 public void check() throws InvalidAssumptionException {
246 checked++;
247 if (!valid) {
248 throw new InvalidAssumptionException();
249 }
250 }
251
252 public boolean isValid() {
253 checked++;
254 return valid;
255 }
256
257 public void invalidate() {
258 throw new UnsupportedOperationException();
259 }
260
261 public String getName() {
262 throw new UnsupportedOperationException();
263 }
264
265 }
266
267 } 119 }