Search in sources :

Example 16 with GraphBuilderConfiguration

use of org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration in project graal by oracle.

the class MethodTypeFlowBuilder method parse.

@SuppressWarnings("try")
private boolean parse() {
    OptionValues options = bb.getOptions();
    DebugContext debug = DebugContext.create(options, new GraalDebugHandlersFactory(bb.getProviders().getSnippetReflection()));
    try (Indent indent = debug.logAndIndent("parse graph %s", method)) {
        boolean needParsing = false;
        graph = method.buildGraph(debug, method, bb.getProviders(), Purpose.ANALYSIS);
        if (graph == null) {
            InvocationPlugin plugin = bb.getProviders().getGraphBuilderPlugins().getInvocationPlugins().lookupInvocation(method);
            if (plugin != null && !plugin.inlineOnly()) {
                Bytecode code = new ResolvedJavaMethodBytecode(method);
                graph = new SubstrateIntrinsicGraphBuilder(options, debug, bb.getProviders().getMetaAccess(), bb.getProviders().getConstantReflection(), bb.getProviders().getConstantFieldProvider(), bb.getProviders().getStampProvider(), code).buildGraph(plugin);
            }
        }
        if (graph == null) {
            if (!method.hasBytecodes()) {
                return false;
            }
            needParsing = true;
            graph = new StructuredGraph.Builder(options, debug).method(method).build();
        }
        try (DebugContext.Scope s = debug.scope("ClosedWorldAnalysis", graph, method, this)) {
            // enable this logging to get log output in compilation passes
            try (Indent indent2 = debug.logAndIndent("parse graph phases")) {
                if (needParsing) {
                    GraphBuilderConfiguration config = GraphBuilderConfiguration.getDefault(bb.getProviders().getGraphBuilderPlugins()).withEagerResolving(true).withUnresolvedIsError(PointstoOptions.UnresolvedIsError.getValue(bb.getOptions())).withNodeSourcePosition(true).withBytecodeExceptionMode(BytecodeExceptionMode.CheckAll);
                    bb.getHostVM().createGraphBuilderPhase(bb.getProviders(), config, OptimisticOptimizations.NONE, null).apply(graph);
                }
            } catch (PermanentBailoutException ex) {
                bb.getUnsupportedFeatures().addMessage(method.format("%H.%n(%p)"), method, ex.getLocalizedMessage(), null, ex);
                return false;
            }
            // Register used types and fields before canonicalization can optimize them.
            registerUsedElements(bb, graph, methodFlow);
            new CanonicalizerPhase().apply(graph, new PhaseContext(bb.getProviders()));
            // Do it again after canonicalization changed type checks and field accesses.
            registerUsedElements(bb, graph, methodFlow);
        } catch (Throwable e) {
            throw debug.handle(e);
        }
    }
    return true;
}
Also used : Indent(org.graalvm.compiler.debug.Indent) OptionValues(org.graalvm.compiler.options.OptionValues) SubstrateIntrinsicGraphBuilder(com.oracle.graal.pointsto.phases.SubstrateIntrinsicGraphBuilder) GraphBuilderConfiguration(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration) DebugContext(org.graalvm.compiler.debug.DebugContext) GraalDebugHandlersFactory(org.graalvm.compiler.printer.GraalDebugHandlersFactory) PhaseContext(org.graalvm.compiler.phases.tiers.PhaseContext) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) InvocationPlugin(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugin) ResolvedJavaMethodBytecode(org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode) Bytecode(org.graalvm.compiler.bytecode.Bytecode) CanonicalizerPhase(org.graalvm.compiler.phases.common.CanonicalizerPhase) ResolvedJavaMethodBytecode(org.graalvm.compiler.bytecode.ResolvedJavaMethodBytecode) PermanentBailoutException(org.graalvm.compiler.core.common.PermanentBailoutException)

Example 17 with GraphBuilderConfiguration

use of org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration in project graal by oracle.

the class CheckGraalInvariants method runTest.

@SuppressWarnings("try")
public static void runTest(InvariantsTool tool) {
    RuntimeProvider rt = Graal.getRequiredCapability(RuntimeProvider.class);
    Providers providers = rt.getHostBackend().getProviders();
    MetaAccessProvider metaAccess = providers.getMetaAccess();
    PhaseSuite<HighTierContext> graphBuilderSuite = new PhaseSuite<>();
    Plugins plugins = new Plugins(new InvocationPlugins());
    GraphBuilderConfiguration config = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withUnresolvedIsError(true);
    graphBuilderSuite.appendPhase(new GraphBuilderPhase(config));
    HighTierContext context = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.NONE);
    Assume.assumeTrue(VerifyPhase.class.desiredAssertionStatus());
    String bootclasspath = tool.getClassPath();
    Assert.assertNotNull("Cannot find boot class path", bootclasspath);
    final List<String> classNames = new ArrayList<>();
    for (String path : bootclasspath.split(File.pathSeparator)) {
        if (tool.shouldProcess(path)) {
            try {
                final ZipFile zipFile = new ZipFile(new File(path));
                for (final Enumeration<? extends ZipEntry> entry = zipFile.entries(); entry.hasMoreElements(); ) {
                    final ZipEntry zipEntry = entry.nextElement();
                    String name = zipEntry.getName();
                    if (name.endsWith(".class") && !name.startsWith("META-INF/versions/")) {
                        String className = name.substring(0, name.length() - ".class".length()).replace('/', '.');
                        if (isInNativeImage(className)) {
                            /*
                                 * Native Image is an external tool and does not need to follow the
                                 * Graal invariants.
                                 */
                            continue;
                        }
                        classNames.add(className);
                    }
                }
            } catch (IOException ex) {
                Assert.fail(ex.toString());
            }
        }
    }
    Assert.assertFalse("Could not find graal jars on boot class path: " + bootclasspath, classNames.isEmpty());
    // Allows a subset of methods to be checked through use of a system property
    String property = System.getProperty(CheckGraalInvariants.class.getName() + ".filters");
    String[] filters = property == null ? null : property.split(",");
    OptionValues options = getInitialOptions();
    CompilerThreadFactory factory = new CompilerThreadFactory("CheckInvariantsThread");
    int availableProcessors = Runtime.getRuntime().availableProcessors();
    ThreadPoolExecutor executor = new ThreadPoolExecutor(availableProcessors, availableProcessors, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), factory);
    List<String> errors = Collections.synchronizedList(new ArrayList<>());
    for (Method m : BadUsageWithEquals.class.getDeclaredMethods()) {
        ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m);
        try (DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER)) {
            StructuredGraph graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.YES).method(method).build();
            try (DebugCloseable s = debug.disableIntercept();
                DebugContext.Scope ds = debug.scope("CheckingGraph", graph, method)) {
                graphBuilderSuite.apply(graph, context);
                // update phi stamps
                graph.getNodes().filter(PhiNode.class).forEach(PhiNode::inferStamp);
                checkGraph(context, graph);
                errors.add(String.format("Expected error while checking %s", m));
            } catch (VerificationError e) {
            // expected!
            } catch (Throwable e) {
                errors.add(String.format("Error while checking %s:%n%s", m, printStackTraceToString(e)));
            }
        }
    }
    if (errors.isEmpty()) {
        // Order outer classes before the inner classes
        classNames.sort((String a, String b) -> a.compareTo(b));
        // Initialize classes in single thread to avoid deadlocking issues during initialization
        List<Class<?>> classes = initializeClasses(tool, classNames);
        for (Class<?> c : classes) {
            String className = c.getName();
            executor.execute(() -> {
                try {
                    checkClass(c, metaAccess);
                } catch (Throwable e) {
                    errors.add(String.format("Error while checking %s:%n%s", className, printStackTraceToString(e)));
                }
            });
            for (Method m : c.getDeclaredMethods()) {
                if (Modifier.isNative(m.getModifiers()) || Modifier.isAbstract(m.getModifiers())) {
                // ignore
                } else {
                    String methodName = className + "." + m.getName();
                    if (matches(filters, methodName)) {
                        executor.execute(() -> {
                            try (DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER)) {
                                ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m);
                                StructuredGraph graph = new StructuredGraph.Builder(options, debug).method(method).build();
                                try (DebugCloseable s = debug.disableIntercept();
                                    DebugContext.Scope ds = debug.scope("CheckingGraph", graph, method)) {
                                    checkMethod(method);
                                    graphBuilderSuite.apply(graph, context);
                                    // update phi stamps
                                    graph.getNodes().filter(PhiNode.class).forEach(PhiNode::inferStamp);
                                    checkGraph(context, graph);
                                } catch (VerificationError e) {
                                    errors.add(e.getMessage());
                                } catch (LinkageError e) {
                                // suppress linkages errors resulting from eager resolution
                                } catch (BailoutException e) {
                                // Graal bail outs on certain patterns in Java bytecode
                                // (e.g.,
                                // unbalanced monitors introduced by jacoco).
                                } catch (Throwable e) {
                                    try {
                                        tool.handleParsingException(e);
                                    } catch (Throwable t) {
                                        errors.add(String.format("Error while checking %s:%n%s", methodName, printStackTraceToString(e)));
                                    }
                                }
                            }
                        });
                    }
                }
            }
        }
        executor.shutdown();
        try {
            executor.awaitTermination(1, TimeUnit.HOURS);
        } catch (InterruptedException e1) {
            throw new RuntimeException(e1);
        }
    }
    if (!errors.isEmpty()) {
        StringBuilder msg = new StringBuilder();
        String nl = String.format("%n");
        for (String e : errors) {
            if (msg.length() != 0) {
                msg.append(nl);
            }
            msg.append(e);
        }
        Assert.fail(msg.toString());
    }
}
Also used : OptionValues(org.graalvm.compiler.options.OptionValues) VerificationError(org.graalvm.compiler.phases.VerifyPhase.VerificationError) ZipEntry(java.util.zip.ZipEntry) RuntimeProvider(org.graalvm.compiler.runtime.RuntimeProvider) ArrayList(java.util.ArrayList) Providers(org.graalvm.compiler.phases.util.Providers) VerifyPhase(org.graalvm.compiler.phases.VerifyPhase) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) GraphBuilderPhase(org.graalvm.compiler.java.GraphBuilderPhase) InvocationPlugins(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins) Plugins(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins) InvocationPlugins(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins) PhiNode(org.graalvm.compiler.nodes.PhiNode) GraphBuilderConfiguration(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration) IOException(java.io.IOException) Method(java.lang.reflect.Method) JavaMethod(jdk.vm.ci.meta.JavaMethod) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) DebugContext(org.graalvm.compiler.debug.DebugContext) BailoutException(jdk.vm.ci.code.BailoutException) ZipFile(java.util.zip.ZipFile) NodeClass(org.graalvm.compiler.graph.NodeClass) HighTierContext(org.graalvm.compiler.phases.tiers.HighTierContext) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) DebugCloseable(org.graalvm.compiler.debug.DebugCloseable) PhaseSuite(org.graalvm.compiler.phases.PhaseSuite) ZipFile(java.util.zip.ZipFile) File(java.io.File) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider) CompilerThreadFactory(org.graalvm.compiler.core.CompilerThreadFactory) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Example 18 with GraphBuilderConfiguration

use of org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration in project graal by oracle.

the class HotSpotSuitesProvider method withNodeSourcePosition.

/**
 * Modifies a given {@link GraphBuilderConfiguration} to record per node source information.
 *
 * @param gbs the current graph builder suite to modify
 */
public static PhaseSuite<HighTierContext> withNodeSourcePosition(PhaseSuite<HighTierContext> gbs) {
    PhaseSuite<HighTierContext> newGbs = gbs.copy();
    GraphBuilderPhase graphBuilderPhase = (GraphBuilderPhase) newGbs.findPhase(GraphBuilderPhase.class).previous();
    GraphBuilderConfiguration graphBuilderConfig = graphBuilderPhase.getGraphBuilderConfig();
    GraphBuilderPhase newGraphBuilderPhase = new GraphBuilderPhase(graphBuilderConfig.withNodeSourcePosition(true));
    newGbs.findPhase(GraphBuilderPhase.class).set(newGraphBuilderPhase);
    return newGbs;
}
Also used : GraphBuilderConfiguration(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration) HighTierContext(org.graalvm.compiler.phases.tiers.HighTierContext) GraphBuilderPhase(org.graalvm.compiler.java.GraphBuilderPhase)

Example 19 with GraphBuilderConfiguration

use of org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration in project graal by oracle.

the class StaticInterfaceFieldTest method eagerlyParseMethod.

@SuppressWarnings("try")
private void eagerlyParseMethod(Class<C> clazz, String methodName) {
    RuntimeProvider rt = Graal.getRequiredCapability(RuntimeProvider.class);
    Providers providers = rt.getHostBackend().getProviders();
    MetaAccessProvider metaAccess = providers.getMetaAccess();
    PhaseSuite<HighTierContext> graphBuilderSuite = new PhaseSuite<>();
    Plugins plugins = new Plugins(new InvocationPlugins());
    GraphBuilderConfiguration config = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withUnresolvedIsError(true);
    graphBuilderSuite.appendPhase(new GraphBuilderPhase(config));
    HighTierContext context = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.NONE);
    Assume.assumeTrue(VerifyPhase.class.desiredAssertionStatus());
    final Method m = getMethod(clazz, methodName);
    ResolvedJavaMethod method = metaAccess.lookupJavaMethod(m);
    OptionValues options = getInitialOptions();
    DebugContext debug = DebugContext.create(options, DebugHandlersFactory.LOADER);
    StructuredGraph graph = new StructuredGraph.Builder(options, debug).method(method).build();
    try (DebugCloseable s = debug.disableIntercept();
        DebugContext.Scope ds = debug.scope("GraphBuilding", graph, method)) {
        graphBuilderSuite.apply(graph, context);
    } catch (Throwable e) {
        throw debug.handle(e);
    }
}
Also used : InvocationPlugins(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins) OptionValues(org.graalvm.compiler.options.OptionValues) RuntimeProvider(org.graalvm.compiler.runtime.RuntimeProvider) GraphBuilderConfiguration(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration) Method(java.lang.reflect.Method) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) DebugContext(org.graalvm.compiler.debug.DebugContext) Providers(org.graalvm.compiler.phases.util.Providers) VerifyPhase(org.graalvm.compiler.phases.VerifyPhase) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) HighTierContext(org.graalvm.compiler.phases.tiers.HighTierContext) DebugCloseable(org.graalvm.compiler.debug.DebugCloseable) PhaseSuite(org.graalvm.compiler.phases.PhaseSuite) GraphBuilderPhase(org.graalvm.compiler.java.GraphBuilderPhase) MetaAccessProvider(jdk.vm.ci.meta.MetaAccessProvider) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod) InvocationPlugins(org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins) Plugins(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins)

Example 20 with GraphBuilderConfiguration

use of org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration in project graal by oracle.

the class FinalizableSubclassTest method parseAndProcess.

private StructuredGraph parseAndProcess(Class<?> cl, AllowAssumptions allowAssumptions) {
    Constructor<?>[] constructors = cl.getConstructors();
    Assert.assertTrue(constructors.length == 1);
    final ResolvedJavaMethod javaMethod = getMetaAccess().lookupJavaMethod(constructors[0]);
    OptionValues options = getInitialOptions();
    StructuredGraph graph = new StructuredGraph.Builder(options, getDebugContext(options, null, javaMethod), allowAssumptions).method(javaMethod).build();
    GraphBuilderConfiguration conf = GraphBuilderConfiguration.getSnippetDefault(getDefaultGraphBuilderPlugins());
    new GraphBuilderPhase.Instance(getMetaAccess(), getProviders().getStampProvider(), getProviders().getConstantReflection(), getProviders().getConstantFieldProvider(), conf, OptimisticOptimizations.ALL, null).apply(graph);
    HighTierContext context = new HighTierContext(getProviders(), getDefaultGraphBuilderSuite(), OptimisticOptimizations.ALL);
    new InliningPhase(new CanonicalizerPhase()).apply(graph, context);
    new CanonicalizerPhase().apply(graph, context);
    return graph;
}
Also used : OptionValues(org.graalvm.compiler.options.OptionValues) StructuredGraph(org.graalvm.compiler.nodes.StructuredGraph) Constructor(java.lang.reflect.Constructor) GraphBuilderConfiguration(org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration) CanonicalizerPhase(org.graalvm.compiler.phases.common.CanonicalizerPhase) HighTierContext(org.graalvm.compiler.phases.tiers.HighTierContext) GraphBuilderPhase(org.graalvm.compiler.java.GraphBuilderPhase) InliningPhase(org.graalvm.compiler.phases.common.inlining.InliningPhase) ResolvedJavaMethod(jdk.vm.ci.meta.ResolvedJavaMethod)

Aggregations

GraphBuilderConfiguration (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration)21 StructuredGraph (org.graalvm.compiler.nodes.StructuredGraph)16 GraphBuilderPhase (org.graalvm.compiler.java.GraphBuilderPhase)15 Plugins (org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins)12 ResolvedJavaMethod (jdk.vm.ci.meta.ResolvedJavaMethod)11 DebugContext (org.graalvm.compiler.debug.DebugContext)11 InvocationPlugins (org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins)11 OptionValues (org.graalvm.compiler.options.OptionValues)10 HighTierContext (org.graalvm.compiler.phases.tiers.HighTierContext)10 MetaAccessProvider (jdk.vm.ci.meta.MetaAccessProvider)9 Providers (org.graalvm.compiler.phases.util.Providers)7 Method (java.lang.reflect.Method)6 CanonicalizerPhase (org.graalvm.compiler.phases.common.CanonicalizerPhase)6 DebugCloseable (org.graalvm.compiler.debug.DebugCloseable)5 PhaseSuite (org.graalvm.compiler.phases.PhaseSuite)5 PhaseContext (org.graalvm.compiler.phases.tiers.PhaseContext)5 RuntimeProvider (org.graalvm.compiler.runtime.RuntimeProvider)5 SnippetReflectionProvider (org.graalvm.compiler.api.replacements.SnippetReflectionProvider)3 Bytecode (org.graalvm.compiler.bytecode.Bytecode)3 IntrinsicContext (org.graalvm.compiler.nodes.graphbuilderconf.IntrinsicContext)3