Skip to content

Commit

Permalink
[GR-50773] Improve typing of GraphProvider and signatures.
Browse files Browse the repository at this point in the history
PullRequest: graal/16295
  • Loading branch information
Christian Wimmer committed Dec 8, 2023
2 parents f0e8373 + c4e157c commit f60f913
Show file tree
Hide file tree
Showing 58 changed files with 645 additions and 857 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ public FixedWithNextNode getBeforeUnwindNode() {
@SuppressWarnings("try")
protected void buildRootMethod() {
FrameStateBuilder startFrameState = new FrameStateBuilder(this, code, graph, graphBuilderConfig.retainLocalVariables());
startFrameState.initializeForMethodStart(graph.getAssumptions(), graphBuilderConfig.eagerResolving() || intrinsicContext != null, graphBuilderConfig.getPlugins());
startFrameState.initializeForMethodStart(graph.getAssumptions(), graphBuilderConfig.eagerResolving() || intrinsicContext != null, graphBuilderConfig.getPlugins(), null);

try (IntrinsicScope s = intrinsicContext != null ? new IntrinsicScope(this) : null) {
build(graph.start(), startFrameState);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ public void initializeFromArgumentsArray(ValueNode[] arguments) {
}
}

public void initializeForMethodStart(Assumptions assumptions, boolean eagerResolve, Plugins plugins) {
public void initializeForMethodStart(Assumptions assumptions, boolean eagerResolve, Plugins plugins, List<ValueNode> collectParameterNodes) {

int javaIndex = 0;
int index = 0;
Expand Down Expand Up @@ -236,7 +236,11 @@ public void initializeForMethodStart(Assumptions assumptions, boolean eagerResol
receiver = new ParameterNode(javaIndex, receiverStamp);
}

locals[javaIndex] = graph.addOrUniqueWithInputs(receiver);
receiver = graph.addOrUniqueWithInputs(receiver);
locals[javaIndex] = receiver;
if (collectParameterNodes != null) {
collectParameterNodes.add(receiver);
}
javaIndex = 1;
index = 1;
}
Expand Down Expand Up @@ -275,7 +279,11 @@ public void initializeForMethodStart(Assumptions assumptions, boolean eagerResol
param = new ParameterNode(index, stamp);
}

locals[javaIndex] = graph.addOrUniqueWithInputs(param);
param = graph.addOrUniqueWithInputs(param);
locals[javaIndex] = param;
if (collectParameterNodes != null) {
collectParameterNodes.add(param);
}
javaIndex++;
if (kind.needsTwoSlots()) {
locals[javaIndex] = TWO_SLOT_MARKER;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public class IntrinsicGraphBuilder extends CoreProvidersDelegate implements Grap

private FrameState createStateAfterStartOfReplacementGraph(ResolvedJavaMethod original, GraphBuilderConfiguration graphBuilderConfig) {
FrameStateBuilder startFrameState = new FrameStateBuilder(this, code, graph, graphBuilderConfig.retainLocalVariables());
startFrameState.initializeForMethodStart(graph.getAssumptions(), false, graphBuilderConfig.getPlugins());
startFrameState.initializeForMethodStart(graph.getAssumptions(), false, graphBuilderConfig.getPlugins(), null);
return startFrameState.createInitialIntrinsicFrameState(original);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaField;
import jdk.vm.ci.meta.Signature;

public abstract class PointsToAnalysis extends AbstractAnalysisEngine {
/** The type of {@link java.lang.Object}. */
Expand Down Expand Up @@ -321,10 +320,8 @@ public AnalysisMethod addRootMethod(Executable method, boolean invokeSpecial, Ob
public AnalysisMethod addRootMethod(AnalysisMethod aMethod, boolean invokeSpecial, Object reason, MultiMethod.MultiMethodKey... otherRoots) {
assert !universe.sealed() : "Cannot register root methods after analysis universe is sealed.";
AnalysisError.guarantee(aMethod.isOriginalMethod());
AnalysisType declaringClass = aMethod.getDeclaringClass();
boolean isStatic = aMethod.isStatic();
Signature signature = aMethod.getSignature();
int paramCount = signature.getParameterCount(!isStatic);
int paramCount = aMethod.getSignature().getParameterCount(!isStatic);
PointsToAnalysisMethod originalPTAMethod = assertPointsToAnalysisMethod(aMethod);

if (isStatic) {
Expand All @@ -339,7 +336,7 @@ public AnalysisMethod addRootMethod(AnalysisMethod aMethod, boolean invokeSpecia
pointsToMethod.registerAsImplementationInvoked(reason.toString());
MethodFlowsGraphInfo flowInfo = analysisPolicy.staticRootMethodGraph(this, pointsToMethod);
for (int idx = 0; idx < paramCount; idx++) {
AnalysisType declaredParamType = (AnalysisType) signature.getParameterType(idx, declaringClass);
AnalysisType declaredParamType = aMethod.getSignature().getParameterType(idx);
FormalParamTypeFlow parameter = flowInfo.getParameter(idx);
processParam(declaredParamType, parameter);
}
Expand Down Expand Up @@ -399,7 +396,7 @@ public AnalysisMethod addRootMethod(AnalysisMethod aMethod, boolean invokeSpecia
* type below we use idx-1 but when accessing the actual parameter flow we
* simply use idx.
*/
AnalysisType declaredParamType = (AnalysisType) signature.getParameterType(idx - 1, declaringClass);
AnalysisType declaredParamType = aMethod.getSignature().getParameterType(idx - 1);
TypeFlow<?> actualParameterFlow = invoke.getActualParameter(idx);
processParam(declaredParamType, actualParameterFlow);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,6 @@
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.JavaConstant;
import jdk.vm.ci.meta.JavaKind;
import jdk.vm.ci.meta.JavaType;
import jdk.vm.ci.meta.ResolvedJavaMethod;
import jdk.vm.ci.meta.VMConstant;

Expand Down Expand Up @@ -411,7 +410,7 @@ private static void registerForeignCall(PointsToAnalysis bb, ForeignCallsProvide
private boolean handleNodeIntrinsic() {
if (AnnotationAccess.isAnnotationPresent(method, NodeIntrinsic.class)) {
graph.getDebug().log("apply MethodTypeFlow on node intrinsic %s", method);
AnalysisType returnType = (AnalysisType) method.getSignature().getReturnType(method.getDeclaringClass());
AnalysisType returnType = method.getSignature().getReturnType();
if (bb.isSupportedJavaKind(returnType.getJavaKind())) {
/*
* This is a method used in a snippet, so most likely the return value does not
Expand All @@ -436,7 +435,7 @@ private boolean handleNodeIntrinsic() {
private void insertAllInstantiatedTypesReturn() {
AnalysisError.guarantee(flowsGraph.getReturnFlow() == null, "Expected null return flow");

AnalysisType returnType = TypeFlow.filterUncheckedInterface((AnalysisType) method.getSignature().getReturnType(method.getDeclaringClass()));
AnalysisType returnType = TypeFlow.filterUncheckedInterface(method.getSignature().getReturnType());
AnalysisError.guarantee(returnType.getJavaKind().isObject(), "Unexpected return type: %s", returnType);

BytecodePosition position = AbstractAnalysisEngine.syntheticSourcePosition(null, method);
Expand All @@ -451,15 +450,14 @@ private void insertAllInstantiatedTypesReturn() {
* Placeholder flows are placed in the graph for any missing flows.
*/
private void insertPlaceholderParamAndReturnFlows() {
boolean isStatic = Modifier.isStatic(method.getModifiers());
JavaType[] paramTypes = method.getSignature().toParameterTypes(isStatic ? null : method.getDeclaringClass());
var paramTypes = method.toParameterList();
BytecodePosition position = AbstractAnalysisEngine.syntheticSourcePosition(null, method);
for (int index = 0; index < paramTypes.length; index++) {
for (int index = 0; index < paramTypes.size(); index++) {
if (flowsGraph.getParameter(index) == null) {
if (bb.isSupportedJavaKind(paramTypes[index].getJavaKind())) {
AnalysisType paramType = (AnalysisType) paramTypes[index];
if (bb.isSupportedJavaKind(paramTypes.get(index).getJavaKind())) {
AnalysisType paramType = paramTypes.get(index);
FormalParamTypeFlow parameter;
if (!isStatic && index == 0) {
if (index == 0 && !method.isStatic()) {
assert paramType.equals(method.getDeclaringClass()) : paramType + ", " + method;
parameter = new FormalReceiverTypeFlow(position, paramType);
} else {
Expand All @@ -471,7 +469,7 @@ private void insertPlaceholderParamAndReturnFlows() {
}

if (flowsGraph.getReturnFlow() == null) {
AnalysisType returnType = (AnalysisType) method.getSignature().getReturnType(method.getDeclaringClass());
AnalysisType returnType = method.getSignature().getReturnType();
if (bb.isSupportedJavaKind(returnType.getJavaKind())) {
flowsGraph.setReturnFlow(new FormalReturnTypeFlow(position, returnType));
}
Expand Down Expand Up @@ -501,7 +499,7 @@ private void createTypeFlow() {
parameter = new FormalReceiverTypeFlow(AbstractAnalysisEngine.sourcePosition(node), paramType);
} else {
int offset = isStatic ? 0 : 1;
AnalysisType paramType = (AnalysisType) method.getSignature().getParameterType(index - offset, method.getDeclaringClass());
AnalysisType paramType = method.getSignature().getParameterType(index - offset);
parameter = new FormalParamTypeFlow(AbstractAnalysisEngine.sourcePosition(node), paramType, index);
}
flowsGraph.setParameter(index, parameter);
Expand Down Expand Up @@ -772,7 +770,7 @@ class NodeIterator extends PostOrderNodeIterator<TypeFlowsOfNodes> {
*/
private TypeFlowBuilder<?> uniqueReturnFlowBuilder(ReturnNode node) {
if (returnBuilder == null) {
AnalysisType returnType = (AnalysisType) method.getSignature().getReturnType(method.getDeclaringClass());
AnalysisType returnType = method.getSignature().getReturnType();
if (bb.isSupportedJavaKind(returnType.getJavaKind())) {
returnBuilder = TypeFlowBuilder.create(bb, node, FormalReturnTypeFlow.class, () -> {
FormalReturnTypeFlow returnFlow = flowsGraph.getReturnFlow();
Expand Down Expand Up @@ -1538,7 +1536,7 @@ protected void processMethodInvocation(TypeFlowsOfNodes state, ValueNode invoke,

if (!createDeoptInvokeTypeFlow && bb.isSupportedJavaKind(invoke.asNode().getStackKind())) {
/* Create the actual return builder. */
AnalysisType returnType = (AnalysisType) targetMethod.getSignature().getReturnType(null);
AnalysisType returnType = targetMethod.getSignature().getReturnType();
TypeFlowBuilder<?> actualReturnBuilder = TypeFlowBuilder.create(bb, invoke.asNode(), ActualReturnTypeFlow.class, () -> {
InvokeTypeFlow invokeFlow = invokeBuilder.get();
ActualReturnTypeFlow actualReturn = new ActualReturnTypeFlow(invokeFlow.source, returnType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,19 +24,19 @@
*/
package com.oracle.graal.pointsto.infrastructure;

import com.oracle.graal.pointsto.meta.AnalysisMethod;
import com.oracle.graal.pointsto.meta.HostedProviders;

import jdk.graal.compiler.debug.DebugContext;
import jdk.graal.compiler.nodes.StructuredGraph;
import jdk.vm.ci.meta.ResolvedJavaMethod;

public interface GraphProvider {
enum Purpose {
ANALYSIS,
PREPARE_RUNTIME_COMPILATION,
}

StructuredGraph buildGraph(DebugContext debug, ResolvedJavaMethod method, HostedProviders providers, Purpose purpose);
StructuredGraph buildGraph(DebugContext debug, AnalysisMethod method, HostedProviders providers, Purpose purpose);

/**
* Returns true if a graph can be provided for {@link Purpose#PREPARE_RUNTIME_COMPILATION}. Note
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ public int getParameterCount(boolean withReceiver) {
return parameterTypes.size() + (withReceiver ? 1 : 0);
}

/*
* Use the version without the accessingClass when calling methods directly on
* ResolvedSignature.
*/
@Deprecated
@Override
public T getParameterType(int index, ResolvedJavaType accessingClass) {
return getParameterType(index);
Expand All @@ -102,6 +107,11 @@ public T getParameterType(int index) {
return parameterTypes.get(index);
}

/*
* Use the version without the accessingClass when calling methods directly on
* ResolvedSignature.
*/
@Deprecated
@Override
public T getReturnType(ResolvedJavaType accessingClass) {
return getReturnType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import jdk.graal.compiler.nodes.graphbuilderconf.InvocationPlugin;
import jdk.vm.ci.code.Architecture;
import jdk.vm.ci.code.BytecodePosition;
import jdk.vm.ci.common.JVMCIError;
import jdk.vm.ci.meta.Constant;
import jdk.vm.ci.meta.ConstantPool;
import jdk.vm.ci.meta.ExceptionHandler;
Expand Down Expand Up @@ -221,7 +222,6 @@ protected AnalysisMethod(AnalysisUniverse universe, ResolvedJavaMethod wrapped,
if (PointstoOptions.TrackAccessChain.getValue(declaringClass.universe.hostVM().options())) {
startTrackInvocations();
}
registerSignatureTypes();
parsingContextMaxDepth = PointstoOptions.ParsingContextMaxDepth.getValue(declaringClass.universe.hostVM.options());
}

Expand Down Expand Up @@ -257,22 +257,6 @@ private static String createName(ResolvedJavaMethod wrapped, MultiMethodKey mult
return aName;
}

/**
* Lookup the parameters and return type so that they are added to the universe even if the
* method is never linked and parsed.
*/
private void registerSignatureTypes() {
boolean isStatic = Modifier.isStatic(getModifiers());
int parameterCount = getSignature().getParameterCount(!isStatic);

int offset = isStatic ? 0 : 1;
for (int i = offset; i < parameterCount; i++) {
getSignature().getParameterType(i - offset, getDeclaringClass());
}

getSignature().getReturnType(getDeclaringClass());
}

public String getQualifiedName() {
return qualifiedName;
}
Expand Down Expand Up @@ -630,22 +614,31 @@ public String getName() {
}

@Override
public jdk.vm.ci.meta.Signature getSignature() {
public ResolvedSignature<AnalysisType> getSignature() {
return signature;
}

@Override
public StructuredGraph buildGraph(DebugContext debug, ResolvedJavaMethod method, HostedProviders providers, Purpose purpose) {
if (wrapped instanceof GraphProvider) {
return ((GraphProvider) wrapped).buildGraph(debug, method, providers, purpose);
public JavaType[] toParameterTypes() {
throw JVMCIError.shouldNotReachHere("ResolvedJavaMethod.toParameterTypes returns the wrong result for constructors. Use toParameterList instead.");
}

public List<AnalysisType> toParameterList() {
return getSignature().toParameterList(isStatic() ? null : getDeclaringClass());
}

@Override
public StructuredGraph buildGraph(DebugContext debug, AnalysisMethod method, HostedProviders providers, Purpose purpose) {
if (wrapped instanceof GraphProvider graphProvider) {
return graphProvider.buildGraph(debug, method, providers, purpose);
}
return null;
}

@Override
public boolean allowRuntimeCompilation() {
if (wrapped instanceof GraphProvider) {
return ((GraphProvider) wrapped).allowRuntimeCompilation();
if (wrapped instanceof GraphProvider graphProvider) {
return graphProvider.allowRuntimeCompilation();
}
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@
import java.util.function.Consumer;
import java.util.function.Function;

import jdk.graal.compiler.debug.GraalError;
import org.graalvm.nativeimage.hosted.Feature.DuringAnalysisAccess;
import org.graalvm.word.WordBase;

Expand All @@ -64,6 +63,7 @@
import com.oracle.svm.util.LogUtils;
import com.oracle.svm.util.UnsafePartitionKind;

import jdk.graal.compiler.debug.GraalError;
import jdk.vm.ci.code.BytecodePosition;
import jdk.vm.ci.meta.Assumptions.AssumptionResult;
import jdk.vm.ci.meta.JavaConstant;
Expand Down Expand Up @@ -899,7 +899,7 @@ public final JavaKind getJavaKind() {
}

@Override
public final AnalysisType resolve(ResolvedJavaType accessingClass) {
public final ResolvedJavaType resolve(ResolvedJavaType accessingClass) {
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,10 @@ private static InvokeTypeFlow createContextInsensitiveInvoke(PointsToAnalysis bb

actualParameters[0] = receiverFlow;
for (int i = 1; i < actualParameters.length; i++) {
actualParameters[i] = new ActualParameterTypeFlow((AnalysisType) method.getSignature().getParameterType(i - 1, null));
actualParameters[i] = new ActualParameterTypeFlow(method.getSignature().getParameterType(i - 1));
}
ActualReturnTypeFlow actualReturn = null;
AnalysisType returnType = (AnalysisType) method.getSignature().getReturnType(null);
AnalysisType returnType = method.getSignature().getReturnType();
if (bb.isSupportedJavaKind(returnType.getStorageKind())) {
actualReturn = new ActualReturnTypeFlow(returnType);
}
Expand Down
Loading

0 comments on commit f60f913

Please sign in to comment.