Skip to content

Commit 2cfa186

Browse files
committed
Avoid eager access to thread state for indirect calls
1 parent 0a6a89d commit 2cfa186

19 files changed

+124
-133
lines changed

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

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@
242242
import com.oracle.graal.python.parser.PythonSSTNodeFactory;
243243
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
244244
import com.oracle.graal.python.runtime.PythonContext;
245-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
246245
import com.oracle.graal.python.runtime.PythonCore;
247246
import com.oracle.graal.python.runtime.exception.PException;
248247
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
@@ -2427,8 +2426,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, PythonLanguage language, S
24272426
}
24282427
// Make slots into a tuple
24292428
}
2430-
PythonThreadState threadState = getContextRef().get().getThreadState(language);
2431-
Object state = IndirectCallContext.enter(frame, threadState, this);
2429+
Object state = IndirectCallContext.enter(frame, language, getContextRef(), this);
24322430
try {
24332431
pythonClass.setAttribute(__SLOTS__, slotsObject);
24342432
if (basesArray.length > 1) {
@@ -2444,7 +2442,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, PythonLanguage language, S
24442442
addNativeSlots(pythonClass, newSlots);
24452443
}
24462444
} finally {
2447-
IndirectCallContext.exit(frame, threadState, state);
2445+
IndirectCallContext.exit(frame, language, getContextRef(), state);
24482446
}
24492447
Object dict = LookupAttributeInMRONode.lookupSlowPath(pythonClass, __DICT__);
24502448
if (!addDict && dict == PNone.NO_VALUE) {
@@ -3393,14 +3391,13 @@ PMemoryView fromNative(VirtualFrame frame, @SuppressWarnings("unused") Object cl
33933391
@Cached PCallCapiFunction callCapiFunction,
33943392
@Cached PythonCextBuiltins.DefaultCheckFunctionResultNode checkFunctionResultNode) {
33953393
PythonContext context = getContext();
3396-
PythonThreadState threadState = context.getThreadState(language);
3397-
Object state = IndirectCallContext.enter(frame, threadState, this);
3394+
Object state = IndirectCallContext.enter(frame, language, context, this);
33983395
try {
33993396
Object result = callCapiFunction.call(FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT, toSulongNode.execute(object));
34003397
checkFunctionResultNode.execute(context, FUN_PY_TRUFFLE_MEMORYVIEW_FROM_OBJECT.getName(), result);
34013398
return (PMemoryView) asPythonObjectNode.execute(result);
34023399
} finally {
3403-
IndirectCallContext.exit(frame, threadState, state);
3400+
IndirectCallContext.exit(frame, language, getContextRef(), state);
34043401
}
34053402
}
34063403

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,6 @@
182182
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
183183
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
184184
import com.oracle.graal.python.runtime.PythonContext;
185-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
186185
import com.oracle.graal.python.runtime.PythonCore;
187186
import com.oracle.graal.python.runtime.PythonOptions;
188187
import com.oracle.graal.python.runtime.PythonParser.ParserMode;
@@ -1101,16 +1100,15 @@ final boolean doRecursiveWithNode(VirtualFrame frame, Object instance, PTuple cl
11011100
final boolean doRecursiveWithLoop(VirtualFrame frame, Object instance, PTuple clsTuple,
11021101
@CachedLanguage PythonLanguage language,
11031102
@Cached("createNonRecursive()") RecursiveBinaryCheckBaseNode node) {
1104-
PythonThreadState threadState = getContext().getThreadState(language);
1105-
Object state = IndirectCallContext.enter(frame, threadState, this);
1103+
Object state = IndirectCallContext.enter(frame, language, getContextRef(), this);
11061104
try {
11071105
// Note: we need actual recursion to trigger the stack overflow error like CPython
11081106
// Note: we need fresh RecursiveBinaryCheckBaseNode and cannot use "this", because
11091107
// children of this executed by other specializations may assume they'll always get
11101108
// non-null frame
11111109
return callRecursiveWithNodeTruffleBoundary(instance, clsTuple, node);
11121110
} finally {
1113-
IndirectCallContext.exit(frame, threadState, state);
1111+
IndirectCallContext.exit(frame, language, getContextRef(), state);
11141112
}
11151113
}
11161114

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@
5757
import com.oracle.graal.python.nodes.statement.AbstractImportNode;
5858
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
5959
import com.oracle.graal.python.runtime.PythonContext;
60-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
6160
import com.oracle.graal.python.runtime.PythonOptions;
6261
import com.oracle.graal.python.runtime.exception.ExceptionUtils;
6362
import com.oracle.graal.python.runtime.exception.PException;
@@ -99,13 +98,12 @@ abstract static class DumpTracebackNode extends PythonClinicBuiltinNode {
9998
PNone doit(VirtualFrame frame, Object file, boolean allThreads,
10099
@CachedLanguage PythonLanguage language) {
101100
PythonContext context = getContext();
102-
PythonThreadState threadState = context.getThreadState(language);
103-
Object state = IndirectCallContext.enter(frame, threadState, this);
101+
Object state = IndirectCallContext.enter(frame, language, context, this);
104102
try {
105103
// it's not important for this to be fast at all
106104
dump(language, context, file, allThreads);
107105
} finally {
108-
IndirectCallContext.exit(frame, threadState, state);
106+
IndirectCallContext.exit(frame, language, context, state);
109107
}
110108
return PNone.NONE;
111109
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,6 @@
9292
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
9393
import com.oracle.graal.python.runtime.GilNode;
9494
import com.oracle.graal.python.runtime.PythonContext;
95-
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
96-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
9795
import com.oracle.graal.python.runtime.PythonCore;
9896
import com.oracle.graal.python.runtime.PythonOptions;
9997
import com.oracle.graal.python.runtime.exception.PException;
@@ -106,6 +104,7 @@
106104
import com.oracle.truffle.api.TruffleLogger;
107105
import com.oracle.truffle.api.dsl.Cached;
108106
import com.oracle.truffle.api.dsl.CachedContext;
107+
import com.oracle.truffle.api.dsl.CachedLanguage;
109108
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
110109
import com.oracle.truffle.api.dsl.NodeFactory;
111110
import com.oracle.truffle.api.dsl.Specialization;
@@ -295,10 +294,9 @@ PException reraise(PConstructAndRaiseNode raiseNode, VirtualFrame frame) {
295294

296295
@Specialization
297296
Object run(VirtualFrame frame, PythonObject moduleSpec, @SuppressWarnings("unused") Object filename,
298-
@Cached GetThreadStateNode getThreadStateNode) {
299-
PythonContext context = getContextRef().get();
300-
PythonThreadState threadState = getThreadStateNode.execute(context);
301-
Object state = IndirectCallContext.enter(frame, threadState, this);
297+
@CachedLanguage PythonLanguage language) {
298+
PythonContext context = getContext();
299+
Object state = IndirectCallContext.enter(frame, language, context, this);
302300
try {
303301
return run(moduleSpec, context);
304302
} catch (ApiInitException ie) {
@@ -308,7 +306,7 @@ Object run(VirtualFrame frame, PythonObject moduleSpec, @SuppressWarnings("unuse
308306
} catch (IOException e) {
309307
throw getConstructAndRaiseNode().raiseOSError(frame, e);
310308
} finally {
311-
IndirectCallContext.exit(frame, threadState, state);
309+
IndirectCallContext.exit(frame, language, context, state);
312310
}
313311
}
314312

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,15 +79,17 @@
7979
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
8080
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
8181
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
82-
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
83-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
82+
import com.oracle.graal.python.runtime.PythonContext;
8483
import com.oracle.graal.python.util.PythonUtils;
8584
import com.oracle.truffle.api.Assumption;
8685
import com.oracle.truffle.api.CompilerAsserts;
8786
import com.oracle.truffle.api.CompilerDirectives;
8887
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
8988
import com.oracle.truffle.api.Truffle;
89+
import com.oracle.truffle.api.TruffleLanguage.ContextReference;
9090
import com.oracle.truffle.api.dsl.Cached;
91+
import com.oracle.truffle.api.dsl.CachedContext;
92+
import com.oracle.truffle.api.dsl.CachedLanguage;
9193
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
9294
import com.oracle.truffle.api.dsl.NodeFactory;
9395
import com.oracle.truffle.api.dsl.Specialization;
@@ -581,15 +583,15 @@ private Object readObjectBoundary(byte[] dataBytes) {
581583

582584
@Specialization
583585
Object readObject(VirtualFrame frame, byte[] dataBytes, @SuppressWarnings("unused") int version,
584-
@Cached GetThreadStateNode getThreadStateNode) {
585-
PythonThreadState threadState = getThreadStateNode.execute();
586-
Object state = IndirectCallContext.enter(frame, threadState, this);
586+
@CachedLanguage PythonLanguage language,
587+
@CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
588+
Object state = IndirectCallContext.enter(frame, language, contextRef, this);
587589
try {
588590
return readObjectBoundary(dataBytes);
589591
} catch (BufferUnderflowException e) {
590592
throw raise(EOFError, "EOF read where not expected");
591593
} finally {
592-
IndirectCallContext.exit(frame, threadState, state);
594+
IndirectCallContext.exit(frame, language, contextRef, state);
593595
}
594596
}
595597

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@
241241
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
242242
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
243243
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
244-
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
245244
import com.oracle.graal.python.runtime.PythonContext;
246245
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
247246
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
@@ -2588,19 +2587,12 @@ private void decodeUTF8(CharBuffer resultBuffer, ByteBuffer inputBuffer, String
25882587

25892588
@Builtin(name = "PyTruffle_IsSequence", minNumOfPositionalArgs = 1)
25902589
@GenerateNodeFactory
2591-
abstract static class PyTruffle_IsSequence extends PythonUnaryBuiltinNode {
2590+
abstract static class PyTruffleIsSequence extends PythonUnaryBuiltinNode {
25922591

25932592
@Specialization(limit = "1")
2594-
boolean doGeneric(VirtualFrame frame, Object object,
2595-
@Shared("getThreadStateNode") @Cached GetThreadStateNode getThreadStateNode,
2593+
static boolean doGeneric(Object object,
25962594
@CachedLibrary("object") PythonObjectLibrary dataModelLibrary) {
2597-
PythonThreadState threadState = getThreadStateNode.execute(getContext());
2598-
Object state = IndirectCallContext.enter(frame, threadState, this);
2599-
try {
2600-
return dataModelLibrary.isSequence(object);
2601-
} finally {
2602-
IndirectCallContext.exit(frame, threadState, state);
2603-
}
2595+
return dataModelLibrary.isSequence(object);
26042596
}
26052597
}
26062598

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,13 @@
5959
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
6060
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
6161
import com.oracle.graal.python.runtime.PythonContext;
62-
import com.oracle.graal.python.runtime.PythonContext.GetThreadStateNode;
63-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
6462
import com.oracle.graal.python.runtime.PythonCore;
6563
import com.oracle.graal.python.runtime.PythonOptions;
6664
import com.oracle.truffle.api.CompilerDirectives;
6765
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6866
import com.oracle.truffle.api.dsl.Cached;
6967
import com.oracle.truffle.api.dsl.CachedContext;
68+
import com.oracle.truffle.api.dsl.CachedLanguage;
7069
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7170
import com.oracle.truffle.api.dsl.NodeFactory;
7271
import com.oracle.truffle.api.dsl.Specialization;
@@ -208,18 +207,17 @@ abstract static class TRegexCallExec extends PythonTernaryBuiltinNode {
208207

209208
@Specialization(limit = "1")
210209
Object call(VirtualFrame frame, Object callable, Object inputStringOrBytes, Number fromIndex,
210+
@CachedLanguage PythonLanguage language,
211211
@Cached BranchProfile typeError,
212-
@CachedLibrary("callable") InteropLibrary interop,
213-
@Cached GetThreadStateNode getThreadStateNode) {
214-
PythonThreadState threadState = getThreadStateNode.execute();
215-
Object state = IndirectCallContext.enter(frame, threadState, this);
212+
@CachedLibrary("callable") InteropLibrary interop) {
213+
Object state = IndirectCallContext.enter(frame, language, getContextRef(), this);
216214
try {
217215
return interop.execute(callable, inputStringOrBytes, fromIndex);
218216
} catch (ArityException | UnsupportedTypeException | UnsupportedMessageException e) {
219217
typeError.enter();
220-
throw raise(TypeError, "%s", e);
218+
throw raise(TypeError, "%m", e);
221219
} finally {
222-
IndirectCallContext.exit(frame, threadState, state);
220+
IndirectCallContext.exit(frame, language, getContextRef(), state);
223221
}
224222
}
225223
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@
8282
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
8383
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
8484
import com.oracle.graal.python.runtime.PythonContext;
85-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
8685
import com.oracle.graal.python.runtime.PythonCore;
8786
import com.oracle.graal.python.runtime.PythonOptions;
8887
import com.oracle.graal.python.runtime.exception.PException;
@@ -667,12 +666,11 @@ private void warnExplicit(VirtualFrame frame, PythonModule warnings,
667666

668667
// the rest of this function is behind a TruffleBoundary, since we don't care so much
669668
// about performance when warnings are enabled.
670-
PythonThreadState threadState = getContext().getThreadState(getLanguage());
671-
Object state = IndirectCallContext.enter(frame, threadState, this);
669+
Object state = IndirectCallContext.enter(frame, getLanguage(), getContext(), this);
672670
try {
673671
warnExplicitPart2(this, warnings, filename, lineno, registry, globals, source, category, message, text, key, item, action);
674672
} finally {
675-
IndirectCallContext.exit(frame, threadState, state);
673+
IndirectCallContext.exit(frame, getLanguage(), getContext(), state);
676674
}
677675
}
678676

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@
7676
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
7777
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
7878
import com.oracle.graal.python.runtime.PythonContext;
79-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
8079
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
8180
import com.oracle.truffle.api.CompilerAsserts;
8281
import com.oracle.truffle.api.CompilerDirectives;
@@ -488,12 +487,11 @@ Object doGeneric(VirtualFrame frame, Object self, Object value) {
488487
CApiContext cApiContext = context.getCApiContext();
489488
// The conversion to a native primitive may call arbitrary user code. So we need
490489
// to prepare an indirect call.
491-
PythonThreadState threadState = context.getThreadState(getLanguage());
492-
Object savedState = IndirectCallContext.enter(frame, threadState, this);
490+
Object savedState = IndirectCallContext.enter(frame, getLanguage(), context, this);
493491
try {
494492
nativeValue = toNativeNode.execute(cApiContext, newValue);
495493
} finally {
496-
IndirectCallContext.exit(frame, threadState, savedState);
494+
IndirectCallContext.exit(frame, getLanguage(), context, savedState);
497495
}
498496
} else {
499497
nativeValue = newValue;

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,6 @@
120120
import com.oracle.graal.python.nodes.object.GetClassNode;
121121
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
122122
import com.oracle.graal.python.runtime.PythonContext;
123-
import com.oracle.graal.python.runtime.PythonContext.PythonThreadState;
124123
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
125124
import com.oracle.graal.python.util.PythonUtils;
126125
import com.oracle.truffle.api.CompilerAsserts;
@@ -530,12 +529,11 @@ Object doGeneric(VirtualFrame frame, Object self, Object value) {
530529
if (toNativeNode != null) {
531530
// The conversion to a native primitive may call arbitrary user code. So we need
532531
// to prepare an indirect call.
533-
PythonThreadState threadState = context.getThreadState(getLanguage());
534-
Object savedState = IndirectCallContext.enter(frame, threadState, this);
532+
Object savedState = IndirectCallContext.enter(frame, getLanguage(), context, this);
535533
try {
536534
nativeValue = toNativeNode.execute(hPyContext, newValue);
537535
} finally {
538-
IndirectCallContext.exit(frame, threadState, savedState);
536+
IndirectCallContext.exit(frame, getLanguage(), context, savedState);
539537
}
540538
} else {
541539
nativeValue = newValue;

0 commit comments

Comments
 (0)