Skip to content

Commit e945f16

Browse files
committed
[GR-30482] Add PyObjectIsTrueNode
PullRequest: graalpython/1803
2 parents 7f9f036 + 00c87cf commit e945f16

25 files changed

+387
-112
lines changed

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

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,6 @@
146146
import com.oracle.graal.python.builtins.objects.floats.FloatUtils;
147147
import com.oracle.graal.python.builtins.objects.floats.PFloat;
148148
import com.oracle.graal.python.builtins.objects.frame.PFrame;
149-
import com.oracle.graal.python.builtins.objects.function.PArguments;
150149
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
151150
import com.oracle.graal.python.builtins.objects.function.PFunction;
152151
import com.oracle.graal.python.builtins.objects.function.PKeyword;
@@ -196,6 +195,7 @@
196195
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
197196
import com.oracle.graal.python.lib.PyNumberFloatNode;
198197
import com.oracle.graal.python.lib.PyNumberIndexNode;
198+
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
199199
import com.oracle.graal.python.nodes.BuiltinNames;
200200
import com.oracle.graal.python.nodes.ErrorMessages;
201201
import com.oracle.graal.python.nodes.PGuards;
@@ -1499,18 +1499,12 @@ private String toString(PBytesLike pByteArray) {
14991499
// bool([x])
15001500
@Builtin(name = BOOL, minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2, constructsClass = PythonBuiltinClassType.Boolean, base = PythonBuiltinClassType.PInt)
15011501
@GenerateNodeFactory
1502-
@SuppressWarnings("unused")
15031502
@ReportPolymorphism
15041503
public abstract static class BoolNode extends PythonBinaryBuiltinNode {
1505-
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
1506-
public static boolean bool(VirtualFrame frame, Object cls, Object obj,
1507-
@Cached ConditionProfile hasFrame,
1508-
@CachedLibrary("obj") PythonObjectLibrary lib) {
1509-
if (hasFrame.profile(frame != null)) {
1510-
return lib.isTrueWithState(obj, PArguments.getThreadState(frame));
1511-
} else {
1512-
return lib.isTrue(obj);
1513-
}
1504+
@Specialization
1505+
public static boolean bool(VirtualFrame frame, @SuppressWarnings("unused") Object cls, Object obj,
1506+
@Cached PyObjectIsTrueNode isTrue) {
1507+
return isTrue.execute(frame, obj);
15141508
}
15151509
}
15161510

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

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,9 @@
5454
import com.oracle.graal.python.builtins.objects.common.SequenceNodes;
5555
import com.oracle.graal.python.builtins.objects.common.SequenceStorageNodes;
5656
import com.oracle.graal.python.builtins.objects.dict.PDict;
57-
import com.oracle.graal.python.builtins.objects.function.PArguments;
5857
import com.oracle.graal.python.builtins.objects.module.PythonModule;
59-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6058
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
59+
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
6160
import com.oracle.graal.python.nodes.ErrorMessages;
6261
import com.oracle.graal.python.nodes.attributes.ReadAttributeFromObjectNode;
6362
import com.oracle.graal.python.nodes.attributes.WriteAttributeToObjectNode;
@@ -78,7 +77,6 @@
7877
import com.oracle.truffle.api.dsl.Specialization;
7978
import com.oracle.truffle.api.frame.VirtualFrame;
8079
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
81-
import com.oracle.truffle.api.library.CachedLibrary;
8280
import com.oracle.truffle.api.object.HiddenKey;
8381
import com.oracle.truffle.tools.coverage.CoverageTracker;
8482
import com.oracle.truffle.tools.coverage.RootCoverage;
@@ -99,13 +97,13 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9997
@Builtin(name = "start", parameterNames = {"mod", "count", "countfuncs", "ignoremods", "ignoredirs"}, minNumOfPositionalArgs = 1, declaresExplicitSelf = true)
10098
@GenerateNodeFactory
10199
abstract static class TraceNew extends PythonBuiltinNode {
102-
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
100+
@Specialization
103101
@TruffleBoundary
104102
PNone doit(PythonModule mod, Object count, Object countfuncs, PSequence ignoremods, PSequence ignoredirs,
105103
@Cached SequenceNodes.GetSequenceStorageNode getStore,
106104
@Cached SequenceStorageNodes.ToArrayNode toArray,
107105
@Cached CastToJavaStringNode castStr,
108-
@CachedLibrary("count") PythonObjectLibrary lib,
106+
@Cached PyObjectIsTrueNode isTrueNode,
109107
@Cached ReadAttributeFromObjectNode readNode,
110108
@Cached WriteAttributeToObjectNode writeNode) {
111109
Object currentTracker = readNode.execute(mod, TRACKER);
@@ -176,7 +174,7 @@ PNone doit(PythonModule mod, Object count, Object countfuncs, PSequence ignoremo
176174
}
177175
writeNode.execute(mod, TRACK_FUNCS, countfuncs);
178176

179-
tracker.start(new CoverageTracker.Config(filter.build(), lib.isTrue(count)));
177+
tracker.start(new CoverageTracker.Config(filter.build(), isTrueNode.execute(null, count)));
180178

181179
return PNone.NONE;
182180
}
@@ -204,11 +202,11 @@ PNone start(PythonModule mod,
204202
abstract static class Results extends PythonUnaryBuiltinNode {
205203
@Specialization
206204
PTuple start(VirtualFrame frame, PythonModule mod,
207-
@CachedLibrary(limit = "getCallSiteInlineCacheMaxDepth()") PythonObjectLibrary lib,
205+
@Cached PyObjectIsTrueNode isTrue,
208206
@Cached ReadAttributeFromObjectNode readNode,
209207
@Cached HashingCollectionNodes.SetItemNode setItemNode) {
210208
Object currentTracker = readNode.execute(mod, TRACKER);
211-
boolean countFuncs = lib.isTrueWithState(readNode.execute(mod, TRACK_FUNCS), PArguments.getThreadState(frame));
209+
boolean countFuncs = isTrue.execute(frame, readNode.execute(mod, TRACK_FUNCS));
212210

213211
CoverageTracker tracker;
214212
if (currentTracker instanceof CoverageTracker) {

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

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import com.oracle.graal.python.builtins.objects.str.PString;
6363
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6464
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
65+
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
6566
import com.oracle.graal.python.nodes.ErrorMessages;
6667
import com.oracle.graal.python.nodes.IndirectCallNode;
6768
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -167,6 +168,7 @@ static final class WarningsModuleNode extends Node implements IndirectCallNode {
167168
@Child PythonObjectLibrary pylib;
168169
@Child GetClassNode getClassNode;
169170
@Child PyNumberAsSizeNode asSizeNode;
171+
@Child PyObjectIsTrueNode isTrueNode;
170172
@Child PythonObjectFactory factory;
171173
@Child IsSubtypeNode isSubtype;
172174
@Child GetDictNode getDictNode;
@@ -234,6 +236,14 @@ private PyNumberAsSizeNode getAsSizeNode() {
234236
return asSizeNode;
235237
}
236238

239+
private PyObjectIsTrueNode getIsTrueNode() {
240+
if (isTrueNode == null) {
241+
CompilerDirectives.transferToInterpreterAndInvalidate();
242+
isTrueNode = insert(PyObjectIsTrueNode.create());
243+
}
244+
return isTrueNode;
245+
}
246+
237247
private CastToJavaStringNode getCastStr() {
238248
if (castStr == null) {
239249
CompilerDirectives.transferToInterpreterAndInvalidate();
@@ -351,7 +361,7 @@ private boolean checkMatched(VirtualFrame frame, Object obj, Object arg) {
351361
}
352362
} catch (CannotCastException e) {
353363
Object result = getPyLib().lookupAndCallRegularMethod(obj, frame, "match", arg);
354-
return getPyLib().isTrue(result);
364+
return getIsTrueNode().execute(frame, result);
355365
}
356366
}
357367

@@ -487,21 +497,22 @@ private String getFilter(VirtualFrame frame, PythonModule _warnings, Object cate
487497
* The variant of alreadyWarned that should not set and that must be on the fast path.
488498
*/
489499
private boolean alreadyWarnedShouldNotSet(VirtualFrame frame, PythonModule _warnings, Object registry, Object key) {
490-
return alreadyWarned(frame, _warnings, registry, key, false, getPyLib(), getWarnLib());
500+
return alreadyWarned(frame, _warnings, registry, key, false, getPyLib(), getIsTrueNode(), getWarnLib());
491501
}
492502

493503
/**
494504
* The variant of alreadyWarned that should set and that's on the slow path where the
495505
* warnings will be printed.
496506
*/
497507
private static boolean alreadyWarnedShouldSet(PythonModule _warnings, Object registry, Object key) {
498-
return alreadyWarned(null, _warnings, registry, key, true, PythonObjectLibrary.getUncached(), DynamicObjectLibrary.getUncached());
508+
return alreadyWarned(null, _warnings, registry, key, true, PythonObjectLibrary.getUncached(), PyObjectIsTrueNode.getUncached(), DynamicObjectLibrary.getUncached());
499509
}
500510

501511
/**
502512
* Used on both fast and slow path.
503513
*/
504-
private static boolean alreadyWarned(VirtualFrame frame, PythonModule _warnings, Object registry, Object key, boolean shouldSet, PythonObjectLibrary polib, DynamicObjectLibrary warnLib) {
514+
private static boolean alreadyWarned(VirtualFrame frame, PythonModule _warnings, Object registry, Object key, boolean shouldSet, PythonObjectLibrary polib, PyObjectIsTrueNode isTrueNode,
515+
DynamicObjectLibrary warnLib) {
505516
Object versionObj = polib.lookupAndCallSpecialMethod(registry, frame, "get", "version", PNone.NONE);
506517
long stateFiltersVersion = getStateFiltersVersion(_warnings, warnLib);
507518
if (versionObj == PNone.NONE || !polib.equals(stateFiltersVersion, versionObj, polib)) {
@@ -510,7 +521,7 @@ private static boolean alreadyWarned(VirtualFrame frame, PythonModule _warnings,
510521
} else {
511522
Object alreadyWarned = polib.lookupAndCallSpecialMethod(registry, frame, "get", key, PNone.NONE);
512523
if (alreadyWarned != PNone.NONE) {
513-
return polib.isTrue(alreadyWarned);
524+
return isTrueNode.execute(frame, alreadyWarned);
514525
}
515526
}
516527
if (shouldSet) {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/BufferedIONodes.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@
6060
import com.oracle.graal.python.PythonLanguage;
6161
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
6262
import com.oracle.graal.python.builtins.modules.ThreadModuleBuiltins;
63-
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
6463
import com.oracle.graal.python.lib.PyNumberIndexNode;
64+
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
6565
import com.oracle.graal.python.nodes.PGuards;
6666
import com.oracle.graal.python.nodes.PNodeWithContext;
6767
import com.oracle.graal.python.nodes.PNodeWithRaise;
@@ -80,7 +80,6 @@
8080
import com.oracle.truffle.api.dsl.Specialization;
8181
import com.oracle.truffle.api.dsl.TypeSystemReference;
8282
import com.oracle.truffle.api.frame.VirtualFrame;
83-
import com.oracle.truffle.api.library.CachedLibrary;
8483
import com.oracle.truffle.api.nodes.Node;
8584
import com.oracle.truffle.api.profiles.ConditionProfile;
8685

@@ -127,9 +126,9 @@ static boolean isClosedFileIO(VirtualFrame frame, PBuffered self) {
127126
@Specialization(guards = {"self.getBuffer() != null", "!self.isFastClosedChecks()"})
128127
static boolean isClosedBuffered(VirtualFrame frame, PBuffered self,
129128
@Cached IONodes.GetClosed getClosed,
130-
@CachedLibrary(limit = "2") PythonObjectLibrary isTrue) {
129+
@Cached PyObjectIsTrueNode isTrue) {
131130
Object res = getClosed.execute(frame, self.getRaw());
132-
return isTrue.isTrue(res, frame);
131+
return isTrue.execute(frame, res);
133132
}
134133
}
135134

@@ -155,10 +154,10 @@ abstract static class IsSeekableNode extends Node {
155154
@Specialization
156155
static boolean isSeekable(VirtualFrame frame, PBuffered self,
157156
@Cached IONodes.CallSeekable seekable,
158-
@CachedLibrary(limit = "1") PythonObjectLibrary isTrue) {
157+
@Cached PyObjectIsTrueNode isTrue) {
159158
assert self.isOK();
160159
Object res = seekable.execute(frame, self.getRaw());
161-
return isTrue.isTrue(res, frame);
160+
return isTrue.execute(frame, res);
162161
}
163162
}
164163

@@ -174,9 +173,9 @@ public boolean isBufferReadable(VirtualFrame frame, PBuffered self) {
174173
@Specialization
175174
static boolean isReadable(VirtualFrame frame, Object raw,
176175
@Cached IONodes.CallReadable readable,
177-
@CachedLibrary(limit = "1") PythonObjectLibrary isTrue) {
176+
@Cached PyObjectIsTrueNode isTrue) {
178177
Object res = readable.execute(frame, raw);
179-
return isTrue.isTrue(res, frame);
178+
return isTrue.execute(frame, res);
180179
}
181180

182181
public static IsReadableNode create() {
@@ -196,9 +195,9 @@ public boolean isBufferWritable(VirtualFrame frame, PBuffered self) {
196195
@Specialization
197196
static boolean isWritable(VirtualFrame frame, Object raw,
198197
@Cached IONodes.CallWritable writable,
199-
@CachedLibrary(limit = "1") PythonObjectLibrary isTrue) {
198+
@Cached PyObjectIsTrueNode isTrue) {
200199
Object res = writable.execute(frame, raw);
201-
return isTrue.isTrue(res, frame);
200+
return isTrue.execute(frame, res);
202201
}
203202

204203
public static IsWritableNode create() {

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/FileIOBuiltins.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@
117117
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
118118
import com.oracle.graal.python.lib.PyIndexCheckNode;
119119
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
120+
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
120121
import com.oracle.graal.python.nodes.PConstructAndRaiseNode;
121122
import com.oracle.graal.python.nodes.PNodeWithRaise;
122123
import com.oracle.graal.python.nodes.attributes.SetAttributeNode;
@@ -1001,10 +1002,10 @@ static Object doit(PFileIO self, @SuppressWarnings("unused") PNone v) {
10011002
return self.isFinalizing();
10021003
}
10031004

1004-
@Specialization(guards = "!isNoValue(v)", limit = "1")
1005-
static Object doit(PFileIO self, Object v,
1006-
@CachedLibrary("v") PythonObjectLibrary isTrue) {
1007-
self.setFinalizing(isTrue.isTrue(v));
1005+
@Specialization(guards = "!isNoValue(v)")
1006+
static Object doit(VirtualFrame frame, PFileIO self, Object v,
1007+
@Cached PyObjectIsTrueNode isTrueNode) {
1008+
self.setFinalizing(isTrueNode.execute(frame, v));
10081009
return PNone.NONE;
10091010
}
10101011
}

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/IOBaseBuiltins.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import com.oracle.graal.python.builtins.objects.function.PArguments;
9090
import com.oracle.graal.python.builtins.objects.object.PythonObject;
9191
import com.oracle.graal.python.builtins.objects.object.PythonObjectLibrary;
92+
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
9293
import com.oracle.graal.python.nodes.ErrorMessages;
9394
import com.oracle.graal.python.nodes.PGuards;
9495
import com.oracle.graal.python.nodes.PRaiseNode;
@@ -171,8 +172,8 @@ abstract static class CheckClosedNode extends PythonUnaryBuiltinNode {
171172
@Specialization(limit = "3")
172173
Object doCheckClosed(VirtualFrame frame, PythonObject self,
173174
@CachedLibrary("self") PythonObjectLibrary selfLib,
174-
@CachedLibrary(limit = "1") PythonObjectLibrary resultLib) {
175-
if (resultLib.isTrue(selfLib.lookupAttributeStrict(self, frame, CLOSED), frame)) {
175+
@Cached PyObjectIsTrueNode isTrueNode) {
176+
if (isTrueNode.execute(frame, selfLib.lookupAttributeStrict(self, frame, CLOSED))) {
176177
throw raise(ValueError, ErrorMessages.IO_CLOSED);
177178
}
178179
return PNone.NONE;

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/modules/io/TextIOWrapperBuiltins.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@
121121
import com.oracle.graal.python.lib.PyLongAsLongNode;
122122
import com.oracle.graal.python.lib.PyNumberAsSizeNode;
123123
import com.oracle.graal.python.lib.PyNumberIndexNode;
124+
import com.oracle.graal.python.lib.PyObjectIsTrueNode;
124125
import com.oracle.graal.python.nodes.call.special.LookupAndCallUnaryNode;
125126
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
126127
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
@@ -269,7 +270,7 @@ Object reconfigure(VirtualFrame frame, PTextIO self, Object encodingObj,
269270
Object lineBufferingObj, Object writeThroughObj,
270271
@Cached IONodes.ToStringNode toStringNode,
271272
@CachedLibrary("self") PythonObjectLibrary libSelf,
272-
@CachedLibrary(limit = "2") PythonObjectLibrary lib,
273+
@Cached PyObjectIsTrueNode isTrueNode,
273274
@Cached TextIOWrapperNodes.ChangeEncodingNode changeEncodingNode) {
274275
String newline = null;
275276
if (!isPNone(newlineObj)) {
@@ -281,12 +282,12 @@ Object reconfigure(VirtualFrame frame, PTextIO self, Object encodingObj,
281282
if (isPNone(lineBufferingObj)) {
282283
lineBuffering = self.isLineBuffering();
283284
} else {
284-
lineBuffering = lib.isTrue(lineBufferingObj, frame);
285+
lineBuffering = isTrueNode.execute(frame, lineBufferingObj);
285286
}
286287
if (isPNone(writeThroughObj)) {
287288
writeThrough = self.isWriteThrough();
288289
} else {
289-
writeThrough = lib.isTrue(writeThroughObj, frame);
290+
writeThrough = isTrueNode.execute(frame, writeThroughObj);
290291
}
291292
libSelf.lookupAndCallRegularMethod(self, frame, FLUSH);
292293
self.setB2cratio(0);
@@ -522,9 +523,9 @@ static Object close(VirtualFrame frame, PTextIO self,
522523
@Cached IONodes.CallFlush flush,
523524
@Cached IONodes.CallDeallocWarn deallocWarn,
524525
@Cached IONodes.CallClose close,
525-
@CachedLibrary(limit = "2") PythonObjectLibrary lib) {
526+
@Cached PyObjectIsTrueNode isTrueNode) {
526527
Object res = closedNode.call(frame, self);
527-
if (lib.isTrue(res, frame)) {
528+
if (isTrueNode.execute(frame, res)) {
528529
return PNone.NONE;
529530
} else {
530531
if (self.isFinalizing()) {

0 commit comments

Comments
 (0)