Skip to content

Commit 7a6d384

Browse files
committed
[GR-38700] Various Bytecode DSL improvements and fixes.
PullRequest: graalpython/4138
2 parents 0267c15 + 4a7b2f6 commit 7a6d384

File tree

12 files changed

+86
-45
lines changed

12 files changed

+86
-45
lines changed

graalpython/com.oracle.graal.python.test/src/com/oracle/graal/python/test/builtin/modules/ConversionNodeTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ protected static Object call(Object arg, ArgumentCastNode castNode) {
7676
public Object execute(VirtualFrame frame) {
7777
GilNode gilNode = GilNode.getUncached();
7878
boolean wasAcquired = gilNode.acquire();
79-
calleeContext.enter(frame);
79+
calleeContext.enter(frame, this);
8080
try {
8181
return node.execute(frame, PArguments.getArgument(frame, 0));
8282
} finally {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/PythonLanguage.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,7 @@ public boolean isSingleContext() {
395395

396396
@CompilationFinal(dimensions = 1) private volatile Object[] engineOptionsStorage;
397397
@CompilationFinal private volatile OptionValues engineOptions;
398+
@CompilationFinal private boolean useNativePrimitiveStorage;
398399

399400
/** For fast access to the PythonThreadState object by the owning thread. */
400401
private final ContextThreadLocal<PythonThreadState> threadState = locals.createContextThreadLocal(PythonContext.PythonThreadState::new);
@@ -476,6 +477,7 @@ protected PythonContext createContext(Env env) {
476477
} else {
477478
assert areOptionsCompatible(options, PythonOptions.createEngineOptions(env)) : "invalid engine options";
478479
}
480+
this.useNativePrimitiveStorage = getEngineOption(PythonOptions.UseNativePrimitiveStorageStrategy);
479481

480482
return context;
481483
}
@@ -490,6 +492,10 @@ public <T> T getEngineOption(OptionKey<T> key) {
490492
}
491493
}
492494

495+
public boolean useNativePrimitiveStorage() {
496+
return useNativePrimitiveStorage;
497+
}
498+
493499
@Override
494500
protected OptionDescriptors getOptionDescriptors() {
495501
return PythonOptions.DESCRIPTORS;
@@ -1101,6 +1107,7 @@ private Shape createBuiltinShape(PythonBuiltinClassType type, int ordinal) {
11011107
shapeBuilder.shapeFlags(PythonObject.HAS_SLOTS_BUT_NO_DICT_FLAG);
11021108
}
11031109
shape = shapeBuilder.build();
1110+
VarHandle.storeStoreFence();
11041111
builtinTypeInstanceShapes[ordinal] = shape;
11051112
return shape;
11061113
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/ExternalFunctionNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -871,7 +871,7 @@ private void prepareArguments(Object[] arguments) {
871871

872872
@Override
873873
public final Object execute(VirtualFrame frame) {
874-
calleeContext.enter(frame);
874+
calleeContext.enter(frame, this);
875875
try {
876876
Object callable = ensureReadCallableNode().execute(frame);
877877
Object[] cArguments = prepareCArguments(frame);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SequenceStorageNodes.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3968,10 +3968,6 @@ static boolean isObjectSequenceStorage(SequenceStorage s) {
39683968
@ImportStatic(PInt.class)
39693969
public abstract static class InsertItemArrayBasedStorageNode extends Node {
39703970

3971-
public static SequenceStorage executeUncached(ArrayBasedSequenceStorage storage, int index, Object value) {
3972-
return SequenceStorageNodesFactory.InsertItemArrayBasedStorageNodeGen.getUncached().execute(null, storage, index, value);
3973-
}
3974-
39753971
protected abstract SequenceStorage execute(Node inliningTarget, ArrayBasedSequenceStorage storage, int index, Object value);
39763972

39773973
@Specialization
@@ -4037,6 +4033,40 @@ static SequenceStorage doGeneralization(Node inliningTarget, ArrayBasedSequenceS
40374033
}
40384034
}
40394035

4036+
@GenerateUncached
4037+
@GenerateInline
4038+
public abstract static class InsertItemNativePrimitiveBasedStorageNode extends Node {
4039+
protected abstract SequenceStorage execute(Node inliningTarget, NativePrimitiveSequenceStorage storage, int index, Object value);
4040+
4041+
@Specialization
4042+
static SequenceStorage doIntStorage(Node inliningTarget, NativeIntSequenceStorage storage, int index, int value,
4043+
@Cached EnsureCapacityNode ensureCapacity) {
4044+
int length = storage.length();
4045+
var context = PythonContext.get(inliningTarget);
4046+
var unsafe = context.getUnsafe();
4047+
long itemSize = storage.getItemSize();
4048+
ensureCapacity.execute(inliningTarget, storage, length + 1);
4049+
// shifting tail to the right by one slot
4050+
long startAddr = storage.getValueBufferAddr() + (index * itemSize);
4051+
long endAddr = startAddr + itemSize;
4052+
long sizeInBytes = (length - index) * itemSize;
4053+
unsafe.copyMemory(startAddr, endAddr, sizeInBytes);
4054+
4055+
storage.setIntItemNormalized(index, value);
4056+
storage.incLength();
4057+
return storage;
4058+
}
4059+
4060+
@Fallback
4061+
static SequenceStorage doGeneralization(Node inliningTarget, NativePrimitiveSequenceStorage storage, int idx, Object value,
4062+
@Cached GetInternalObjectArrayNode getInternalObjectArrayNode) {
4063+
Object[] values = getInternalObjectArrayNode.execute(inliningTarget, storage);
4064+
ObjectSequenceStorage newStorage = new ObjectSequenceStorage(values);
4065+
newStorage.insertItem(idx, value);
4066+
return newStorage;
4067+
}
4068+
}
4069+
40404070
@GenerateUncached
40414071
@GenerateInline
40424072
@GenerateCached(false)
@@ -4062,24 +4092,10 @@ static SequenceStorage doArrayBasedStorage(Node inliningTarget, ArrayBasedSequen
40624092

40634093
}
40644094

4065-
// TODO introduce something similar to InsertItemArrayBasedStorageNode
40664095
@Specialization
4067-
static SequenceStorage doNativeInt(Node inliningTarget, NativeIntSequenceStorage storage, int index, int value,
4068-
@Exclusive @Cached EnsureCapacityNode ensureCapacity) {
4069-
int length = storage.length();
4070-
var context = PythonContext.get(inliningTarget);
4071-
var unsafe = context.getUnsafe();
4072-
long itemSize = storage.getItemSize();
4073-
ensureCapacity.execute(inliningTarget, storage, length + 1);
4074-
// shifting tail to the right by one slot
4075-
long startAddr = storage.getValueBufferAddr() + (index * itemSize);
4076-
long endAddr = startAddr + itemSize;
4077-
long sizeInBytes = (length - index) * itemSize;
4078-
unsafe.copyMemory(startAddr, endAddr, sizeInBytes);
4079-
4080-
storage.setIntItemNormalized(index, value);
4081-
storage.incLength();
4082-
return storage;
4096+
static SequenceStorage doNativeStorage(Node inliningTarget, NativeIntSequenceStorage storage, int index, Object value,
4097+
@Cached InsertItemNativePrimitiveBasedStorageNode insertNativeStorageNode) {
4098+
return insertNativeStorageNode.execute(inliningTarget, storage, index, value);
40834099
}
40844100

40854101
@Specialization

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/common/SortNodes.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ enum Result {
125125

126126
@Override
127127
public Object execute(VirtualFrame frame) {
128-
calleeContext.enter(frame);
128+
calleeContext.enter(frame, this);
129129
try {
130130
Object[] arguments = frame.getArguments();
131131
Object a = arguments[PArguments.USER_ARGUMENTS_OFFSET];

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/PyTraceBackPrint.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -345,7 +345,7 @@ private static void printInternal(Node inliningTarget, TracebackBuiltins.GetTrac
345345
PythonLanguage language = PythonLanguage.get(inliningTarget);
346346
while (tb != null) {
347347
final PCode code = getCode(language, getTbFrameNode, tb);
348-
if (lastFile == null ||
348+
if (lastFile == null || code.getFilename() == null ||
349349
!tstrEqNode.execute(code.getFilename(), lastFile, TS_ENCODING) ||
350350
lastLine == -1 || tb.getLineno() != lastLine ||
351351
lastName == null || !tstrEqNode.execute(code.getName(), lastName, TS_ENCODING)) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/bytecode/PBytecodeRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ int getInitialStackTop() {
10861086

10871087
@Override
10881088
public Object execute(VirtualFrame virtualFrame) {
1089-
calleeContext.enter(virtualFrame);
1089+
calleeContext.enter(virtualFrame, this);
10901090
try {
10911091
copyArgsAndCells(virtualFrame, virtualFrame.getArguments());
10921092
return executeFromBci(virtualFrame, virtualFrame, this, 0, getInitialStackTop());

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ public static final class EnterCalleeContext {
452452
@Specialization
453453
public static void doEnter(VirtualFrame frame,
454454
@Bind PBytecodeDSLRootNode root) {
455-
root.calleeContext.enter(frame);
455+
root.calleeContext.enter(frame, root);
456456

457457
if (root.needsTraceAndProfileInstrumentation()) {
458458
root.ensureTraceAndProfileEnabled();
@@ -487,8 +487,11 @@ public static void doExit(VirtualFrame frame, AbstractTruffleException ate,
487487
}
488488

489489
if (root.needsTraceAndProfileInstrumentation()) {
490-
root.traceOrProfileReturn(frame, location, null);
491-
root.getThreadState().popInstrumentationData(root);
490+
try {
491+
root.traceOrProfileReturn(frame, location, null);
492+
} finally {
493+
root.getThreadState().popInstrumentationData(root);
494+
}
492495
}
493496

494497
root.calleeContext.exit(frame, root, location);
@@ -1055,11 +1058,13 @@ public static Object doYield(
10551058
@Bind ContinuationRootNode continuationRootNode,
10561059
@Bind PBytecodeDSLRootNode innerRoot,
10571060
@Bind BytecodeNode bytecodeNode) {
1058-
Object result = createGenerator(continuationFrame, inliningTarget, continuationRootNode, innerRoot);
1059-
if (innerRoot.needsTraceAndProfileInstrumentation()) {
1060-
innerRoot.getThreadState().popInstrumentationData(innerRoot);
1061+
try {
1062+
return createGenerator(continuationFrame, inliningTarget, continuationRootNode, innerRoot);
1063+
} finally {
1064+
if (innerRoot.needsTraceAndProfileInstrumentation()) {
1065+
innerRoot.getThreadState().popInstrumentationData(innerRoot);
1066+
}
10611067
}
1062-
return result;
10631068
}
10641069

10651070
private static PythonAbstractObject createGenerator(MaterializedFrame continuationFrame, Node inliningTarget,
@@ -1115,8 +1120,11 @@ public static Object doObject(Object value,
11151120
@Bind PBytecodeDSLRootNode root,
11161121
@Bind BytecodeNode bytecode) {
11171122
if (root.needsTraceAndProfileInstrumentation()) {
1118-
root.traceOrProfileReturn(frame, bytecode, value);
1119-
root.getThreadState().popInstrumentationData(root);
1123+
try {
1124+
root.traceOrProfileReturn(frame, bytecode, value);
1125+
} finally {
1126+
root.getThreadState().popInstrumentationData(root);
1127+
}
11201128
}
11211129

11221130
// Suspended generators have no backref
@@ -1682,8 +1690,18 @@ public static final class MakeConstantIntList {
16821690
@Specialization
16831691
public static PList perform(int[] array,
16841692
@Bind PBytecodeDSLRootNode rootNode) {
1685-
SequenceStorage storage = new IntSequenceStorage(PythonUtils.arrayCopyOf(array, array.length));
1686-
return PFactory.createList(rootNode.getLanguage(), storage);
1693+
PythonLanguage language = rootNode.getLanguage();
1694+
if (!language.useNativePrimitiveStorage()) {
1695+
return PFactory.createList(language, new IntSequenceStorage(PythonUtils.arrayCopyOf(array, array.length)));
1696+
} else {
1697+
return createNativeList(array, rootNode, language);
1698+
}
1699+
}
1700+
1701+
@InliningCutoff
1702+
private static PList createNativeList(int[] array, PBytecodeDSLRootNode rootNode, PythonLanguage language) {
1703+
SequenceStorage storage = PythonContext.get(rootNode).nativeBufferContext.toNativeIntStorage(array);
1704+
return PFactory.createList(language, storage);
16871705
}
16881706
}
16891707

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/BuiltinFunctionRootNode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public Object execute(VirtualFrame frame) {
332332
body = insert(newBody);
333333
}
334334
}
335-
calleeContext.enter(frame);
335+
calleeContext.enter(frame, this);
336336
try {
337337
return body.execute(frame);
338338
} finally {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ protected CallRootNode(TruffleLanguage<?> language) {
288288

289289
@Override
290290
public Object execute(VirtualFrame frame) {
291-
calleeContext.enter(frame);
291+
calleeContext.enter(frame, this);
292292
Object[] frameArguments = frame.getArguments();
293293
Object callable = PArguments.getArgument(frameArguments, ASYNC_CALLABLE_INDEX);
294294
int frameIndex = (int) PArguments.getArgument(frameArguments, ASYNC_FRAME_INDEX_INDEX);

0 commit comments

Comments
 (0)