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