Skip to content

Commit a082fe3

Browse files
committed
Fixed incorrect compiler directives and profiles
1 parent b24b197 commit a082fe3

File tree

2 files changed

+85
-52
lines changed

2 files changed

+85
-52
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/nodes/util/CastToJavaUnsignedLongNodeTests.java

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3939
* SOFTWARE.
4040
*/
41-
4241
package com.oracle.graal.python.nodes.util;
4342

4443
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
@@ -47,6 +46,9 @@
4746
import com.oracle.graal.python.runtime.exception.PException;
4847
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
4948
import com.oracle.graal.python.test.PythonTests;
49+
import com.oracle.truffle.api.Truffle;
50+
import com.oracle.truffle.api.frame.VirtualFrame;
51+
import com.oracle.truffle.api.nodes.RootNode;
5052
import org.junit.Assert;
5153
import org.junit.Before;
5254
import org.junit.Test;
@@ -57,68 +59,66 @@
5759
import static com.oracle.graal.python.runtime.exception.PythonErrorType.TypeError;
5860

5961
public class CastToJavaUnsignedLongNodeTests {
62+
private static PythonObjectFactory factory = PythonObjectFactory.getUncached();
6063

6164
@Before
6265
public void setUp() {
6366
PythonTests.enterContext();
6467
}
6568

66-
private static CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode.getUncached();
67-
private static PythonObjectFactory factory = PythonObjectFactory.getUncached();
68-
6969
@Test
7070
public void positiveInt() {
71-
Assert.assertEquals(0, castNode.execute(0));
72-
Assert.assertEquals(16, castNode.execute(16));
73-
Assert.assertEquals(Integer.MAX_VALUE, castNode.execute(Integer.MAX_VALUE));
74-
Assert.assertEquals(42, castNode.execute(Integer.valueOf(42)));
71+
Assert.assertEquals(0, castInt(0));
72+
Assert.assertEquals(16, castInt(16));
73+
Assert.assertEquals(Integer.MAX_VALUE, castInt(Integer.MAX_VALUE));
74+
Assert.assertEquals(42, castObject(42));
7575
}
7676

7777
@Test
7878
public void negativeInt() {
79-
expect(OverflowError, () -> castNode.execute(-1));
80-
expect(OverflowError, () -> castNode.execute(Integer.MIN_VALUE));
79+
expect(OverflowError, () -> castInt(-1));
80+
expect(OverflowError, () -> castInt(Integer.MIN_VALUE));
8181
}
8282

8383
@Test
8484
public void positiveLong() {
85-
Assert.assertEquals(0, castNode.execute(0L));
86-
Assert.assertEquals(0x80000000L, castNode.execute(0x80000000L));
87-
Assert.assertEquals(Long.MAX_VALUE, castNode.execute(Long.MAX_VALUE));
88-
Assert.assertEquals(1234567890123L, castNode.execute(Long.valueOf(1234567890123L)));
85+
Assert.assertEquals(0, castLong(0L));
86+
Assert.assertEquals(0x80000000L, castLong(0x80000000L));
87+
Assert.assertEquals(Long.MAX_VALUE, castLong(Long.MAX_VALUE));
88+
Assert.assertEquals(1234567890123L, castLong(1234567890123L));
8989
}
9090

9191
@Test
9292
public void negativeLong() {
93-
expect(OverflowError, () -> castNode.execute(-1L));
94-
expect(OverflowError, () -> castNode.execute(Long.MIN_VALUE));
93+
expect(OverflowError, () -> castLong(-1L));
94+
expect(OverflowError, () -> castLong(Long.MIN_VALUE));
9595
}
9696

9797
@Test
9898
public void positiveBigInt() {
99-
Assert.assertEquals(0, castNode.execute(makePInt(0)));
100-
Assert.assertEquals(1234567890123L, castNode.execute(makePInt(1234567890123L)));
101-
Assert.assertEquals(Long.MAX_VALUE, castNode.execute(makePInt(Long.MAX_VALUE)));
102-
Assert.assertEquals(0x8000000000000000L, castNode.execute(makePInt("8000000000000000")));
103-
Assert.assertEquals(0xFFFFFFFFFFFFFFFFL, castNode.execute(makePInt("FFFFFFFFFFFFFFFF")));
99+
Assert.assertEquals(0, castObject(makePInt(0)));
100+
Assert.assertEquals(1234567890123L, castObject(makePInt(1234567890123L)));
101+
Assert.assertEquals(Long.MAX_VALUE, castObject(makePInt(Long.MAX_VALUE)));
102+
Assert.assertEquals(0x8000000000000000L, castObject(makePInt("8000000000000000")));
103+
Assert.assertEquals(0xFFFFFFFFFFFFFFFFL, castObject(makePInt("FFFFFFFFFFFFFFFF")));
104104
}
105105

106106
@Test
107107
public void negativeBigInt() {
108-
expect(OverflowError, () -> castNode.execute(makePInt(-1)));
109-
expect(OverflowError, () -> castNode.execute(makePInt("-10000000000000000")));
108+
expect(OverflowError, () -> castObject(makePInt(-1)));
109+
expect(OverflowError, () -> castObject(makePInt("-10000000000000000")));
110110
}
111111

112112
@Test
113113
public void largeBigInt() {
114-
expect(OverflowError, () -> castNode.execute(makePInt("10000000000000000")));
115-
expect(OverflowError, () -> castNode.execute(makePInt("10000000000000001")));
114+
expect(OverflowError, () -> castObject(makePInt("10000000000000000")));
115+
expect(OverflowError, () -> castObject(makePInt("10000000000000001")));
116116
}
117117

118118
@Test
119119
public void nonInteger() {
120-
expect(TypeError, () -> castNode.execute("123"));
121-
expect(TypeError, () -> castNode.execute(2.7));
120+
expect(TypeError, () -> castObject("123"));
121+
expect(TypeError, () -> castObject(2.7));
122122
}
123123

124124
private static PInt makePInt(long l) {
@@ -137,4 +137,37 @@ private static void expect(PythonBuiltinClassType errorType, Runnable test) {
137137
e.expect(errorType, IsBuiltinClassProfile.getUncached());
138138
}
139139
}
140+
141+
private static long castInt(int arg) {
142+
return (Long) Truffle.getRuntime().createCallTarget(new RootNode(null) {
143+
@Child private CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode.create();
144+
145+
@Override
146+
public Object execute(VirtualFrame frame) {
147+
return castNode.execute(arg);
148+
}
149+
}).call();
150+
}
151+
152+
private static long castLong(long arg) {
153+
return (Long) Truffle.getRuntime().createCallTarget(new RootNode(null) {
154+
@Child private CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode.create();
155+
156+
@Override
157+
public Object execute(VirtualFrame frame) {
158+
return castNode.execute(arg);
159+
}
160+
}).call();
161+
}
162+
163+
private static long castObject(Object arg) {
164+
return (Long) Truffle.getRuntime().createCallTarget(new RootNode(null) {
165+
@Child private CastToJavaUnsignedLongNode castNode = CastToJavaUnsignedLongNode.create();
166+
167+
@Override
168+
public Object execute(VirtualFrame frame) {
169+
return castNode.execute(arg);
170+
}
171+
}).call();
172+
}
140173
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/util/CastToJavaUnsignedLongNode.java

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,9 @@
4747
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
4848
import com.oracle.truffle.api.CompilerDirectives;
4949
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
50-
import com.oracle.truffle.api.dsl.Cached;
51-
import com.oracle.truffle.api.dsl.Cached.Shared;
5250
import com.oracle.truffle.api.dsl.Fallback;
53-
import com.oracle.truffle.api.dsl.GenerateUncached;
5451
import com.oracle.truffle.api.dsl.Specialization;
5552
import com.oracle.truffle.api.dsl.TypeSystemReference;
56-
import com.oracle.truffle.api.profiles.ConditionProfile;
5753

5854
import java.math.BigInteger;
5955

@@ -71,57 +67,61 @@
7167
* Note that since Java {@code long} is signed, the values in the between 2^63 and 2^64-1 are
7268
* returned as negative numbers.
7369
*/
74-
@GenerateUncached
7570
@TypeSystemReference(PythonArithmeticTypes.class)
7671
public abstract class CastToJavaUnsignedLongNode extends PNodeWithContext {
7772

73+
@Child private PRaiseNode raiseNode;
74+
7875
public static CastToJavaUnsignedLongNode create() {
7976
return CastToJavaUnsignedLongNodeGen.create();
8077
}
8178

82-
public static CastToJavaUnsignedLongNode getUncached() {
83-
return CastToJavaUnsignedLongNodeGen.getUncached();
84-
}
85-
8679
public abstract long execute(int x);
8780

8881
public abstract long execute(long x);
8982

9083
public abstract long execute(Object x);
9184

9285
@Specialization
93-
public long toUnsignedLong(long x,
94-
@Cached @Shared("negative") ConditionProfile negativeProfile) {
95-
checkNegative(x < 0, negativeProfile);
86+
long toUnsignedLong(long x) {
87+
checkNegative(x < 0);
9688
return x;
9789
}
9890

9991
@Specialization
100-
protected long toUnsignedLong(PInt x,
101-
@Cached @Shared("negative") ConditionProfile negativeProfile) {
102-
checkNegative(x.isNegative(), negativeProfile);
92+
long toUnsignedLong(PInt x) {
93+
checkNegative(x.isNegative());
10394
return convertBigInt(x.getValue());
10495
}
10596

10697
@Fallback
107-
static long doUnsupported(@SuppressWarnings("unused") Object x) {
108-
CompilerDirectives.transferToInterpreter();
109-
throw PRaiseNode.getUncached().raise(TypeError, ErrorMessages.INTEGER_IS_REQUIRED);
98+
long doUnsupported(@SuppressWarnings("unused") Object x) {
99+
throw getRaiseNode().raise(TypeError, ErrorMessages.INTEGER_IS_REQUIRED);
110100
}
111101

112-
private static void checkNegative(boolean negative, ConditionProfile profile) {
113-
if (profile.profile(negative)) {
114-
CompilerDirectives.transferToInterpreter();
115-
throw PRaiseNode.getUncached().raise(OverflowError, ErrorMessages.CANNOT_CONVERT_NEGATIVE_VALUE_TO_UNSIGNED_INT);
102+
private void checkNegative(boolean negative) {
103+
if (negative) {
104+
throw getRaiseNode().raise(OverflowError, ErrorMessages.CANNOT_CONVERT_NEGATIVE_VALUE_TO_UNSIGNED_INT);
116105
}
117106
}
118107

119108
@TruffleBoundary
120-
private static long convertBigInt(BigInteger bi) {
109+
private long convertBigInt(BigInteger bi) {
121110
if (bi.bitLength() > 64) {
122-
CompilerDirectives.transferToInterpreter();
123-
throw PRaiseNode.getUncached().raise(OverflowError, ErrorMessages.PYTHON_INT_TOO_LARGE_TO_CONV_TO, "unsigned long");
111+
throw getRaiseNode().raise(OverflowError, ErrorMessages.PYTHON_INT_TOO_LARGE_TO_CONV_TO, "unsigned long");
124112
}
125113
return bi.longValue();
126114
}
115+
116+
private PRaiseNode getRaiseNode() {
117+
if (raiseNode == null) {
118+
CompilerDirectives.transferToInterpreterAndInvalidate();
119+
if (isAdoptable()) {
120+
raiseNode = insert(PRaiseNode.create());
121+
} else {
122+
raiseNode = PRaiseNode.getUncached();
123+
}
124+
}
125+
return raiseNode;
126+
}
127127
}

0 commit comments

Comments
 (0)