1 /* 2 * Copyright (c) 2015, 2018, 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 25 package org.graalvm.compiler.hotspot; 26 27 import static jdk.vm.ci.common.InitTimer.timer; 28 import static org.graalvm.compiler.hotspot.HotSpotGraalOptionValues.GRAAL_OPTION_PROPERTY_PREFIX; 29 30 import java.io.PrintStream; 31 32 import org.graalvm.compiler.api.runtime.GraalRuntime; 33 import org.graalvm.compiler.debug.MethodFilter; 34 import org.graalvm.compiler.options.Option; 35 import org.graalvm.compiler.options.OptionKey; 36 import org.graalvm.compiler.options.OptionType; 37 import org.graalvm.compiler.options.OptionValues; 38 import org.graalvm.compiler.options.OptionsParser; 39 import org.graalvm.compiler.phases.tiers.CompilerConfiguration; 40 41 import jdk.vm.ci.common.InitTimer; 42 import jdk.vm.ci.hotspot.HotSpotJVMCICompilerFactory; 43 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime; 44 import jdk.vm.ci.hotspot.HotSpotSignature; 45 import jdk.vm.ci.runtime.JVMCIRuntime; 46 47 public final class HotSpotGraalCompilerFactory extends HotSpotJVMCICompilerFactory { 48 49 private static MethodFilter[] graalCompileOnlyFilter; 50 private static boolean compileGraalWithC1Only; 51 52 private IsGraalPredicate isGraalPredicate; 53 54 private final HotSpotGraalJVMCIServiceLocator locator; 55 HotSpotGraalCompilerFactory(HotSpotGraalJVMCIServiceLocator locator)56 HotSpotGraalCompilerFactory(HotSpotGraalJVMCIServiceLocator locator) { 57 this.locator = locator; 58 } 59 60 @Override getCompilerName()61 public String getCompilerName() { 62 return "graal"; 63 } 64 65 /** 66 * Initialized when this factory is {@linkplain #onSelection() selected}. 67 */ 68 private OptionValues options; 69 70 @Override onSelection()71 public void onSelection() { 72 JVMCIVersionCheck.check(false); 73 assert options == null : "cannot select " + getClass() + " service more than once"; 74 options = HotSpotGraalOptionValues.HOTSPOT_OPTIONS; 75 initializeGraalCompilePolicyFields(options); 76 isGraalPredicate = compileGraalWithC1Only ? new IsGraalPredicate() : null; 77 /* 78 * Exercise this code path early to encourage loading now. This doesn't solve problem of 79 * deadlock during class loading but seems to eliminate it in practice. 80 */ 81 adjustCompilationLevelInternal(Object.class, "hashCode", "()I", CompilationLevel.FullOptimization); 82 adjustCompilationLevelInternal(Object.class, "hashCode", "()I", CompilationLevel.Simple); 83 } 84 initializeGraalCompilePolicyFields(OptionValues options)85 private static void initializeGraalCompilePolicyFields(OptionValues options) { 86 compileGraalWithC1Only = Options.CompileGraalWithC1Only.getValue(options); 87 String optionValue = Options.GraalCompileOnly.getValue(options); 88 if (optionValue != null) { 89 MethodFilter[] filter = MethodFilter.parse(optionValue); 90 if (filter.length == 0) { 91 filter = null; 92 } 93 graalCompileOnlyFilter = filter; 94 } 95 } 96 97 @Override printProperties(PrintStream out)98 public void printProperties(PrintStream out) { 99 out.println("[Graal properties]"); 100 options.printHelp(OptionsParser.getOptionsLoader(), out, GRAAL_OPTION_PROPERTY_PREFIX); 101 } 102 103 static class Options { 104 105 // @formatter:off 106 @Option(help = "In tiered mode compile Graal and JVMCI using optimized first tier code.", type = OptionType.Expert) 107 public static final OptionKey<Boolean> CompileGraalWithC1Only = new OptionKey<>(true); 108 109 @Option(help = "A filter applied to a method the VM has selected for compilation by Graal. " + 110 "A method not matching the filter is redirected to a lower tier compiler. " + 111 "The filter format is the same as for the MethodFilter option.", type = OptionType.Expert) 112 public static final OptionKey<String> GraalCompileOnly = new OptionKey<>(null); 113 // @formatter:on 114 115 } 116 117 @Override createCompiler(JVMCIRuntime runtime)118 public HotSpotGraalCompiler createCompiler(JVMCIRuntime runtime) { 119 CompilerConfigurationFactory factory = CompilerConfigurationFactory.selectFactory(null, options); 120 if (isGraalPredicate != null) { 121 isGraalPredicate.onCompilerConfigurationFactorySelection(factory); 122 } 123 HotSpotGraalCompiler compiler = createCompiler("VM", runtime, options, factory); 124 // Only the HotSpotGraalRuntime associated with the compiler created via 125 // jdk.vm.ci.runtime.JVMCIRuntime.getCompiler() is registered for receiving 126 // VM events. 127 locator.onCompilerCreation(compiler); 128 return compiler; 129 } 130 131 /** 132 * Creates a new {@link HotSpotGraalRuntime} object and a new {@link HotSpotGraalCompiler} and 133 * returns the latter. 134 * 135 * @param runtimeNameQualifier a qualifier to be added to the {@linkplain GraalRuntime#getName() 136 * name} of the {@linkplain HotSpotGraalCompiler#getGraalRuntime() runtime} created 137 * by this method 138 * @param runtime the JVMCI runtime on which the {@link HotSpotGraalRuntime} is built 139 * @param compilerConfigurationFactory factory for the {@link CompilerConfiguration} 140 */ 141 @SuppressWarnings("try") createCompiler(String runtimeNameQualifier, JVMCIRuntime runtime, OptionValues options, CompilerConfigurationFactory compilerConfigurationFactory)142 public static HotSpotGraalCompiler createCompiler(String runtimeNameQualifier, JVMCIRuntime runtime, OptionValues options, CompilerConfigurationFactory compilerConfigurationFactory) { 143 HotSpotJVMCIRuntime jvmciRuntime = (HotSpotJVMCIRuntime) runtime; 144 try (InitTimer t = timer("HotSpotGraalRuntime.<init>")) { 145 HotSpotGraalRuntime graalRuntime = new HotSpotGraalRuntime(runtimeNameQualifier, jvmciRuntime, compilerConfigurationFactory, options); 146 return new HotSpotGraalCompiler(jvmciRuntime, graalRuntime, graalRuntime.getOptions()); 147 } 148 } 149 150 @Override getCompilationLevelAdjustment()151 public CompilationLevelAdjustment getCompilationLevelAdjustment() { 152 if (graalCompileOnlyFilter != null) { 153 return CompilationLevelAdjustment.ByFullSignature; 154 } 155 if (compileGraalWithC1Only) { 156 // We only decide using the class declaring the method 157 // so no need to have the method name and signature 158 // symbols converted to a String. 159 return CompilationLevelAdjustment.ByHolder; 160 } 161 return CompilationLevelAdjustment.None; 162 } 163 164 @Override adjustCompilationLevel(Object declaringClassObject, String name, String signature, boolean isOsr, CompilationLevel level)165 public CompilationLevel adjustCompilationLevel(Object declaringClassObject, String name, String signature, boolean isOsr, CompilationLevel level) { 166 if (declaringClassObject instanceof String) { 167 // This must be SVM mode in which case only GraalCompileC1Only matters since Graal and 168 // JVMCI are already compiled. 169 return checkGraalCompileOnlyFilter((String) declaringClassObject, name, signature, level); 170 } 171 Class<?> declaringClass = (Class<?>) declaringClassObject; 172 return adjustCompilationLevelInternal(declaringClass, name, signature, level); 173 } 174 175 static { 176 // Fail-fast detection for package renaming to guard use of package 177 // prefixes in adjustCompilationLevelInternal. 178 assert jdk.vm.ci.services.Services.class.getName().equals("jdk.vm.ci.services.Services"); 179 assert HotSpotGraalCompilerFactory.class.getName().equals("org.graalvm.compiler.hotspot.HotSpotGraalCompilerFactory"); 180 } 181 adjustCompilationLevelInternal(Class<?> declaringClass, String name, String signature, CompilationLevel level)182 private CompilationLevel adjustCompilationLevelInternal(Class<?> declaringClass, String name, String signature, CompilationLevel level) { 183 if (compileGraalWithC1Only) { 184 if (level.ordinal() > CompilationLevel.Simple.ordinal()) { 185 if (isGraalPredicate.apply(declaringClass)) { 186 return CompilationLevel.Simple; 187 } 188 } 189 } 190 return checkGraalCompileOnlyFilter(declaringClass.getName(), name, signature, level); 191 } 192 checkGraalCompileOnlyFilter(String declaringClassName, String name, String signature, CompilationLevel level)193 public static CompilationLevel checkGraalCompileOnlyFilter(String declaringClassName, String name, String signature, CompilationLevel level) { 194 if (graalCompileOnlyFilter != null) { 195 if (level == CompilationLevel.FullOptimization) { 196 HotSpotSignature sig = null; 197 for (MethodFilter filter : graalCompileOnlyFilter) { 198 if (filter.hasSignature() && sig == null) { 199 sig = new HotSpotSignature(HotSpotJVMCIRuntime.runtime(), signature); 200 } 201 if (filter.matches(declaringClassName, name, sig)) { 202 return level; 203 } 204 } 205 return CompilationLevel.Simple; 206 } 207 } 208 return level; 209 } 210 } 211