comparison jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCICompilerConfig.java @ 23723:3db9e47b477a

simplified JVMCI compiler auto selection (JDK-8160730)
author Doug Simon <doug.simon@oracle.com>
date Sun, 03 Jul 2016 00:13:22 +0200
parents c61554b3f6df
children 5595ab8ba8bd
comparison
equal deleted inserted replaced
23722:eb3fb34bcd47 23723:3db9e47b477a
20 * or visit www.oracle.com if you need additional information or have any 20 * or visit www.oracle.com if you need additional information or have any
21 * questions. 21 * questions.
22 */ 22 */
23 package jdk.vm.ci.hotspot; 23 package jdk.vm.ci.hotspot;
24 24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Comparator;
28
29 import jdk.vm.ci.code.CompilationRequest; 25 import jdk.vm.ci.code.CompilationRequest;
30 import jdk.vm.ci.code.CompilationRequestResult; 26 import jdk.vm.ci.code.CompilationRequestResult;
31 import jdk.vm.ci.common.JVMCIError; 27 import jdk.vm.ci.common.JVMCIError;
32 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option; 28 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option;
33 import jdk.vm.ci.runtime.JVMCICompiler; 29 import jdk.vm.ci.runtime.JVMCICompiler;
34 import jdk.vm.ci.runtime.JVMCIRuntime; 30 import jdk.vm.ci.runtime.JVMCIRuntime;
35 import jdk.vm.ci.runtime.services.JVMCICompilerFactory; 31 import jdk.vm.ci.runtime.services.JVMCICompilerFactory;
36 import jdk.vm.ci.runtime.services.JVMCICompilerFactory.AutoSelectionPrecedence;
37 import jdk.vm.ci.services.Services; 32 import jdk.vm.ci.services.Services;
38 33
39 final class HotSpotJVMCICompilerConfig { 34 final class HotSpotJVMCICompilerConfig {
40 35
41 private static class DummyCompilerFactory extends JVMCICompilerFactory implements JVMCICompiler { 36 private static class DummyCompilerFactory extends JVMCICompilerFactory implements JVMCICompiler {
59 * Factory of the selected system compiler. 54 * Factory of the selected system compiler.
60 */ 55 */
61 private static JVMCICompilerFactory compilerFactory; 56 private static JVMCICompilerFactory compilerFactory;
62 57
63 /** 58 /**
64 * Comparator that sorts available {@link JVMCICompilerFactory} objects according to their
65 * {@link JVMCICompilerFactory#getAutoSelectionRelationTo(JVMCICompilerFactory) relative}
66 * auto-selection preferences. Factories with higher preferences are sorted earlier. If a
67 */
68 static class FactoryComparator implements Comparator<JVMCICompilerFactory> {
69
70 /**
71 * Compares two compiler factories and returns -1 if {@code o1} should be auto-selected over
72 * {@code o2}, -1 if {@code o1} should be auto-selected over {@code o2} or 0 if
73 * {@code o1 == o2 || o1.getClass() == o2.getClass()}.
74 *
75 * @throws JVMCIError there is no auto-selection preference relation between {@code o1} and
76 * {@code o2}
77 */
78 public int compare(JVMCICompilerFactory o1, JVMCICompilerFactory o2) {
79 if (o1 == o2 || o1.getClass() == o2.getClass()) {
80 return 0;
81 }
82 AutoSelectionPrecedence o1Precedence = o1.getAutoSelectionRelationTo(o2);
83 AutoSelectionPrecedence o2Precedence = o2.getAutoSelectionRelationTo(o1);
84 switch (o1Precedence) {
85 case HIGHER: {
86 assert o2Precedence != o1Precedence : "auto selection precedence of " + o1 + " and " + o2 + " cannot both be " + o1Precedence;
87 return -1;
88 }
89 case LOWER: {
90 assert o2Precedence != o1Precedence : "auto selection precedence of " + o1 + " and " + o2 + " cannot both be " + o1Precedence;
91 return 1;
92 }
93 case UNRELATED: {
94 switch (o2Precedence) {
95 case HIGHER: {
96 return 1;
97 }
98 case LOWER: {
99 return -1;
100 }
101 default:
102 break;
103 }
104 }
105 }
106 // No auto-selection preference relation between o1 and o2
107 throw new JVMCIError("JVMCI compiler must be specified with the '%s' system property", Option.JVMCI_OPTION_PROPERTY_PREFIX + Option.Compiler);
108 }
109 }
110
111 /**
112 * Gets the selected system compiler factory. 59 * Gets the selected system compiler factory.
113 * 60 *
114 * @return the selected system compiler factory 61 * @return the selected system compiler factory
115 */ 62 */
116 static JVMCICompilerFactory getCompilerFactory() { 63 static JVMCICompilerFactory getCompilerFactory() {
125 } 72 }
126 if (factory == null) { 73 if (factory == null) {
127 throw new JVMCIError("JVMCI compiler '%s' not found", compilerName); 74 throw new JVMCIError("JVMCI compiler '%s' not found", compilerName);
128 } 75 }
129 } else { 76 } else {
130 // Auto selection 77 // Auto select a single available compiler
131 ArrayList<JVMCICompilerFactory> factories = new ArrayList<>();
132 for (JVMCICompilerFactory f : Services.load(JVMCICompilerFactory.class)) { 78 for (JVMCICompilerFactory f : Services.load(JVMCICompilerFactory.class)) {
133 factories.add(f); 79 if (factory == null) {
80 factory = f;
81 } else {
82 // Multiple factories seen - cancel auto selection
83 factory = null;
84 break;
85 }
134 } 86 }
135 if (!factories.isEmpty()) { 87 if (factory == null) {
136 if (factories.size() == 1) {
137 factory = factories.get(0);
138 } else {
139 Collections.sort(factories, new FactoryComparator());
140 factory = factories.get(0);
141 }
142 } else {
143 factory = new DummyCompilerFactory(); 88 factory = new DummyCompilerFactory();
144 } 89 }
145 factory.onSelection();
146 } 90 }
147 assert factory != null; 91 factory.onSelection();
148 compilerFactory = factory; 92 compilerFactory = factory;
149 } 93 }
150 return compilerFactory; 94 return compilerFactory;
151 } 95 }
152 } 96 }