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 24 package jdk.tools.jaotc; 25 26 import java.util.ListIterator; 27 28 import org.graalvm.compiler.code.CompilationResult; 29 import org.graalvm.compiler.core.GraalCompiler; 30 import org.graalvm.compiler.debug.DebugContext; 31 import org.graalvm.compiler.hotspot.HotSpotBackend; 32 import org.graalvm.compiler.hotspot.HotSpotCompiledCodeBuilder; 33 import org.graalvm.compiler.hotspot.meta.HotSpotProviders; 34 import org.graalvm.compiler.java.GraphBuilderPhase; 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 }