comparison jvmci/jdk.vm.ci.hotspot/src/jdk/vm/ci/hotspot/HotSpotJVMCICompilerConfig.java @ 23721:c61554b3f6df

compiler selection should work without -Djvmci.Compiler (JDK-8160730)
author Doug Simon <doug.simon@oracle.com>
date Sat, 02 Jul 2016 01:00:12 +0200
parents b3a816d3b844
children 3db9e47b477a
comparison
equal deleted inserted replaced
23720:286b19732922 23721:c61554b3f6df
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
25 import jdk.vm.ci.code.CompilationRequest; 29 import jdk.vm.ci.code.CompilationRequest;
26 import jdk.vm.ci.code.CompilationRequestResult; 30 import jdk.vm.ci.code.CompilationRequestResult;
27 import jdk.vm.ci.common.JVMCIError; 31 import jdk.vm.ci.common.JVMCIError;
28 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option; 32 import jdk.vm.ci.hotspot.HotSpotJVMCIRuntime.Option;
29 import jdk.vm.ci.runtime.JVMCICompiler; 33 import jdk.vm.ci.runtime.JVMCICompiler;
30 import jdk.vm.ci.runtime.JVMCIRuntime; 34 import jdk.vm.ci.runtime.JVMCIRuntime;
31 import jdk.vm.ci.runtime.services.JVMCICompilerFactory; 35 import jdk.vm.ci.runtime.services.JVMCICompilerFactory;
36 import jdk.vm.ci.runtime.services.JVMCICompilerFactory.AutoSelectionPrecedence;
32 import jdk.vm.ci.services.Services; 37 import jdk.vm.ci.services.Services;
33 38
34 final class HotSpotJVMCICompilerConfig { 39 final class HotSpotJVMCICompilerConfig {
35 40
36 private static class DummyCompilerFactory extends JVMCICompilerFactory implements JVMCICompiler { 41 private static class DummyCompilerFactory extends JVMCICompilerFactory implements JVMCICompiler {
54 * Factory of the selected system compiler. 59 * Factory of the selected system compiler.
55 */ 60 */
56 private static JVMCICompilerFactory compilerFactory; 61 private static JVMCICompilerFactory compilerFactory;
57 62
58 /** 63 /**
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 /**
59 * Gets the selected system compiler factory. 112 * Gets the selected system compiler factory.
60 * 113 *
61 * @return the selected system compiler factory 114 * @return the selected system compiler factory
62 */ 115 */
63 static JVMCICompilerFactory getCompilerFactory() { 116 static JVMCICompilerFactory getCompilerFactory() {
65 JVMCICompilerFactory factory = null; 118 JVMCICompilerFactory factory = null;
66 String compilerName = Option.Compiler.getString(); 119 String compilerName = Option.Compiler.getString();
67 if (compilerName != null) { 120 if (compilerName != null) {
68 for (JVMCICompilerFactory f : Services.load(JVMCICompilerFactory.class)) { 121 for (JVMCICompilerFactory f : Services.load(JVMCICompilerFactory.class)) {
69 if (f.getCompilerName().equals(compilerName)) { 122 if (f.getCompilerName().equals(compilerName)) {
70 f.onSelection();
71 factory = f; 123 factory = f;
72 } 124 }
73 } 125 }
74 if (factory == null) { 126 if (factory == null) {
75 throw new JVMCIError("JVMCI compiler '%s' not found", compilerName); 127 throw new JVMCIError("JVMCI compiler '%s' not found", compilerName);
76 } 128 }
77 } else { 129 } else {
78 factory = new DummyCompilerFactory(); 130 // Auto selection
131 ArrayList<JVMCICompilerFactory> factories = new ArrayList<>();
132 for (JVMCICompilerFactory f : Services.load(JVMCICompilerFactory.class)) {
133 factories.add(f);
134 }
135 if (!factories.isEmpty()) {
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();
144 }
145 factory.onSelection();
79 } 146 }
147 assert factory != null;
80 compilerFactory = factory; 148 compilerFactory = factory;
81 } 149 }
82 return compilerFactory; 150 return compilerFactory;
83 } 151 }
84 } 152 }