Skip to content

Commit 0d0f5ad

Browse files
committed
Use condition profiles instead of deopt and PRaiseNode.Lazy for raising unbound local errors
1 parent eb63317 commit 0d0f5ad

File tree

1 file changed

+31
-22
lines changed

1 file changed

+31
-22
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@
225225
import com.oracle.truffle.api.nodes.Node.Child;
226226
import com.oracle.truffle.api.object.DynamicObjectLibrary;
227227
import com.oracle.truffle.api.profiles.InlinedBranchProfile;
228+
import com.oracle.truffle.api.profiles.InlinedConditionProfile;
228229
import com.oracle.truffle.api.source.Source;
229230
import com.oracle.truffle.api.source.SourceSection;
230231
import com.oracle.truffle.api.strings.TruffleString;
@@ -857,22 +858,26 @@ protected byte[] extractCode() {
857858
return MarshalModuleBuiltins.serializeCodeUnit(co);
858859
}
859860

860-
private static Object checkUnboundCell(PCell cell, int index, PBytecodeDSLRootNode rootNode, Node inliningTarget, PRaiseNode.Lazy raiseNode) {
861+
private static Object checkUnboundCell(PCell cell, int index, PBytecodeDSLRootNode rootNode, Node inliningTarget, InlinedConditionProfile nullProfile) {
861862
Object result = cell.getRef();
862-
if (result == null) {
863-
CompilerDirectives.transferToInterpreterAndInvalidate();
864-
CodeUnit codeUnit = rootNode.getCodeUnit();
865-
if (index < codeUnit.cellvars.length) {
866-
TruffleString localName = codeUnit.cellvars[index];
867-
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
868-
} else {
869-
TruffleString localName = codeUnit.freevars[index - codeUnit.cellvars.length];
870-
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.NameError, ErrorMessages.UNBOUNDFREEVAR, localName);
871-
}
863+
if (nullProfile.profile(inliningTarget, result == null)) {
864+
throw raiseUnboundCell(index, rootNode, inliningTarget);
872865
}
873866
return result;
874867
}
875868

869+
@TruffleBoundary
870+
private static PException raiseUnboundCell(int index, PBytecodeDSLRootNode rootNode, Node inliningTarget) {
871+
CodeUnit codeUnit = rootNode.getCodeUnit();
872+
if (index < codeUnit.cellvars.length) {
873+
TruffleString localName = codeUnit.cellvars[index];
874+
throw PRaiseNode.getUncached().raise(PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
875+
} else {
876+
TruffleString localName = codeUnit.freevars[index - codeUnit.cellvars.length];
877+
throw PRaiseNode.getUncached().raise(PythonBuiltinClassType.NameError, ErrorMessages.UNBOUNDFREEVAR, localName);
878+
}
879+
}
880+
876881
public PCell readClassCell(Frame frame) {
877882
if (classcellIndex < 0) {
878883
return null;
@@ -3073,8 +3078,8 @@ public static final class LoadCell {
30733078
public static Object doLoadCell(int index, PCell cell,
30743079
@Bind PBytecodeDSLRootNode rootNode,
30753080
@Bind Node inliningTarget,
3076-
@Cached PRaiseNode.Lazy raiseNode) {
3077-
return checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
3081+
@Cached InlinedConditionProfile nullProfile) {
3082+
return checkUnboundCell(cell, index, rootNode, inliningTarget, nullProfile);
30783083
}
30793084
}
30803085

@@ -3088,15 +3093,15 @@ public static Object doLoadCell(VirtualFrame frame,
30883093
@Bind PBytecodeDSLRootNode rootNode,
30893094
@Bind Node inliningTarget,
30903095
@Cached ReadFromLocalsNode readLocalsNode,
3091-
@Cached PRaiseNode.Lazy raiseNode) {
3096+
@Cached InlinedConditionProfile nullProfile) {
30923097
CodeUnit co = rootNode.getCodeUnit();
30933098
TruffleString name = co.freevars[index - co.cellvars.length];
30943099
Object locals = PArguments.getSpecialArgument(frame);
30953100
Object value = readLocalsNode.execute(frame, inliningTarget, locals, name);
30963101
if (value != PNone.NO_VALUE) {
30973102
return value;
30983103
} else {
3099-
return checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
3104+
return checkUnboundCell(cell, index, rootNode, inliningTarget, nullProfile);
31003105
}
31013106
}
31023107
}
@@ -3126,8 +3131,8 @@ public static final class ClearCell {
31263131
public static void doClearCell(int index, PCell cell,
31273132
@Bind PBytecodeDSLRootNode rootNode,
31283133
@Bind Node inliningTarget,
3129-
@Cached PRaiseNode.Lazy raiseNode) {
3130-
checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
3134+
@Cached InlinedConditionProfile nullProfile) {
3135+
checkUnboundCell(cell, index, rootNode, inliningTarget, nullProfile);
31313136
cell.clearRef();
31323137
}
31333138
}
@@ -4121,14 +4126,18 @@ public static final class CheckUnboundLocal {
41214126
public static Object doObject(int index, Object localValue,
41224127
@Bind PBytecodeDSLRootNode rootNode,
41234128
@Bind Node inliningTarget,
4124-
@Cached PRaiseNode.Lazy raiseNode) {
4125-
if (localValue == null) {
4126-
CompilerDirectives.transferToInterpreterAndInvalidate();
4127-
TruffleString localName = rootNode.getCodeUnit().varnames[index];
4128-
throw raiseNode.get(inliningTarget).raise(PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
4129+
@Cached InlinedConditionProfile nullProfile) {
4130+
if (nullProfile.profile(inliningTarget, localValue == null)) {
4131+
throw raiseUnboundLocal(index, rootNode, inliningTarget);
41294132
}
41304133
return localValue;
41314134
}
4135+
4136+
@TruffleBoundary
4137+
private static PException raiseUnboundLocal(int index, PBytecodeDSLRootNode rootNode, Node inliningTarget) {
4138+
TruffleString localName = rootNode.getCodeUnit().varnames[index];
4139+
throw PRaiseNode.getUncached().raise(PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
4140+
}
41324141
}
41334142

41344143
@Operation

0 commit comments

Comments
 (0)