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, main.options.compileWithAssertions); 81 highTierContext = new HighTierContext(providers, graphBuilderSuite, OptimisticOptimizations.ALL); 82 } 83 84 public PhaseSuite<HighTierContext> getGraphBuilderSuite() { 85 return graphBuilderSuite; 86 } 87 88 private Suites getSuites() { 89 // create suites every time, as we modify options for the compiler 90 return backend.getSuites().getDefaultSuites(); 91 } 92 93 private LIRSuites getLirSuites() { 94 // create suites every time, as we modify options for the compiler 95 return backend.getSuites().getDefaultLIRSuites(); 96 } 97 98 @SuppressWarnings("try") 99 public CompilationResult compileMethod(ResolvedJavaMethod resolvedMethod) { 100 try (OverrideScope s = OptionValue.override(ImmutableCode, true, GeneratePIC, true)) { 101 StructuredGraph graph = buildStructuredGraph(resolvedMethod); 102 if (graph != null) { 103 return compileGraph(resolvedMethod, graph); 104 } 105 return null; 106 } 107 } 108 109 /** 110 * Build a structured graph for the member. 111 * 112 * @param javaMethod method for whose code the graph is to be created 113 * @return structured graph 114 */ 115 @SuppressWarnings("try") 116 private StructuredGraph buildStructuredGraph(ResolvedJavaMethod javaMethod) { 117 try (Scope s = Debug.scope("AOTParseMethod")) { 118 StructuredGraph graph = new StructuredGraph(javaMethod, StructuredGraph.AllowAssumptions.NO, false, CompilationIdentifier.INVALID_COMPILATION_ID); 119 graphBuilderSuite.apply(graph, highTierContext); 120 return graph; 121 } catch (Throwable e) { 122 handleError(javaMethod, e, " (building graph)"); 123 } 124 return null; 125 } 126 127 @SuppressWarnings("try") 128 private CompilationResult compileGraph(ResolvedJavaMethod resolvedMethod, StructuredGraph graph) { 129 try (Scope s = Debug.scope("AOTCompileMethod")) { 130 ProfilingInfo profilingInfo = DefaultProfilingInfo.get(TriState.FALSE); 131 132 final boolean isImmutablePIC = true; 133 CompilationResult compilationResult = new CompilationResult(resolvedMethod.getName(), isImmutablePIC); 134 135 return GraalCompiler.compileGraph(graph, resolvedMethod, providers, backend, graphBuilderSuite, OptimisticOptimizations.ALL, profilingInfo, getSuites(), getLirSuites(), 136 compilationResult, CompilationResultBuilderFactory.Default); 137 138 } catch (Throwable e) { 139 handleError(resolvedMethod, e, " (compiling graph)"); 140 } 141 return null; 142 } 143 144 /** 145 * Returns whether the VM is a debug build. 146 * 147 * @return true is debug VM, false otherwise 148 */ 149 public boolean isDebugVM() { 150 return backend.getRuntime().getVMConfig().cAssertions; 151 } 152 153 private static PhaseSuite<HighTierContext> initGraphBuilderSuite(HotSpotBackend backend, boolean compileWithAssertions) { 154 PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite().copy(); 155 ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class); 156 GraphBuilderConfiguration baseConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig(); 157 158 // Use all default plugins. 159 Plugins plugins = baseConfig.getPlugins(); 160 GraphBuilderConfiguration aotConfig = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true).withOmitAssertions(!compileWithAssertions); 161 162 iterator.next(); 163 iterator.remove(); 164 iterator.add(new GraphBuilderPhase(aotConfig)); 165 166 return graphBuilderSuite; 167 } 168 169 private void handleError(ResolvedJavaMethod resolvedMethod, Throwable e, String message) { 170 String methodName = MiscUtils.uniqueMethodName(resolvedMethod); 171 172 if (main.options.debug) { 173 main.printError("Failed compilation: " + methodName + ": " + e); 174 } 175 176 // Ignore some exceptions when meta-compiling Graal. 177 if (filters.shouldIgnoreException(e)) { 178 return; 179 } 180 181 Main.writeLog("Failed compilation of method " + methodName + message); 182 183 if (!main.options.debug) { 184 main.printError("Failed compilation: " + methodName + ": " + e); 185 } 186 187 if (main.options.verbose) { 188 e.printStackTrace(main.log); 189 } 190 191 if (main.options.exitOnError) { 192 System.exit(1); 193 } 194 } 195 196 public void printCompiledMethod(HotSpotResolvedJavaMethod resolvedMethod, CompilationResult compResult) { 197 // This is really not installing the method. 198 InstalledCode installedCode = codeCache.addCode(resolvedMethod, HotSpotCompiledCodeBuilder.createCompiledCode(null, null, compResult), null, null); 199 String disassembly = codeCache.disassemble(installedCode); 200 if (disassembly != null) { 201 main.printlnDebug(disassembly); 202 } 203 } 204} 205