AOTBackend.java revision 12968:4d8a004e5c6d
1/*
2 * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24package jdk.tools.jaotc;
25
26import java.util.ListIterator;
27
28import org.graalvm.compiler.code.CompilationResult;
29import org.graalvm.compiler.core.GraalCompiler;
30import org.graalvm.compiler.debug.Debug;
31import org.graalvm.compiler.debug.Debug.Scope;
32import org.graalvm.compiler.hotspot.HotSpotBackend;
33import org.graalvm.compiler.hotspot.HotSpotCompiledCodeBuilder;
34import org.graalvm.compiler.hotspot.meta.HotSpotProviders;
35import org.graalvm.compiler.java.GraphBuilderPhase;
36import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
37import org.graalvm.compiler.lir.phases.LIRSuites;
38import org.graalvm.compiler.nodes.StructuredGraph;
39import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
40import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
41import org.graalvm.compiler.options.OptionValues;
42import org.graalvm.compiler.phases.BasePhase;
43import org.graalvm.compiler.phases.OptimisticOptimizations;
44import org.graalvm.compiler.phases.PhaseSuite;
45import org.graalvm.compiler.phases.tiers.HighTierContext;
46import org.graalvm.compiler.phases.tiers.Suites;
47
48import jdk.vm.ci.code.InstalledCode;
49import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
50import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
51import jdk.vm.ci.meta.DefaultProfilingInfo;
52import jdk.vm.ci.meta.ProfilingInfo;
53import jdk.vm.ci.meta.ResolvedJavaMethod;
54import jdk.vm.ci.meta.TriState;
55
56public class AOTBackend {
57
58    private final Main main;
59    private final OptionValues graalOptions;
60
61    private final HotSpotBackend backend;
62
63    private final HotSpotProviders providers;
64    private final HotSpotCodeCacheProvider codeCache;
65    private final PhaseSuite<HighTierContext> graphBuilderSuite;
66    private final HighTierContext highTierContext;
67    private final GraalFilters filters;
68
69    public AOTBackend(Main main, OptionValues graalOptions, HotSpotBackend backend, GraalFilters filters) {
70        this.main = main;
71        this.graalOptions = graalOptions;
72        this.backend = backend;
73        this.filters = filters;
74        providers = backend.getProviders();
75        codeCache = providers.getCodeCache();
76        graphBuilderSuite = initGraphBuilderSuite(backend, main.options.compileWithAssertions);
77        highTierContext = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.ALL);
78    }
79
80    public PhaseSuite<HighTierContext> getGraphBuilderSuite() {
81        return graphBuilderSuite;
82    }
83
84    private Suites getSuites() {
85        // create suites every time, as we modify options for the compiler
86        return backend.getSuites().getDefaultSuites(graalOptions);
87    }
88
89    private LIRSuites getLirSuites() {
90        // create suites every time, as we modify options for the compiler
91        return backend.getSuites().getDefaultLIRSuites(graalOptions);
92    }
93
94    @SuppressWarnings("try")
95    public CompilationResult compileMethod(ResolvedJavaMethod resolvedMethod) {
96        StructuredGraph graph = buildStructuredGraph(resolvedMethod);
97        if (graph != null) {
98            return compileGraph(resolvedMethod, graph);
99        }
100        return null;
101    }
102
103    /**
104     * Build a structured graph for the member.
105     *
106     * @param javaMethod method for whose code the graph is to be created
107     * @return structured graph
108     */
109    @SuppressWarnings("try")
110    private StructuredGraph buildStructuredGraph(ResolvedJavaMethod javaMethod) {
111        try (Scope s = Debug.scope("AOTParseMethod")) {
112            StructuredGraph graph = new StructuredGraph.Builder(graalOptions).method(javaMethod).useProfilingInfo(false).build();
113            graphBuilderSuite.apply(graph, highTierContext);
114            return graph;
115        } catch (Throwable e) {
116            handleError(javaMethod, e, " (building graph)");
117        }
118        return null;
119    }
120
121    @SuppressWarnings("try")
122    private CompilationResult compileGraph(ResolvedJavaMethod resolvedMethod, StructuredGraph graph) {
123        try (Scope s = Debug.scope("AOTCompileMethod")) {
124            ProfilingInfo profilingInfo = DefaultProfilingInfo.get(TriState.FALSE);
125
126            final boolean isImmutablePIC = true;
127            CompilationResult compilationResult = new CompilationResult(resolvedMethod.getName(), isImmutablePIC);
128
129            return GraalCompiler.compileGraph(graph, resolvedMethod, providers, backend, graphBuilderSuite, OptimisticOptimizations.ALL, profilingInfo, getSuites(), getLirSuites(),
130                            compilationResult, CompilationResultBuilderFactory.Default);
131
132        } catch (Throwable e) {
133            handleError(resolvedMethod, e, " (compiling graph)");
134        }
135        return null;
136    }
137
138    /**
139     * Returns whether the VM is a debug build.
140     *
141     * @return true is debug VM, false otherwise
142     */
143    public boolean isDebugVM() {
144        return backend.getRuntime().getVMConfig().cAssertions;
145    }
146
147    private static PhaseSuite<HighTierContext> initGraphBuilderSuite(HotSpotBackend backend, boolean compileWithAssertions) {
148        PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite().copy();
149        ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class);
150        GraphBuilderConfiguration baseConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig();
151
152        // Use all default plugins.
153        Plugins plugins = baseConfig.getPlugins();
154        GraphBuilderConfiguration aotConfig = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withOmitAssertions(!compileWithAssertions);
155
156        iterator.next();
157        iterator.remove();
158        iterator.add(new GraphBuilderPhase(aotConfig));
159
160        return graphBuilderSuite;
161    }
162
163    private void handleError(ResolvedJavaMethod resolvedMethod, Throwable e, String message) {
164        String methodName = MiscUtils.uniqueMethodName(resolvedMethod);
165
166        if (main.options.debug) {
167            main.printError("Failed compilation: " + methodName + ": " + e);
168        }
169
170        // Ignore some exceptions when meta-compiling Graal.
171        if (filters.shouldIgnoreException(e)) {
172            return;
173        }
174
175        Main.writeLog("Failed compilation of method " + methodName + message);
176
177        if (!main.options.debug) {
178            main.printError("Failed compilation: " + methodName + ": " + e);
179        }
180
181        if (main.options.verbose) {
182            e.printStackTrace(main.log);
183        }
184
185        if (main.options.exitOnError) {
186            System.exit(1);
187        }
188    }
189
190    public void printCompiledMethod(HotSpotResolvedJavaMethod resolvedMethod, CompilationResult compResult) {
191        // This is really not installing the method.
192        InstalledCode installedCode = codeCache.addCode(resolvedMethod, HotSpotCompiledCodeBuilder.createCompiledCode(null, null, compResult), null, null);
193        String disassembly = codeCache.disassemble(installedCode);
194        if (disassembly != null) {
195            main.printlnDebug(disassembly);
196        }
197    }
198}
199