Created
September 11, 2017 15:23
-
-
Save anonymous/e546ee6db622976f109f71b70ade856b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * | |
| */ | |
| package foo; | |
| import java.lang.reflect.Method; | |
| import java.util.Arrays; | |
| import org.graalvm.compiler.api.test.Graal; | |
| import org.graalvm.compiler.bytecode.BytecodeDisassembler; | |
| import org.graalvm.compiler.code.CompilationResult; | |
| import org.graalvm.compiler.core.GraalCompiler; | |
| import org.graalvm.compiler.core.common.CompilationIdentifier; | |
| import org.graalvm.compiler.core.target.Backend; | |
| import org.graalvm.compiler.debug.DebugContext; | |
| import org.graalvm.compiler.debug.DebugHandlersFactory; | |
| import org.graalvm.compiler.java.GraphBuilderPhase; | |
| import org.graalvm.compiler.lir.asm.CompilationResultBuilderFactory; | |
| import org.graalvm.compiler.lir.phases.LIRSuites; | |
| import org.graalvm.compiler.nodes.StructuredGraph; | |
| import org.graalvm.compiler.nodes.StructuredGraph.AllowAssumptions; | |
| import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration; | |
| import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.BytecodeExceptionMode; | |
| import org.graalvm.compiler.nodes.graphbuilderconf.GraphBuilderConfiguration.Plugins; | |
| import org.graalvm.compiler.nodes.graphbuilderconf.InvocationPlugins; | |
| import org.graalvm.compiler.nodes.spi.StampProvider; | |
| import org.graalvm.compiler.options.OptionValues; | |
| import org.graalvm.compiler.phases.OptimisticOptimizations; | |
| import org.graalvm.compiler.phases.PhaseSuite; | |
| import org.graalvm.compiler.phases.tiers.HighTierContext; | |
| import org.graalvm.compiler.phases.tiers.Suites; | |
| import org.graalvm.compiler.phases.util.Providers; | |
| import org.graalvm.compiler.runtime.RuntimeProvider; | |
| import jdk.vm.ci.code.CodeCacheProvider; | |
| import jdk.vm.ci.code.InstalledCode; | |
| import jdk.vm.ci.code.InvalidInstalledCodeException; | |
| import jdk.vm.ci.code.TargetDescription; | |
| import jdk.vm.ci.meta.MetaAccessProvider; | |
| import jdk.vm.ci.meta.ProfilingInfo; | |
| import jdk.vm.ci.meta.ResolvedJavaMethod; | |
| import static org.graalvm.compiler.core.common.CompilationRequestIdentifier.asCompilationRequest; | |
| import static org.graalvm.compiler.core.test.GraalCompilerTest.getInitialOptions; | |
| /** | |
| * @author hd | |
| * | |
| */ | |
| public class HelloWorld { | |
| protected final Backend backend; | |
| protected final Providers providers; | |
| protected final MetaAccessProvider metaAccess; | |
| protected final CodeCacheProvider codeCache; | |
| protected final StampProvider stampProvider; | |
| protected final TargetDescription target; | |
| protected final OptionValues options; | |
| protected final DebugContext debug; | |
| public HelloWorld() { | |
| /* Ask the hosting Java VM for the entry point object to the Graal API. */ | |
| RuntimeProvider runtimeProvider = Graal.getRequiredCapability(RuntimeProvider.class); | |
| /* | |
| * The default backend (architecture, VM configuration) that the hosting VM is running on. | |
| */ | |
| backend = runtimeProvider.getHostBackend(); | |
| /* Access to all of the Graal API providers, as implemented by the hosting VM. */ | |
| providers = backend.getProviders(); | |
| /* Some frequently used providers and configuration objects. */ | |
| metaAccess = providers.getMetaAccess(); | |
| codeCache = providers.getCodeCache(); | |
| stampProvider = providers.getStampProvider(); | |
| target = codeCache.getTarget(); | |
| options = getInitialOptions(); | |
| debug = DebugContext.create(options, DebugHandlersFactory.LOADER); | |
| } | |
| public void getMethodByteCode(ResolvedJavaMethod method) { | |
| byte[] bytecodes = method.getCode(); | |
| /* | |
| * BytecodeDisassembler shows you how to iterate bytecodes, how to access type information, and | |
| * more. | |
| */ | |
| String disassembly = new BytecodeDisassembler().disassemble(method); | |
| System.out.println(disassembly); | |
| } | |
| public void buildGraalGraph(ResolvedJavaMethod method) { | |
| StructuredGraph graph = new StructuredGraph.Builder(options, debug, AllowAssumptions.YES).method(method).build(); | |
| try (DebugContext.Scope scope = debug.scope("graph building", graph)) { | |
| Plugins plugins = new Plugins(new InvocationPlugins()); | |
| GraphBuilderConfiguration config = GraphBuilderConfiguration.getDefault(plugins).withEagerResolving(true); | |
| config = config.withBytecodeExceptionMode(BytecodeExceptionMode.OmitAll); | |
| OptimisticOptimizations optimisticOpts = OptimisticOptimizations.NONE; | |
| GraphBuilderPhase.Instance graphBuilder = new GraphBuilderPhase.Instance(metaAccess, stampProvider, null, null, config, optimisticOpts, null); | |
| graphBuilder.apply(graph); | |
| System.out.println(graph.toString()); | |
| } catch (Throwable e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| public CompilationResult compileMethod(ResolvedJavaMethod method, boolean optimization) { | |
| CompilationIdentifier compilationId = backend.getCompilationIdentifier(method); | |
| StructuredGraph graph = new StructuredGraph.Builder(options, debug).method(method).compilationId(compilationId).build(); | |
| /* | |
| * The phases used to build the graph. Usually this is just the GraphBuilderPhase. If the graph | |
| * already contains nodes, it is ignored. | |
| */ | |
| PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite(); | |
| /* | |
| * The optimization phases that are applied to the graph. This is the main configuration point for | |
| * Graal. Add or remove phases to customize your compilation. | |
| */ | |
| Suites suites = backend.getSuites().getDefaultSuites(options); // !!!!!!!!!!!!!!!!! | |
| /* | |
| * The low-level phases that are applied to the low-level representation. | |
| */ | |
| LIRSuites lirSuites = backend.getSuites().getDefaultLIRSuites(options); | |
| OptimisticOptimizations optimisticOpts; | |
| if (optimization) | |
| optimisticOpts = OptimisticOptimizations.ALL; | |
| else | |
| optimisticOpts = OptimisticOptimizations.NONE; | |
| ProfilingInfo profilingInfo = graph.getProfilingInfo(method); | |
| CompilationResult compilationResult = new CompilationResult(graph.compilationId()); | |
| CompilationResultBuilderFactory factory = CompilationResultBuilderFactory.Default; | |
| GraalCompiler.compileGraph(graph, method, providers, backend, graphBuilderSuite, optimisticOpts, | |
| profilingInfo, suites, lirSuites, compilationResult, factory); | |
| return compilationResult; | |
| // return backend.addInstalledCode(debug, method, asCompilationRequest(compilationId), | |
| // compilationResult); | |
| } | |
| /** | |
| * @param args | |
| */ | |
| public static void main(String[] args) { | |
| HelloWorld world = new HelloWorld(); | |
| Method reflectionMethod; | |
| try { | |
| Example example = new Example(); | |
| reflectionMethod = example.getClass().getDeclaredMethod("codeHoisting", Integer.TYPE, Integer.TYPE, Integer.TYPE); | |
| ResolvedJavaMethod method = world.metaAccess.lookupJavaMethod(reflectionMethod); | |
| // InstalledCode optimized = world.compileAndInstallMethod(method, true); | |
| // InstalledCode unoptimized = world.compileAndInstallMethod(method, true); | |
| // System.out.println(Arrays.equals(optimized.getCode(), unoptimized.getCode())); | |
| // System.out.println(Arrays.toString(optimized.getCode())); | |
| // System.out.println(Arrays.toString(unoptimized.getCode())); | |
| CompilationResult optimized = world.compileMethod(method, true); | |
| CompilationResult unoptimized = world.compileMethod(method, false); | |
| System.out.println(optimized.getName()); | |
| System.out.println(unoptimized.getName()); | |
| System.out.println(optimized); | |
| System.out.println(unoptimized); | |
| System.out.println(optimized.getBytecodeSize()); | |
| System.out.println(unoptimized.getBytecodeSize()); | |
| System.out.println(optimized.getTargetCodeSize()); | |
| System.out.println(unoptimized.getTargetCodeSize()); | |
| System.out.println(Arrays.equals(optimized.getTargetCode(), unoptimized.getTargetCode())); | |
| } catch (NoSuchMethodException e) { | |
| e.printStackTrace(); | |
| } catch (SecurityException e) { | |
| e.printStackTrace(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment