Skip to content

Commit c52c129

Browse files
committed
[GR-69264] Minor Bytecode DSL improvements.
PullRequest: graalpython/4167
2 parents b85f7a6 + ad7af80 commit c52c129

5 files changed

Lines changed: 34 additions & 54 deletions

File tree

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/generator/CommonGeneratorBuiltins.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ static Object cachedBytecodeDSL(VirtualFrame frame, Node inliningTarget, PGenera
170170
self.setRunning(true);
171171
Object generatorResult;
172172
try {
173-
self.prepareResume();
173+
Object[] generatorArguments = self.prepareResume();
174174
RootCallTarget callTarget = (RootCallTarget) callNode.getCurrentCallTarget();
175175
PRootNode rootNode = PGenerator.unwrapContinuationRoot((ContinuationRootNode) callTarget.getRootNode());
176176
/*
@@ -186,7 +186,6 @@ static Object cachedBytecodeDSL(VirtualFrame frame, Node inliningTarget, PGenera
186186
*/
187187
MaterializedFrame generatorFrame = self.getGeneratorFrame();
188188
Object[] callArguments = new Object[]{generatorFrame, sendValue};
189-
Object[] generatorArguments = generatorFrame.getArguments();
190189
if (frame == null) {
191190
PythonContext context = PythonContext.get(inliningTarget);
192191
PythonThreadState threadState = context.getThreadState(context.getLanguage(inliningTarget));
@@ -252,13 +251,12 @@ static Object genericBytecodeDSL(VirtualFrame frame, Node inliningTarget, PGener
252251
self.setRunning(true);
253252
Object generatorResult;
254253
try {
255-
self.prepareResume();
254+
Object[] generatorArguments = self.prepareResume();
256255
RootCallTarget callTarget = self.getCurrentCallTarget();
257256
// See the cached specialization for notes about the arguments handling
258257
PRootNode rootNode = PGenerator.unwrapContinuationRoot((ContinuationRootNode) callTarget.getRootNode());
259258
MaterializedFrame generatorFrame = self.getGeneratorFrame();
260259
Object[] callArguments = new Object[]{generatorFrame, sendValue};
261-
Object[] generatorArguments = generatorFrame.getArguments();
262260
if (frame == null) {
263261
PythonContext context = PythonContext.get(inliningTarget);
264262
PythonThreadState threadState = context.getThreadState(context.getLanguage(inliningTarget));

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/generator/PGenerator.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ protected static class BytecodeState {
8484
* Each AST can then specialize towards which nodes are executed when starting from that
8585
* particular entry point. When yielding, the next index to the next call target to continue
8686
* from is updated via {@link #handleResult}.
87+
* <p>
88+
* The owner of this array is really the
89+
* {@link com.oracle.graal.python.nodes.bytecode.PBytecodeGeneratorFunctionRootNode}. Every
90+
* {@link PGenerator} instance representing the same generator on AST level gets reference
91+
* to the same array with call targets.
8792
*/
8893
@CompilationFinal(dimensions = 1) private final RootCallTarget[] callTargets;
8994
private int currentCallTarget;
@@ -191,11 +196,12 @@ public Object[] getCallArguments(Object sendValue) {
191196
}
192197
}
193198

194-
public void prepareResume() {
199+
public Object[] prepareResume() {
195200
assert PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER; // not needed for manual interpreter
196-
Object[] frame = getGeneratorFrame().getArguments();
197-
PArguments.setException(frame, null);
198-
PArguments.setCallerFrameInfo(frame, null);
201+
Object[] arguments = getGeneratorFrame().getArguments();
202+
PArguments.setException(arguments, null);
203+
PArguments.setCallerFrameInfo(arguments, null);
204+
return arguments;
199205
}
200206

201207
/**

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

Lines changed: 7 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,19 @@
4040
*/
4141
package com.oracle.graal.python.lib;
4242

43-
import static com.oracle.graal.python.builtins.PythonBuiltinClassType.ValueError;
4443
import static com.oracle.graal.python.util.PythonUtils.TS_ENCODING;
4544

4645
import com.oracle.graal.python.builtins.objects.bytes.PBytesLike;
4746
import com.oracle.graal.python.builtins.objects.common.HashingStorageNodes.HashingStorageLen;
48-
import com.oracle.graal.python.builtins.objects.dict.PDict;
47+
import com.oracle.graal.python.builtins.objects.common.PHashingCollection;
4948
import com.oracle.graal.python.builtins.objects.list.PList;
50-
import com.oracle.graal.python.builtins.objects.set.PSet;
5149
import com.oracle.graal.python.builtins.objects.str.PString;
5250
import com.oracle.graal.python.builtins.objects.str.StringNodes;
5351
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
54-
import com.oracle.graal.python.nodes.ErrorMessages;
52+
import com.oracle.graal.python.nodes.PGuards;
5553
import com.oracle.graal.python.nodes.PNodeWithContext;
56-
import com.oracle.graal.python.nodes.PRaiseNode;
57-
import com.oracle.graal.python.nodes.util.CastToJavaIntLossyNode;
58-
import com.oracle.graal.python.runtime.exception.PException;
5954
import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff;
6055
import com.oracle.truffle.api.dsl.Cached;
61-
import com.oracle.truffle.api.dsl.Cached.Shared;
6256
import com.oracle.truffle.api.dsl.Fallback;
6357
import com.oracle.truffle.api.dsl.GenerateCached;
6458
import com.oracle.truffle.api.dsl.GenerateInline;
@@ -112,15 +106,13 @@ static int doTuple(PTuple object) {
112106
return object.getSequenceStorage().length();
113107
}
114108

115-
@Specialization(guards = "isBuiltinDict(object)")
116-
static int doDict(Node inliningTarget, PDict object,
117-
@Shared("hashingStorageLen") @Cached HashingStorageLen lenNode) {
118-
return lenNode.execute(inliningTarget, object.getDictStorage());
109+
public static boolean isBuiltinHashingCollection(PHashingCollection object) {
110+
return PGuards.isBuiltinDict(object) || PGuards.isBuiltinAnySet(object);
119111
}
120112

121-
@Specialization(guards = "isBuiltinAnySet(object)")
122-
static int doSet(Node inliningTarget, PSet object,
123-
@Shared("hashingStorageLen") @Cached HashingStorageLen lenNode) {
113+
@Specialization(guards = "isBuiltinHashingCollection(object)")
114+
static int doSet(Node inliningTarget, PHashingCollection object,
115+
@Cached HashingStorageLen lenNode) {
124116
return lenNode.execute(inliningTarget, object.getDictStorage());
125117
}
126118

@@ -143,33 +135,6 @@ static int doOthers(VirtualFrame frame, Object object,
143135
return genericNode.execute(frame, object);
144136
}
145137

146-
static int checkLen(Node inliningTarget, PRaiseNode raiseNode, int len) {
147-
if (len < 0) {
148-
throw raiseNode.raise(inliningTarget, ValueError, ErrorMessages.LEN_SHOULD_RETURN_GT_ZERO);
149-
}
150-
return len;
151-
}
152-
153-
public static int convertAndCheckLen(VirtualFrame frame, Node inliningTarget, Object result, PyNumberIndexNode indexNode,
154-
CastToJavaIntLossyNode castLossy, PyNumberAsSizeNode asSizeNode, PRaiseNode raiseNode) {
155-
int len;
156-
Object index = indexNode.execute(frame, inliningTarget, result);
157-
try {
158-
len = asSizeNode.executeExact(frame, inliningTarget, index);
159-
} catch (PException e) {
160-
/*
161-
* CPython first checks whether the number is negative before converting it to an
162-
* integer. Comparing PInts is not cheap for us, so we do the conversion first. If the
163-
* conversion overflowed, we need to do the negativity check before raising the overflow
164-
* error.
165-
*/
166-
len = castLossy.execute(inliningTarget, index);
167-
checkLen(inliningTarget, raiseNode, len);
168-
throw e;
169-
}
170-
return checkLen(inliningTarget, raiseNode, len);
171-
}
172-
173138
@NeverDefault
174139
public static PyObjectSizeNode create() {
175140
return PyObjectSizeNodeGen.create();

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/lib/fastpath/PyNumberSubtractFastPathsBase.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.oracle.graal.python.nodes.expression.BinaryOpNode;
4747
import com.oracle.graal.python.nodes.truffle.PythonIntegerTypes;
4848
import com.oracle.graal.python.runtime.object.PFactory;
49+
import com.oracle.truffle.api.HostCompilerDirectives.InliningCutoff;
4950
import com.oracle.truffle.api.dsl.Bind;
5051
import com.oracle.truffle.api.dsl.GenerateCached;
5152
import com.oracle.truffle.api.dsl.Specialization;
@@ -81,13 +82,18 @@ public static Object doLongWithOverflow(long x, long y,
8182
/* Inlined version of Math.subtractExact(x, y) with BigInteger fallback. */
8283
long r = x - y;
8384
// HD 2-12 Overflow iff the arguments have different signs and
84-
// the sign of the result is different than the sign of x
85+
// the sign of the result is different from the sign of x
8586
if (((x ^ y) & (x ^ r)) < 0) {
86-
return PFactory.createInt(PythonLanguage.get(inliningTarget), IntBuiltins.SubNode.sub(PInt.longToBigInteger(x), PInt.longToBigInteger(y)));
87+
return subtractAsPInt(inliningTarget, x, y);
8788
}
8889
return r;
8990
}
9091

92+
@InliningCutoff
93+
private static PInt subtractAsPInt(Node inliningTarget, long x, long y) {
94+
return PFactory.createInt(PythonLanguage.get(inliningTarget), IntBuiltins.SubNode.sub(PInt.longToBigInteger(x), PInt.longToBigInteger(y)));
95+
}
96+
9197
/*
9298
* All the following fast paths need to be kept in sync with the corresponding builtin functions
9399
* in FloatBuiltins

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1432,7 +1432,7 @@ protected static PFunction createFunction(VirtualFrame frame,
14321432
PKeyword[] kwDefaults = new PKeyword[kwDefaultsObject.length];
14331433
// Note: kwDefaultsObject should be a result of operation MakeKeywords, which produces
14341434
// PKeyword[]
1435-
System.arraycopy(kwDefaultsObject, 0, kwDefaults, 0, kwDefaults.length);
1435+
PythonUtils.arraycopy(kwDefaultsObject, 0, kwDefaults, 0, kwDefaults.length);
14361436
PFunction function = PFactory.createFunction(PythonLanguage.get(node), name, qualifiedName, code, PArguments.getGlobals(frame), defaults, kwDefaults, (PCell[]) closure);
14371437

14381438
if (annotations != null) {
@@ -3592,10 +3592,15 @@ static void doPException(PException exception,
35923592
@Bind BytecodeNode bytecodeNode,
35933593
@Cached IsBuiltinObjectProfile isStopAsyncIteration) {
35943594
if (!isStopAsyncIteration.profileException(inliningTarget, exception, PythonBuiltinClassType.StopAsyncIteration)) {
3595-
throw exception.getExceptionForReraise(!((PBytecodeDSLRootNode) bytecodeNode.getRootNode()).internal);
3595+
reraiseException(exception, bytecodeNode);
35963596
}
35973597
}
35983598

3599+
@InliningCutoff
3600+
private static void reraiseException(PException exception, BytecodeNode bytecodeNode) {
3601+
throw exception.getExceptionForReraise(!((PBytecodeDSLRootNode) bytecodeNode.getRootNode()).internal);
3602+
}
3603+
35993604
@Specialization(guards = "!isPException(exception)")
36003605
static void doInteropException(AbstractTruffleException exception) {
36013606
throw exception;

0 commit comments

Comments
 (0)