Skip to content

Commit ba4d71a

Browse files
committed
Bytecode DSL: use @Bind BytecodeNode instead of PBytecodeDSLRootNode where possible, remove unused operations
1 parent 01f98d5 commit ba4d71a

File tree

2 files changed

+40
-29
lines changed

2 files changed

+40
-29
lines changed

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

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -969,17 +969,17 @@ protected byte[] extractCode() {
969969
return MarshalModuleBuiltins.serializeCodeUnit(null, PythonContext.get(this), co);
970970
}
971971

972-
private static Object checkUnboundCell(PCell cell, int index, PBytecodeDSLRootNode rootNode, Node inliningTarget, PRaiseNode raiseNode) {
972+
private static Object checkUnboundCell(PCell cell, int index, BytecodeNode bytecodeNode) {
973973
Object result = cell.getRef();
974974
if (result == null) {
975975
CompilerDirectives.transferToInterpreterAndInvalidate();
976-
CodeUnit codeUnit = rootNode.getCodeUnit();
976+
CodeUnit codeUnit = ((PBytecodeDSLRootNode) bytecodeNode.getRootNode()).getCodeUnit();
977977
if (index < codeUnit.cellvars.length) {
978978
TruffleString localName = codeUnit.cellvars[index];
979-
throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
979+
throw PRaiseNode.raiseStatic(bytecodeNode, PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
980980
} else {
981981
TruffleString localName = codeUnit.freevars[index - codeUnit.cellvars.length];
982-
throw raiseNode.raise(inliningTarget, PythonBuiltinClassType.NameError, ErrorMessages.UNBOUNDFREEVAR, localName);
982+
throw PRaiseNode.raiseStatic(bytecodeNode, PythonBuiltinClassType.NameError, ErrorMessages.UNBOUNDFREEVAR, localName);
983983
}
984984
}
985985
return result;
@@ -2511,10 +2511,8 @@ public static void doAssertFailed(VirtualFrame frame, Object assertionMessage,
25112511
public static final class LoadCell {
25122512
@Specialization
25132513
public static Object doLoadCell(int index, PCell cell,
2514-
@Bind PBytecodeDSLRootNode rootNode,
2515-
@Bind Node inliningTarget,
2516-
@Cached PRaiseNode raiseNode) {
2517-
return checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
2514+
@Bind BytecodeNode bytecodeNode) {
2515+
return checkUnboundCell(cell, index, bytecodeNode);
25182516
}
25192517
}
25202518

@@ -2528,11 +2526,10 @@ public static Object doLoadCell(int index, PCell cell,
25282526
public static final class LoadFromDictOrCell {
25292527
@Specialization
25302528
public static Object doLoadCell(VirtualFrame frame, int index, Object locals, PCell cell,
2531-
@Bind PBytecodeDSLRootNode rootNode,
2529+
@Bind BytecodeNode bytecodeNode,
25322530
@Bind Node inliningTarget,
2533-
@Cached ReadFromLocalsNode readLocalsNode,
2534-
@Cached PRaiseNode raiseNode) {
2535-
CodeUnit co = rootNode.getCodeUnit();
2531+
@Cached ReadFromLocalsNode readLocalsNode) {
2532+
CodeUnit co = ((PBytecodeDSLRootNode) bytecodeNode.getRootNode()).getCodeUnit();
25362533
TruffleString name;
25372534
if (index < co.cellvars.length) {
25382535
name = co.cellvars[index];
@@ -2543,7 +2540,7 @@ public static Object doLoadCell(VirtualFrame frame, int index, Object locals, PC
25432540
if (value != PNone.NO_VALUE) {
25442541
return value;
25452542
} else {
2546-
return checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
2543+
return checkUnboundCell(cell, index, bytecodeNode);
25472544
}
25482545
}
25492546
}
@@ -2608,10 +2605,8 @@ public static void doCreateCells(VirtualFrame frame, LocalRangeAccessor locals,
26082605
public static final class ClearCell {
26092606
@Specialization
26102607
public static void doClearCell(int index, PCell cell,
2611-
@Bind PBytecodeDSLRootNode rootNode,
2612-
@Bind Node inliningTarget,
2613-
@Cached PRaiseNode raiseNode) {
2614-
checkUnboundCell(cell, index, rootNode, inliningTarget, raiseNode);
2608+
@Bind BytecodeNode bytecodeNode) {
2609+
checkUnboundCell(cell, index, bytecodeNode);
26152610
cell.clearRef();
26162611
}
26172612
}
@@ -3605,16 +3600,6 @@ private static PException raiseUnbound(PBytecodeDSLRootNode rootNode, Node inlin
36053600
throw PRaiseNode.raiseStatic(inliningTarget, PythonBuiltinClassType.UnboundLocalError, ErrorMessages.LOCAL_VAR_REFERENCED_BEFORE_ASSIGMENT, localName);
36063601
}
36073602

3608-
@Operation(storeBytecodeIndex = true)
3609-
public static final class RaiseNotImplementedError {
3610-
@Specialization
3611-
public static void doRaise(VirtualFrame frame, TruffleString name,
3612-
@Bind BytecodeNode node) {
3613-
throw PRaiseNode.raiseStatic(node, PythonBuiltinClassType.NotImplementedError, name);
3614-
3615-
}
3616-
}
3617-
36183603
/**
36193604
* Creates a TypeVar, TypeVarTuple or ParamSpec object. The constant argument determines
36203605
* (defined in {@link MakeTypeParamKind}) which and whether it will need to pop bound or
@@ -3656,8 +3641,8 @@ public static Object doObject(int kind, TruffleString name, Object boundOrConstr
36563641
public static final class MakeTypeAliasType {
36573642
@Specialization
36583643
public static PTypeAliasType doObject(TruffleString name, Object typeParams, Object computeValue,
3659-
@Bind PBytecodeDSLRootNode rootNode) {
3660-
PythonLanguage language = PythonLanguage.get(rootNode);
3644+
@Bind BytecodeNode bytecodeNode) {
3645+
PythonLanguage language = PythonLanguage.get(bytecodeNode);
36613646
// bytecode compiler should ensure that typeParams are either PTuple or null
36623647
return PFactory.createTypeAliasType(language, name, (PTuple) typeParams, computeValue, null, null);
36633648
}

mx.graalpython/mx_graalpython.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1993,6 +1993,7 @@ def python_style_checks(args):
19931993
"Check (and fix where possible) copyrights, eclipse formatting, and spotbugs"
19941994
warn_about_old_hardcoded_version()
19951995
python_run_mx_filetests(args)
1996+
check_unused_operations()
19961997
python_checkcopyrights(["--fix"] if "--fix" in args else [])
19971998
if not os.environ.get("ECLIPSE_EXE"):
19981999
find_eclipse()
@@ -2054,6 +2055,31 @@ def python_run_mx_filetests(_):
20542055
mx.run([sys.executable, test, "-v"])
20552056

20562057

2058+
def check_unused_operations():
2059+
root_node = "graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode_dsl/PBytecodeDSLRootNode.java"
2060+
in_operation = False
2061+
operations = []
2062+
with open(os.path.join(SUITE.dir, root_node)) as f:
2063+
while line := f.readline():
2064+
if not in_operation and ('@Operation\n' in line or '@Operation(' in line):
2065+
in_operation = True
2066+
elif in_operation:
2067+
if names := re.findall(r'public static final class (\S+)', line):
2068+
in_operation = False
2069+
operations += names
2070+
compiler = "graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java"
2071+
with open(os.path.join(SUITE.dir, compiler)) as f:
2072+
contents = f.read().lower()
2073+
result = True
2074+
for n in operations:
2075+
if not n.lower() in contents:
2076+
mx.log_error(f"ERROR: unused @Operation {n}")
2077+
result = False
2078+
if not result:
2079+
mx.abort("Found unused @Operation, see above")
2080+
2081+
2082+
20572083
def _python_checkpatchfiles():
20582084
env = os.environ.copy()
20592085
mx_dir = Path(__file__).parent

0 commit comments

Comments
 (0)