Skip to content

Commit 427aa6d

Browse files
committed
Implement workaround for unadoptable 'MaterializeFrameNode'.
1 parent 6266719 commit 427aa6d

File tree

1 file changed

+52
-5
lines changed
  • graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime

1 file changed

+52
-5
lines changed

graalpython/com.oracle.graal.python/src/com/oracle/graal/python/runtime/AsyncHandler.java

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,16 @@
5151
import java.util.function.Supplier;
5252

5353
import com.oracle.graal.python.PythonLanguage;
54+
import com.oracle.graal.python.builtins.objects.frame.PFrame;
55+
import com.oracle.graal.python.builtins.objects.frame.PFrame.Reference;
5456
import com.oracle.graal.python.builtins.objects.function.PArguments;
57+
import com.oracle.graal.python.builtins.objects.function.Signature;
58+
import com.oracle.graal.python.nodes.PRootNode;
5559
import com.oracle.graal.python.nodes.call.CallNode;
5660
import com.oracle.graal.python.nodes.frame.MaterializeFrameNode;
5761
import com.oracle.graal.python.nodes.frame.MaterializeFrameNodeGen;
62+
import com.oracle.graal.python.nodes.frame.ReadCallerFrameNode;
63+
import com.oracle.graal.python.runtime.ExecutionContext.CalleeContext;
5864
import com.oracle.truffle.api.CompilerDirectives;
5965
import com.oracle.truffle.api.RootCallTarget;
6066
import com.oracle.truffle.api.Truffle;
@@ -63,6 +69,7 @@
6369
import com.oracle.truffle.api.nodes.Node;
6470
import com.oracle.truffle.api.nodes.Node.Child;
6571
import com.oracle.truffle.api.nodes.RootNode;
72+
import com.oracle.truffle.api.profiles.BranchProfile;
6673

6774
/**
6875
* A handler for asynchronous actions events that need to be handled on a main thread of execution,
@@ -129,28 +136,59 @@ public void run() {
129136
}
130137
}
131138

132-
private static class CallRootNode extends RootNode {
139+
private static class CallRootNode extends PRootNode {
133140
static final int ASYNC_ARGS = 4;
134141

135142
@Child private CallNode callNode = CallNode.create();
136143
@Child private MaterializeFrameNode materializeNode = MaterializeFrameNodeGen.create();
144+
@Child private ReadCallerFrameNode readCallerFrameNode = ReadCallerFrameNode.create();
145+
@Child private CalleeContext calleeContext = CalleeContext.create();
146+
147+
private final BranchProfile profile = BranchProfile.create();
137148

138149
protected CallRootNode(TruffleLanguage<?> language) {
139150
super(language);
140151
}
141152

142153
@Override
143154
public Object execute(VirtualFrame frame) {
155+
CalleeContext.enter(frame, profile);
144156
Object[] frameArguments = frame.getArguments();
145157
Object callable = PArguments.getArgument(frameArguments, 0);
146158
int frameIndex = (int) PArguments.getArgument(frameArguments, 1);
147-
VirtualFrame callerFrame = (VirtualFrame) PArguments.getArgument(frameArguments, 3);
148159
Object[] arguments = Arrays.copyOfRange(frameArguments, PArguments.USER_ARGUMENTS_OFFSET + ASYNC_ARGS, frameArguments.length);
160+
161+
// TODO: frames: This should eventually go away as soon as this root node is properly
162+
// called using an invoke node.
163+
PFrame.Reference callerInfo = PArguments.getCallerFrameInfo(frame);
164+
if (callerInfo != null) {
165+
VirtualFrame callerFrame = (VirtualFrame) PArguments.getArgument(frameArguments, 3);
166+
// the caller can't do, so it must be done here
167+
materializeNode.execute(frame, true, true, callerFrame);
168+
}
149169
if (frameIndex >= 0) {
150-
Node location = (Node) PArguments.getArgument(frameArguments, 2);
151-
arguments[frameIndex] = materializeNode.execute(frame, location, true, false, callerFrame);
170+
arguments[frameIndex] = readCallerFrameNode.executeWith(frame, 0);
152171
}
153-
return callNode.execute(callerFrame, callable, arguments);
172+
try {
173+
return callNode.execute(frame, callable, arguments);
174+
} finally {
175+
calleeContext.exit(frame, this);
176+
}
177+
}
178+
179+
@Override
180+
public Signature getSignature() {
181+
return Signature.EMPTY;
182+
}
183+
184+
@Override
185+
public boolean isPythonInternal() {
186+
return true;
187+
}
188+
189+
@Override
190+
public boolean isInternal() {
191+
return true;
154192
}
155193
}
156194

@@ -222,6 +260,15 @@ private void processAsyncActions(VirtualFrame frame, Node location) {
222260
PArguments.setArgument(args, 1, action.frameIndex());
223261
PArguments.setArgument(args, 2, location);
224262
PArguments.setArgument(args, 3, frame);
263+
264+
// TODO: frames: workaround because we can't use an invoke node here; this
265+
// should eventually be done properly
266+
RootNode rootNode = callTarget.getRootNode();
267+
if (rootNode instanceof PRootNode && ((PRootNode) rootNode).needsCallerFrame()) {
268+
Reference currentFrameInfo = PArguments.getCurrentFrameInfo(frame);
269+
currentFrameInfo.setCallNode(location);
270+
PArguments.setCallerFrameInfo(args, currentFrameInfo);
271+
}
225272
try {
226273
callTarget.call(args);
227274
} catch (RuntimeException e) {

0 commit comments

Comments
 (0)