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