Skip to content

Commit ae56d7c

Browse files
committed
Migrate arguemnt cast nodes using PythonBufferAcquireLibrary
1 parent c05ff4d commit ae56d7c

File tree

7 files changed

+54
-22
lines changed

7 files changed

+54
-22
lines changed

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

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@
6868
import com.oracle.graal.python.nodes.function.builtins.PythonClinicBuiltinNode;
6969
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
7070
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
71-
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
71+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaiseAndIndirectCall;
7272
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
7373
import com.oracle.graal.python.nodes.util.CastToJavaStringNode;
7474
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
@@ -78,6 +78,7 @@
7878
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
7979
import com.oracle.truffle.api.dsl.NodeFactory;
8080
import com.oracle.truffle.api.dsl.Specialization;
81+
import com.oracle.truffle.api.frame.VirtualFrame;
8182
import com.oracle.truffle.api.library.CachedLibrary;
8283
import com.oracle.truffle.api.library.ExportLibrary;
8384
import com.oracle.truffle.api.library.ExportMessage;
@@ -90,11 +91,11 @@ protected List<? extends NodeFactory<? extends PythonBuiltinBaseNode>> getNodeFa
9091
return BinasciiModuleBuiltinsFactory.getFactories();
9192
}
9293

93-
abstract static class AsciiBufferConverter extends ArgumentCastNode.ArgumentCastNodeWithRaise {
94+
abstract static class AsciiBufferConverter extends ArgumentCastNodeWithRaiseAndIndirectCall {
9495
@Specialization(guards = "acquireLib.hasBuffer(value)", limit = "getCallSiteInlineCacheMaxDepth()")
95-
Object doObject(Object value,
96+
Object doObject(VirtualFrame frame, Object value,
9697
@CachedLibrary("value") PythonBufferAcquireLibrary acquireLib) {
97-
return acquireLib.acquireReadonly(value);
98+
return acquireLib.acquireReadonly(value, frame, getContext(), getLanguage(), this);
9899
}
99100

100101
@ExportLibrary(PythonBufferAccessLibrary.class)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@
9696
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryClinicBuiltinNode;
9797
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
9898
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaise;
99+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaiseAndIndirectCall;
99100
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
100101
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
101102
import com.oracle.graal.python.nodes.util.CastToJavaLongLossyNode;
@@ -2462,7 +2463,7 @@ public static DirFdConversionNode create() {
24622463
* Equivalent of CPython's {@code path_converter()}. Always returns an instance of
24632464
* {@link PosixFileHandle}.
24642465
*/
2465-
public abstract static class PathConversionNode extends ArgumentCastNodeWithRaise {
2466+
public abstract static class PathConversionNode extends ArgumentCastNodeWithRaiseAndIndirectCall {
24662467

24672468
private final String functionNameWithColon;
24682469
private final String argumentName;
@@ -2530,7 +2531,7 @@ PosixFileHandle doBuffer(VirtualFrame frame, Object value,
25302531
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib,
25312532
@CachedLibrary("getPosixSupport()") PosixSupportLibrary posixLib,
25322533
@Cached WarningsModuleBuiltins.WarnNode warningNode) {
2533-
Object buffer = bufferAcquireLib.acquireReadonly(value);
2534+
Object buffer = bufferAcquireLib.acquireReadonly(value, frame, getContext(), getLanguage(), this);
25342535
try {
25352536
warningNode.warnFormat(frame, null, PythonBuiltinClassType.DeprecationWarning, 1,
25362537
ErrorMessages.S_S_SHOULD_BE_S_NOT_P, functionNameWithColon, argumentName, getAllowedTypes(), value);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/bytes/BytesBuiltins.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,7 @@
111111
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryClinicBuiltinNode;
112112
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
113113
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode;
114+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaiseAndIndirectCall;
114115
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentClinicProvider;
115116
import com.oracle.graal.python.nodes.truffle.PythonArithmeticTypes;
116117
import com.oracle.graal.python.nodes.util.CannotCastException;
@@ -1116,7 +1117,7 @@ static int find(PBytesLike self, Object sub, int start, int end,
11161117
}
11171118
}
11181119

1119-
public abstract static class SepExpectByteNode extends ArgumentCastNode.ArgumentCastNodeWithRaise {
1120+
public abstract static class SepExpectByteNode extends ArgumentCastNodeWithRaiseAndIndirectCall {
11201121
private final Object defaultValue;
11211122

11221123
protected SepExpectByteNode(Object defaultValue) {
@@ -1143,10 +1144,10 @@ byte pstring(PString str,
11431144
}
11441145

11451146
@Specialization(guards = "bufferAcquireLib.hasBuffer(object)", limit = "3")
1146-
byte doBuffer(Object object,
1147+
byte doBuffer(VirtualFrame frame, Object object,
11471148
@CachedLibrary("object") PythonBufferAcquireLibrary bufferAcquireLib,
11481149
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib) {
1149-
Object buffer = bufferAcquireLib.acquireReadonly(object);
1150+
Object buffer = bufferAcquireLib.acquireReadonly(object, frame, getContext(), getLanguage(), this);
11501151
try {
11511152
if (bufferLib.getBufferLength(buffer) != 1) {
11521153
throw raise(ValueError, SEP_MUST_BE_LENGTH_1);
@@ -1753,7 +1754,7 @@ public static ExpectIntNode create(@ClinicConverterFactory.DefaultValue int defa
17531754
}
17541755
}
17551756

1756-
public abstract static class ExpectByteLikeNode extends ArgumentCastNode.ArgumentCastNodeWithRaise {
1757+
public abstract static class ExpectByteLikeNode extends ArgumentCastNodeWithRaiseAndIndirectCall {
17571758
private final byte[] defaultValue;
17581759

17591760
protected ExpectByteLikeNode(byte[] defaultValue) {
@@ -1769,10 +1770,10 @@ byte[] handleNone(@SuppressWarnings("unused") PNone none) {
17691770
}
17701771

17711772
@Specialization(guards = {"!isPNone(object)"}, limit = "3")
1772-
static byte[] doBuffer(Object object,
1773+
byte[] doBuffer(VirtualFrame frame, Object object,
17731774
@CachedLibrary("object") PythonBufferAcquireLibrary bufferAcquireLib,
17741775
@CachedLibrary(limit = "1") PythonBufferAccessLibrary bufferLib) {
1775-
Object buffer = bufferAcquireLib.acquireReadonly(object);
1776+
Object buffer = bufferAcquireLib.acquireReadonly(object, frame, getContext(), getLanguage(), this);
17761777
try {
17771778
// TODO avoid copying
17781779
return bufferLib.getCopiedByteArray(buffer);

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/clinic/ArgumentCastNode.java

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,18 @@
4444
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
4545
import com.oracle.graal.python.builtins.objects.exception.PBaseException;
4646
import com.oracle.graal.python.nodes.BuiltinNames;
47+
import com.oracle.graal.python.nodes.IndirectCallNode;
4748
import com.oracle.graal.python.nodes.PGuards;
4849
import com.oracle.graal.python.nodes.PRaiseNode;
4950
import com.oracle.graal.python.nodes.SpecialAttributeNames;
5051
import com.oracle.graal.python.nodes.SpecialMethodNames;
5152
import com.oracle.graal.python.runtime.PythonContext;
5253
import com.oracle.graal.python.runtime.PythonOptions;
5354
import com.oracle.graal.python.runtime.exception.PException;
55+
import com.oracle.truffle.api.Assumption;
5456
import com.oracle.truffle.api.CompilerDirectives;
57+
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
58+
import com.oracle.truffle.api.Truffle;
5559
import com.oracle.truffle.api.dsl.ImportStatic;
5660
import com.oracle.truffle.api.frame.VirtualFrame;
5761
import com.oracle.truffle.api.nodes.Node;
@@ -99,11 +103,34 @@ public final PRaiseNode getRaiseNode() {
99103
}
100104
}
101105

102-
public PythonLanguage getLanguage() {
106+
public abstract static class ArgumentCastNodeWithRaiseAndIndirectCall extends ArgumentCastNodeWithRaise implements IndirectCallNode {
107+
@CompilationFinal private Assumption nativeCodeDoesntNeedExceptionState;
108+
@CompilationFinal private Assumption nativeCodeDoesntNeedMyFrame;
109+
110+
@Override
111+
public final Assumption needNotPassFrameAssumption() {
112+
if (nativeCodeDoesntNeedMyFrame == null) {
113+
CompilerDirectives.transferToInterpreterAndInvalidate();
114+
nativeCodeDoesntNeedMyFrame = Truffle.getRuntime().createAssumption();
115+
}
116+
return nativeCodeDoesntNeedMyFrame;
117+
}
118+
119+
@Override
120+
public final Assumption needNotPassExceptionAssumption() {
121+
if (nativeCodeDoesntNeedExceptionState == null) {
122+
CompilerDirectives.transferToInterpreterAndInvalidate();
123+
nativeCodeDoesntNeedExceptionState = Truffle.getRuntime().createAssumption();
124+
}
125+
return nativeCodeDoesntNeedExceptionState;
126+
}
127+
}
128+
129+
public final PythonLanguage getLanguage() {
103130
return PythonLanguage.get(this);
104131
}
105132

106-
public PythonContext getContext() {
133+
public final PythonContext getContext() {
107134
return PythonContext.get(this);
108135
}
109136

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/clinic/ObjectConversionBaseNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@
4141
package com.oracle.graal.python.nodes.function.builtins.clinic;
4242

4343
import com.oracle.graal.python.builtins.objects.PNone;
44-
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaise;
44+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaiseAndIndirectCall;
4545
import com.oracle.truffle.api.dsl.Specialization;
4646

47-
public abstract class ObjectConversionBaseNode extends ArgumentCastNodeWithRaise {
47+
public abstract class ObjectConversionBaseNode extends ArgumentCastNodeWithRaiseAndIndirectCall {
4848
private final Object defaultValue;
4949
protected final boolean useDefaultForNone;
5050

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/clinic/ReadableBufferConversionNode.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
import com.oracle.graal.python.builtins.objects.PNone;
4545
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary;
4646
import com.oracle.truffle.api.dsl.Specialization;
47+
import com.oracle.truffle.api.frame.VirtualFrame;
4748
import com.oracle.truffle.api.library.CachedLibrary;
4849

4950
public abstract class ReadableBufferConversionNode extends ObjectConversionBaseNode {
@@ -52,9 +53,9 @@ protected ReadableBufferConversionNode(Object defaultValue, boolean useDefaultFo
5253
}
5354

5455
@Specialization(guards = "!isHandledPNone(value)", limit = "getCallSiteInlineCacheMaxDepth()")
55-
Object doObject(Object value,
56+
Object doObject(VirtualFrame frame, Object value,
5657
@CachedLibrary("value") PythonBufferAcquireLibrary acquireLib) {
57-
return acquireLib.acquireReadonly(value);
58+
return acquireLib.acquireReadonly(value, frame, getContext(), getLanguage(), this);
5859
}
5960

6061
@ClinicConverterFactory

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/nodes/function/builtins/clinic/WritableBufferConversionNode.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,23 +46,24 @@
4646
import com.oracle.graal.python.annotations.ClinicConverterFactory.BuiltinName;
4747
import com.oracle.graal.python.builtins.objects.buffer.PythonBufferAcquireLibrary;
4848
import com.oracle.graal.python.nodes.ErrorMessages;
49-
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaise;
49+
import com.oracle.graal.python.nodes.function.builtins.clinic.ArgumentCastNode.ArgumentCastNodeWithRaiseAndIndirectCall;
5050
import com.oracle.graal.python.runtime.exception.PException;
5151
import com.oracle.truffle.api.dsl.Specialization;
52+
import com.oracle.truffle.api.frame.VirtualFrame;
5253
import com.oracle.truffle.api.library.CachedLibrary;
5354

54-
public abstract class WritableBufferConversionNode extends ArgumentCastNodeWithRaise {
55+
public abstract class WritableBufferConversionNode extends ArgumentCastNodeWithRaiseAndIndirectCall {
5556
private final String builtinName;
5657

5758
public WritableBufferConversionNode(String builtinName) {
5859
this.builtinName = builtinName;
5960
}
6061

6162
@Specialization(limit = "getCallSiteInlineCacheMaxDepth()")
62-
Object doObject(Object value,
63+
Object doObject(VirtualFrame frame, Object value,
6364
@CachedLibrary("value") PythonBufferAcquireLibrary acquireLib) {
6465
try {
65-
return acquireLib.acquireWritable(value);
66+
return acquireLib.acquireWritable(value, frame, getContext(), getLanguage(), this);
6667
} catch (PException e) {
6768
throw raise(TypeError, ErrorMessages.S_BRACKETS_ARG_MUST_BE_READ_WRITE_BYTES_LIKE_NOT_P, builtinName, value);
6869
}

0 commit comments

Comments
 (0)