Skip to content

Commit d4f0d73

Browse files
committed
Split AllToJavaNode
AllToJavaNode used AsPythonObjectNode which doesn't resolve deref handles. There are situations where that is needed, i.e. when a function pointer is executed directly. Split it into two nodes where one does the reference resolution and one does not.
1 parent 167d579 commit d4f0d73

File tree

1 file changed

+79
-21
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi

1 file changed

+79
-21
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/cext/capi/CExtNodes.java

Lines changed: 79 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@
7575
import com.oracle.graal.python.builtins.objects.cext.PythonNativeObject;
7676
import com.oracle.graal.python.builtins.objects.cext.PythonNativeVoidPtr;
7777
import com.oracle.graal.python.builtins.objects.cext.capi.CApiContext.LLVMType;
78-
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.AllToJavaNodeGen;
7978
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.AllToSulongNodeGen;
8079
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.AsPythonObjectNodeGen;
8180
import com.oracle.graal.python.builtins.objects.cext.capi.CExtNodesFactory.BinaryFirstToSulongNodeGen;
@@ -1405,6 +1404,10 @@ static Object doForeign(@SuppressWarnings("unused") CExtContext nativeContext, O
14051404
}
14061405
return asPythonObjectNode.execute(resolveNativeReferenceNode.execute(resolveHandleNode.execute(value), false));
14071406
}
1407+
1408+
public static ToJavaNode create() {
1409+
return ToJavaNodeGen.create();
1410+
}
14081411
}
14091412

14101413
/**
@@ -1739,7 +1742,7 @@ Object add(Object pointer, long offset,
17391742

17401743
// -----------------------------------------------------------------------------------------------------------------
17411744
@GenerateUncached
1742-
public abstract static class AllToJavaNode extends PNodeWithContext {
1745+
public abstract static class AllToPythonNode extends PNodeWithContext {
17431746

17441747
final Object[] execute(Object[] args) {
17451748
return execute(args, 0);
@@ -1788,8 +1791,63 @@ static AsPythonObjectNode[] createNodes(int n) {
17881791
return nodes;
17891792
}
17901793

1794+
public static AllToPythonNode create() {
1795+
return CExtNodesFactory.AllToPythonNodeGen.create();
1796+
}
1797+
}
1798+
1799+
@GenerateUncached
1800+
public abstract static class AllToJavaNode extends PNodeWithContext {
1801+
1802+
final Object[] execute(Object[] args) {
1803+
return execute(args, 0);
1804+
}
1805+
1806+
abstract Object[] execute(Object[] args, int offset);
1807+
1808+
@Specialization(guards = { //
1809+
"args.length == cachedLength", //
1810+
"offset == cachedOffset", //
1811+
"effectiveLen(cachedLength, cachedOffset) < 5"}, //
1812+
limit = "5")
1813+
@ExplodeLoop
1814+
static Object[] cached(Object[] args, @SuppressWarnings("unused") int offset,
1815+
@Cached("args.length") int cachedLength,
1816+
@Cached("offset") int cachedOffset,
1817+
@Cached("createNodes(args.length)") ToJavaNode[] toJavaNodes) {
1818+
int n = cachedLength - cachedOffset;
1819+
Object[] output = new Object[n];
1820+
for (int i = 0; i < n; i++) {
1821+
output[i] = toJavaNodes[i].execute(args[i + cachedOffset]);
1822+
}
1823+
return output;
1824+
}
1825+
1826+
@Specialization(replaces = "cached")
1827+
static Object[] uncached(Object[] args, int offset,
1828+
@Exclusive @Cached ToJavaNode toJavaNode) {
1829+
int len = args.length - offset;
1830+
Object[] output = new Object[len];
1831+
for (int i = 0; i < len; i++) {
1832+
output[i] = toJavaNode.execute(args[i + offset]);
1833+
}
1834+
return output;
1835+
}
1836+
1837+
static int effectiveLen(int len, int offset) {
1838+
return len - offset;
1839+
}
1840+
1841+
static ToJavaNode[] createNodes(int n) {
1842+
ToJavaNode[] nodes = new ToJavaNode[n];
1843+
for (int i = 0; i < n; i++) {
1844+
nodes[i] = ToJavaNode.create();
1845+
}
1846+
return nodes;
1847+
}
1848+
17911849
public static AllToJavaNode create() {
1792-
return AllToJavaNodeGen.create();
1850+
return CExtNodesFactory.AllToJavaNodeGen.create();
17931851
}
17941852
}
17951853

@@ -1887,24 +1945,24 @@ Object upcall1(VirtualFrame frame, Object[] args,
18871945
@Specialization(guards = "args.length == 3")
18881946
Object upcall2(VirtualFrame frame, Object[] args,
18891947
@Cached CallBinaryMethodNode callNode,
1890-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode) {
1891-
Object[] converted = allToJavaNode.execute(args, 1);
1948+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode) {
1949+
Object[] converted = allToPythonNode.execute(args, 1);
18921950
return callNode.executeObject(frame, args[0], converted[0], converted[1]);
18931951
}
18941952

18951953
@Specialization(guards = "args.length == 4")
18961954
Object upcall3(VirtualFrame frame, Object[] args,
18971955
@Cached CallTernaryMethodNode callNode,
1898-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode) {
1899-
Object[] converted = allToJavaNode.execute(args, 1);
1956+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode) {
1957+
Object[] converted = allToPythonNode.execute(args, 1);
19001958
return callNode.execute(frame, args[0], converted[0], converted[1], converted[2]);
19011959
}
19021960

19031961
@Specialization(replaces = {"upcall0", "upcall1", "upcall2", "upcall3"})
19041962
Object upcall(VirtualFrame frame, Object[] args,
19051963
@Cached CallNode callNode,
1906-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode) {
1907-
Object[] converted = allToJavaNode.execute(args, 1);
1964+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode) {
1965+
Object[] converted = allToPythonNode.execute(args, 1);
19081966
return callNode.execute(frame, args[0], converted, new PKeyword[0]);
19091967
}
19101968

@@ -2115,9 +2173,9 @@ Object upcall1(VirtualFrame frame, Object cextModule, Object[] args,
21152173
@Specialization(guards = "args.length == 3")
21162174
Object upcall2(VirtualFrame frame, Object cextModule, Object[] args,
21172175
@Cached CallBinaryMethodNode callNode,
2118-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode,
2176+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode,
21192177
@Shared("getAttrNode") @Cached ReadAttributeFromObjectNode getAttrNode) {
2120-
Object[] converted = allToJavaNode.execute(args, 1);
2178+
Object[] converted = allToPythonNode.execute(args, 1);
21212179
assert args[0] instanceof String;
21222180
Object callable = getAttrNode.execute(cextModule, args[0]);
21232181
return callNode.executeObject(frame, callable, converted[0], converted[1]);
@@ -2126,9 +2184,9 @@ Object upcall2(VirtualFrame frame, Object cextModule, Object[] args,
21262184
@Specialization(guards = "args.length == 4")
21272185
Object upcall3(VirtualFrame frame, Object cextModule, Object[] args,
21282186
@Cached CallTernaryMethodNode callNode,
2129-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode,
2187+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode,
21302188
@Shared("getAttrNode") @Cached ReadAttributeFromObjectNode getAttrNode) {
2131-
Object[] converted = allToJavaNode.execute(args, 1);
2189+
Object[] converted = allToPythonNode.execute(args, 1);
21322190
assert args[0] instanceof String;
21332191
Object callable = getAttrNode.execute(cextModule, args[0]);
21342192
return callNode.execute(frame, callable, converted[0], converted[1], converted[2]);
@@ -2137,9 +2195,9 @@ Object upcall3(VirtualFrame frame, Object cextModule, Object[] args,
21372195
@Specialization(replaces = {"upcall0", "upcall1", "upcall2", "upcall3"})
21382196
Object upcall(VirtualFrame frame, Object cextModule, Object[] args,
21392197
@Cached CallNode callNode,
2140-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode,
2198+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode,
21412199
@Shared("getAttrNode") @Cached ReadAttributeFromObjectNode getAttrNode) {
2142-
Object[] converted = allToJavaNode.execute(args, 1);
2200+
Object[] converted = allToPythonNode.execute(args, 1);
21432201
assert args[0] instanceof String;
21442202
Object callable = getAttrNode.execute(cextModule, args[0]);
21452203
return callNode.execute(frame, callable, converted, PKeyword.EMPTY_KEYWORDS);
@@ -2191,9 +2249,9 @@ Object upcall1(VirtualFrame frame, Object[] args,
21912249
Object upcall2(VirtualFrame frame, Object[] args,
21922250
@Cached CallBinaryMethodNode callNode,
21932251
@Cached CExtNodes.AsPythonObjectNode receiverToJavaNode,
2194-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode,
2252+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode,
21952253
@Shared("getAttrNode") @Cached GetAttrNode getAttrNode) {
2196-
Object[] converted = allToJavaNode.execute(args, 2);
2254+
Object[] converted = allToPythonNode.execute(args, 2);
21972255
Object receiver = receiverToJavaNode.execute(args[0]);
21982256
assert PGuards.isString(args[1]);
21992257
Object callable = getAttrNode.execute(frame, receiver, args[1], PNone.NO_VALUE);
@@ -2204,9 +2262,9 @@ Object upcall2(VirtualFrame frame, Object[] args,
22042262
Object upcall3(VirtualFrame frame, Object[] args,
22052263
@Cached CallTernaryMethodNode callNode,
22062264
@Cached CExtNodes.AsPythonObjectNode receiverToJavaNode,
2207-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode,
2265+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode,
22082266
@Shared("getAttrNode") @Cached GetAttrNode getAttrNode) {
2209-
Object[] converted = allToJavaNode.execute(args, 2);
2267+
Object[] converted = allToPythonNode.execute(args, 2);
22102268
Object receiver = receiverToJavaNode.execute(args[0]);
22112269
assert PGuards.isString(args[1]);
22122270
Object callable = getAttrNode.execute(frame, receiver, args[1], PNone.NO_VALUE);
@@ -2217,11 +2275,11 @@ Object upcall3(VirtualFrame frame, Object[] args,
22172275
Object upcall(VirtualFrame frame, Object[] args,
22182276
@Cached CallNode callNode,
22192277
@Cached CExtNodes.AsPythonObjectNode receiverToJavaNode,
2220-
@Shared("allToJavaNode") @Cached AllToJavaNode allToJavaNode,
2278+
@Shared("allToJavaNode") @Cached AllToPythonNode allToPythonNode,
22212279
@Shared("getAttrNode") @Cached GetAttrNode getAttrNode) {
22222280
// we needs at least a receiver and a member name
22232281
assert args.length >= 2;
2224-
Object[] converted = allToJavaNode.execute(args, 2);
2282+
Object[] converted = allToPythonNode.execute(args, 2);
22252283
Object receiver = receiverToJavaNode.execute(args[0]);
22262284
assert PGuards.isString(args[1]);
22272285
Object callable = getAttrNode.execute(frame, receiver, args[1], PNone.NO_VALUE);

0 commit comments

Comments
 (0)