author | dlong |
Tue, 24 Apr 2018 09:04:57 -0700 | |
changeset 49873 | 26ebfe8ce852 |
parent 47668 | fc4cfca10556 |
child 51206 | 14b870bda24f |
permissions | -rw-r--r-- |
42650 | 1 |
/* |
46260
5de61384fba6
8172670: AOT Platform Support for Windows and Mac OS X x64
bobv
parents:
42650
diff
changeset
|
2 |
* Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. |
42650 | 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 |
package jdk.tools.jaotc; |
|
25 |
||
26 |
import static org.graalvm.compiler.core.common.GraalOptions.GeneratePIC; |
|
27 |
import static org.graalvm.compiler.core.common.GraalOptions.ImmutableCode; |
|
28 |
import static org.graalvm.compiler.hotspot.meta.HotSpotAOTProfilingPlugin.Options.TieredAOT; |
|
29 |
||
30 |
import java.io.PrintWriter; |
|
31 |
import java.text.MessageFormat; |
|
32 |
import java.util.List; |
|
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
33 |
import java.util.ListIterator; |
42650 | 34 |
import java.util.Set; |
35 |
||
36 |
import jdk.tools.jaotc.binformat.BinaryContainer; |
|
46949 | 37 |
import jdk.tools.jaotc.Options.Option; |
42650 | 38 |
|
46640 | 39 |
import org.graalvm.compiler.api.replacements.SnippetReflectionProvider; |
42650 | 40 |
import org.graalvm.compiler.api.runtime.GraalJVMCICompiler; |
46640 | 41 |
import org.graalvm.compiler.debug.DebugContext; |
42 |
import org.graalvm.compiler.debug.DebugContext.Activation; |
|
46344 | 43 |
import org.graalvm.compiler.hotspot.CompilerConfigurationFactory; |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
44 |
import org.graalvm.compiler.hotspot.GraalHotSpotVMConfig; |
46344 | 45 |
import org.graalvm.compiler.hotspot.HotSpotGraalCompilerFactory; |
46 |
import org.graalvm.compiler.hotspot.HotSpotGraalOptionValues; |
|
42650 | 47 |
import org.graalvm.compiler.hotspot.HotSpotGraalRuntimeProvider; |
48 |
import org.graalvm.compiler.hotspot.HotSpotHostBackend; |
|
47668 | 49 |
import org.graalvm.compiler.hotspot.meta.HotSpotInvokeDynamicPlugin; |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
50 |
import org.graalvm.compiler.java.GraphBuilderPhase; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
51 |
import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; |
46344 | 52 |
import org.graalvm.compiler.options.OptionValues; |
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
53 |
import org.graalvm.compiler.phases.BasePhase; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
54 |
import org.graalvm.compiler.phases.PhaseSuite; |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
55 |
import org.graalvm.compiler.phases.tiers.HighTierContext; |
46640 | 56 |
import org.graalvm.compiler.printer.GraalDebugHandlersFactory; |
42650 | 57 |
import org.graalvm.compiler.runtime.RuntimeProvider; |
58 |
||
59 |
import jdk.vm.ci.meta.MetaAccessProvider; |
|
60 |
import jdk.vm.ci.meta.ResolvedJavaMethod; |
|
61 |
import jdk.vm.ci.runtime.JVMCI; |
|
62 |
||
46949 | 63 |
public final class Main { |
42650 | 64 |
|
46949 | 65 |
final Options options = new Options(); |
66 |
private PrintWriter log; |
|
67 |
LogPrinter printer; |
|
68 |
GraalFilters filters; |
|
42650 | 69 |
|
70 |
private static final int EXIT_OK = 0; // No errors. |
|
71 |
private static final int EXIT_CMDERR = 2; // Bad command-line arguments and/or switches. |
|
72 |
private static final int EXIT_ABNORMAL = 4; // Terminated abnormally. |
|
73 |
||
74 |
private static final String PROGNAME = "jaotc"; |
|
75 |
||
76 |
private static final String JVM_VERSION = System.getProperty("java.runtime.version"); |
|
77 |
||
78 |
public static void main(String[] args) throws Exception { |
|
79 |
Main t = new Main(); |
|
80 |
final int exitCode = t.run(args); |
|
81 |
System.exit(exitCode); |
|
82 |
} |
|
83 |
||
84 |
private int run(String[] args) { |
|
46949 | 85 |
log = new PrintWriter(System.out); |
86 |
printer = new LogPrinter(this, log); |
|
42650 | 87 |
|
88 |
try { |
|
46949 | 89 |
Options.handleOptions(this, args); |
42650 | 90 |
if (options.help) { |
91 |
showHelp(); |
|
92 |
return EXIT_OK; |
|
93 |
} |
|
94 |
if (options.version) { |
|
95 |
showVersion(); |
|
96 |
return EXIT_OK; |
|
97 |
} |
|
98 |
||
46949 | 99 |
printer.printlnInfo("Compiling " + options.outputName + "..."); |
42650 | 100 |
final long start = System.currentTimeMillis(); |
43464
f38fde4a6b52
8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
43405
diff
changeset
|
101 |
if (!run()) { |
46344 | 102 |
return EXIT_ABNORMAL; |
43464
f38fde4a6b52
8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
43405
diff
changeset
|
103 |
} |
42650 | 104 |
final long end = System.currentTimeMillis(); |
46949 | 105 |
printer.printlnInfo("Total time: " + (end - start) + " ms"); |
42650 | 106 |
|
107 |
return EXIT_OK; |
|
46949 | 108 |
} catch (Options.BadArgs e) { |
109 |
printer.reportError(e.key, e.args); |
|
42650 | 110 |
if (e.showUsage) { |
111 |
showUsage(); |
|
112 |
} |
|
113 |
return EXIT_CMDERR; |
|
114 |
} catch (Exception e) { |
|
115 |
e.printStackTrace(); |
|
116 |
return EXIT_ABNORMAL; |
|
117 |
} finally { |
|
118 |
log.flush(); |
|
119 |
} |
|
120 |
} |
|
121 |
||
122 |
@SuppressWarnings("try") |
|
43464
f38fde4a6b52
8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
43405
diff
changeset
|
123 |
private boolean run() throws Exception { |
46949 | 124 |
LogPrinter.openLog(); |
42650 | 125 |
|
126 |
try { |
|
127 |
||
46949 | 128 |
final Linker linker = new Linker(this); |
129 |
final String objectFileName = linker.objFile(); |
|
130 |
final Collector collector = new Collector(this); |
|
131 |
Set<Class<?>> classesToCompile; |
|
42650 | 132 |
|
133 |
try (Timer t = new Timer(this, "")) { |
|
46949 | 134 |
classesToCompile = collector.collectClassesToCompile(); |
135 |
printer.printInfo(classesToCompile.size() + " classes found"); |
|
42650 | 136 |
} |
137 |
||
46344 | 138 |
OptionValues graalOptions = HotSpotGraalOptionValues.HOTSPOT_OPTIONS; |
139 |
// Setting -Dgraal.TieredAOT overrides --compile-for-tiered |
|
140 |
if (!TieredAOT.hasBeenSet(graalOptions)) { |
|
141 |
graalOptions = new OptionValues(graalOptions, TieredAOT, options.tiered); |
|
142 |
} |
|
46557 | 143 |
graalOptions = new OptionValues(graalOptions, GeneratePIC, true, ImmutableCode, true); |
49873 | 144 |
GraalJVMCICompiler graalCompiler = HotSpotGraalCompilerFactory.createCompiler("JAOTC", JVMCI.getRuntime(), graalOptions, CompilerConfigurationFactory.selectFactory(null, graalOptions)); |
42650 | 145 |
HotSpotGraalRuntimeProvider runtime = (HotSpotGraalRuntimeProvider) graalCompiler.getGraalRuntime(); |
146 |
HotSpotHostBackend backend = (HotSpotHostBackend) runtime.getCapability(RuntimeProvider.class).getHostBackend(); |
|
147 |
MetaAccessProvider metaAccess = backend.getProviders().getMetaAccess(); |
|
46949 | 148 |
filters = new GraalFilters(metaAccess); |
42650 | 149 |
|
150 |
List<AOTCompiledClass> classes; |
|
151 |
||
152 |
try (Timer t = new Timer(this, "")) { |
|
46949 | 153 |
classes = collector.collectMethodsToCompile(classesToCompile, metaAccess); |
42650 | 154 |
} |
155 |
||
156 |
// Free memory! |
|
157 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
46949 | 158 |
printer.printMemoryUsage(); |
42650 | 159 |
classesToCompile = null; |
160 |
System.gc(); |
|
161 |
} |
|
162 |
||
47668 | 163 |
AOTDynamicTypeStore dynoStore = new AOTDynamicTypeStore(); |
164 |
AOTCompiledClass.setDynamicTypeStore(dynoStore); |
|
165 |
||
166 |
AOTBackend aotBackend = new AOTBackend(this, graalOptions, backend, new HotSpotInvokeDynamicPlugin(dynoStore)); |
|
46640 | 167 |
SnippetReflectionProvider snippetReflection = aotBackend.getProviders().getSnippetReflection(); |
46344 | 168 |
AOTCompiler compiler = new AOTCompiler(this, graalOptions, aotBackend, options.threads); |
42650 | 169 |
classes = compiler.compileClasses(classes); |
170 |
||
43405
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
171 |
GraalHotSpotVMConfig graalHotSpotVMConfig = runtime.getVMConfig(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
172 |
PhaseSuite<HighTierContext> graphBuilderSuite = aotBackend.getGraphBuilderSuite(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
173 |
ListIterator<BasePhase<? super HighTierContext>> iterator = graphBuilderSuite.findPhase(GraphBuilderPhase.class); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
174 |
GraphBuilderConfiguration graphBuilderConfig = ((GraphBuilderPhase) iterator.previous()).getGraphBuilderConfig(); |
1fab8e6866d7
8168792: [AOT] problems in MethodHandle with aot-compiled java.base
iveresov
parents:
42650
diff
changeset
|
175 |
|
42650 | 176 |
// Free memory! |
177 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
46949 | 178 |
printer.printMemoryUsage(); |
42650 | 179 |
aotBackend = null; |
180 |
compiler = null; |
|
181 |
System.gc(); |
|
182 |
} |
|
183 |
||
46344 | 184 |
BinaryContainer binaryContainer = new BinaryContainer(graalOptions, graalHotSpotVMConfig, graphBuilderConfig, JVM_VERSION); |
42650 | 185 |
DataBuilder dataBuilder = new DataBuilder(this, backend, classes, binaryContainer); |
46640 | 186 |
|
187 |
try (DebugContext debug = DebugContext.create(graalOptions, new GraalDebugHandlersFactory(snippetReflection)); Activation a = debug.activate()) { |
|
188 |
dataBuilder.prepareData(debug); |
|
189 |
} |
|
42650 | 190 |
|
191 |
// Print information about section sizes |
|
46949 | 192 |
printer.containersInfo(binaryContainer); |
42650 | 193 |
|
194 |
// Free memory! |
|
195 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
46949 | 196 |
printer.printMemoryUsage(); |
42650 | 197 |
backend = null; |
198 |
for (AOTCompiledClass aotCompClass : classes) { |
|
199 |
aotCompClass.clear(); |
|
200 |
} |
|
201 |
classes.clear(); |
|
202 |
classes = null; |
|
203 |
dataBuilder = null; |
|
204 |
binaryContainer.freeMemory(); |
|
205 |
System.gc(); |
|
206 |
} |
|
207 |
||
208 |
try (Timer t = new Timer(this, "Creating binary: " + objectFileName)) { |
|
46949 | 209 |
binaryContainer.createBinary(objectFileName); |
42650 | 210 |
} |
211 |
||
212 |
// Free memory! |
|
213 |
try (Timer t = options.verbose ? new Timer(this, "Freeing memory") : null) { |
|
46949 | 214 |
printer.printMemoryUsage(); |
42650 | 215 |
binaryContainer = null; |
216 |
System.gc(); |
|
217 |
} |
|
218 |
||
46949 | 219 |
try (Timer t = new Timer(this, "Creating shared library: " + linker.libFile())) { |
220 |
linker.link(); |
|
42650 | 221 |
} |
222 |
||
46949 | 223 |
printer.printVerbose("Final memory "); |
224 |
printer.printMemoryUsage(); |
|
225 |
printer.printlnVerbose(""); |
|
42650 | 226 |
|
227 |
} finally { |
|
46949 | 228 |
LogPrinter.closeLog(); |
42650 | 229 |
} |
43464
f38fde4a6b52
8169588: [AOT] jaotc --classpath option is confusing
rbackman
parents:
43405
diff
changeset
|
230 |
return true; |
42650 | 231 |
} |
232 |
||
46949 | 233 |
void handleError(ResolvedJavaMethod resolvedMethod, Throwable e, String message) { |
234 |
String methodName = JavaMethodInfo.uniqueMethodName(resolvedMethod); |
|
42650 | 235 |
|
46949 | 236 |
if (options.debug) { |
237 |
printer.printError("Failed compilation: " + methodName + ": " + e); |
|
42650 | 238 |
} |
46949 | 239 |
|
240 |
// Ignore some exceptions when meta-compiling Graal. |
|
241 |
if (GraalFilters.shouldIgnoreException(e)) { |
|
42650 | 242 |
return; |
243 |
} |
|
244 |
||
46949 | 245 |
LogPrinter.writeLog("Failed compilation of method " + methodName + message); |
42650 | 246 |
|
46949 | 247 |
if (!options.debug) { |
248 |
printer.printError("Failed compilation: " + methodName + ": " + e); |
|
42650 | 249 |
} |
250 |
||
46949 | 251 |
if (options.verbose) { |
252 |
e.printStackTrace(log); |
|
253 |
} |
|
42650 | 254 |
|
46949 | 255 |
if (options.exitOnError) { |
256 |
System.exit(1); |
|
42650 | 257 |
} |
258 |
} |
|
259 |
||
46949 | 260 |
void warning(String key, Object... args) { |
42650 | 261 |
log.println("Warning: " + MessageFormat.format(key, args)); |
262 |
log.flush(); |
|
263 |
} |
|
264 |
||
265 |
private void showUsage() { |
|
43479 | 266 |
log.println("Usage: " + PROGNAME + " <options> list"); |
42650 | 267 |
log.println("use --help for a list of possible options"); |
46949 | 268 |
log.flush(); |
42650 | 269 |
} |
270 |
||
271 |
private void showHelp() { |
|
43479 | 272 |
log.println("Usage: " + PROGNAME + " <options> list"); |
42650 | 273 |
log.println(); |
43479 | 274 |
log.println(" list A : separated list of class names, modules, jar files"); |
275 |
log.println(" or directories which contain class files."); |
|
42650 | 276 |
log.println(); |
43479 | 277 |
log.println("where options include:"); |
46949 | 278 |
for (Option o : Options.recognizedOptions) { |
42650 | 279 |
String name = o.aliases[0].substring(1); // there must always be at least one name |
280 |
name = name.charAt(0) == '-' ? name.substring(1) : name; |
|
281 |
if (o.isHidden() || name.equals("h")) { |
|
282 |
continue; |
|
283 |
} |
|
284 |
log.println(o.help); |
|
285 |
} |
|
46949 | 286 |
log.flush(); |
42650 | 287 |
} |
288 |
||
289 |
private void showVersion() { |
|
290 |
log.println(PROGNAME + " " + JVM_VERSION); |
|
291 |
} |
|
292 |
} |