1 /* 2 * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved. 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 com.oracle.graal.compiler.hsail; 25 26 import static com.oracle.graal.api.code.CodeUtil.*; 27 28 import java.util.logging.*; 29 30 import com.oracle.graal.api.code.*; 31 import com.oracle.graal.api.code.CallingConvention.*; 32 import com.oracle.graal.api.runtime.Graal; 33 import com.oracle.graal.compiler.GraalCompiler; 34 import com.oracle.graal.java.GraphBuilderConfiguration; 35 import com.oracle.graal.java.GraphBuilderPhase; 36 import com.oracle.graal.nodes.StructuredGraph; 37 import com.oracle.graal.nodes.spi.GraalCodeCacheProvider; 38 import com.oracle.graal.phases.OptimisticOptimizations; 39 import com.oracle.graal.phases.PhasePlan; 40 import com.oracle.graal.phases.PhasePlan.PhasePosition; 41 import com.oracle.graal.api.meta.*; 42 import com.oracle.graal.nodes.type.*; 43 import com.oracle.graal.graph.GraalInternalError; 44 import com.oracle.graal.debug.*; 45 import com.oracle.graal.nodes.*; 46 import com.oracle.graal.phases.*; 47 import com.oracle.graal.phases.tiers.*; 48 import com.oracle.graal.nodes.spi.Replacements; 49 import com.oracle.graal.compiler.target.Backend; 50 import com.oracle.graal.hsail.*; 51 52 import java.lang.reflect.Method; 53 54 /** 55 * Class that represents a HSAIL compilation result. Includes the compiled HSAIL code. 56 */ 57 public class HSAILCompilationResult { 58 59 private CompilationResult compResult; 60 protected static GraalCodeCacheProvider runtime = Graal.getRequiredCapability(GraalCodeCacheProvider.class); 61 protected static Replacements replacements = Graal.getRequiredCapability(Replacements.class); 62 protected static Backend backend = Graal.getRequiredCapability(Backend.class); 63 protected static SuitesProvider suitesProvider = Graal.getRequiredCapability(SuitesProvider.class); 64 private static final String propPkgName = HSAILCompilationResult.class.getPackage().getName(); 65 private static Level logLevel; 66 private static ConsoleHandler consoleHandler; 67 public static Logger logger; 68 static { 69 logger = Logger.getLogger(propPkgName); 70 logLevel = Level.FINE; 71 // This block configures the logger with handler and formatter. 72 consoleHandler = new ConsoleHandler(); 73 logger.addHandler(consoleHandler); 74 logger.setUseParentHandlers(false); 75 SimpleFormatter formatter = new SimpleFormatter() { 76 77 @SuppressWarnings("sync-override") 78 @Override 79 public String format(LogRecord record) { 80 return (record.getMessage() + "\n"); 81 } 82 }; 83 consoleHandler.setFormatter(formatter); 84 logger.setLevel(logLevel); 85 consoleHandler.setLevel(logLevel); 86 } 87 88 public static HSAILCompilationResult getHSAILCompilationResult(Method meth) { 89 ResolvedJavaMethod javaMethod = runtime.lookupJavaMethod(meth); 90 return getHSAILCompilationResult(javaMethod); 91 } 92 93 public static HSAILCompilationResult getHSAILCompilationResult(ResolvedJavaMethod javaMethod) { 94 StructuredGraph graph = new StructuredGraph(javaMethod); 95 new GraphBuilderPhase(runtime, GraphBuilderConfiguration.getEagerDefault(), OptimisticOptimizations.ALL).apply(graph); 96 return getHSAILCompilationResult(graph); 97 } 98 99 public static HSAILCompilationResult getHSAILCompilationResult(StructuredGraph graph) { 100 Debug.dump(graph, "Graph"); 101 TargetDescription target = new TargetDescription(new HSAIL(), true, 1, 0, true); 102 HSAILBackend hsailBackend = new HSAILBackend(Graal.getRequiredCapability(GraalCodeCacheProvider.class), target); 103 PhasePlan phasePlan = new PhasePlan(); 104 GraphBuilderPhase graphBuilderPhase = new GraphBuilderPhase(runtime, GraphBuilderConfiguration.getDefault(), OptimisticOptimizations.NONE); 105 phasePlan.addPhase(PhasePosition.AFTER_PARSING, graphBuilderPhase); 106 phasePlan.addPhase(PhasePosition.AFTER_PARSING, new HSAILPhase()); 107 new HSAILPhase().apply(graph); 108 CallingConvention cc = getCallingConvention(runtime, Type.JavaCallee, graph.method(), false); 109 try { 110 CompilationResult compResult = GraalCompiler.compileGraph(graph, cc, graph.method(), runtime, replacements, hsailBackend, target, null, phasePlan, OptimisticOptimizations.NONE, 111 new SpeculationLog(), suitesProvider.getDefaultSuites(), new CompilationResult()); 112 return new HSAILCompilationResult(compResult); 113 } catch (GraalInternalError e) { 114 String partialCode = hsailBackend.getPartialCodeString(); 115 if (partialCode != null && !partialCode.equals("")) { 116 logger.fine("-------------------\nPartial Code Generation:\n--------------------"); 117 logger.fine(partialCode); 118 logger.fine("-------------------\nEnd of Partial Code Generation\n--------------------"); 119 } 120 throw e; 121 } 122 } 123 124 private static class HSAILPhase extends Phase { 125 126 @Override 127 protected void run(StructuredGraph graph) { 128 for (LocalNode local : graph.getNodes(LocalNode.class)) { 129 if (local.kind() == Kind.Object) { 130 local.setStamp(StampFactory.declaredNonNull(local.objectStamp().type())); 131 } 132 } 133 } 134 } 135 136 protected HSAILCompilationResult(CompilationResult compResultInput) { 137 compResult = compResultInput; 138 } 139 140 public CompilationResult getCompilationResult() { 141 return compResult; 142 } 143 144 public String getHSAILCode() { 145 return new String(compResult.getTargetCode(), 0, compResult.getTargetCodeSize()); 146 } 147 148 public void dumpCompilationResult() { 149 logger.fine("targetCodeSize=" + compResult.getTargetCodeSize()); 150 logger.fine(getHSAILCode()); 151 } 152 153 }