Skip to content

Commit d8f81e4

Browse files
committed
Load normal arguments and freevars in one respective instruction
1 parent 350b4a3 commit d8f81e4

File tree

2 files changed

+58
-63
lines changed

2 files changed

+58
-63
lines changed

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

Lines changed: 36 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,28 +1429,19 @@ 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) {
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+
}
14521443
b.emitCreateCells(cellVariableLocals);
1453-
for (int i = 0; i < cellVariableLocals.length; i++) {
1444+
for (int i = 0; i < cellVariables.length; i++) {
14541445
if (scope.getUseOfName(cellVariables[i]).contains(DefUse.DefParam)) {
14551446
/*
14561447
* To simplify the argument copying performed above, we copy cell params into
@@ -1467,10 +1458,15 @@ public void setUpFrame(ArgumentsTy args, Builder b) {
14671458
}
14681459
}
14691460

1470-
if (freeVariableLocals.length > 0) {
1471-
b.beginStoreRange(freeVariableLocals);
1472-
b.emitLoadClosure();
1473-
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);
14741470
}
14751471
}
14761472

@@ -1479,50 +1475,42 @@ private void copyArguments(ArgumentsTy args, Builder b) {
14791475
return;
14801476
}
14811477

1482-
int argIdx = PArguments.USER_ARGUMENTS_OFFSET;
1483-
if (args.posOnlyArgs != null) {
1484-
for (int i = 0; i < args.posOnlyArgs.length; i++) {
1485-
BytecodeLocal local = getLocal(args.posOnlyArgs[i].arg);
1486-
assert local != null;
1487-
b.beginStoreLocal(local);
1488-
b.emitLoadArgument(argIdx++);
1489-
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);
14901488
}
1491-
}
14921489

1493-
if (args.args != null) {
1494-
for (int i = 0; i < args.args.length; i++) {
1495-
BytecodeLocal local = getLocal(args.args[i].arg);
1496-
assert local != null;
1497-
b.beginStoreLocal(local);
1498-
b.emitLoadArgument(argIdx++);
1499-
b.endStoreLocal();
1490+
for (int i = 0; i < argsCount; i++) {
1491+
locals[idx++] = getLocal(args.args[i].arg);
15001492
}
1501-
}
15021493

1503-
if (args.kwOnlyArgs != null) {
1504-
for (int i = 0; i < args.kwOnlyArgs.length; i++) {
1505-
BytecodeLocal local = getLocal(args.kwOnlyArgs[i].arg);
1506-
assert local != null;
1507-
b.beginStoreLocal(local);
1508-
b.emitLoadArgument(argIdx++);
1509-
b.endStoreLocal();
1494+
for (int i = 0; i < kwOnlyArgsLength; i++) {
1495+
locals[idx++] = getLocal(args.kwOnlyArgs[i].arg);
15101496
}
1497+
1498+
b.emitCopyArguments(locals);
15111499
}
15121500

15131501
if (args.varArg != null) {
15141502
BytecodeLocal local = getLocal(args.varArg.arg);
15151503
assert local != null;
15161504
b.beginStoreLocal(local);
1517-
b.emitLoadVariableArguments(argIdx++);
1505+
b.emitLoadVariableArguments(idx++);
15181506
b.endStoreLocal();
15191507
}
15201508

15211509
if (args.kwArg != null) {
15221510
BytecodeLocal local = getLocal(args.kwArg.arg);
15231511
assert local != null;
15241512
b.beginStoreLocal(local);
1525-
b.emitLoadKeywordArguments(argIdx);
1513+
b.emitLoadKeywordArguments(idx);
15261514
b.endStoreLocal();
15271515
}
15281516
}

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

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,7 @@
263263
import com.oracle.truffle.api.frame.VirtualFrame;
264264
import com.oracle.truffle.api.library.CachedLibrary;
265265
import com.oracle.truffle.api.nodes.EncapsulatingNodeReference;
266+
import com.oracle.truffle.api.nodes.ExplodeLoop;
266267
import com.oracle.truffle.api.nodes.Node;
267268
import com.oracle.truffle.api.nodes.UnexpectedResultException;
268269
import com.oracle.truffle.api.object.DynamicObjectLibrary;
@@ -1050,13 +1051,26 @@ public static boolean hasLocals(VirtualFrame frame) {
10501051
}
10511052
}
10521053

1054+
@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+
10531067
@Operation
10541068
@ConstantOperand(type = int.class)
10551069
public static final class LoadVariableArguments {
10561070
@Specialization
10571071
public static Object perform(VirtualFrame frame, int index,
10581072
@Bind PBytecodeDSLRootNode rootNode) {
1059-
return PFactory.createTuple(rootNode.getLanguage(), (Object[]) frame.getArguments()[index]);
1073+
return PFactory.createTuple(rootNode.getLanguage(), (Object[]) PArguments.getArgument(frame, index));
10601074
}
10611075
}
10621076

@@ -1066,7 +1080,7 @@ public static final class LoadKeywordArguments {
10661080
@Specialization
10671081
public static Object perform(VirtualFrame frame, int index,
10681082
@Bind PBytecodeDSLRootNode rootNode) {
1069-
return PFactory.createDict(rootNode.getLanguage(), (PKeyword[]) frame.getArguments()[index]);
1083+
return PFactory.createDict(rootNode.getLanguage(), (PKeyword[]) PArguments.getArgument(frame, index));
10701084
}
10711085
}
10721086

@@ -2462,6 +2476,7 @@ public static void doStoreCell(PCell cell, Object value) {
24622476
@ConstantOperand(type = LocalRangeAccessor.class)
24632477
public static final class CreateCells {
24642478
@Specialization
2479+
@ExplodeLoop
24652480
public static void doCreateCells(VirtualFrame frame, LocalRangeAccessor locals,
24662481
@Bind PBytecodeDSLRootNode rootNode) {
24672482
for (int i = 0; i < locals.getLength(); i++) {
@@ -2494,24 +2509,16 @@ public static void doClearLocal(VirtualFrame frame, LocalAccessor localAccessor,
24942509
}
24952510
}
24962511

2497-
@Operation
2498-
public static final class LoadClosure {
2499-
@Specialization
2500-
public static PCell[] doLoadClosure(VirtualFrame frame) {
2501-
return PArguments.getFunctionObject(frame.getArguments()).getClosure();
2502-
}
2503-
}
2504-
25052512
@Operation
25062513
@ConstantOperand(type = LocalRangeAccessor.class)
2507-
public static final class StoreRange {
2514+
public static final class InitFreeVars {
25082515
@Specialization
2509-
public static void perform(VirtualFrame frame, LocalRangeAccessor locals, Object[] values,
2516+
@ExplodeLoop
2517+
public static void doLoadClosure(VirtualFrame frame, LocalRangeAccessor locals,
25102518
@Bind BytecodeNode bytecode) {
2511-
CompilerAsserts.partialEvaluationConstant(locals.getLength());
2512-
assert values.length == locals.getLength();
2519+
PCell[] closure = PArguments.getFunctionObject(frame.getArguments()).getClosure();
25132520
for (int i = 0; i < locals.getLength(); i++) {
2514-
locals.setObject(bytecode, frame, i, values[i]);
2521+
locals.setObject(bytecode, frame, i, closure[i]);
25152522
}
25162523
}
25172524
}

0 commit comments

Comments
 (0)