Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GR-60866] Use plugin class name to filter fold plugin application #10447

Merged
merged 1 commit into from
Jan 17, 2025
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 @@ -28,6 +28,7 @@
import static jdk.graal.compiler.replacements.processor.FoldHandler.INJECTED_PARAMETER_CLASS_NAME;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -66,138 +67,55 @@ public void extraImports(AbstractProcessor processor, Set<String> imports) {
imports.add("jdk.vm.ci.meta.JavaConstant");
imports.add("jdk.vm.ci.meta.JavaKind");
imports.add("jdk.graal.compiler.nodes.ConstantNode");
imports.add("jdk.graal.compiler.core.common.type.Stamp");
}

@Override
protected void createExecute(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
List<? extends VariableElement> params = intrinsicMethod.getParameters();

out.printf(" if (b.shouldDeferPlugin(this)) {\n");
out.printf(" b.replacePlugin%s(this, targetMethod, args, %s.FUNCTION);\n", getReplacementFunctionSuffix(processor), getReplacementName());
out.printf(" return true;\n");
out.printf(" }\n");

int argCount = 0;
Object receiver;
if (intrinsicMethod.getModifiers().contains(Modifier.STATIC)) {
receiver = intrinsicMethod.getEnclosingElement();
} else {
receiver = "arg0";
TypeElement type = (TypeElement) intrinsicMethod.getEnclosingElement();
constantArgument(processor, out, deps, argCount, type.asType(), argCount, false);
argCount++;
}

int firstArg = argCount;
for (VariableElement param : params) {
if (processor.getAnnotation(param, processor.getType(INJECTED_PARAMETER_CLASS_NAME)) == null) {
constantArgument(processor, out, deps, argCount, param.asType(), argCount, false);
} else {
int argCount = intrinsicMethod.getModifiers().contains(Modifier.STATIC) ? 0 : 1;
for (VariableElement param : intrinsicMethod.getParameters()) {
if (processor.getAnnotation(param, processor.getType(INJECTED_PARAMETER_CLASS_NAME)) != null) {
out.printf(" if (!checkInjectedArgument(b, args[%d], targetMethod)) {\n", argCount);
out.printf(" return false;\n");
out.printf(" }\n");
out.printf(" %s arg%d = %s;\n", param.asType(), argCount, deps.use(processor, (DeclaredType) param.asType()));
}
argCount++;
}

Set<String> suppressWarnings = new TreeSet<>();
if (intrinsicMethod.getAnnotation(Deprecated.class) != null) {
suppressWarnings.add("deprecation");
}
if (hasRawtypeWarning(intrinsicMethod.getReturnType())) {
suppressWarnings.add("rawtypes");
}
for (VariableElement param : params) {
if (hasUncheckedWarning(param.asType())) {
suppressWarnings.add("unchecked");
}
}
if (suppressWarnings.size() > 0) {
out.printf(" @SuppressWarnings({");
String sep = "";
for (String suppressWarning : suppressWarnings) {
out.printf("%s\"%s\"", sep, suppressWarning);
sep = ", ";
}
out.printf("})\n");
}
// Exercise the emission (but swallow generated output) to populate the deps
emitReplace(processor, new PrintWriter(new StringWriter()), deps);

out.printf(" %s result = %s.%s(", getErasedType(intrinsicMethod.getReturnType()), receiver, intrinsicMethod.getSimpleName());
if (argCount > firstArg) {
out.printf("arg%d", firstArg);
for (int i = firstArg + 1; i < argCount; i++) {
out.printf(", arg%d", i);
}
// Build the list of extra arguments to be passed
StringBuilder extraArguments = new StringBuilder();
for (InjectedDependencies.Dependency dep : deps) {
extraArguments.append(", ").append(dep.getName(processor, intrinsicMethod));
}
out.printf(");\n");

TypeMirror returnType = intrinsicMethod.getReturnType();
switch (returnType.getKind()) {
case BOOLEAN:
out.printf(" JavaConstant constant = JavaConstant.forInt(result ? 1 : 0);\n");
break;
case BYTE:
case SHORT:
case CHAR:
case INT:
out.printf(" JavaConstant constant = JavaConstant.forInt(result);\n");
break;
case LONG:
out.printf(" JavaConstant constant = JavaConstant.forLong(result);\n");
break;
case FLOAT:
out.printf(" JavaConstant constant = JavaConstant.forFloat(result);\n");
break;
case DOUBLE:
out.printf(" JavaConstant constant = JavaConstant.forDouble(result);\n");
break;
case ARRAY:
case TYPEVAR:
case DECLARED:
if (returnType.equals(processor.getType("java.lang.String"))) {
out.printf(" JavaConstant constant = %s.forString(result);\n", deps.use(processor, WellKnownDependency.CONSTANT_REFLECTION));
} else {
out.printf(" JavaConstant constant = %s.forObject(result);\n", deps.use(processor, WellKnownDependency.SNIPPET_REFLECTION));
}
break;
default:
throw new IllegalArgumentException(returnType.toString());
}

out.printf(" ConstantNode node = ConstantNode.forConstant(constant, %s, %s);\n", deps.use(processor, WellKnownDependency.META_ACCESS),
deps.use(processor, WellKnownDependency.STRUCTURED_GRAPH));
out.printf(" b.push(JavaKind.%s, node);\n", getReturnKind(intrinsicMethod));
out.printf(" return true;\n");
out.printf(" return doExecute(b, args%s);\n", extraArguments);
}

@Override
protected void createHelpers(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
out.printf("\n");
out.printf(" @Override\n");
out.printf(" public boolean replace(GraphBuilderContext b, Replacements injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");

private void emitReplace(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
List<? extends VariableElement> params = intrinsicMethod.getParameters();

int argCount = 0;
final int firstArg = intrinsicMethod.getModifiers().contains(Modifier.STATIC) ? 0 : 1;
Object receiver;
if (intrinsicMethod.getModifiers().contains(Modifier.STATIC)) {
if (firstArg == 0) {
receiver = intrinsicMethod.getEnclosingElement();
} else {
receiver = "arg0";
TypeElement type = (TypeElement) intrinsicMethod.getEnclosingElement();
constantArgument(processor, out, deps, argCount, type.asType(), argCount, true);
argCount++;
constantArgument(processor, out, deps, 0, type.asType(), 0, false);
}

int firstArg = argCount;
int argCount = firstArg;
for (VariableElement param : params) {
if (processor.getAnnotation(param, processor.getType(INJECTED_PARAMETER_CLASS_NAME)) == null) {
constantArgument(processor, out, deps, argCount, param.asType(), argCount, true);
constantArgument(processor, out, deps, argCount, param.asType(), argCount, false);
} else {
out.printf(" assert args.get(%d).isNullConstant() : \"Must be null constant \" + args.get(%d);\n", argCount, argCount);
out.printf(" %s arg%d = %s;\n", param.asType(), argCount, deps.find(processor, (DeclaredType) param.asType()).getExpression(processor, intrinsicMethod));
out.printf(" assert args[%d].isNullConstant() : \"Must be null constant \" + args[%d];\n", argCount, argCount);
out.printf(" %s arg%d = %s;\n", param.asType(), argCount, deps.use(processor, (DeclaredType) param.asType()));
}
argCount++;
}
Expand Down Expand Up @@ -270,6 +188,43 @@ protected void createHelpers(AbstractProcessor processor, PrintWriter out, Injec
deps.use(processor, WellKnownDependency.STRUCTURED_GRAPH));
out.printf(" b.push(JavaKind.%s, node);\n", getReturnKind(intrinsicMethod));
out.printf(" return true;\n");
}

@Override
protected void createPrivateMembersAndConstructor(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps, String constructorName) {
// Add declarations for the extra arguments
StringBuilder extraArguments = new StringBuilder();
for (InjectedDependencies.Dependency dep : deps) {
extraArguments.append(", ").append(dep.getType()).append(" ").append(dep.getName(processor, intrinsicMethod));
}
out.printf("\n");
out.printf(" @SuppressWarnings(\"unused\")\n");
out.printf(" static boolean doExecute(GraphBuilderContext b, ValueNode[] args%s) {\n", extraArguments);
emitReplace(processor, out, deps);
out.printf(" }\n");

// This must be done after the code emission above to ensure that deps includes all required
// dependencies.
super.createPrivateMembersAndConstructor(processor, out, deps, constructorName);
}

@Override
protected void createHelpers(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
out.printf("\n");
out.printf(" @Override\n");
out.printf(" public boolean replace(GraphBuilderContext b, GeneratedPluginInjectionProvider injection, ValueNode[] args) {\n");

// Create local declarations for all the injected arguments
for (InjectedDependencies.Dependency dep : deps) {
out.printf(" %s %s = %s;\n", dep.getType(), dep.getName(processor, intrinsicMethod), dep.getExpression(processor, intrinsicMethod));
}

// Build the list of extra arguments to be passed
StringBuilder extraArguments = new StringBuilder();
for (InjectedDependencies.Dependency dep : deps) {
extraArguments.append(", ").append(dep.getName(processor, intrinsicMethod));
}
out.printf(" return %s.doExecute(b, args%s);\n", getPluginName(), extraArguments);
out.printf(" }\n");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,6 @@ protected String pluginSuperclass() {
@Override
public void extraImports(AbstractProcessor processor, Set<String> imports) {
imports.add("jdk.graal.compiler.nodes.graphbuilderconf.GraphBuilderContext");
if (needsReplacement(processor)) {
imports.add("jdk.graal.compiler.core.common.type.Stamp");
}
}

protected abstract List<? extends VariableElement> getParameters();
Expand All @@ -87,7 +84,7 @@ protected void createExecute(AbstractProcessor processor, PrintWriter out, Injec

for (int i = 0; i < signature.length; i++, idx++) {
if (processor.getAnnotation(intrinsicMethod.getParameters().get(i), processor.getType(NodeIntrinsicHandler.CONSTANT_NODE_PARAMETER_CLASS_NAME)) != null) {
String argName = constantArgument(processor, out, deps, idx, signature[i], i, false);
String argName = constantArgument(processor, out, deps, idx, signature[i], i, true);
verifyConstantArgument(out, argName, signature[i]);
} else {
if (signature[i].equals(processor.getType(NodeIntrinsicHandler.VALUE_NODE_CLASS_NAME))) {
Expand Down Expand Up @@ -199,7 +196,7 @@ protected void verifyConstantArgument(PrintWriter out, String argName, TypeMirro
}

@Override
protected void createOtherClasses(AbstractProcessor processor, PrintWriter out) {
protected void createOtherClasses(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
if (needsReplacement(processor)) {
if (isWithExceptionReplacement(processor)) {
/*
Expand All @@ -212,11 +209,10 @@ protected void createOtherClasses(AbstractProcessor processor, PrintWriter out)
out.printf("@ExcludeFromJacocoGeneratedReport(\"deferred plugin support that is only called in libgraal\")\n");
out.printf("final class %s implements PluginReplacementWithExceptionNode.ReplacementWithExceptionFunction {\n", name);
out.printf(" static PluginReplacementWithExceptionNode.ReplacementWithExceptionFunction FUNCTION = new %s();\n", name);
InjectedDependencies deps = new InjectedDependencies(false, intrinsicMethod);
createHelpers(processor, out, deps);
out.printf("}\n");
} else {
super.createOtherClasses(processor, out);
super.createOtherClasses(processor, out, deps);
}
}
}
Expand All @@ -241,13 +237,15 @@ protected boolean needsReplacement(AbstractProcessor processor) {
}

@Override
protected void createHelpers(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
protected void createHelpers(AbstractProcessor processor, PrintWriter out, InjectedDependencies originalDeps) {
if (!needsReplacement(processor)) {
return;
}
// In this context all values must be retrieved from the injection argument
InjectedDependencies deps = new InjectedDependencies(false, intrinsicMethod);
out.printf("\n");
out.printf(" @Override\n");
out.printf(" public boolean replace(GraphBuilderContext b, Replacements injection, Stamp stamp, NodeInputList<ValueNode> args) {\n");
out.printf(" public boolean replace(GraphBuilderContext b, GeneratedPluginInjectionProvider injection, ValueNode[] args) {\n");

List<? extends VariableElement> params = getParameters();

Expand All @@ -263,12 +261,12 @@ protected void createHelpers(AbstractProcessor processor, PrintWriter out, Injec

for (int i = 0; i < signature.length; i++, idx++) {
if (processor.getAnnotation(intrinsicMethod.getParameters().get(i), processor.getType(NodeIntrinsicHandler.CONSTANT_NODE_PARAMETER_CLASS_NAME)) != null) {
constantArgument(processor, out, deps, idx, signature[i], i, true);
constantArgument(processor, out, deps, idx, signature[i], i, false);
} else {
if (signature[i].equals(processor.getType(NodeIntrinsicHandler.VALUE_NODE_CLASS_NAME))) {
out.printf(" ValueNode arg%d = args.get(%d);\n", idx, i);
out.printf(" ValueNode arg%d = args[%d];\n", idx, i);
} else {
out.printf(" %s arg%d = (%s) args.get(%d);\n", signature[i], idx, signature[i], i);
out.printf(" %s arg%d = (%s) args[%d];\n", signature[i], idx, signature[i], i);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,7 @@ public void setPluginName(String pluginName) {
this.pluginName = pluginName;
}

protected String pluginSuperclass() {
return "GeneratedInvocationPlugin";
}
protected abstract String pluginSuperclass();

public void generate(AbstractProcessor processor, PrintWriter out) {
out.printf("// class: %s\n", intrinsicMethod.getEnclosingElement());
Expand All @@ -84,28 +82,22 @@ public void generate(AbstractProcessor processor, PrintWriter out) {
InjectedDependencies deps = new InjectedDependencies(true, intrinsicMethod);
createExecute(processor, out, deps);
out.printf(" }\n");
out.printf(" @Override\n");
out.printf(" public Class<? extends Annotation> getSource() {\n");
out.printf(" return %s.class;\n", getAnnotationClass(processor).getQualifiedName().toString().replace('$', '.'));
out.printf(" }\n");

createPrivateMembersAndConstructor(processor, out, deps, pluginName);

out.printf("}\n");

createOtherClasses(processor, out);

createOtherClasses(processor, out, deps);
}

protected void createOtherClasses(AbstractProcessor processor, PrintWriter out) {
protected void createOtherClasses(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps) {
String name = getReplacementName();
out.printf("// class: %s\n", intrinsicMethod.getEnclosingElement());
out.printf("// method: %s\n", intrinsicMethod);
out.printf("// generated-by: %s\n", getClass().getName());
out.printf("@ExcludeFromJacocoGeneratedReport(\"deferred plugin support that is only called in libgraal\")\n");
out.printf("final class %s implements PluginReplacementNode.ReplacementFunction {\n", name);
out.printf(" static PluginReplacementNode.ReplacementFunction FUNCTION = new %s();\n", name);
InjectedDependencies deps = new InjectedDependencies(false, intrinsicMethod);
createHelpers(processor, out, deps);
out.printf("}\n");
}
Expand Down Expand Up @@ -187,13 +179,12 @@ static boolean hasUncheckedWarning(TypeMirror type) {
}
}

protected void createPrivateMembersAndConstructor(AbstractProcessor processor, PrintWriter out, InjectedDependencies deps, String constructorName) {
protected void createPrivateMembersAndConstructor(@SuppressWarnings("unused") AbstractProcessor processor, PrintWriter out, InjectedDependencies deps, String constructorName) {
if (!deps.isEmpty()) {
out.printf("\n");
for (InjectedDependencies.Dependency dep : deps) {
out.printf(" private final %s %s;\n", dep.getType(), dep.getName(processor, intrinsicMethod));
}

needInjectionProvider = true;
}

Expand Down Expand Up @@ -258,13 +249,8 @@ protected String constantArgument(AbstractProcessor processor,
int argIdx,
TypeMirror type,
int nodeIdx,
boolean isReplacement) {
Function<Integer, String> argFormatter;
if (isReplacement) {
argFormatter = (i) -> String.format("args.get(%d)", i);
} else {
argFormatter = (i) -> String.format("args[%d]", i);
}
boolean checkShouldDefer) {
Function<Integer, String> argFormatter = (i) -> String.format("args[%d]", i);
if (hasRawtypeWarning(type)) {
out.printf(" @SuppressWarnings({\"rawtypes\"})\n");
}
Expand Down Expand Up @@ -314,7 +300,7 @@ protected String constantArgument(AbstractProcessor processor,
}
}
out.printf(" } else {\n");
if (!isReplacement) {
if (checkShouldDefer) {
out.printf(" if (b.shouldDeferPlugin(this)) {\n");
out.printf(" b.replacePlugin%s(this, targetMethod, args, %s.FUNCTION);\n", getReplacementFunctionSuffix(processor), getReplacementName());
out.printf(" return true;\n");
Expand Down
Loading
Loading