Skip to content

Commit bc38716

Browse files
committed
[GR-69452] Create cell assumptions ahead of execution in DSL interpreter
PullRequest: graalpython/3988
2 parents fc07038 + 9c7069f commit bc38716

File tree

4 files changed

+91
-91
lines changed

4 files changed

+91
-91
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/traceback/TracebackBuiltins.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,10 @@
3737

3838
import com.oracle.graal.python.PythonLanguage;
3939
import com.oracle.graal.python.annotations.ArgumentClinic;
40+
import com.oracle.graal.python.annotations.Builtin;
4041
import com.oracle.graal.python.annotations.Slot;
4142
import com.oracle.graal.python.annotations.Slot.SlotKind;
4243
import com.oracle.graal.python.annotations.Slot.SlotSignature;
43-
import com.oracle.graal.python.annotations.Builtin;
4444
import com.oracle.graal.python.builtins.CoreFunctions;
4545
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4646
import com.oracle.graal.python.builtins.PythonBuiltins;
@@ -61,12 +61,14 @@
6161
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
6262
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
6363
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
64+
import com.oracle.graal.python.runtime.PythonOptions;
6465
import com.oracle.graal.python.runtime.exception.PException;
6566
import com.oracle.graal.python.runtime.exception.PythonErrorType;
6667
import com.oracle.graal.python.runtime.object.PFactory;
6768
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
6869
import com.oracle.truffle.api.TruffleStackTrace;
6970
import com.oracle.truffle.api.TruffleStackTraceElement;
71+
import com.oracle.truffle.api.bytecode.ContinuationRootNode;
7072
import com.oracle.truffle.api.dsl.Bind;
7173
import com.oracle.truffle.api.dsl.Cached;
7274
import com.oracle.truffle.api.dsl.Cached.Exclusive;
@@ -165,6 +167,7 @@ static void doExisting(@SuppressWarnings("unused") PTraceback tb) {
165167
@TruffleBoundary
166168
@Specialization(guards = "!tb.isMaterialized()")
167169
static void doMaterialize(Node inliningTarget, PTraceback tb,
170+
@Bind PythonLanguage language,
168171
@Cached(inline = false) MaterializeFrameNode materializeFrameNode,
169172
@Cached MaterializeLazyTracebackNode materializeLazyTracebackNode) {
170173
/*
@@ -192,10 +195,14 @@ static void doMaterialize(Node inliningTarget, PTraceback tb,
192195
TruffleStackTraceElement element = stackTrace.get(truffleIndex);
193196
if (LazyTraceback.elementWantedForTraceback(element)) {
194197
PFrame pFrame = materializeFrame(element, materializeFrameNode);
195-
next = PFactory.createTraceback(PythonLanguage.get(null), pFrame, pFrame.getLine(), next);
198+
next = PFactory.createTraceback(language, pFrame, pFrame.getLine(), next);
196199
next.setLocation(pFrame.getBci(), pFrame.getBytecodeNode());
197200
pyIndex++;
198201
}
202+
// XXX workaround for DSL continuations not capturing frames
203+
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER && element.getTarget().getRootNode() instanceof ContinuationRootNode && element.getFrame() == null) {
204+
pyIndex++;
205+
}
199206
}
200207
}
201208
if (lazyTraceback.catchingFrameWantedForTraceback()) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/bytecode_dsl/RootNodeCompiler.java

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,58 +1429,44 @@ public void setUpFrame(ArgumentsTy args, Builder b) {
14291429
}
14301430
}
14311431

1432-
String[] cellVariables = orderedKeys(cellvars, new String[0]);
1433-
BytecodeLocal[] cellVariableLocals = new BytecodeLocal[cellVariables.length];
1434-
for (int i = 0; i < cellVariables.length; i++) {
1435-
BytecodeLocal local = b.createLocal();
1436-
cellLocals.put(cellVariables[i], local);
1437-
cellVariableLocals[i] = local;
1438-
}
1439-
1440-
String[] freeVariables = orderedKeys(freevars, new String[0]);
1441-
BytecodeLocal[] freeVariableLocals = new BytecodeLocal[freeVariables.length];
1442-
for (int i = 0; i < freeVariables.length; i++) {
1443-
BytecodeLocal local = b.createLocal();
1444-
freeLocals.put(freeVariables[i], local);
1445-
freeVariableLocals[i] = local;
1446-
}
1447-
14481432
// 2. Copy arguments, initialize cells, and copy free variables.
14491433
copyArguments(args, b);
14501434

1451-
if (cellVariableLocals.length > 0) {
1452-
List<BytecodeLocal> toClear = new ArrayList<>();
1453-
1454-
b.beginStoreRange(cellVariableLocals);
1455-
b.beginCollectToObjectArray();
1456-
for (int i = 0; i < cellVariableLocals.length; i++) {
1457-
b.beginCreateCell();
1435+
if (!cellvars.isEmpty()) {
1436+
String[] cellVariables = orderedKeys(cellvars, new String[0]);
1437+
BytecodeLocal[] cellVariableLocals = new BytecodeLocal[cellVariables.length];
1438+
for (int i = 0; i < cellVariables.length; i++) {
1439+
BytecodeLocal local = b.createLocal();
1440+
cellLocals.put(cellVariables[i], local);
1441+
cellVariableLocals[i] = local;
1442+
}
1443+
b.emitCreateCells(cellVariableLocals);
1444+
for (int i = 0; i < cellVariables.length; i++) {
14581445
if (scope.getUseOfName(cellVariables[i]).contains(DefUse.DefParam)) {
14591446
/*
14601447
* To simplify the argument copying performed above, we copy cell params into
14611448
* regular locals just like all other arguments. Then, here we move the value
14621449
* into a cell and clear the regular local.
14631450
*/
14641451
BytecodeLocal param = getLocal(cellVariables[i]);
1452+
b.beginStoreCell();
1453+
b.emitLoadLocal(cellVariableLocals[i]);
14651454
b.emitLoadLocal(param);
1466-
toClear.add(param);
1467-
} else {
1468-
b.emitLoadNull();
1455+
b.endStoreCell();
1456+
b.emitClearLocal(param);
14691457
}
1470-
b.endCreateCell();
1471-
}
1472-
b.endCollectToObjectArray();
1473-
b.endStoreRange();
1474-
1475-
for (BytecodeLocal local : toClear) {
1476-
b.emitClearLocal(local);
14771458
}
14781459
}
14791460

1480-
if (freeVariableLocals.length > 0) {
1481-
b.beginStoreRange(freeVariableLocals);
1482-
b.emitLoadClosure();
1483-
b.endStoreRange();
1461+
if (!freevars.isEmpty()) {
1462+
String[] freeVariables = orderedKeys(freevars, new String[0]);
1463+
BytecodeLocal[] freeVariableLocals = new BytecodeLocal[freeVariables.length];
1464+
for (int i = 0; i < freeVariables.length; i++) {
1465+
BytecodeLocal local = b.createLocal();
1466+
freeLocals.put(freeVariables[i], local);
1467+
freeVariableLocals[i] = local;
1468+
}
1469+
b.emitInitFreeVars(freeVariableLocals);
14841470
}
14851471
}
14861472

@@ -1489,50 +1475,42 @@ private void copyArguments(ArgumentsTy args, Builder b) {
14891475
return;
14901476
}
14911477

1492-
int argIdx = PArguments.USER_ARGUMENTS_OFFSET;
1493-
if (args.posOnlyArgs != null) {
1494-
for (int i = 0; i < args.posOnlyArgs.length; i++) {
1495-
BytecodeLocal local = getLocal(args.posOnlyArgs[i].arg);
1496-
assert local != null;
1497-
b.beginStoreLocal(local);
1498-
b.emitLoadArgument(argIdx++);
1499-
b.endStoreLocal();
1478+
int idx = 0;
1479+
int posOnlyArgsCount = args.posOnlyArgs != null ? args.posOnlyArgs.length : 0;
1480+
int argsCount = args.args != null ? args.args.length : 0;
1481+
int kwOnlyArgsLength = args.kwOnlyArgs != null ? args.kwOnlyArgs.length : 0;
1482+
int totalLocals = posOnlyArgsCount + argsCount + kwOnlyArgsLength;
1483+
if (totalLocals > 0) {
1484+
BytecodeLocal[] locals = new BytecodeLocal[totalLocals];
1485+
1486+
for (int i = 0; i < posOnlyArgsCount; i++) {
1487+
locals[idx++] = getLocal(args.posOnlyArgs[i].arg);
15001488
}
1501-
}
15021489

1503-
if (args.args != null) {
1504-
for (int i = 0; i < args.args.length; i++) {
1505-
BytecodeLocal local = getLocal(args.args[i].arg);
1506-
assert local != null;
1507-
b.beginStoreLocal(local);
1508-
b.emitLoadArgument(argIdx++);
1509-
b.endStoreLocal();
1490+
for (int i = 0; i < argsCount; i++) {
1491+
locals[idx++] = getLocal(args.args[i].arg);
15101492
}
1511-
}
15121493

1513-
if (args.kwOnlyArgs != null) {
1514-
for (int i = 0; i < args.kwOnlyArgs.length; i++) {
1515-
BytecodeLocal local = getLocal(args.kwOnlyArgs[i].arg);
1516-
assert local != null;
1517-
b.beginStoreLocal(local);
1518-
b.emitLoadArgument(argIdx++);
1519-
b.endStoreLocal();
1494+
for (int i = 0; i < kwOnlyArgsLength; i++) {
1495+
locals[idx++] = getLocal(args.kwOnlyArgs[i].arg);
15201496
}
1497+
1498+
b.emitCopyArguments(locals);
15211499
}
15221500

15231501
if (args.varArg != null) {
15241502
BytecodeLocal local = getLocal(args.varArg.arg);
15251503
assert local != null;
15261504
b.beginStoreLocal(local);
1527-
b.emitLoadVariableArguments();
1505+
b.emitLoadVariableArguments(idx++);
15281506
b.endStoreLocal();
15291507
}
15301508

15311509
if (args.kwArg != null) {
15321510
BytecodeLocal local = getLocal(args.kwArg.arg);
15331511
assert local != null;
15341512
b.beginStoreLocal(local);
1535-
b.emitLoadKeywordArguments();
1513+
b.emitLoadKeywordArguments(idx);
15361514
b.endStoreLocal();
15371515
}
15381516
}

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

Lines changed: 40 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,7 @@
228228
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
229229
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
230230
import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff;
231+
import com.oracle.truffle.api.Truffle;
231232
import com.oracle.truffle.api.bytecode.BytecodeConfig;
232233
import com.oracle.truffle.api.bytecode.BytecodeLocation;
233234
import com.oracle.truffle.api.bytecode.BytecodeNode;
@@ -262,6 +263,7 @@
262263
import com.oracle.truffle.api.frame.VirtualFrame;
263264
import com.oracle.truffle.api.library.CachedLibrary;
264265
import com.oracle.truffle.api.nodes.EncapsulatingNodeReference;
266+
import com.oracle.truffle.api.nodes.ExplodeLoop;
265267
import com.oracle.truffle.api.nodes.Node;
266268
import com.oracle.truffle.api.nodes.UnexpectedResultException;
267269
import com.oracle.truffle.api.object.DynamicObjectLibrary;
@@ -345,6 +347,7 @@ public abstract class PBytecodeDSLRootNode extends PRootNode implements Bytecode
345347
@CompilationFinal protected transient int selfIndex;
346348
@CompilationFinal protected transient int classcellIndex;
347349
@CompilationFinal public int yieldFromGeneratorIndex = -1;
350+
@CompilationFinal(dimensions = 1) protected transient Assumption[] cellEffectivelyFinalAssumptions;
348351

349352
private transient boolean pythonInternal;
350353
@CompilationFinal private transient boolean internal;
@@ -370,6 +373,12 @@ public void setMetadata(BytecodeDSLCodeUnit co, ParserCallbacksImpl parserErrorC
370373
this.selfIndex = co.selfIndex;
371374
this.internal = getSource().isInternal();
372375
this.parserErrorCallback = parserErrorCallback;
376+
if (co.cellvars.length > 0) {
377+
this.cellEffectivelyFinalAssumptions = new Assumption[co.cellvars.length];
378+
for (int i = 0; i < co.cellvars.length; i++) {
379+
cellEffectivelyFinalAssumptions[i] = Truffle.getRuntime().createAssumption("cell is effectively final");
380+
}
381+
}
373382
}
374383

375384
@Override
@@ -1043,24 +1052,34 @@ public static boolean hasLocals(VirtualFrame frame) {
10431052
}
10441053

10451054
@Operation
1055+
@ConstantOperand(type = LocalRangeAccessor.class)
1056+
public static final class CopyArguments {
1057+
@Specialization
1058+
@ExplodeLoop
1059+
public static void perform(VirtualFrame frame, LocalRangeAccessor locals,
1060+
@Bind BytecodeNode bytecodeNode) {
1061+
for (int i = 0; i < locals.getLength(); i++) {
1062+
locals.setObject(bytecodeNode, frame, i, PArguments.getArgument(frame, i));
1063+
}
1064+
}
1065+
}
1066+
1067+
@Operation
1068+
@ConstantOperand(type = int.class)
10461069
public static final class LoadVariableArguments {
10471070
@Specialization
1048-
public static Object perform(VirtualFrame frame,
1071+
public static Object perform(VirtualFrame frame, int index,
10491072
@Bind PBytecodeDSLRootNode rootNode) {
1050-
int index = rootNode.co.getRegularArgCount();
10511073
return PFactory.createTuple(rootNode.getLanguage(), (Object[]) PArguments.getArgument(frame, index));
10521074
}
10531075
}
10541076

10551077
@Operation
1078+
@ConstantOperand(type = int.class)
10561079
public static final class LoadKeywordArguments {
10571080
@Specialization
1058-
public static Object perform(VirtualFrame frame,
1081+
public static Object perform(VirtualFrame frame, int index,
10591082
@Bind PBytecodeDSLRootNode rootNode) {
1060-
int index = rootNode.co.getRegularArgCount();
1061-
if (rootNode.co.takesVarArgs()) {
1062-
index++;
1063-
}
10641083
return PFactory.createDict(rootNode.getLanguage(), (PKeyword[]) PArguments.getArgument(frame, index));
10651084
}
10661085
}
@@ -2454,12 +2473,16 @@ public static void doStoreCell(PCell cell, Object value) {
24542473
}
24552474

24562475
@Operation
2457-
public static final class CreateCell {
2476+
@ConstantOperand(type = LocalRangeAccessor.class)
2477+
public static final class CreateCells {
24582478
@Specialization
2459-
public static PCell doCreateCell(Object value) {
2460-
PCell cell = new PCell(Assumption.create());
2461-
cell.setRef(value);
2462-
return cell;
2479+
@ExplodeLoop
2480+
public static void doCreateCells(VirtualFrame frame, LocalRangeAccessor locals,
2481+
@Bind PBytecodeDSLRootNode rootNode) {
2482+
for (int i = 0; i < locals.getLength(); i++) {
2483+
PCell cell = new PCell(rootNode.cellEffectivelyFinalAssumptions[i]);
2484+
locals.setObject(rootNode.getBytecodeNode(), frame, i, cell);
2485+
}
24632486
}
24642487
}
24652488

@@ -2486,24 +2509,16 @@ public static void doClearLocal(VirtualFrame frame, LocalAccessor localAccessor,
24862509
}
24872510
}
24882511

2489-
@Operation
2490-
public static final class LoadClosure {
2491-
@Specialization
2492-
public static PCell[] doLoadClosure(VirtualFrame frame) {
2493-
return PArguments.getFunctionObject(frame.getArguments()).getClosure();
2494-
}
2495-
}
2496-
24972512
@Operation
24982513
@ConstantOperand(type = LocalRangeAccessor.class)
2499-
public static final class StoreRange {
2514+
public static final class InitFreeVars {
25002515
@Specialization
2501-
public static void perform(VirtualFrame frame, LocalRangeAccessor locals, Object[] values,
2516+
@ExplodeLoop
2517+
public static void doLoadClosure(VirtualFrame frame, LocalRangeAccessor locals,
25022518
@Bind BytecodeNode bytecode) {
2503-
CompilerAsserts.partialEvaluationConstant(locals.getLength());
2504-
assert values.length == locals.getLength();
2519+
PCell[] closure = PArguments.getFunctionObject(frame.getArguments()).getClosure();
25052520
for (int i = 0; i < locals.getLength(); i++) {
2506-
locals.setObject(bytecode, frame, i, values[i]);
2521+
locals.setObject(bytecode, frame, i, closure[i]);
25072522
}
25082523
}
25092524
}

mx.graalpython/mx_graalpython_benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def interpreter(self):
292292
def post_process_command_line_args(self, args):
293293
if os.environ.get('BYTECODE_DSL_INTERPRETER', '').lower() == 'true' and not self.is_bytecode_dsl_config():
294294
print("Found environment variable BYTECODE_DSL_INTERPRETER, but the guest vm config is not Bytecode DSL config.")
295-
print("Did you want to use, e.g., `mx benchmark ... -- --host-vm-config=default-bc-dsl`?")
295+
print("Did you want to use, e.g., `mx benchmark ... -- --python-vm-config=default-bc-dsl`?")
296296
sys.exit(1)
297297
return self.get_extra_polyglot_args() + args
298298

0 commit comments

Comments
 (0)