Skip to content

Commit dfe43fe

Browse files
committed
[GR-33179] Bytecode interpreter improvements (OSR, generators)
PullRequest: graalpython/2278
2 parents b0e4eba + 1084d56 commit dfe43fe

32 files changed

+1686
-752
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/compiler/CompilerTests.java

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,10 @@
5555
import com.oracle.graal.python.compiler.CompilationUnit;
5656
import com.oracle.graal.python.compiler.Compiler;
5757
import com.oracle.graal.python.pegparser.FExprParser;
58+
import com.oracle.graal.python.pegparser.InputType;
5859
import com.oracle.graal.python.pegparser.NodeFactory;
5960
import com.oracle.graal.python.pegparser.NodeFactoryImp;
6061
import com.oracle.graal.python.pegparser.Parser;
61-
import com.oracle.graal.python.pegparser.ParserErrorCallback;
6262
import com.oracle.graal.python.pegparser.ParserTokenizer;
6363
import com.oracle.graal.python.pegparser.sst.ExprTy;
6464
import com.oracle.graal.python.pegparser.sst.ModTy;
@@ -703,24 +703,32 @@ public void testImportStar() {
703703
doTest("from a.b import *");
704704
}
705705

706+
@Test
707+
public void testEval() {
708+
doTest("1", InputType.EVAL);
709+
}
710+
711+
@Test
712+
public void testSingle() {
713+
doTest("1", InputType.SINGLE);
714+
}
715+
706716
private void doTest(String src) {
717+
doTest(src, InputType.FILE);
718+
}
719+
720+
private void doTest(String src, InputType type) {
707721
ParserTokenizer tokenizer = new ParserTokenizer(src);
708722
NodeFactory factory = new NodeFactoryImp();
709-
ParserErrorCallback errorCb = new ParserErrorCallback() {
710-
@Override
711-
public void onError(ParserErrorCallback.ErrorType type, int start, int end, String message) {
712-
System.err.println(String.format("TODO: %s[%d:%d]: %s", type.name(), start, end, message));
713-
}
714-
};
715723
FExprParser fexpParser = new FExprParser() {
716724
@Override
717725
public ExprTy parse(String code) {
718726
ParserTokenizer tok = new ParserTokenizer(code);
719-
return new Parser(tok, factory, this, errorCb).fstring_rule();
727+
return new Parser(tok, factory, this).fstring_rule();
720728
}
721729
};
722-
Parser parser = new Parser(tokenizer, factory, fexpParser, errorCb);
723-
ModTy result = parser.file_rule();
730+
Parser parser = new Parser(tokenizer, factory, fexpParser);
731+
ModTy result = (ModTy) parser.parse(type);
724732
Compiler compiler = new Compiler();
725733
CompilationUnit cu = compiler.compile(result, EnumSet.noneOf(Compiler.Flags.class), 2);
726734
CodeUnit co = cu.assemble(0);
@@ -741,7 +749,7 @@ private void checkCodeUnit(CodeUnit co) {
741749
Assert.assertEquals(Files.readString(goldenFile), coString);
742750
}
743751
} catch (IOException ex) {
744-
Assert.assertTrue(ex.getMessage(), false);
752+
Assert.fail(ex.getMessage());
745753
}
746754
}
747755

graalpython/com.oracle.graal.python.test/testData/goldenFiles/CompilerTests/testCoroutine.co

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ Flags: CO_COROUTINE
99
000028 0 LOAD_FAST 0 (a)
1010
000022 2 GET_AWAITABLE
1111
000022 3 LOAD_NONE
12-
000022 >> 4 SEND 6 (to 10)
13-
000022 6 YIELD_VALUE
12+
000022 >> 4 SEND 10 (to 14)
13+
000022 >> 6 YIELD_VALUE
1414
000022 7 RESUME_YIELD
1515
000022 8 JUMP_BACKWARD 4 (to 4)
16-
000022 >> 10 POP_TOP
17-
000000 11 LOAD_NONE
18-
000000 12 RETURN_VALUE
16+
000022 10 THROW 4 (exc handler 7 - 10; stack: 1)
17+
000022 12 JUMP_BACKWARD 6 (to 6)
18+
000022 >> 14 POP_TOP
19+
000000 15 LOAD_NONE
20+
000000 16 RETURN_VALUE
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Disassembly of <module>:
2+
000000 0 LOAD_BYTE 1
3+
000000 2 RETURN_VALUE
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
Disassembly of <module>:
2+
000000 0 LOAD_BYTE 1
3+
000000 2 PRINT_EXPR
4+
000000 3 LOAD_NONE
5+
000000 4 RETURN_VALUE

graalpython/com.oracle.graal.python.test/testData/goldenFiles/CompilerTests/testYieldFrom.co

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ Flags: CO_GENERATOR
99
000027 0 LOAD_FAST 0 (a)
1010
000016 2 GET_ITER
1111
000016 3 LOAD_NONE
12-
000016 >> 4 SEND 6 (to 10)
13-
000016 6 YIELD_VALUE
12+
000016 >> 4 SEND 10 (to 14)
13+
000016 >> 6 YIELD_VALUE
1414
000016 7 RESUME_YIELD
1515
000016 8 JUMP_BACKWARD 4 (to 4)
16-
000016 >> 10 POP_TOP
17-
000000 11 LOAD_NONE
18-
000000 12 RETURN_VALUE
16+
000016 10 THROW 4 (exc handler 7 - 10; stack: 1)
17+
000016 12 JUMP_BACKWARD 6 (to 6)
18+
000016 >> 14 POP_TOP
19+
000000 15 LOAD_NONE
20+
000000 16 RETURN_VALUE

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,7 @@
207207
import com.oracle.graal.python.nodes.util.CastToJavaIntExactNode;
208208
import com.oracle.graal.python.nodes.util.CastToJavaLongExactNode;
209209
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
210+
import com.oracle.graal.python.pegparser.InputType;
210211
import com.oracle.graal.python.runtime.ExecutionContext.IndirectCallContext;
211212
import com.oracle.graal.python.runtime.PythonContext;
212213
import com.oracle.graal.python.runtime.PythonOptions;
@@ -1041,12 +1042,15 @@ PCode compile(String expression, String filename, String mode, int kwFlags, Obje
10411042
Supplier<CallTarget> createCode = () -> {
10421043
if (pm == ParserMode.File) {
10431044
Source source = PythonLanguage.newSource(context, finalCode, filename, mayBeFromFile, PythonLanguage.getCompileMimeType(kwOptimize));
1044-
return getContext().getEnv().parsePublic(source);
1045+
return context.getEnv().parsePublic(source);
10451046
} else if (pm == ParserMode.Eval) {
10461047
Source source = PythonLanguage.newSource(context, finalCode, filename, mayBeFromFile, PythonLanguage.getEvalMimeType(kwOptimize));
1047-
return getContext().getEnv().parsePublic(source);
1048+
return context.getEnv().parsePublic(source);
10481049
} else {
10491050
Source source = PythonLanguage.newSource(context, finalCode, filename, mayBeFromFile, PythonLanguage.MIME_TYPE);
1051+
if (context.getOption(PythonOptions.EnableBytecodeInterpreter)) {
1052+
return context.getLanguage().parseForBytecodeInterpreter(context, source, InputType.SINGLE, false, kwOptimize);
1053+
}
10501054
return PythonUtils.getOrCreateCallTarget((RootNode) getCore().getParser().parse(pm, kwOptimize, getCore(), source, null, null));
10511055
}
10521056
};

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/code/PCode.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,13 @@
5454
import com.oracle.graal.python.builtins.objects.str.StringUtils;
5555
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
5656
import com.oracle.graal.python.nodes.ModuleRootNode;
57-
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
5857
import com.oracle.graal.python.nodes.PClosureFunctionRootNode;
5958
import com.oracle.graal.python.nodes.PClosureRootNode;
6059
import com.oracle.graal.python.nodes.PRootNode;
6160
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
6261
import com.oracle.graal.python.nodes.argument.ReadVarKeywordsNode;
62+
import com.oracle.graal.python.nodes.bytecode.PBytecodeGeneratorFunctionRootNode;
63+
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
6364
import com.oracle.graal.python.nodes.expression.ExpressionNode;
6465
import com.oracle.graal.python.nodes.frame.GlobalNode;
6566
import com.oracle.graal.python.nodes.frame.PythonFrame;
@@ -359,7 +360,13 @@ public boolean visit(Node node) {
359360
}
360361

361362
private static RootNode rootNodeForExtraction(RootNode rootNode) {
362-
return (rootNode instanceof GeneratorFunctionRootNode) ? ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode() : rootNode;
363+
if (rootNode instanceof GeneratorFunctionRootNode) {
364+
return ((GeneratorFunctionRootNode) rootNode).getFunctionRootNode();
365+
}
366+
if (rootNode instanceof PBytecodeGeneratorFunctionRootNode) {
367+
return ((PBytecodeGeneratorFunctionRootNode) rootNode).getBytecodeRootNode();
368+
}
369+
return rootNode;
363370
}
364371

365372
@TruffleBoundary

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/function/PArguments.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2017, 2021, Oracle and/or its affiliates.
2+
* Copyright (c) 2017, 2022, Oracle and/or its affiliates.
33
* Copyright (c) 2013, Regents of the University of California
44
*
55
* All rights reserved.
@@ -33,6 +33,7 @@
3333
import com.oracle.graal.python.builtins.objects.object.PythonObject;
3434
import com.oracle.graal.python.runtime.exception.PException;
3535
import com.oracle.graal.python.util.PythonUtils;
36+
import com.oracle.truffle.api.CompilerAsserts;
3637
import com.oracle.truffle.api.CompilerDirectives.ValueType;
3738
import com.oracle.truffle.api.Truffle;
3839
import com.oracle.truffle.api.frame.Frame;
@@ -46,7 +47,7 @@
4647
* The layout of an argument array for a Python frame.
4748
*
4849
* +-------------------+
49-
* INDEX_VARIABLE_ARGUMENTS -> | Object[] |
50+
* INDEX_VARIABLE_ARGUMENTS -> | Object[] | This slot is also used to pass parent frame reference in bytecode OSR compilation.
5051
* +-------------------+
5152
* INDEX_KEYWORD_ARGUMENTS -> | PKeyword[] |
5253
* +-------------------+
@@ -279,6 +280,19 @@ public static PCell[] getClosure(Object[] arguments) {
279280
return (PCell[]) arguments[INDEX_CLOSURE];
280281
}
281282

283+
/*
284+
* We repurpose the varargs slot for storing the OSR frame. In the bytecode interpreter, varargs
285+
* should only be read once at the beginning of execute which is before OSR.
286+
*/
287+
public static void setOSRFrame(Object[] arguments, VirtualFrame parentFrame) {
288+
CompilerAsserts.neverPartOfCompilation();
289+
arguments[INDEX_VARIABLE_ARGUMENTS] = parentFrame;
290+
}
291+
292+
public static Frame getOSRFrame(Object[] arguments) {
293+
return (Frame) arguments[INDEX_VARIABLE_ARGUMENTS];
294+
}
295+
282296
public static PCell[] getClosure(Frame frame) {
283297
return getClosure(frame.getArguments());
284298
}

0 commit comments

Comments
 (0)