Skip to content

Commit cd5f4ad

Browse files
committed
[GR-19955] Add an interface for state assumptions that all nodes using the IndirectCallContext.enter method must implement
PullRequest: graalpython/757
2 parents 38c5db5 + 304c45d commit cd5f4ad

31 files changed

+810
-316
lines changed

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

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1945,27 +1945,27 @@ public abstract static class FunctionNode extends PythonBuiltinNode {
19451945

19461946
@Specialization
19471947
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, @SuppressWarnings("unused") PNone closure) {
1948-
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, null);
1948+
return factory().createFunction(name, getTypeName(cls), code, globals, null);
19491949
}
19501950

19511951
@Specialization
19521952
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, @SuppressWarnings("unused") PNone defaultArgs, PTuple closure,
19531953
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
1954-
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, (PCell[]) getObjectArrayNode.execute(closure));
1954+
return factory().createFunction(name, getTypeName(cls), code, globals, (PCell[]) getObjectArrayNode.execute(closure));
19551955
}
19561956

19571957
@Specialization
19581958
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, @SuppressWarnings("unused") PNone closure,
19591959
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
19601960
// TODO split defaults of positional args from kwDefaults
1961-
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, getObjectArrayNode.execute(defaultArgs), null, null);
1961+
return factory().createFunction(name, getTypeName(cls), code, globals, getObjectArrayNode.execute(defaultArgs), null, null);
19621962
}
19631963

19641964
@Specialization
19651965
public PFunction function(LazyPythonClass cls, PCode code, PDict globals, String name, PTuple defaultArgs, PTuple closure,
19661966
@Shared("getObjectArrayNode") @Cached GetObjectArrayNode getObjectArrayNode) {
19671967
// TODO split defaults of positional args from kwDefaults
1968-
return factory().createFunction(name, getTypeName(cls), code.getRootCallTarget(), globals, getObjectArrayNode.execute(defaultArgs), null, (PCell[]) getObjectArrayNode.execute(closure));
1968+
return factory().createFunction(name, getTypeName(cls), code, globals, getObjectArrayNode.execute(defaultArgs), null, (PCell[]) getObjectArrayNode.execute(closure));
19691969
}
19701970

19711971
@Fallback
@@ -2192,7 +2192,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
21922192
// Make slots into a tuple
21932193
}
21942194
PythonContext context = getContextRef().get();
2195-
PException caughtException = ForeignCallContext.enter(frame, context, this);
2195+
Object state = ForeignCallContext.enter(frame, context, this);
21962196
try {
21972197
PTuple newSlots = copySlots(name, slotList, slotlen, addDict, false, namespace);
21982198
pythonClass.setAttribute(__SLOTS__, newSlots);
@@ -2206,7 +2206,7 @@ private PythonClass typeMetaclass(VirtualFrame frame, String name, PTuple bases,
22062206
addNativeSlots(pythonClass, newSlots);
22072207
}
22082208
} finally {
2209-
ForeignCallContext.exit(frame, context, caughtException);
2209+
ForeignCallContext.exit(frame, context, state);
22102210
}
22112211
}
22122212

@@ -2670,15 +2670,15 @@ Object methodBuiltin(@SuppressWarnings("unused") LazyPythonClass cls, PBuiltinFu
26702670
Object methodGeneric(VirtualFrame frame, @SuppressWarnings("unused") LazyPythonClass cls, Object func, Object self,
26712671
@CachedLibrary(limit = "3") PythonObjectLibrary dataModelLibrary) {
26722672
PythonContext context = getContextRef().get();
2673-
PException caughtException = IndirectCallContext.enter(frame, context, this);
2673+
Object state = IndirectCallContext.enter(frame, context, this);
26742674
try {
26752675
if (dataModelLibrary.isCallable(func)) {
26762676
return factory().createMethod(self, func);
26772677
} else {
26782678
throw raise(TypeError, "first argument must be callable");
26792679
}
26802680
} finally {
2681-
IndirectCallContext.exit(frame, context, caughtException);
2681+
IndirectCallContext.exit(frame, context, state);
26822682
}
26832683
}
26842684
}
@@ -2822,11 +2822,11 @@ protected boolean isBuiltinMapping(Object o) {
28222822

28232823
protected boolean isSequence(VirtualFrame frame, Object o, PythonObjectLibrary library) {
28242824
PythonContext context = getContextRef().get();
2825-
PException caughtException = IndirectCallContext.enter(frame, context, this);
2825+
Object state = IndirectCallContext.enter(frame, context, this);
28262826
try {
28272827
return library.isSequence(o);
28282828
} finally {
2829-
IndirectCallContext.exit(frame, context, caughtException);
2829+
IndirectCallContext.exit(frame, context, state);
28302830
}
28312831
}
28322832
}

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1863,10 +1863,9 @@ public boolean visit(Node node) {
18631863
});
18641864

18651865
String name = func.getName();
1866-
builtinFunc =
1867-
1868-
factory().createFunction(name, func.getEnclosingClassName(), Truffle.getRuntime().createCallTarget(functionRootNode),
1869-
func.getGlobals(), func.getDefaults(), func.getKwDefaults(), func.getClosure());
1866+
builtinFunc = factory().createFunction(name, func.getEnclosingClassName(),
1867+
new PCode(PythonBuiltinClassType.PCode, Truffle.getRuntime().createCallTarget(functionRootNode)),
1868+
func.getGlobals(), func.getDefaults(), func.getKwDefaults(), func.getClosure());
18701869
}
18711870

18721871
return builtinFunc;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,11 +176,11 @@ public abstract static class CreateDynamic extends PythonBuiltinNode {
176176
public Object run(VirtualFrame frame, PythonObject moduleSpec, @SuppressWarnings("unused") Object filename,
177177
@CachedLibrary(limit = "1") InteropLibrary interop) {
178178
PythonContext context = getContextRef().get();
179-
PException caughtException = ForeignCallContext.enter(frame, context, this);
179+
Object state = ForeignCallContext.enter(frame, context, this);
180180
try {
181181
return run(moduleSpec, interop);
182182
} finally {
183-
ForeignCallContext.exit(frame, context, caughtException);
183+
ForeignCallContext.exit(frame, context, state);
184184
}
185185
}
186186

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ private static FilterOptions createFilterById(int id) {
254254

255255
private boolean isSequence(VirtualFrame frame, ContextReference<PythonContext> contextRef, Object obj, PythonObjectLibrary library) {
256256
PythonContext context = contextRef.get();
257-
PException caughtException = IndirectCallContext.enter(frame, context, this);
257+
Object state = IndirectCallContext.enter(frame, context, this);
258258
try {
259259
return library.isSequence(obj);
260260
} finally {
261-
IndirectCallContext.exit(frame, context, caughtException);
261+
IndirectCallContext.exit(frame, context, state);
262262
}
263263
}
264264

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

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
import com.oracle.graal.python.builtins.objects.str.PString;
6868
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6969
import com.oracle.graal.python.builtins.objects.type.LazyPythonClass;
70+
import com.oracle.graal.python.nodes.IndirectCallNode;
7071
import com.oracle.graal.python.nodes.PNodeWithContext;
7172
import com.oracle.graal.python.nodes.PRaiseNode;
7273
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
@@ -76,9 +77,11 @@
7677
import com.oracle.graal.python.runtime.PythonContext;
7778
import com.oracle.graal.python.runtime.exception.PException;
7879
import com.oracle.graal.python.runtime.object.PythonObjectFactory;
80+
import com.oracle.truffle.api.Assumption;
7981
import com.oracle.truffle.api.CompilerAsserts;
8082
import com.oracle.truffle.api.CompilerDirectives;
8183
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
84+
import com.oracle.truffle.api.Truffle;
8285
import com.oracle.truffle.api.dsl.Cached;
8386
import com.oracle.truffle.api.dsl.CachedContext;
8487
import com.oracle.truffle.api.dsl.Fallback;
@@ -504,11 +507,23 @@ public static MarshallerNode create() {
504507
}
505508
}
506509

507-
public abstract static class UnmarshallerNode extends PNodeWithState {
510+
public abstract static class UnmarshallerNode extends PNodeWithState implements IndirectCallNode {
508511
public abstract Object execute(VirtualFrame frame, byte[] dataBytes, int version);
509512

510513
@Child private HashingStorageNodes.SetItemNode setItemNode;
511514
@Child private CodeNodes.CreateCodeNode createCodeNode;
515+
private final Assumption dontNeedExceptionState = Truffle.getRuntime().createAssumption();
516+
private final Assumption dontNeedCallerFrame = Truffle.getRuntime().createAssumption();
517+
518+
@Override
519+
public Assumption needNotPassFrameAssumption() {
520+
return dontNeedCallerFrame;
521+
}
522+
523+
@Override
524+
public Assumption needNotPassExceptionAssumption() {
525+
return dontNeedExceptionState;
526+
}
512527

513528
private int index;
514529
private byte[] data;
@@ -739,11 +754,11 @@ Object readObject(VirtualFrame frame, byte[] dataBytes, @SuppressWarnings("unuse
739754
@CachedContext(PythonLanguage.class) PythonContext context) {
740755
reset();
741756
this.data = dataBytes;
742-
PException savedExceptionState = IndirectCallContext.enter(frame, context, this);
757+
Object state = IndirectCallContext.enter(frame, context, this);
743758
try {
744759
return readObject(0);
745760
} finally {
746-
IndirectCallContext.exit(frame, context, savedExceptionState);
761+
IndirectCallContext.exit(frame, context, state);
747762
}
748763
}
749764

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@
7171
import com.oracle.graal.python.runtime.PosixResources;
7272
import com.oracle.graal.python.runtime.PythonContext;
7373
import com.oracle.graal.python.runtime.PythonOptions;
74-
import com.oracle.graal.python.runtime.exception.PException;
7574
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
7675
import com.oracle.truffle.api.dsl.Cached;
7776
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
@@ -102,12 +101,12 @@ int forkExec(VirtualFrame frame, PList args, @SuppressWarnings("unused") PList e
102101
@SuppressWarnings("unused") boolean restore_signals, @SuppressWarnings("unused") boolean call_setsid, @SuppressWarnings("unused") PNone preexec_fn) {
103102

104103
PythonContext context = getContext();
105-
PException caughtException = IndirectCallContext.enter(frame, context, this);
104+
Object state = IndirectCallContext.enter(frame, context, this);
106105
try {
107106
return forkExec(args, execList, closeFds, fdsToKeep, cwd, env, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, errpipe_read, errpipe_write, restore_signals, call_setsid,
108107
preexec_fn);
109108
} finally {
110-
IndirectCallContext.exit(frame, context, caughtException);
109+
IndirectCallContext.exit(frame, context, state);
111110
}
112111
}
113112

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

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@
144144
import com.oracle.graal.python.builtins.objects.type.PythonManagedClass;
145145
import com.oracle.graal.python.builtins.objects.type.TypeNodes;
146146
import com.oracle.graal.python.builtins.objects.type.TypeNodes.GetTypeFlagsNode;
147+
import com.oracle.graal.python.nodes.IndirectCallNode;
147148
import com.oracle.graal.python.nodes.PGuards;
148149
import com.oracle.graal.python.nodes.PNodeWithContext;
149150
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -161,7 +162,7 @@
161162
import com.oracle.graal.python.nodes.attributes.WriteAttributeToDynamicObjectNode;
162163
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
163164
import com.oracle.graal.python.nodes.call.CallNode;
164-
import com.oracle.graal.python.nodes.call.InvokeNode;
165+
import com.oracle.graal.python.nodes.call.FunctionInvokeNode;
165166
import com.oracle.graal.python.nodes.call.PythonCallNode;
166167
import com.oracle.graal.python.nodes.call.special.LookupAndCallBinaryNode;
167168
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
@@ -196,8 +197,10 @@
196197
import com.oracle.graal.python.runtime.sequence.storage.ByteSequenceStorage;
197198
import com.oracle.graal.python.runtime.sequence.storage.MroSequenceStorage;
198199
import com.oracle.graal.python.runtime.sequence.storage.SequenceStorage;
200+
import com.oracle.truffle.api.Assumption;
199201
import com.oracle.truffle.api.CompilerAsserts;
200202
import com.oracle.truffle.api.CompilerDirectives;
203+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
201204
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
202205
import com.oracle.truffle.api.RootCallTarget;
203206
import com.oracle.truffle.api.Truffle;
@@ -767,7 +770,7 @@ public static CheckFunctionResultNode create() {
767770
}
768771
}
769772

770-
abstract static class ExternalFunctionNode extends PRootNode {
773+
abstract static class ExternalFunctionNode extends PRootNode implements IndirectCallNode {
771774
private final Signature signature;
772775
private final TruffleObject cwrapper;
773776
private final TruffleObject callable;
@@ -777,6 +780,26 @@ abstract static class ExternalFunctionNode extends PRootNode {
777780
@Child private PForeignToPTypeNode fromForeign = PForeignToPTypeNode.create();
778781
@Child private InteropLibrary lib;
779782
@Child private CalleeContext calleeContext = CalleeContext.create();
783+
@CompilationFinal private Assumption nativeCodeDoesntNeedExceptionState = Truffle.getRuntime().createAssumption();
784+
@CompilationFinal private Assumption nativeCodeDoesntNeedMyFrame = Truffle.getRuntime().createAssumption();
785+
786+
@Override
787+
public Assumption needNotPassFrameAssumption() {
788+
return nativeCodeDoesntNeedMyFrame;
789+
}
790+
791+
@Override
792+
public Assumption needNotPassExceptionAssumption() {
793+
return nativeCodeDoesntNeedExceptionState;
794+
}
795+
796+
@Override
797+
public Node copy() {
798+
ExternalFunctionNode node = (ExternalFunctionNode) super.copy();
799+
node.nativeCodeDoesntNeedMyFrame = Truffle.getRuntime().createAssumption();
800+
node.nativeCodeDoesntNeedExceptionState = Truffle.getRuntime().createAssumption();
801+
return node;
802+
}
780803

781804
ExternalFunctionNode(PythonLanguage lang, String name, TruffleObject cwrapper, TruffleObject callable, Signature signature) {
782805
super(lang);
@@ -815,19 +838,21 @@ Object doIt(VirtualFrame frame,
815838
}
816839
// If any code requested the caught exception (i.e. used 'sys.exc_info()'), we store
817840
// it to the context since we cannot propagate it through the native frames.
818-
PException savedExceptionState = ForeignCallContext.enter(frame, ctx, this);
841+
Object state = ForeignCallContext.enter(frame, ctx, this);
819842

820843
try {
821844
return fromNative(asPythonObjectNode.execute(checkResultNode.execute(name, lib.execute(fun, arguments))));
822845
} catch (UnsupportedTypeException | UnsupportedMessageException e) {
846+
CompilerDirectives.transferToInterpreter();
823847
throw raiseNode.raise(PythonBuiltinClassType.TypeError, "Calling native function %s failed: %m", name, e);
824848
} catch (ArityException e) {
849+
CompilerDirectives.transferToInterpreter();
825850
throw raiseNode.raise(PythonBuiltinClassType.TypeError, "Calling native function %s expected %d arguments but got %d.", name, e.getExpectedArity(), e.getActualArity());
826851
} finally {
827852
// special case after calling a C function: transfer caught exception back to frame
828853
// to simulate the global state semantics
829854
PArguments.setException(frame, ctx.getCaughtException());
830-
ForeignCallContext.exit(frame, ctx, savedExceptionState);
855+
ForeignCallContext.exit(frame, ctx, state);
831856
calleeContext.exit(frame, this);
832857
}
833858
}
@@ -1612,7 +1637,7 @@ Object call(Object self, PBuiltinFunction function) {
16121637
}
16131638

16141639
abstract static class MethodDescriptorRoot extends PRootNode {
1615-
@Child protected InvokeNode invokeNode;
1640+
@Child protected FunctionInvokeNode invokeNode;
16161641
@Child protected ReadIndexedArgumentNode readSelfNode;
16171642
@Child private CalleeContext calleeContext = CalleeContext.create();
16181643

@@ -1625,7 +1650,7 @@ protected MethodDescriptorRoot(PythonLanguage language, PythonObjectFactory fact
16251650
this.factory = factory;
16261651
this.readSelfNode = ReadIndexedArgumentNode.create(0);
16271652
assert builtinFunction.getCallTarget().getRootNode() instanceof ExternalFunctionNode;
1628-
this.invokeNode = InvokeNode.create(builtinFunction);
1653+
this.invokeNode = FunctionInvokeNode.create(builtinFunction);
16291654
}
16301655

16311656
@Override
@@ -2446,11 +2471,11 @@ abstract static class PyFloat_AsDouble extends PythonUnaryBuiltinNode {
24462471
@Shared("asPythonObjectNode") @Cached CExtNodes.AsPythonObjectNode asPythonObjectNode,
24472472
@Shared("asDoubleNode") @Cached CExtNodes.AsNativeDoubleNode asDoubleNode,
24482473
@Shared("context") @CachedContext(PythonLanguage.class) PythonContext context) {
2449-
PException exceptionState = IndirectCallContext.enter(frame, context, this);
2474+
Object state = IndirectCallContext.enter(frame, context, this);
24502475
try {
24512476
return asDoubleNode.execute(asPythonObjectNode.execute(object));
24522477
} finally {
2453-
IndirectCallContext.exit(frame, context, exceptionState);
2478+
IndirectCallContext.exit(frame, context, state);
24542479
}
24552480
}
24562481

@@ -2684,11 +2709,11 @@ boolean doGeneric(VirtualFrame frame, Object object,
26842709
@CachedLibrary(limit = "1") PythonObjectLibrary dataModelLibrary,
26852710
@CachedContext(PythonLanguage.class) ContextReference<PythonContext> contextRef) {
26862711
PythonContext context = contextRef.get();
2687-
PException caughtException = IndirectCallContext.enter(frame, context, this);
2712+
Object state = IndirectCallContext.enter(frame, context, this);
26882713
try {
26892714
return dataModelLibrary.isSequence(object);
26902715
} finally {
2691-
IndirectCallContext.exit(frame, context, caughtException);
2716+
IndirectCallContext.exit(frame, context, state);
26922717
}
26932718
}
26942719
}

0 commit comments

Comments
 (0)