Skip to content

Commit 21844b8

Browse files
committed
Do not use PythonBuiltinBaseNode for helper nodes
1 parent 2adaebf commit 21844b8

File tree

5 files changed

+143
-141
lines changed

5 files changed

+143
-141
lines changed

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

Lines changed: 77 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@
114114
import com.oracle.graal.python.runtime.GilNode;
115115
import com.oracle.graal.python.runtime.PosixConstants;
116116
import com.oracle.graal.python.runtime.PosixConstants.IntConstant;
117+
import com.oracle.graal.python.runtime.PosixSupport;
117118
import com.oracle.graal.python.runtime.PosixSupportLibrary;
118119
import com.oracle.graal.python.runtime.PosixSupportLibrary.Buffer;
119120
import com.oracle.graal.python.runtime.PosixSupportLibrary.OpenPtyResult;
@@ -134,6 +135,7 @@
134135
import com.oracle.truffle.api.dsl.Cached;
135136
import com.oracle.truffle.api.dsl.Cached.Exclusive;
136137
import com.oracle.truffle.api.dsl.Cached.Shared;
138+
import com.oracle.truffle.api.dsl.GenerateCached;
137139
import com.oracle.truffle.api.dsl.GenerateInline;
138140
import com.oracle.truffle.api.dsl.GenerateNodeFactory;
139141
import com.oracle.truffle.api.dsl.ImportStatic;
@@ -533,7 +535,7 @@ private void execv(VirtualFrame frame, PosixPath path, Object argv, SequenceStor
533535
}
534536
Object[] opaqueArgs = new Object[args.length];
535537
for (int i = 0; i < args.length; ++i) {
536-
opaqueArgs[i] = toOpaquePathNode.execute(frame, args[i], i == 0);
538+
opaqueArgs[i] = toOpaquePathNode.execute(frame, inliningTarget, args[i], i == 0);
537539
}
538540
// TODO ValueError "execv() arg 2 first element cannot be empty"
539541

@@ -1844,7 +1846,7 @@ static Object dupAndFdopendir(VirtualFrame frame, Node inliningTarget, PosixSupp
18441846
}
18451847

18461848
@SuppressWarnings("truffle-static-method")
1847-
abstract static class UtimeArgsToTimespecNode extends PythonBuiltinBaseNode {
1849+
abstract static class UtimeArgsToTimespecNode extends PNodeWithRaise {
18481850
abstract long[] execute(VirtualFrame frame, Object times, Object ns);
18491851

18501852
Timeval[] toTimeval(VirtualFrame frame, Object times, Object ns) {
@@ -1906,8 +1908,8 @@ private long[] convertToTimespec(VirtualFrame frame, Node inliningTarget, PTuple
19061908
throw timesTupleError();
19071909
}
19081910
long[] timespec = new long[4];
1909-
convertToTimespecBaseNode.execute(frame, getItemNode.execute(times.getSequenceStorage(), 0), timespec, 0);
1910-
convertToTimespecBaseNode.execute(frame, getItemNode.execute(times.getSequenceStorage(), 1), timespec, 2);
1911+
convertToTimespecBaseNode.execute(frame, inliningTarget, getItemNode.execute(times.getSequenceStorage(), 0), timespec, 0);
1912+
convertToTimespecBaseNode.execute(frame, inliningTarget, getItemNode.execute(times.getSequenceStorage(), 1), timespec, 2);
19111913
return timespec;
19121914
}
19131915
}
@@ -2667,26 +2669,23 @@ Object register(Object before, Object afterInChild, Object afterInParent) {
26672669
/**
26682670
* Helper node that accepts either str or bytes and converts it to {@code PBytes}.
26692671
*/
2670-
public abstract static class StringOrBytesToBytesNode extends PythonBuiltinBaseNode {
2671-
public abstract PBytes execute(Object obj);
2672-
2673-
@Specialization
2674-
PBytes doString(TruffleString str,
2675-
@Shared("switchEncoding") @Cached TruffleString.SwitchEncodingNode switchEncodingNode,
2676-
@Shared("copyToByteArray") @Cached TruffleString.CopyToByteArrayNode copyToByteArrayNode) {
2672+
@GenerateInline
2673+
@GenerateCached(false)
2674+
@ImportStatic(PGuards.class)
2675+
public abstract static class StringOrBytesToBytesNode extends Node {
2676+
public abstract PBytes execute(Node inliningTarget, Object obj);
2677+
2678+
@Specialization(guards = "isString(strObj)")
2679+
static PBytes doString(Node inliningTarget, Object strObj,
2680+
@Cached CastToTruffleStringNode castToStringNode,
2681+
@Cached(inline = false) TruffleString.SwitchEncodingNode switchEncodingNode,
2682+
@Cached(inline = false) TruffleString.CopyToByteArrayNode copyToByteArrayNode,
2683+
@Cached(inline = false) PythonObjectFactory factory) {
2684+
TruffleString str = castToStringNode.execute(inliningTarget, strObj);
26772685
TruffleString utf8 = switchEncodingNode.execute(str, Encoding.UTF_8);
26782686
byte[] bytes = new byte[utf8.byteLength(Encoding.UTF_8)];
26792687
copyToByteArrayNode.execute(utf8, 0, bytes, 0, bytes.length, Encoding.UTF_8);
2680-
return factory().createBytes(bytes);
2681-
}
2682-
2683-
@Specialization
2684-
PBytes doPString(PString pstr,
2685-
@Bind("this") Node inliningTarget,
2686-
@Cached CastToTruffleStringNode castToStringNode,
2687-
@Shared("switchEncoding") @Cached TruffleString.SwitchEncodingNode switchEncodingNode,
2688-
@Shared("copyToByteArray") @Cached TruffleString.CopyToByteArrayNode copyToByteArrayNode) {
2689-
return doString(castToStringNode.execute(inliningTarget, pstr), switchEncodingNode, copyToByteArrayNode);
2688+
return factory.createBytes(bytes);
26902689
}
26912690

26922691
@Specialization
@@ -2700,34 +2699,32 @@ static PBytes doBytes(PBytes bytes) {
27002699
* the {@link PosixSupportLibrary} in use. Basically equivalent of
27012700
* {@code PyUnicode_EncodeFSDefault}.
27022701
*/
2703-
abstract static class StringOrBytesToOpaquePathNode extends PNodeWithRaise {
2704-
abstract Object execute(Object obj);
2705-
2706-
@Specialization
2707-
Object doString(TruffleString str,
2708-
@CachedLibrary("getContext().getPosixSupport()") PosixSupportLibrary posixLib) {
2709-
return checkPath(posixLib.createPathFromString(getContext().getPosixSupport(), str));
2710-
}
2711-
2712-
@Specialization
2713-
@SuppressWarnings("truffle-static-method")
2714-
Object doPString(PString pstr,
2715-
@Bind("this") Node inliningTarget,
2702+
@GenerateInline
2703+
@GenerateCached(false)
2704+
@ImportStatic(PGuards.class)
2705+
abstract static class StringOrBytesToOpaquePathNode extends Node {
2706+
abstract Object execute(Node inliningTarget, Object obj);
2707+
2708+
@Specialization(guards = "isString(strObj)")
2709+
static Object doString(Node inliningTarget, Object strObj,
27162710
@Cached CastToTruffleStringNode castToStringNode,
2717-
@CachedLibrary("getContext().getPosixSupport()") PosixSupportLibrary posixLib) {
2718-
return doString(castToStringNode.execute(inliningTarget, pstr), posixLib);
2711+
@Shared @CachedLibrary(limit = "1") PosixSupportLibrary posixLib,
2712+
@Shared @Cached PRaiseNode.Lazy raiseNode) {
2713+
TruffleString str = castToStringNode.execute(inliningTarget, strObj);
2714+
return checkPath(inliningTarget, posixLib.createPathFromString(PosixSupport.get(inliningTarget), str), raiseNode);
27192715
}
27202716

27212717
@Specialization
2722-
Object doBytes(PBytes bytes,
2723-
@Cached BytesNodes.ToBytesNode toBytesNode,
2724-
@CachedLibrary("getContext().getPosixSupport()") PosixSupportLibrary posixLib) {
2725-
return checkPath(posixLib.createPathFromBytes(getContext().getPosixSupport(), toBytesNode.execute(bytes)));
2718+
static Object doBytes(Node inliningTarget, PBytes bytes,
2719+
@Cached(inline = false) BytesNodes.ToBytesNode toBytesNode,
2720+
@Shared @CachedLibrary(limit = "1") PosixSupportLibrary posixLib,
2721+
@Shared @Cached PRaiseNode.Lazy raiseNode) {
2722+
return checkPath(inliningTarget, posixLib.createPathFromBytes(PosixSupport.get(inliningTarget), toBytesNode.execute(bytes)), raiseNode);
27262723
}
27272724

2728-
private Object checkPath(Object path) {
2725+
private static Object checkPath(Node inliningTarget, Object path, PRaiseNode.Lazy raiseNode) {
27292726
if (path == null) {
2730-
throw raise(ValueError, ErrorMessages.EMBEDDED_NULL_BYTE);
2727+
throw raiseNode.get(inliningTarget).raise(ValueError, ErrorMessages.EMBEDDED_NULL_BYTE);
27312728
}
27322729
return path;
27332730
}
@@ -2737,43 +2734,51 @@ private Object checkPath(Object path) {
27372734
* Similar to {@code PyUnicode_FSConverter}, but the actual conversion is delegated to the
27382735
* {@link PosixSupportLibrary} implementation.
27392736
*/
2740-
abstract static class ObjectToOpaquePathNode extends PNodeWithRaise {
2741-
abstract Object execute(VirtualFrame frame, Object obj, boolean checkEmpty);
2737+
@GenerateInline
2738+
@GenerateCached(false)
2739+
abstract static class ObjectToOpaquePathNode extends Node {
2740+
abstract Object execute(VirtualFrame frame, Node inliningTarget, Object obj, boolean checkEmpty);
27422741

27432742
@Specialization(guards = "!checkEmpty")
2744-
static Object noCheck(VirtualFrame frame, Object obj, @SuppressWarnings("unused") boolean checkEmpty,
2745-
@Bind("this") Node inliningTarget,
2743+
static Object noCheck(VirtualFrame frame, Node inliningTarget, Object obj, @SuppressWarnings("unused") boolean checkEmpty,
27462744
@Exclusive @Cached PyOSFSPathNode fspathNode,
27472745
@Exclusive @Cached StringOrBytesToOpaquePathNode stringOrBytesToOpaquePathNode) {
2748-
return stringOrBytesToOpaquePathNode.execute(fspathNode.execute(frame, inliningTarget, obj));
2746+
return stringOrBytesToOpaquePathNode.execute(inliningTarget, fspathNode.execute(frame, inliningTarget, obj));
27492747
}
27502748

27512749
@Specialization(guards = "checkEmpty")
2752-
@SuppressWarnings("truffle-static-method")
2753-
Object withCheck(VirtualFrame frame, Object obj, @SuppressWarnings("unused") boolean checkEmpty,
2754-
@Bind("this") Node inliningTarget,
2750+
static Object withCheck(VirtualFrame frame, Node inliningTarget, Object obj, @SuppressWarnings("unused") boolean checkEmpty,
27552751
@Exclusive @Cached PyOSFSPathNode fspathNode,
27562752
@Cached PyObjectSizeNode sizeNode,
2757-
@Exclusive @Cached StringOrBytesToOpaquePathNode stringOrBytesToOpaquePathNode) {
2753+
@Exclusive @Cached StringOrBytesToOpaquePathNode stringOrBytesToOpaquePathNode,
2754+
@Cached PRaiseNode.Lazy raiseNode) {
27582755
Object stringOrBytes = fspathNode.execute(frame, inliningTarget, obj);
27592756
if (sizeNode.execute(frame, inliningTarget, obj) == 0) {
2760-
throw raise(ValueError, ErrorMessages.EXECV_ARG2_FIRST_ELEMENT_CANNOT_BE_EMPTY);
2757+
throw raiseNode.get(inliningTarget).raise(ValueError, ErrorMessages.EXECV_ARG2_FIRST_ELEMENT_CANNOT_BE_EMPTY);
27612758
}
2762-
return stringOrBytesToOpaquePathNode.execute(stringOrBytes);
2759+
return stringOrBytesToOpaquePathNode.execute(inliningTarget, stringOrBytes);
27632760
}
27642761
}
27652762

2766-
abstract static class ConvertToTimespecBaseNode extends PythonBuiltinBaseNode {
2767-
abstract void execute(VirtualFrame frame, Object obj, long[] timespec, int offset);
2763+
abstract static class ConvertToTimespecBaseNode extends Node {
2764+
abstract void execute(VirtualFrame frame, Node inliningTarget, Object obj, long[] timespec, int offset);
27682765
}
27692766

27702767
/**
27712768
* Equivalent of {@code _PyTime_ObjectToTimespec} as used in {@code os_utime_impl}.
27722769
*/
2770+
@GenerateInline
2771+
@GenerateCached(false)
2772+
@ImportStatic(PGuards.class)
27732773
abstract static class ObjectToTimespecNode extends ConvertToTimespecBaseNode {
27742774

2775-
@Specialization(guards = "!isNan(value)")
2776-
void doDoubleNotNan(double value, long[] timespec, int offset) {
2775+
@Specialization
2776+
static void doDouble(Node inliningTarget, double value, long[] timespec, int offset,
2777+
@Shared @Cached PRaiseNode.Lazy raiseNode) {
2778+
if (Double.isNaN(value)) {
2779+
throw raiseNode.get(inliningTarget).raise(ValueError, ErrorMessages.INVALID_VALUE_NAN);
2780+
}
2781+
27772782
double denominator = 1000000000.0;
27782783
double floatPart = value % 1;
27792784
double intPart = value - floatPart;
@@ -2788,26 +2793,17 @@ void doDoubleNotNan(double value, long[] timespec, int offset) {
27882793
}
27892794
assert 0.0 <= floatPart && floatPart < denominator;
27902795
if (!MathGuards.fitLong(intPart)) {
2791-
throw raise(OverflowError, ErrorMessages.TIMESTAMP_OUT_OF_RANGE);
2796+
throw raiseNode.get(inliningTarget).raise(OverflowError, ErrorMessages.TIMESTAMP_OUT_OF_RANGE);
27922797
}
27932798
timespec[offset] = (long) intPart;
27942799
timespec[offset + 1] = (long) floatPart;
27952800
assert 0 <= timespec[offset + 1] && timespec[offset + 1] < (long) denominator;
27962801
}
27972802

2798-
@Specialization(guards = "isNan(value)")
2799-
@SuppressWarnings("unused")
2800-
void doDoubleNan(double value, long[] timespec, int offset) {
2801-
throw raise(ValueError, ErrorMessages.INVALID_VALUE_NAN);
2802-
}
2803-
28042803
@Specialization
2805-
void doPFloat(PFloat obj, long[] timespec, int offset) {
2806-
double value = obj.getValue();
2807-
if (Double.isNaN(value)) {
2808-
throw raise(ValueError, ErrorMessages.INVALID_VALUE_NAN);
2809-
}
2810-
doDoubleNotNan(value, timespec, offset);
2804+
static void doPFloat(Node inliningTarget, PFloat obj, long[] timespec, int offset,
2805+
@Shared @Cached PRaiseNode.Lazy raiseNode) {
2806+
doDouble(inliningTarget, obj.getValue(), timespec, offset, raiseNode);
28112807
}
28122808

28132809
@Specialization
@@ -2823,26 +2819,24 @@ static void doLong(long value, long[] timespec, int offset) {
28232819
}
28242820

28252821
@Specialization(guards = {"!isDouble(value)", "!isPFloat(value)", "!isInteger(value)"})
2826-
void doGeneric(VirtualFrame frame, Object value, long[] timespec, int offset,
2827-
@Bind("this") Node inliningTarget,
2828-
@Cached PyLongAsLongAndOverflowNode asLongNode) {
2822+
static void doGeneric(VirtualFrame frame, Node inliningTarget, Object value, long[] timespec, int offset,
2823+
@Cached PyLongAsLongAndOverflowNode asLongNode,
2824+
@Shared @Cached PRaiseNode.Lazy raiseNode) {
28292825
try {
28302826
timespec[offset] = asLongNode.execute(frame, inliningTarget, value);
28312827
} catch (OverflowException e) {
2832-
throw raise(OverflowError, ErrorMessages.TIMESTAMP_OUT_OF_RANGE);
2828+
throw raiseNode.get(inliningTarget).raise(OverflowError, ErrorMessages.TIMESTAMP_OUT_OF_RANGE);
28332829
}
28342830
timespec[offset + 1] = 0;
28352831
}
2836-
2837-
protected static boolean isNan(double value) {
2838-
return Double.isNaN(value);
2839-
}
28402832
}
28412833

28422834
/**
28432835
* Equivalent of {@code split_py_long_to_s_and_ns} as used in {@code os_utime_impl}.
28442836
*/
2845-
@ImportStatic(BinaryArithmetic.class)
2837+
@GenerateInline
2838+
@GenerateCached(false)
2839+
@ImportStatic({BinaryArithmetic.class, PGuards.class})
28462840
abstract static class SplitLongToSAndNsNode extends ConvertToTimespecBaseNode {
28472841

28482842
private static final long BILLION = 1000000000;
@@ -2859,11 +2853,10 @@ static void doLong(long value, long[] timespec, int offset) {
28592853
}
28602854

28612855
@Specialization(guards = {"!isInteger(value)"})
2862-
static void doGeneric(VirtualFrame frame, Object value, long[] timespec, int offset,
2863-
@Bind("this") Node inliningTarget,
2864-
@Cached("DivMod.create()") BinaryOpNode callDivmod,
2856+
static void doGeneric(VirtualFrame frame, Node inliningTarget, Object value, long[] timespec, int offset,
2857+
@Cached(value = "DivMod.create()", inline = false) BinaryOpNode callDivmod,
28652858
@Cached LenNode lenNode,
2866-
@Cached("createNotNormalized()") GetItemNode getItemNode,
2859+
@Cached(value = "createNotNormalized()", inline = false) GetItemNode getItemNode,
28672860
@Cached PyLongAsLongNode asLongNode,
28682861
@Cached PRaiseNode.Lazy raiseNode) {
28692862
Object divmod = callDivmod.executeObject(frame, value, BILLION);
@@ -2923,7 +2916,7 @@ static PBytes convert(VirtualFrame frame, Object value,
29232916
@Bind("this") Node inliningTarget,
29242917
@Cached PyOSFSPathNode fspathNode,
29252918
@Cached StringOrBytesToBytesNode stringOrBytesToBytesNode) {
2926-
return stringOrBytesToBytesNode.execute(fspathNode.execute(frame, inliningTarget, value));
2919+
return stringOrBytesToBytesNode.execute(inliningTarget, fspathNode.execute(frame, inliningTarget, value));
29272920
}
29282921

29292922
@ClinicConverterFactory

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ Object[] doSequence(VirtualFrame frame, Object processArgs,
142142
throw raise(RuntimeError, ErrorMessages.ARGS_CHANGED_DURING_ITERATION);
143143
}
144144
Object o = getItemNode.execute(argsStorage, i);
145-
argsArray[i] = objectToOpaquePathNode.execute(frame, o, false);
145+
argsArray[i] = objectToOpaquePathNode.execute(frame, inliningTarget, o, false);
146146
}
147147
return argsArray;
148148
}
@@ -264,7 +264,7 @@ int forkExec(VirtualFrame frame, Object[] args, Object executableList, boolean c
264264
}
265265
Object[] processArgs = args;
266266
int[] fdsToKeep = convertFdSequence(inliningTarget, (PTuple) fdsToKeepObj, tupleGetItem, castToIntNode);
267-
Object cwd = PGuards.isPNone(cwdObj) ? null : objectToOpaquePathNode.execute(frame, cwdObj, false);
267+
Object cwd = PGuards.isPNone(cwdObj) ? null : objectToOpaquePathNode.execute(frame, inliningTarget, cwdObj, false);
268268

269269
byte[] sysExecutable = fsEncode(getContext().getOption(PythonOptions.Executable).toJavaStringUncached());
270270

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ Object doGetpwname(VirtualFrame frame, TruffleString name,
200200
@Cached PConstructAndRaiseNode.Lazy constructAndRaiseNode) {
201201
// Note: CPython also takes only Strings, not bytes, and then encodes the String
202202
// StringOrBytesToOpaquePathNode already checks for embedded '\0'
203-
Object nameEncoded = encodeFSDefault.execute(name);
203+
Object nameEncoded = encodeFSDefault.execute(inliningTarget, name);
204204
PwdResult pwd;
205205
try {
206206
gil.release(true);

0 commit comments

Comments
 (0)