Skip to content
Snippets Groups Projects
Commit a66a0c12 authored by Florian Angerer's avatar Florian Angerer
Browse files

Restructured caller frame handling and added special treatment for top

level calls.
parent 4a43016b
No related branches found
No related tags found
No related merge requests found
......@@ -55,6 +55,7 @@ import com.oracle.truffle.r.nodes.function.PromiseNode.VarArgNode;
import com.oracle.truffle.r.nodes.function.PromiseNode.VarArgsPromiseNode;
import com.oracle.truffle.r.nodes.function.RCallNode;
import com.oracle.truffle.r.runtime.ArgumentsSignature;
import com.oracle.truffle.r.runtime.CallerFrameClosure;
import com.oracle.truffle.r.runtime.HasSignature;
import com.oracle.truffle.r.runtime.RArguments;
import com.oracle.truffle.r.runtime.RCaller;
......@@ -164,7 +165,6 @@ public class FrameFunctions {
return current;
}
}
notifyRCallNodes(actualFrame, RArguments.getCall(frame));
}
return Utils.getStackFrame(access, actualFrame);
}
......@@ -172,21 +172,13 @@ public class FrameFunctions {
private static Frame getCallerFrame(Frame current) {
Object callerFrame = RArguments.getCallerFrame(current);
return callerFrame instanceof Frame ? (Frame) callerFrame : null;
}
@TruffleBoundary
private static void notifyRCallNodes(int actualFrame, RCaller caller) {
RCaller currentCaller = caller;
for (int i = 0; i < ITERATE_LEVELS; i++) {
if (currentCaller == null || currentCaller.getDepth() <= actualFrame) {
break;
}
if (currentCaller.isValidCaller() && !currentCaller.isPromise() && currentCaller.getSyntaxNode() instanceof RCallNode) {
((RCallNode) currentCaller.getSyntaxNode()).setNeedsCallerFrame();
}
currentCaller = currentCaller.getParent();
if (callerFrame instanceof CallerFrameClosure) {
CallerFrameClosure closure = (CallerFrameClosure) callerFrame;
closure.setNeedsCallerFrame();
return closure.getMaterializedCallerFrame();
}
assert callerFrame instanceof Frame;
return (Frame) callerFrame;
}
}
......
......@@ -16,6 +16,7 @@ import java.util.function.Supplier;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.frame.VirtualFrame;
import com.oracle.truffle.api.nodes.ExplodeLoop;
import com.oracle.truffle.api.nodes.NodeCost;
......@@ -32,6 +33,7 @@ import com.oracle.truffle.r.nodes.function.visibility.SetVisibilityNode;
import com.oracle.truffle.r.runtime.ArgumentsSignature;
import com.oracle.truffle.r.runtime.RArguments;
import com.oracle.truffle.r.runtime.RArguments.DispatchArgs;
import com.oracle.truffle.r.runtime.RArguments.S3Args;
import com.oracle.truffle.r.runtime.RCaller;
import com.oracle.truffle.r.runtime.RInternalError;
import com.oracle.truffle.r.runtime.RVisibility;
......@@ -263,8 +265,11 @@ public abstract class CallMatcherNode extends RBaseNode {
String genFunctionName = functionName == null ? function.getName() : functionName;
Supplier<RSyntaxElement> argsSupplier = RCallerHelper.createFromArguments(genFunctionName, preparePermutation, suppliedArguments, suppliedSignature);
RCaller caller = genFunctionName == null ? RCaller.createInvalid(frame, parent) : RCaller.create(frame, parent, argsSupplier);
MaterializedFrame callerFrame = dispatchArgs instanceof S3Args ? ((S3Args) dispatchArgs).callEnv : null;
try {
return call.execute(frame, cachedFunction, caller, reorderedArgs, matchedArgs.getSignature(), cachedFunction.getEnclosingFrame(), dispatchArgs);
return call.execute(frame, cachedFunction, caller, callerFrame, parent == null || parent.getDepth() == 0, reorderedArgs, matchedArgs.getSignature(),
cachedFunction.getEnclosingFrame(),
dispatchArgs);
} finally {
visibility.executeAfterCall(frame, caller);
}
......@@ -343,8 +348,9 @@ public abstract class CallMatcherNode extends RBaseNode {
RCaller caller = genFunctionName == null ? RCaller.createInvalid(frame, parent)
: RCaller.create(frame, RCallerHelper.createFromArguments(genFunctionName,
new RArgsValuesAndNames(reorderedArgs.getArguments(), ArgumentsSignature.empty(reorderedArgs.getLength()))));
MaterializedFrame callerFrame = (dispatchArgs instanceof S3Args) ? ((S3Args) dispatchArgs).callEnv : null;
try {
return call.execute(frame, function, caller, reorderedArgs.getArguments(), reorderedArgs.getSignature(), function.getEnclosingFrame(), dispatchArgs);
return call.execute(frame, function, caller, callerFrame, reorderedArgs.getArguments(), reorderedArgs.getSignature(), function.getEnclosingFrame(), dispatchArgs);
} finally {
visibility.executeAfterCall(frame, caller);
}
......
......@@ -22,7 +22,6 @@
*/
package com.oracle.truffle.r.nodes.function;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.CompilerDirectives.CompilationFinal;
import com.oracle.truffle.api.frame.Frame;
......@@ -30,9 +29,9 @@ import com.oracle.truffle.api.frame.FrameInstance.FrameAccess;
import com.oracle.truffle.api.frame.MaterializedFrame;
import com.oracle.truffle.api.nodes.NodeCost;
import com.oracle.truffle.api.profiles.BranchProfile;
import com.oracle.truffle.r.runtime.CallerFrameClosure;
import com.oracle.truffle.r.runtime.RArguments;
import com.oracle.truffle.r.runtime.RError;
import com.oracle.truffle.r.runtime.CallerFrameClosure;
import com.oracle.truffle.r.runtime.Utils;
import com.oracle.truffle.r.runtime.nodes.RBaseNode;
......@@ -48,7 +47,6 @@ public final class GetCallerFrameNode extends RBaseNode {
public MaterializedFrame execute(Frame frame) {
Object callerFrameObject = RArguments.getCallerFrame(frame);
MaterializedFrame mCallerFrame;
if (callerFrameObject instanceof CallerFrameClosure) {
if (!slowPathInitialized) {
CompilerDirectives.transferToInterpreterAndInvalidate();
......@@ -62,14 +60,14 @@ public final class GetCallerFrameNode extends RBaseNode {
// if interpreted, we will have a materialized frame in the closure
MaterializedFrame materializedCallerFrame = closure.getMaterializedCallerFrame();
if (materializedCallerFrame != null) {
CompilerAsserts.neverPartOfCompilation();
assert CompilerDirectives.inInterpreter() || materializedCallerFrame == null;
return materializedCallerFrame;
}
RError.performanceWarning("slow caller frame access");
// for now, get it on the very slow path
Frame callerFrame = Utils.getCallerFrame(frame, FrameAccess.MATERIALIZE);
if (callerFrame != null) {
mCallerFrame = callerFrame.materialize();
callerFrameObject = callerFrame.materialize();
}
}
if (callerFrameObject == null) {
......@@ -78,11 +76,9 @@ public final class GetCallerFrameNode extends RBaseNode {
// must be the top level case.
topLevelProfile.enter();
return frame.materialize();
} else {
mCallerFrame = (MaterializedFrame) callerFrameObject;
}
assert callerFrameObject instanceof MaterializedFrame;
return mCallerFrame;
return (MaterializedFrame) callerFrameObject;
}
}
......@@ -986,7 +986,8 @@ public abstract class RCallNode extends RCallBaseNode implements RSyntaxNode, RS
}
}
return call.execute(frame, function, originalCall.createCaller(frame, function), orderedArguments.getArguments(), orderedArguments.getSignature(), function.getEnclosingFrame(), s3Args);
return call.execute(frame, function, originalCall.createCaller(frame, function), orderedArguments.getArguments(), orderedArguments.getSignature(), function.getEnclosingFrame(),
s3Args);
}
}
......
/*
* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package com.oracle.truffle.r.nodes.function.call;
import com.oracle.truffle.api.Assumption;
......@@ -12,6 +34,7 @@ public abstract class CallRFunctionBaseNode extends Node {
protected final Assumption needsNoCallerFrame = Truffle.getRuntime().createAssumption("no caller frame");
protected final CallerFrameClosure invalidateNoCallerFrame = new InvalidateNoCallerFrame(needsNoCallerFrame);
private static final CallerFrameClosure DUMMY = new DummyCallerFrameClosure();
public boolean setNeedsCallerFrame() {
boolean value = !needsNoCallerFrame.isValid();
......@@ -19,17 +42,50 @@ public abstract class CallRFunctionBaseNode extends Node {
return value;
}
private Object getCallerFrameClosure(MaterializedFrame callerFrame) {
if (CompilerDirectives.inInterpreter()) {
return new InvalidateNoCallerFrame(needsNoCallerFrame, callerFrame);
}
return invalidateNoCallerFrame;
}
private Object getCallerFrameClosure(VirtualFrame callerFrame) {
if (CompilerDirectives.inInterpreter()) {
return new InvalidateNoCallerFrame(needsNoCallerFrame, callerFrame.materialize());
return new InvalidateNoCallerFrame(needsNoCallerFrame, callerFrame != null ? callerFrame.materialize() : null);
}
return invalidateNoCallerFrame;
}
protected final Object getCallerFrameObject(VirtualFrame curFrame, MaterializedFrame callerFrame, boolean topLevel) {
if (needsNoCallerFrame.isValid()) {
return getCallerFrameClosure(callerFrame);
} else {
if (callerFrame != null) {
return callerFrame;
} else if (topLevel) {
return DUMMY;
}
return curFrame.materialize();
}
}
protected final Object getCallerFrameObject(VirtualFrame callerFrame) {
return needsNoCallerFrame.isValid() ? getCallerFrameClosure(callerFrame) : callerFrame.materialize();
}
private static final class DummyCallerFrameClosure extends CallerFrameClosure {
@Override
public void setNeedsCallerFrame() {
}
@Override
public MaterializedFrame getMaterializedCallerFrame() {
return null;
}
}
public static final class InvalidateNoCallerFrame extends CallerFrameClosure {
private final Assumption needsNoCallerFrame;
......
......@@ -61,6 +61,12 @@ public abstract class CallRFunctionCachedNode extends CallRFunctionBaseNode {
return execute(frame, function.getTarget(), callArgs, call);
}
public final Object execute(VirtualFrame frame, RFunction function, RCaller call, MaterializedFrame callerFrame, Object[] evaluatedArgs,
ArgumentsSignature suppliedSignature, MaterializedFrame enclosingFrame, DispatchArgs dispatchArgs) {
Object[] callArgs = RArguments.create(function, call, getCallerFrameObject(frame, callerFrame, false), evaluatedArgs, suppliedSignature, enclosingFrame, dispatchArgs);
return execute(frame, function.getTarget(), callArgs, call);
}
protected abstract Object execute(VirtualFrame frame, CallTarget target, Object[] arguments, RCaller caller);
protected static DirectCallNode createDirectCallNode(CallTarget target) {
......
......@@ -50,9 +50,19 @@ public final class CallRFunctionNode extends CallRFunctionBaseNode {
return new CallRFunctionNode(callTarget);
}
public Object execute(VirtualFrame frame, RFunction function, RCaller caller, MaterializedFrame candidate, boolean topLevel, Object[] evaluatedArgs, ArgumentsSignature suppliedSignature,
MaterializedFrame enclosingFrame, DispatchArgs dispatchArgs) {
Object[] callArgs = RArguments.create(function, caller, getCallerFrameObject(frame, candidate, topLevel), evaluatedArgs, suppliedSignature, enclosingFrame, dispatchArgs);
try {
return callNode.call(callArgs);
} finally {
visibility.executeAfterCall(frame, caller);
}
}
public Object execute(VirtualFrame frame, RFunction function, RCaller caller, Object[] evaluatedArgs, ArgumentsSignature suppliedSignature,
MaterializedFrame enclosingFrame, DispatchArgs dispatchArgs) {
Object[] callArgs = RArguments.create(function, caller, getCallerFrameObject(frame), evaluatedArgs, suppliedSignature, enclosingFrame, dispatchArgs);
Object[] callArgs = RArguments.create(function, caller, getCallerFrameObject(frame, null, true), evaluatedArgs, suppliedSignature, enclosingFrame, dispatchArgs);
try {
return callNode.call(callArgs);
} finally {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment