Skip to content

Commit 2fed075

Browse files
committed
[GR-23089] Fix deopt loops.
PullRequest: graalpython/978
2 parents 797068c + 1ae4696 commit 2fed075

22 files changed

+253
-132
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
import com.oracle.graal.python.runtime.PythonContext;
7373
import com.oracle.graal.python.runtime.PythonCore;
7474
import com.oracle.graal.python.runtime.exception.PException;
75+
import com.oracle.graal.python.util.OverflowException;
7576
import com.oracle.truffle.api.CompilerDirectives;
7677
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
7778
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -461,7 +462,7 @@ abstract static class IsCheckSupportedNode extends PythonUnaryBuiltinNode {
461462

462463
@Specialization
463464
@TruffleBoundary
464-
boolean doInt(int checkID) {
465+
static boolean doInt(int checkID) {
465466
try {
466467
return Check.getInstance(checkID) != null;
467468
} catch (UnsupportedOptionsException e) {
@@ -470,16 +471,16 @@ boolean doInt(int checkID) {
470471
}
471472

472473
@Specialization
473-
boolean doLong(long checkID) {
474+
static boolean doLong(long checkID) {
474475
try {
475476
return doInt(PInt.intValueExact(checkID));
476-
} catch (ArithmeticException e) {
477+
} catch (OverflowException e) {
477478
return false;
478479
}
479480
}
480481

481482
@Specialization
482-
boolean doLong(PInt checkID) {
483+
static boolean doLong(PInt checkID) {
483484
try {
484485
return doInt(checkID.intValueExact());
485486
} catch (ArithmeticException e) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
6464
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
6565
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
66+
import com.oracle.graal.python.util.OverflowException;
6667
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6768
import com.oracle.truffle.api.TruffleFile;
6869
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -121,7 +122,7 @@ PMMap doFile(LazyPythonClass clazz, long fd, long length, @SuppressWarnings("unu
121122
int ifd;
122123
try {
123124
ifd = PInt.intValueExact(fd);
124-
} catch (ArithmeticException e) {
125+
} catch (OverflowException e) {
125126
throw raise(ValueError, ErrorMessages.INVALID_FILE_DESCRIPTOR);
126127
}
127128

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

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,8 +71,8 @@
7171
import com.oracle.graal.python.builtins.modules.BuiltinConstructors.IntNode;
7272
import com.oracle.graal.python.builtins.modules.BuiltinConstructorsFactory.IntNodeFactory;
7373
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.AllocFuncRootNode;
74-
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethDirectRoot;
7574
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.GetAttrFuncRootNode;
75+
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethDirectRoot;
7676
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethFastcallRoot;
7777
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethFastcallWithKeywordsRoot;
7878
import com.oracle.graal.python.builtins.modules.ExternalFunctionNodes.MethKeywordsRoot;
@@ -243,6 +243,7 @@
243243
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
244244
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
245245
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
246+
import com.oracle.graal.python.util.OverflowException;
246247
import com.oracle.graal.python.util.Supplier;
247248
import com.oracle.truffle.api.CompilerAsserts;
248249
import com.oracle.truffle.api.CompilerDirectives;
@@ -629,11 +630,11 @@ public Object run(Object module,
629630
abstract static class PyErrOccurred extends PythonUnaryBuiltinNode {
630631
@Specialization
631632
Object run(Object errorMarker,
632-
@Cached GetClassNode getClass) {
633+
@Cached GetClassNode getClassNode) {
633634
PException currentException = getContext().getCurrentException();
634635
if (currentException != null) {
635-
PBaseException exceptionObject = currentException.getEscapedException();
636-
return getClass.execute(exceptionObject);
636+
// getClassNode acts as a branch profile
637+
return getClassNode.execute(currentException.getExceptionObject());
637638
}
638639
return errorMarker;
639640
}
@@ -1185,12 +1186,11 @@ long doPInt4(VirtualFrame frame, PInt obj, int signed, @SuppressWarnings("unused
11851186
return obj.intValueExact();
11861187
} else if (obj.bitCount() <= 32) {
11871188
return obj.intValue();
1188-
} else {
1189-
throw new ArithmeticException();
11901189
}
11911190
} catch (ArithmeticException e) {
1192-
return raiseTooLarge(frame, targetTypeSize);
1191+
// fall through
11931192
}
1193+
return raiseTooLarge(frame, targetTypeSize);
11941194
}
11951195

11961196
@Specialization(guards = "targetTypeSize == 8")
@@ -1200,12 +1200,11 @@ long doPInt8(VirtualFrame frame, PInt obj, int signed, @SuppressWarnings("unused
12001200
return obj.longValueExact();
12011201
} else if (obj.bitCount() <= 64) {
12021202
return obj.longValue();
1203-
} else {
1204-
throw new ArithmeticException();
12051203
}
12061204
} catch (ArithmeticException e) {
1207-
return raiseTooLarge(frame, targetTypeSize);
1205+
// fall through
12081206
}
1207+
return raiseTooLarge(frame, targetTypeSize);
12091208
}
12101209

12111210
@Specialization(guards = {"targetTypeSize != 4", "targetTypeSize != 8"})
@@ -2098,8 +2097,8 @@ PBytes doInt(int size) {
20982097
return factory().createBytes(new byte[size]);
20992098
}
21002099

2101-
@Specialization(rewriteOn = ArithmeticException.class)
2102-
PBytes doLong(long size) {
2100+
@Specialization(rewriteOn = OverflowException.class)
2101+
PBytes doLong(long size) throws OverflowException {
21032102
return doInt(PInt.intValueExact(size));
21042103
}
21052104

@@ -2108,7 +2107,7 @@ PBytes doLongOvf(long size,
21082107
@Shared("raiseNode") @Cached PRaiseNode raiseNode) {
21092108
try {
21102109
return doInt(PInt.intValueExact(size));
2111-
} catch (ArithmeticException e) {
2110+
} catch (OverflowException e) {
21122111
throw raiseNode.raiseNumberTooLarge(IndexError, size);
21132112
}
21142113
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.Date;
5252
import java.util.List;
5353

54+
import com.oracle.graal.python.util.OverflowException;
5455
import org.graalvm.nativeimage.ImageInfo;
5556

5657
import com.oracle.graal.python.PythonLanguage;
@@ -357,10 +358,10 @@ PFrame counted(VirtualFrame frame, int num,
357358
return requested;
358359
}
359360

360-
@Specialization(rewriteOn = ArithmeticException.class)
361+
@Specialization(rewriteOn = OverflowException.class)
361362
PFrame countedLong(VirtualFrame frame, long num,
362363
@Shared("caller") @Cached ReadCallerFrameNode readCallerNode,
363-
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
364+
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) throws OverflowException {
364365
return counted(frame, PInt.intValueExact(num), readCallerNode, callStackDepthProfile);
365366
}
366367

@@ -370,7 +371,7 @@ PFrame countedLongOvf(VirtualFrame frame, long num,
370371
@Shared("callStackDepthProfile") @Cached("createBinaryProfile()") ConditionProfile callStackDepthProfile) {
371372
try {
372373
return counted(frame, PInt.intValueExact(num), readCallerNode, callStackDepthProfile);
373-
} catch (ArithmeticException e) {
374+
} catch (OverflowException e) {
374375
throw raiseCallStackDepth();
375376
}
376377
}

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

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@
4040
*/
4141
package com.oracle.graal.python.builtins.objects;
4242

43+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.AttributeError;
44+
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
45+
import static com.oracle.graal.python.nodes.SpecialMethodNames.FILENO;
4346
import static com.oracle.graal.python.nodes.SpecialMethodNames.__BOOL__;
4447
import static com.oracle.graal.python.nodes.SpecialMethodNames.__CALL__;
4548
import static com.oracle.graal.python.nodes.SpecialMethodNames.__DELETE__;
@@ -48,17 +51,20 @@
4851
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EQ__;
4952
import static com.oracle.graal.python.nodes.SpecialMethodNames.__EXIT__;
5053
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FLOAT__;
54+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FSPATH__;
55+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
56+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__;
5157
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETITEM__;
5258
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GET__;
5359
import static com.oracle.graal.python.nodes.SpecialMethodNames.__HASH__;
5460
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INDEX__;
61+
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INT__;
5562
import static com.oracle.graal.python.nodes.SpecialMethodNames.__ITER__;
5663
import static com.oracle.graal.python.nodes.SpecialMethodNames.__LEN__;
5764
import static com.oracle.graal.python.nodes.SpecialMethodNames.__NEXT__;
5865
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SETITEM__;
5966
import static com.oracle.graal.python.nodes.SpecialMethodNames.__SET__;
6067
import static com.oracle.graal.python.nodes.SpecialMethodNames.__STR__;
61-
import static com.oracle.graal.python.nodes.SpecialMethodNames.FILENO;
6268

6369
import java.time.LocalDate;
6470
import java.time.LocalTime;
@@ -68,8 +74,6 @@
6874

6975
import com.oracle.graal.python.PythonLanguage;
7076
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
71-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.AttributeError;
72-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.TypeError;
7377
import com.oracle.graal.python.builtins.modules.MathGuards;
7478
import com.oracle.graal.python.builtins.objects.bytes.PBytes;
7579
import com.oracle.graal.python.builtins.objects.cext.CApiGuards;
@@ -102,10 +106,6 @@
102106
import com.oracle.graal.python.nodes.PGuards;
103107
import com.oracle.graal.python.nodes.PRaiseNode;
104108
import com.oracle.graal.python.nodes.SpecialMethodNames;
105-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__FSPATH__;
106-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTRIBUTE__;
107-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__GETATTR__;
108-
import static com.oracle.graal.python.nodes.SpecialMethodNames.__INT__;
109109
import com.oracle.graal.python.nodes.attributes.HasInheritedAttributeNode;
110110
import com.oracle.graal.python.nodes.attributes.LookupAttributeInMRONode;
111111
import com.oracle.graal.python.nodes.attributes.LookupInheritedAttributeNode;
@@ -133,6 +133,7 @@
133133
import com.oracle.graal.python.runtime.PythonContext;
134134
import com.oracle.graal.python.runtime.PythonOptions;
135135
import com.oracle.graal.python.runtime.exception.PException;
136+
import com.oracle.graal.python.util.OverflowException;
136137
import com.oracle.truffle.api.CompilerDirectives;
137138
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
138139
import com.oracle.truffle.api.TruffleLanguage;
@@ -1065,7 +1066,7 @@ public int asSizeWithState(LazyPythonClass type, ThreadState state,
10651066
}
10661067
try {
10671068
return PInt.intValueExact(longResult);
1068-
} catch (ArithmeticException e) {
1069+
} catch (OverflowException e) {
10691070
overflow.enter();
10701071
if (ignoreOverflow.profile(type != null)) {
10711072
throw raise.raiseNumberTooLarge(type, result);

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2020, 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
@@ -44,6 +44,8 @@
4444

4545
import com.oracle.graal.python.builtins.objects.cext.CExtNodes.PCallCapiFunction;
4646
import com.oracle.graal.python.builtins.objects.ints.PInt;
47+
import com.oracle.graal.python.util.OverflowException;
48+
import com.oracle.truffle.api.CompilerDirectives;
4749
import com.oracle.truffle.api.dsl.Cached;
4850
import com.oracle.truffle.api.dsl.Cached.Exclusive;
4951
import com.oracle.truffle.api.interop.InteropLibrary;
@@ -134,9 +136,10 @@ final Object readArrayElement(long index,
134136
} else if (idx == s.length()) {
135137
return '\0';
136138
}
137-
} catch (ArithmeticException e) {
139+
} catch (OverflowException e) {
138140
// fall through
139141
}
142+
CompilerDirectives.transferToInterpreterAndInvalidate();
140143
throw InvalidArrayIndexException.create(index);
141144
}
142145

@@ -198,9 +201,10 @@ Object readArrayElement(long index,
198201
} else if (idx == arr.length) {
199202
return (byte) 0;
200203
}
201-
} catch (ArithmeticException e) {
204+
} catch (OverflowException e) {
202205
// fall through
203206
}
207+
CompilerDirectives.transferToInterpreterAndInvalidate();
204208
throw InvalidArrayIndexException.create(index);
205209
}
206210

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

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@
162162
import com.oracle.truffle.api.interop.UnsupportedTypeException;
163163
import com.oracle.truffle.api.library.CachedLibrary;
164164
import com.oracle.truffle.api.nodes.ExplodeLoop;
165+
import com.oracle.truffle.api.nodes.InvalidAssumptionException;
165166
import com.oracle.truffle.api.nodes.Node;
166167
import com.oracle.truffle.api.nodes.NodeUtil;
167168
import com.oracle.truffle.api.profiles.BranchProfile;
@@ -3114,25 +3115,31 @@ public abstract static class ResolveHandleNode extends Node {
31143115

31153116
@Specialization(limit = "3", //
31163117
guards = {"cachedPointer == pointer", "cachedValue != null"}, //
3117-
assumptions = {"singleContextAssumption()", "getHandleValidAssumption(cachedValue)"})
3118-
static PythonNativeWrapper doLongCachedSingleContext(@SuppressWarnings("unused") long pointer,
3118+
assumptions = "singleContextAssumption()", //
3119+
rewriteOn = InvalidAssumptionException.class)
3120+
static PythonNativeWrapper resolveLongCached(@SuppressWarnings("unused") long pointer,
31193121
@Cached("pointer") @SuppressWarnings("unused") long cachedPointer,
3120-
@Cached("resolveHandleUncached(pointer)") PythonNativeWrapper cachedValue) {
3122+
@Cached("resolveHandleUncached(pointer)") PythonNativeWrapper cachedValue,
3123+
@Cached("getHandleValidAssumption(cachedValue)") Assumption associationValidAssumption) throws InvalidAssumptionException {
3124+
associationValidAssumption.check();
31213125
return cachedValue;
31223126
}
31233127

31243128
@Specialization(limit = "3", //
31253129
guards = {"isSame(referenceLibrary, cachedPointerObject, pointerObject)", "cachedValue != null"}, //
3126-
assumptions = {"singleContextAssumption()", "getHandleValidAssumption(cachedValue)"})
3127-
static PythonNativeWrapper doObjectCachedSingleContext(@SuppressWarnings("unused") Object pointerObject,
3130+
assumptions = "singleContextAssumption()", //
3131+
rewriteOn = InvalidAssumptionException.class)
3132+
static PythonNativeWrapper resolveObjectCached(@SuppressWarnings("unused") Object pointerObject,
31283133
@Cached("pointerObject") @SuppressWarnings("unused") Object cachedPointerObject,
31293134
@CachedLibrary("cachedPointerObject") @SuppressWarnings("unused") ReferenceLibrary referenceLibrary,
3130-
@Cached("resolveHandleUncached(pointerObject)") PythonNativeWrapper cachedValue) {
3135+
@Cached("resolveHandleUncached(pointerObject)") PythonNativeWrapper cachedValue,
3136+
@Cached("getHandleValidAssumption(cachedValue)") Assumption associationValidAssumption) throws InvalidAssumptionException {
3137+
associationValidAssumption.check();
31313138
return cachedValue;
31323139
}
31333140

3134-
@Specialization(replaces = {"doObjectCachedSingleContext", "doLongCachedSingleContext"})
3135-
static Object doGeneric(Object pointerObject,
3141+
@Specialization(replaces = {"resolveLongCached", "resolveObjectCached"})
3142+
static Object resolveGeneric(Object pointerObject,
31363143
@Cached PCallCapiFunction callTruffleCannotBeHandleNode,
31373144
@Cached PCallCapiFunction callTruffleManagedFromHandleNode) {
31383145
if (!((boolean) callTruffleCannotBeHandleNode.call(NativeCAPISymbols.FUN_TRUFFLE_CANNOT_BE_HANDLE, pointerObject))) {

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
7070
import com.oracle.graal.python.runtime.PythonOptions;
7171
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
72+
import com.oracle.graal.python.util.OverflowException;
7273
import com.oracle.truffle.api.CompilerAsserts;
7374
import com.oracle.truffle.api.CompilerDirectives;
7475
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
@@ -269,7 +270,7 @@ static String doBytes(Object arr, @SuppressWarnings("unused") long elementSize,
269270
throw raiseNode.raise(ValueError, ErrorMessages.UNSUPPORTED_SIZE_WAS, "wchar_t", cachedElementSize);
270271
}
271272
return decode(bytes);
272-
} catch (ArithmeticException e) {
273+
} catch (OverflowException e) {
273274
throw raiseNode.raise(ValueError, ErrorMessages.ARRAY_SIZE_TOO_LARGE);
274275
} catch (CharacterCodingException e) {
275276
throw raiseNode.raise(UnicodeError, "%m", e);
@@ -324,7 +325,7 @@ private static int readElement(InteropLibrary arrLib, InteropLibrary elemLib, Ob
324325
if (elemLib.fitsInLong(elem)) {
325326
barr[j] = (byte) elemLib.asLong(elem);
326327
} else {
327-
CompilerDirectives.transferToInterpreter();
328+
CompilerDirectives.transferToInterpreterAndInvalidate();
328329
throw new IllegalElementTypeException(elem);
329330
}
330331
}

0 commit comments

Comments
 (0)