Skip to content

Commit 14934e0

Browse files
author
Adam Hrbac
committed
Final changes
1 parent de166f6 commit 14934e0

File tree

4 files changed

+36
-44
lines changed

4 files changed

+36
-44
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2023, 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
@@ -71,9 +71,9 @@ static Object get(PFrame frame,
7171
@CApiBuiltin(ret = Int, args = {PyFrameObject}, call = Direct)
7272
abstract static class PyFrame_GetLineNumber extends CApiUnaryBuiltinNode {
7373
@Specialization
74-
static int get(PFrame frame,
75-
@Cached FrameBuiltins.GetLinenoNode getLinenoNode) {
76-
return getLinenoNode.executeInt(null, frame);
74+
static int get(PFrame frame) {
75+
// do not sync location here since we have no VirtualFrame
76+
return frame.getLine();
7777
}
7878
}
7979

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/frame/FrameBuiltins.java

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,15 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
8787
@GenerateNodeFactory
8888
abstract static class ReprNode extends PythonUnaryBuiltinNode {
8989
@Specialization
90-
static TruffleString repr(VirtualFrame frame, PFrame self,
90+
TruffleString repr(VirtualFrame frame, PFrame self,
9191
@Cached GetCodeNode getCodeNode,
92-
@Cached GetLinenoNode getLinenoNode,
92+
@Bind("this") Node inliningTarget,
93+
@Cached InlinedConditionProfile profile,
94+
@Cached MaterializeFrameNode materializeFrameNode,
9395
@Cached SimpleTruffleStringFormatNode simpleTruffleStringFormatNode) {
9496
PCode code = getCodeNode.executeObject(frame, self);
95-
int lineno = getLinenoNode.executeInt(frame, self);
97+
LinenoNode.syncLocationIfNeeded(frame, self, this, inliningTarget, profile, materializeFrameNode);
98+
int lineno = self.getLine();
9699
return simpleTruffleStringFormatNode.format("<frame at 0x%s, file '%s', line %d, code %s>",
97100
objectHashCodeAsHexString(self), code.getFilename(), lineno, code.getName());
98101
}
@@ -145,26 +148,6 @@ public static GetBuiltinsNode create() {
145148
}
146149
}
147150

148-
@GenerateNodeFactory
149-
public abstract static class GetLinenoNode extends PythonBuiltinNode {
150-
// Kept around since it is used by other nodes
151-
public abstract int executeInt(VirtualFrame frame, PFrame self);
152-
153-
@Specialization
154-
int get(VirtualFrame frame, PFrame self,
155-
@Bind("this") Node inliningTarget,
156-
@Cached InlinedConditionProfile isCurrentFrameProfile,
157-
@Cached MaterializeFrameNode materializeNode) {
158-
LinenoNode.syncLocationIfNeeded(frame, self, this, inliningTarget, isCurrentFrameProfile, materializeNode);
159-
return self.getLine();
160-
}
161-
162-
@NeverDefault
163-
public static GetLinenoNode create() {
164-
return FrameBuiltinsFactory.GetLinenoNodeFactory.create(null);
165-
}
166-
}
167-
168151
@Builtin(name = "f_lineno", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, isGetter = true, isSetter = true, allowsDelete = true)
169152
@GenerateNodeFactory
170153
public abstract static class LinenoNode extends PythonBinaryBuiltinNode {
@@ -179,8 +162,11 @@ Object delete(VirtualFrame frame, PFrame self, DescriptorDeleteMarker ignored,
179162

180163
@Specialization(guards = "isNoValue(newLineno)")
181164
int get(VirtualFrame frame, PFrame self, Object newLineno,
182-
@Cached GetLinenoNode getLinenoNode) {
183-
return getLinenoNode.executeInt(frame, self);
165+
@Bind("this") Node inliningTarget,
166+
@Cached @Cached.Exclusive InlinedConditionProfile profile,
167+
@Cached @Cached.Exclusive MaterializeFrameNode frameNode) {
168+
syncLocationIfNeeded(frame, self, this, inliningTarget, profile, frameNode);
169+
return self.getLine();
184170
}
185171

186172
static void syncLocationIfNeeded(VirtualFrame frame, PFrame self, Node location, Node inliningTarget, InlinedConditionProfile isCurrentFrameProfile, MaterializeFrameNode materializeNode) {
@@ -198,8 +184,8 @@ static void syncLocationIfNeeded(VirtualFrame frame, PFrame self, Node location,
198184
@Specialization(guards = {"!isNoValue(newLineno)", "!isDeleteMarker(newLineno)"})
199185
PNone set(VirtualFrame frame, PFrame self, Object newLineno,
200186
@Bind("this") Node inliningTarget,
201-
@Cached InlinedConditionProfile isCurrentFrameProfile,
202-
@Cached MaterializeFrameNode materializeNode,
187+
@Cached @Cached.Exclusive InlinedConditionProfile isCurrentFrameProfile,
188+
@Cached @Cached.Exclusive MaterializeFrameNode materializeNode,
203189
@Cached @Cached.Exclusive PRaiseNode.Lazy raise,
204190
@Cached PyLongCheckExactNode isLong,
205191
@Cached PyLongAsLongAndOverflowNode toLong) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/CodeUnit.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,9 @@ private static String collectionTypeToString(int oparg) {
534534
throw new IllegalStateException("Unknown type");
535535
}
536536

537+
public static final int LINE_TO_BCI_LINE_AFTER_CODEBLOCK = -1;
538+
public static final int LINE_TO_BCI_LINE_BEFORE_CODEBLOCK = -2;
539+
537540
// -1 for line after the code block, -2 for line before the code block, line number otherwise
538541
public int lineToBci(int line) {
539542
if (startLine == line) {
@@ -544,7 +547,7 @@ public int lineToBci(int line) {
544547
return 0;
545548
}
546549
int[] map = getSourceMap().startLineMap;
547-
int bestBci = -1;
550+
int bestBci = LINE_TO_BCI_LINE_AFTER_CODEBLOCK;
548551
int lineDiff = Integer.MAX_VALUE;
549552
boolean afterFirst = false;
550553
for (int bci = 0; bci < map.length; ++bci) {
@@ -562,7 +565,7 @@ public int lineToBci(int line) {
562565
}
563566
}
564567
// bestBci being -1 means the line is outside the code block
565-
return afterFirst ? bestBci : -2;
568+
return afterFirst ? bestBci : LINE_TO_BCI_LINE_BEFORE_CODEBLOCK;
566569
}
567570

568571
public enum StackItem {

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

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1296,12 +1296,15 @@ private Object bytecodeLoop(VirtualFrame virtualFrame, Frame localFrame, Bytecod
12961296
final int beginBci = bci;
12971297
tracingOrProfilingEnabled = checkTracingAndProfilingEnabled(noTraceOrProfile, mutableData);
12981298
if (isTracingEnabled(tracingOrProfilingEnabled)) {
1299-
int stackDiff = traceLine(virtualFrame, mutableData, localBC, bci);
1299+
final int stackDiff = traceLine(virtualFrame, mutableData, localBC, bci);
13001300
if (stackDiff <= 0) {
1301-
// traceLine already set the correct bci
1301+
// The loop must be partially unrollable assuming a certain sequence of bytecode
1302+
// instructions. A jump can happen non-deterministically and thus break this
1303+
// assumption
1304+
CompilerDirectives.transferToInterpreterAndInvalidate();
13021305
bci = virtualFrame.getInt(bciSlot);
13031306
stackTop += stackDiff;
1304-
continue; // todo code in big loop
1307+
continue;
13051308
}
13061309
}
13071310

@@ -2926,23 +2929,23 @@ private int traceLine(VirtualFrame virtualFrame, MutableLoopData mutableData, by
29262929
PFrame pyFrame = mutableData.getPyFrame();
29272930
if (pyFrame.didJump()) {
29282931
mutableData.setPastBci(bci);
2929-
return bci;
2932+
return ret;
29302933
}
29312934
if (pyFrame.getTraceLine()) {
2932-
pyFrame.setJumpDestLine(PFrame.NO_JUMP); // jumps from live event allowed
2935+
pyFrame.setJumpDestLine(PFrame.NO_JUMP); // jumps from line event allowed
29332936
invokeTraceFunction(virtualFrame, null, mutableData.getThreadState(this), mutableData, PythonContext.TraceEvent.LINE,
29342937
mutableData.getPastLine(), true);
29352938
if (pyFrame.didJump()) {
29362939
int newBci = lineToBci(pyFrame.getJumpDestLine());
29372940
mutableData.setPastBci(bci);
2938-
if (newBci == -1) {
2939-
// line before the code block
2940-
throw PRaiseNode.getUncached().raise(ValueError, ErrorMessages.LINE_D_COMES_AFTER_THE_CURRENT_CODE_BLOCK, pyFrame.getLine());
2941-
} else if (newBci == -2) {
2941+
if (newBci == CodeUnit.LINE_TO_BCI_LINE_AFTER_CODEBLOCK) {
29422942
// line after the code block
2943+
throw PRaiseNode.getUncached().raise(ValueError, ErrorMessages.LINE_D_COMES_AFTER_THE_CURRENT_CODE_BLOCK, pyFrame.getLine());
2944+
} else if (newBci == CodeUnit.LINE_TO_BCI_LINE_BEFORE_CODEBLOCK) {
2945+
// line before the code block
29432946
throw PRaiseNode.getUncached().raise(ValueError, ErrorMessages.LINE_D_COMES_BEFORE_THE_CURRENT_CODE_BLOCK, pyFrame.getJumpDestLine());
29442947
} else {
2945-
ret = computeJump(bci, newBci);
2948+
ret = computeJumpStackDifference(bci, newBci);
29462949
setCurrentBci(virtualFrame, bcioffset, newBci);
29472950
}
29482951
}
@@ -2954,7 +2957,7 @@ private int traceLine(VirtualFrame virtualFrame, MutableLoopData mutableData, by
29542957
}
29552958

29562959
@TruffleBoundary
2957-
private int computeJump(int bci, int newBci) {
2960+
private int computeJumpStackDifference(int bci, int newBci) {
29582961
int ret;
29592962
var stacks = co.computeStackElems();
29602963
String error = co.checkJump(stacks, bci, newBci);

0 commit comments

Comments
 (0)