Skip to content

Commit fdb78f2

Browse files
committed
[GR-55079] Fix some usages of transferToInterpreter()
* transferToInterpreter() cannot be moved by the compiler, so should be avoided. * transferToInterpreterAndInvalidate() is better if it should never happen or leads to an internal error.
1 parent fe06e1f commit fdb78f2

16 files changed

+32
-42
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/AtexitModuleBuiltins.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,6 @@
6565
import com.oracle.graal.python.runtime.PythonContextFactory.GetThreadStateNodeGen;
6666
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
6767
import com.oracle.graal.python.runtime.exception.PException;
68-
import com.oracle.truffle.api.CompilerDirectives;
6968
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7069
import com.oracle.truffle.api.RootCallTarget;
7170
import com.oracle.truffle.api.TruffleLanguage;
@@ -131,9 +130,9 @@ private static void handleException(PythonContext context, PException e) {
131130
}
132131
}
133132

133+
@TruffleBoundary
134134
@Specialization
135135
Object register(Object callable, Object[] arguments, PKeyword[] keywords) {
136-
CompilerDirectives.transferToInterpreter();
137136
RootCallTarget callTarget = getLanguage().createCachedCallTarget(AtExitRootNode::new, AtExitRootNode.class);
138137
getContext().registerAtexitHook(callable, arguments, keywords, callTarget);
139138
return callable;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/BuiltinFunctions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ static TruffleString doO(VirtualFrame frame, Node inliningTarget, Object x, Truf
642642
isPInt.enter(inliningTarget);
643643
return doPI((PInt) index, prefix, radix, longToString, fromJavaStringNode);
644644
} else {
645-
CompilerDirectives.transferToInterpreter();
645+
CompilerDirectives.transferToInterpreterAndInvalidate();
646646
throw PRaiseNode.raiseUncached(inliningTarget, PythonBuiltinClassType.NotImplementedError, toTruffleStringUncached("bin/oct/hex with native integer subclasses"));
647647
}
648648
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/MathModuleBuiltins.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1121,7 +1121,7 @@ static Object gcd(VirtualFrame frame, Object x, Object y,
11211121

11221122
@Specialization
11231123
Object gcdNative(@SuppressWarnings("unused") PythonAbstractNativeObject a, @SuppressWarnings("unused") Object b) {
1124-
CompilerDirectives.transferToInterpreter();
1124+
CompilerDirectives.transferToInterpreterAndInvalidate();
11251125
throw PRaiseNode.raiseUncached(this, SystemError, ErrorMessages.GCD_FOR_NATIVE_NOT_SUPPORTED);
11261126
}
11271127

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextBuiltins.java

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ protected final CApiContext getCApiContext() {
367367
}
368368

369369
protected final PException badInternalCall(String argName) {
370-
CompilerDirectives.transferToInterpreter();
370+
CompilerDirectives.transferToInterpreterAndInvalidate();
371371
throw PRaiseNode.raiseUncached(this, SystemError, ErrorMessages.S_S_BAD_ARG_TO_INTERNAL_FUNC, getName(), argName);
372372
}
373373

@@ -600,8 +600,7 @@ public CApiBuiltinExecutable(String name, CApiCallPath call, ArgDescriptor ret,
600600

601601
CallTarget getCallTarget() {
602602
if (callTarget == null) {
603-
CompilerDirectives.transferToInterpreter();
604-
CompilerDirectives.shouldNotReachHere("call target slow path not implemented");
603+
throw CompilerDirectives.shouldNotReachHere("call target slow path not implemented");
605604
}
606605
return callTarget;
607606
}
@@ -656,15 +655,15 @@ public static Object doExecute(@SuppressWarnings("unused") CApiBuiltinExecutable
656655
CompilerDirectives.transferToInterpreter();
657656
throw t;
658657
} catch (Throwable t) {
659-
CompilerDirectives.transferToInterpreter();
658+
CompilerDirectives.transferToInterpreterAndInvalidate();
660659
t.printStackTrace();
661660
throw CompilerDirectives.shouldNotReachHere(t);
662661
}
663662
}
664663

665664
@Specialization
666665
public static Object doFallback(@SuppressWarnings("unused") CApiBuiltinExecutable self, @SuppressWarnings("unused") Object[] arguments) {
667-
CompilerDirectives.transferToInterpreter();
666+
CompilerDirectives.transferToInterpreterAndInvalidate();
668667
throw CompilerDirectives.shouldNotReachHere("shouldn't hit generic case of " + Execute.class.getName());
669668
}
670669
}
@@ -824,7 +823,7 @@ Object execute(CApiBuiltinExecutable self, Object[] arguments) {
824823
} else if (cachedSelf.getRetDescriptor().isVoid()) {
825824
return PNone.NO_VALUE;
826825
} else {
827-
CompilerDirectives.transferToInterpreter();
826+
CompilerDirectives.transferToInterpreterAndInvalidate();
828827
throw CompilerDirectives.shouldNotReachHere("return type while handling PException: " + cachedSelf.getRetDescriptor() + " in " + self.name);
829828
}
830829
} finally {
@@ -1412,8 +1411,7 @@ static boolean traceCalls(PythonContext context) {
14121411

14131412
@SuppressWarnings("unused")
14141413
protected void trace(PythonContext context, Object ptr, Reference ref, TruffleString className) {
1415-
CompilerDirectives.transferToInterpreter();
1416-
throw new IllegalStateException("should not reach");
1414+
throw CompilerDirectives.shouldNotReachHere();
14171415
}
14181416
}
14191417

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/cext/PythonCextTypeBuiltins.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,8 +247,7 @@ int trace(Object ptr, Object classNameObj,
247247
try {
248248
className = switchEncodingNode.execute(nameLib.asTruffleString(classNameObj), TS_ENCODING);
249249
} catch (UnsupportedMessageException e) {
250-
CompilerDirectives.transferToInterpreter();
251-
throw new IllegalStateException();
250+
throw CompilerDirectives.shouldNotReachHere(e);
252251
}
253252
} else {
254253
className = null;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/PythonAbstractObject.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@
153153
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
154154
import com.oracle.graal.python.util.OverflowException;
155155
import com.oracle.graal.python.util.PythonUtils;
156-
import com.oracle.truffle.api.CompilerDirectives;
157156
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
158157
import com.oracle.truffle.api.TruffleLanguage;
159158
import com.oracle.truffle.api.dsl.Bind;
@@ -180,6 +179,7 @@
180179
import com.oracle.truffle.api.nodes.Node;
181180
import com.oracle.truffle.api.object.DynamicObject;
182181
import com.oracle.truffle.api.object.Shape;
182+
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
183183
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
184184
import com.oracle.truffle.api.strings.TruffleString;
185185
import com.oracle.truffle.api.strings.TruffleString.RegionEqualNode;
@@ -1364,11 +1364,13 @@ public Keys(Object[] keys) {
13641364
}
13651365

13661366
@ExportMessage
1367-
Object readArrayElement(long index) throws InvalidArrayIndexException {
1368-
try {
1367+
Object readArrayElement(long index,
1368+
@Cached InlinedBranchProfile outOfBoundsProfile,
1369+
@Bind("$node") Node inliningTarget) throws InvalidArrayIndexException {
1370+
if (Long.compareUnsigned(index, keys.length) < 0) {
13691371
return keys[(int) index];
1370-
} catch (IndexOutOfBoundsException e) {
1371-
CompilerDirectives.transferToInterpreter();
1372+
} else {
1373+
outOfBoundsProfile.enter(inliningTarget);
13721374
throw InvalidArrayIndexException.create(index);
13731375
}
13741376
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/PythonAbstractNativeObject.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,8 +102,7 @@ public int compareTo(Object o) {
102102

103103
public void lookupChanged() {
104104
// TODO invalidate cached native MRO
105-
CompilerDirectives.transferToInterpreter();
106-
throw new UnsupportedOperationException("not yet implemented");
105+
throw CompilerDirectives.shouldNotReachHere("not yet implemented");
107106
}
108107

109108
@Override

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CExtNodes.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@
7070
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
7171
import static com.oracle.graal.python.util.PythonUtils.toTruffleStringUncached;
7272
import static com.oracle.truffle.api.CompilerDirectives.shouldNotReachHere;
73-
import static com.oracle.truffle.api.CompilerDirectives.transferToInterpreter;
73+
import static com.oracle.truffle.api.CompilerDirectives.transferToInterpreterAndInvalidate;
7474

7575
import java.util.regex.Matcher;
7676
import java.util.regex.Pattern;
@@ -2144,7 +2144,7 @@ private static PythonObject castToPythonObject(Object callable) {
21442144
if (callable instanceof PythonObject pythonObject) {
21452145
return pythonObject;
21462146
}
2147-
transferToInterpreter();
2147+
transferToInterpreterAndInvalidate();
21482148
throw shouldNotReachHere("Unexpected class of callable: " + callable.getClass());
21492149
}
21502150

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/FunctionBuiltins.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,6 @@
7878
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
7979
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
8080
import com.oracle.graal.python.util.PythonUtils;
81-
import com.oracle.truffle.api.CompilerDirectives;
8281
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
8382
import com.oracle.truffle.api.dsl.Bind;
8483
import com.oracle.truffle.api.dsl.Cached;
@@ -230,7 +229,6 @@ static Object set(PFunction self, @SuppressWarnings("unused") PNone arg) {
230229
@Specialization(guards = "!isNoValue(arg)")
231230
@TruffleBoundary
232231
Object set(PFunction self, PDict arg) {
233-
CompilerDirectives.transferToInterpreter();
234232
ArrayList<PKeyword> keywords = new ArrayList<>();
235233
final HashingStorage storage = arg.getDictStorage();
236234
HashingStorageIterator it = HashingStorageGetIterator.executeUncached(storage);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/slice/PObjectSlice.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2020, 2021, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -50,7 +50,6 @@
5050
import com.oracle.graal.python.nodes.PRaiseNode;
5151
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
5252
import com.oracle.truffle.api.CompilerAsserts;
53-
import com.oracle.truffle.api.CompilerDirectives;
5453
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
5554
import com.oracle.truffle.api.CompilerDirectives.ValueType;
5655

@@ -135,7 +134,6 @@ public static SliceObjectInfo computeIndicesSlowPath(PObjectSlice slice, Object
135134
BigInteger start, stop, step, length;
136135
length = (BigInteger) lengthIn;
137136
if (pySign(length) < 0) {
138-
CompilerDirectives.transferToInterpreter();
139137
throw PRaiseNode.raiseUncached(null, ValueError, ErrorMessages.LENGTH_SHOULD_NOT_BE_NEG);
140138
}
141139
if (slice.getStep() == PNone.NONE) {
@@ -145,7 +143,6 @@ public static SliceObjectInfo computeIndicesSlowPath(PObjectSlice slice, Object
145143
step = (BigInteger) slice.getStep();
146144
stepIsNegative = pySign(step) < 0;
147145
if (pySign(step) == 0) {
148-
CompilerDirectives.transferToInterpreter();
149146
throw PRaiseNode.raiseUncached(null, ValueError, ErrorMessages.SLICE_STEP_CANNOT_BE_ZERO);
150147
}
151148
}

0 commit comments

Comments
 (0)