Skip to content

Commit b71a1a8

Browse files
committed
Get rid of outputCanQuicken bytearrays
1 parent 702b493 commit b71a1a8

File tree

8 files changed

+250
-188
lines changed

8 files changed

+250
-188
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,6 @@ private BytecodeCodeUnit readBytecodeCodeUnit() {
13711371
int startColumn = readInt();
13721372
int endLine = readInt();
13731373
int endColumn = readInt();
1374-
byte[] outputCanQuicken = readBytes();
13751374
byte[] variableShouldUnbox = readBytes();
13761375
int[] generalizeInputsKeys = readIntArray();
13771376
int[] generalizeInputsIndices = readIntArray();
@@ -1381,7 +1380,7 @@ private BytecodeCodeUnit readBytecodeCodeUnit() {
13811380
return new BytecodeCodeUnit(name, qualname, argCount, kwOnlyArgCount, positionalOnlyArgCount, flags, names, varnames,
13821381
cellvars, freevars, cell2arg, constants, startLine, startColumn, endLine, endColumn, code, srcOffsetTable,
13831382
primitiveConstants, exceptionHandlerRanges, stacksize, conditionProfileCount,
1384-
outputCanQuicken, variableShouldUnbox, generalizeInputsKeys, generalizeInputsIndices, generalizeInputsValues, generalizeVarsIndices, generalizeVarsValues);
1383+
variableShouldUnbox, generalizeInputsKeys, generalizeInputsIndices, generalizeInputsValues, generalizeVarsIndices, generalizeVarsValues);
13851384
}
13861385

13871386
private BytecodeDSLCodeUnit readBytecodeDSLCodeUnit() {
@@ -1436,7 +1435,7 @@ private void writeBytecodeCodeUnit(BytecodeCodeUnit code) throws IOException {
14361435
writeInt(code.kwOnlyArgCount);
14371436
writeInt(code.positionalOnlyArgCount);
14381437
writeInt(code.stacksize);
1439-
writeBytes(code.getBytecodeForSerialization());
1438+
writeBytes(code.code);
14401439
writeBytes(code.srcOffsetTable);
14411440
writeInt(code.flags);
14421441
writeStringArray(code.names);
@@ -1456,7 +1455,6 @@ private void writeBytecodeCodeUnit(BytecodeCodeUnit code) throws IOException {
14561455
writeInt(code.startColumn);
14571456
writeInt(code.endLine);
14581457
writeInt(code.endColumn);
1459-
writeBytes(code.outputCanQuicken);
14601458
writeBytes(code.variableShouldUnbox);
14611459
writeIntArray(code.generalizeInputsKeys);
14621460
writeIntArray(code.generalizeInputsIndices);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private RootCallTarget deserializeForBytecodeInterpreter(PythonLanguage language
174174
code.constants, code.startLine,
175175
code.startColumn, code.endLine, code.endColumn, code.code, code.srcOffsetTable,
176176
code.primitiveConstants, code.exceptionHandlerRanges, code.stacksize, code.conditionProfileCount,
177-
code.outputCanQuicken, code.variableShouldUnbox,
177+
code.variableShouldUnbox,
178178
code.generalizeInputsKeys, code.generalizeInputsIndices, code.generalizeInputsValues, code.generalizeVarsIndices, code.generalizeVarsValues);
179179
}
180180
rootNode = PBytecodeRootNode.create(context.getLanguage(), code, new LazySource(PythonUtils.createFakeSource()), false);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/BytecodeCodeUnit.java

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,6 @@ public final class BytecodeCodeUnit extends CodeUnit {
7373
public final int conditionProfileCount;
7474

7575
/* Quickening data. See docs in PBytecodeRootNode */
76-
@CompilationFinal(dimensions = 1) public final byte[] outputCanQuicken;
7776
@CompilationFinal(dimensions = 1) public final byte[] variableShouldUnbox;
7877
@CompilationFinal(dimensions = 1) public final int[] generalizeVarsIndices;
7978
@CompilationFinal(dimensions = 1) public final int[] generalizeVarsValues;
@@ -93,7 +92,7 @@ public BytecodeCodeUnit(TruffleString name, TruffleString qualname,
9392
int endLine, int endColumn,
9493
byte[] code, byte[] linetable,
9594
long[] primitiveConstants, int[] exceptionHandlerRanges, int stacksize, int conditionProfileCount,
96-
byte[] outputCanQuicken, byte[] variableShouldUnbox, int[] generalizeInputsKeys, int[] generalizeInputsIndices, int[] generalizeInputsValues,
95+
byte[] variableShouldUnbox, int[] generalizeInputsKeys, int[] generalizeInputsIndices, int[] generalizeInputsValues,
9796
int[] generalizeVarsIndices, int[] generalizeVarsValues) {
9897
super(name, qualname, argCount, kwOnlyArgCount, positionalOnlyArgCount, flags, names, varnames, cellvars, freevars, cell2arg, constants, startLine, startColumn, endLine, endColumn);
9998
this.code = code;
@@ -102,7 +101,6 @@ public BytecodeCodeUnit(TruffleString name, TruffleString qualname,
102101
this.exceptionHandlerRanges = exceptionHandlerRanges;
103102
this.stacksize = stacksize;
104103
this.conditionProfileCount = conditionProfileCount;
105-
this.outputCanQuicken = outputCanQuicken;
106104
this.variableShouldUnbox = variableShouldUnbox;
107105
this.generalizeInputsKeys = generalizeInputsKeys;
108106
this.generalizeInputsIndices = generalizeInputsIndices;
@@ -161,7 +159,7 @@ protected void dumpBytecode(StringBuilder sb, boolean quickened) {
161159
OpCodes opcode = OpCodes.fromOpCode(code[bci++]);
162160

163161
if (!quickened) {
164-
opcode = unquicken(opcode);
162+
opcode = opcode.quickens != null ? opcode.quickens : opcode;
165163
}
166164

167165
String[] line = lines.computeIfAbsent(bcBCI, k -> new String[DISASSEMBLY_NUM_COLUMNS]);
@@ -378,26 +376,15 @@ protected void dumpBytecode(StringBuilder sb, boolean quickened) {
378376
line[5] = line[5] == null ? "" : String.format("(%s)", line[5]);
379377
line[6] = line[6] == null ? "" : String.format("(%s)", line[6]);
380378
line[7] = "";
381-
int generalizationIndex2 = -1;
382379
if (generalizeInputsKeys != null && generalizationIndex1 < generalizeInputsKeys.length && generalizeInputsKeys[generalizationIndex1] == bci) {
383-
generalizationIndex2 = generalizeInputsIndices[generalizationIndex1++];
384-
}
385-
if (outputCanQuicken != null && (outputCanQuicken[bci] != 0 || generalizationIndex2 >= 0)) {
380+
int generalizationIndex2 = generalizeInputsIndices[generalizationIndex1++];
386381
StringBuilder quickenSb = new StringBuilder();
387-
if (outputCanQuicken[bci] != 0) {
388-
quickenSb.append("can quicken");
389-
}
390-
if (generalizationIndex2 >= 0) {
391-
if (!quickenSb.isEmpty()) {
382+
quickenSb.append("generalizes: ");
383+
for (int i = generalizationIndex2; i < generalizeInputsIndices[generalizationIndex1]; i++) {
384+
if (i > generalizationIndex2) {
392385
quickenSb.append(", ");
393386
}
394-
quickenSb.append("generalizes: ");
395-
for (int i = generalizationIndex2; i < generalizeInputsIndices[generalizationIndex1]; i++) {
396-
if (i > generalizationIndex2) {
397-
quickenSb.append(", ");
398-
}
399-
quickenSb.append(generalizeInputsValues[i]);
400-
}
387+
quickenSb.append(generalizeInputsValues[i]);
401388
}
402389
line[7] = quickenSb.toString();
403390
}
@@ -674,7 +661,7 @@ private static int opCodeAt(byte[] bytecode, int bci, BytecodeAction action) {
674661
bci += 2;
675662
op = OpCodes.fromOpCode(bytecode[bci]);
676663
}
677-
op = unquicken(op);
664+
op = op.quickens != null ? op.quickens : op;
678665
byte[] followingArgs = null;
679666
if (op.argLength > 0) {
680667
oparg |= Byte.toUnsignedInt(bytecode[bci + 1]);
@@ -696,26 +683,4 @@ public static void iterateBytecode(byte[] bytecode, BytecodeAction action) {
696683
public void iterateBytecode(BytecodeAction action) {
697684
iterateBytecode(code, action);
698685
}
699-
700-
public byte[] getBytecodeForSerialization() {
701-
byte[] bytecode = Arrays.copyOf(code, code.length);
702-
for (int bci = 0; bci < bytecode.length;) {
703-
OpCodes op = OpCodes.fromOpCode(bytecode[bci]);
704-
bytecode[bci] = (byte) unquicken(op).ordinal();
705-
bci += op.length();
706-
}
707-
return bytecode;
708-
}
709-
710-
private static OpCodes unquicken(OpCodes op) {
711-
if (op.quickens == null) {
712-
return op;
713-
}
714-
return switch (op.quickens) {
715-
// These are already quickened by the compiler, so keep them quickened
716-
// See CompilationUnit.emitBytecode
717-
case LOAD_BYTE, LOAD_INT, LOAD_LONG, LOAD_DOUBLE, LOAD_TRUE, LOAD_FALSE -> op;
718-
default -> op.quickens;
719-
};
720-
}
721686
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/CompilationUnit.java

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,13 @@ public BytecodeCodeUnit assemble() {
225225
addExceptionRange(finishedExceptionHandlerRanges, start, end, handlerBci, stackLevel);
226226
}
227227
for (Instruction i : b.instr) {
228-
if (i.quickenOutput != 0 || i.quickeningGeneralizeList != null) {
228+
if (i.quickenOutput || i.quickeningGeneralizeList != null) {
229229
quickenedInstructions.add(i);
230230
}
231231
if (i.opcode == OpCodes.STORE_FAST) {
232232
variableStores.get(i.arg).add(i);
233233
} else if (i.opcode == OpCodes.LOAD_FAST) {
234-
boxingMetric[i.arg] += i.quickenOutput != 0 ? quickenMetricWeight : -quickenMetricWeight;
234+
boxingMetric[i.arg] += i.quickenOutput ? quickenMetricWeight : -quickenMetricWeight;
235235
}
236236
i.bci = buf.size();
237237
emitBytecode(i, buf, sourceMapBuilder);
@@ -286,12 +286,10 @@ public BytecodeCodeUnit assemble() {
286286
int[] generalizeInputsKeys = generizationListSize > 0 ? new int[generizationListSize] : EMPTY_INT_ARRAY;
287287
int[] generalizeInputsIndices = generizationListSize > 0 ? new int[generizationListSize + 1] : EMPTY_INT_ARRAY;
288288
int[] generalizeInputsValues = generizationListSize > 0 ? new int[totalGeneralizations] : EMPTY_INT_ARRAY;
289-
byte[] finishedCanQuickenOutput = new byte[buf.size()];
290289
int generalizationIndex1 = 0;
291290
int generalizationIndex2 = 0;
292291
for (Instruction insn : quickenedInstructions) {
293292
int insnBodyBci = insn.bodyBci();
294-
finishedCanQuickenOutput[insnBodyBci] = insn.quickenOutput;
295293
if (insn.quickeningGeneralizeList != null && !insn.quickeningGeneralizeList.isEmpty()) {
296294
generalizeInputsKeys[generalizationIndex1] = insnBodyBci;
297295
generalizeInputsIndices[generalizationIndex1] = generalizationIndex2;
@@ -358,7 +356,7 @@ public BytecodeCodeUnit assemble() {
358356
buf.toByteArray(),
359357
sourceMapBuilder.build(),
360358
orderedLong(primitiveConstants), exceptionHandlerRanges, maxStackSize, conditionProfileCount,
361-
finishedCanQuickenOutput, shouldUnboxVariable, generalizeInputsKeys, generalizeInputsIndices, generalizeInputsValues, generalizeVarsIndices, generalizeVarsValues);
359+
shouldUnboxVariable, generalizeInputsKeys, generalizeInputsIndices, generalizeInputsValues, generalizeVarsIndices, generalizeVarsValues);
362360
}
363361

364362
private static void addExceptionRange(Collection<int[]> finishedExceptionHandlerRanges, int start, int end, int handler, int stackLevel) {
@@ -459,17 +457,31 @@ private static void emitBytecode(Instruction instr, ByteArrayOutputStream buf, S
459457
OpCodes opcode = instr.opcode;
460458
// Pre-quicken constant loads
461459
if (opcode == OpCodes.LOAD_BYTE) {
462-
opcode = (instr.quickenOutput & QuickeningTypes.INT) != 0 ? OpCodes.LOAD_BYTE_I : OpCodes.LOAD_BYTE_O;
460+
opcode = instr.quickenOutput ? OpCodes.LOAD_BYTE_I : OpCodes.LOAD_BYTE_O;
463461
} else if (opcode == OpCodes.LOAD_INT) {
464-
opcode = (instr.quickenOutput & QuickeningTypes.INT) != 0 ? OpCodes.LOAD_INT_I : OpCodes.LOAD_INT_O;
462+
opcode = instr.quickenOutput ? OpCodes.LOAD_INT_I : OpCodes.LOAD_INT_O;
465463
} else if (opcode == OpCodes.LOAD_LONG) {
466-
opcode = (instr.quickenOutput & QuickeningTypes.LONG) != 0 ? OpCodes.LOAD_LONG_L : OpCodes.LOAD_LONG_O;
464+
opcode = instr.quickenOutput ? OpCodes.LOAD_LONG_L : OpCodes.LOAD_LONG_O;
467465
} else if (opcode == OpCodes.LOAD_DOUBLE) {
468-
opcode = (instr.quickenOutput & QuickeningTypes.DOUBLE) != 0 ? OpCodes.LOAD_DOUBLE_D : OpCodes.LOAD_DOUBLE_O;
466+
opcode = instr.quickenOutput ? OpCodes.LOAD_DOUBLE_D : OpCodes.LOAD_DOUBLE_O;
469467
} else if (opcode == OpCodes.LOAD_TRUE) {
470-
opcode = (instr.quickenOutput & QuickeningTypes.BOOLEAN) != 0 ? OpCodes.LOAD_TRUE_B : OpCodes.LOAD_TRUE_O;
468+
opcode = instr.quickenOutput ? OpCodes.LOAD_TRUE_B : OpCodes.LOAD_TRUE_O;
471469
} else if (opcode == OpCodes.LOAD_FALSE) {
472-
opcode = (instr.quickenOutput & QuickeningTypes.BOOLEAN) != 0 ? OpCodes.LOAD_FALSE_B : OpCodes.LOAD_FALSE_O;
470+
opcode = instr.quickenOutput ? OpCodes.LOAD_FALSE_B : OpCodes.LOAD_FALSE_O;
471+
} else if (opcode == OpCodes.LOAD_FAST) {
472+
opcode = instr.quickenOutput ? OpCodes.LOAD_FAST : OpCodes.LOAD_FAST_ADAPTIVE_O;
473+
} else if (opcode == OpCodes.FOR_ITER) {
474+
opcode = instr.quickenOutput ? OpCodes.FOR_ITER_I : OpCodes.FOR_ITER_O;
475+
} else if (!instr.quickenOutput) {
476+
if (opcode == OpCodes.UNARY_OP) {
477+
opcode = OpCodes.UNARY_OP_ADAPTIVE_O;
478+
} else if (opcode == OpCodes.BINARY_OP) {
479+
opcode = OpCodes.BINARY_OP_ADAPTIVE_O;
480+
} else if (opcode == OpCodes.BINARY_SUBSCR) {
481+
opcode = OpCodes.BINARY_SUBSCR_ADAPTIVE_O;
482+
} else if (opcode.generalizesTo != null) {
483+
opcode = instr.opcode.generalizesTo;
484+
}
473485
}
474486
assert opcode.ordinal() < 256;
475487
SourceRange location = instr.location;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/Compiler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ void pushOp(Instruction insn) {
359359
Instruction index = popQuickeningStack();
360360
popQuickeningStack(); // Ignore the collection, it's always object
361361
if (index != null && (index.opcode.canQuickenOutputTypes() & QuickeningTypes.INT) != 0) {
362-
index.quickenOutput = QuickeningTypes.INT;
362+
index.quickenOutput = true;
363363
insn.quickeningGeneralizeList = List.of(index);
364364
}
365365
quickeningStack.add(insn);
@@ -370,9 +370,9 @@ void pushOp(Instruction insn) {
370370
popQuickeningStack(); // Ignore the collection, it's always object
371371
Instruction value = popQuickeningStack();
372372
if (index != null && (index.opcode.canQuickenOutputTypes() & QuickeningTypes.INT) != 0) {
373-
index.quickenOutput = QuickeningTypes.INT;
373+
index.quickenOutput = true;
374374
if (value != null && (value.opcode.canQuickenOutputTypes() & canQuickenInputTypes) != 0) {
375-
value.quickenOutput = canQuickenInputTypes;
375+
value.quickenOutput = true;
376376
insn.quickeningGeneralizeList = List.of(value, index);
377377
} else {
378378
insn.quickeningGeneralizeList = List.of(index);
@@ -415,7 +415,7 @@ void pushOp(Instruction insn) {
415415
}
416416
if (canQuickenInputTypes != 0 && inputs != null) {
417417
for (int i = 0; i < inputs.size(); i++) {
418-
inputs.get(i).quickenOutput = canQuickenInputTypes;
418+
inputs.get(i).quickenOutput = true;
419419
}
420420
}
421421
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/Instruction.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2021, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2021, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* The Universal Permissive License (UPL), Version 1.0
@@ -57,7 +57,7 @@ final class Instruction {
5757
* extended args
5858
*/
5959
public int bci = -1;
60-
public byte quickenOutput;
60+
public boolean quickenOutput;
6161
public List<Instruction> quickeningGeneralizeList;
6262

6363
Instruction(OpCodes opcode, int arg, byte[] followingArgs, Block target, SourceRange location) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/compiler/OpCodes.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ public enum OpCodes {
850850
LOAD_DOUBLE_O(LOAD_DOUBLE, 0, QuickeningTypes.OBJECT),
851851
LOAD_DOUBLE_D(LOAD_DOUBLE, 0, QuickeningTypes.DOUBLE, LOAD_DOUBLE_O),
852852
LOAD_FAST_O(LOAD_FAST, 0, QuickeningTypes.OBJECT),
853+
LOAD_FAST_ADAPTIVE_O(LOAD_FAST, 0, QuickeningTypes.OBJECT),
853854
LOAD_FAST_I_BOX(LOAD_FAST, 0, QuickeningTypes.OBJECT),
854855
LOAD_FAST_I(LOAD_FAST, 0, QuickeningTypes.INT, LOAD_FAST_I_BOX),
855856
LOAD_FAST_L_BOX(LOAD_FAST, 0, QuickeningTypes.OBJECT),
@@ -871,13 +872,15 @@ public enum OpCodes {
871872
STORE_FAST_UNBOX_B(STORE_FAST, QuickeningTypes.OBJECT, 0),
872873
STORE_FAST_BOXED_B(STORE_FAST, QuickeningTypes.OBJECT, 0),
873874
STORE_FAST_B(STORE_FAST, QuickeningTypes.BOOLEAN, 0),
875+
UNARY_OP_ADAPTIVE_O(UNARY_OP, QuickeningTypes.OBJECT, QuickeningTypes.OBJECT),
874876
UNARY_OP_O_O(UNARY_OP, QuickeningTypes.OBJECT, QuickeningTypes.OBJECT),
875877
UNARY_OP_I_O(UNARY_OP, QuickeningTypes.INT, QuickeningTypes.OBJECT),
876878
UNARY_OP_I_I(UNARY_OP, QuickeningTypes.INT, QuickeningTypes.INT, UNARY_OP_I_O),
877879
UNARY_OP_D_O(UNARY_OP, QuickeningTypes.DOUBLE, QuickeningTypes.OBJECT),
878880
UNARY_OP_D_D(UNARY_OP, QuickeningTypes.DOUBLE, QuickeningTypes.DOUBLE, UNARY_OP_D_O),
879881
UNARY_OP_B_O(UNARY_OP, QuickeningTypes.BOOLEAN, QuickeningTypes.OBJECT),
880882
UNARY_OP_B_B(UNARY_OP, QuickeningTypes.BOOLEAN, QuickeningTypes.BOOLEAN, UNARY_OP_I_O),
883+
BINARY_OP_ADAPTIVE_O(BINARY_OP, QuickeningTypes.OBJECT, QuickeningTypes.OBJECT),
881884
BINARY_OP_OO_O(BINARY_OP, QuickeningTypes.OBJECT, QuickeningTypes.OBJECT),
882885
BINARY_OP_II_O(BINARY_OP, QuickeningTypes.INT, QuickeningTypes.OBJECT),
883886
BINARY_OP_II_I(BINARY_OP, QuickeningTypes.INT, QuickeningTypes.INT, BINARY_OP_II_O),
@@ -887,7 +890,8 @@ public enum OpCodes {
887890
BINARY_OP_DD_B(BINARY_OP, QuickeningTypes.DOUBLE, QuickeningTypes.BOOLEAN, BINARY_OP_DD_O),
888891
FOR_ITER_O(FOR_ITER, 0, QuickeningTypes.OBJECT),
889892
FOR_ITER_I(FOR_ITER, 0, QuickeningTypes.INT, FOR_ITER_O),
890-
BINARY_SUBSCR_SEQ_O_O(BINARY_SUBSCR, QuickeningTypes.OBJECT, QuickeningTypes.OBJECT),
893+
BINARY_SUBSCR_ADAPTIVE_O(BINARY_SUBSCR, QuickeningTypes.OBJECT, QuickeningTypes.OBJECT),
894+
BINARY_SUBSCR_O_O(BINARY_SUBSCR, QuickeningTypes.OBJECT, QuickeningTypes.OBJECT),
891895
BINARY_SUBSCR_SEQ_I_O(BINARY_SUBSCR, QuickeningTypes.INT, QuickeningTypes.OBJECT),
892896
BINARY_SUBSCR_SEQ_I_I(BINARY_SUBSCR, QuickeningTypes.INT, QuickeningTypes.INT, BINARY_SUBSCR_SEQ_I_O),
893897
BINARY_SUBSCR_SEQ_I_D(BINARY_SUBSCR, QuickeningTypes.INT, QuickeningTypes.DOUBLE, BINARY_SUBSCR_SEQ_I_O),

0 commit comments

Comments
 (0)