Skip to content

Instantly share code, notes, and snippets.

Created September 7, 2017 01:40
Show Gist options
  • Save anonymous/20c385bfbac1dfebffd648d88761274e to your computer and use it in GitHub Desktop.
Save anonymous/20c385bfbac1dfebffd648d88761274e to your computer and use it in GitHub Desktop.
/**
*
*/
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.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 InstalledCode compileAndInstallMethod(ResolvedJavaMethod method, boolean optimization) {
CompilationIdentifier compilationId = backend.getCompilationIdentifier(method);
StructuredGraph graph = new StructuredGraph.Builder(options, debug).method(method).compilationId(compilationId).build();
PhaseSuite<HighTierContext> graphBuilderSuite = backend.getSuites().getDefaultGraphBuilderSuite();
Suites suites = backend.getSuites().getDefaultSuites(options); // !!!!!!!!!!!!!!!!!
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 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()));
} 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