Skip to content

Commit 5071f32

Browse files
author
Adam Hrbac
committed
Move more logic into invokeTraceFunction
1 parent 7d8f0af commit 5071f32

File tree

1 file changed

+50
-53
lines changed

1 file changed

+50
-53
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 50 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,7 @@
158158
import com.oracle.graal.python.nodes.frame.WriteGlobalNodeGen;
159159
import com.oracle.graal.python.nodes.frame.WriteNameNode;
160160
import com.oracle.graal.python.nodes.frame.WriteNameNodeGen;
161+
import com.oracle.graal.python.nodes.object.GetClassNode;
161162
import com.oracle.graal.python.nodes.object.IsNode;
162163
import com.oracle.graal.python.nodes.statement.ExceptNode.ExceptMatchNode;
163164
import com.oracle.graal.python.nodes.statement.ExceptNodeFactory.ExceptMatchNodeGen;
@@ -497,9 +498,7 @@ public final class PBytecodeRootNode extends PRootNode implements BytecodeOSRNod
497498
@Child private CalleeContext calleeContext = CalleeContext.create();
498499
@Child private PythonObjectFactory factory = PythonObjectFactory.create();
499500
@Child private ExceptionStateNodes.GetCaughtExceptionNode getCaughtExceptionNode;
500-
// private GetExceptionTracebackNode traceGetExceptionTracebackNode = null;
501-
private MaterializeFrameNode traceMaterializeFrameNode = null;
502-
// private CallTernaryMethodNode traceCallTernaryMethodNode = null;
501+
@Child private MaterializeFrameNode traceMaterializeFrameNode = null;
503502

504503
private final LoopConditionProfile exceptionChainProfile1 = LoopConditionProfile.createCountingProfile();
505504
private final LoopConditionProfile exceptionChainProfile2 = LoopConditionProfile.createCountingProfile();
@@ -1107,14 +1106,11 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
11071106
CompilerAsserts.partialEvaluationConstant(bci);
11081107
CompilerAsserts.partialEvaluationConstant(stackTop);
11091108

1110-
if (!noTrace.isValid() && threadState.getTraceFun() != null && !threadState.isTracing()) {
1111-
mutableData.pyFrame = ensurePyFrame(virtualFrame, null);
1112-
// if we are simply continuing to run an OSR loop after the replacememnt, tracing an
1113-
// extra CALL event would be incorrect
1114-
if (!fromOSR) {
1115-
mutableData.pyFrame.setLocalTraceFun(invokeTraceFunction(threadState.getTraceFun(), null, threadState, virtualFrame, mutableData.pyFrame, PythonContext.TraceEvent.CALL,
1116-
initialBci == 0 ? getFirstLineno() : (mutableData.pastLine = bciToLine(initialBci))));
1117-
}
1109+
// if we are simply continuing to run an OSR loop after the replacement, tracing an
1110+
// extra CALL event would be incorrect
1111+
if (!noTrace.isValid() && threadState.getTraceFun() != null && !fromOSR) {
1112+
invokeTraceFunction(virtualFrame, null, threadState, mutableData, PythonContext.TraceEvent.CALL,
1113+
initialBci == 0 ? getFirstLineno() : (mutableData.pastLine = bciToLine(initialBci)), false);
11181114
}
11191115

11201116
mutableData.returnLine = mutableData.pastLine;
@@ -1124,8 +1120,7 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
11241120
while (true) {
11251121
final byte bc = localBC[bci];
11261122
final int beginBci = bci;
1127-
if (!noTrace.isValid() && threadState.getTraceFun() != null && !threadState.isTracing()) {
1128-
mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
1123+
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
11291124
int thisLine = bciToLine(bci);
11301125
boolean onANewLine = thisLine != mutableData.pastLine;
11311126
mutableData.pastLine = thisLine;
@@ -1147,22 +1142,21 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
11471142
* https://github.com/python/cpython/blob/main/Objects/lnotab_notes.txt#L210-L215
11481143
* for more details
11491144
*/
1150-
boolean shouldTrace = mutableData.pyFrame.getLocalTraceFun() != null && mutableData.pyFrame.getTraceLine();
1151-
if (shouldTrace) {
1152-
shouldTrace = mutableData.pastBci > bci; // is a backward jump
1153-
if (!shouldTrace) {
1154-
shouldTrace = onANewLine &&
1155-
// is not a forward jump
1156-
(mutableData.pastBci + c.length() >= bci ||
1157-
// is a forward jump to the start of line
1158-
bciToLine(bci - 1) != thisLine);
1159-
}
1145+
boolean shouldTrace = mutableData.pastBci > bci; // is a backward jump
1146+
if (!shouldTrace) {
1147+
shouldTrace = onANewLine &&
1148+
// is not a forward jump
1149+
(mutableData.pastBci + c.length() >= bci ||
1150+
// is a forward jump to the start of line
1151+
bciToLine(bci - 1) != thisLine);
11601152
}
11611153
if (shouldTrace) {
11621154
mutableData.returnLine = mutableData.pastLine;
1163-
mutableData.pyFrame.setLocalTraceFun(
1164-
invokeTraceFunction(mutableData.pyFrame.getLocalTraceFun(), null, threadState, virtualFrame, mutableData.pyFrame, PythonContext.TraceEvent.LINE,
1165-
mutableData.pastLine));
1155+
mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
1156+
if (mutableData.pyFrame.getTraceLine()) {
1157+
invokeTraceFunction(virtualFrame, null, threadState, mutableData, PythonContext.TraceEvent.LINE,
1158+
mutableData.pastLine, true);
1159+
}
11661160
}
11671161
}
11681162
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
@@ -1622,12 +1616,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
16221616
LoopNode.reportLoopCount(this, mutableData.loopCount);
16231617
}
16241618
Object value = virtualFrame.getObject(stackTop);
1625-
if (!noTrace.isValid() && threadState.getTraceFun() != null && !threadState.isTracing()) {
1626-
mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
1627-
if (mutableData.pyFrame.getLocalTraceFun() != null) {
1628-
invokeTraceFunction(mutableData.pyFrame.getLocalTraceFun(), value, threadState, virtualFrame, mutableData.pyFrame, PythonContext.TraceEvent.RETURN,
1629-
mutableData.pyFrame.getTraceLine() ? mutableData.returnLine : bciToLine(bci));
1630-
}
1619+
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
1620+
invokeTraceFunction(virtualFrame, value, threadState, mutableData, PythonContext.TraceEvent.RETURN,
1621+
mutableData.returnLine, true);
16311622
}
16321623
if (isGeneratorOrCoroutine) {
16331624
throw new GeneratorReturnException(value);
@@ -2067,12 +2058,9 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
20672058
// Clear slots that were popped (if any)
20682059
clearFrameSlots(localFrame, stackTop + 1, initialStackTop);
20692060
}
2070-
if (!noTrace.isValid() && threadState.getTraceFun() != null && !threadState.isTracing()) {
2071-
mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
2072-
if (mutableData.pyFrame.getLocalTraceFun() != null) {
2073-
invokeTraceFunction(mutableData.pyFrame.getLocalTraceFun(), value, threadState, virtualFrame, mutableData.pyFrame, PythonContext.TraceEvent.RETURN,
2074-
mutableData.pyFrame.getTraceLine() ? mutableData.returnLine : bciToLine(bci));
2075-
}
2061+
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
2062+
invokeTraceFunction(virtualFrame, value, threadState, mutableData, PythonContext.TraceEvent.RETURN,
2063+
mutableData.returnLine, true);
20762064
}
20772065
return new GeneratorYieldResult(bci + 1, stackTop, value);
20782066
}
@@ -2165,10 +2153,12 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
21652153
mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
21662154
if (mutableData.pyFrame.getLocalTraceFun() != null) {
21672155
Object traceback = GetExceptionTracebackNode.getUncached().execute(pe);
2168-
mutableData.pyFrame.setLocalTraceFun(invokeTraceFunction(mutableData.pyFrame.getLocalTraceFun(),
2169-
factory.createTuple(new Object[]{pe.getClass(), pe.setCatchingFrameAndGetEscapedException(virtualFrame, this), traceback}), threadState, virtualFrame,
2170-
mutableData.pyFrame,
2171-
PythonContext.TraceEvent.EXCEPTION, bciToLine(bci)));
2156+
PBaseException peForPython = pe.setCatchingFrameAndGetEscapedException(virtualFrame, this);
2157+
Object peType = GetClassNode.getUncached().execute(peForPython);
2158+
invokeTraceFunction(virtualFrame,
2159+
factory.createTuple(new Object[]{peType, peForPython, traceback}), threadState,
2160+
mutableData,
2161+
PythonContext.TraceEvent.EXCEPTION, bciToLine(bci), true);
21722162
}
21732163
}
21742164

@@ -2201,11 +2191,8 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
22012191
LoopNode.reportLoopCount(this, mutableData.loopCount);
22022192
}
22032193
if (!noTrace.isValid() && threadState.getTraceFun() != null) {
2204-
mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
2205-
if (mutableData.pyFrame.getLocalTraceFun() != null) {
2206-
invokeTraceFunction(mutableData.pyFrame.getLocalTraceFun(), PNone.NONE, threadState, virtualFrame, mutableData.pyFrame, PythonContext.TraceEvent.RETURN,
2207-
mutableData.pyFrame.getTraceLine() ? mutableData.returnLine : bciToLine(bci));
2208-
}
2194+
invokeTraceFunction(virtualFrame, PNone.NONE, threadState, mutableData, PythonContext.TraceEvent.RETURN,
2195+
mutableData.returnLine, true);
22092196
}
22102197
if (e == pe) {
22112198
throw pe;
@@ -2243,22 +2230,32 @@ private PFrame ensurePyFrame(VirtualFrame virtualFrame, PFrame pyFrame) {
22432230
}
22442231

22452232
@HostCompilerDirectives.InliningCutoff
2246-
private Object invokeTraceFunction(Object traceFn, Object arg, PythonContext.PythonThreadState threadState, VirtualFrame virtualFrame, PFrame tracing,
2247-
PythonContext.TraceEvent event, int line) {
2233+
private void invokeTraceFunction(VirtualFrame virtualFrame, Object arg, PythonContext.PythonThreadState threadState, MutableLoopData mutableData,
2234+
PythonContext.TraceEvent event, int line, boolean useLocalFn) {
2235+
if (threadState.isTracing()) {
2236+
return;
2237+
}
22482238
threadState.tracingStart(event);
2239+
PFrame pyFrame = mutableData.pyFrame = ensurePyFrame(virtualFrame, mutableData.pyFrame);
2240+
Object traceFn = useLocalFn ? pyFrame.getLocalTraceFun() : threadState.getTraceFun();
2241+
if (traceFn == null) {
2242+
threadState.tracingStop();
2243+
return;
2244+
}
22492245
Object nonNullArg = arg == null ? PNone.NONE : arg;
22502246
try {
22512247
if (line != -1) {
2252-
tracing.setLineLock(line);
2248+
pyFrame.setLineLock(line);
22532249
}
2254-
Object result = CallTernaryMethodNode.getUncached().execute(null, traceFn, tracing, event.pythonName, nonNullArg);
2255-
return result == PNone.NONE ? null : result;
2250+
Object result = CallTernaryMethodNode.getUncached().execute(null, traceFn, pyFrame, event.pythonName, nonNullArg);
2251+
Object realResult = result == PNone.NONE ? null : result;
2252+
pyFrame.setLocalTraceFun(realResult);
22562253
} catch (Throwable e) {
22572254
threadState.setTraceFun(null, cachedLanguage);
22582255
throw e;
22592256
} finally {
22602257
if (line != -1) {
2261-
tracing.lineUnlock();
2258+
pyFrame.lineUnlock();
22622259
}
22632260
threadState.tracingStop();
22642261
}

0 commit comments

Comments
 (0)