Skip to content

Commit ea3acaf

Browse files
committed
Use eager native tuple allocation in tp_init
1 parent 6b46965 commit ea3acaf

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

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

+53
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@
150150
import com.oracle.truffle.api.dsl.GenerateInline;
151151
import com.oracle.truffle.api.dsl.GenerateUncached;
152152
import com.oracle.truffle.api.dsl.ImportStatic;
153+
import com.oracle.truffle.api.dsl.InlineSupport.InlineTarget;
154+
import com.oracle.truffle.api.dsl.InlineSupport.RequiredField;
155+
import com.oracle.truffle.api.dsl.InlineSupport.StateField;
153156
import com.oracle.truffle.api.dsl.NeverDefault;
154157
import com.oracle.truffle.api.dsl.Specialization;
155158
import com.oracle.truffle.api.exception.AbstractTruffleException;
@@ -2141,6 +2144,52 @@ private ReadIndexedArgumentNode ensureReadArgNode() {
21412144
}
21422145
}
21432146

2147+
/**
2148+
* An inlined node-like object for keeping track of eager native allocation state bit. Should be
2149+
* {@code @Cached} and passed into
2150+
* {@link CreateArgsTupleNode#execute(Node, PythonLanguage, Object[], EagerTupleState)}. Then
2151+
* the {@link #report(Node, PTuple)} method should be called with the tuple after the native
2152+
* call returns.
2153+
*/
2154+
public static final class EagerTupleState {
2155+
private final StateField state;
2156+
2157+
private static final EagerTupleState UNCACHED = new EagerTupleState();
2158+
2159+
private EagerTupleState() {
2160+
this.state = null;
2161+
}
2162+
2163+
private EagerTupleState(InlineTarget target) {
2164+
this.state = target.getState(0, 1);
2165+
}
2166+
2167+
public boolean isEager(Node inliningTarget) {
2168+
if (state == null) {
2169+
return false;
2170+
}
2171+
return state.get(inliningTarget) != 0;
2172+
}
2173+
2174+
public void report(Node inliningTarget, PTuple tuple) {
2175+
if (state != null) {
2176+
if (!isEager(inliningTarget) && tuple.getSequenceStorage() instanceof NativeSequenceStorage) {
2177+
CompilerDirectives.transferToInterpreterAndInvalidate();
2178+
state.set(inliningTarget, 1);
2179+
}
2180+
}
2181+
}
2182+
2183+
public static EagerTupleState inline(
2184+
@RequiredField(value = StateField.class, bits = 1) InlineTarget target) {
2185+
return new EagerTupleState(target);
2186+
}
2187+
2188+
public static EagerTupleState getUncached() {
2189+
return UNCACHED;
2190+
}
2191+
}
2192+
21442193
/**
21452194
* We need to inflate all primitives in order to avoid memory leaks. Explanation: Primitives
21462195
* would currently be wrapped into a PrimitiveNativeWrapper. If any of those will receive a
@@ -2154,6 +2203,10 @@ private ReadIndexedArgumentNode ensureReadArgNode() {
21542203
public abstract static class CreateArgsTupleNode extends Node {
21552204
public abstract PTuple execute(PythonLanguage language, Object[] args, boolean eagerNative);
21562205

2206+
public final PTuple execute(Node inliningTarget, PythonLanguage language, Object[] args, EagerTupleState state) {
2207+
return execute(language, args, state.isEager(inliningTarget));
2208+
}
2209+
21572210
@Specialization(guards = {"args.length == cachedLen", "cachedLen <= 8", "!eagerNative"}, limit = "1")
21582211
@ExplodeLoop(kind = LoopExplosionKind.FULL_UNROLL)
21592212
static PTuple doCachedLen(PythonLanguage language, Object[] args, @SuppressWarnings("unused") boolean eagerNative,

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/builtins/objects/type/slots/TpSlotInit.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import com.oracle.graal.python.builtins.PythonBuiltins;
5151
import com.oracle.graal.python.builtins.objects.PNone;
5252
import com.oracle.graal.python.builtins.objects.cext.capi.ExternalFunctionNodes.CreateArgsTupleNode;
53+
import com.oracle.graal.python.builtins.objects.cext.capi.ExternalFunctionNodes.EagerTupleState;
5354
import com.oracle.graal.python.builtins.objects.cext.capi.ExternalFunctionNodes.ExternalFunctionInvokeNode;
5455
import com.oracle.graal.python.builtins.objects.cext.capi.ExternalFunctionNodes.InitCheckFunctionResultNode;
5556
import com.oracle.graal.python.builtins.objects.cext.capi.ExternalFunctionNodes.PExternalFunctionWrapper;
@@ -58,6 +59,7 @@
5859
import com.oracle.graal.python.builtins.objects.function.PBuiltinFunction;
5960
import com.oracle.graal.python.builtins.objects.function.PKeyword;
6061
import com.oracle.graal.python.builtins.objects.function.Signature;
62+
import com.oracle.graal.python.builtins.objects.tuple.PTuple;
6163
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot.TpSlotBuiltin;
6264
import com.oracle.graal.python.builtins.objects.type.slots.TpSlot.TpSlotPythonSingle;
6365
import com.oracle.graal.python.nodes.ErrorMessages;
@@ -263,16 +265,17 @@ static void callNative(VirtualFrame frame, Node inliningTarget, TpSlot.TpSlotCEx
263265
@Cached GetThreadStateNode getThreadStateNode,
264266
@Cached(inline = false) PythonToNativeNode toNativeNode,
265267
@Cached CreateArgsTupleNode createArgsTupleNode,
268+
@Cached EagerTupleState eagerTupleState,
266269
@Cached ExternalFunctionInvokeNode externalInvokeNode,
267270
@Cached InitCheckFunctionResultNode checkResult) {
268271
PythonLanguage language = context.getLanguage(inliningTarget);
269272
PythonThreadState state = getThreadStateNode.execute(inliningTarget, context);
270-
// TODO eager native tuple
271-
Object argsTuple = createArgsTupleNode.execute(language, args, false);
273+
PTuple argsTuple = createArgsTupleNode.execute(inliningTarget, language, args, eagerTupleState);
272274
Object kwargsDict = PFactory.createDict(language, keywords);
273275
Object nativeResult = externalInvokeNode.call(frame, inliningTarget, state, C_API_TIMING, T___INIT__, slot.callable,
274276
toNativeNode.execute(self), toNativeNode.execute(argsTuple), toNativeNode.execute(kwargsDict));
275277
checkResult.execute(state, T___INIT__, nativeResult);
278+
eagerTupleState.report(inliningTarget, argsTuple);
276279
}
277280

278281
@Specialization(replaces = "callCachedBuiltin")

0 commit comments

Comments
 (0)