Skip to content

Commit 11275a0

Browse files
committed
inline the may_raise wrappers, too, by generating unary/binary/ternary builtin root nodes
1 parent 0721fc3 commit 11275a0

File tree

1 file changed

+151
-17
lines changed

1 file changed

+151
-17
lines changed

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

Lines changed: 151 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
import com.oracle.graal.python.builtins.PythonBuiltins;
6161
import com.oracle.graal.python.builtins.modules.TruffleCextBuiltinsFactory.CheckFunctionResultNodeGen;
6262
import com.oracle.graal.python.builtins.modules.TruffleCextBuiltinsFactory.GetByteArrayNodeGen;
63+
import com.oracle.graal.python.builtins.modules.TruffleCextBuiltinsFactory.MakeMayRaiseWrapperNodeFactory.MayRaiseBinaryNodeGen;
64+
import com.oracle.graal.python.builtins.modules.TruffleCextBuiltinsFactory.MakeMayRaiseWrapperNodeFactory.MayRaiseTernaryNodeGen;
65+
import com.oracle.graal.python.builtins.modules.TruffleCextBuiltinsFactory.MakeMayRaiseWrapperNodeFactory.MayRaiseUnaryNodeGen;
6366
import com.oracle.graal.python.builtins.objects.PNone;
6467
import com.oracle.graal.python.builtins.objects.PythonAbstractObject;
6568
import com.oracle.graal.python.builtins.objects.bytes.BytesBuiltins;
@@ -109,6 +112,7 @@
109112
import com.oracle.graal.python.nodes.SpecialAttributeNames;
110113
import com.oracle.graal.python.nodes.SpecialMethodNames;
111114
import com.oracle.graal.python.nodes.argument.CreateArgumentsNode;
115+
import com.oracle.graal.python.nodes.argument.ReadArgumentNode;
112116
import com.oracle.graal.python.nodes.argument.ReadIndexedArgumentNode;
113117
import com.oracle.graal.python.nodes.argument.ReadVarArgsNode;
114118
import com.oracle.graal.python.nodes.argument.ReadVarKeywordsNode;
@@ -117,9 +121,11 @@
117121
import com.oracle.graal.python.nodes.call.InvokeNode;
118122
import com.oracle.graal.python.nodes.call.PythonCallNode;
119123
import com.oracle.graal.python.nodes.expression.BinaryComparisonNode;
120-
import com.oracle.graal.python.nodes.function.FunctionRootNode;
124+
import com.oracle.graal.python.nodes.function.BuiltinFunctionRootNode;
121125
import com.oracle.graal.python.nodes.function.PythonBuiltinBaseNode;
122126
import com.oracle.graal.python.nodes.function.PythonBuiltinNode;
127+
import com.oracle.graal.python.nodes.function.builtins.PythonBinaryBuiltinNode;
128+
import com.oracle.graal.python.nodes.function.builtins.PythonTernaryBuiltinNode;
123129
import com.oracle.graal.python.nodes.function.builtins.PythonUnaryBuiltinNode;
124130
import com.oracle.graal.python.nodes.interop.PForeignToPTypeNode;
125131
import com.oracle.graal.python.nodes.object.GetClassNode;
@@ -154,6 +160,7 @@
154160
import com.oracle.truffle.api.interop.UnsupportedTypeException;
155161
import com.oracle.truffle.api.nodes.DirectCallNode;
156162
import com.oracle.truffle.api.nodes.Node;
163+
import com.oracle.truffle.api.nodes.NodeUtil;
157164
import com.oracle.truffle.api.nodes.NodeVisitor;
158165
import com.oracle.truffle.api.nodes.RootNode;
159166
import com.oracle.truffle.api.profiles.BranchProfile;
@@ -1768,46 +1775,144 @@ Object upcall(VirtualFrame frame, PythonModule cextModule, String name, Object[]
17681775
@Builtin(name = "make_may_raise_wrapper", minNumOfPositionalArgs = 1, maxNumOfPositionalArgs = 2)
17691776
@GenerateNodeFactory
17701777
abstract static class MakeMayRaiseWrapperNode extends PythonBuiltinNode {
1771-
static class MayRaiseWrapper extends RootNode {
1778+
static class MayRaiseNodeFactory<T extends PythonBuiltinBaseNode> implements NodeFactory<T> {
1779+
private final T node;
1780+
1781+
public MayRaiseNodeFactory(T node) {
1782+
this.node = node;
1783+
}
1784+
1785+
public T createNode(Object... arguments) {
1786+
return NodeUtil.cloneNode(node);
1787+
}
1788+
1789+
@SuppressWarnings("unchecked")
1790+
public Class<T> getNodeClass() {
1791+
return (Class<T>) node.getClass();
1792+
}
1793+
1794+
public List<List<Class<?>>> getNodeSignatures() {
1795+
return null;
1796+
}
1797+
1798+
public List<Class<? extends Node>> getExecutionSignature() {
1799+
return null;
1800+
}
1801+
}
1802+
1803+
@Builtin(fixedNumOfPositionalArgs = 1)
1804+
static abstract class MayRaiseUnaryNode extends PythonUnaryBuiltinNode {
1805+
@Child private CreateArgumentsNode createArgsNode;
1806+
@Child private InvokeNode invokeNode;
1807+
private final Object errorResult;
1808+
1809+
public MayRaiseUnaryNode(PFunction func, Object errorResult) {
1810+
this.createArgsNode = CreateArgumentsNode.create();
1811+
this.invokeNode = InvokeNode.create(func);
1812+
this.errorResult = errorResult;
1813+
}
1814+
1815+
@Specialization
1816+
Object doit(Object argument) {
1817+
try {
1818+
Object[] arguments = createArgsNode.execute(argument);
1819+
return invokeNode.execute(null, arguments, new PKeyword[0]);
1820+
} catch (PException e) {
1821+
getContext().setCurrentException(e);
1822+
return errorResult;
1823+
}
1824+
}
1825+
}
1826+
1827+
@Builtin(fixedNumOfPositionalArgs = 2)
1828+
static abstract class MayRaiseBinaryNode extends PythonBinaryBuiltinNode {
1829+
@Child private CreateArgumentsNode createArgsNode;
1830+
@Child private InvokeNode invokeNode;
1831+
private final Object errorResult;
1832+
1833+
public MayRaiseBinaryNode(PFunction func, Object errorResult) {
1834+
this.createArgsNode = CreateArgumentsNode.create();
1835+
this.invokeNode = InvokeNode.create(func);
1836+
this.errorResult = errorResult;
1837+
}
1838+
1839+
@Specialization
1840+
Object doit(Object arg1, Object arg2) {
1841+
try {
1842+
Object[] arguments = createArgsNode.execute(arg1, arg2);
1843+
return invokeNode.execute(null, arguments, new PKeyword[0]);
1844+
} catch (PException e) {
1845+
getContext().setCurrentException(e);
1846+
return errorResult;
1847+
}
1848+
}
1849+
}
1850+
1851+
@Builtin(fixedNumOfPositionalArgs = 3)
1852+
static abstract class MayRaiseTernaryNode extends PythonTernaryBuiltinNode {
1853+
@Child private CreateArgumentsNode createArgsNode;
1854+
@Child private InvokeNode invokeNode;
1855+
private final Object errorResult;
1856+
1857+
public MayRaiseTernaryNode(PFunction func, Object errorResult) {
1858+
this.createArgsNode = CreateArgumentsNode.create();
1859+
this.invokeNode = InvokeNode.create(func);
1860+
this.errorResult = errorResult;
1861+
}
1862+
1863+
@Specialization
1864+
Object doit(Object arg1, Object arg2, Object arg3) {
1865+
try {
1866+
Object[] arguments = createArgsNode.execute(arg1, arg2, arg3);
1867+
return invokeNode.execute(null, arguments, new PKeyword[0]);
1868+
} catch (PException e) {
1869+
getContext().setCurrentException(e);
1870+
return errorResult;
1871+
}
1872+
}
1873+
}
1874+
1875+
@Builtin(takesVarArgs = true)
1876+
static class MayRaiseNode extends PythonBuiltinNode {
17721877
@Child private InvokeNode invokeNode;
17731878
@Child private ReadVarArgsNode readVarargsNode;
17741879
@Child private CreateArgumentsNode createArgsNode;
17751880
@Child private PythonObjectFactory factory;
17761881
private final Object errorResult;
17771882

1778-
@TruffleBoundary
1779-
protected MayRaiseWrapper(PythonLanguage language, PythonObjectFactory factory, PythonCallable callable, Object errorResult) {
1780-
super(language);
1781-
this.factory = factory;
1883+
protected MayRaiseNode(PythonCallable callable, Object errorResult) {
17821884
this.readVarargsNode = ReadVarArgsNode.create(0, true);
17831885
this.createArgsNode = CreateArgumentsNode.create();
17841886
this.invokeNode = InvokeNode.create(callable);
17851887
this.errorResult = errorResult;
17861888
}
17871889

17881890
@Override
1789-
public boolean isCloningAllowed() {
1790-
return true;
1791-
}
1792-
1793-
@Override
1794-
public Object execute(VirtualFrame frame) {
1891+
public final Object execute(VirtualFrame frame) {
17951892
Object[] args = readVarargsNode.executeObjectArray(frame);
17961893
try {
17971894
Object[] arguments = createArgsNode.execute(args);
17981895
return invokeNode.execute(null, arguments, new PKeyword[0]);
17991896
} catch (PException e) {
1800-
PythonContext context = factory.getCore().getContext();
1801-
context.setCurrentException(e);
1897+
getContext().setCurrentException(e);
18021898
return errorResult;
18031899
}
18041900
}
1901+
1902+
@Override
1903+
protected ReadArgumentNode[] getArguments() {
1904+
throw new IllegalAccessError();
1905+
}
18051906
}
18061907

1908+
private static final Builtin unaryBuiltin = MayRaiseUnaryNode.class.getAnnotation(Builtin.class);
1909+
private static final Builtin binaryBuiltin = MayRaiseBinaryNode.class.getAnnotation(Builtin.class);
1910+
private static final Builtin ternaryBuiltin = MayRaiseTernaryNode.class.getAnnotation(Builtin.class);
1911+
private static final Builtin varargsBuiltin = MayRaiseNode.class.getAnnotation(Builtin.class);
1912+
18071913
@Specialization
18081914
Object make(PFunction func, Object errorResult) {
18091915
CompilerDirectives.transferToInterpreter();
1810-
FunctionRootNode functionRootNode = (FunctionRootNode) func.getFunctionRootNode();
18111916
func.getFunctionRootNode().accept(new NodeVisitor() {
18121917
public boolean visit(Node node) {
18131918
if (node instanceof PythonCallNode) {
@@ -1816,8 +1921,37 @@ public boolean visit(Node node) {
18161921
return true;
18171922
}
18181923
});
1819-
return factory().createBuiltinFunction(func.getName(), null, func.getArity(),
1820-
Truffle.getRuntime().createCallTarget(new MayRaiseWrapper(getRootNode().getLanguage(PythonLanguage.class), factory(), func, errorResult)));
1924+
1925+
RootNode rootNode = null;
1926+
Arity arity = func.getArity();
1927+
if (arity.takesFixedNumOfPositionalArgs()) {
1928+
switch (arity.getMinNumOfArgs()) {
1929+
case 1:
1930+
rootNode = new BuiltinFunctionRootNode(getRootNode().getLanguage(PythonLanguage.class), unaryBuiltin,
1931+
new MayRaiseNodeFactory<PythonUnaryBuiltinNode>(MayRaiseUnaryNodeGen.create(func, errorResult)),
1932+
true);
1933+
break;
1934+
case 2:
1935+
rootNode = new BuiltinFunctionRootNode(getRootNode().getLanguage(PythonLanguage.class), binaryBuiltin,
1936+
new MayRaiseNodeFactory<PythonBinaryBuiltinNode>(MayRaiseBinaryNodeGen.create(func, errorResult)),
1937+
true);
1938+
break;
1939+
case 3:
1940+
rootNode = new BuiltinFunctionRootNode(getRootNode().getLanguage(PythonLanguage.class), ternaryBuiltin,
1941+
new MayRaiseNodeFactory<PythonTernaryBuiltinNode>(MayRaiseTernaryNodeGen.create(func, errorResult)),
1942+
true);
1943+
break;
1944+
default:
1945+
break;
1946+
}
1947+
}
1948+
if (rootNode == null) {
1949+
rootNode = new BuiltinFunctionRootNode(getRootNode().getLanguage(PythonLanguage.class), varargsBuiltin,
1950+
new MayRaiseNodeFactory<PythonBuiltinNode>(new MayRaiseNode(func, errorResult)),
1951+
true);
1952+
}
1953+
1954+
return factory().createBuiltinFunction(func.getName(), null, arity, Truffle.getRuntime().createCallTarget(rootNode));
18211955
}
18221956
}
18231957

0 commit comments

Comments
 (0)