Skip to content

Commit 9f12693

Browse files
committed
report polymorphism and reduce footprint for users of PythonObjectLibrary.equals and PythonObjectLibrary.isTrue
1 parent 2d5aaa7 commit 9f12693

File tree

3 files changed

+36
-18
lines changed

3 files changed

+36
-18
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.truffle.api.dsl.Cached;
6363
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
6464
import com.oracle.truffle.api.dsl.NodeFactory;
65+
import com.oracle.truffle.api.dsl.ReportPolymorphism;
6566
import com.oracle.truffle.api.dsl.Specialization;
6667
import com.oracle.truffle.api.frame.VirtualFrame;
6768
import com.oracle.truffle.api.profiles.ConditionProfile;
@@ -95,6 +96,7 @@ public Object iter(PGenerator self) {
9596

9697
@Builtin(name = __NEXT__, minNumOfPositionalArgs = 1)
9798
@GenerateNodeFactory
99+
@ReportPolymorphism
98100
public abstract static class NextNode extends PythonUnaryBuiltinNode {
99101

100102
@Child private GetCaughtExceptionNode getCaughtExceptionNode;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/object/PythonObjectLibrary.java

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,6 @@
6767
import com.oracle.truffle.api.library.LibraryFactory;
6868
import com.oracle.truffle.api.nodes.Node;
6969
import com.oracle.truffle.api.nodes.NodeCost;
70-
import com.oracle.truffle.api.profiles.BranchProfile;
7170

7271
/**
7372
* The standard Python object library. This implements a general-purpose Python object interface.
@@ -249,14 +248,18 @@ public final long hash(double receiver) {
249248
}
250249

251250
private static class DefaultNodes extends Node {
251+
private static final byte REVERSE_COMP = 0b001;
252+
private static final byte LEFT_COMPARE = 0b010;
253+
private static final byte SUBT_COMPARE = 0b100;
254+
252255
@Child private IsSubtypeNode isSubtype;
253256
@Child private IsSameTypeNode isSameType;
254-
@CompilationFinal private BranchProfile reverseEquals;
255-
@CompilationFinal private BranchProfile subtypeEquals;
257+
@CompilationFinal byte state = 0;
256258

257259
protected IsSubtypeNode getIsSubtypeNode() {
258260
if (isSubtype == null) {
259261
CompilerDirectives.transferToInterpreterAndInvalidate();
262+
reportPolymorphicSpecialize();
260263
isSubtype = insert(IsSubtypeNode.create());
261264
}
262265
return isSubtype;
@@ -270,20 +273,28 @@ protected IsSameTypeNode getIsSameTypeNode() {
270273
return isSameType;
271274
}
272275

273-
protected BranchProfile getReverseProfile() {
274-
if (reverseEquals == null) {
276+
protected void enterReverseCompare() {
277+
if ((state & REVERSE_COMP) == 0) {
278+
CompilerDirectives.transferToInterpreterAndInvalidate();
279+
reportPolymorphicSpecialize();
280+
state |= REVERSE_COMP;
281+
}
282+
}
283+
284+
protected void enterLeftCompare() {
285+
if ((state & LEFT_COMPARE) == 0) {
275286
CompilerDirectives.transferToInterpreterAndInvalidate();
276-
reverseEquals = BranchProfile.create();
287+
reportPolymorphicSpecialize();
288+
state |= LEFT_COMPARE;
277289
}
278-
return reverseEquals;
279290
}
280291

281-
protected BranchProfile getSubtypeProfile() {
282-
if (subtypeEquals == null) {
292+
protected void enterSubtypeCompare() {
293+
if ((state & SUBT_COMPARE) == 0) {
283294
CompilerDirectives.transferToInterpreterAndInvalidate();
284-
subtypeEquals = BranchProfile.create();
295+
reportPolymorphicSpecialize();
296+
state |= SUBT_COMPARE;
285297
}
286-
return subtypeEquals;
287298
}
288299

289300
private static final class Disabled extends DefaultNodes {
@@ -300,13 +311,15 @@ protected IsSameTypeNode getIsSameTypeNode() {
300311
}
301312

302313
@Override
303-
protected BranchProfile getReverseProfile() {
304-
return BranchProfile.getUncached();
314+
protected void enterReverseCompare() {
305315
}
306316

307317
@Override
308-
protected BranchProfile getSubtypeProfile() {
309-
return BranchProfile.getUncached();
318+
protected void enterLeftCompare() {
319+
}
320+
321+
@Override
322+
protected void enterSubtypeCompare() {
310323
}
311324
}
312325

@@ -365,20 +378,20 @@ public boolean equalsWithState(Object receiver, Object other, PythonObjectLibrar
365378
int result;
366379
boolean isSameType = getDefaultNodes().getIsSameTypeNode().execute(leftClass, rightClass);
367380
if (!isSameType && getDefaultNodes().getIsSubtypeNode().execute(rightClass, leftClass)) {
368-
getDefaultNodes().getSubtypeProfile().enter();
381+
getDefaultNodes().enterSubtypeCompare();
369382
checkedReverseOp = true;
370383
result = otherLibrary.equalsInternal(other, receiver, threadState);
371384
if (result != -1) {
372385
return result == 1;
373386
}
374387
}
375-
388+
getDefaultNodes().enterLeftCompare();
376389
result = equalsInternal(receiver, other, threadState);
377390
if (result != -1) {
378391
return result == 1;
379392
}
380393
if (!isSameType && !checkedReverseOp) {
381-
getDefaultNodes().getReverseProfile().enter();
394+
getDefaultNodes().enterReverseCompare();
382395
result = otherLibrary.equalsInternal(other, receiver, threadState);
383396
}
384397

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/expression/CoerceToBooleanNode.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import com.oracle.graal.python.nodes.expression.CoerceToBooleanNodeFactory.NotNodeGen;
3131
import com.oracle.graal.python.nodes.expression.CoerceToBooleanNodeFactory.YesNodeGen;
3232
import com.oracle.graal.python.nodes.object.IsBuiltinClassProfile;
33+
import com.oracle.truffle.api.dsl.ReportPolymorphism;
3334
import com.oracle.truffle.api.dsl.Specialization;
3435
import com.oracle.truffle.api.frame.VirtualFrame;
3536
import com.oracle.truffle.api.instrumentation.GenerateWrapper;
@@ -66,6 +67,7 @@ public static CoerceToBooleanNode createIfFalseNode(ExpressionNode operand) {
6667
@Override
6768
public abstract boolean executeBoolean(VirtualFrame frame);
6869

70+
@ReportPolymorphism
6971
public abstract static class YesNode extends CoerceToBooleanNode {
7072
@Specialization
7173
boolean doBoolean(boolean operand) {
@@ -99,6 +101,7 @@ boolean doObject(VirtualFrame frame, Object object,
99101
}
100102
}
101103

104+
@ReportPolymorphism
102105
public abstract static class NotNode extends CoerceToBooleanNode {
103106
@Specialization
104107
boolean doBool(boolean operand) {

0 commit comments

Comments
 (0)