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 */
35 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
36 import org.graalvm.compiler.lir.phases.LIRSuites;
37 import org.graalvm.compiler.nodes.StructuredGraph;
38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
39 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
40 import org.graalvm.compiler.options.OptionValues;
41 import org.graalvm.compiler.phases.BasePhase;
42 import org.graalvm.compiler.phases.OptimisticOptimizations;
43 import org.graalvm.compiler.phases.PhaseSuite;
44 import org.graalvm.compiler.phases.tiers.HighTierContext;
45 import org.graalvm.compiler.phases.tiers.Suites;
46
47 import jdk.vm.ci.code.InstalledCode;
48 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
49 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
50 import jdk.vm.ci.meta.DefaultProfilingInfo;
51 import jdk.vm.ci.meta.ProfilingInfo;
52 import jdk.vm.ci.meta.ResolvedJavaMethod;
53 import jdk.vm.ci.meta.TriState;
54
55 public class AOTBackend {
56 private final Main main;
57 private final OptionValues graalOptions;
58 private final HotSpotBackend backend;
59 private final HotSpotProviders providers;
60 private final HotSpotCodeCacheProvider codeCache;
61 private final PhaseSuite<HighTierContext> graphBuilderSuite;
62 private final HighTierContext highTierContext;
63 private final GraalFilters filters;
64
65 public AOTBackend(Main main, OptionValues graalOptions, HotSpotBackend backend, GraalFilters filters) {
66 this.main = main;
67 this.graalOptions = graalOptions;
68 this.backend = backend;
69 this.filters = filters;
70 providers = backend.getProviders();
71 codeCache = providers.getCodeCache();
72 graphBuilderSuite = initGraphBuilderSuite(backend, main.options.compileWithAssertions);
73 highTierContext = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.ALL);
74 }
75
76 public PhaseSuite<HighTierContext> getGraphBuilderSuite() {
77 return graphBuilderSuite;
78 }
79
80 public HotSpotBackend getBackend() {
81 return backend;
82 }
83
84 public HotSpotProviders getProviders() {
85 return providers;
86 }
87
88 private Suites getSuites() {
89 // create suites every time, as we modify options for the compiler
90 return backend.getSuites().getDefaultSuites(graalOptions);
91 }
92
93 private LIRSuites getLirSuites() {
94 // create suites every time, as we modify options for the compiler
95 return backend.getSuites().getDefaultLIRSuites(graalOptions);
96 }
97
98 @SuppressWarnings("try")
99 public CompilationResult compileMethod(ResolvedJavaMethod resolvedMethod, DebugContext debug) {
100 StructuredGraph graph = buildStructuredGraph(resolvedMethod, debug);
101 if (graph != null) {
102 return compileGraph(resolvedMethod, graph, debug);
103 }
104 return null;
105 }
106
107 /**
108 * Build a structured graph for the member.
109 *
110 * @param javaMethod method for whose code the graph is to be created
111 * @param debug
112 * @return structured graph
113 */
114 @SuppressWarnings("try")
115 private StructuredGraph buildStructuredGraph(ResolvedJavaMethod javaMethod, DebugContext debug) {
116 try (DebugContext.Scope s = debug.scope("AOTParseMethod")) {
117 StructuredGraph graph = new StructuredGraph.Builder(graalOptions, debug).method(javaMethod).useProfilingInfo(false).build();
118 graphBuilderSuite.apply(graph, highTierContext);
119 return graph;
120 } catch (Throwable e) {
121 handleError(javaMethod, e, " (building graph)");
122 }
123 return null;
124 }
125
126 @SuppressWarnings("try")
127 private CompilationResult compileGraph(ResolvedJavaMethod resolvedMethod, StructuredGraph graph, DebugContext debug) {
128 try (DebugContext.Scope s = debug.scope("AOTCompileMethod")) {
129 ProfilingInfo profilingInfo = DefaultProfilingInfo.get(TriState.FALSE);
130
131 final boolean isImmutablePIC = true;
132 CompilationResult compilationResult = new CompilationResult(resolvedMethod.getName(), isImmutablePIC);
133
134 return GraalCompiler.compileGraph(graph, resolvedMethod, providers, backend, graphBuilderSuite, OptimisticOptimizations.ALL, profilingInfo, getSuites(), getLirSuites(),
135 compilationResult, CompilationResultBuilderFactory.Default);
136
137 } catch (Throwable e) {
138 handleError(resolvedMethod, e, " (compiling graph)");
139 }
140 return null;
141 }
142
143 /**
144 * Returns whether the VM is a debug build.
145 *
146 * @return true is debug VM, false otherwise
147 */
148 public boolean isDebugVM() {
149 return backend.getRuntime().getVMConfig().cAssertions;
150 }
151
152 private static PhaseSuite<HighTierContext> initGraphBuilderSuite(HotSpotBackend backend, boolean compileWithAssertions) {
153 PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite().copy();
154 ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class);
155 GraphBuilderConfiguration baseConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig();
156
157 // Use all default plugins.
158 Plugins plugins = baseConfig.getPlugins();
159 GraphBuilderConfiguration aotConfig = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withOmitAssertions(!compileWithAssertions);
160
161 iterator.next();
162 iterator.remove();
163 iterator.add(new GraphBuilderPhase(aotConfig));
164
165 return graphBuilderSuite;
166 }
167
168 private void handleError(ResolvedJavaMethod resolvedMethod, Throwable e, String message) {
169 String methodName = MiscUtils.uniqueMethodName(resolvedMethod);
170
171 if (main.options.debug) {
172 main.printError("Failed compilation: " + methodName + ": " + e);
173 }
174
175 // Ignore some exceptions when meta-compiling Graal.
176 if (filters.shouldIgnoreException(e)) {
177 return;
178 }
179
180 Main.writeLog("Failed compilation of method " + methodName + message);
181
182 if (!main.options.debug) {
183 main.printError("Failed compilation: " + methodName + ": " + e);
184 }
185
186 if (main.options.verbose) {
187 e.printStackTrace(main.log);
188 }
189
190 if (main.options.exitOnError) {
191 System.exit(1);
192 }
193 }
194
195 public void printCompiledMethod(HotSpotResolvedJavaMethod resolvedMethod, CompilationResult compResult) {
196 // This is really not installing the method.
197 InstalledCode installedCode = codeCache.addCode(resolvedMethod, HotSpotCompiledCodeBuilder.createCompiledCode(codeCache, null, null, compResult), null, null);
198 String disassembly = codeCache.disassemble(installedCode);
199 if (disassembly != null) {
200 main.printlnDebug(disassembly);
201 }
202 }
203 }
|
1 /*
2 * Copyright (c) 2016, 2017, 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 */
35 import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory;
36 import org.graalvm.compiler.lir.phases.LIRSuites;
37 import org.graalvm.compiler.nodes.StructuredGraph;
38 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration;
39 import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins;
40 import org.graalvm.compiler.options.OptionValues;
41 import org.graalvm.compiler.phases.BasePhase;
42 import org.graalvm.compiler.phases.OptimisticOptimizations;
43 import org.graalvm.compiler.phases.PhaseSuite;
44 import org.graalvm.compiler.phases.tiers.HighTierContext;
45 import org.graalvm.compiler.phases.tiers.Suites;
46
47 import jdk.vm.ci.code.InstalledCode;
48 import jdk.vm.ci.hotspot.HotSpotCodeCacheProvider;
49 import jdk.vm.ci.hotspot.HotSpotResolvedJavaMethod;
50 import jdk.vm.ci.meta.DefaultProfilingInfo;
51 import jdk.vm.ci.meta.ProfilingInfo;
52 import jdk.vm.ci.meta.ResolvedJavaMethod;
53 import jdk.vm.ci.meta.TriState;
54
55 final class AOTBackend {
56 private final Main main;
57 private final OptionValues graalOptions;
58 private final HotSpotBackend backend;
59 private final HotSpotProviders providers;
60 private final HotSpotCodeCacheProvider codeCache;
61 private final PhaseSuite<HighTierContext> graphBuilderSuite;
62 private final HighTierContext highTierContext;
63
64 AOTBackend(Main main, OptionValues graalOptions, HotSpotBackend backend) {
65 this.main = main;
66 this.graalOptions = graalOptions;
67 this.backend = backend;
68 providers = backend.getProviders();
69 codeCache = providers.getCodeCache();
70 graphBuilderSuite = initGraphBuilderSuite(backend, main.options.compileWithAssertions);
71 highTierContext = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.ALL);
72 }
73
74 PhaseSuite<HighTierContext> getGraphBuilderSuite() {
75 return graphBuilderSuite;
76 }
77
78 HotSpotBackend getBackend() {
79 return backend;
80 }
81
82 HotSpotProviders getProviders() {
83 return providers;
84 }
85
86 private Suites getSuites() {
87 // create suites every time, as we modify options for the compiler
88 return backend.getSuites().getDefaultSuites(graalOptions);
89 }
90
91 private LIRSuites getLirSuites() {
92 // create suites every time, as we modify options for the compiler
93 return backend.getSuites().getDefaultLIRSuites(graalOptions);
94 }
95
96 @SuppressWarnings("try")
97 CompilationResult compileMethod(ResolvedJavaMethod resolvedMethod, DebugContext debug) {
98 StructuredGraph graph = buildStructuredGraph(resolvedMethod, debug);
99 if (graph != null) {
100 return compileGraph(resolvedMethod, graph, debug);
101 }
102 return null;
103 }
104
105 /**
106 * Build a structured graph for the member.
107 *
108 * @param javaMethod method for whose code the graph is to be created
109 * @param debug
110 * @return structured graph
111 */
112 @SuppressWarnings("try")
113 private StructuredGraph buildStructuredGraph(ResolvedJavaMethod javaMethod, DebugContext debug) {
114 try (DebugContext.Scope s = debug.scope("AOTParseMethod")) {
115 StructuredGraph graph = new StructuredGraph.Builder(graalOptions, debug).method(javaMethod).useProfilingInfo(false).build();
116 graphBuilderSuite.apply(graph, highTierContext);
117 return graph;
118 } catch (Throwable e) {
119 main.handleError(javaMethod, e, " (building graph)");
120 }
121 return null;
122 }
123
124 @SuppressWarnings("try")
125 private CompilationResult compileGraph(ResolvedJavaMethod resolvedMethod, StructuredGraph graph, DebugContext debug) {
126 try (DebugContext.Scope s = debug.scope("AOTCompileMethod")) {
127 ProfilingInfo profilingInfo = DefaultProfilingInfo.get(TriState.FALSE);
128
129 final boolean isImmutablePIC = true;
130 CompilationResult compilationResult = new CompilationResult(resolvedMethod.getName(), isImmutablePIC);
131
132 return GraalCompiler.compileGraph(graph, resolvedMethod, providers, backend, graphBuilderSuite, OptimisticOptimizations.ALL, profilingInfo, getSuites(), getLirSuites(),
133 compilationResult, CompilationResultBuilderFactory.Default);
134
135 } catch (Throwable e) {
136 main.handleError(resolvedMethod, e, " (compiling graph)");
137 }
138 return null;
139 }
140
141 private static PhaseSuite<HighTierContext> initGraphBuilderSuite(HotSpotBackend backend, boolean compileWithAssertions) {
142 PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite().copy();
143 ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class);
144 GraphBuilderConfiguration baseConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig();
145
146 // Use all default plugins.
147 Plugins plugins = baseConfig.getPlugins();
148 GraphBuilderConfiguration aotConfig = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withOmitAssertions(!compileWithAssertions);
149
150 iterator.next();
151 iterator.remove();
152 iterator.add(new GraphBuilderPhase(aotConfig));
153
154 return graphBuilderSuite;
155 }
156
157 void printCompiledMethod(HotSpotResolvedJavaMethod resolvedMethod, CompilationResult compResult) {
158 // This is really not installing the method.
159 InstalledCode installedCode = codeCache.addCode(resolvedMethod, HotSpotCompiledCodeBuilder.createCompiledCode(codeCache, null, null, compResult), null, null);
160 String disassembly = codeCache.disassemble(installedCode);
161 if (disassembly != null) {
162 main.printer.printlnDebug(disassembly);
163 }
164 }
165 }
|