Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@
import com.oracle.graal.python.builtins.objects.cext.copying.NativeLibraryToolException;
import com.oracle.graal.python.builtins.objects.cext.structs.CFields;
import com.oracle.graal.python.builtins.objects.cext.structs.CStructAccess;
import com.oracle.graal.python.builtins.objects.code.CodeNodes;
import com.oracle.graal.python.builtins.objects.code.PCode;
import com.oracle.graal.python.builtins.objects.common.DynamicObjectStorage;
import com.oracle.graal.python.builtins.objects.common.EconomicMapStorage;
Expand Down Expand Up @@ -147,7 +146,6 @@
import com.oracle.graal.python.nodes.PRaiseNode;
import com.oracle.graal.python.nodes.arrow.ArrowArray;
import com.oracle.graal.python.nodes.arrow.ArrowSchema;
import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetCallTargetNode;
import com.oracle.graal.python.nodes.bytecode.PBytecodeRootNode;
import com.oracle.graal.python.nodes.bytecode_dsl.PBytecodeDSLRootNode;
import com.oracle.graal.python.nodes.call.CallNode;
Expand Down Expand Up @@ -614,37 +612,27 @@ PBytes doString(VirtualFrame frame, Object filenameObj,
public abstract static class DumpTruffleAstNode extends PythonUnaryBuiltinNode {
@Specialization
@TruffleBoundary
public TruffleString doIt(PFunction func) {
return toTruffleStringUncached(NodeUtil.printTreeToString(GetCallTargetNode.getUncached().execute(func).getRootNode()));
}

@Specialization(guards = "isFunction(method.getFunction())")
@TruffleBoundary
public TruffleString doIt(PMethod method) {
return toTruffleStringUncached(NodeUtil.printTreeToString(GetCallTargetNode.getUncached().execute(method).getRootNode()));
}

@Specialization
@TruffleBoundary
public TruffleString doIt(PGenerator gen) {
return toTruffleStringUncached(NodeUtil.printTreeToString(gen.getCurrentCallTarget().getRootNode()));
}

@Specialization
@TruffleBoundary
public TruffleString doIt(PCode code) {
return toTruffleStringUncached(NodeUtil.printTreeToString(CodeNodes.GetCodeRootNode.executeUncached(code)));
}

@Fallback
@TruffleBoundary
public TruffleString doit(Object object) {
public TruffleString doIt(Object object) {
if (object instanceof PFunction func) {
return toTruffleStringUncached(NodeUtil.printTreeToString(func.getCode().getRootNode()));
}
if (object instanceof PBuiltinFunction func) {
return toTruffleStringUncached(NodeUtil.printTreeToString(func.getFunctionRootNode()));
}
if (object instanceof PMethod method) {
return doIt(method.getFunction());
}
if (object instanceof PBuiltinMethod method) {
return doIt(method.getBuiltinFunction());
}
if (object instanceof PGenerator gen) {
return toTruffleStringUncached(NodeUtil.printTreeToString(gen.getCurrentCallTarget().getRootNode()));
}
if (object instanceof PCode code) {
return toTruffleStringUncached(NodeUtil.printTreeToString(code.getRootNode()));
}
return toTruffleStringUncached("truffle ast dump not supported for " + object.toString());
}

protected static boolean isFunction(Object callee) {
return callee instanceof PFunction;
}
}

@Builtin(name = "current_import", minNumOfPositionalArgs = 0)
Expand Down Expand Up @@ -829,7 +817,7 @@ public Object doIt(VirtualFrame frame, PFunction func,

@TruffleBoundary
public synchronized PFunction convertToBuiltin(PFunction func) {
RootNode rootNode = CodeNodes.GetCodeRootNode.executeUncached(func.getCode());
RootNode rootNode = func.getCode().getRootNode();
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) {
if (rootNode instanceof PBytecodeDSLRootNode r) {
r.setPythonInternal(true);
Expand All @@ -846,10 +834,8 @@ public synchronized PFunction convertToBuiltin(PFunction func) {
@GenerateNodeFactory
public abstract static class BuiltinMethodNode extends PythonUnaryBuiltinNode {
@Specialization
public Object doIt(PFunction func,
@Bind Node inliningTarget,
@Cached CodeNodes.GetCodeRootNode getRootNode) {
RootNode rootNode = getRootNode.execute(inliningTarget, func.getCode());
public Object doIt(PFunction func) {
RootNode rootNode = func.getCode().getRootNode();
if (PythonOptions.ENABLE_BYTECODE_DSL_INTERPRETER) {
if (rootNode instanceof PBytecodeDSLRootNode r) {
r.setPythonInternal(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@

import com.oracle.graal.python.PythonLanguage;
import com.oracle.graal.python.builtins.modules.MarshalModuleBuiltins;
import com.oracle.graal.python.builtins.objects.code.CodeNodesFactory.GetCodeRootNodeGen;
import com.oracle.graal.python.builtins.objects.function.PFunction;
import com.oracle.graal.python.builtins.objects.function.Signature;
import com.oracle.graal.python.compiler.BytecodeCodeUnit;
Expand Down Expand Up @@ -271,22 +270,4 @@ static Signature doCode(PCode code) {
return code.getSignature();
}
}

@GenerateUncached
@GenerateInline
@GenerateCached(false)
public abstract static class GetCodeRootNode extends Node {

public abstract RootNode execute(Node inliningTarget, PCode code);

public static RootNode executeUncached(PCode code) {
return GetCodeRootNodeGen.getUncached().execute(null, code);
}

@Specialization
static RootNode doIt(Node inliningTarget, PCode code,
@Cached GetCodeCallTargetNode getCodeCallTargetNode) {
return getCodeCallTargetNode.execute(inliningTarget, code).getRootNode();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,7 @@ private static CodeUnit getCodeUnit(RootNode node) {
return null;
}

RootNode getRootNode() {
public RootNode getRootNode() {
return rootNode;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@

import com.oracle.graal.python.PythonLanguage;
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
import com.oracle.graal.python.builtins.objects.code.CodeNodes.GetCodeRootNode;
import com.oracle.graal.python.builtins.objects.code.PCode;
import com.oracle.graal.python.builtins.objects.function.PArguments;
import com.oracle.graal.python.builtins.objects.function.PFunction;
Expand Down Expand Up @@ -227,7 +226,7 @@ public PFrame(PythonLanguage lang, @SuppressWarnings("unused") long threadState,
// TODO: frames: extract the information from the threadState object
this.globals = globals;
this.code = code;
this.location = GetCodeRootNode.executeUncached(code);
this.location = code.getRootNode();
Reference curFrameInfo = new Reference(location != null ? location.getRootNode() : null, null);
this.virtualFrameInfo = curFrameInfo;
curFrameInfo.setPyFrame(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
import com.oracle.graal.python.runtime.object.PFactory;
import com.oracle.graal.python.util.PythonUtils;
import com.oracle.truffle.api.CompilerAsserts;
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.RootCallTarget;
Expand Down Expand Up @@ -81,6 +82,8 @@ public final class PBuiltinFunction extends PythonBuiltinObject implements Bound
@CompilationFinal(dimensions = 1) private final Object[] defaults;
@CompilationFinal(dimensions = 1) private final PKeyword[] kwDefaults;

@CompilationFinal private RootCallTarget callTarget;

public PBuiltinFunction(PythonBuiltinClassType cls, Shape shape, TruffleString name, Object enclosingType, Object[] defaults, PKeyword[] kwDefaults, int flags, RootNode functionRootNode,
Signature signature,
TpSlot slot, PExternalFunctionWrapper slotWrapper) {
Expand Down Expand Up @@ -196,7 +199,24 @@ public Signature getSignature() {
}

public RootCallTarget getCallTarget() {
return functionRootNode.getCallTarget();
RootCallTarget ct = callTarget;
if (CompilerDirectives.injectBranchProbability(CompilerDirectives.SLOWPATH_PROBABILITY, ct == null)) {
if (CompilerDirectives.inCompiledCode() && CompilerDirectives.isPartialEvaluationConstant(this)) {
CompilerDirectives.transferToInterpreterAndInvalidate();
}
ct = initializeCallTarget();
}
return ct;
}

@TruffleBoundary
private RootCallTarget initializeCallTarget() {
RootCallTarget ct = callTarget;
if (ct == null) {
ct = functionRootNode.getCallTarget();
callTarget = ct;
}
return ct;
}

public boolean declaresExplicitSelf() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,11 @@
import com.oracle.graal.python.builtins.PythonBuiltinClassType;
import com.oracle.graal.python.builtins.objects.PNone;
import com.oracle.graal.python.builtins.objects.cell.PCell;
import com.oracle.graal.python.builtins.objects.code.CodeNodes.GetCodeCallTargetNode;
import com.oracle.graal.python.builtins.objects.code.PCode;
import com.oracle.graal.python.builtins.objects.object.PythonObject;
import com.oracle.graal.python.compiler.CodeUnit;
import com.oracle.graal.python.lib.PyUnicodeCheckNode;
import com.oracle.graal.python.nodes.PRootNode;
import com.oracle.graal.python.nodes.builtins.FunctionNodes.GetCallTargetNode;
import com.oracle.graal.python.runtime.GilNode;
import com.oracle.graal.python.util.PythonUtils;
import com.oracle.truffle.api.Assumption;
Expand All @@ -45,14 +43,12 @@
import com.oracle.truffle.api.CompilerDirectives.TruffleBoundary;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.Truffle;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.interop.InteropLibrary;
import com.oracle.truffle.api.interop.UnsupportedMessageException;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import com.oracle.truffle.api.nodes.Node;
import com.oracle.truffle.api.nodes.RootNode;
import com.oracle.truffle.api.source.SourceSection;
import com.oracle.truffle.api.strings.TruffleString;
Expand Down Expand Up @@ -221,7 +217,7 @@ public void setKwDefaults(PKeyword[] defaults) {

@TruffleBoundary
String getSourceCode() {
RootNode rootNode = GetCallTargetNode.getUncached().execute(this).getRootNode();
RootNode rootNode = getCode().getRootNode();
SourceSection sourceSection = rootNode.getSourceSection();
if (sourceSection != null) {
return sourceSection.getCharacters().toString();
Expand All @@ -248,12 +244,10 @@ String getExecutableName(@Shared("gil") @Cached GilNode gil,

@ExportMessage
public SourceSection getSourceLocation(
@Bind Node inliningTarget,
@Shared("getCt") @Cached GetCodeCallTargetNode getCt,
@Shared("gil") @Cached GilNode gil) throws UnsupportedMessageException {
boolean mustRelease = gil.acquire();
try {
SourceSection result = getSourceLocationDirect(inliningTarget, getCt);
SourceSection result = getSourceLocationDirect();
if (result == null) {
throw UnsupportedMessageException.create();
} else {
Expand All @@ -265,8 +259,8 @@ public SourceSection getSourceLocation(
}

@TruffleBoundary
private SourceSection getSourceLocationDirect(Node inliningTarget, GetCodeCallTargetNode getCt) {
RootNode rootNode = getCt.execute(inliningTarget, code).getRootNode();
private SourceSection getSourceLocationDirect() {
RootNode rootNode = code.getRootNode();
SourceSection result;
if (rootNode instanceof PRootNode) {
result = ((PRootNode) rootNode).getSourceSection();
Expand All @@ -283,12 +277,10 @@ private static SourceSection getForeignSourceSection(RootNode rootNode) {

@ExportMessage
public boolean hasSourceLocation(
@Bind Node inliningTarget,
@Shared("getCt") @Cached GetCodeCallTargetNode getCt,
@Shared("gil") @Cached GilNode gil) {
boolean mustRelease = gil.acquire();
try {
return getSourceLocationDirect(inliningTarget, getCt) != null;
return getSourceLocationDirect() != null;
} finally {
gil.release(mustRelease);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2025, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2026, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* The Universal Permissive License (UPL), Version 1.0
Expand Down Expand Up @@ -51,15 +51,12 @@
import com.oracle.graal.python.builtins.objects.method.PMethodBase;
import com.oracle.graal.python.nodes.PGuards;
import com.oracle.graal.python.nodes.PNodeWithContext;
import com.oracle.graal.python.nodes.builtins.FunctionNodesFactory.GetCallTargetNodeGen;
import com.oracle.graal.python.nodes.builtins.FunctionNodesFactory.GetSignatureNodeGen;
import com.oracle.truffle.api.CompilerAsserts;
import com.oracle.truffle.api.CompilerDirectives;
import com.oracle.truffle.api.RootCallTarget;
import com.oracle.truffle.api.dsl.Bind;
import com.oracle.truffle.api.dsl.Cached;
import com.oracle.truffle.api.dsl.Cached.Shared;
import com.oracle.truffle.api.dsl.Fallback;
import com.oracle.truffle.api.dsl.GenerateCached;
import com.oracle.truffle.api.dsl.GenerateInline;
import com.oracle.truffle.api.dsl.GenerateUncached;
Expand Down Expand Up @@ -278,54 +275,4 @@ public static GetSignatureNode getUncached() {
return GetSignatureNodeGen.getUncached();
}
}

@ImportStatic(PGuards.class)
@GenerateUncached
@GenerateInline(false) // used lazily
public abstract static class GetCallTargetNode extends PNodeWithContext {

public abstract RootCallTarget execute(Object function);

@Specialization
static RootCallTarget doFunction(PFunction function,
@Bind Node inliningTarget,
@Shared("getCode") @Cached GetFunctionCodeNode getFunctionCodeNode,
@Shared("getCt") @Cached CodeNodes.GetCodeCallTargetNode getCt) {
return getCt.execute(inliningTarget, getFunctionCodeNode.execute(inliningTarget, function));
}

@Specialization
static RootCallTarget doBuiltinFunction(PBuiltinFunction builtinFunction) {
return builtinFunction.getCallTarget();
}

@Specialization(guards = "isPFunction(function)")
static RootCallTarget doMethod(@SuppressWarnings("unused") PMethod method,
@Bind Node inliningTarget,
@Bind("method.getFunction()") Object function,
@Shared("getCode") @Cached GetFunctionCodeNode getFunctionCodeNode,
@Shared("getCt") @Cached CodeNodes.GetCodeCallTargetNode getCt) {
return getCt.execute(inliningTarget, getFunctionCodeNode.execute(inliningTarget, (PFunction) function));
}

@Specialization(guards = "isPBuiltinFunction(method.getFunction())")
static RootCallTarget doMethod(@SuppressWarnings("unused") PMethod method,
@Bind("method.getFunction()") Object function) {
return ((PBuiltinFunction) function).getCallTarget();
}

@Specialization
static RootCallTarget doBuiltinMethod(PBuiltinMethod builtinMethod) {
return builtinMethod.getBuiltinFunction().getCallTarget();
}

@Fallback
static RootCallTarget fallback(@SuppressWarnings("unused") Object callable) {
return null;
}

public static GetCallTargetNode getUncached() {
return GetCallTargetNodeGen.getUncached();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1778,7 +1778,7 @@ protected static boolean isSingleContext(Node node) {

@NeverDefault
static Assumption getCodeStableAssumption(PCode code) {
PBytecodeDSLRootNode rootNode = (PBytecodeDSLRootNode) code.getRootCallTarget().getRootNode();
PBytecodeDSLRootNode rootNode = (PBytecodeDSLRootNode) code.getRootNode();
return rootNode.getFunctionCodeFinalAssumption();
}

Expand Down
Loading
Loading